:root {
  --bg: #f7f5f0;
  --surface: #ffffff;
  --surface-2: #f0ede5;
  --text: #1f2328;
  --muted: #646a73;
  --border: #e2ddd2;
  --accent: #2856c8;
  --accent-soft: #e6ecfb;
  --ok: #1a7f4b;
  --ok-soft: #e3f4ea;
  --bad: #c0362c;
  --bad-soft: #fbe7e5;
  --warn-soft: #fff4d6;
  --hl: #8a4b00;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.04);
  --radius: 10px;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15171b; --surface: #1d2025; --surface-2: #262a31; --text: #e6e6e6; --muted: #9aa1ab;
    --border: #333943; --accent: #7aa2ff; --accent-soft: #24304d; --ok: #5cc98d; --ok-soft: #1c3528;
    --bad: #ff7b70; --bad-soft: #3d2120; --warn-soft: #3a3220; --hl: #f0b35a;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #15171b; --surface: #1d2025; --surface-2: #262a31; --text: #e6e6e6; --muted: #9aa1ab;
  --border: #333943; --accent: #7aa2ff; --accent-soft: #24304d; --ok: #5cc98d; --ok-soft: #1c3528;
  --bad: #ff7b70; --bad-soft: #3d2120; --warn-soft: #3a3220; --hl: #f0b35a;
  --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body { overflow-wrap: break-word; margin: 0; background: var(--bg); color: var(--text); font: 16px/1.6 var(--sans); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { font-family: var(--serif); line-height: 1.25; font-weight: 600; }
h1 { font-size: 2rem; margin: .2em 0 .4em; }
h2 { font-size: 1.45rem; margin: 1.6em 0 .6em; }
h3 { font-size: 1.15rem; margin: 1.2em 0 .5em; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.wrap { max-width: 920px; margin: 0 auto; padding: 0 16px; }

/* header */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10; }
.topbar .wrap { display: flex; align-items: center; gap: 18px; min-height: 56px; flex-wrap: wrap; }
.brand { font-family: var(--serif); font-weight: 700; font-size: 1.1rem; color: var(--text); white-space: nowrap; }
.brand span { color: var(--accent); }
.nav { display: flex; gap: 14px; flex: 1; flex-wrap: wrap; }
.nav a { color: var(--muted); font-size: .95rem; }
.nav a.active, .nav a:hover { color: var(--text); text-decoration: none; }
.userbox { display: flex; align-items: center; gap: 10px; font-size: .9rem; }
.theme-toggle { background: none; border: 1px solid var(--border); color: var(--muted); border-radius: 8px; padding: 2px 8px; cursor: pointer; font-size: .9rem; }

main { padding: 24px 0 64px; }
footer { color: var(--muted); font-size: .85rem; padding: 24px 0 40px; border-top: 1px solid var(--border); }

/* generic */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px 22px; box-shadow: var(--shadow); }
.card + .card { margin-top: 16px; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 16px; }
.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--accent); background: var(--accent); color: #fff;
  border-radius: 8px; padding: 8px 16px; font: inherit; font-size: .95rem; cursor: pointer; text-decoration: none; }
.btn:hover { filter: brightness(1.07); text-decoration: none; }
.btn.secondary { background: transparent; color: var(--accent); }
.btn.ghost { background: transparent; color: var(--muted); border-color: var(--border); }
.btn:disabled { opacity: .5; cursor: default; }
.pill { display: inline-block; font-size: .78rem; padding: 1px 8px; border-radius: 99px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.pill.ok { background: var(--ok-soft); color: var(--ok); }
.pill.mid { background: var(--warn-soft); color: var(--hl); }
.pill.bad { background: var(--bad-soft); color: var(--bad); }
.banner { background: var(--accent-soft); border-radius: var(--radius); padding: 10px 14px; font-size: .92rem; margin: 0 0 18px; }
.err { color: var(--bad); }
input[type=text], input[type=password], input[type=search], select {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; }
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }

/* home */
.hero { padding: 12px 0 8px; }
.hero p { font-size: 1.08rem; max-width: 640px; }
.stat { font-size: 1.9rem; font-family: var(--serif); font-weight: 600; }
.stat-label { color: var(--muted); font-size: .85rem; }

/* contents */
.toc-tools { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.toc-tools input { flex: 1; min-width: 200px; }
.toc-section { margin-bottom: 22px; }
.toc-section h2 { margin: 0 0 8px; font-size: 1.2rem; }
.toc-section h2 .muted { font-family: var(--sans); font-size: .9rem; font-weight: 400; }
.toc-list { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.toc-list li { border-top: 1px solid var(--border); }
.toc-list li:first-child { border-top: none; }
.toc-list a { display: flex; gap: 12px; align-items: baseline; padding: 9px 14px; color: var(--text); }
.toc-list a:hover { background: var(--surface-2); text-decoration: none; }
.toc-num { color: var(--muted); min-width: 2.2em; text-align: right; font-variant-numeric: tabular-nums; }
.toc-title { flex: 1; }
.toc-list .missing a { color: var(--muted); }

/* unit page */
.unit-head { display: flex; gap: 16px; align-items: baseline; flex-wrap: wrap; }
.unit-badge { font-family: var(--serif); font-size: 1rem; color: var(--accent); font-weight: 700; }
.unit-summary { font-size: 1.05rem; color: var(--muted); margin-top: 0; }
.tabs { display: flex; gap: 6px; overflow-x: auto; margin: 18px 0; border-bottom: 1px solid var(--border); }
.tabs button { white-space: nowrap; background: none; border: none; border-bottom: 2px solid transparent; font: inherit; color: var(--muted); padding: 8px 12px; cursor: pointer; margin-bottom: -1px; }
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
.theory-block { display: grid; grid-template-columns: 34px minmax(0, 1fr); gap: 0 12px; padding: 18px 0; border-top: 1px solid var(--border); }
.theory-block:first-child { border-top: none; }
.theory-key { font-family: var(--serif); font-weight: 700; font-size: 1.15rem; width: 30px; height: 30px; border-radius: 6px; background: var(--accent); color: #fff; display: grid; place-items: center; }
.theory-block h3 { margin: 2px 0 8px; }
.theory-body > :first-child { margin-top: 0; }
.theory-body b, .theory-body strong { color: var(--hl); }
ul.ex { list-style: none; padding-left: 0; margin: .5em 0 1em; }
ul.ex li { position: relative; padding: 3px 0 3px 18px; }
ul.ex li::before { content: "•"; position: absolute; left: 4px; color: var(--accent); }
.tr { display: block; color: var(--muted); font-size: .9rem; }
li .tr { display: inline; }
.note { background: var(--warn-soft); border-left: 3px solid var(--hl); padding: 10px 14px; border-radius: 0 8px 8px 0; margin: 12px 0; font-size: .95rem; }
.cmp { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin: 10px 0; }
.cmp > div { background: var(--surface-2); border-radius: 8px; padding: 6px 14px; }
table.grid { border-collapse: collapse; margin: 10px 0; font-size: .95rem; width: 100%; display: block; overflow-x: auto; }
table.grid th, table.grid td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; vertical-align: top; }
table.grid th { background: var(--surface-2); }
.wrong { color: var(--bad); text-decoration: line-through; }
code { background: var(--surface-2); padding: 0 4px; border-radius: 4px; font-size: .92em; }

/* exercises */
.exercise { margin-top: 18px; }
.ex-head { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; flex-wrap: wrap; }
.ex-head h3 { margin: 0; }
.ex-instr { margin: 6px 0 10px; }
.ex-example { color: var(--muted); font-size: .93rem; border-left: 3px solid var(--border); padding-left: 10px; margin-bottom: 12px; }
.ex-example b { color: var(--text); }
ol.items { margin: 0; padding-left: 0; list-style: none; counter-reset: item; }
ol.items > li { counter-increment: item; display: grid; grid-template-columns: 2em minmax(0, 1fr); padding: 8px 0; border-top: 1px dashed var(--border); }
ol.items > li::before { content: counter(item) "."; color: var(--muted); font-variant-numeric: tabular-nums; padding-top: 4px; }
.item-prompt { color: var(--muted); font-size: .93rem; margin-bottom: 2px; }
.item-text { line-height: 2.3; }
.blank-input { min-width: 6em; line-height: 1.4 !important; max-width: 100%; padding: 3px 8px !important; border-radius: 6px !important; border-width: 1px 1px 2px !important; }
.blank-input.wide { width: min(100%, 34em); }
.choice { display: inline-flex; gap: 4px; vertical-align: baseline; flex-wrap: wrap; }
.choice button { font: inherit; font-size: .95rem; line-height: 1.6; border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 6px; padding: 1px 10px; cursor: pointer; }
.choice button:hover { border-color: var(--accent); }
.choice button.sel { background: var(--accent); border-color: var(--accent); color: #fff; }
.is-ok .blank-input, .is-ok.choice button.sel { border-color: var(--ok) !important; background: var(--ok-soft); color: var(--ok); }
.is-bad .blank-input, .is-bad.choice button.sel { border-color: var(--bad) !important; background: var(--bad-soft); color: var(--bad); }
.blank { display: inline; }
.fix { display: inline-block; font-size: .85rem; color: var(--ok); margin-left: 4px; font-weight: 600; }
.explain { font-size: .9rem; color: var(--muted); margin-top: 2px; line-height: 1.5; }
.explain.bad { color: var(--text); }
.ex-actions { display: flex; gap: 10px; align-items: center; margin-top: 14px; flex-wrap: wrap; }
.ex-result { font-weight: 600; }
.ex-result.ok { color: var(--ok); }
.ex-result.bad { color: var(--bad); }
.unit-nav { display: flex; justify-content: space-between; gap: 12px; margin-top: 30px; flex-wrap: wrap; }

/* progress / mistakes */
.bars { display: flex; flex-direction: column; gap: 8px; }
.bar-row { display: grid; grid-template-columns: minmax(120px, 220px) 1fr 70px; gap: 10px; align-items: center; font-size: .92rem; }
.bar { height: 10px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--ok); }
.bar > span.mid { background: var(--hl); }
.bar > span.bad { background: var(--bad); }
.days { display: flex; gap: 3px; align-items: flex-end; height: 70px; margin-top: 8px; }
.days span { flex: 1; min-width: 4px; max-width: 18px; background: var(--accent); border-radius: 3px 3px 0 0; opacity: .8; }
.mistake { padding: 12px 0; border-top: 1px solid var(--border); }
.mistake:first-child { border-top: none; }
.mistake-q { margin: 4px 0; }
.mistake-q .gap { background: var(--surface-2); border-radius: 4px; padding: 0 6px; font-weight: 600; }
.mistake-meta { font-size: .85rem; color: var(--muted); display: flex; gap: 10px; flex-wrap: wrap; }
.given-bad { color: var(--bad); text-decoration: line-through; }
.given-ok { color: var(--ok); font-weight: 600; }
.filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
table.list { width: 100%; border-collapse: collapse; font-size: .92rem; }
table.list td, table.list th { padding: 6px 8px; border-top: 1px solid var(--border); text-align: left; }
table.list th { color: var(--muted); font-weight: 500; border-top: none; }

/* auth */
.auth { max-width: 380px; margin: 30px auto; }
.auth form { display: flex; flex-direction: column; gap: 12px; }
.auth label { display: flex; flex-direction: column; gap: 4px; font-size: .9rem; color: var(--muted); }

@media (max-width: 640px) {
  h1 { font-size: 1.6rem; }
  .cmp { grid-template-columns: minmax(0, 1fr); }
  ol.items > li { grid-template-columns: 1.7em minmax(0, 1fr); }
  .blank-input { min-width: 4.5em; }
  .card { padding: 16px; }
  .theory-block { grid-template-columns: minmax(0, 1fr); }
  .theory-key { margin-bottom: 6px; }
  .bar-row { grid-template-columns: 1fr 60px; }
  .bar-row .bar { grid-column: 1 / -1; grid-row: 2; }
  .nav { order: 3; flex-basis: 100%; padding-bottom: 8px; }
}
