*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--page); color: var(--ink); font: var(--fs)/var(--lh) var(--font-ui); -webkit-text-size-adjust: 100%; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
button:focus-visible, textarea:focus-visible, a:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 6px; }
svg { width: 1.1em; height: 1.1em; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; flex: none; }
[hidden] { display: none !important; }

.shell { display: flex; height: 100dvh; }

/* ---------- sidebar ---------- */
.side { width: 250px; flex: none; background: var(--side); display: flex; flex-direction: column; padding: 18px 12px; gap: 4px; }
.brand { font-size: 1.15rem; font-weight: 700; color: var(--gold); padding: 6px 12px 14px; }
.side-item { display: flex; align-items: center; gap: 10px; width: 100%; text-align: start; padding: 4px 12px; border-radius: 10px; line-height: 1.9; }
.side-item:hover { background: var(--bubble); }
.side-spacer { flex: 1; }
.menu-btn { display: none; }
.scrim { display: none; }

/* ---------- main ---------- */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar { display: none; align-items: center; gap: 8px; padding: 8px 12px; }
.scroll { flex: 1; overflow-y: auto; overscroll-behavior: contain; }
.col { max-width: 740px; margin: 0 auto; padding: 24px 16px 12px; }

/* empty screen */
.empty { padding-top: 12vh; text-align: center; }
.empty h1 { font-size: 1.7rem; margin: 0 0 4px; color: var(--gold); }
.empty p { margin: 0 0 28px; color: var(--muted); }
.examples { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.example { padding: 2px 14px; border-radius: 12px; color: var(--ink); text-align: center; }
.example:hover { background: var(--bubble); }

/* messages */
.turn { margin-block: 0 28px; }
.user-msg { display: flex; margin-block-end: 14px; }
.user-bubble { background: var(--bubble); padding: 6px 16px; border-radius: 20px; max-width: 85%; white-space: pre-wrap; overflow-wrap: anywhere; }

/* actions row under an answer */
.actions { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; margin-block-start: 6px; color: var(--muted); }
.icon-btn { width: 36px; height: 36px; display: grid; place-items: center; border-radius: 10px; }
.icon-btn:hover { background: var(--bubble); color: var(--ink); }
.text-btn { padding: 0 10px; border-radius: 10px; font-size: .85rem; line-height: 36px; }
.text-btn:hover { background: var(--bubble); color: var(--ink); }
.text-btn[aria-pressed="true"] { color: var(--gold); font-weight: 600; }

/* states */
.state { color: var(--muted); display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.dots[hidden] { display: none; }
.steps { list-style: none; margin: 0; padding: 0; flex-basis: 100%; display: flex; flex-direction: column; gap: 6px; }
.step-row { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; animation: step-in .25s ease-out; }
.step-row.done { opacity: .75; }
.think-toggle { display: inline-flex; align-items: center; gap: 2px; font-size: .78rem; color: var(--muted); padding: 0 6px; border-radius: 8px; }
.think-toggle:hover { background: var(--bubble); color: var(--ink); }
.think-toggle svg { width: 13px; height: 13px; transition: transform .2s; }
.think-toggle[aria-expanded="true"] svg { transform: rotate(180deg); }
.step-thinking { flex-basis: 100%; margin-inline-start: 26px; padding-inline-start: 10px; border-inline-start: 2px solid var(--line);
  color: var(--muted); font-size: .85rem; line-height: 1.8; max-height: 9.5em; overflow: auto; overscroll-behavior: contain; }
.step-thinking[hidden] { display: none; }
.step-thinking p { margin: 0 0 .3em; animation: step-in .25s ease-out; }
.step-row.active .step-text { color: var(--ink); }
.step-mark { flex: none; width: 16px; height: 16px; border-radius: 50%; box-sizing: border-box; }
.step-row.active .step-mark { border: 2px solid var(--soft); border-top-color: var(--gold); animation: spin .8s linear infinite; }
.step-row.done .step-mark { background: var(--gold); position: relative; }
.step-row.done .step-mark::after { content: ""; position: absolute; inset: 3px 4px 5px; border: solid var(--page); border-width: 0 0 2px 2px; transform: rotate(-45deg); }
.state-foot { flex-basis: 100%; display: flex; align-items: center; gap: 10px; }
.trace { margin-block: 0 10px; }
.trace-head { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: .88rem; padding: 4px 8px; border-radius: 10px; }
.trace-head:hover { background: var(--bubble); color: var(--ink); }
.trace-head svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; transition: transform .2s; }
.trace.open .trace-head svg { transform: rotate(180deg); }
.trace .steps { margin-block-start: 6px; padding-inline: 8px; color: var(--muted); }
.steps[hidden] { display: none; }
.step-row.link { cursor: pointer; border-radius: 8px; padding-inline: 4px; margin-inline: -4px; }
.step-row.link:hover .step-text { text-decoration: underline; color: var(--ink); }
.step-row.link:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.step-row.link svg { width: 15px; height: 15px; color: var(--gold); }
/* A comparison's references, one line in the trace («طه ✓ · القصص ⏳»); a reference opens the drawer. */
.ref-line { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 2px; }
.ref-chip { font: inherit; color: inherit; padding: 1px 6px; border-radius: 8px; cursor: pointer; text-decoration: underline dotted; text-underline-offset: 3px; }
.ref-chip:hover { background: var(--bubble); color: var(--ink); }
.ref-chip:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.ref-chip[data-status="read"] { color: var(--ink); }
.ref-chip[data-status="empty"] { opacity: .7; }
/* A reference's ayat in the drawer: the tafsir window entry, with the ayah font and no clamp. */
.drawer .ref-span { direction: ltr; text-align: end; }
.drawer .trace-ayah .loc { direction: ltr; text-align: end; }
.drawer .trace-ayah .t { display: block; -webkit-line-clamp: unset; overflow: visible; white-space: normal;
  font-family: var(--font-ayah); font-size: 1.35rem; line-height: 2.1; }
.drawer .trace-book { margin-block: 14px; }
.drawer .trace-book h3 { margin: 0; font-size: 1rem; }
.drawer .trace-quote { display: block; width: 100%; text-align: start; margin-block: 8px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--soft); }
.drawer .trace-quote:hover { border-color: var(--gold); }
.drawer .trace-quote .loc { display: block; font-size: .8rem; color: var(--muted); }
.drawer .trace-quote .t { display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden; }
.drawer .trace-note { font-size: .8rem; margin-block-start: 18px; }
.drawer .trace-window { margin-block: 8px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--soft); }
.drawer .trace-window .loc { display: block; font-size: .8rem; color: var(--muted); }
.drawer .trace-window .t { display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden; white-space: pre-wrap; }
.drawer .trace-window.open .t { display: block; -webkit-line-clamp: unset; overflow: visible; }
.drawer .trace-window mark { background: var(--hl-quote); color: inherit; border-radius: 3px; }
.drawer .trace-window-actions { display: flex; align-items: center; gap: 8px; margin-block-start: 2px; }
.drawer .badge-quoted { font-size: .78rem; color: var(--gold); border: 1px solid var(--gold); border-radius: 999px; padding: 0 10px; line-height: 1.9; }
.drawer .badge-quoted:hover { background: var(--bubble); }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes step-in { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.dots { display: inline-flex; gap: 5px; }
.dots i { width: 7px; height: 7px; border-radius: 50%; background: var(--gold); animation: pulse 1.2s infinite ease-in-out; }
.dots i:nth-child(2) { animation-delay: .2s; }
.dots i:nth-child(3) { animation-delay: .4s; }
@keyframes pulse { 0%, 80%, 100% { opacity: .25; transform: scale(.8); } 40% { opacity: 1; transform: scale(1); } }
.slow { font-size: .85rem; }
.banner { background: var(--soft); border-radius: 12px; padding: 4px 14px; color: var(--muted); margin-block: 0 10px; font-size: .92rem; }
.banner.error { background: var(--err-bg); color: var(--err-ink); display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.banner .retry { text-decoration: underline; color: inherit; }
.notice { max-width: 740px; margin: 0 auto 8px; text-align: center; }
.refusal p { margin: 0 0 .4em; }
.refusal .follow { display: block; margin-block-start: 6px; color: var(--gold); text-decoration: underline; text-align: start; }

/* composer */
.composer-wrap { padding: 8px 16px calc(14px + env(safe-area-inset-bottom)); }
.composer { max-width: 740px; margin: 0 auto; display: flex; align-items: flex-end; gap: 6px; background: var(--side); border: 1px solid var(--line); border-radius: 28px; padding: 6px 18px 6px 8px; padding-inline: 18px 8px; }
.composer:focus-within { border-color: var(--gold); }
.composer textarea { flex: 1; resize: none; border: 0; outline: 0; background: transparent; color: inherit; font: inherit; line-height: 1.8; max-height: 9em; padding: 4px 0; min-height: 2.2em; }
.composer textarea::placeholder { color: var(--muted); }
.send { width: 40px; height: 40px; border-radius: 50%; background: var(--gold); color: var(--page); display: grid; place-items: center; flex: none; }
.send:disabled { opacity: .35; cursor: default; }
.send svg { transform: scaleX(-1); }
.counter { max-width: 740px; margin: 2px auto 0; font-size: .72rem; color: var(--muted); text-align: end; padding-inline: 18px; height: 1.2em; }

/* dialogs & drawer */
.overlay { position: fixed; inset: 0; background: var(--overlay); z-index: 40; display: flex; }
.dialog { background: var(--page); border-radius: 18px; margin: auto; width: min(460px, calc(100vw - 32px)); max-height: 86dvh; overflow: auto; padding: 18px 20px; }
.dialog h2 { margin: 0 0 6px; font-size: 1.15rem; }
.dialog label { display: flex; gap: 8px; align-items: center; }
.dialog textarea { width: 100%; margin-block-start: 10px; background: var(--side); border: 1px solid var(--line); border-radius: 12px; padding: 8px 12px; font: inherit; color: inherit; resize: vertical; }
.dialog .row { display: flex; gap: 8px; justify-content: flex-end; margin-block-start: 12px; }
.btn { padding: 2px 16px; border-radius: 12px; }
.btn.primary { background: var(--gold); color: var(--page); }
.btn.ghost:hover { background: var(--bubble); }
.toast { position: fixed; inset-inline: 0; bottom: 90px; margin-inline: auto; width: fit-content; background: var(--ink); color: var(--page); padding: 2px 16px; border-radius: 20px; z-index: 60; font-size: .9rem; }

.drawer { background: var(--page); position: fixed; top: 0; bottom: 0; left: 0; right: auto; width: min(520px, 100vw); overflow: auto; padding: 18px 22px calc(18px + env(safe-area-inset-bottom)); box-shadow: 0 0 40px #0004; }
.drawer-head { display: flex; align-items: center; justify-content: space-between; }
.drawer h2 { margin: 0; font-size: 1.15rem; }
.drawer .meta { color: var(--muted); font-size: .9rem; }
.drawer .neighbour { opacity: .45; }
.drawer .pagebreak { display: block; text-align: center; color: var(--muted); font-size: .8rem; margin-block: 6px; }
.drawer details { margin-block: 12px; color: var(--muted); font-size: .85rem; }
.drawer details dd, .drawer details dt { margin: 0; }
.drawer details code { direction: ltr; unicode-bidi: embed; font-size: .8rem; }
.drawer .cites h3 { font-size: .95rem; margin: 14px 0 2px; color: var(--muted); }

@media (max-width: 767px) {
  :root { --fs: 18px; }
  .side { position: fixed; inset-block: 0; inset-inline-start: 0; z-index: 30; transform: translateX(100%); transition: transform .2s; width: min(280px, 82vw); }
  .side.open { transform: none; }
  .scrim.show { display: block; position: fixed; inset: 0; background: var(--overlay); z-index: 25; }
  .topbar { display: flex; }
  .menu-btn { display: grid; }
  .col { padding-top: 8px; }
  .empty { padding-top: 6vh; }
  .drawer { top: auto; inset-inline: 0; width: 100%; max-height: 82dvh; border-radius: 18px 18px 0 0; }
  .overlay.sheet { align-items: flex-end; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
  .dots i { opacity: .7; }
}
