:root {
  color-scheme: light dark;
  --bg: #f6f4ef;
  --panel: #ffffff;
  --text: #1f2a27;
  --muted: #5d6b66;
  --line: #dcd8cf;
  --accent: #2f5d50;
  --accent-text: #ffffff;
  --accent-soft: #e3eee9;
  --new: #2563a8;
  --learn: #b4530a;
  --due: #257a3e;
  --again: #b3261e;
  --hard: #a15c00;
  --good: #257a3e;
  --easy: #2563a8;
  --ok-bg: #e3f3e7;
  --bad-bg: #fbe6e4;
  --mask: #e9b949;
  --radius: 12px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141a18;
    --panel: #1d2523;
    --text: #e8ece9;
    --muted: #a0ada8;
    --line: #34403c;
    --accent: #7cc0a8;
    --accent-text: #10201b;
    --accent-soft: #24332e;
    --new: #7fb2ee;
    --learn: #f0a35e;
    --due: #7fd197;
    --again: #f28b82;
    --hard: #f0b45e;
    --good: #7fd197;
    --easy: #7fb2ee;
    --ok-bg: #1d3325;
    --bad-bg: #3a2321;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 var(--font);
  min-height: 100dvh;
}
#app { display: flex; flex-direction: column; min-height: 100dvh; }
main { width: 100%; max-width: 60rem; margin: 0 auto; padding: 1rem; flex: 1; outline: none; }
h1 { font-size: 1.5rem; margin: 0 0 .5rem; line-height: 1.2; }
h2 { font-size: 1.15rem; margin: 0 0 .5rem; }
h3 { font-size: 1rem; margin: 0 0 .5rem; }
p { margin: 0 0 .75rem; }
a { color: var(--accent); }
hr { border: 0; border-top: 1px solid var(--line); margin: 1rem 0; width: 100%; }
code { background: var(--accent-soft); padding: .1em .35em; border-radius: 4px; font-size: .9em; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap;
  padding: .6rem 1rem; padding-top: max(.6rem, env(safe-area-inset-top));
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 700; color: var(--text); text-decoration: none; }
.topnav { display: flex; gap: .25rem; flex-wrap: wrap; }
.topnav a { padding: .4rem .7rem; border-radius: 8px; text-decoration: none; color: var(--muted); font-weight: 600; }
.topnav a.active { background: var(--accent-soft); color: var(--accent); }
.footer { text-align: center; padding: 1rem; font-size: .85rem; padding-bottom: max(1rem, env(safe-area-inset-bottom)); }

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.25rem; }
.narrow { max-width: 26rem; margin: 2rem auto; }
.stack { display: flex; flex-direction: column; gap: 1rem; }
.stack > * { margin-bottom: 0; }
.row { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.row.spread { justify-content: space-between; }
.row.center { justify-content: center; }
.row.end { align-items: flex-end; }
.row.tight { gap: .35rem; }
.row > input[type="text"], .row > input[type="search"] { flex: 1; min-width: 10rem; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr)); gap: 1rem; }
.center { text-align: center; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.hint { color: var(--muted); font-size: .85rem; margin: 0; }
.error { color: var(--again); }
.pre { white-space: pre-wrap; overflow-wrap: anywhere; }
.crumbs { font-size: .9rem; color: var(--muted); }
details > summary { cursor: pointer; font-weight: 600; }
details[open] > summary { margin-bottom: 1rem; }
fieldset { border: 1px solid var(--line); border-radius: 8px; padding: .5rem 1rem; margin: 0; }

input, select, textarea, button, .button { font: inherit; color: inherit; }
input[type="text"], input[type="email"], input[type="password"], input[type="number"], input[type="search"], select, textarea {
  width: 100%; padding: .55rem .7rem; border: 1px solid var(--line); border-radius: 8px; background: var(--bg);
}
input.tiny { width: 5rem; }
textarea { resize: vertical; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.field { display: flex; flex-direction: column; gap: .25rem; margin-bottom: 1rem; flex: 1; }
.stack > .field, .grid2 > .field, .row > .field { margin-bottom: 0; }
.label { font-weight: 600; font-size: .9rem; }
.check { display: flex; gap: .5rem; align-items: center; padding: .2rem 0; }

button, .button {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  padding: .55rem 1rem; border-radius: 8px; border: 1px solid var(--line); background: var(--panel);
  cursor: pointer; text-decoration: none; font-weight: 600; min-height: 2.5rem;
}
button:hover, .button:hover { border-color: var(--accent); }
button:disabled { opacity: .55; cursor: default; }
.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.danger { color: var(--again); border-color: var(--again); }
.block { width: 100%; }
.big { min-height: 3.25rem; font-size: 1.05rem; }
button.link { border: 0; background: none; padding: 0; min-height: 0; color: var(--accent); font-weight: 400; text-decoration: underline; }

.deck-list { list-style: none; margin: 0 0 1rem; padding: 0; }
.deck-row { display: flex; align-items: center; gap: .5rem; padding: .55rem 0; border-bottom: 1px solid var(--line); }
.deck-row:last-child { border-bottom: 0; }
.indent { flex: none; }
.deck-name { font-weight: 600; text-decoration: none; overflow-wrap: anywhere; }
.counts { margin-left: auto; display: flex; gap: .75rem; font-variant-numeric: tabular-nums; }
.count { font-weight: 700; min-width: 1.5rem; text-align: right; }
.count.new { color: var(--new); }
.count.learn { color: var(--learn); }
.count.due { color: var(--due); }
.count.zero { color: var(--line); }
.tag-pill { font-size: .75rem; padding: .1rem .5rem; border-radius: 99px; background: var(--accent-soft); color: var(--accent); font-weight: 600; white-space: nowrap; }

.progress { display: flex; flex-direction: column; gap: .25rem; }
.bar { height: .6rem; background: var(--accent-soft); border-radius: 99px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--accent); border-radius: 99px; transition: width .3s; }

.study { display: flex; flex-direction: column; gap: 1rem; max-width: 42rem; margin: 0 auto; }
.flashcard { min-height: 14rem; display: flex; align-items: center; justify-content: center; padding: 2rem 1.25rem; }
.card-face { width: 100%; text-align: center; }
.card-text { font-size: 1.35rem; white-space: pre-wrap; overflow-wrap: anywhere; }
.card-text.extra { font-size: 1rem; color: var(--muted); margin-top: .75rem; }
.cloze { color: var(--new); font-weight: 700; }
.cloze.shown { text-decoration: underline; }
.ratings { display: grid; grid-template-columns: repeat(4, 1fr); gap: .5rem; }
.rate { flex-direction: column; gap: 0; min-height: 3.5rem; border-width: 2px; }
.rate .small { font-weight: 400; min-height: 1.2em; }
.rate.again { border-color: var(--again); color: var(--again); }
.rate.hard { border-color: var(--hard); color: var(--hard); }
.rate.good { border-color: var(--good); color: var(--good); }
.rate.easy { border-color: var(--easy); color: var(--easy); }
@media (hover: none) { .keys { display: none; } }

.choices { display: grid; grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr)); gap: .5rem; }
.choice { justify-content: flex-start; text-align: left; min-height: 3.25rem; white-space: pre-wrap; overflow-wrap: anywhere; font-weight: 400; }
.choice.ok, .result.ok, .results .ok { background: var(--ok-bg); border-color: var(--good); }
.choice.bad, .result.bad, .results .bad { background: var(--bad-bg); border-color: var(--again); }
.choice:disabled { opacity: 1; }
.result { border: 1px solid; border-radius: 8px; padding: .75rem 1rem; }
.results { list-style: none; padding: 0; margin: 1rem 0 0; display: flex; flex-direction: column; gap: .5rem; }
.results li { border: 1px solid; border-radius: 8px; padding: .6rem .8rem; }
.question .check { padding: .35rem 0; }
.match-row { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; align-items: center; padding: .4rem 0; border-bottom: 1px solid var(--line); }

.occ { position: relative; display: inline-block; max-width: 100%; line-height: 0; user-select: none; -webkit-user-select: none; }
.occ img { max-width: 100%; max-height: 70vh; border-radius: 8px; }
.occ.editing { touch-action: none; cursor: crosshair; min-height: 2rem; }
.occ-rect { position: absolute; background: var(--mask); border: 2px solid #8a6a12; border-radius: 4px; padding: 0; min-height: 0; color: #3a2c05; font-size: .8rem; line-height: 1; }
.occ-rect.target { background: #d2493f; border-color: #7d1e17; }
.occ-rect.target.revealed { background: transparent; border-style: dashed; }
.occ-rect.ghost { opacity: .6; pointer-events: none; }

.browse { display: grid; grid-template-columns: 14rem 1fr; gap: 1rem; align-items: start; }
.browse > * { min-width: 0; }
@media (max-width: 720px) { .browse { grid-template-columns: 1fr; } }
.tag-tree { list-style: none; margin: 0; padding-left: 0; }
.tag-tree .tag-tree { padding-left: 1rem; }
.tag-tree li { padding: .15rem 0; overflow-wrap: anywhere; }
.bulk { padding: .5rem 0; }
.bulk:empty { display: none; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .92rem; }
th, td { text-align: left; padding: .45rem .5rem; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-size: .8rem; font-weight: 600; white-space: nowrap; }
td.clip { max-width: 16rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(9rem, 1fr)); gap: 1rem; margin-top: .5rem; }
.stat-value { font-size: 1.75rem; font-weight: 700; line-height: 1.1; }
.stat-label { font-weight: 600; font-size: .9rem; }
.chart { width: 100%; height: auto; display: block; }
.chart .axis { stroke: var(--line); }
.chart .tick { fill: var(--muted); font-size: 11px; }
.chart .bar-a { fill: var(--accent); }
.chart .bar-b { fill: var(--again); }
.legend { font-size: .85rem; color: var(--muted); display: flex; flex-wrap: wrap; gap: .35rem; align-items: center; margin-top: .5rem; }
.key { display: inline-block; width: .8rem; height: .8rem; border-radius: 3px; }
.key.bar-a { background: var(--accent); }
.key.bar-b { background: var(--again); margin-left: .5rem; }
.stackbar { display: flex; height: 1.25rem; border-radius: 6px; overflow: hidden; gap: 2px; }
.seg.new { background: var(--new); }
.seg.learn { background: var(--learn); }
.seg.young { background: #6fb58c; }
.seg.mature { background: var(--accent); }

.toast {
  position: fixed; left: 50%; bottom: max(1.25rem, env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: .65rem 1rem; border-radius: 8px; z-index: 30;
  max-width: calc(100vw - 2rem); box-shadow: 0 4px 16px rgb(0 0 0 / .25);
}
.toast.error { background: var(--again); color: #fff; }
@media (prefers-color-scheme: dark) { .toast.error { color: #1a0d0c; } }

.consent {
  position: fixed; left: 1rem; right: 1rem; bottom: max(1rem, env(safe-area-inset-bottom)); z-index: 20;
  max-width: 34rem; margin: 0 auto; background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 1rem 1.25rem; box-shadow: 0 8px 30px rgb(0 0 0 / .25);
}
.consent .row { margin-top: .75rem; }
.noscript { text-align: center; padding: 2rem; }

@media (max-width: 480px) {
  main { padding: .75rem; }
  .panel { padding: 1rem; }
  .card-text { font-size: 1.2rem; }
  .match-row { grid-template-columns: 1fr; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

.occ { align-self: flex-start; width: fit-content; }
.card-face .occ { margin: .75rem auto 0; }
.row.tight select { width: auto; }
.panel > .progress { margin: .5rem 0 1rem; }
.panel > .row { margin-bottom: .75rem; }
.panel > .row:last-child, .panel > form.row { margin-bottom: 0; }
