/* Mobile-first styles for the Study App shell and the seven writing modules. */
:root {
  --bg: #f8fafc; --surface: #ffffff; --ink: #0f172a; --muted: #64748b; --line: #e2e8f0;
  --accent: #0f172a; --accent-ink: #ffffff; --brand: #0ea5e9;
  --ok: #16a34a; --ok-bg: #dcfce7; --bad: #dc2626; --bad-bg: #fee2e2; --warn: #d97706; --warn-bg: #fef3c7;
  --radius: 12px; --tap: 44px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #0b1220; --surface: #111a2e; --ink: #e5eefb; --muted: #94a3b8; --line: #1f2b45; --accent: #38bdf8; --accent-ink: #06121f; --ok-bg: #0f3d23; --bad-bg: #4a1414; --warn-bg: #433107; }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--ink); line-height: 1.5; padding-bottom: calc(72px + var(--safe-bottom)); }
a { color: inherit; }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5rem; }
h2 { font-size: 1.4rem; } h3 { font-size: 1.05rem; }
p { margin: 0 0 .75rem; }
.muted { color: var(--muted); } .tiny { font-size: .8rem; } .center { text-align: center; } .right { margin-left: auto; }
.hidden { display: none !important; }

/* ---------- shell ---------- */
.topbar { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; justify-content: space-between; padding: .6rem 1rem; background: var(--surface); border-bottom: 1px solid var(--line); }
.brand { font-weight: 800; text-decoration: none; letter-spacing: .02em; }
.hud { display: flex; gap: .5rem; font-size: .85rem; }
.hud-item { background: var(--bg); border: 1px solid var(--line); border-radius: 999px; padding: .15rem .6rem; }
.view { max-width: 680px; margin: 0 auto; padding: 1rem; }
.tabs { position: fixed; bottom: 0; left: 0; right: 0; display: flex; background: var(--surface); border-top: 1px solid var(--line); padding-bottom: var(--safe-bottom); z-index: 5; }
.tabs a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: .1rem; padding: .5rem 0; font-size: .7rem; text-decoration: none; color: var(--muted); min-height: var(--tap); }
.tabs a.active { color: var(--brand); font-weight: 600; }
.net-status { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(64px + var(--safe-bottom)); font-size: .72rem; color: var(--muted); background: var(--surface); border: 1px solid var(--line); border-radius: 999px; padding: .1rem .6rem; pointer-events: none; }
.net-status.offline { color: var(--warn); }

/* ---------- cards ---------- */
.card { display: block; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; margin-bottom: .75rem; text-decoration: none; }
.cards, .modules { display: grid; gap: .75rem; }
.card.subject, .card.module, .card.review { display: flex; gap: .9rem; align-items: flex-start; position: relative; transition: transform .1s; }
.card.subject:active, .card.module:active { transform: scale(.99); }
.card.disabled { opacity: .55; pointer-events: none; }
.card .icon { font-size: 1.7rem; line-height: 1; }
.card-body { flex: 1; min-width: 0; }
.card-body p { margin-bottom: .25rem; }
.mod-num { width: 34px; height: 34px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); font-weight: 700; display: grid; place-items: center; flex-shrink: 0; }
.due-pill { position: absolute; top: .75rem; right: .75rem; background: var(--brand); color: #fff; font-size: .7rem; border-radius: 999px; padding: .1rem .5rem; font-weight: 600; }
.page-head { display: flex; justify-content: space-between; align-items: baseline; gap: 1rem; margin-bottom: .75rem; }
.hero { margin-bottom: 1rem; }
.meter, .progress { height: 6px; background: var(--line); border-radius: 999px; overflow: hidden; margin: .4rem 0; }
.meter .bar, .progress .bar { height: 100%; background: var(--brand); transition: width .3s; }

/* ---------- buttons ---------- */
button, .button { font: inherit; }
button.primary, .button.primary { background: var(--accent); color: var(--accent-ink); border: none; padding: .7rem 1.2rem; border-radius: 10px; cursor: pointer; font-weight: 600; min-height: var(--tap); text-decoration: none; display: inline-flex; align-items: center; justify-content: center; }
button.primary:disabled { opacity: .45; cursor: not-allowed; }
.button { background: var(--surface); border: 1px solid var(--line); color: var(--ink); padding: .7rem 1.2rem; border-radius: 10px; text-decoration: none; min-height: var(--tap); display: inline-flex; align-items: center; justify-content: center; }
button.ghost { background: transparent; border: 1px dashed var(--line); color: var(--muted); padding: .45rem .8rem; border-radius: 10px; cursor: pointer; min-height: 40px; }
button.ghost:disabled { opacity: .5; }
button.link, a.link { background: none; border: none; color: var(--muted); cursor: pointer; padding: .3rem .2rem; text-decoration: none; font-size: .9rem; }
.actions { display: flex; gap: .6rem; flex-wrap: wrap; margin-top: 1rem; }
.actions > * { flex: 1 1 140px; }

/* ---------- session ---------- */
.session-head { display: flex; align-items: center; gap: .75rem; margin-bottom: .5rem; }
.session-head .progress { flex: 1; margin: 0; }
.counter { font-size: .8rem; color: var(--muted); min-width: 2.5rem; text-align: right; }
.item-meta { display: flex; align-items: center; gap: .6rem; font-size: .75rem; color: var(--muted); margin-bottom: .75rem; }
.mod-tag { background: var(--surface); border: 1px solid var(--line); border-radius: 999px; padding: .1rem .55rem; }
.tier { letter-spacing: .1em; } .streak { color: var(--warn); font-weight: 700; }
.item-body { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; }
.instruction { font-weight: 600; margin-bottom: .75rem; }
.sentence { font-size: 1.1rem; line-height: 1.9; }
.quote { margin: 0 0 1rem; padding: .6rem .9rem; border-left: 4px solid var(--brand); background: var(--bg); border-radius: 6px; font-size: 1.05rem; }
.hint-row { margin-top: .6rem; }
.hintbox { margin-top: .6rem; background: var(--warn-bg); border-radius: 8px; padding: .6rem .8rem; font-size: .9rem; }

/* fill-in-the-blank input (inline, normalised on check) */
input.blank { font: inherit; font-size: 1.05rem; padding: .35rem .6rem; border: 2px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--ink); min-width: 9ch; width: 11ch; max-width: 100%; text-align: center; }
input.blank:focus { outline: none; border-color: var(--brand); }
input.blank.correct { border-color: var(--ok); background: var(--ok-bg); }
input.blank.wrong { border-color: var(--bad); background: var(--bad-bg); }
.inline-input { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; margin-top: .5rem; }
.arrow-label { color: var(--muted); }
.blank-slot { display: inline-block; min-width: 4ch; border-bottom: 2px solid var(--muted); padding: 0 .3rem; text-align: center; font-weight: 600; }
.blank-slot.correct { color: var(--ok); border-color: var(--ok); } .blank-slot.wrong { color: var(--bad); border-color: var(--bad); text-decoration: line-through; }
mark.phrase { background: var(--warn-bg); color: inherit; padding: 0 .2rem; border-radius: 4px; }

/* tokenised sentences: chips keep bare words; punctuation sits outside the tap target */
.tokens { display: flex; flex-wrap: wrap; align-items: baseline; gap: .35rem .15rem; font-size: 1.08rem; line-height: 1.6; }
.tok { display: inline-flex; align-items: baseline; white-space: nowrap; }
.punct { color: var(--muted); }
.chip { font: inherit; background: var(--bg); border: 1.5px solid var(--line); color: var(--ink); border-radius: 8px; padding: .25rem .5rem; min-height: 38px; cursor: pointer; }
.chip.selected { border-color: var(--brand); background: color-mix(in srgb, var(--brand) 18%, var(--surface)); font-weight: 600; }
.chip.correct { border-color: var(--ok); background: var(--ok-bg); } .chip.wrong { border-color: var(--bad); background: var(--bad-bg); }
.chip.struck { text-decoration: line-through; color: var(--muted); border-style: dashed; }
.chip:disabled { cursor: default; }

/* option buttons */
.options { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: .75rem; }
.options-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.option { font: inherit; background: var(--bg); border: 1.5px solid var(--line); color: var(--ink); border-radius: 10px; padding: .6rem .8rem; min-height: var(--tap); cursor: pointer; text-align: center; }
.option:hover:not(:disabled) { border-color: var(--brand); }
.option.correct { border-color: var(--ok); background: var(--ok-bg); font-weight: 600; } .option.wrong { border-color: var(--bad); background: var(--bad-bg); }
.option:disabled { cursor: default; opacity: .9; }

/* theory cards + pills */
.theory h3 { margin-bottom: .4rem; }
.pills { display: flex; flex-wrap: wrap; gap: .4rem; margin: .5rem 0 .75rem; }
.pill { background: var(--bg); border: 1px solid var(--line); border-radius: 999px; padding: .15rem .6rem; font-size: .9rem; }

/* paragraph sorting */
.sortrow { border: 1px solid var(--line); border-radius: 10px; padding: .6rem; margin-bottom: .6rem; }
.sortrow.row-correct { border-color: var(--ok); } .sortrow.row-wrong { border-color: var(--bad); }
.para { margin-bottom: .5rem; font-size: .95rem; }
.segmented { display: grid; grid-template-columns: repeat(auto-fit, minmax(90px, 1fr)); gap: .3rem; }
.seg { font: inherit; font-size: .85rem; background: var(--bg); border: 1.5px solid var(--line); color: var(--ink); border-radius: 8px; padding: .45rem .3rem; min-height: 40px; cursor: pointer; }
.seg.selected { border-color: var(--brand); background: color-mix(in srgb, var(--brand) 18%, var(--surface)); font-weight: 600; }
.seg.correct { border-color: var(--ok); background: var(--ok-bg); } .seg.wrong { border-color: var(--bad); background: var(--bad-bg); }

/* register overlay */
.overlay { position: fixed; inset: 0; background: rgba(2, 6, 23, .55); display: flex; align-items: flex-end; justify-content: center; z-index: 20; padding: 0; }
.sheet { width: 100%; max-width: 680px; background: var(--surface); border-radius: 16px 16px 0 0; padding: 1rem 1rem calc(1rem + var(--safe-bottom)); box-shadow: 0 -8px 30px rgba(0,0,0,.25); animation: rise .18s ease-out; }
@keyframes rise { from { transform: translateY(30px); opacity: 0; } to { transform: none; opacity: 1; } }
@media (min-width: 600px) { .overlay { align-items: center; padding: 1rem; } .sheet { border-radius: 16px; } }

/* cohesion slotting: slots are inline-flex so [ n ] never wraps mid-bracket */
.target { background: var(--warn-bg); border: 2px dashed var(--warn); border-radius: 10px; padding: .7rem .9rem; margin-bottom: .9rem; cursor: grab; font-weight: 500; }
.target.dragging { opacity: .5; }
.paragraph { line-height: 2; font-size: 1rem; }
.part { display: inline; }
.slot { font: inherit; display: inline-flex; align-items: center; justify-content: center; vertical-align: baseline; white-space: nowrap; min-width: var(--tap); min-height: 32px; margin: 0 .1rem; padding: 0 .4rem; border-radius: 8px; border: 1.5px dashed var(--brand); background: color-mix(in srgb, var(--brand) 10%, var(--surface)); color: var(--brand); font-weight: 700; cursor: pointer; line-height: 1.4; }
.slot.over { background: color-mix(in srgb, var(--brand) 30%, var(--surface)); }
.slot.filled { white-space: normal; display: inline; text-align: left; line-height: 1.6; border-style: solid; color: var(--ink); font-weight: 500; padding: .1rem .35rem; background: var(--warn-bg); border-color: var(--warn); }
.slot.correct { border-color: var(--ok); background: var(--ok-bg); } .slot.wrong { border-color: var(--bad); background: var(--bad-bg); }
.slot:disabled { cursor: default; }
.slot-num { letter-spacing: .05em; }

/* epistemic badges */
.badges { display: flex; flex-wrap: wrap; gap: .4rem; margin-bottom: .75rem; }
.badge { display: inline-flex; gap: .3rem; align-items: center; border-radius: 999px; padding: .2rem .7rem; font-size: .85rem; border: 1px solid var(--line); background: var(--bg); }
.badge-label { color: var(--muted); font-weight: 600; text-transform: uppercase; font-size: .7rem; letter-spacing: .04em; }
.badge-low, .badge-l-g { border-color: var(--bad); } .badge-high, .badge-h-g { border-color: var(--ok); } .badge-medium, .badge-medel, .badge-medium-high, .badge-medelh-g { border-color: var(--warn); }

/* feedback */
.feedback { margin-top: .75rem; border-radius: var(--radius); padding: 1rem; border: 1px solid var(--line); background: var(--surface); }
.feedback.correct { border-color: var(--ok); background: var(--ok-bg); } .feedback.wrong { border-color: var(--bad); background: var(--bad-bg); } .feedback.partial { border-color: var(--warn); background: var(--warn-bg); }
.fb-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: .4rem; font-size: 1.05rem; }
.xp { font-weight: 700; color: var(--brand); }
.feedback .label { font-weight: 600; }
.feedback .model { font-style: italic; }

/* summary & stats */
.summary { text-align: center; }
.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: .6rem; margin: 1rem 0; }
.stat { display: flex; flex-direction: column; align-items: center; background: var(--bg); border-radius: 10px; padding: .6rem; }
.stat strong { font-size: 1.3rem; }
.badge-list, .badge-grid { display: grid; gap: .5rem; margin-bottom: 1rem; }
.badge-grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
.badge-card { display: flex; flex-direction: column; gap: .15rem; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: .6rem; font-size: .85rem; text-align: left; }
.badge-card.locked { opacity: .55; }
.bucket-list { list-style: none; padding: 0; margin: 0 0 1rem; }
.bucket-list li { display: flex; align-items: center; gap: .3rem; padding: .5rem .2rem; border-bottom: 1px solid var(--line); font-size: .9rem; }
.lang-tag { font-size: .65rem; font-weight: 700; background: var(--line); border-radius: 4px; padding: .05rem .3rem; }
.error { color: var(--bad); }
pre { white-space: pre-wrap; font-size: .8rem; }

@media (min-width: 720px) {
  .cards { grid-template-columns: 1fr 1fr; }
  .modules { grid-template-columns: 1fr 1fr; }
  .options-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* ---------- coding subject ---------- */
:root { --code-bg: #f1f5f9; --code-gutter: #e2e8f0; --code-active: rgba(14, 165, 233, .08); }
@media (prefers-color-scheme: dark) { :root { --code-bg: #0d1627; --code-gutter: #111c33; --code-active: rgba(56, 189, 248, .08); } }
.rich p { margin-bottom: .6rem; } .rich ul { margin: 0 0 .6rem 1.2rem; padding: 0; } .rich h4 { margin: .6rem 0 .3rem; }
.rich code, .examples code, .test code, .quiz-q code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .9em; background: var(--code-bg); border-radius: 4px; padding: .05rem .3rem; }
pre.code { white-space: pre; background: var(--code-bg); border: 1px solid var(--line); border-radius: 10px; padding: .7rem .9rem; overflow-x: auto; font-size: .85rem; line-height: 1.5; margin: 0 0 .75rem; }
pre.code code { background: none; padding: 0; font-size: inherit; }
pre.code.small { font-size: .8rem; padding: .4rem .6rem; margin-bottom: .4rem; }
pre.err { background: var(--bad-bg); border: 1px solid var(--bad); border-radius: 8px; padding: .5rem .7rem; font-size: .78rem; white-space: pre-wrap; margin: .4rem 0; }
.math-display { text-align: center; margin: .6rem 0; overflow-x: auto; }
.math-src { font-family: ui-monospace, monospace; color: var(--muted); }
.puzzle-title { margin-bottom: .2rem; }
.lang-pill { display: inline-block; font-size: .72rem; background: var(--bg); border: 1px solid var(--line); border-radius: 999px; padding: .1rem .55rem; color: var(--muted); margin-bottom: .6rem; }
.ex-title { font-weight: 600; font-size: .85rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin: .4rem 0 .3rem; }
.examples { display: grid; gap: .35rem; margin-bottom: .8rem; }
.example { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: .45rem .6rem; font-size: .9rem; overflow-x: auto; }
.example .arrow { color: var(--muted); } .example .expect { color: var(--ok); }
.ex-label { font-size: .7rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin: .2rem 0; }
.editor-wrap { margin: .5rem 0; }
.cm-editor { border-radius: 10px; }
textarea.code-fallback { width: 100%; min-height: 200px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 14px; line-height: 1.5; background: var(--code-bg); color: var(--ink); border: 1px solid var(--line); border-radius: 10px; padding: .6rem; resize: vertical; tab-size: 4; }
.toolbar { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; margin: .5rem 0; }
.toolbar .button, .toolbar .primary { flex: 1 1 140px; }
.toolbar .link.tiny { flex: 1 1 100%; text-align: right; font-size: .75rem; }
.pystatus { font-size: .78rem; color: var(--muted); min-height: 1.2em; margin-bottom: .4rem; }
.pystatus.error { color: var(--bad); }
.results-head { font-weight: 700; margin: .5rem 0 .3rem; }
.results-head.ok { color: var(--ok); } .results-head.bad { color: var(--bad); }
.tests { display: grid; gap: .35rem; }
.test { border: 1px solid var(--line); border-radius: 8px; padding: .4rem .6rem; font-size: .85rem; }
.test-ok { border-color: var(--ok); background: var(--ok-bg); } .test-bad { border-color: var(--bad); background: var(--bad-bg); }
.test-head { font-weight: 600; }
.test-detail { margin-top: .3rem; display: grid; gap: .15rem; overflow-x: auto; }
.test-detail .label { color: var(--muted); }
details.stdout, details.solution { margin-top: .5rem; font-size: .85rem; }
details.stdout summary, details.solution summary { cursor: pointer; color: var(--muted); }
.giveup { margin-top: .6rem; text-align: right; }
.lesson h3 { margin-bottom: .5rem; }
.quiz-q { font-size: 1.05rem; font-weight: 500; }
.quiz-expl { margin-top: .6rem; font-size: .9rem; background: var(--bg); border-radius: 8px; padding: .5rem .7rem; }
.card.track .icon { font-size: 2rem; }
@media (min-width: 720px) { .toolbar .link.tiny { flex: 0 0 auto; margin-left: auto; } }

/* ---------- math micro-drills ---------- */
.math-icon { font-family: "Times New Roman", serif; font-weight: 700; color: var(--brand); }
.math-stage { background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: .8rem .6rem; margin: .5rem 0; text-align: center; overflow-x: auto; font-size: 1.15rem; }
.math-stage.stage-correct { border-color: var(--ok); background: var(--ok-bg); } .math-stage.stage-wrong { border-color: var(--bad); background: var(--bad-bg); }
.math-stage .katex-display { margin: .2rem 0; }
.math-opt .katex { font-size: 1.05em; }
.lightning-prompt { font-size: 1.6rem; text-align: center; padding: 1rem .5rem; }
.lightning-prompt .katex { font-size: 1.1em; }
.counter.urgent { color: var(--bad); font-weight: 700; }
.toast { position: fixed; left: 50%; bottom: calc(132px + var(--safe-bottom)); transform: translateX(-50%); background: var(--surface); border: 2px solid var(--line); border-radius: 999px; padding: .5rem 1.1rem; font-size: 1.05rem; display: flex; gap: .5rem; align-items: center; z-index: 15; box-shadow: 0 6px 24px rgba(0,0,0,.18); animation: rise .15s ease-out; max-width: 92vw; }
.toast.correct { border-color: var(--ok); background: var(--ok-bg); } .toast.wrong, .toast.partial { border-color: var(--bad); background: var(--bad-bg); }
.toast .model { margin: 0; font-size: .9rem; }
.tray, .terms { display: flex; flex-wrap: wrap; gap: .5rem; margin: .6rem 0; }
.chip.token, .chip.term, .chip.signchip { min-height: var(--tap); padding: .3rem .7rem; font-size: 1.05rem; display: inline-flex; align-items: center; gap: .3rem; }
.chip.term { cursor: grab; } .chip.term.selected { border-color: var(--brand); background: color-mix(in srgb, var(--brand) 18%, var(--surface)); box-shadow: 0 0 0 2px color-mix(in srgb, var(--brand) 40%, transparent); }
.chip.signchip .sgn { font-weight: 800; color: var(--brand); width: 1em; text-align: center; font-size: 1.2em; }
.picker { background: var(--surface); border: 1px solid var(--brand); border-radius: 10px; padding: .6rem; margin: .4rem 0; }
.picker .options { margin-top: .4rem; }
.eq-label { text-align: center; color: var(--muted); font-size: 1.3rem; line-height: 1; }
.shake { animation: shake .3s; }
@keyframes shake { 0%, 100% { transform: translateX(0); } 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }
.rowop { display: grid; gap: .5rem; margin: .4rem 0; }
.rowpick { display: flex; flex-wrap: wrap; align-items: center; gap: .4rem; }
.rowpick .label { color: var(--muted); font-size: .85rem; min-width: 5.5rem; }
.rowpick .seg { min-width: 3rem; flex: 0 0 auto; padding: .45rem .7rem; }
.slider-row { display: flex; align-items: center; gap: .8rem; }
.slider-row input[type=range] { flex: 1; min-height: var(--tap); accent-color: var(--brand); }
.k-label { font-family: ui-monospace, monospace; min-width: 4.5rem; }
.op-summary { font-size: .85rem; margin: 0 0 .3rem; }
.solution-lines { display: grid; gap: .4rem; margin: .4rem 0; }
.sol-line { font: inherit; display: flex; align-items: center; gap: .6rem; width: 100%; text-align: left; background: var(--bg); border: 1.5px solid var(--line); border-radius: 10px; padding: .4rem .6rem; min-height: var(--tap); cursor: pointer; color: var(--ink); overflow-x: auto; }
.sol-line .katex-display { margin: 0; flex: 1; text-align: left; }
.sol-line .line-no { color: var(--muted); font-size: .8rem; min-width: 1.2rem; }
.sol-line.selected { border-color: var(--brand); background: color-mix(in srgb, var(--brand) 14%, var(--surface)); }
.sol-line.correct { border-color: var(--ok); background: var(--ok-bg); } .sol-line.wrong { border-color: var(--bad); background: var(--bad-bg); }
.sol-line:disabled { cursor: default; }
