:root {
  --bg: #f5f2ec;
  --surface: #fffdf9;
  --surface-2: #efeae1;
  --ink: #1c1917;
  --ink-2: #57534e;
  --line: #e2dbcf;
  --accent: #c2410c;
  --accent-ink: #fff;
  --accent-soft: #fbe6da;
  --ok: #15803d;
  --ok-soft: #dcfce7;
  --err: #b91c1c;
  --err-soft: #fee2e2;
  --shadow: 0 1px 2px rgba(28, 25, 23, .06), 0 6px 24px rgba(28, 25, 23, .06);
  --radius: 14px;
  --serif: 'Fraunces', Georgia, serif;
  --sans: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171412;
    --surface: #211d1a;
    --surface-2: #2b2622;
    --ink: #f3eee7;
    --ink-2: #a8a29e;
    --line: #3a332d;
    --accent: #f97316;
    --accent-ink: #1c1917;
    --accent-soft: #3b2415;
    --ok: #4ade80;
    --ok-soft: #14301f;
    --err: #f87171;
    --err-soft: #3a1717;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 28px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font: 15px/1.55 var(--sans); -webkit-font-smoothing: antialiased; }
[hidden] { display: none !important; }
h1, h2 { font-family: var(--serif); font-weight: 650; letter-spacing: -.01em; margin: 0; }
h1 { font-size: clamp(26px, 4vw, 36px); line-height: 1.15; }
h2 { font-size: 18px; margin-bottom: 10px; }
.muted { color: var(--ink-2); }
.small { font-size: 13px; }
button, input, select, textarea { font: inherit; color: inherit; }

/* ---------- Composants ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid transparent; border-radius: 10px; padding: 9px 16px; font-weight: 600; cursor: pointer; text-decoration: none; transition: transform .08s, background .15s, opacity .15s; }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; border-color: var(--line); color: var(--ink); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.big { padding: 12px 22px; font-size: 16px; }
.btn.small { padding: 6px 10px; font-size: 13px; }
.btn[disabled], .btn.disabled { opacity: .5; pointer-events: none; }
.link { background: none; border: 0; padding: 0; color: var(--ink-2); cursor: pointer; text-decoration: underline; text-underline-offset: 3px; }
.link:hover { color: var(--ink); }
.link.danger { color: var(--err); }
input, select, textarea { width: 100%; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; outline: none; transition: border-color .15s, box-shadow .15s; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { resize: vertical; min-height: 90px; }
label { display: grid; gap: 6px; font-weight: 500; font-size: 14px; }
.form-error { color: var(--err); min-height: 1em; margin: 8px 0 0; font-size: 14px; }
.form-error:empty { display: none; }
.badge { display: inline-block; font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 99px; background: var(--surface-2); color: var(--ink-2); white-space: nowrap; }
.badge.done { background: var(--ok-soft); color: var(--ok); }
.badge.running, .badge.queued { background: var(--accent-soft); color: var(--accent); }
.badge.error, .badge.cancelled { background: var(--err-soft); color: var(--err); }
.chip { border: 1px solid var(--line); background: var(--surface); border-radius: 99px; padding: 6px 12px; cursor: pointer; font-size: 13px; }
.chip:hover { border-color: var(--accent); color: var(--accent); }
.spinner { width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--accent-soft); border-top-color: var(--accent); animation: spin .8s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }

.brand { display: flex; align-items: center; gap: 10px; font-family: var(--serif); font-weight: 650; font-size: 20px; }
.brand-mark { width: 22px; height: 28px; border-radius: 4px; background: var(--accent); position: relative; box-shadow: 3px 3px 0 var(--accent-soft); }
.brand-mark::after { content: ''; position: absolute; inset: 8px 5px auto; height: 9px; border-top: 2px solid var(--accent-ink); border-bottom: 2px solid var(--accent-ink); opacity: .85; }

/* ---------- Connexion ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(380px, 100%); display: grid; gap: 14px; background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 32px 28px; box-shadow: var(--shadow); }
.login-card .muted { margin: -6px 0 6px; }

/* ---------- Barre du haut ---------- */
.topbar { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 24px; padding: 12px clamp(16px, 4vw, 40px); background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); }
.tabs { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 12px; }
.tab { border: 0; background: transparent; padding: 7px 16px; border-radius: 9px; cursor: pointer; font-weight: 600; color: var(--ink-2); }
.tab[aria-selected="true"] { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.count { font-size: 12px; color: var(--ink-2); font-weight: 500; }
.who { margin-left: auto; display: flex; gap: 14px; align-items: center; font-size: 14px; color: var(--ink-2); }

.page { max-width: 1180px; margin: 0 auto; padding: clamp(20px, 4vw, 44px) clamp(16px, 4vw, 40px) 60px; }

/* ---------- Generer ---------- */
#tab-generate { max-width: 820px; }
.gen-head { margin-bottom: 22px; }
.gen-head p { margin: 8px 0 0; }
.gen-card { padding: 18px; display: grid; gap: 14px; }
.gen-card textarea { font-size: 16px; border: 0; padding: 6px; box-shadow: none; min-height: 150px; }
.gen-card textarea:focus { box-shadow: none; }
.gen-row { display: flex; gap: 14px; align-items: end; flex-wrap: wrap; border-top: 1px solid var(--line); padding-top: 14px; }
.field { width: 190px; }
.dropzone { flex: 1; min-width: 220px; display: flex; align-items: center; gap: 12px; flex-wrap: wrap; border: 1.5px dashed var(--line); border-radius: 10px; padding: 8px 10px; transition: border-color .15s, background .15s; }
.dropzone.over { border-color: var(--accent); background: var(--accent-soft); }
.dropzone.small { padding: 6px; }
.file-chips { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.file-chips:empty { display: none; }
.file-chips li { display: flex; align-items: center; gap: 6px; background: var(--surface-2); border-radius: 8px; padding: 4px 6px 4px 10px; font-size: 13px; max-width: 100%; }
.file-chips li span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-chips button { border: 0; background: none; cursor: pointer; color: var(--ink-2); font-size: 16px; line-height: 1; padding: 0 4px; }
.gen-actions { display: flex; justify-content: flex-end; }
.ideas { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 18px; }

/* ---------- Bibliotheque ---------- */
.lib-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.lib-head input { width: min(280px, 100%); }
.lib-table { overflow: hidden; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-2); font-weight: 600; padding: 12px 14px; border-bottom: 1px solid var(--line); }
td { padding: 10px 14px; border-bottom: 1px solid var(--line); vertical-align: middle; }
tbody tr { cursor: pointer; transition: background .12s; }
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.col-thumb { width: 64px; }
.thumb { width: 44px; height: 58px; border-radius: 4px; border: 1px solid var(--line); background: var(--surface-2) center/cover no-repeat; display: block; }
.thumb.placeholder { display: grid; place-items: center; }
.doc-name { font-weight: 600; }
.doc-sub { font-size: 13px; color: var(--ink-2); }
.row-actions { text-align: right; white-space: nowrap; }
.empty { text-align: center; padding: 40px 16px; color: var(--ink-2); margin: 0; }

/* ---------- Detail ---------- */
.back { margin-bottom: 18px; display: inline-block; }
.doc-layout { display: grid; grid-template-columns: 1fr 320px; gap: 28px; align-items: start; }
.doc-title-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
#doc-title { cursor: text; border-radius: 6px; outline: none; }
#doc-title[contenteditable="true"] { box-shadow: 0 0 0 2px var(--accent); padding: 0 6px; }
#doc-meta { margin: 6px 0 18px; }
.progress { padding: 16px 18px; margin-bottom: 18px; }
.progress-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.progress-head .link { margin-left: auto; }
.log { margin: 12px 0 0; padding: 0 0 0 26px; max-height: 260px; overflow: auto; font-size: 14px; color: var(--ink-2); display: flex; flex-direction: column-reverse; }
.log li { padding: 2px 0; }
.log li:first-child { color: var(--ink); font-weight: 500; }
.alert { background: var(--err-soft); color: var(--err); padding: 12px 16px; border-radius: 10px; margin-bottom: 18px; display: flex; gap: 12px; align-items: flex-start; justify-content: space-between; }
.alert p { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.alert .btn { flex: none; color: var(--ink); background: var(--surface); }
.summary { background: var(--surface); border-left: 3px solid var(--accent); padding: 12px 16px; border-radius: 0 10px 10px 0; margin: 0 0 22px; white-space: pre-wrap; }
.pages { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 18px; }
.page-thumb { margin: 0; cursor: zoom-in; }
.page-thumb img { width: 100%; display: block; border-radius: 4px; background: #fff; box-shadow: var(--shadow); border: 1px solid var(--line); }
.page-thumb figcaption { text-align: center; font-size: 12px; color: var(--ink-2); margin-top: 6px; }
.doc-side { display: grid; gap: 16px; position: sticky; top: 84px; }
.side-card { padding: 16px; display: grid; gap: 10px; }
#doc-actions { grid-template-columns: 1fr 1fr; }
.versions { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; max-height: 320px; overflow: auto; }
.versions li { padding: 8px; border-radius: 8px; display: grid; gap: 2px; font-size: 14px; }
.versions li.selected { background: var(--surface-2); }
.versions li[data-n] { cursor: pointer; }
.versions li[data-n]:hover { background: var(--surface-2); }
.v-head { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.v-instr { color: var(--ink-2); font-size: 13px; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
#doc-delete { justify-self: start; font-size: 14px; }

dialog { border: 0; padding: 0; background: transparent; max-width: 95vw; max-height: 95vh; }
dialog::backdrop { background: rgba(0, 0, 0, .7); }
dialog img { max-width: 95vw; max-height: 88vh; display: block; background: #fff; border-radius: 4px; }
dialog .btn { margin: 10px auto 0; display: flex; background: var(--surface); }

@media (max-width: 860px) {
  .doc-layout { grid-template-columns: 1fr; }
  .doc-side { position: static; }
}
@media (max-width: 600px) {
  .topbar { flex-wrap: wrap; gap: 10px; }
  .topbar .brand { font-size: 17px; }
  .tabs { order: 3; width: 100%; }
  .tab { flex: 1; }
  .hide-sm { display: none; }
  .field { width: 100%; }
  .pages { grid-template-columns: repeat(2, 1fr); gap: 12px; }
  td, th { padding: 10px 8px; }
}
