/* flash-cards design system: CSS variables, light/dark (auto, or forced via html[data-theme]), mobile first.
   Text colours meet WCAG AA (4.5:1) on --surface and --bg in both themes. */
:root {
  --bg: #f6f7fb; --surface: #fff; --surface-2: #eef0f6; --text: #1d2130; --muted: #5f6678; --border: #dde1ea;
  --primary: #3b5bdb; --primary-text: #fff; --primary-soft: #e3e9ff;
  --new: #1864ab; --learning: #c2410c; --review: #237032; --danger: #c92a2a; --danger-soft: #fff0f0; --ok: #237032;
  --radius: 12px; --radius-sm: 8px; --shadow: 0 1px 2px rgb(20 25 40 / .06), 0 4px 16px rgb(20 25 40 / .06);
  --gap: 1rem; --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12141b; --surface: #1b1e28; --surface-2: #252a37; --text: #e6e8ef; --muted: #9aa1b3; --border: #2f3544;
    --primary: #748ffc; --primary-text: #0d1020; --primary-soft: #262f55;
    --new: #4dabf7; --learning: #ff922b; --review: #51cf66; --danger: #ff6b6b; --danger-soft: #3a1e22; --ok: #69db7c;
    --shadow: 0 1px 2px rgb(0 0 0 / .4); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #12141b; --surface: #1b1e28; --surface-2: #252a37; --text: #e6e8ef; --muted: #9aa1b3; --border: #2f3544;
  --primary: #748ffc; --primary-text: #0d1020; --primary-soft: #262f55;
  --new: #4dabf7; --learning: #ff922b; --review: #51cf66; --danger: #ff6b6b; --danger-soft: #3a1e22; --ok: #69db7c;
  --shadow: 0 1px 2px rgb(0 0 0 / .4); color-scheme: dark;
}

/* --- base --- */
*, *::before, *::after { 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); }
h1, h2, h3 { line-height: 1.25; margin: 0; }
h1 { font-size: 1.6rem; } h2 { font-size: 1.2rem; }
p { margin: 0; }
a { color: var(--primary); }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
table { border-collapse: collapse; width: 100%; }
th, td { padding: .55rem .75rem; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { font-size: .85rem; color: var(--muted); font-weight: 600; }
tbody tr:last-child td { border-bottom: 0; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.ok { color: var(--ok); font-weight: 600; }
.boot { padding: 2rem; text-align: center; color: var(--muted); }
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip-link { position: absolute; left: .5rem; top: -3rem; z-index: 20; padding: .5rem 1rem; border-radius: var(--radius-sm);
             background: var(--primary); color: var(--primary-text); font-weight: 600; }
.skip-link:focus { top: .5rem; }
main:focus, h1:focus { outline: none; }  /* focused by the router for screen readers, not by the user */
.loading { display: flex; align-items: center; gap: .6rem; color: var(--muted); animation: appear .2s ease .3s backwards; }
.spinner { width: 1.1rem; height: 1.1rem; border: 2px solid var(--border); border-top-color: var(--primary); border-radius: 50%;
           animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes appear { from { opacity: 0; } }
.btn[aria-busy="true"]::before { content: ""; width: .9rem; height: .9rem; border: 2px solid currentColor;
                                 border-right-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; }
.alert .retry { margin-top: .5rem; color: inherit; border-color: currentColor; }
.alert.error > strong { display: block; }

/* --- layout --- */
.container { width: 100%; max-width: 960px; margin: 0 auto; padding: 0 max(1rem, env(safe-area-inset-left)); }
main.container { padding-top: 1.5rem; padding-bottom: 3rem; }
.stack { display: flex; flex-direction: column; gap: var(--gap); }
.grid { display: grid; gap: var(--gap); grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: var(--gap); flex-wrap: wrap; }
.actions { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.actions.end { justify-content: flex-end; }
.actions.sticky { position: sticky; bottom: 0; padding: .75rem 0 max(.75rem, env(safe-area-inset-bottom));
                  background: linear-gradient(transparent, var(--bg) 35%); }
.spacer { flex: 1; }
.grow { flex: 1; min-width: 0; }
.row-wrap { display: flex; align-items: center; gap: .75rem 1rem; flex-wrap: wrap; }

/* --- top bar --- */
.topbar { position: sticky; top: 0; z-index: 10; background: var(--surface); border-bottom: 1px solid var(--border);
          padding-top: env(safe-area-inset-top); }
.bar { display: flex; align-items: center; gap: 1rem; min-height: 3.5rem; }
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 700; color: var(--text); text-decoration: none; }
.logo { width: 1.4rem; height: 1rem; border-radius: 4px; background: var(--primary);
        box-shadow: inset .25rem .2rem 0 -.05rem var(--primary), inset .75rem .6rem 0 -.1rem var(--surface); }
nav { display: flex; gap: .25rem; margin-left: auto; overflow-x: auto; }
nav a { padding: .4rem .75rem; border-radius: 999px; color: var(--muted); text-decoration: none; font-weight: 500; white-space: nowrap; }
nav a:hover { color: var(--text); background: var(--surface-2); }
nav a.active { color: var(--primary); background: var(--primary-soft); }
nav a .count { display: inline-block; min-width: 1.4em; margin-left: .3rem; padding: 0 .35rem; border-radius: 999px;
  background: var(--danger-soft); color: var(--danger); font-size: .8em; font-weight: 600; text-align: center; }
.sync:empty { display: none; }
.sync .synced { color: var(--muted); font-size: .8rem; white-space: nowrap; }
.badge.offline { background: var(--danger-soft); color: var(--danger); }
@media (max-width: 480px) { .brand { font-size: 0; gap: 0; } .profile, .sync .synced { display: none; } }

/* --- surfaces --- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.tile { padding: 1rem; display: flex; flex-direction: column; gap: .75rem; }
.tile-head { display: flex; align-items: flex-start; justify-content: space-between; gap: .5rem; }
.tile-nav { margin-top: auto; flex-wrap: nowrap; }
.empty { padding: 2rem 1rem; text-align: center; display: flex; flex-direction: column; align-items: center; gap: .75rem; }
.group { padding: 1rem 1.25rem 1.25rem; margin: 0; min-width: 0; }
fieldset.group legend { font-weight: 700; font-size: 1.05rem; padding: 0 .35rem; margin-left: -.35rem; }
.table-wrap { overflow-x: auto; }
.list { list-style: none; margin: 0; padding: 0; }
.list .row { display: flex; align-items: center; gap: .75rem; padding: .75rem 1rem; border-bottom: 1px solid var(--border); }
.list .row:last-child { border-bottom: 0; }
.group .list .row { padding-inline: 0; }
.badge { display: inline-block; padding: .1rem .55rem; border-radius: 999px; background: var(--surface-2);
         color: var(--muted); font-size: .8rem; font-weight: 600; white-space: nowrap; }
.alert { padding: .75rem 1rem; border-radius: var(--radius-sm); border: 1px solid; }
.alert.error { background: var(--danger-soft); border-color: var(--danger); color: var(--danger); }
.alert.info { background: var(--primary-soft); border-color: var(--primary); }
.alert ul { margin: .35rem 0 0; padding-left: 1.2rem; }
.done { color: var(--ok); font-weight: 600; }

/* --- counts --- */
.counts { display: flex; gap: .5rem; list-style: none; margin: 0; padding: 0; }
.count { flex: 1; display: flex; flex-direction: column; align-items: center; padding: .4rem;
         border-radius: var(--radius-sm); background: var(--surface-2); }
.count .n { font-size: 1.4rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.count .label { font-size: .75rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.count.new .n { color: var(--new); } .count.learning .n { color: var(--learning); } .count.review .n { color: var(--review); }
.count.zero .n { color: var(--muted); }

/* --- buttons --- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: .35rem; min-height: 2.5rem;
       padding: .45rem 1rem; border: 1px solid transparent; border-radius: var(--radius-sm); font: inherit;
       font-weight: 600; text-decoration: none; cursor: pointer; background: var(--surface-2); color: var(--text); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--primary); color: var(--primary-text); }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); }
.btn.ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.icon { padding: .45rem; min-width: 2.5rem; }
.btn.small { min-height: 2rem; padding: .25rem .6rem; font-size: .875rem; }
.btn.icon.small { min-width: 2rem; }

/* --- forms --- */
input, select { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
                border-radius: var(--radius-sm); padding: .45rem .6rem; min-height: 2.5rem; width: 100%; }
input[type="checkbox"], input[type="radio"] { width: 1.15rem; height: 1.15rem; min-height: 0; accent-color: var(--primary); }
input:invalid:not(:placeholder-shown) { border-color: var(--danger); }
.form-grid { display: grid; gap: 1rem 1.25rem; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); margin-top: .5rem; }
.field { display: flex; flex-direction: column; gap: .3rem; }
.field > label, .field > span, .field .label { font-weight: 600; font-size: .9rem; }
.field .control { display: flex; gap: .4rem; align-items: center; }
.field.check .control { min-height: 2.5rem; }
.field.overridden > label::after { content: " •"; color: var(--primary); }
.choices { border: 0; padding: 0; margin: 0; display: flex; flex-direction: column; gap: .5rem; }
.choices legend { font-weight: 600; font-size: .9rem; margin-bottom: .4rem; }
.choice { display: flex; align-items: center; gap: .6rem; padding: .6rem .75rem; border: 1px solid var(--border);
          border-radius: var(--radius-sm); cursor: pointer; }
.choice:has(input:checked) { border-color: var(--primary); background: var(--primary-soft); }
.choice-text { display: flex; flex-wrap: wrap; align-items: baseline; column-gap: .6rem; }
.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; align-self: flex-start; }
.segmented label { padding: .45rem .9rem; cursor: pointer; text-transform: capitalize; }
.segmented label + label { border-left: 1px solid var(--border); }
.segmented label.on { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.segmented label:has(:focus-visible) { outline: 2px solid var(--primary); outline-offset: -2px; }
.segmented a { padding: .45rem .9rem; border-radius: 0; color: var(--text); text-decoration: none; white-space: nowrap; }
.segmented a + a { border-left: 1px solid var(--border); }
.segmented a.on { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.segmented.small label { padding: .2rem .6rem; font-size: .85rem; }
nav.segmented { margin: 0; gap: 0; border-radius: var(--radius-sm); }

/* --- dialog --- */
.dialog { width: min(440px, calc(100vw - 2rem)); padding: 1.25rem; border: 1px solid var(--border);
          border-radius: var(--radius); background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.dialog::backdrop { background: rgb(10 12 20 / .5); }

/* --- import --- */
.dropzone { display: flex; flex-direction: column; align-items: center; gap: .25rem; padding: 2.5rem 1rem; cursor: pointer;
            border: 2px dashed var(--border); border-radius: var(--radius); background: var(--surface); text-align: center;
            transition: border-color .15s, background .15s; }
.dropzone:hover, .dropzone.over { border-color: var(--primary); background: var(--primary-soft); }
.dropzone:has(:focus-visible) { outline: 2px solid var(--primary); outline-offset: 2px; }
.dropzone .big { font-size: 2rem; line-height: 1; color: var(--primary); }
.status { width: 1.75rem; height: 1.75rem; flex: none; display: grid; place-items: center; border-radius: 50%;
          font-weight: 700; background: var(--surface-2); }
.status.done { color: var(--ok); } .status.error { color: var(--danger); }
.status.uploading { animation: pulse 1s ease-in-out infinite alternate; }
@keyframes pulse { to { opacity: .4; } }

/* --- study --- */
.study { max-width: 680px; margin: 0 auto; }
.progress { height: .4rem; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.progress span { display: block; height: 100%; background: var(--primary); transition: width .3s ease; }
.current-new .count.new, .current-learning .count.learning, .current-review .count.review { opacity: 1; box-shadow: inset 0 -3px 0 currentColor; }
.current-new .count.new { color: var(--new); } .current-learning .count.learning { color: var(--learning); }
.current-review .count.review { color: var(--review); }
.flashcard { display: grid; perspective: 1400px; cursor: pointer; animation: card-in .2s ease-out; }
.flashcard.flipped { cursor: default; }
.face { grid-area: 1 / 1; min-height: 16rem; padding: 1.25rem; display: flex; flex-direction: column; gap: .75rem;
        backface-visibility: hidden; -webkit-backface-visibility: hidden; transition: transform .45s ease; }
.face.back { transform: rotateY(180deg); }
.flashcard.flipped .front { transform: rotateY(-180deg); }
.flashcard.flipped .back { transform: rotateY(0); }
@keyframes card-in { from { opacity: 0; transform: translateY(.5rem); } }
.meta { display: flex; flex-wrap: wrap; gap: .35rem; min-height: 1.5rem; }
.badge.topic { background: var(--primary-soft); color: var(--primary); }
.chip { padding: .1rem .5rem; border: 1px solid var(--border); border-radius: 999px; font-size: .75rem; color: var(--muted); }
.side { display: flex; flex-direction: column; align-items: center; gap: .5rem; text-align: center; }
.side .text { font-size: 1.2rem; overflow-wrap: anywhere; }
.side.big .text { font-size: 1.7rem; font-weight: 600; }
.side .say { vertical-align: middle; margin-left: .25rem; border-color: transparent; }
.reading { color: var(--muted); font-size: 1rem; }
.media { display: flex; flex-direction: column; align-items: center; gap: .5rem; width: 100%; }
.media img, .media video { max-width: 100%; max-height: 16rem; border-radius: var(--radius-sm); }
.media audio { width: min(100%, 320px); }
.face .hint { margin-top: auto; text-align: center; }
.front .side { flex: 1; justify-content: center; }
.face hr { width: 100%; border: 0; border-top: 1px solid var(--border); margin: .25rem 0; }
.answer-bar { position: sticky; bottom: 0; display: grid; grid-template-columns: repeat(4, 1fr); gap: .5rem;
              padding: .75rem 0 max(.75rem, env(safe-area-inset-bottom)); background: linear-gradient(transparent, var(--bg) 30%); }
.answer-bar .show { grid-column: 1 / -1; min-height: 3.25rem; }
.rate { flex-direction: column; gap: 0; min-height: 3.75rem; padding: .35rem .25rem; border-color: var(--border); background: var(--surface); }
.rate .ivl { font-size: .8rem; color: var(--muted); font-weight: 500; }
.rate:hover:not(:disabled) { background: var(--surface-2); }
.r1 { --c: var(--danger); } .r2 { --c: var(--learning); } .r3 { --c: var(--review); } .r4 { --c: var(--new); }
.rate { border-bottom: 3px solid var(--c); } .rate > span:nth-child(2) { color: var(--c); }
kbd { font: .7rem/1 var(--font); padding: .15rem .3rem; border: 1px solid var(--border); border-radius: 4px; color: var(--muted); }
@media (hover: none) { kbd { display: none; } }
.finished .big-check { font-size: 2.5rem; line-height: 1; color: var(--ok); }
.rating-summary { display: flex; gap: 1rem; list-style: none; margin: 0; padding: 0; flex-wrap: wrap; justify-content: center; }
.rating-summary li { color: var(--c); }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }

/* --- stats --- */
:root { --young: #94d82d; --heat-0: var(--surface-2); }
.kpis { display: grid; gap: var(--gap); grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); list-style: none; margin: 0; padding: 0; }
.kpis li { padding: .9rem 1rem; display: flex; flex-direction: column; }
.kpis .n { font-size: 1.6rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.kpis .label { font-size: .75rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.grid.charts { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.chart { position: relative; height: 220px; }
.donut { position: relative; }
.donut-center { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
                pointer-events: none; }
.donut-center strong { font-size: 1.6rem; line-height: 1.1; }
.legend { display: flex; flex-wrap: wrap; gap: .35rem 1rem; list-style: none; margin: 0; padding: 0; font-size: .9rem; }
.legend li { display: flex; align-items: center; gap: .35rem; }
.swatch { width: .8rem; height: .8rem; border-radius: 3px; }
.heatmap-wrap { overflow-x: auto; }
.heatmap { display: grid; grid-template-rows: repeat(7, .8rem); grid-auto-flow: column; grid-auto-columns: .8rem; gap: 3px; width: max-content; }
.heatmap span { border-radius: 2px; background: var(--heat-0); }
.heatmap .l1 { background: color-mix(in srgb, var(--review) 30%, var(--heat-0)); }
.heatmap .l2 { background: color-mix(in srgb, var(--review) 55%, var(--heat-0)); }
.heatmap .l3 { background: color-mix(in srgb, var(--review) 80%, var(--heat-0)); }
.heatmap .l4 { background: var(--review); }
.meter { display: inline-block; vertical-align: middle; width: 5rem; height: .45rem; margin-right: .4rem; border-radius: 999px;
         background: var(--surface-2); overflow: hidden; }
.meter span { display: block; height: 100%; background: var(--review); }

/* --- card browser, filters & topic order --- */
.filters .facet { display: flex; flex-direction: column; gap: .35rem; }
.facet > .label { display: flex; align-items: center; gap: .5rem; font-weight: 600; font-size: .9rem; }
.chips { display: flex; flex-wrap: wrap; gap: .35rem; }
.chip.toggle { font: inherit; font-size: .85rem; padding: .25rem .65rem; min-height: 2rem; cursor: pointer; background: var(--surface); }
.chip.toggle:hover { background: var(--surface-2); color: var(--text); }
.chip.toggle.on { background: var(--primary-soft); border-color: var(--primary); color: var(--primary); font-weight: 600; }
.chip .n { opacity: .7; font-variant-numeric: tabular-nums; margin-left: .15rem; }
.session { display: flex; flex-wrap: wrap; align-items: center; gap: .75rem; }
.session .full { flex-basis: 100%; }
.badge.practice { background: var(--learning); color: var(--surface); }
table.cards td { white-space: normal; vertical-align: top; }
table.cards td:nth-child(-n+2) { min-width: 9rem; }
table.cards .chip { display: inline-block; margin: 0 .2rem .2rem 0; }
.due { font-variant-numeric: tabular-nums; white-space: nowrap !important; }
.due.new { color: var(--new); } .due.learning { color: var(--learning); } .due.young { color: var(--review); }
.due.mature { color: var(--review); font-weight: 600; }
.field.wide { grid-column: 1 / -1; }
.field.wide .control { align-items: flex-start; }
.topic-order { flex: 1; display: flex; flex-direction: column; gap: .5rem; min-width: 0; }
.ranked { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .3rem; }
.ranked li { display: flex; align-items: center; gap: .5rem; padding: .3rem .4rem .3rem .6rem; border: 1px solid var(--border);
             border-radius: var(--radius-sm); background: var(--surface); cursor: grab; }
.ranked li.dragging { opacity: .45; }
.ranked li.over { border-color: var(--primary); box-shadow: inset 0 2px 0 var(--primary); }
.ranked .grip { color: var(--muted); }
.ranked .rank { min-width: 1.4rem; font-weight: 700; color: var(--primary); font-variant-numeric: tabular-nums; }

/* Sign in and user management */
.narrow { max-width: 36rem; margin-inline: auto; }
.stack.tight { gap: .4rem; }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.danger:hover:not(:disabled) { background: var(--danger-soft); }
code.big { font-size: 1.15rem; letter-spacing: .04em; user-select: all; }
a.badge.profile { color: inherit; text-decoration: none; }
