:root {
  --bg: #f7f5f0; --panel: #fff; --panel-2: #fbfaf7; --ink: #1f2420; --muted: #6f746f; --line: #e4dfd5;
  --accent: #1f5f4a; --accent-ink: #fff; --accent-soft: #e4efe9;
  --ok: #2e7d4f; --ok-soft: #e8f5ec; --warn: #a86b12; --warn-soft: #fbf2e1;
  --bad: #b93a2c; --bad-soft: #fbebe9; --info: #285c9e; --info-soft: #e8eff9;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.05);
  --radius: 12px;
  font-family: system-ui, "Segoe UI", sans-serif; color: var(--ink); background: var(--bg);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121614; --panel: #1a1f1c; --panel-2: #202622; --ink: #e6e9e6; --muted: #98a09a; --line: #2e3631;
    --accent: #4fae8a; --accent-ink: #0d1411; --accent-soft: #1f3a30;
    --ok: #5cc48a; --ok-soft: #1c3426; --warn: #e0a64b; --warn-soft: #3a2e1a;
    --bad: #ec7b6e; --bad-soft: #3d2220; --info: #7fb0ec; --info-soft: #1d2b3d;
    --shadow: 0 1px 2px rgba(0,0,0,.3);
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; display: flex; height: 100dvh; overflow: hidden; }
.ar, [lang="ar"] { font-family: "Amiri", "Traditional Arabic", "Noto Naskh Arabic", "Segoe UI", serif; }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border-radius: 8px; border: 1px solid var(--line); background: var(--panel); padding: 7px 14px; transition: background .15s, border-color .15s; }
button:hover:not(:disabled) { border-color: var(--accent); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button:disabled { opacity: .5; cursor: default; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
select, input[type="search"], input[type="password"], input[type="text"] { padding: 6px 8px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel); }
.meta { color: var(--muted); font-size: .85rem; }

/* ── Barre latérale ─────────────────────────────────────────────────────── */
#topbar { display: none; }
#sidebar { width: 280px; flex-shrink: 0; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: 14px; gap: 10px; }
.brand { display: flex; align-items: center; gap: 8px; }
.brand .ar { font-size: 1.7rem; color: var(--accent); }
.brand small { color: var(--muted); }
#conv-search { width: 100%; }
#conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
#conv-list .group { font-size: .72rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 10px 8px 4px; }
#conv-list li.item { padding: 8px 10px; border-radius: 8px; cursor: pointer; font-size: .88rem; overflow: hidden; text-overflow: ellipsis; }
#conv-list li.item:hover { background: var(--panel-2); }
#conv-list li.item.active { background: var(--accent-soft); }
#conv-list li small { display: block; color: var(--muted); font-size: .72rem; }
#conv-list .empty { color: var(--muted); font-size: .85rem; padding: 8px; }
.side-tools { display: flex; flex-direction: column; gap: 6px; border-top: 1px solid var(--line); padding-top: 10px; }
.side-tools button { text-align: left; font-size: .88rem; }

/* ── Fil de la conversation ─────────────────────────────────────────────── */
main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#thread { flex: 1; overflow-y: auto; padding: 24px max(20px, calc((100% - 880px) / 2)); scroll-behavior: smooth; }

#welcome { text-align: center; margin: 10vh auto 0; max-width: 640px; }
#welcome img { width: 72px; height: 72px; }
#welcome h1 { font-size: 3rem; color: var(--accent); margin: 4px 0; }
#welcome p { color: var(--muted); }
.steps { display: flex; justify-content: center; gap: 8px; flex-wrap: wrap; margin: 18px 0; font-size: .82rem; color: var(--muted); }
.steps span { background: var(--panel); border: 1px solid var(--line); border-radius: 20px; padding: 3px 10px; }
.examples { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 10px; }
.examples button { border-radius: 20px; font-size: .88rem; background: var(--panel); }
.examples button small { color: var(--muted); margin-inline-start: 4px; }

.turn { margin-bottom: 30px; animation: fade .25s ease-out; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } }
.user-msg { background: var(--accent-soft); border-radius: var(--radius); padding: 10px 14px; margin: 0 0 12px auto; white-space: pre-wrap; max-width: 85%; width: fit-content; }
.stage-badge { display: inline-block; font-size: .74rem; background: var(--accent); color: var(--accent-ink); border-radius: 4px; padding: 2px 8px; margin-bottom: 8px; }
details.trace { font-size: .82rem; color: var(--muted); margin-bottom: 10px; border-left: 3px solid var(--line); padding-left: 10px; }
details.trace summary { cursor: pointer; }
.trace .thinking { white-space: pre-wrap; font-style: italic; margin: 6px 0; }
.trace .tool { font-family: ui-monospace, Consolas, monospace; font-size: .76rem; margin: 2px 0; overflow-wrap: anywhere; }
.trace .tool.err { color: var(--bad); }

/* Fiche de hukm en tête du dossier */
.hukm-card { display: grid; grid-template-columns: auto 1fr; gap: 4px 14px; align-items: center; background: var(--panel); border: 1px solid var(--line); border-left: 5px solid var(--muted); border-radius: var(--radius); padding: 12px 16px; margin-bottom: 10px; box-shadow: var(--shadow); }
.hukm-card .label { font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.hukm-card .hukm { font-size: 1.35rem; font-weight: 700; unicode-bidi: plaintext; }
.hukm-card .sujet { unicode-bidi: plaintext; }
.hukm-card .regle { font-size: .82rem; color: var(--muted); unicode-bidi: plaintext; }
.hukm-card.haram { border-left-color: var(--bad); } .hukm-card.haram .hukm { color: var(--bad); }
.hukm-card.nahy { border-left-color: var(--warn); } .hukm-card.nahy .hukm { color: var(--warn); }
.hukm-card.amr { border-left-color: var(--info); } .hukm-card.amr .hukm { color: var(--info); }
.hukm-card.mubah { border-left-color: var(--ok); } .hukm-card.mubah .hukm { color: var(--ok); }

.answer { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 20px; line-height: 1.7; box-shadow: var(--shadow); overflow-wrap: break-word; }
.answer:empty { display: none; }
.answer h1, .answer h2, .answer h3, .answer h4 { color: var(--accent); margin: 20px 0 6px; font-size: 1.06rem; padding-bottom: 4px; border-bottom: 1px solid var(--line); }
.answer p, .answer li, .answer h2, .answer h3, .answer h4, .answer blockquote { unicode-bidi: isolate; }
.answer bdi { unicode-bidi: isolate; }
.answer blockquote { margin: 8px 0; padding: 6px 14px; border-inline-start: 3px solid var(--accent); background: var(--panel-2); border-radius: 0 8px 8px 0; }
.answer :is(p, li, blockquote):lang(ar), .answer .arabic { font-size: 1.15rem; }
.answer code { background: var(--panel-2); padding: 0 4px; border-radius: 4px; }
.answer hr { border: none; border-top: 1px solid var(--line); }

.ref { display: inline-block; font-size: .72rem; font-family: ui-monospace, Consolas, monospace; padding: 0 5px; margin: 0 2px; border-radius: 4px; cursor: pointer; border: 1px solid var(--line); background: var(--panel-2); direction: ltr; unicode-bidi: isolate; }
.ref:hover { border-color: var(--accent); }
.ref.verifie { border-color: var(--ok); color: var(--ok); background: var(--ok-soft); }
.ref.non_consulte { border-color: var(--warn); color: var(--warn); background: var(--warn-soft); }
.ref.introuvable { border-color: var(--bad); color: var(--bad); background: var(--bad-soft); text-decoration: line-through; }

.report { font-size: .8rem; color: var(--muted); margin-top: 8px; display: flex; flex-wrap: wrap; gap: 6px 14px; align-items: center; }
.report .warn { color: var(--warn); width: 100%; }
.actions { margin-top: 10px; display: flex; flex-wrap: wrap; gap: 8px; align-items: flex-start; }
.actions > button { font-size: .88rem; }
.error { color: var(--bad); background: var(--bad-soft); padding: 8px 12px; border-radius: 8px; margin: 8px 0; }
.loading { display: inline-flex; gap: 4px; padding: 10px 4px; }
.loading i { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); animation: blink 1s infinite; }
.loading i:nth-child(2) { animation-delay: .15s; } .loading i:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%, 100% { opacity: .2; } 50% { opacity: 1; } }

/* ── Zone de saisie ─────────────────────────────────────────────────────── */
#composer { border-top: 1px solid var(--line); background: var(--panel); padding: 10px max(20px, calc((100% - 880px) / 2)) max(10px, env(safe-area-inset-bottom)); }
#composer .row { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; }
#composer .options { flex-wrap: wrap; row-gap: 4px; }
#composer select { max-width: 280px; }
#composer textarea { flex: 1; resize: none; font-size: 1rem; padding: 10px 12px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel-2); max-height: 40vh; }
#composer textarea:focus { border-color: var(--accent); outline: none; }
#send { align-self: stretch; }
#status { font-size: .78rem; color: var(--muted); min-height: 1em; }
#explain-label { font-size: .82rem; display: flex; align-items: center; gap: 4px; white-space: nowrap; }
#explain-label.off { opacity: .45; }
#llm-state { font-size: .75rem; padding: 2px 8px; border-radius: 10px; background: var(--panel-2); color: var(--muted); white-space: nowrap; border: 1px solid var(--line); }
#llm-state.on { background: var(--accent-soft); color: var(--accent); }

/* ── Fenêtres ───────────────────────────────────────────────────────────── */
dialog { max-width: min(760px, 94vw); width: 100%; max-height: 88dvh; border: 1px solid var(--line); border-radius: 14px; padding: 18px 22px; box-shadow: 0 10px 40px rgba(0,0,0,.25); background: var(--panel); color: var(--ink); }
dialog::backdrop { background: rgba(0,0,0,.4); }
dialog .close { float: right; border: none; font-size: 1.4rem; padding: 0 6px; background: none; }
#source-body .src-ar { font-size: 1.4rem; line-height: 2.1; direction: rtl; white-space: pre-wrap; }
#source-body .src-fr { margin-top: 10px; white-space: pre-wrap; }
#source-body table { border-collapse: collapse; margin-top: 10px; font-size: .85rem; }
#source-body td { border: 1px solid var(--line); padding: 3px 8px; }

/* Carnet et méthode */
.carnet-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.carnet-actions .saved { color: var(--ok); font-size: .85rem; }
.carnet-form { width: 100%; display: flex; flex-direction: column; gap: 6px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 10px; }
.carnet-form input, .carnet-form textarea, #methode-text { padding: 7px 9px; border: 1px solid var(--line); border-radius: 8px; width: 100%; background: var(--panel); }
.etude { border-bottom: 1px solid var(--line); padding: 10px 0; }
.etude .q { font-weight: 600; }
.etude .del { float: right; font-size: .75rem; padding: 2px 8px; }
.etude .comment { white-space: pre-wrap; margin-top: 4px; }
#carnet-filter { width: 100%; margin-bottom: 6px; }

/* ── Page de connexion ──────────────────────────────────────────────────── */
body.login { align-items: center; justify-content: center; }
.login-box { background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 32px 28px; width: min(360px, 92vw); text-align: center; box-shadow: var(--shadow); }
.login-box h1 { color: var(--accent); font-size: 2.6rem; margin: 6px 0 0; }
.login-box p { color: var(--muted); margin: 4px 0 20px; }
#pin { width: 100%; font-size: 1.6rem; text-align: center; letter-spacing: .5em; padding: 10px; border-radius: 12px; }
.login-box button { width: 100%; margin-top: 14px; padding: 11px; }
#login-error { color: var(--bad); min-height: 1.3em; margin-top: 10px; font-size: .9rem; }

/* ── Mobile ─────────────────────────────────────────────────────────────── */
@media (max-width: 800px) {
  body { flex-direction: column; }
  #topbar { display: flex; align-items: center; justify-content: space-between; padding: 6px 10px; background: var(--panel); border-bottom: 1px solid var(--line); padding-top: max(6px, env(safe-area-inset-top)); }
  #topbar button { border: none; font-size: 1.3rem; padding: 4px 10px; background: none; }
  .brand-mini { font-size: 1.4rem; color: var(--accent); }
  #sidebar { position: fixed; inset: 0 auto 0 0; z-index: 20; width: min(300px, 86vw); transform: translateX(-100%); transition: transform .2s ease-out; box-shadow: 0 0 30px rgba(0,0,0,.25); }
  body.menu-open #sidebar { transform: none; }
  #scrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 10; }
  main { flex: 1; min-height: 0; }
  #thread { padding: 14px 12px; }
  #welcome { margin-top: 4vh; }
  #welcome h1 { font-size: 2.3rem; }
  .user-msg { max-width: 95%; }
  .answer { padding: 6px 14px; }
  #composer { padding-left: 10px; padding-right: 10px; }
  #composer .options select { flex: 1 1 45%; min-width: 0; max-width: none; font-size: .85rem; }
  #composer .row, #composer textarea { min-width: 0; }
  #explain-label { white-space: normal; }
  #send { padding: 7px 12px; }
  #composer textarea { font-size: 16px; } /* pas de zoom automatique sur iPhone */
  dialog { padding: 14px; }
}

/* ── Parcours de تدبّر ─────────────────────────────────────────────────── */
[hidden] { display: none !important; }
mark.only-here, mark.only-there { border-radius: 4px; padding: 0 3px; color: inherit; }
mark.only-here { background: var(--info-soft); box-shadow: inset 0 -2px 0 var(--info); }
mark.only-there { background: var(--warn-soft); box-shadow: inset 0 -2px 0 var(--warn); }
.answer h3 { font-size: 1.12rem; }
.answer hr { margin: 28px 0; border-top: 2px dashed var(--line); }
.steps span { font-family: "Amiri", "Traditional Arabic", "Segoe UI", serif; }
