:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #eef1f8;
  --text: #1c2333;
  --muted: #5d6679;
  --border: #d7dce8;
  --primary: #2f62d8;
  --primary-text: #ffffff;
  --primary-soft: #e3ebfd;
  --ok: #1d8a4e;
  --ok-soft: #dcf3e5;
  --almost: #a86400;
  --almost-soft: #fdf0d7;
  --bad: #c62f3a;
  --bad-soft: #fbe1e3;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(20 30 60 / 6%), 0 4px 14px rgb(20 30 60 / 6%);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151f;
    --surface: #1a2030;
    --surface-2: #232b3e;
    --text: #e8ecf5;
    --muted: #9aa3b8;
    --border: #323c55;
    --primary: #6d95f5;
    --primary-text: #0d1220;
    --primary-soft: #253459;
    --ok: #4cc785;
    --ok-soft: #173a29;
    --almost: #f0b44c;
    --almost-soft: #3d2f12;
    --bad: #f07079;
    --bad-soft: #451d23;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 17px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

#app { max-width: 640px; margin: 0 auto; padding: 12px 16px 48px; }

h1 { font-size: 1.35rem; margin: 0; }
h2 { font-size: 1.15rem; margin: 1.2em 0 .5em; }
h3 { font-size: 1rem; margin: 1em 0 .4em; }
p { margin: .5em 0; }

.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }
.hidden { display: none !important; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.spread { justify-content: space-between; }
.grow { flex: 1; }
.stack > * + * { margin-top: 10px; }

/* ---------------------------------------------------------------- controls */
button, .btn {
  font: inherit;
  min-height: 48px;
  padding: 10px 18px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
}
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--primary); color: var(--primary-text); border-color: var(--primary); font-weight: 600; }
button.danger { color: var(--bad); border-color: var(--bad); }
button.ghost { background: transparent; border-color: transparent; color: var(--muted); }
button.small { min-height: 36px; padding: 6px 12px; font-size: .9rem; }
button.block { display: block; width: 100%; }
button:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible {
  outline: 3px solid var(--primary); outline-offset: 2px;
}

input, textarea, select {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  min-height: 48px;
  width: 100%;
}
textarea { min-height: 90px; resize: vertical; }
input[type="checkbox"], input[type="radio"] { width: 22px; min-height: 22px; height: 22px; }
label { display: block; font-size: .9rem; color: var(--muted); margin: 8px 0 4px; }
label.inline { display: flex; gap: 8px; align-items: center; color: var(--text); font-size: 1rem; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}

.banner {
  border-radius: var(--radius);
  padding: 12px 16px;
  margin: 10px 0;
  background: var(--primary-soft);
}
.banner.warn { background: var(--almost-soft); }
.banner.error { background: var(--bad-soft); }

/* ---------------------------------------------------------------- child app header */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 12px; overflow: hidden; background: var(--surface); }
.segmented button { border: 0; border-radius: 0; min-width: 64px; font-weight: 600; }
.segmented button[aria-pressed="true"] { background: var(--primary); color: var(--primary-text); }
.syncdot { font-size: .8rem; color: var(--muted); }

/* box tabs */
.tabs { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; margin: 8px 0 14px; }
.tabs button { padding: 6px 2px; min-height: 64px; display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1.15; }
.tabs button .n { font-size: .78rem; color: var(--muted); }
.tabs button .due { font-size: .72rem; font-weight: 700; color: var(--primary); }
.tabs button[aria-selected="true"] { border: 2px solid var(--primary); background: var(--primary-soft); }

.boxpanel .big { font-size: 2.6rem; font-weight: 700; line-height: 1; }
.progress { height: 10px; border-radius: 6px; background: var(--surface-2); overflow: hidden; margin: 12px 0; }
.progress > span { display: block; height: 100%; background: var(--primary); border-radius: 6px; transition: width .3s; }

/* ---------------------------------------------------------------- session */
.sessionbar { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.sessionbar .progress { flex: 1; margin: 0; }
.dirlabel { font-size: .85rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.prompt { font-size: 1.6rem; font-weight: 700; margin: 6px 0 4px; overflow-wrap: anywhere; }
.subprompt { color: var(--muted); }
.context { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; margin: 8px 0; font-size: 1.05rem; }
.context mark { background: var(--almost-soft); color: inherit; border-radius: 4px; padding: 0 2px; }
.options { display: grid; gap: 10px; margin-top: 14px; }
.options button { text-align: left; min-height: 56px; font-size: 1.05rem; }
.options button[aria-pressed="true"] { border: 2px solid var(--primary); background: var(--primary-soft); }
.options button.right { border: 2px solid var(--ok); background: var(--ok-soft); }
.options button.wrong { border: 2px solid var(--bad); background: var(--bad-soft); }
.options button:disabled, .tfbuttons button:disabled { opacity: .75; }
.options button.right:disabled, .options button.wrong:disabled { opacity: 1; }
.tfbuttons { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 14px; }
.gaptext { font-size: 1.2rem; line-height: 2.4; }
.gaptext input { display: inline-block; width: 9em; min-height: 40px; padding: 4px 8px; margin: 0 2px; }
.answer-input { font-size: 1.25rem; margin-top: 12px; }
.actions { margin-top: 16px; display: grid; gap: 10px; }
.flagbtn { margin-top: 26px; }

.feedback { margin-top: 16px; border-radius: var(--radius); padding: 14px 16px; }
.feedback.correct { background: var(--ok-soft); }
.feedback.almost { background: var(--almost-soft); }
.feedback.wrong { background: var(--bad-soft); }
.feedback .verdict { font-weight: 700; font-size: 1.15rem; }
.marked { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 1.15rem; letter-spacing: .02em; margin: 6px 0; overflow-wrap: anywhere; }
.marked .err { color: var(--bad); font-weight: 700; text-decoration: underline wavy; }
.marked .extra { color: var(--bad); text-decoration: line-through; }
.marked .miss { color: var(--bad); font-weight: 700; }
.solution { font-weight: 600; }

.summary-list { list-style: none; padding: 0; margin: 10px 0; }
.summary-list li { display: flex; justify-content: space-between; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.up { color: var(--ok); font-weight: 600; }
.down { color: var(--bad); font-weight: 600; }

dialog { border: 0; border-radius: var(--radius); padding: 20px; max-width: min(92vw, 560px); background: var(--surface); color: var(--text); box-shadow: 0 10px 40px rgb(0 0 0 / 30%); }
dialog::backdrop { background: rgb(10 15 30 / 45%); }

.spinner { width: 28px; height: 28px; border: 3px solid var(--border); border-top-color: var(--primary); border-radius: 50%; animation: spin 1s linear infinite; display: inline-block; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } .progress > span { transition: none; } }

/* ---------------------------------------------------------------- admin */
body.admin #app { max-width: 1100px; }
.adminnav { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 6px; margin-bottom: 12px; }
.adminnav button { white-space: nowrap; min-height: 42px; }
.adminnav button[aria-current="page"] { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }
.badge { display: inline-block; font-size: .75rem; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); vertical-align: middle; }
.badge.warn { background: var(--almost-soft); color: var(--almost); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.info { background: var(--primary-soft); color: var(--primary); }
table { width: 100%; border-collapse: collapse; font-size: .95rem; }
th, td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: .85rem; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.tablewrap { overflow-x: auto; }
.split { display: grid; gap: 16px; }
@media (min-width: 900px) {
  .split { grid-template-columns: minmax(0, 5fr) minmax(0, 6fr); align-items: start; }
  .split .sticky { position: sticky; top: 10px; }
}
.photo { width: 100%; border-radius: 10px; border: 1px solid var(--border); touch-action: pinch-zoom; }
.entry { border: 1px solid var(--border); border-radius: 12px; padding: 12px; background: var(--surface); }
.entry.unsure { border-color: var(--almost); }
.entry.removed { opacity: .45; }
.entry .grid2 { display: grid; gap: 8px; }
@media (min-width: 600px) { .entry .grid2 { grid-template-columns: 1fr 1fr; } }
.entry input, .entry textarea, .entry select { min-height: 40px; padding: 6px 10px; }
.tasklist { list-style: none; padding: 0; margin: 6px 0; }
.tasklist li { border-top: 1px solid var(--border); padding: 8px 0; display: flex; gap: 8px; align-items: flex-start; }
.tasklist li { flex-wrap: wrap; }
.tasklist li .grow { overflow-wrap: anywhere; flex: 1 1 220px; min-width: 0; }
.tasklist li .badge { white-space: nowrap; }
.steps { display: flex; gap: 4px; flex-wrap: wrap; margin-bottom: 12px; }
.steps span { font-size: .8rem; padding: 4px 10px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.steps span.on { background: var(--primary); color: var(--primary-text); }
.steps span.done { background: var(--ok-soft); color: var(--ok); }
.qr { background: #fff; padding: 10px; border-radius: 10px; width: 220px; height: 220px; image-rendering: pixelated; }
.linkbox { font-family: ui-monospace, monospace; font-size: .8rem; word-break: break-all; background: var(--surface-2); padding: 8px; border-radius: 8px; }
.toast { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 10px; z-index: 20; max-width: 90vw; }
