:root {
  --bg: #f7f7f5;
  --surface: #ffffff;
  --text: #1d1d1b;
  --muted: #6b6b66;
  --border: #e2e2dd;
  --accent: #2f5d50;
  --accent-text: #ffffff;
  --user: #e8efe9;
  --fact: #fff6dc;
  --fact-border: #e9d79a;
  --green: #2e8b57;
  --orange: #d98a1c;
  --red: #c0392b;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161716;
    --surface: #1f201f;
    --text: #e8e8e4;
    --muted: #9a9a94;
    --border: #333431;
    --accent: #6fb39d;
    --accent-text: #0f1a16;
    --user: #253229;
    --fact: #33301f;
    --fact-border: #6b5f2c;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
button, textarea, input { font: inherit; color: inherit; }
button {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
}
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.link, a.link { border: none; background: none; padding: 4px 6px; color: var(--muted); text-decoration: none; }
.muted { color: var(--muted); }
.error { color: var(--red); }

.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--surface);
}
.topbar h1 { font-size: 17px; margin: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar form { margin: 0; }

.login { max-width: 320px; margin: 18vh auto; padding: 0 16px; text-align: center; }
.login form { display: flex; flex-direction: column; gap: 10px; }
.login input { padding: 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); }

.home { max-width: 720px; margin: 24px auto; padding: 0 16px; }
.theses { list-style: none; padding: 0; margin: 20px 0; }
.theses li a {
  display: flex; flex-direction: column; padding: 12px 14px; margin-bottom: 8px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  color: inherit; text-decoration: none;
}
.t-title { font-weight: 600; }
.t-meta { color: var(--muted); font-size: 14px; }

.stages { display: flex; gap: 4px; padding: 8px 16px; background: var(--surface); border-bottom: 1px solid var(--border); }
.stage { flex: 1; font-size: 12px; color: var(--muted); text-align: center; }
.stage::before { content: ""; display: block; height: 4px; border-radius: 2px; background: var(--border); margin-bottom: 3px; }
.stage.done::before { background: var(--accent); opacity: .45; }
.stage.current::before { background: var(--accent); }
.stage.current { color: var(--text); font-weight: 600; }

.layout { display: flex; height: calc(100vh - 100px); height: calc(100dvh - 100px); }
.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 760px; padding: 10px 14px; border-radius: 12px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.assistant { background: var(--surface); border: 1px solid var(--border); align-self: flex-start; }
.msg.user { background: var(--user); align-self: flex-end; }
.msg.fact { background: var(--fact); border: 1px solid var(--fact-border); align-self: flex-start; font-size: 15px; }
.msg.fact::before { content: "Fact · documentaliste"; display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.msg.error { border: 1px solid var(--red); color: var(--red); background: transparent; }
.stage-sep { align-self: center; font-size: 12px; color: var(--muted); }
.status { padding: 4px 16px; font-size: 13px; color: var(--muted); }

.assistants { padding: 10px 16px; border-top: 1px solid var(--border); background: var(--fact); font-size: 14px; display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.assistants p { margin: 0; width: 100%; }
.decision { padding: 10px 16px; border-top: 1px solid var(--border); display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.decision p { margin: 0; width: 100%; }

.composer { border-top: 1px solid var(--border); padding: 10px 16px; background: var(--surface); }
.composer textarea {
  width: 100%; resize: vertical; min-height: 48px; max-height: 40vh;
  border: 1px solid var(--border); border-radius: 8px; padding: 8px; background: var(--bg);
}
.composer.fact-mode textarea { border-color: var(--fact-border); background: var(--fact); }
.actions { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.actions #btn-send { margin-left: auto; }
#btn-fact.active { background: var(--fact); border-color: var(--fact-border); }

.panel { width: 380px; border-left: 1px solid var(--border); background: var(--surface); display: flex; flex-direction: column; }
.tabs { display: flex; border-bottom: 1px solid var(--border); }
.tabs button { flex: 1; border: none; border-radius: 0; background: none; padding: 10px; color: var(--muted); }
.tabs button.active { color: var(--text); box-shadow: inset 0 -2px 0 var(--accent); }
.tab { flex: 1; overflow-y: auto; padding: 12px 14px; font-size: 14px; }
.usage { border-top: 1px solid var(--border); padding: 8px 14px; font-size: 12px; color: var(--muted); }
.usage table { width: 100%; border-collapse: collapse; }
.usage td { padding: 1px 4px; }
.usage td:not(:first-child) { text-align: right; }

.f-section { margin-bottom: 14px; }
.f-section h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0 0 6px; }
.f-item { display: flex; gap: 6px; align-items: flex-start; padding: 4px 0; }
.f-item .txt { flex: 1; white-space: pre-wrap; }
.f-item .del { padding: 0 6px; font-size: 13px; }
.badge { font-size: 11px; padding: 1px 6px; border-radius: 10px; border: 1px solid var(--border); color: var(--muted); white-space: nowrap; }
.dot { width: 10px; height: 10px; border-radius: 50%; margin-top: 6px; flex: none; background: var(--border); }
.dot.vert { background: var(--green); } .dot.orange { background: var(--orange); } .dot.rouge { background: var(--red); }

.dq { margin-bottom: 14px; }
.dq h4 { margin: 0 0 6px; font-size: 14px; }
.dq .rows { display: flex; gap: 8px; overflow-x: auto; }
.dq .row { flex: 1 0 160px; border: 1px solid var(--border); border-radius: 8px; padding: 8px; }
.dq .val { font-weight: 600; }
.dq .src { color: var(--muted); font-size: 12px; overflow-wrap: anywhere; }
.dq.contradiction .rows .row { border-color: var(--orange); }

dialog { width: min(760px, calc(100vw - 32px)); max-height: 85vh; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); color: var(--text); padding: 0; }
dialog::backdrop { background: rgba(0, 0, 0, .4); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; padding: 10px 16px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); }
.dialog-head a { color: var(--accent); margin-right: 8px; }
.md { padding: 8px 20px 20px; }
.md h1 { font-size: 20px; } .md h2 { font-size: 16px; margin-top: 18px; }

@media (max-width: 860px) {
  .panel { position: fixed; inset: 100px 0 0 0; width: auto; z-index: 5; border-left: none; }
  .panel:not(.open) { display: none; }
  .stage span { display: none; }
  .stage.current span { display: inline; }
}
@media (min-width: 861px) {
  #toggle-panel { display: none; }
}

.grille { font-size: 13px; margin: 12px 0 2px; }
