:root {
  --bg: #f8f4ea; --surface: #fffdf8; --surface2: #f1ece0; --line: #ddd6c6; --text: #17181c; --muted: #6b6a66;
  --accent: #1d5cff; --accent-ink: #ffffff; --accent-soft: #e2e8fb; --answer: #e3e9fb; --ok: #1d8a4a; --bad: #c43d2f; --warn: #b86a00; --warn-bg: #fff0d6;
  --g1: #1a4df0; --g2: #e0602a; --grid: #e6e0d2; --grid2: #cfc7b5; --axis: #3a3a3a; --mark: #111;
  --math: "Times New Roman", "STIX Two Text", "Nimbus Roman", Georgia, serif;
  --ui: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, "Segoe UI", Roboto, sans-serif;
  --r: 14px; --tab-h: 58px;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #121316; --surface: #1c1e23; --surface2: #25282f; --line: #343842; --text: #f1f1f3; --muted: #a2a5ad;
  --accent: #3a73ff; --accent-ink: #ffffff; --accent-soft: #1f2b47; --answer: #233150; --ok: #46c27a; --bad: #ff7b6b; --warn: #ffb547; --warn-bg: #3a2c12;
  --g1: #6c98ff; --g2: #ffa25c; --grid: #262931; --grid2: #383c47; --axis: #c9cbd1; --mark: #fff;
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-family: var(--ui); }
body { overscroll-behavior: none; }
button { font: inherit; color: inherit; }
#app { display: flex; flex-direction: column; height: 100vh; height: 100dvh; max-width: 640px; margin: 0 auto; }
header.top { padding: calc(env(safe-area-inset-top) + 8px) 16px 6px; display: grid; grid-template-columns: 40px 1fr 40px; align-items: center; row-gap: 8px; }
header.top h1 { grid-column: 2; margin: 0; text-align: center; font: 700 28px/1.1 var(--math); letter-spacing: .2px; }
.iconbtn { width: 40px; height: 40px; border: 0; background: none; border-radius: 12px; display: grid; place-items: center; cursor: pointer; }
.iconbtn svg { width: 22px; height: 22px; }
#offlineBtn { grid-column: 3; position: relative; }
#offlineBtn .dot { position: absolute; top: 9px; right: 8px; width: 9px; height: 9px; border-radius: 50%; background: var(--warn); border: 2px solid var(--bg); }
#offlineBtn.ready .dot { background: var(--ok); }
.seg { display: flex; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 3px; gap: 2px; }
.seg button { flex: 1; border: 0; background: none; padding: 7px 6px; border-radius: 9px; font-size: 15px; display: flex; align-items: center; justify-content: center; gap: 6px; cursor: pointer; min-height: 36px; }
.seg button svg { width: 17px; height: 17px; }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }
header .seg { grid-column: 1 / -1; }
main { flex: 1; overflow: hidden; position: relative; }
.tab { position: absolute; inset: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 6px 16px 24px; display: none; }
.tab.active { display: block; }
.field { position: relative; display: flex; align-items: center; background: var(--surface); border: 2px solid var(--line); border-radius: var(--r); min-height: 60px; padding: 0 6px 0 14px; transition: border-color .15s; }
.field:focus-within { border-color: var(--accent); }
.field input { flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; color: var(--text); font: 28px/1.3 var(--math); padding: 10px 0; }
.field input::placeholder { color: var(--muted); font-size: 22px; font-family: var(--ui); }
.field .iconbtn { flex: none; color: var(--text); }
.field .clear { width: 34px; height: 34px; color: var(--muted); }
.field .clear[hidden] { display: none; }
.field-label { font-size: 13px; color: var(--muted); margin: 0 0 6px 4px; font-weight: 600; letter-spacing: .3px; }
.preview { min-height: 26px; margin: 8px 4px 0; font-size: 15px; color: var(--muted); display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.preview .math { color: var(--text); font-size: 20px; }
.preview .err { color: var(--bad); }
.notes .note { background: var(--warn-bg); border-radius: 12px; padding: 10px 12px; margin-top: 8px; font-size: 15px; }
.notes .note .fixes { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.chip { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 8px 14px; font-size: 15px; cursor: pointer; min-height: 40px; }
.chip.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.chip.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.strip { display: flex; gap: 6px; margin-top: 10px; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
.strip::-webkit-scrollbar { display: none; }
.key { flex: none; min-width: 46px; height: 44px; border: 1px solid var(--line); background: var(--surface); border-radius: 11px; font: 21px var(--math); cursor: pointer; display: grid; place-items: center; padding: 0 8px; touch-action: manipulation; user-select: none; -webkit-user-select: none; }
.key.ui { font: 600 15px var(--ui); }
.key:active { background: var(--surface2); }
.key.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.keypad { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; margin-top: 8px; }
.keypad[hidden] { display: none; }
.keypad .key { min-width: 0; width: 100%; height: 46px; font-size: 20px; }
.keypad .key.num { background: var(--surface2); }
.keypad .key.go { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font: 600 15px var(--ui); }
.answer { margin-top: 14px; background: var(--answer); border-radius: 18px; min-height: 96px; padding: 16px; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; transition: opacity .12s; }
.answer { position: relative; }
.answer .bm { position: absolute; top: 4px; right: 4px; color: var(--muted); }
.answer .bm.on svg, .hlist .star.on svg { fill: #f5b301; stroke: #f5b301; }
.answer.idle { background: var(--surface); border: 1px dashed var(--line); }
.answer .aline { font: 40px/1.25 var(--math); overflow-x: auto; max-width: 100%; padding: 2px 4px; }
.answer.long .aline { font-size: 28px; }
.answer.xlong .aline { font-size: 22px; }
.answer .approx { margin-top: 6px; font: 18px var(--math); color: var(--muted); }
.answer .tag { margin-top: 8px; font-size: 13px; font-weight: 600; color: var(--warn); background: var(--warn-bg); padding: 3px 10px; border-radius: 999px; }
.answer .msg { color: var(--muted); font-size: 16px; }
.answer .info { margin-top: 6px; font: 18px/1.5 var(--math); }
.answer.stale { opacity: .45; }
.answer .wait { color: var(--muted); font-size: 28px; letter-spacing: 6px; }
.alts { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.alts .chip { font-size: 14px; min-height: 36px; padding: 6px 12px; }
.actions { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; margin-top: 10px; }
.actions .btn { white-space: nowrap; font-size: 15px; padding: 0 6px; }
.actions[hidden] { display: none; }
.btn { border: 1px solid var(--line); background: var(--surface); border-radius: 12px; min-height: 48px; display: flex; align-items: center; justify-content: center; gap: 7px; font-size: 15px; font-weight: 500; cursor: pointer; padding: 0 8px; }
.btn svg { width: 19px; height: 19px; flex: none; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.on { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.btn[hidden] { display: none; }
.panel { margin-top: 14px; }
.panel h3 { margin: 0 0 8px; font-size: 19px; }
.steps { list-style: none; margin: 0; padding: 0; }
.steps li { display: grid; grid-template-columns: 30px 1fr; gap: 10px; padding: 11px 0; border-top: 1px solid var(--line); }
.steps li:first-child { border-top: 0; }
.steps .n { width: 28px; height: 28px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; font-weight: 700; font-size: 14px; }
.steps .t { font-size: 16px; line-height: 1.4; }
.steps .m { font: 22px/1.5 var(--math); margin-top: 3px; overflow-x: auto; }
.check { border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin-bottom: 8px; background: var(--surface); }
.check .h { display: flex; justify-content: space-between; align-items: center; font: 20px var(--math); }
.check .ok { color: var(--ok); font: 600 14px var(--ui); }
.check .bad { color: var(--bad); font: 600 14px var(--ui); }
.check .l { font: 19px/1.5 var(--math); overflow-x: auto; }
.examples { margin-top: 18px; }
.examples h3 { font-size: 15px; color: var(--muted); margin: 0 0 8px; font-weight: 600; }
.examples .grid { display: flex; flex-wrap: wrap; gap: 8px; }
.examples .chip { font-family: var(--math); font-size: 17px; }
.examples .chip small { font-family: var(--ui); color: var(--muted); font-size: 12px; margin-left: 6px; }
/* math */
.math, .aline, .m, .l, .preview .math { white-space: nowrap; }
.math i.v, .aline i.v, .m i.v, .l i.v, i.v { font-style: italic; padding-right: .04em; }
.op { padding: 0 .22em; }
.op.dot { padding: 0 .08em; }
.op.rel { padding: 0 .3em; }
.jx { padding-left: .05em; }
.word { font-family: var(--ui); font-size: .6em; color: var(--muted); padding: 0 .55em; vertical-align: middle; }
.txt { font-family: var(--ui); font-size: .72em; color: var(--muted); }
.raw { font-family: var(--math); }
.frac { display: inline-flex; flex-direction: column; vertical-align: middle; text-align: center; font-size: .82em; margin: 0 .12em; }
.frac > .fn { border-bottom: .06em solid currentColor; padding: 0 .15em .04em; }
.frac > .fd { padding: .04em .15em 0; }
sup { font-size: .62em; vertical-align: .62em; line-height: 0; }
sub { font-size: .62em; line-height: 0; }
.sqrt { display: inline-flex; align-items: stretch; white-space: nowrap; }
.sqrt .rs { font-size: 1.05em; padding-right: .02em; }
.sqrt .rad { border-top: .06em solid currentColor; padding: .06em .1em 0 .08em; margin-top: .12em; }
.sqrt .idx { font-size: .5em; margin-right: -.45em; vertical-align: .9em; }
.abs { padding: 0 .06em; }
.pn { font-weight: 300; }
.mline + .mline { margin-top: 4px; }
/* scan info */
.scan { display: flex; gap: 12px; align-items: center; margin-top: 10px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 8px; }
.scan[hidden] { display: none; }
.scan img { width: 92px; height: 62px; object-fit: cover; border-radius: 8px; background: #ddd; flex: none; }
.scan .meta { flex: 1; min-width: 0; font-size: 14px; color: var(--muted); }
.scan .rec { font: 20px var(--math); color: var(--text); overflow-x: auto; white-space: nowrap; }
.scan .rec mark { background: var(--warn-bg); color: var(--warn); border-bottom: 2px solid var(--warn); border-radius: 3px; padding: 0 2px; }
.scan .warn { color: var(--warn); font-weight: 600; }
/* graph */
.gfields { display: grid; gap: 8px; grid-template-columns: minmax(0, 1fr); }
.gfields .field { min-height: 52px; }
.gfields .field input { font-size: 23px; }
.gfields .lbl { font: italic 20px var(--math); margin-right: 8px; white-space: nowrap; }
.gfields .lbl b { font-style: normal; display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: 6px; vertical-align: 1px; }
.gseg { margin-top: 10px; }
.gbox { position: relative; margin-top: 10px; border-radius: 14px; overflow: hidden; border: 1px solid var(--line); background: var(--surface); height: min(52vh, 420px); touch-action: none; }
.gbox canvas { width: 100%; height: 100%; display: block; touch-action: none; }
.gbox .undefinedTag { position: absolute; left: 10px; top: 10px; font-size: 13px; background: var(--warn-bg); color: var(--warn); padding: 3px 9px; border-radius: 999px; }
.gctl { display: flex; gap: 8px; align-items: center; margin-top: 10px; flex-wrap: wrap; }
.trace { display: flex; align-items: center; gap: 8px; border: 1px solid var(--line); border-radius: 12px; padding: 4px 8px 4px 12px; background: var(--surface); min-height: 48px; }
.switch { position: relative; width: 50px; height: 30px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch span { position: absolute; inset: 0; background: var(--line); border-radius: 999px; transition: .2s; }
.switch span::after { content: ""; position: absolute; width: 26px; height: 26px; left: 2px; top: 2px; background: #fff; border-radius: 50%; transition: .2s; box-shadow: 0 1px 3px rgba(0,0,0,.3); }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(20px); }
.xin { display: flex; align-items: center; gap: 6px; font: italic 20px var(--math); }
.xin input { width: 76px; font: 19px var(--math); border: 1px solid var(--line); border-radius: 10px; padding: 7px 8px; background: var(--bg); color: var(--text); }
.yout { font: 19px var(--math); background: var(--accent-soft); color: var(--text); padding: 8px 12px; border-radius: 10px; min-width: 80px; text-align: center; }
.zoom { display: flex; gap: 8px; margin-left: auto; }
.zoom .btn { min-width: 48px; }
.marks { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.marks .chip { font: 15px var(--math); min-height: 34px; padding: 5px 11px; }
.tbl-ctl { display: flex; gap: 10px; align-items: center; margin-top: 10px; font-size: 15px; flex-wrap: wrap; }
.tbl-ctl label { display: flex; align-items: center; gap: 6px; }
.tbl-ctl input { width: 80px; font: 18px var(--math); border: 1px solid var(--line); border-radius: 10px; padding: 7px 8px; background: var(--surface); color: var(--text); }
table.vals { width: 100%; border-collapse: collapse; margin-top: 10px; font: 19px var(--math); background: var(--surface); border-radius: 12px; overflow: hidden; }
table.vals th { font: 600 14px var(--ui); color: var(--muted); text-align: right; padding: 8px 12px; border-bottom: 1px solid var(--line); }
table.vals td { text-align: right; padding: 9px 12px; border-bottom: 1px solid var(--line); }
table.vals tr.hl td { background: var(--accent-soft); }
table.vals td.und { color: var(--warn); font: 14px var(--ui); }
/* history */
.hsearch { display: flex; gap: 8px; }
.hsearch input { flex: 1; font-size: 17px; border: 1px solid var(--line); border-radius: 12px; padding: 11px 12px; background: var(--surface); color: var(--text); }
.hfilter { display: flex; gap: 8px; margin-top: 10px; }
.hlist { list-style: none; padding: 0; margin: 12px 0 0; }
.hlist li { display: flex; align-items: center; gap: 4px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 6px 6px 6px 14px; margin-bottom: 8px; }
.hlist .main { flex: 1; min-width: 0; text-align: left; border: 0; background: none; padding: 6px 0; cursor: pointer; }
.hlist .q { font: 21px var(--math); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hlist .a { font: 16px var(--math); color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 2px; }
.hlist .d { font-size: 12px; color: var(--muted); margin-top: 2px; }
.hlist .star.on svg { fill: #f5b301; stroke: #f5b301; }
.empty { color: var(--muted); text-align: center; padding: 30px 10px; font-size: 15px; }
.status-card { margin-top: 18px; border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; background: var(--surface); font-size: 15px; }
.status-card .row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 6px 0; }
.st-ok { color: var(--ok); font-weight: 600; }
.st-wait { color: var(--warn); font-weight: 600; }
.st-bad { color: var(--bad); font-weight: 600; }
/* tab bar */
nav.tabbar { display: flex; border-top: 1px solid var(--line); background: var(--bg); padding-bottom: env(safe-area-inset-bottom); }
nav.tabbar button { flex: 1; border: 0; background: none; height: var(--tab-h); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; font-size: 12px; color: var(--muted); cursor: pointer; }
nav.tabbar button svg { width: 25px; height: 25px; }
nav.tabbar button[aria-selected="true"] { color: var(--accent); font-weight: 600; }
body.kbd nav.tabbar { display: none; }
body.kbd header .seg { display: none; }
/* sheets */
.sheet-back { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 50; display: flex; align-items: flex-end; justify-content: center; }
.sheet-back[hidden] { display: none; }
.sheet { background: var(--bg); width: 100%; max-width: 640px; border-radius: 20px 20px 0 0; padding: 14px 16px calc(16px + env(safe-area-inset-bottom)); max-height: 92dvh; overflow-y: auto; }
.sheet h2 { margin: 4px 0 12px; font-size: 20px; text-align: center; }
.sheet .grab { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 0 auto 8px; }
.sheet .btn { width: 100%; margin-top: 8px; min-height: 54px; font-size: 17px; }
.review { position: fixed; inset: 0; z-index: 60; background: #0d0e10; color: #fff; display: flex; flex-direction: column; }
.review[hidden] { display: none; }
.review .bar { display: flex; justify-content: space-between; align-items: center; padding: calc(env(safe-area-inset-top) + 6px) 10px 6px; }
.review .bar button { color: #fff; background: none; border: 0; font-size: 17px; padding: 10px; cursor: pointer; }
.review .stage { flex: 1; position: relative; overflow: hidden; touch-action: none; }
.review canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.review .hint { text-align: center; font-size: 14px; color: #c9cbd1; padding: 6px 12px; }
.review .tools { display: flex; gap: 8px; padding: 8px 12px calc(12px + env(safe-area-inset-bottom)); }
.review .tools .btn { flex: 1; background: #24262b; color: #fff; border-color: #3a3d44; }
.review .tools .btn.primary { background: var(--accent); border-color: var(--accent); }
.review .progress { position: absolute; left: 50%; bottom: 18px; transform: translateX(-50%); background: rgba(40,42,48,.92); padding: 9px 16px; border-radius: 999px; font-size: 15px; display: flex; gap: 8px; align-items: center; }
.review .progress[hidden] { display: none; }
.lines { padding: 8px 12px; display: grid; gap: 8px; max-height: 40vh; overflow: auto; }
.lines button { text-align: left; background: #24262b; color: #fff; border: 1px solid #3a3d44; border-radius: 12px; padding: 12px; font: 20px var(--math); cursor: pointer; }
.spin { width: 16px; height: 16px; border: 2px solid rgba(255,255,255,.35); border-top-color: #fff; border-radius: 50%; animation: sp 0.8s linear infinite; }
@keyframes sp { to { transform: rotate(360deg); } }
.toast { position: fixed; left: 50%; bottom: calc(var(--tab-h) + 20px + env(safe-area-inset-bottom)); transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: 15px; z-index: 80; display: flex; gap: 12px; align-items: center; }
.toast[hidden] { display: none; }
.toast button { background: none; border: 0; color: var(--bg); font-weight: 700; text-decoration: underline; cursor: pointer; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
@media (min-width: 700px) { #app { border-left: 1px solid var(--line); border-right: 1px solid var(--line); } }
