/* ---------- Jetons ---------- */
:root {
  --paper: #F2F4F6;
  --surface: #FFFFFF;
  --ink: #18212B;
  --ink-2: #556270;
  --line: #DCE1E6;
  --line-2: #EAEEF1;
  --accent: #C8102E;
  --accent-ink: #FFFFFF;
  --accent-soft: #FBE4E6;
  --marker: #FFC72C;
  --marker-soft: #FFF3D2;
  --danger: #B0362C;
  --danger-soft: #FBE9E7;
  --ok: #2C7A4B;
  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-read: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --sidebar-w: 288px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #11161B; --surface: #182028; --ink: #E7ECF1; --ink-2: #9AA7B4;
    --line: #2A343E; --line-2: #212A33; --accent: #FF6B6B; --accent-ink: #241012; --accent-soft: #3B1216;
    --marker: #FFCF56; --marker-soft: #3A2E10; --danger: #FF9A8F; --danger-soft: #3A1F1C; --ok: #6CC28C;
  }
}

*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--paper); color: var(--ink); font: 15px/1.5 var(--font-ui); -webkit-font-smoothing: antialiased; }
button, input, textarea, select { font: inherit; color: inherit; }
h1, h2, h3 { line-height: 1.25; margin: 0; }
a { color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
[hidden] { display: none !important; }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; scroll-behavior: auto !important; } }

/* ---------- Boutons & champs ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 8px 14px; border: 1px solid var(--line); background: var(--surface); border-radius: 8px; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: var(--ink-2); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--accent-soft); }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.icon { padding: 6px; width: 32px; height: 32px; }
.btn svg, .icon-btn svg { width: 16px; height: 16px; flex: none; }

.field { display: grid; gap: 6px; margin-bottom: 16px; }
.field > label, .field > .label { font-weight: 600; font-size: 14px; }
.field .hint { color: var(--ink-2); font-size: 13px; margin-top: -2px; }
input[type=text], input[type=password], input[type=number], textarea, select {
  width: 100%; padding: 9px 12px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
}
textarea { resize: vertical; min-height: 84px; line-height: 1.5; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
.check { display: flex; gap: 10px; align-items: flex-start; }
.check input { margin-top: 4px; accent-color: var(--accent); }
.error-text { color: var(--danger); font-size: 14px; min-height: 1.4em; }
.muted { color: var(--ink-2); }
.chip { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12.5px; background: var(--accent-soft); color: var(--ink); }
.chip.marker { background: var(--marker-soft); box-shadow: inset 3px 0 0 var(--marker); padding-left: 11px; }
.chip.warn { background: var(--danger-soft); color: var(--danger); }
.chip.ok { color: var(--ok); }

/* ---------- Écrans d’accueil (connexion, mot de passe, avis) ---------- */
.gate { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.gate-card { width: min(460px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 32px; }
.gate-card h1 { font-size: 26px; letter-spacing: -.01em; }
.gate-card .sub { color: var(--ink-2); margin: 4px 0 24px; }
.gate-card .notice { font-family: var(--font-read); font-size: 16.5px; line-height: 1.65; margin: 16px 0 24px; }
.gate-card .btn.primary { width: 100%; padding: 11px; }
.gate-card .foot { margin-top: 18px; font-size: 13px; color: var(--ink-2); }
.mark { display: block; width: 36px; height: 36px; margin-bottom: 18px; }

/* ---------- Coquille ---------- */
.app { display: grid; grid-template-columns: var(--sidebar-w) 1fr; height: 100dvh; }
.sidebar { background: var(--surface); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.brand { display: flex; gap: 10px; align-items: center; padding: 16px 16px 8px; }
.brand .mark { width: 28px; height: 28px; margin: 0; }
.brand b { font-size: 16px; }
.brand span { display: block; font-size: 12.5px; color: var(--ink-2); line-height: 1.2; }
.side-actions { padding: 8px 12px; }
.side-actions .btn { width: 100%; justify-content: flex-start; }
.convo-list { flex: 1; overflow-y: auto; padding: 4px 8px 12px; }
.convo-list h3 { font-size: 12.5px; font-weight: 600; color: var(--ink-2); padding: 14px 8px 4px; }
.convo { display: flex; align-items: center; gap: 2px; border-radius: 8px; }
.convo a { flex: 1; min-width: 0; padding: 7px 8px; color: inherit; text-decoration: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; border-radius: 8px; }
.convo:hover, .convo.active { background: var(--accent-soft); }
.convo.active a { font-weight: 600; }
.convo .tools { display: none; }
.convo:hover .tools, .convo:focus-within .tools { display: flex; }
.icon-btn { background: none; border: 0; padding: 5px; border-radius: 6px; cursor: pointer; color: var(--ink-2); display: grid; place-items: center; }
.icon-btn:hover { color: var(--ink); background: var(--surface); }
.side-foot { border-top: 1px solid var(--line); padding: 8px 12px 12px; display: grid; gap: 2px; }
.side-foot a.navlink { display: block; padding: 7px 8px; border-radius: 8px; color: inherit; text-decoration: none; }
.side-foot a.navlink:hover, .side-foot a.navlink.active { background: var(--accent-soft); }
.who { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 8px 0; font-size: 13px; color: var(--ink-2); }
.who b { color: var(--ink); font-weight: 600; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
main { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.topbar { display: none; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--line); background: var(--surface); }
.scrim { display: none; }

/* ---------- Conversation ---------- */
.chat-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 20px; border-bottom: 1px solid var(--line); background: var(--paper); }
.chat-head h1 { font-size: 15px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-head .actions { display: flex; gap: 8px; flex: none; }
.scroll { flex: 1; overflow-y: auto; scroll-behavior: smooth; }
.thread { max-width: 780px; margin: 0 auto; padding: 24px 20px 40px; display: grid; gap: 22px; }
.thread.selecting .msg { cursor: pointer; }

.msg { position: relative; padding-left: 0; }
.msg .pick { display: none; position: absolute; left: -30px; top: 6px; }
.thread.selecting .msg .pick { display: block; }
@media (max-width: 900px) { .thread.selecting .msg .pick { left: -14px; } .thread.selecting { padding-left: 34px; } }
.pick input { width: 18px; height: 18px; accent-color: var(--accent); }
.msg.user { justify-self: end; max-width: 86%; }
.msg.user .body { background: var(--accent-soft); border-radius: 18px 18px 4px 18px; padding: 10px 16px; white-space: pre-wrap; overflow-wrap: anywhere; font-size: 15.5px; }
.msg.assistant .body { font-family: var(--font-read); font-size: 17px; line-height: 1.68; overflow-wrap: anywhere; }
.msg .meta { display: flex; gap: 10px; align-items: center; font-size: 12.5px; color: var(--ink-2); margin-top: 6px; min-height: 24px; }
.msg.user .meta { justify-content: flex-end; }
.msg .meta .btn { color: var(--ink-2); }

/* Le surligneur : ce qui est retenu comme preuve */
.msg.marked::before { content: ""; position: absolute; left: -14px; top: 2px; bottom: 2px; width: 4px; border-radius: 2px; background: var(--marker); }
.msg.user.marked::before { left: auto; right: -14px; }
.msg.selected .body { outline: 2px solid var(--marker); outline-offset: 4px; }
.episode-chip { justify-self: start; background: var(--marker-soft); border: 0; border-left: 4px solid var(--marker); padding: 4px 12px 4px 10px; border-radius: 4px 999px 999px 4px; font-size: 13px; cursor: pointer; margin-bottom: -10px; }
.episode-chip:hover { filter: brightness(.97); }

.body > :first-child { margin-top: 0; }
.body > :last-child { margin-bottom: 0; }
.body p, .body ul, .body ol, .body pre, .body blockquote, .body table { margin: 0 0 .9em; }
.body ul, .body ol { padding-left: 1.4em; }
.body li + li { margin-top: .25em; }
.body h1, .body h2, .body h3, .body h4 { font-family: var(--font-ui); margin: 1.3em 0 .5em; }
.body h1 { font-size: 1.35em; } .body h2 { font-size: 1.2em; } .body h3, .body h4 { font-size: 1.05em; }
.body blockquote { border-left: 3px solid var(--line); padding-left: 14px; color: var(--ink-2); }
.body code { font-family: var(--font-mono); font-size: .86em; background: var(--line-2); padding: .12em .38em; border-radius: 5px; }
.body pre { position: relative; background: var(--line-2); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; overflow-x: auto; }
.body pre code { background: none; padding: 0; font-size: 13.5px; line-height: 1.55; }
.body pre .copy { position: absolute; top: 6px; right: 6px; }
.body .table-wrap { overflow-x: auto; margin: 0 0 .9em; }
.body table { border-collapse: collapse; font-family: var(--font-ui); font-size: 14px; margin: 0; }
.body th, .body td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; vertical-align: top; }
.body th { background: var(--line-2); }
.cursor::after { content: ""; display: inline-block; width: 8px; height: 1.05em; margin-left: 3px; vertical-align: text-bottom; background: var(--ink-2); animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

.banner { display: flex; gap: 12px; align-items: center; justify-content: space-between; padding: 10px 14px; border-radius: 10px; background: var(--danger-soft); color: var(--danger); font-size: 14px; }
.banner.info { background: var(--accent-soft); color: var(--ink); }

.empty { min-height: 42vh; display: grid; align-content: center; gap: 10px; }
.empty h2 { font-size: 26px; letter-spacing: -.01em; }
.empty p { color: var(--ink-2); max-width: 56ch; margin: 0; }

/* ---------- Zone de saisie ---------- */
.composer-wrap { padding: 0 20px 18px; background: linear-gradient(to top, var(--paper) 70%, transparent); }
.composer { max-width: 780px; margin: 0 auto; display: flex; align-items: flex-end; gap: 8px; background: var(--surface); border: 1px solid var(--line); border-radius: 20px; padding: 8px 8px 8px 16px; }
.composer:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.composer textarea { border: 0; outline: 0; background: transparent; resize: none; min-height: 28px; max-height: 240px; padding: 6px 0; font-size: 15.5px; }
.composer textarea:focus { outline: 0; }
.composer .send { flex: none; width: 38px; height: 38px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink); cursor: pointer; display: grid; place-items: center; }
.composer .send:disabled { opacity: .35; cursor: not-allowed; }
.composer .send svg { width: 18px; height: 18px; }
.composer-note { max-width: 780px; margin: 6px auto 0; text-align: center; font-size: 12.5px; color: var(--ink-2); }
.selectbar { max-width: 780px; margin: 0 auto; display: flex; align-items: center; justify-content: space-between; gap: 12px; background: var(--marker-soft); border: 1px solid var(--marker); border-radius: 14px; padding: 10px 14px; }

/* ---------- Pièces jointes ---------- */
.attach-bar { max-width: 780px; margin: 0 auto 8px; display: flex; flex-wrap: wrap; gap: 8px; }
.attach-chip { display: flex; align-items: center; gap: 8px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 6px 8px 6px 6px; font-size: 13px; max-width: 220px; }
.attach-chip .thumb { width: 32px; height: 32px; border-radius: 6px; object-fit: cover; flex: none; background: var(--line-2); }
.attach-chip .glyph { width: 32px; height: 32px; border-radius: 6px; background: var(--line-2); display: grid; place-items: center; flex: none; color: var(--ink-2); }
.attach-chip .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attach-chip .name b { display: block; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attach-chip .size { color: var(--ink-2); font-size: 12px; }
.attach-chip .warn-dot { color: var(--danger); }
.attach-chip button { flex: none; background: none; border: 0; color: var(--ink-2); cursor: pointer; padding: 2px; border-radius: 50%; display: grid; place-items: center; }
.attach-chip button:hover { background: var(--line-2); color: var(--ink); }
.attach-chip.uploading { opacity: .6; }
.attach-btn { flex: none; width: 34px; height: 34px; border-radius: 50%; border: 0; background: transparent; color: var(--ink-2); cursor: pointer; display: grid; place-items: center; }
.attach-btn:hover { background: var(--accent-soft); color: var(--ink); }
.attach-btn:disabled { opacity: .4; cursor: not-allowed; }
.composer.dragover { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); background: var(--accent-soft); }
.msg-attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.msg.user .msg-attachments { justify-content: flex-end; }
.att-card { display: flex; align-items: center; gap: 8px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 6px 10px 6px 6px; font-size: 13px; text-decoration: none; color: inherit; max-width: 240px; }
.att-card:hover { border-color: var(--ink-2); }
.att-card img.thumb { width: 34px; height: 34px; border-radius: 6px; object-fit: cover; }
.att-card .glyph { width: 34px; height: 34px; border-radius: 6px; background: var(--line-2); display: grid; place-items: center; color: var(--ink-2); flex: none; }
.att-card .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att-image-full { max-width: min(360px, 100%); border-radius: 12px; display: block; margin-bottom: 8px; cursor: zoom-in; }

/* ---------- Pages (portfolio, admin) ---------- */
.page { overflow-y: auto; flex: 1; }
.page-inner { max-width: 1080px; margin: 0 auto; padding: 28px 24px 60px; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 22px; flex-wrap: wrap; }
.page-head h1 { font-size: 24px; letter-spacing: -.01em; }
.page-head p { color: var(--ink-2); margin: 4px 0 0; max-width: 62ch; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 22px; overflow-x: auto; }
.tabs a { padding: 9px 14px; color: var(--ink-2); text-decoration: none; border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs a.active { color: var(--ink); border-color: var(--accent); font-weight: 600; }
.stats { display: flex; gap: 28px; flex-wrap: wrap; margin-bottom: 22px; color: var(--ink-2); }
.stats b { display: block; font-size: 22px; color: var(--ink); font-weight: 600; }
.table-scroll { overflow-x: auto; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); }
table.data { width: 100%; border-collapse: collapse; font-size: 14px; }
table.data th { text-align: left; font-weight: 600; font-size: 13px; color: var(--ink-2); padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.data td { padding: 10px 12px; border-bottom: 1px solid var(--line-2); vertical-align: middle; }
table.data tr:last-child td { border-bottom: 0; }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data tr.off td { color: var(--ink-2); }
table.data .actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }
.code { font-family: var(--font-mono); font-size: 13px; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 20px; margin-bottom: 20px; }
.panel h2 { font-size: 17px; margin-bottom: 12px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 20px; }
@media (max-width: 700px) { .grid2 { grid-template-columns: 1fr; } }

.episode-card { background: var(--surface); border: 1px solid var(--line); border-left: 5px solid var(--marker); border-radius: 4px 12px 12px 4px; padding: 18px 20px; margin-bottom: 16px; }
.episode-card h2 { font-size: 18px; margin-bottom: 6px; }
.episode-card .chips { display: flex; gap: 6px; flex-wrap: wrap; margin: 8px 0 12px; }
.episode-card dl { margin: 0; display: grid; gap: 12px; }
.episode-card dt { font-weight: 600; font-size: 13.5px; color: var(--ink-2); }
.episode-card dd { margin: 2px 0 0; white-space: pre-wrap; font-family: var(--font-read); font-size: 16px; line-height: 1.6; }
.episode-card details { margin-top: 14px; border-top: 1px solid var(--line-2); padding-top: 10px; }
.episode-card summary { cursor: pointer; font-weight: 600; font-size: 14px; }
.excerpt { margin-top: 10px; display: grid; gap: 10px; }
.excerpt .x { border-radius: 10px; padding: 8px 12px; font-size: 14px; white-space: pre-wrap; overflow-wrap: anywhere; }
.excerpt .x.user { background: var(--accent-soft); }
.excerpt .x.assistant { background: var(--line-2); font-family: var(--font-read); font-size: 15px; }
.excerpt .who { font-size: 12px; color: var(--ink-2); margin-bottom: 2px; }

/* ---------- Dialogue ---------- */
dialog { border: 1px solid var(--line); border-radius: 16px; padding: 0; background: var(--surface); color: var(--ink); width: min(640px, calc(100% - 24px)); max-height: calc(100dvh - 32px); }
dialog::backdrop { background: rgba(10, 16, 22, .5); }
dialog form, dialog .dlg { padding: 22px 24px; display: block; }
dialog h2 { font-size: 19px; margin-bottom: 4px; }
dialog .lead { color: var(--ink-2); margin: 0 0 18px; }
dialog .foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
dialog.wide { width: min(820px, calc(100% - 24px)); }
.types { display: grid; gap: 6px; }
.suggestion { border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; margin-bottom: 10px; }
.suggestion h3 { font-size: 15px; margin-bottom: 4px; }
.suggestion p { margin: 6px 0 10px; font-size: 14px; color: var(--ink-2); }

#toasts { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); display: grid; gap: 8px; z-index: 50; }
.toast { background: var(--ink); color: var(--paper); padding: 9px 16px; border-radius: 10px; font-size: 14px; box-shadow: 0 6px 24px rgba(0,0,0,.25); }
.toast.error { background: var(--danger); color: #fff; }

/* ---------- Mobile ---------- */
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .topbar { display: flex; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: min(310px, 88vw); z-index: 30; transform: translateX(-102%); transition: transform .2s ease; }
  .app.nav-open .sidebar { transform: none; }
  .app.nav-open .scrim { display: block; position: fixed; inset: 0; background: rgba(10,16,22,.45); z-index: 20; }
  .chat-head { padding: 8px 12px; }
  .chat-head .btn .label { display: none; }
  .thread { padding: 18px 14px 32px; }
  .composer-wrap { padding: 0 10px 12px; }
  .msg.user { max-width: 94%; }
  .page-inner { padding: 20px 14px 48px; }
}

/* ---------- Impression du portfolio ---------- */
@media print {
  body { background: #fff; color: #000; font-size: 11pt; }
  .app { display: block; height: auto; }
  .sidebar, .topbar, .no-print, .scrim, #toasts { display: none !important; }
  main, .page { display: block; overflow: visible; }
  .page-inner { max-width: none; padding: 0; }
  .episode-card { break-inside: avoid; border-color: #999; }
  .episode-card details { display: block; }
}
