/* Design: an exercise book. Graph-paper ground, blue pen ink, a red margin line that marks
   the question you are on. Bitter (textbook slab) for headings, Atkinson Hyperlegible for reading. */
:root {
  --paper: #f5f8fc; --grid: #dbe5f1; --card: #ffffff; --ink: #14233b; --ink2: #4a5b76; --line: #c6d3e6;
  --pen: #2352d8; --pen-ink: #ffffff; --pen2: #e3ebfe; --margin: #d9435b;
  --ok: #17794f; --ok2: #dcf2e7; --bad: #b92f47; --bad2: #fbe3e8; --hl: #ffe585;
  --font-d: 'Bitter', Georgia, serif;
  --font-b: 'Atkinson Hyperlegible', system-ui, -apple-system, 'Segoe UI', sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #0e1828; --grid: #182841; --card: #14233a; --ink: #e8eef9; --ink2: #9db0cc; --line: #2a3d5a;
    --pen: #86a6ff; --pen-ink: #0e1828; --pen2: #1a2c53; --margin: #ff7d90;
    --ok: #5fd5a0; --ok2: #123828; --bad: #ff8d9d; --bad2: #43202b; --hl: #6d5c14;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; font: 17px/1.55 var(--font-b); color: var(--ink); background-color: var(--paper);
  background-image: linear-gradient(var(--grid) 1px, transparent 1px), linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 24px 24px; min-height: 100vh;
}
.boot { text-align: center; padding: 4rem 1rem; color: var(--ink2); }
svg { width: 22px; height: 22px; stroke: currentColor; fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
h1, h2, h3 { font-family: var(--font-d); line-height: 1.25; margin: 0 0 .5rem; text-wrap: balance; }
h2 { font-size: 1.35rem; } h3 { font-size: 1.1rem; } h4 { margin: .6rem 0 .2rem; font-size: 1rem; }
p { margin: 0 0 .6rem; } ul, ol { margin: 0 0 .6rem; padding-left: 1.3rem; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--pen); outline-offset: 2px; }
[hidden] { display: none !important; }

/* shell */
.bar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; justify-content: space-between; gap: .75rem;
  padding: calc(.6rem + env(safe-area-inset-top, 0px)) max(16px, env(safe-area-inset-right, 0px)) .6rem max(16px, env(safe-area-inset-left, 0px));
  background: var(--card); border-bottom: 2px solid var(--margin);
}
.brand { font: 700 1.15rem var(--font-d); letter-spacing: .01em; }
.who { color: var(--ink2); font-size: .9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 55vw; }
.page { max-width: 780px; margin: 0 auto; padding: 16px max(16px, env(safe-area-inset-right, 0px)) calc(92px + env(safe-area-inset-bottom, 0px)) max(16px, env(safe-area-inset-left, 0px)); }
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5; display: flex; justify-content: center; gap: 0;
  background: var(--card); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom, 0px);
}
.tabs button {
  flex: 1; max-width: 190px; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: .55rem .25rem .5rem;
  background: none; border: 0; border-top: 3px solid transparent; color: var(--ink2); cursor: pointer; font-size: .82rem;
}
.tabs button[aria-current="page"] { color: var(--pen); border-top-color: var(--pen); font-weight: 700; }

/* generic */
.card { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 1rem; margin-bottom: 1rem; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: .4rem; min-height: 46px; padding: .5rem 1.1rem; border-radius: 8px; border: 2px solid var(--pen); background: var(--pen); color: var(--pen-ink); font-weight: 700; cursor: pointer; }
.btn.ghost { background: transparent; color: var(--pen); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.small { min-height: 36px; padding: .2rem .8rem; font-size: .9rem; }
.row { display: flex; gap: .6rem; flex-wrap: wrap; align-items: center; }
.row.between { justify-content: space-between; }
.note { color: var(--ink2); font-size: .92rem; }
.err { color: var(--bad); font-weight: 700; }
.ainote { border-left: 3px solid var(--margin); padding-left: .6rem; font-size: .85rem; margin: .5rem 0; }
label.field { display: block; margin-bottom: .8rem; font-weight: 700; font-size: .95rem; }
label.field > span.help { display: block; font-weight: 400; color: var(--ink2); font-size: .85rem; }
input[type=text], input[type=email], input[type=password], select, textarea {
  width: 100%; min-height: 46px; padding: .5rem .7rem; border: 2px solid var(--line); border-radius: 8px; background: var(--paper); margin-top: .25rem;
}
input:focus, select:focus, textarea:focus { border-color: var(--pen); }
.chips { display: flex; gap: .4rem; flex-wrap: wrap; }
.chip { padding: .3rem .8rem; min-height: 38px; border-radius: 999px; border: 2px solid var(--line); background: var(--card); cursor: pointer; font-size: .95rem; }
.chip.on { background: var(--hl); border-color: var(--ink); color: #14233b; font-weight: 700; }
.check { display: flex; gap: .6rem; align-items: flex-start; padding: .35rem 0; cursor: pointer; }
.check input { width: 22px; height: 22px; margin: .1rem 0 0; accent-color: var(--pen); flex: none; }
.switch { display: inline-flex; gap: .5rem; align-items: center; cursor: pointer; font-size: .95rem; }
.switch input { width: 20px; height: 20px; accent-color: var(--pen); }
.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(100px + env(safe-area-inset-bottom, 0px)); z-index: 20; background: var(--ink); color: var(--paper); padding: .6rem 1rem; border-radius: 8px; max-width: 90vw; box-shadow: 0 4px 16px rgb(0 0 0 / .25); }

/* auth */
.auth { max-width: 440px; margin: 0 auto; padding: calc(32px + env(safe-area-inset-top, 0px)) 16px 40px; }
.auth h1 { font-size: 1.9rem; }
.seg { display: flex; border: 2px solid var(--pen); border-radius: 8px; overflow: hidden; margin: 1rem 0; }
.seg button { flex: 1; min-height: 44px; border: 0; background: transparent; color: var(--pen); font-weight: 700; cursor: pointer; }
.seg button[aria-pressed="true"] { background: var(--pen); color: var(--pen-ink); }

/* tutor */
.pickers { display: grid; gap: .6rem; margin-bottom: .8rem; }
.msgs { display: flex; flex-direction: column; gap: .7rem; min-height: 40vh; }
.empty { text-align: center; color: var(--ink2); padding: 1.5rem 0 .5rem; }
.starters { display: flex; flex-direction: column; gap: .5rem; margin-top: 1rem; align-items: stretch; }
.starters .chip { border-radius: 10px; text-align: left; }
.msg { max-width: 92%; padding: .65rem .9rem; border-radius: 10px; overflow-wrap: anywhere; }
.msg p:last-child { margin-bottom: 0; }
.msg.user { align-self: flex-end; background: var(--pen2); border: 1px solid var(--pen); }
.msg.assistant { align-self: flex-start; background: var(--card); border: 1px solid var(--line); border-left: 3px double var(--margin); }
.msg.error { border-color: var(--bad); color: var(--bad); }
.msg img { display: block; max-width: 100%; max-height: 260px; border-radius: 6px; margin-bottom: .4rem; }
.msg .tools { margin-top: .3rem; }
.msg .tools button { background: none; border: 0; color: var(--ink2); cursor: pointer; padding: .2rem; min-height: 32px; }
.dots span { display: inline-block; width: 8px; height: 8px; margin-right: 4px; border-radius: 50%; background: var(--ink2); animation: blink 1.2s infinite both; }
.dots span:nth-child(2) { animation-delay: .2s; } .dots span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
.composer-wrap { position: sticky; bottom: calc(66px + env(safe-area-inset-bottom, 0px)); padding-top: .6rem; background: linear-gradient(transparent, var(--paper) 20%); }
.attach { display: flex; align-items: center; gap: .6rem; margin-bottom: .4rem; }
.attach img { height: 56px; border-radius: 6px; border: 1px solid var(--line); }
.composer { display: flex; gap: .4rem; align-items: flex-end; background: var(--card); border: 2px solid var(--line); border-radius: 12px; padding: .35rem; }
.composer textarea { flex: 1; min-height: 44px; max-height: 140px; margin: 0; border: 0; background: transparent; resize: none; }
.icon { width: 44px; height: 44px; display: grid; place-items: center; border-radius: 10px; border: 0; background: transparent; color: var(--ink2); cursor: pointer; flex: none; }
.icon:hover { background: var(--pen2); color: var(--pen); }
.icon.rec { background: var(--margin); color: #fff; animation: pulse 1.4s infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 8px rgb(217 67 91 / .25); } }
.send { width: 44px; height: 44px; display: grid; place-items: center; border: 0; border-radius: 10px; background: var(--pen); color: var(--pen-ink); cursor: pointer; flex: none; }
.send:disabled { opacity: .4; }

/* exam */
.runbar { display: flex; justify-content: space-between; align-items: center; gap: .6rem; margin-bottom: .6rem; font-weight: 700; }
.qnav { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: .8rem; }
.qnav button { width: 34px; height: 34px; border-radius: 6px; border: 2px dashed var(--line); background: transparent; color: var(--ink2); font-size: .85rem; cursor: pointer; }
.qnav button.loaded { border-style: solid; background: var(--card); color: var(--ink); }
.qnav button.done { background: var(--pen2); border-color: var(--pen); }
.qnav button.cur { border-color: var(--margin); color: var(--margin); font-weight: 700; box-shadow: 0 0 0 2px var(--margin) inset; }
.qnav button:disabled { cursor: default; }
.qcard { position: relative; background: var(--card); border: 1px solid var(--line); border-left: 4px double var(--margin); border-radius: 10px; padding: 1rem 1rem 1rem 1.1rem; margin-bottom: .8rem; }
.qcard .meta { color: var(--ink2); font-size: .85rem; margin-bottom: .4rem; }
.stem { font-size: 1.05rem; margin-bottom: .8rem; }
.passage { margin: 0 0 .8rem; padding: .6rem .9rem; background: var(--paper); border-left: 3px solid var(--pen); border-radius: 4px; font-size: .97rem; }
.opts { display: grid; gap: .5rem; }
.opt { display: flex; gap: .7rem; align-items: flex-start; text-align: left; width: 100%; padding: .6rem .7rem; border: 2px solid var(--line); border-radius: 8px; background: var(--paper); cursor: pointer; }
.opt .lt { flex: none; width: 28px; height: 28px; display: grid; place-items: center; border: 2px solid currentColor; border-radius: 6px; font-weight: 700; font-size: .9rem; }
.opt.multi .lt { border-radius: 50%; }
.opt:disabled, .bs .tf button:disabled { opacity: 1; cursor: default; }
.opt.sel { background: var(--hl); border-color: var(--ink); color: #14233b; }
.opt.right { background: var(--ok2); border-color: var(--ok); color: var(--ok); }
.opt.wrong { background: var(--bad2); border-color: var(--bad); color: var(--bad); }
.opt > span:last-child { color: var(--ink); overflow-wrap: anywhere; min-width: 0; }
.opt.sel > span:last-child { color: #14233b; }
.bs { display: grid; gap: .6rem; }
.bs .st { display: grid; gap: .4rem; padding: .5rem .6rem; border: 1px solid var(--line); border-radius: 8px; }
.bs .tf { display: flex; gap: .4rem; }
.bs .tf button { flex: 1; min-height: 40px; border: 2px solid var(--line); border-radius: 8px; background: var(--paper); cursor: pointer; font-weight: 700; }
.bs .tf button.sel { background: var(--hl); border-color: var(--ink); color: #14233b; }
.bs .tf button.right { background: var(--ok2); border-color: var(--ok); color: var(--ok); }
.bs .tf button.wrong { background: var(--bad2); border-color: var(--bad); color: var(--bad); }
.fb { margin-top: .8rem; padding: .7rem .9rem; border-radius: 8px; border: 1px solid; }
.fb.ok { background: var(--ok2); border-color: var(--ok); }
.fb.bad { background: var(--bad2); border-color: var(--bad); }
.fb strong.v { display: block; margin-bottom: .2rem; }
.qactions { display: flex; gap: .6rem; justify-content: space-between; }
.qactions .btn { flex: 1; }
.qactions .btn.ghost { flex: 0 0 auto; }
.score { display: flex; align-items: center; gap: 1rem; }
.score .big { font: 700 2.6rem var(--font-d); line-height: 1; }
.rev { border: 1px solid var(--line); border-radius: 10px; background: var(--card); margin-bottom: .6rem; }
.rev summary { cursor: pointer; padding: .7rem .9rem; display: flex; gap: .6rem; align-items: baseline; }
.rev summary b { flex: none; }
.rev.ok summary b { color: var(--ok); } .rev.bad summary b { color: var(--bad); }
.rev .inner { padding: 0 .9rem .9rem; }

/* progress */
.bars { display: grid; gap: .7rem; }
.bar-row .top { display: flex; justify-content: space-between; gap: .6rem; font-size: .92rem; }
.track { height: 10px; background: var(--paper); border: 1px solid var(--line); border-radius: 6px; overflow: hidden; margin: .25rem 0; }
.fill { height: 100%; background: var(--pen); }
.fill.low { background: var(--margin); } .fill.high { background: var(--ok); }
.hist { display: grid; gap: .4rem; }
.hist .item { display: flex; justify-content: space-between; gap: .6rem; padding: .5rem 0; border-bottom: 1px dashed var(--line); font-size: .95rem; }

.katex { font-size: 1.05em; }
.katex-display { overflow-x: auto; overflow-y: hidden; padding: .2rem 0; }
pre { overflow-x: auto; background: var(--paper); padding: .6rem; border-radius: 6px; }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; } }
