:root {
  color-scheme: light;
  --page: #f6f6f3;
  --surface: #fcfcfb;
  --surface-2: #f1f1ed;
  --sidebar: #fbfbf9;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #898781;
  --line: #e4e3dc;
  --ring: rgba(11, 11, 11, 0.1);
  --accent: #2a78d6;
  --accent-ink: #1c5cab;
  --accent-soft: #e8f1fc;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --good: #0ca30c;
  --bad: #d03b3b;
  --good-ink: #006300;
  --bad-ink: #b02c2c;
  --warn-soft: #fff6e0;
  --bad-soft: #fdeeee;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --shadow: 0 1px 2px rgba(15, 15, 10, 0.04), 0 8px 24px rgba(15, 15, 10, 0.05);
  --radius: 14px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", Menlo, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232322;
    --sidebar: #141413;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #898781;
    --line: #2c2c2a;
    --ring: rgba(255, 255, 255, 0.1);
    --accent: #3987e5;
    --accent-ink: #86b6ef;
    --accent-soft: #172a42;
    --series-1: #3987e5;
    --series-2: #d95926;
    --good-ink: #0ca30c;
    --bad-ink: #e66767;
    --warn-soft: #2d2410;
    --bad-soft: #2e1717;
    --grid: #2c2c2a;
    --axis: #383835;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.25);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232322;
  --sidebar: #141413;
  --text: #f0efec;
  --text-2: #c3c2b7;
  --muted: #898781;
  --line: #2c2c2a;
  --ring: rgba(255, 255, 255, 0.1);
  --accent: #3987e5;
  --accent-ink: #86b6ef;
  --accent-soft: #172a42;
  --series-1: #3987e5;
  --series-2: #d95926;
  --good-ink: #0ca30c;
  --bad-ink: #e66767;
  --warn-soft: #2d2410;
  --bad-soft: #2e1717;
  --grid: #2c2c2a;
  --axis: #383835;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.25);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--page);
  color: var(--text);
  font: 15px/1.55 var(--font);
  -webkit-font-smoothing: antialiased;
}
button, textarea, input { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }

/* --- Structure ------------------------------------------------------------------------------------ */
.app { display: grid; grid-template-columns: 264px minmax(0, 1fr); height: 100vh; height: 100dvh; }
.workspace { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.panes { flex: 1; display: grid; grid-template-columns: minmax(380px, 1fr) minmax(420px, 1.05fr); min-height: 0; }

/* --- Barre latérale ------------------------------------------------------------------------------- */
.sidebar {
  display: flex; flex-direction: column; gap: 6px; padding: 20px 14px 14px;
  background: var(--sidebar); border-right: 1px solid var(--line); min-height: 0; overflow-y: auto;
}
.brand { display: flex; align-items: center; gap: 12px; padding: 0 2px 18px 8px; }
.theme-toggle { margin-left: auto; }
.brand-mark {
  display: grid; place-items: center; width: 34px; height: 34px; border-radius: 10px;
  background: var(--accent); color: #fff; font-weight: 700; letter-spacing: 0.02em;
}
.brand-name { font-weight: 700; letter-spacing: 0.14em; font-size: 13px; }
.brand-sub { color: var(--muted); font-size: 12.5px; white-space: nowrap; }
.section-label { padding: 8px 10px 4px; color: var(--muted); font-size: 11.5px; font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase; }
.agent-list { display: flex; flex-direction: column; gap: 2px; }
.agent {
  display: grid; grid-template-columns: 32px minmax(0, 1fr); align-items: center; gap: 10px;
  width: 100%; padding: 9px 10px; border: 0; border-radius: 10px; background: transparent; text-align: left;
}
.agent:hover:not([aria-disabled="true"]) { background: var(--surface-2); }
.agent[aria-current="true"] { background: var(--accent-soft); }
.agent[aria-current="true"] .agent-icon { background: var(--accent); color: #fff; }
.agent[aria-disabled="true"] { cursor: default; opacity: 0.6; }
.agent-list .section-label { padding-top: 16px; }
.agent-icon {
  display: grid; place-items: center; width: 32px; height: 32px; border-radius: 9px;
  background: var(--surface-2); color: var(--text-2);
}
.agent-icon svg { width: 17px; height: 17px; }
.agent-name { display: block; font-weight: 560; font-size: 14px; line-height: 1.3; }
.agent-desc { color: var(--muted); font-size: 12.5px; line-height: 1.35; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.status-card { margin-top: auto; padding: 12px; border-radius: 12px; background: var(--surface); box-shadow: 0 0 0 1px var(--ring); font-size: 13px; }
.status-row { display: flex; align-items: center; gap: 8px; color: var(--text-2); min-width: 0; }
.status-row span:last-child { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status-row + .status-row { margin-top: 6px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.on { background: var(--good); }
.dot.off { background: var(--bad); }
.status-card .button { margin-top: 10px; width: 100%; justify-content: center; }

/* --- Boutons ---------------------------------------------------------------------------------------- */
.button {
  display: inline-flex; align-items: center; gap: 7px; padding: 8px 14px; border-radius: 10px;
  border: 1px solid transparent; background: var(--accent); color: #fff; font-weight: 560; font-size: 14px;
}
.button:hover { filter: brightness(1.05); }
.button.secondary { background: var(--surface); color: var(--text); border-color: var(--line); }
.button.ghost { background: transparent; color: var(--text-2); border-color: var(--line); }
.button.ghost:hover { background: var(--surface-2); filter: none; }
.button svg, .icon-button svg { width: 16px; height: 16px; }
.button:disabled { opacity: 0.5; cursor: default; filter: none; }
.icon-button { display: grid; place-items: center; width: 34px; height: 34px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); color: var(--text-2); }
.icon-button:hover { background: var(--surface-2); }

/* --- Barre du haut ---------------------------------------------------------------------------------- */
.topbar { display: flex; align-items: center; gap: 14px; padding: 14px 22px; border-bottom: 1px solid var(--line); background: var(--page); }
.topbar-title { flex: 1; min-width: 0; }
.topbar h1 { margin: 0; font-size: 16px; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar p { margin: 0; color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.menu-button, .pane-switch, .scrim { display: none; }
.pane-switch { gap: 2px; padding: 3px; border-radius: 10px; background: var(--surface-2); }
.pane-switch button { border: 0; background: transparent; padding: 5px 12px; border-radius: 8px; color: var(--text-2); font-size: 13.5px; }
.pane-switch button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: 0 0 0 1px var(--ring); }

/* --- Conversation ----------------------------------------------------------------------------------- */
.chat { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--line); }
.messages { flex: 1; overflow-y: auto; padding: 26px 26px 12px; display: flex; flex-direction: column; gap: 22px; }
.empty { margin: auto 0; padding: 10px 6px 40px; }
.empty h2 { margin: 0 0 6px; font-size: 26px; font-weight: 650; letter-spacing: -0.01em; }
.empty p { margin: 0 0 22px; color: var(--text-2); max-width: 52ch; }
.examples { display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.example {
  border: 1px solid var(--line); background: var(--surface); border-radius: 12px; padding: 9px 14px; text-align: left;
  color: var(--text); font-size: 14px; display: inline-flex; gap: 10px; align-items: center;
}
.example:hover { border-color: var(--accent); }
.example svg { width: 15px; height: 15px; color: var(--accent); flex: none; }
.msg-user { align-self: flex-end; max-width: 85%; background: var(--accent-soft); color: var(--text); padding: 10px 14px; border-radius: 14px 14px 4px 14px; white-space: pre-wrap; }
.turn { display: grid; grid-template-columns: 30px minmax(0, 1fr); gap: 12px; }
.avatar { width: 30px; height: 30px; border-radius: 9px; background: var(--accent); color: #fff; display: grid; place-items: center; }
.avatar svg { width: 16px; height: 16px; }
.turn-body { display: flex; flex-direction: column; gap: 12px; min-width: 0; padding-top: 3px; }
.steps { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 5px; font-size: 13.5px; color: var(--text-2); }
.steps li { display: flex; align-items: center; gap: 9px; }
.steps li.done { color: var(--muted); }
.steps .mark { width: 16px; height: 16px; display: grid; place-items: center; flex: none; }
.steps .mark svg { width: 14px; height: 14px; color: var(--good-ink); }
.steps li.failed .mark svg { color: var(--bad-ink); }
.spinner { width: 13px; height: 13px; border-radius: 50%; border: 2px solid var(--line); border-top-color: var(--accent); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.4s; } }

.report { font-size: 14.5px; line-height: 1.62; overflow-wrap: anywhere; }
.report h3 { font-size: 14px; font-weight: 650; margin: 18px 0 6px; letter-spacing: 0.01em; }
.report h3:first-child { margin-top: 0; }
.report p { margin: 0 0 10px; }
.report ul, .report ol { margin: 0 0 10px; padding-left: 20px; }
.report table { width: 100%; border-collapse: collapse; margin: 6px 0 14px; font-size: 13px; font-variant-numeric: tabular-nums; display: block; overflow-x: auto; }
.report th, .report td { padding: 6px 10px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
.report th { color: var(--text-2); font-weight: 600; }
.report code { font-family: var(--mono); font-size: 12.5px; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }
.cursor::after { content: ""; display: inline-block; width: 7px; height: 15px; margin-left: 2px; vertical-align: -2px; background: var(--accent); animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

.alert { border-radius: 12px; padding: 11px 14px; font-size: 13.5px; background: var(--bad-soft); color: var(--bad-ink); }
.alert .button { margin-top: 8px; }
.notice { font-size: 13px; color: var(--muted); }
.link-button { border: 0; background: none; padding: 0; color: var(--accent-ink); font-weight: 560; font-size: 13.5px; }

/* Carte de validation du mapping */
.card { background: var(--surface); border-radius: var(--radius); box-shadow: 0 0 0 1px var(--ring), var(--shadow); }
.mapping { padding: 16px 18px; }
.mapping-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.mapping-head h3 { margin: 0; font-size: 14.5px; font-weight: 650; }
.tag { font-size: 11.5px; font-weight: 600; color: var(--accent-ink); background: var(--accent-soft); border-radius: 999px; padding: 2px 9px; white-space: nowrap; }
.tag.ok { color: var(--good-ink); background: transparent; box-shadow: inset 0 0 0 1px currentColor; }
.kv { display: grid; grid-template-columns: 128px minmax(0, 1fr); gap: 8px 14px; margin: 0; font-size: 13.5px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; min-width: 0; }
.chips { display: flex; flex-wrap: wrap; gap: 5px; }
.chip { display: inline-flex; gap: 4px; align-items: baseline; padding: 2px 8px; border-radius: 7px; background: var(--surface-2); font-size: 12.5px; }
.chip b { font-weight: 600; }
.chip span { color: var(--muted); }
.mapping ul { margin: 6px 0 0; padding-left: 18px; font-size: 13px; color: var(--text-2); }
.mapping .block { margin-top: 12px; }
.mapping .block-title { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
.mapping .questions { background: var(--warn-soft); border-radius: 10px; padding: 8px 12px; }
.mapping .questions ul { color: var(--text); }
.mapping .errors { background: var(--bad-soft); border-radius: 10px; padding: 8px 12px; }
.mapping .errors ul { color: var(--bad-ink); }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.correction { margin-top: 12px; display: flex; flex-direction: column; gap: 8px; }
.correction textarea, .composer textarea { width: 100%; resize: none; border: 0; background: transparent; outline: none; }
.correction textarea { border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; background: var(--page); min-height: 64px; }
.mapping.decided .actions, .mapping.decided .correction { display: none; }

/* Zone de saisie */
.composer { padding: 10px 22px 16px; }
.composer-box {
  display: flex; align-items: flex-end; gap: 8px; padding: 8px 8px 8px 16px; border-radius: 16px;
  background: var(--surface); box-shadow: 0 0 0 1px var(--ring), var(--shadow);
}
.composer-box:focus-within { box-shadow: 0 0 0 2px var(--accent), var(--shadow); }
.composer textarea { padding: 7px 0; max-height: 180px; line-height: 1.45; }
.send { width: 36px; height: 36px; border-radius: 11px; border: 0; background: var(--accent); color: #fff; display: grid; place-items: center; flex: none; }
.send svg { width: 17px; height: 17px; }
.send:disabled { opacity: 0.35; cursor: default; }
.composer-hint { margin: 7px 4px 0; color: var(--muted); font-size: 12px; }

/* --- Visualisation ---------------------------------------------------------------------------------- */
.viz { overflow-y: auto; padding: 22px 24px 28px; display: flex; flex-direction: column; gap: 16px; min-width: 0; }
.viz-empty { margin: auto; text-align: center; color: var(--muted); max-width: 34ch; }
.viz-empty svg { width: 120px; height: 90px; margin-bottom: 14px; color: var(--line); }
.viz-empty h2 { color: var(--text); font-size: 16px; margin: 0 0 4px; }
.viz-head h2 { margin: 0; font-size: 16px; font-weight: 650; }
.viz-head p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; }
.kpi { padding: 14px 16px; }
.kpi-label { color: var(--muted); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi-value { font-size: 21px; font-weight: 650; letter-spacing: -0.01em; margin-top: 2px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.kpi-value .unit { font-size: 0.62em; font-weight: 600; opacity: 0.75; }
.kpi-sub { font-size: 12.5px; color: var(--text-2); margin-top: 1px; }
.kpi-sense { font-size: 16px; margin-top: 6px; }
.good { color: var(--good-ink); }
.bad { color: var(--bad-ink); }
.sense { display: inline-flex; align-items: center; gap: 5px; font-weight: 600; }
.sense svg { width: 13px; height: 13px; }
.panel { padding: 16px 18px 12px; }
.panel-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 6px; flex-wrap: wrap; }
.panel h3 { margin: 0; font-size: 14px; font-weight: 650; }
.panel .sub { color: var(--muted); font-size: 12.5px; }
.chart { width: 100%; height: 260px; }
.chart.tall { height: 300px; }
.levels { display: flex; flex-wrap: wrap; gap: 5px; }
.levels button { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 3px 10px; font-size: 12.5px; color: var(--text-2); }
.levels button[aria-pressed="true"] { border-color: var(--accent); color: var(--accent-ink); background: var(--accent-soft); }
.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
table.data th { text-align: right; color: var(--muted); font-weight: 600; font-size: 12px; padding: 6px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.data td { text-align: right; padding: 7px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.data th:first-child, table.data td:first-child { text-align: left; white-space: normal; }
table.data td small { color: var(--muted); margin-left: 4px; }
.flags { margin: 0; padding-left: 18px; font-size: 13.5px; color: var(--text-2); }
.flags li + li { margin-top: 4px; }
details.queries summary { cursor: pointer; color: var(--text-2); font-size: 13.5px; font-weight: 560; }
details.queries pre { margin: 10px 0 0; padding: 10px 12px; background: var(--surface-2); border-radius: 10px; font: 11.5px/1.5 var(--mono); white-space: pre-wrap; overflow-wrap: anywhere; max-height: 260px; overflow-y: auto; }

/* --- Écrans plus étroits ---------------------------------------------------------------------------- */
@media (max-width: 1180px) {
  .panes { grid-template-columns: minmax(0, 1fr); }
  .pane-switch { display: flex; }
  .chat { border-right: 0; }
  .app[data-pane="chat"] .viz, .app[data-pane="viz"] .chat { display: none; }
}
@media (max-width: 820px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: min(300px, 86vw); z-index: 20; transform: translateX(-102%); transition: transform 0.2s ease; box-shadow: var(--shadow); }
  .app.menu-open .sidebar { transform: none; }
  .app.menu-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, 0.3); z-index: 10; }
  .menu-button { display: grid; }
  .topbar { padding: 10px 16px; gap: 10px; flex-wrap: wrap; }
  .pane-switch { order: 3; flex-basis: 100%; }
  .pane-switch button { flex: 1; }
  .topbar p { display: none; }
  #new-chat .label { display: none; }
  .messages { padding: 18px 16px 8px; }
  .composer { padding: 8px 16px 14px; }
  .viz { padding: 16px; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kv { grid-template-columns: minmax(0, 1fr); gap: 2px; }
  .kv dd { margin-bottom: 8px; }
}
