:root {
  --bg: #ffffff; --surface: #f4f4f2; --surface-2: #ebebe8;
  --text: #161616; --dim: #6f6f6c; --line: #e4e4e0; --line-2: #cfcfca;
  --accent: #161616; --on-accent: #ffffff;
  --red: #b23f2e; --amber: #8a6a1a; --green: #3f6b3a;
  --sans: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --serif: 'Fraunces', Georgia, serif;
  --bar: 56px; --composer: 96px; --col: 760px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #121212; --surface: #1e1e1e; --surface-2: #2a2a2a;
    --text: #ececec; --dim: #9a9a96; --line: #2c2c2c; --line-2: #444442;
    --accent: #ececec; --on-accent: #121212;
    --red: #e07b6c; --amber: #d8b25a; --green: #8fbf86;
  }
}
:root[data-theme='dark'] {
  --bg: #121212; --surface: #1e1e1e; --surface-2: #2a2a2a;
  --text: #ececec; --dim: #9a9a96; --line: #2c2c2c; --line-2: #444442;
  --accent: #ececec; --on-accent: #121212;
  --red: #e07b6c; --amber: #d8b25a; --green: #8fbf86;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15.5px/1.6 var(--sans); -webkit-font-smoothing: antialiased; min-height: 100vh; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.primary { background: var(--accent); color: var(--on-accent); border: 0; border-radius: 999px; padding: 12px 20px; font-weight: 600; font-size: 15px; transition: opacity .15s; }
.primary:hover { opacity: .9; }
.primary:disabled { background: var(--surface-2); color: var(--dim); cursor: default; }
.linkish { background: none; border: 0; padding: 0; color: var(--dim); text-decoration: underline; text-underline-offset: 3px; font-size: 13.5px; }
.linkish:hover { color: var(--text); }
: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; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* top bar (always visible) */
.bar { position: fixed; top: 0; left: 0; right: 0; height: var(--bar); background: color-mix(in srgb, var(--bg) 86%, transparent); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); border-bottom: 1px solid var(--line); z-index: 20; }
.barin { max-width: var(--col); margin: 0 auto; height: 100%; padding: 0 16px; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.brand { font: 600 21px/1 var(--serif); color: var(--text); text-decoration: none; letter-spacing: -.01em; }
.barright { display: flex; align-items: center; gap: 2px; }
.iconbtn { display: inline-flex; align-items: center; justify-content: center; width: 38px; height: 38px; border-radius: 10px; border: 0; background: transparent; color: var(--dim); }
.iconbtn:hover { background: var(--surface); color: var(--text); }
.whobtn { display: inline-flex; align-items: center; gap: 4px; border: 1px solid var(--line); background: var(--bg); border-radius: 999px; padding: 6px 8px 6px 12px; font-size: 13px; color: var(--text); margin-left: 4px; }
.whobtn svg { width: 16px; height: 16px; color: var(--dim); }
.whobtn:hover { background: var(--surface); }

/* page column */
.page { max-width: var(--col); margin: 0 auto; padding: calc(var(--bar) + 20px) 16px calc(var(--composer) + 28px); min-height: 100vh; }
body.gated { overflow: hidden; }
body.gated .composer .row { opacity: .55; pointer-events: none; }

/* gate: a compact card over the live interface */
.gateoverlay { position: fixed; inset: 0; z-index: 40; display: flex; align-items: center; justify-content: center; padding: 16px; background: rgba(20, 20, 20, .22); backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px); }
.gate { width: 100%; max-width: 440px; background: var(--bg); border: 1px solid var(--line); border-radius: 20px; padding: 22px 22px 16px; box-shadow: 0 24px 60px rgba(0, 0, 0, .18); }
.gate .mark { width: 32px; height: 32px; margin-bottom: 8px; }
.gate .mark svg { width: 30px; height: 30px; }
.gate h1 { font: 600 21px/1.2 var(--sans); letter-spacing: -.02em; margin: 0 0 4px; }
.lede { color: var(--dim); margin: 0 0 12px; font-size: 14px; }
.roles { display: grid; gap: 8px; margin-bottom: 14px; }
.role { text-align: left; background: var(--bg); border: 1px solid var(--line-2); border-radius: 12px; padding: 10px 12px; display: flex; flex-direction: column; gap: 2px; color: var(--text); }
.role:hover { background: var(--surface); }
.role[aria-checked='true'] { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.role b { font-weight: 600; font-size: 15px; }
.role span { color: var(--dim); font-size: 12.5px; line-height: 1.4; }
.startrow { display: flex; align-items: center; gap: 14px; }
.startrow .primary { width: 100%; }
.err { color: var(--red); font-size: 13px; min-height: 1em; margin: 8px 0 0; }
.notice { font-size: 12px; line-height: 1.5; color: var(--dim); text-align: center; margin: 14px 0 0; }
@media (max-width: 560px) { .gateoverlay { align-items: flex-end; padding: 0; } .gate { max-width: none; border-radius: 20px 20px 0 0; padding-bottom: max(16px, env(safe-area-inset-bottom)); } }

/* welcome */
.welcome { padding-top: 10vh; text-align: center; display: flex; flex-direction: column; align-items: center; }
.welcome .mark { margin-bottom: 14px; }
.welcome h1 { font: 600 28px/1.2 var(--sans); letter-spacing: -.02em; margin: 0 0 6px; }
.welcome .sub { color: var(--dim); margin: 0 0 24px; font-size: 14.5px; }
.starters { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; max-width: 620px; }
.starters button { text-align: left; background: var(--bg); border: 1px solid var(--line-2); border-radius: 12px; padding: 9px 13px; font-size: 13.5px; color: var(--text); line-height: 1.4; }
.starters button:hover { background: var(--surface); }

/* thread */
.thread { display: flex; flex-direction: column; gap: 18px; }
.msg { max-width: 100%; }
.msg.user { align-self: flex-end; max-width: 85%; background: var(--surface); padding: 10px 15px; border-radius: 18px 18px 4px 18px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.user .file { display: inline-flex; align-items: center; gap: 6px; font-size: 13.5px; color: var(--dim); }
.msg.user .file svg { width: 16px; height: 16px; }
.msg.bot { align-self: stretch; padding: 2px 2px 0; }
.msg.bot p { margin: 0 0 12px; } .msg.bot p:last-child { margin-bottom: 0; }
.msg.bot ul, .msg.bot ol { margin: 0 0 12px; padding-left: 22px; } .msg.bot li { margin-bottom: 5px; }
.cite { display: inline-block; font-size: 10.5px; line-height: 1; color: var(--dim); background: var(--surface); border: 0; border-radius: 5px; padding: 3px 5px; margin: 0 1px; vertical-align: 2px; }
.cite:hover { color: var(--text); background: var(--surface-2); }
.meta { margin-top: 10px; font-size: 12.5px; color: var(--dim); }
.gapnote { display: none; }
.sources { list-style: none; margin: 8px 0 0; padding: 0; display: grid; gap: 6px; }
.sources li { display: grid; grid-template-columns: 22px 1fr; gap: 8px; }
.sources .n { font-size: 11px; padding-top: 3px; }
.sources a { color: var(--text); text-underline-offset: 3px; }
.badge { display: inline-block; font-size: 10.5px; border: 1px solid var(--line); border-radius: 999px; padding: 1px 7px; margin-right: 4px; color: var(--dim); }
details > summary { cursor: pointer; color: var(--dim); font-size: 12.5px; list-style: none; }
details > summary::-webkit-details-marker { display: none; }
details > summary::before { content: '›'; display: inline-block; margin-right: 6px; transition: transform .15s; }
details[open] > summary::before { transform: rotate(90deg); }

/* actions + feedback (one quiet row of icon-ish buttons) */
.actions, .fb { display: flex; gap: 4px; align-items: center; margin-top: 8px; flex-wrap: wrap; }
.actions button, .actions a.btn, .fb button { background: transparent; border: 0; border-radius: 8px; padding: 5px 9px; font-size: 12.5px; color: var(--dim); text-decoration: none; }
.actions button:hover, .actions a.btn:hover, .fb button:hover { background: var(--surface); color: var(--text); }
.fb .done { color: var(--dim); font-size: 12.5px; padding: 5px 9px; }
.sharenote { flex-basis: 100%; font-size: 12px; line-height: 1.5; color: var(--dim); padding: 0 9px; }
.sharenote a { color: var(--text); }
.fbform { display: grid; gap: 6px; margin-top: 6px; max-width: 460px; flex-basis: 100%; }
.fbform textarea { width: 100%; background: var(--surface); border: 1px solid var(--line-2); border-radius: 10px; padding: 8px 10px; min-height: 64px; }

/* thinking spinner: three notes orbiting */
.thinking { display: inline-flex; align-items: center; gap: 10px; color: var(--dim); font-size: 13.5px; padding: 4px 0; }
.notes { position: relative; width: 28px; height: 28px; animation: spin 1.6s linear infinite; }
.notes svg { position: absolute; width: 12px; height: 12px; fill: currentColor; stroke: none; left: 50%; top: 50%; margin: -6px 0 0 -6px; transform-origin: 6px 6px; }
.notes svg:nth-child(1) { transform: rotate(0deg) translateY(-9px); }
.notes svg:nth-child(2) { transform: rotate(120deg) translateY(-9px); }
.notes svg:nth-child(3) { transform: rotate(240deg) translateY(-9px); }
.notes svg:nth-child(1) { animation: pulse 1.6s ease-in-out infinite; }
.notes svg:nth-child(2) { animation: pulse 1.6s ease-in-out .53s infinite; }
.notes svg:nth-child(3) { animation: pulse 1.6s ease-in-out 1.06s infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pulse { 0%, 100% { opacity: .35; } 50% { opacity: 1; } }
.msg.bot.care { border-left: 2px solid var(--line-2); padding-left: 14px; }

/* upgrade card (inline in the thread) */
.msg.bot.upgrade { background: var(--surface); border-radius: 16px; padding: 16px 18px; max-width: 520px; }
.msg.bot.upgrade b { display: block; font-size: 16px; margin-bottom: 4px; }
.msg.bot.upgrade p { color: var(--dim); font-size: 14px; }
.msg.bot.upgrade .row { display: flex; gap: 8px; margin: 10px 0; flex-wrap: wrap; }
.msg.bot.upgrade input { flex: 1; min-width: 180px; background: var(--bg); border: 1px solid var(--line-2); border-radius: 10px; padding: 9px 11px; }
.msg.bot.upgrade details { margin-top: 4px; }
.msg.bot.upgrade details button { background: transparent; border: 1px solid var(--line-2); border-radius: 999px; padding: 8px 14px; font-size: 13px; }

/* document breakdown inside a bot message */
.result { display: grid; gap: 12px; }
.card { background: var(--surface); border-radius: 14px; padding: 14px 16px; }
.card h3 { font-size: 15px; font-weight: 600; margin: 0 0 8px; }
.summary { font-size: 16px; line-height: 1.55; margin: 0; }
.terms { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 8px; }
.term { background: var(--bg); border-radius: 10px; padding: 10px 12px; font-size: 14px; }
.term .k { font-size: 12px; color: var(--dim); margin-bottom: 3px; }
.flag { border-left: 3px solid var(--line-2); padding: 8px 12px; margin-bottom: 10px; background: var(--bg); border-radius: 0 10px 10px 0; font-size: 14px; }
.flag:last-child { margin-bottom: 0; }
.flag.high { border-left-color: var(--red); } .flag.medium { border-left-color: var(--amber); } .flag.low { border-left-color: var(--line-2); } .flag.info { border-left-color: var(--green); }
.flag .sev { font-size: 11px; color: var(--dim); margin-right: 6px; text-transform: uppercase; letter-spacing: .02em; }
.flag q, .term q { display: block; font-style: italic; color: var(--dim); margin: 6px 0; font-size: 13.5px; quotes: '“' '”'; }
.flag .ask { font-size: 13.5px; margin-top: 4px; }
.stamp { font-size: 12.5px; line-height: 1.6; color: var(--dim); margin: 0; }

/* composer (fixed at the bottom) */
.composerwrap { position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; background: linear-gradient(to top, var(--bg) 70%, transparent); padding: 18px 16px max(10px, env(safe-area-inset-bottom)); }
.composer { max-width: var(--col); margin: 0 auto; }
.composer .row { display: flex; align-items: flex-end; gap: 6px; background: var(--bg); border: 1px solid var(--line-2); border-radius: 22px; padding: 6px 6px 6px 8px; box-shadow: 0 2px 14px rgba(0,0,0,.06); }
.composer .row:focus-within { border-color: var(--text); }
.composer .iconbtn { width: 36px; height: 36px; flex: none; }
.composer textarea { flex: 1; resize: none; background: transparent; border: 0; padding: 8px 4px; min-height: 36px; max-height: 180px; line-height: 1.45; outline: none; }
.send { flex: none; width: 36px; height: 36px; border-radius: 50%; border: 0; background: var(--accent); color: var(--on-accent); display: inline-flex; align-items: center; justify-content: center; }
.send:disabled { opacity: .35; cursor: default; }
.send svg { width: 18px; height: 18px; stroke-width: 2.2; }
.attach { display: inline-flex; align-items: center; gap: 8px; background: var(--surface); border-radius: 999px; padding: 5px 6px 5px 12px; font-size: 13px; margin-bottom: 8px; max-width: 100%; }
.attach span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attach button { border: 0; background: var(--bg); border-radius: 50%; width: 22px; height: 22px; line-height: 1; color: var(--dim); }
.under { display: flex; justify-content: space-between; gap: 10px; font-size: 12px; color: var(--dim); padding: 6px 12px 0; min-height: 1.4em; }
.under .linkish { font-size: 12px; }

/* scroll-to-bottom */
.tobottom { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--composer) + 10px); z-index: 19; width: 36px; height: 36px; border-radius: 50%; border: 1px solid var(--line-2); background: var(--bg); color: var(--text); display: inline-flex; align-items: center; justify-content: center; box-shadow: 0 2px 10px rgba(0,0,0,.12); }
.tobottom svg { width: 18px; height: 18px; }

/* sheets (history, settings) */
dialog.sheet { background: var(--bg); color: var(--text); border: 1px solid var(--line); border-radius: 18px; padding: 16px 18px; width: min(560px, calc(100vw - 24px)); max-height: 85vh; display: none; flex-direction: column; box-shadow: 0 10px 40px rgba(0,0,0,.18); }
dialog.sheet[open] { display: flex; }
dialog.sheet.small { width: min(420px, calc(100vw - 24px)); }
dialog::backdrop { background: rgba(0,0,0,.45); }
.sheethead { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; }
.sheethead h2 { font-size: 18px; font-weight: 600; margin: 0; }
.sheetbody { overflow: auto; margin: 8px 0; display: grid; gap: 4px; }
.sheetbody h3 { font-size: 12.5px; font-weight: 500; color: var(--dim); margin: 12px 0 2px; }
.hist { text-align: left; background: transparent; border: 0; border-radius: 10px; padding: 9px 12px; display: grid; gap: 2px; font-size: 14px; color: var(--text); }
.hist:hover { background: var(--surface); }
.sheetfoot { border-top: 1px solid var(--line); padding-top: 10px; margin-top: 6px; }

/* share page */
.shell { max-width: var(--col); margin: 0 auto; padding: 20px 16px 40px; }
header.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding-bottom: 14px; border-bottom: 1px solid var(--line); margin-bottom: 18px; }
header.top h1 { margin: 0; }
.cta { margin-top: 28px; background: var(--surface); border-radius: 16px; padding: 18px 20px; }
.cta h3 { font-size: 18px; font-weight: 600; margin: 0 0 6px; }
.cta p { color: var(--dim); margin: 0 0 12px; }
.cta a.primary { display: inline-block; text-decoration: none; }
a.linkish { text-decoration: underline; font-size: 13.5px; }

/* print: only the chosen answer or breakdown, cloned into #printArea */
#printArea { display: none; }
@media print {
  body { background: #fff; color: #111; font-size: 12pt; }
  body:has(#printArea) .page, body:has(#printArea) .bar, body:has(#printArea) .composerwrap, body:has(#printArea) .tobottom, body:has(#printArea) .shell { display: none !important; }
  #printArea { display: block; }
  #printArea .msg.user { background: #eee; color: #111; }
  .print-head { font: 600 18pt var(--sans); margin-bottom: 12pt; }
  .print-foot { margin-top: 18pt; border-top: 1px solid #999; padding-top: 6pt; font-size: 9pt; color: #555; }
  .card, .flag, .term { background: #fff !important; color: #111 !important; border-color: #bbb !important; break-inside: avoid; }
  .cite { color: #111; background: none; border: 1px solid #999; }
  a { color: #111; text-decoration: none; }
}

@media (max-width: 560px) { .gate h1 { font-size: 26px; } .welcome h1 { font-size: 24px; } .msg.user { max-width: 92%; } .whobtn span { max-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } html { scroll-behavior: auto; } .notes svg { opacity: 1; } }

/* sign-in on the welcome card */
.signinrow { margin: 12px 0 0; text-align: center; font-size: 13px; }
#signinBox { margin-top: 10px; }
#signinBox .row, .upgrade .row { display: flex; gap: 8px; margin: 8px 0; }
#signinBox input, .upgrade input { flex: 1; min-width: 0; background: var(--bg); border: 1px solid var(--line-2); border-radius: 10px; padding: 9px 11px; }
/* memory editor */
.mem { display: grid; gap: 10px; }
.mem textarea, .mem input { width: 100%; background: var(--surface); border: 1px solid var(--line-2); border-radius: 10px; padding: 8px 10px; font-size: 13.5px; }
.mem textarea { min-height: 70px; resize: vertical; }
.mem .k { font-size: 12px; color: var(--dim); margin-bottom: 4px; display: block; }
.mem .act { border: 1px solid var(--line); border-radius: 12px; padding: 10px; display: grid; gap: 6px; }
.mem .act .head { display: flex; gap: 8px; align-items: center; }
.mem .act .head input { flex: 1; }
.mem .live { font-size: 12.5px; color: var(--dim); }
.mem .foot { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.mem .empty { color: var(--dim); font-size: 13.5px; }
.lookup { font-size: 12.5px; color: var(--dim); margin: 4px 0 0; }
.seg { display: inline-flex; flex-wrap: wrap; border: 1px solid var(--line-2); border-radius: 18px; overflow: hidden; }
.seg button { background: transparent; border: 0; padding: 7px 14px; font-size: 13.5px; color: var(--dim); }
.seg button[aria-checked='true'] { background: var(--accent); color: var(--on-accent); }
.field { margin: 12px 0; }
.field .label { display: block; font-weight: 600; margin-bottom: 6px; }
