:root {
  --bg: #f7f4ee;
  --surface: #ffffff;
  --surface-2: #efe9df;
  --text: #1f1b16;
  --muted: #6b6257;
  --line: #e2d9cc;
  --accent: #b4531c;
  --accent-ink: #ffffff;
  --accent-soft: #f8e6d6;
  --mine-bg: #fff4e8;
  --mine-edge: #e07a2c;
  --mask: #ddd3c5;
  --good: #2f7d4f;
  --good-soft: #e1f1e6;
  --warn: #a5690f;
  --warn-soft: #f8ecd4;
  --bad: #b23a3a;
  --bad-soft: #f8e1e1;
  --shadow: 0 1px 2px rgb(60 40 20 / 0.06), 0 4px 16px rgb(60 40 20 / 0.06);

  --ui-font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --script-font: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --script-size: 19px;
  --bar-h: 56px;
  --tabs-h: calc(60px + env(safe-area-inset-bottom));
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141210;
    --surface: #1d1a17;
    --surface-2: #29241f;
    --text: #f1ebe2;
    --muted: #a59a8c;
    --line: #362f29;
    --accent: #f0a35e;
    --accent-ink: #1a1209;
    --accent-soft: #3a2817;
    --mine-bg: #2a1f15;
    --mine-edge: #f0a35e;
    --mask: #3d362f;
    --good: #6cc792;
    --good-soft: #1b3024;
    --warn: #e7b65a;
    --warn-soft: #362a14;
    --bad: #ef7c7c;
    --bad-soft: #3a1c1c;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.3);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #141210;
  --surface: #1d1a17;
  --surface-2: #29241f;
  --text: #f1ebe2;
  --muted: #a59a8c;
  --line: #362f29;
  --accent: #f0a35e;
  --accent-ink: #1a1209;
  --accent-soft: #3a2817;
  --mine-bg: #2a1f15;
  --mine-edge: #f0a35e;
  --mask: #3d362f;
  --good: #6cc792;
  --good-soft: #1b3024;
  --warn: #e7b65a;
  --warn-soft: #362a14;
  --bad: #ef7c7c;
  --bad-soft: #3a1c1c;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.3);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 var(--ui-font);
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.2; }
h2 { font-size: 1.2rem; margin: 1.6rem 0 .4rem; }
h3 { font-size: 1rem; margin: 1.4rem 0 .6rem; }
button, input, select, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.center { text-align: center; }
.hint { color: var(--muted); font-size: .88rem; margin: .5rem 0 1rem; }
.error { color: var(--bad); }
.spacer { flex: 1; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- bars ---------- */

.bar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  min-height: var(--bar-h);
  padding: env(safe-area-inset-top) max(8px, env(safe-area-inset-right)) 0 max(8px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.bar h1 { font-size: 1.1rem; margin: 0 0 0 4px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 1.15rem; padding-left: 8px; }
.titlebox { flex: 1; min-width: 0; }
.titlebox .t { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.titlebox .sub { font-size: .8rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.subbar {
  position: sticky; top: calc(var(--bar-h) + env(safe-area-inset-top)); z-index: 19;
  display: flex; gap: 8px; align-items: center;
  padding: 8px 16px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.subbar select {
  flex: 1; min-width: 0;
  height: 38px; padding: 0 30px 0 12px;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--surface) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8'%3E%3Cpath d='M1 1l5 5 5-5' fill='none' stroke='%23888' stroke-width='2'/%3E%3C/svg%3E") no-repeat right 12px center;
  appearance: none;
  font-size: .92rem;
}

.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: flex;
  height: var(--tabs-h);
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--line);
}
.tab {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); text-decoration: none; font-size: .72rem; font-weight: 600;
}
.tab.active { color: var(--accent); }

.actionbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 25;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}
.actionbar.above-tabs { bottom: var(--tabs-h); padding-bottom: 12px; }

/* ---------- page ---------- */

.page {
  max-width: 720px; margin: 0 auto;
  padding: 16px 16px calc(110px + env(safe-area-inset-bottom));
}
.page.narrow { max-width: 560px; }
.page.with-tabs { padding-bottom: calc(var(--tabs-h) + 32px); }
.page.with-actionbar.with-tabs { padding-bottom: calc(var(--tabs-h) + 100px); }
.page.with-footer { padding-bottom: calc(var(--tabs-h) + 170px); }
.section-title { margin-top: .5rem; }

/* ---------- buttons & inputs ---------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 46px; padding: 0 18px;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--surface); color: var(--text);
  font-weight: 600; text-decoration: none; cursor: pointer;
  transition: transform .08s, background .15s;
  touch-action: manipulation;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--bad); }
.btn.block { display: flex; width: 100%; margin-top: 10px; }
.btn.small { min-height: 36px; padding: 0 14px; font-size: .9rem; }
.btn[disabled] { opacity: .45; pointer-events: none; }
.btn.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 44px; height: 44px; border: 0; border-radius: 12px;
  background: transparent; color: var(--text); cursor: pointer; text-decoration: none;
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn.small { width: 40px; height: 40px; color: var(--muted); }
.icon-btn.small svg { width: 18px; height: 18px; }

.round-btn {
  display: grid; place-items: center;
  width: 64px; height: 64px; border: 0; border-radius: 50%;
  background: var(--accent); color: var(--accent-ink); cursor: pointer;
  box-shadow: var(--shadow);
}
.round-btn:active { transform: scale(.95); }
.round-btn svg { width: 26px; height: 26px; }

.chip {
  display: inline-flex; align-items: center; gap: 6px;
  height: 38px; padding: 0 12px;
  border: 1px solid var(--line); border-radius: 999px;
  background: var(--surface); font-size: .85rem; font-weight: 600; white-space: nowrap; cursor: pointer;
}
.chip svg { width: 16px; height: 16px; }
.chip.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0; }

.field { display: block; margin: 12px 0; }
.field > span { display: flex; justify-content: space-between; font-size: .85rem; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
.field input:not([type=range]), .field select, textarea {
  width: 100%; min-height: 46px; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--surface);
  font-size: 16px; /* stops iOS zooming on focus */
}
.field input[type=range] { width: 100%; accent-color: var(--accent); }
textarea { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 14px; line-height: 1.5; resize: vertical; }
.row { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 24px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.seg { display: flex; padding: 3px; gap: 3px; background: var(--surface-2); border-radius: 12px; margin: 12px 0; }
.field .seg { margin: 0; }
.seg button {
  flex: 1; min-height: 40px; border: 0; border-radius: 9px; background: transparent;
  font-weight: 600; font-size: .9rem; color: var(--muted); cursor: pointer;
}
.seg button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.toggle {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 12px 0; border-bottom: 1px solid var(--line); cursor: pointer;
}
.toggle small { display: block; color: var(--muted); font-size: .82rem; margin-top: 2px; }
.toggle input {
  appearance: none; flex: none;
  width: 48px; height: 28px; border-radius: 14px; background: var(--mask);
  position: relative; cursor: pointer; transition: background .15s;
}
.toggle input::after {
  content: ""; position: absolute; top: 3px; left: 3px;
  width: 22px; height: 22px; border-radius: 50%; background: #fff;
  box-shadow: 0 1px 3px rgb(0 0 0 / .3); transition: transform .15s;
}
.toggle input:checked { background: var(--accent); }
.toggle input:checked::after { transform: translateX(20px); }

.meter { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.meter i { display: block; height: 100%; background: var(--accent); border-radius: 3px; transition: width .3s; }
.meter.big { height: 10px; border-radius: 5px; }

.notice { padding: 12px 14px; border-radius: 12px; background: var(--warn-soft); color: var(--text); font-size: .92rem; margin: 12px 0; }
.panel, .progress-card { padding: 16px; margin-top: 16px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.progress-card p { margin: 10px 0 0; }

/* ---------- library ---------- */

.empty { padding: 12vh 8px 0; }
.empty h1 { font: 700 2.2rem/1.1 var(--script-font); margin: 0 0 12px; }
.empty p { color: var(--muted); font-size: 1.05rem; max-width: 34em; }
.empty.small { padding: 40px 0; text-align: center; }
.cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.card {
  display: flex; align-items: center; gap: 14px;
  padding: 16px; border-radius: var(--radius);
  background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow);
  color: inherit; text-decoration: none;
}
.card-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.card-main strong { font-size: 1.05rem; }
.card-main .muted { font-size: .88rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-meter { width: 84px; flex: none; text-align: right; font-size: .8rem; color: var(--muted); }
.card-meter span { font-weight: 700; color: var(--text); font-size: .95rem; }
.card-meter .meter { margin: 4px 0; }

/* ---------- import ---------- */

.drop {
  display: flex; flex-direction: column; align-items: center; gap: 6px; text-align: center;
  padding: 28px 20px; border: 2px dashed var(--line); border-radius: var(--radius);
  background: var(--surface); cursor: pointer; color: var(--muted);
}
.drop strong { color: var(--text); }
.drop svg { color: var(--accent); }
.drop input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.drop:focus-within { border-color: var(--accent); }

/* ---------- setup ---------- */

.chars { list-style: none; padding: 0; margin: 8px 0; border-top: 1px solid var(--line); }
.char-row { display: flex; align-items: center; border-bottom: 1px solid var(--line); }
.char { flex: 1; display: flex; align-items: center; gap: 12px; min-height: 52px; cursor: pointer; min-width: 0; }
.char input { width: 22px; height: 22px; accent-color: var(--accent); flex: none; }
.char .name { flex: 1; font-weight: 600; letter-spacing: .02em; overflow: hidden; text-overflow: ellipsis; }
.char .count { color: var(--muted); font-size: .85rem; white-space: nowrap; }
.rare { margin-top: 16px; }
.rare summary { cursor: pointer; padding: 8px 0; font-weight: 600; }

.edit textarea { min-height: 70vh; margin-top: 8px; }
.help { font-size: .9rem; color: var(--muted); }
.help summary { cursor: pointer; font-weight: 600; color: var(--text); padding: 4px 0; }
.help code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }

/* ---------- script ---------- */

.script { font-family: var(--script-font); font-size: var(--script-size); line-height: 1.5; }
.sc-heading { font-family: var(--ui-font); font-size: .78em; letter-spacing: .12em; text-transform: uppercase; color: var(--muted); text-align: center; margin: 1.6em 0 1em; }
.sc-dir, .sc-idir { font-style: italic; color: var(--muted); }
.sc-dir { margin: .8em 0; padding: 0 4px; font-size: .92em; }
.sc-idir { font-size: .92em; }
.sc-line { margin: 0 0 .9em; padding: 2px 4px; border-radius: 10px; }
.sc-line p { margin: 0; }
.who {
  display: flex; align-items: center; gap: 8px;
  font-family: var(--ui-font); font-size: .7em; font-weight: 700; letter-spacing: .08em; color: var(--muted);
}
.sc-line.mine { background: var(--mine-bg); border-left: 4px solid var(--mine-edge); padding: 10px 12px; }
.sc-line.mine .who { color: var(--accent); }
.dots { display: inline-flex; gap: 3px; }
.dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--mask); }
.dots i.on { background: var(--good); }

.masked { cursor: pointer; }
.masked .real { display: none; }
.masked .mask { color: var(--muted); letter-spacing: .06em; }
.masked.revealed .real { display: inline; }
.masked.revealed .mask { display: none; }

/* ---------- drill ---------- */

.drill-top { display: flex; align-items: center; gap: 12px; font-family: var(--ui-font); font-size: .82rem; color: var(--muted); margin-bottom: 12px; }
.drill-top .meter { flex: 1; }
.cues .sc-line.cue:not(.mine) { opacity: .85; }
.cues .sc-heading { margin-top: .5em; }
.cue.collapsed { cursor: pointer; }
.cue.collapsed .more { color: var(--accent); font-weight: 700; }
.answer-card {
  scroll-margin-bottom: calc(var(--tabs-h) + 110px);
  margin-top: 8px; padding: 14px 16px 16px;
  background: var(--surface); border: 2px solid var(--mine-edge); border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.answer-card .who { color: var(--accent); margin-bottom: 6px; }
.answer { margin: 0; font-size: 1.05em; }
.answer .redact {
  color: transparent; background: var(--mask); border-radius: 4px;
  -webkit-box-decoration-break: clone; box-decoration-break: clone;
  user-select: none; line-height: 1.75;
}
.answer .letters { color: var(--muted); letter-spacing: .08em; }
.answer .shown { animation: fade .25s; }
@keyframes fade { from { opacity: 0; } }

.drill-actions, .run-controls {
  position: fixed; left: 0; right: 0; bottom: var(--tabs-h); z-index: 25;
  padding: 10px 12px 12px;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-top: 1px solid var(--line);
}
.drill-actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.drill-actions .btn { flex-direction: column; gap: 0; min-height: 62px; padding: 6px; line-height: 1.2; }
.drill-actions .btn small { font-weight: 500; font-size: .72rem; opacity: .75; }
.drill-actions.grade .btn.bad { background: var(--bad-soft); border-color: transparent; color: var(--bad); }
.drill-actions.grade .btn.warn { background: var(--warn-soft); border-color: transparent; color: var(--warn); }
.drill-actions.grade .btn.good { background: var(--good-soft); border-color: transparent; color: var(--good); }

.summary { font-family: var(--ui-font); text-align: center; padding-top: 6vh; max-width: 420px; margin: 0 auto; }
.summary h2 { font: 700 1.8rem var(--script-font); }
.tally { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 20px 0; }
.tally div { padding: 12px 4px; border-radius: 12px; font-size: .85rem; }
.tally b { display: block; font-size: 1.6rem; }
.tally .good { background: var(--good-soft); color: var(--good); }
.tally .warn { background: var(--warn-soft); color: var(--warn); }
.tally .bad { background: var(--bad-soft); color: var(--bad); }

/* ---------- run ---------- */

.run-list [data-q] { transition: opacity .2s, background .2s; cursor: pointer; }
.run-list .past { opacity: .5; }
.run-list .current:not(.mine) { background: var(--surface-2); padding: 10px 12px; }
.run-list .current.mine { box-shadow: 0 0 0 2px var(--mine-edge); }
.run-list .current.sc-dir, .run-list .current.sc-heading { color: var(--text); }
.gap { text-align: center; color: var(--muted); letter-spacing: .3em; font-size: .8em; margin: .4em 0 1em; }
.run-controls { font-family: var(--ui-font); }
.run-status { margin: 0 0 8px; text-align: center; font-size: .85rem; color: var(--muted); min-height: 1.2em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.run-status.yours { color: var(--accent); font-weight: 700; }
.run-buttons { display: flex; align-items: center; justify-content: center; gap: 12px; }
.run-buttons .btn.wide { flex: 1; max-width: 240px; min-height: 56px; }
.run-buttons .btn:not(.wide) { min-height: 56px; }

/* ---------- sheet ---------- */

.sheet {
  width: 100%; max-width: 560px; max-height: 88vh; max-height: 88dvh;
  margin: auto auto 0; padding: 0;
  border: 0; border-radius: 20px 20px 0 0;
  background: var(--surface); color: var(--text);
  box-shadow: 0 -8px 40px rgb(0 0 0 / .25);
}
.sheet::backdrop { background: rgb(0 0 0 / .4); }
.sheet[open] { animation: up .2s ease-out; }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
.sheet-body { padding: 8px 20px calc(24px + env(safe-area-inset-bottom)); }
.sheet-head { position: sticky; top: 0; display: flex; align-items: center; justify-content: space-between; padding: 12px 0; background: var(--surface); z-index: 1; }
.sheet-head h2 { margin: 0; }
.sheet h3 { color: var(--muted); font-size: .78rem; letter-spacing: .1em; text-transform: uppercase; margin-top: 1.6rem; }
.sheet-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; }
.sheet .btn.block { margin-top: 18px; }

@media (min-width: 720px) {
  .sheet { margin: auto; border-radius: 20px; }
  .drill-actions, .run-controls, .actionbar { padding-left: calc(50vw - 340px); padding-right: calc(50vw - 340px); }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}
