:root {
  --paper: #f2f4f6;
  --sheet: #ffffff;
  --ink: #18212c;
  --ink-2: #5a6573;
  --rule: #dde2e8;
  --brand: #0f4c5c;
  --brand-ink: #ffffff;
  --user: #0f4c5c;
  --user-ink: #ffffff;
  --error-bg: #fbeaea;
  --error-ink: #8f1d1d;
  /* Section tabs, one per handbook section — the colour tells you where an answer came from. */
  --s01: #2f6db0;
  --s02: #7149b0;
  --s03: #2c8259;
  --s04: #b07415;
  --s01-bg: #e7eff9;
  --s02-bg: #efe9f8;
  --s03-bg: #e5f3ec;
  --s04-bg: #f8efdf;
  --vh: 100dvh;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --serif: "New York", ui-serif, "Songti SC", Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "PingFang SC", "Segoe UI", "Noto Sans SC", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #0f1418;
    --sheet: #161d23;
    --ink: #e6e9ec;
    --ink-2: #93a0ad;
    --rule: #26313a;
    --brand: #4fa3b5;
    --brand-ink: #071216;
    --user: #1d4a56;
    --user-ink: #eef6f8;
    --error-bg: #3a1c1c;
    --error-ink: #f4a9a9;
    --s01: #79a9e0;
    --s02: #a98be0;
    --s03: #6cc297;
    --s04: #e0aa55;
    --s01-bg: #182638;
    --s02-bg: #221b33;
    --s03-bg: #142a20;
    --s04-bg: #2e2312;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: var(--vh); overflow: hidden; }
body {
  background: var(--paper);
  color: var(--ink);
  font: 16px/1.65 var(--sans);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
button, input, textarea { font: inherit; color: inherit; }
/* iOS Safari zooms into any focused field under 16px. */
input, textarea, select { font-size: 16px; }
/* No double-tap zoom. */
html, body, button, a, summary { touch-action: manipulation; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
#app { height: 100%; }
.muted { color: var(--ink-2); }
.center { display: grid; place-items: center; height: 100%; padding: 24px; }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

/* ---------- buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 1px solid var(--rule); background: var(--sheet); border-radius: 10px; padding: 10px 14px;
}
.btn svg { width: 18px; height: 18px; flex: none; }
.btn.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); font-weight: 600; }
.btn.block { width: 100%; margin-top: 18px; padding: 13px 14px; }
.btn.danger { color: var(--error-ink); }
.btn:disabled { opacity: 0.5; cursor: default; }
.icon-btn {
  width: 40px; height: 40px; display: grid; place-items: center; border-radius: 10px;
  background: transparent; border: none; color: var(--ink);
}
.icon-btn:active { background: var(--rule); }
.icon-btn svg { width: 22px; height: 22px; }
.link { background: none; border: none; padding: 0; color: var(--brand); font-weight: 500; }

/* ---------- auth screens */
.auth { height: 100%; overflow-y: auto; display: grid; place-items: center; padding: calc(24px + var(--safe-top)) 16px calc(24px + var(--safe-bottom)); }
.card { width: 100%; max-width: 400px; }
.card .mark { display: flex; gap: 5px; margin-bottom: 22px; }
.card .mark span { width: 30px; height: 6px; border-radius: 3px; }
.card h1 { font: 600 30px/1.2 var(--serif); margin: 0 0 6px; }
.card .sub { margin: 0 0 8px; color: var(--ink-2); }
.card label { display: block; font-size: 14px; color: var(--ink-2); margin: 16px 0 6px; }
.card input {
  width: 100%; padding: 12px 13px; border-radius: 10px; border: 1px solid var(--rule); background: var(--sheet);
}
.links { display: flex; justify-content: space-between; gap: 12px; margin-top: 22px; font-size: 15px; }
.code {
  font: 600 34px/1.2 ui-monospace, "SF Mono", Menlo, monospace; letter-spacing: 3px; text-align: center;
  background: var(--sheet); border: 1px solid var(--rule); border-radius: 12px; padding: 20px 8px; margin: 16px 0 8px;
}
.notice { border-radius: 10px; padding: 11px 13px; margin-top: 14px; font-size: 15px; }
.notice.error { background: var(--error-bg); color: var(--error-ink); }
.notice.info { background: var(--s03-bg); }

/* ---------- shell */
.shell { display: grid; grid-template-columns: 290px 1fr; height: 100%; }
.side {
  background: var(--sheet); border-right: 1px solid var(--rule); display: flex; flex-direction: column; min-height: 0;
  padding: calc(14px + var(--safe-top)) 12px calc(12px + var(--safe-bottom));
}
.side .brand { display: flex; align-items: center; gap: 10px; padding: 4px 6px 14px; font: 600 18px/1.2 var(--serif); }
.side .brand img { width: 30px; height: 30px; border-radius: 8px; }
.side .new { width: 100%; justify-content: flex-start; }
.side .read { margin-top: 8px; }
.side .new.active { border-color: var(--brand); color: var(--brand); font-weight: 600; }
.side .group { font-size: 13px; color: var(--ink-2); padding: 16px 8px 6px; }
.convs { flex: 1; overflow-y: auto; min-height: 0; margin: 0 -4px; padding: 0 4px; }
.conv {
  display: flex; align-items: center; gap: 4px; padding: 9px 10px; border-radius: 9px; cursor: pointer; font-size: 15px;
}
.conv span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv:hover { background: var(--paper); }
.conv.active { background: var(--paper); font-weight: 600; }
.conv .del { opacity: 0; background: none; border: none; color: var(--ink-2); padding: 2px 6px; border-radius: 6px; }
.conv:hover .del, .conv.active .del { opacity: 1; }
.usage { font-size: 12px; padding: 10px 6px 12px; border-top: 1px solid var(--rule); }
.usage-row { display: flex; justify-content: space-between; gap: 8px; line-height: 1.5; }
.meter { height: 5px; border-radius: 3px; background: var(--rule); margin: 5px 0 6px; overflow: hidden; }
.meter i { display: block; height: 100%; background: var(--s03); border-radius: 3px; }
.meter.warn i { background: var(--s04); }
.msg.assistant .meta { font-size: 12px; color: var(--ink-2); margin-top: 8px; }
.side .foot { display: flex; gap: 8px; border-top: 1px solid var(--rule); padding-top: 12px; }
.side .foot .btn { flex: 1; font-size: 14px; padding: 9px 10px; }

.scrim { display: none; }
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; height: 100%; }

/* Sticky header: in normal flow above the scrolling thread, so it never scrolls away. */
.topbar {
  position: sticky; top: 0; z-index: 3; flex: none;
  display: flex; align-items: center; gap: 6px;
  padding: calc(6px + var(--safe-top)) 8px 6px;
  background: color-mix(in srgb, var(--paper) 86%, transparent);
  -webkit-backdrop-filter: saturate(1.4) blur(14px); backdrop-filter: saturate(1.4) blur(14px);
  border-bottom: 1px solid var(--rule);
}
.topbar .title {
  flex: 1; min-width: 0; text-align: center; font-weight: 600; font-size: 16px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.topbar .menu { visibility: hidden; }

.thread { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 24px 20px 16px; -webkit-overflow-scrolling: touch; }
.thread-inner { max-width: 720px; margin: 0 auto; }

/* ---------- empty state */
.empty { padding: 4vh 0 8px; }
.empty h2 { font: 600 28px/1.25 var(--serif); margin: 0 0 6px; }
.empty .lede { color: var(--ink-2); margin: 0 0 22px; }
.tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 26px; }
.tab {
  border-radius: 10px; padding: 10px 12px; font-size: 14px; line-height: 1.35; border-left: 4px solid;
}
.tab b { display: block; font-size: 12px; font-weight: 600; opacity: 0.85; }
.examples-title { font-size: 14px; color: var(--ink-2); margin: 0 0 6px; }
.examples { border-top: 1px solid var(--rule); }
.examples button {
  display: flex; width: 100%; text-align: left; gap: 10px; align-items: baseline;
  background: none; border: none; border-bottom: 1px solid var(--rule); padding: 13px 2px;
}
.examples button::after { content: ""; margin-left: auto; width: 7px; height: 7px; flex: none; border-right: 2px solid var(--ink-2); border-top: 2px solid var(--ink-2); transform: rotate(45deg); align-self: center; }
.examples button:active { background: var(--sheet); }

/* ---------- messages */
.msg { margin: 0 0 26px; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble {
  background: var(--user); color: var(--user-ink); border-radius: 18px 18px 6px 18px; padding: 10px 15px;
  max-width: 86%; white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.assistant .body { overflow-wrap: anywhere; }
.msg.assistant .body > :first-child { margin-top: 0; }
.msg.assistant .body :is(p, ul, ol, table, blockquote) { margin: 0 0 12px; }
.msg.assistant .body :is(h1, h2, h3, h4) { font: 600 17px/1.4 var(--sans); margin: 18px 0 8px; }
.msg.assistant .body ul, .msg.assistant .body ol { padding-left: 22px; }
.msg.assistant .body li { margin: 3px 0; }
.msg.assistant .body strong { font-weight: 650; }
.msg.assistant .body blockquote {
  margin-left: 0; padding: 10px 14px; border-left: 3px solid var(--brand); background: var(--sheet);
  border-radius: 0 10px 10px 0; font-family: var(--serif);
}
.msg.assistant .body blockquote p:last-child { margin-bottom: 0; }
.msg.assistant .body table { border-collapse: collapse; display: block; overflow-x: auto; font-size: 14px; }
.msg.assistant .body th { text-align: left; background: var(--sheet); }
.msg.assistant .body th, .msg.assistant .body td { border: 1px solid var(--rule); padding: 7px 10px; vertical-align: top; }
.msg.assistant .body code { background: var(--sheet); padding: 1px 5px; border-radius: 5px; font-size: 14px; }

.status { color: var(--ink-2); font-size: 15px; display: flex; align-items: center; gap: 10px; }
.reading { display: inline-flex; gap: 4px; }
.reading i { width: 6px; height: 14px; border-radius: 2px; animation: flip 1.1s infinite ease-in-out; }
.reading i:nth-child(1) { background: var(--s01); }
.reading i:nth-child(2) { background: var(--s02); animation-delay: 0.12s; }
.reading i:nth-child(3) { background: var(--s03); animation-delay: 0.24s; }
.reading i:nth-child(4) { background: var(--s04); animation-delay: 0.36s; }
@keyframes flip { 0%, 100% { transform: scaleY(0.45); opacity: 0.5; } 40% { transform: scaleY(1); opacity: 1; } }

/* ---------- sources: section-coloured index tabs */
.sources { margin-top: 12px; }
.sources .label { font-size: 13px; color: var(--ink-2); margin-bottom: 6px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; max-width: 100%;
  border: none; border-left: 4px solid; border-radius: 4px 8px 8px 4px; padding: 5px 10px 5px 8px;
  font-size: 13px; line-height: 1.3; text-align: left;
}
.chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip b { font-weight: 700; }
.chip[aria-expanded="true"] { outline: 1px solid currentColor; }
.excerpt {
  margin-top: 8px; border-left: 4px solid; border-radius: 4px 10px 10px 4px; background: var(--sheet);
  padding: 10px 14px; font-size: 14px;
}
.excerpt .h { font-weight: 600; margin-bottom: 4px; }
.excerpt pre { white-space: pre-wrap; font: inherit; margin: 0; color: var(--ink-2); max-height: 260px; overflow: auto; }
.sec-01 { color: var(--s01); border-color: var(--s01); background: var(--s01-bg); }
.sec-02 { color: var(--s02); border-color: var(--s02); background: var(--s02-bg); }
.sec-03 { color: var(--s03); border-color: var(--s03); background: var(--s03-bg); }
.sec-04 { color: var(--s04); border-color: var(--s04); background: var(--s04-bg); }
.excerpt.sec-01, .excerpt.sec-02, .excerpt.sec-03, .excerpt.sec-04 { background: var(--sheet); color: var(--ink); }
.tab.sec-01, .tab.sec-02, .tab.sec-03, .tab.sec-04 { color: var(--ink); }
.tab.sec-01 b { color: var(--s01); } .tab.sec-02 b { color: var(--s02); }
.tab.sec-03 b { color: var(--s03); } .tab.sec-04 b { color: var(--s04); }

.err { background: var(--error-bg); color: var(--error-ink); border-radius: 10px; padding: 11px 13px; font-size: 15px; }
.err code { display: block; margin-top: 4px; font-size: 12px; opacity: 0.85; overflow-wrap: anywhere; }

/* ---------- composer */
.composer {
  flex: none; padding: 8px 12px calc(10px + var(--safe-bottom));
  background: color-mix(in srgb, var(--paper) 86%, transparent);
  -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
  border-top: 1px solid var(--rule);
}
.composer form {
  max-width: 720px; margin: 0 auto; display: flex; gap: 6px; align-items: flex-end;
  background: var(--sheet); border: 1px solid var(--rule); border-radius: 22px; padding: 5px 5px 5px 14px;
}
.composer form:focus-within { border-color: var(--brand); }
.composer textarea {
  flex: 1; resize: none; border: none; outline: none; background: transparent;
  max-height: 160px; min-height: 34px; padding: 6px 0; line-height: 1.45;
}
.send {
  width: 36px; height: 36px; flex: none; border-radius: 50%; border: none; display: grid; place-items: center;
  background: var(--brand); color: var(--brand-ink);
}
.send:disabled { opacity: 0.35; }
.send svg { width: 18px; height: 18px; }

/* ---------- devices sheet */
.overlay { position: fixed; inset: 0; background: rgba(8, 14, 20, 0.45); display: grid; place-items: end center; z-index: 10; }
.panel {
  background: var(--paper); width: 100%; max-width: 560px; max-height: 88%; overflow-y: auto;
  border-radius: 18px 18px 0 0; padding: 20px 18px calc(20px + var(--safe-bottom));
}
@media (min-width: 761px) { .overlay { place-items: center; } .panel { border-radius: 18px; } }
.panel h2 { margin: 0 0 4px; font: 600 22px/1.3 var(--serif); }
.panel h3 { font-size: 15px; margin: 24px 0 6px; }
.row { display: flex; align-items: center; gap: 10px; padding: 11px 0; border-bottom: 1px solid var(--rule); }
.row .grow { flex: 1; min-width: 0; }
.tag { font-size: 12px; background: var(--s03-bg); color: var(--s03); border-radius: 6px; padding: 1px 7px; margin-left: 6px; }
.inline { display: flex; gap: 8px; }
.inline input { flex: 1; min-width: 0; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--rule); background: var(--sheet); text-transform: uppercase; }
.events { font-size: 13px; color: var(--ink-2); }
.events div { padding: 4px 0; border-bottom: 1px solid var(--rule); }

/* ---------- mobile */
@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; }
  .side {
    position: fixed; inset: 0 auto 0 0; width: 86%; max-width: 330px; z-index: 6;
    transform: translateX(-102%); transition: transform 0.22s ease;
  }
  .scrim { display: block; position: fixed; inset: 0; z-index: 5; background: rgba(8, 14, 20, 0.4); opacity: 0; pointer-events: none; transition: opacity 0.22s; }
  .shell.open .side { transform: none; }
  .shell.open .scrim { opacity: 1; pointer-events: auto; }
  .topbar .menu { visibility: visible; }
  .thread { padding: 18px 16px 12px; }
  .conv .del { opacity: 1; }
  .tabs { grid-template-columns: 1fr; }
}

/* ---------- reading page */
.reader { padding-bottom: 40px; }
.sec-tabs { display: flex; gap: 6px; overflow-x: auto; margin: 0 -4px 22px; padding: 0 4px 2px; scrollbar-width: none; }
.sec-tab {
  flex: none; display: flex; gap: 6px; align-items: baseline; border: none; border-left: 4px solid;
  border-radius: 4px 9px 9px 4px; padding: 7px 12px 7px 9px; font-size: 14px; opacity: 0.7;
}
.sec-tab.on { opacity: 1; font-weight: 600; outline: 1px solid currentColor; }
.sec-title { font: 600 30px/1.2 var(--serif); margin: 0 0 4px; }
.sec-meta { margin: 0 0 18px; font-size: 14px; }
.guide { border-left: 4px solid; border-radius: 4px 12px 12px 4px; padding: 14px 16px; color: var(--ink); }
.guide p { margin: 0; line-height: 1.75; }
.guide-label { font-weight: 700; font-size: 14px; margin-bottom: 4px; }
.guide.sec-01 .guide-label { color: var(--s01); } .guide.sec-02 .guide-label { color: var(--s02); }
.guide.sec-03 .guide-label { color: var(--s03); } .guide.sec-04 .guide-label { color: var(--s04); }
.reader-tools { display: flex; justify-content: space-between; align-items: center; margin: 22px 0 6px; font-size: 14px; }
details.item { border: none; background: none; color: var(--ink); border-bottom: 1px solid var(--rule); }
details.item summary { list-style: none; cursor: pointer; padding: 14px 28px 14px 0; position: relative; }
details.item summary::-webkit-details-marker { display: none; }
details.item summary::after {
  content: ""; position: absolute; right: 6px; top: 22px; width: 8px; height: 8px;
  border-right: 2px solid var(--ink-2); border-bottom: 2px solid var(--ink-2); transform: rotate(45deg); transition: transform 0.15s;
}
details.item[open] summary::after { transform: rotate(-135deg); top: 26px; }
details.item .ih { display: block; font-weight: 650; }
details.item .sum { display: block; color: var(--ink-2); font-size: 15px; margin-top: 2px; }
.orig { border-left: 3px solid var(--rule); padding: 0 0 6px 14px; margin: 0 0 16px; }
details.item.sec-01 .orig { border-color: var(--s01); } details.item.sec-02 .orig { border-color: var(--s02); }
details.item.sec-03 .orig { border-color: var(--s03); } details.item.sec-04 .orig { border-color: var(--s04); }
.orig-label { font-size: 12px; color: var(--ink-2); margin-bottom: 4px; }
.orig .body :is(p, ul, ol, table, blockquote) { margin: 0 0 10px; }
.orig .body ul { padding-left: 20px; }
.orig .body blockquote { margin-left: 0; padding: 8px 12px; background: var(--sheet); border-radius: 8px; font-family: var(--serif); }
.orig .body blockquote p:last-child { margin: 0; }
.orig .body table { border-collapse: collapse; display: block; overflow-x: auto; font-size: 14px; }
.orig .body th, .orig .body td { border: 1px solid var(--rule); padding: 6px 9px; vertical-align: top; text-align: left; }
.orig .body h3 { font-size: 15px; margin: 14px 0 6px; }
.ask { font-size: 14px; margin-top: 4px; }
