/* Tokens follow budget-app/design/colors: one semantic name, value per theme (light / system dark).
   Text pairs are the AA-checked values from that palette. */
:root {
  --bg: #f6f6f4;
  --surface: #ffffff;
  --surface-2: #f0f0ee;
  --surface-3: #e9e9e6;
  --text: #17171a;
  --text-2: #56565c;
  --text-3: #66666c;
  --border: rgba(17, 17, 20, 0.09);
  --border-2: rgba(17, 17, 20, 0.14);
  --accent: #4f46e5;
  --accent-soft: rgba(79, 70, 229, 0.1);
  --accent-fg: #ffffff;
  --pos: #00774f;
  --neg: #bd3532;
  --warn: #8f5400;
  --known: #10a36f;
  --learning: #e0a030;
  --new: #d6d6d2;
  --shadow: 0 1px 2px rgba(17, 17, 20, 0.04), 0 4px 16px -6px rgba(17, 17, 20, 0.08);
  --shadow-pop: 0 8px 30px -8px rgba(17, 17, 20, 0.18);
  --radius: 16px;
  --font: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #0d0d0f;
    --surface: #161619;
    --surface-2: #1e1e22;
    --surface-3: #26262b;
    --text: #f3f3f4;
    --text-2: #a8a8ae;
    --text-3: #87878f;
    --border: rgba(255, 255, 255, 0.08);
    --border-2: rgba(255, 255, 255, 0.14);
    --accent: #8b85f0;
    --accent-soft: rgba(139, 133, 240, 0.14);
    --accent-fg: #14131a;
    --pos: #3ac08b;
    --neg: #f0706d;
    --warn: #e8a93c;
    --known: #3ac08b;
    --learning: #e8a93c;
    --new: #34343a;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px -10px rgba(0, 0, 0, 0.5);
    --shadow-pop: 0 12px 40px -8px rgba(0, 0, 0, 0.6);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; touch-action: manipulation; }
a { color: inherit; text-decoration: none; touch-action: manipulation; }
[hidden] { display: none !important; }
.muted { color: var(--text-2); }
.small { font-size: 13px; }
.mt { margin-top: 24px; }
::selection { background: var(--accent-soft); }

/* press feedback: the only thing that tells a finger the tap registered (design/mobile-pwa §9) */
.press { transition: background-color .15s, border-color .15s, color .15s, opacity .15s, transform .12s ease-out; }
.press:active:not(:disabled) { transform: scale(0.97); }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 8px; }

/* ---------- layout ---------- */
.app {
  display: flex;
  min-height: 100dvh;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}
.sidebar { display: none; }
.main { flex: 1; min-width: 0; display: flex; }
.view { flex: 1; min-width: 0; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 0 18px; border-radius: 12px;
  background: var(--surface-2); color: var(--text); font-weight: 600;
  border: 1px solid var(--border);
}
@media (hover: hover) { .btn:hover { background: var(--surface-3); } }
.btn-primary { background: var(--accent); color: var(--accent-fg); border-color: transparent; }
@media (hover: hover) { .btn-primary:hover { background: var(--accent); filter: brightness(1.08); } }
.icon-btn {
  position: relative; width: 44px; height: 44px; border-radius: 12px;
  display: inline-grid; place-items: center; color: var(--text-2);
}
@media (hover: hover) { .icon-btn:hover { background: var(--surface-2); color: var(--text); } }
.icon-btn:disabled { opacity: .35; cursor: default; }
.icon-btn svg, .nav-item svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.dot { position: absolute; top: 9px; right: 9px; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }

/* ---------- review ---------- */
.view-review {
  display: flex; flex-direction: column;
  height: 100dvh;
  padding-top: env(safe-area-inset-top);
}
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 8px 12px 4px 16px;
}
.counters { display: flex; gap: 6px; flex-wrap: wrap; font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; }
.counter { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px; background: var(--surface-2); color: var(--text-2); }
.counter b { color: var(--text); }
.counter i { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.topbar-actions { display: flex; gap: 2px; }

.stage { flex: 1; min-height: 0; display: flex; flex-direction: column; padding: 8px 16px; }
.card {
  flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 20px 20px 24px; outline: none;
  display: flex; flex-direction: column;
}
.card.flip-in { animation: flip-in .22s ease-out; }
@keyframes flip-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
.card-meta { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; font-size: 12px; color: var(--text-2); }
.tag { padding: 3px 9px; border-radius: 999px; background: var(--surface-2); font-weight: 600; white-space: nowrap; }
.tag-p1 { background: var(--accent-soft); color: var(--accent); }
.tag-state { margin-left: auto; }
.tag-new { color: var(--accent); }
.tag-learn { color: var(--warn); }
.tag-review { color: var(--pos); }
.card-topic { width: 100%; margin-top: 2px; }
.card-word {
  font-size: clamp(24px, 6.5vw, 34px); line-height: 1.2; letter-spacing: -0.01em;
  margin: 18px 0 8px; font-weight: 700; overflow-wrap: anywhere;
}
.card-word { transform-origin: left top; }
/* front side: the term alone, big, in the middle of the card */
.card.front .card-word {
  margin: auto 0; padding-bottom: 6vh; text-align: center;
  font-size: clamp(30px, 8.5vw, 46px); line-height: 1.15;
}
.card-answer { margin-top: 14px; padding-top: 16px; border-top: 1px dashed var(--border-2); font-size: 16px; line-height: 1.6; }
.card-answer p { margin: 0 0 10px; }
.answer-short { font-size: 19px; line-height: 1.55; font-weight: 500; color: var(--text); margin: 0 !important; }
.weak-def .answer-short { font-size: 15px; color: var(--text-2); }
.slide { margin-top: 18px; font-size: 15px; color: var(--text-2); }
.slide summary {
  cursor: pointer; list-style: none; display: inline-flex; align-items: center; gap: 6px; min-height: 36px;
  font-size: 13px; font-weight: 600; color: var(--text-3); padding: 0 10px; border-radius: 8px; background: var(--surface-2);
}
.slide summary::-webkit-details-marker { display: none; }
.slide summary::after { content: '▾'; font-size: 11px; }
.slide[open] summary::after { content: '▴'; }
.slide[open] summary { margin-bottom: 10px; }
.card-answer ul { margin: 4px 0 12px; padding-left: 20px; }
.card-answer li { margin: 3px 0; }
.card-answer li::marker { color: var(--accent); }
.card-answer .src { display: inline-block; font-size: 11px; font-weight: 700; color: var(--text-2); background: var(--surface-2); border-radius: 6px; padding: 1px 6px; margin-right: 6px; vertical-align: 1px; }
.card-answer .seg + .seg { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--border); }
.card-answer code, .formula { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: .92em; background: var(--surface-2); padding: 1px 5px; border-radius: 6px; }

.actions {
  padding: 8px 16px calc(12px + env(safe-area-inset-bottom));
}
.btn-reveal { width: 100%; min-height: 56px; font-size: 17px; border-radius: 14px; }
.ratings { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.rate {
  min-height: 60px; border-radius: 14px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  background: var(--surface); border: 1px solid var(--border-2); font-weight: 600; font-size: 14px;
}
.rate-key { display: none; }
.rate-ivl { font-size: 12px; font-weight: 500; color: var(--text-2); font-variant-numeric: tabular-nums; }
.rate-1 { color: var(--neg); }
.rate-2 { color: var(--warn); }
.rate-3 { color: var(--pos); }
.rate-4 { color: var(--accent); }
@media (hover: hover) { .rate:hover { background: var(--surface-2); } }

.empty { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 24px; }
.empty-icon { width: 64px; height: 64px; border-radius: 50%; display: grid; place-items: center; font-size: 28px; background: var(--accent-soft); color: var(--accent); }
.empty h2 { margin: 16px 0 4px; }
.empty p { color: var(--text-2); margin: 0 0 20px; max-width: 360px; }
.empty-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }

/* ---------- filter sheet ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 20; background: rgba(0, 0, 0, .35);
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade .15s ease-out;
}
@keyframes fade { from { opacity: 0; } }
.sheet {
  width: 100%; max-width: 560px; background: var(--surface); border-radius: 20px 20px 0 0;
  padding: 8px 20px calc(20px + env(safe-area-inset-bottom)); box-shadow: var(--shadow-pop);
  animation: up .2s ease-out;
}
@keyframes up { from { transform: translateY(24px); opacity: .6; } }
.sheet-grip { width: 40px; height: 5px; border-radius: 3px; background: var(--border-2); margin: 4px auto 12px; }
.sheet h2 { margin: 0 0 12px; font-size: 19px; }
.field-label { font-size: 13px; font-weight: 600; color: var(--text-2); margin: 14px 0 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  min-height: 44px; min-width: 44px; padding: 0 14px; border-radius: 12px; font-weight: 600;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text-2);
  display: inline-flex; align-items: center; gap: 6px;
}
.chip small { font-weight: 500; opacity: .8; }
.chip[aria-pressed="true"] { background: var(--accent); color: var(--accent-fg); border-color: transparent; }
.sheet-actions { display: flex; gap: 8px; margin-top: 18px; }
.sheet-actions .btn { flex: 1; }

.toast {
  position: fixed; left: 50%; bottom: calc(96px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px; font-weight: 500;
  box-shadow: var(--shadow-pop); z-index: 30; max-width: calc(100vw - 32px);
}

/* ---------- progress ---------- */
.view-progress { padding: 28px 32px 48px; max-width: 1280px; margin: 0 auto; width: 100%; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 20px; }
.page-head h1 { margin: 0; font-size: 28px; letter-spacing: -0.01em; }
.page-head p { margin: 4px 0 0; }
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.tile-label { font-size: 13px; color: var(--text-2); font-weight: 500; }
.tile-value { font-size: 28px; font-weight: 700; letter-spacing: -0.02em; margin-top: 4px; font-variant-numeric: tabular-nums; }
.tile-value small { font-size: 15px; color: var(--text-2); font-weight: 600; }
.tile-sub { font-size: 12px; color: var(--text-3); margin-top: 2px; }
.grid-2 { display: grid; grid-template-columns: 1fr; gap: 16px; margin-bottom: 16px; }
@media (min-width: 1200px) { .grid-2 { grid-template-columns: 1.25fr 1fr; } }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; box-shadow: var(--shadow); min-width: 0; }
.panel h2 { font-size: 16px; margin: 0 0 14px; }

.lrow { display: grid; grid-template-columns: 28px minmax(120px, 1fr) minmax(140px, 1.6fr) 52px 46px; align-items: center; gap: 12px; padding: 8px 0; border-top: 1px solid var(--border); }
.lrow:first-child { border-top: 0; }
.lrow.head { font-size: 12px; color: var(--text-3); font-weight: 600; padding-top: 0; }
.lnum { width: 28px; height: 28px; border-radius: 8px; background: var(--surface-2); display: grid; place-items: center; font-weight: 700; font-size: 13px; }
.lname { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lname small { display: block; font-weight: 500; color: var(--text-3); font-size: 12px; }
.num { text-align: right; font-variant-numeric: tabular-nums; font-size: 14px; font-weight: 600; }
.num small { display: block; font-size: 11px; color: var(--text-3); font-weight: 500; }
.bar { display: flex; height: 10px; border-radius: 999px; overflow: hidden; background: var(--new); }
.bar span { height: 100%; }
.bar .b-known { background: var(--known); }
.bar .b-learning { background: var(--learning); }
.legend { display: flex; gap: 16px; margin-top: 12px; font-size: 12px; color: var(--text-2); }
.sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.sw-known { background: var(--known); } .sw-learning { background: var(--learning); } .sw-new { background: var(--new); }

.prow { display: grid; grid-template-columns: 70px 1fr 64px; gap: 12px; align-items: center; padding: 6px 0; }
.prow b { font-size: 14px; }
.matrix { display: grid; grid-template-columns: 36px repeat(9, 1fr); gap: 4px; font-size: 12px; }
.matrix .mh { text-align: center; color: var(--text-3); font-weight: 600; padding: 2px 0; }
.matrix .ml { color: var(--text-3); font-weight: 600; display: flex; align-items: center; }
.mcell { aspect-ratio: 1.4; border-radius: 6px; display: grid; place-items: center; font-weight: 600; font-variant-numeric: tabular-nums; font-size: 11px; }
.mcell.empty-cell { background: transparent; border: 1px dashed var(--border); color: var(--text-3); }

.chart { width: 100%; height: 200px; display: block; }
.chart text { fill: var(--text-3); font-size: 11px; font-family: var(--font); }
.chart .grid { stroke: var(--border); }
.chart .b-rev { fill: var(--accent); }
.chart .b-again { fill: var(--neg); opacity: .85; }
.chart-legend { display: flex; gap: 16px; font-size: 12px; color: var(--text-2); margin-top: 6px; }

.weak-list { display: flex; flex-direction: column; }
.weak-item { border-top: 1px solid var(--border); }
.weak-item:first-child { border-top: 0; }
.weak-btn { width: 100%; text-align: left; display: grid; grid-template-columns: 1fr auto; gap: 12px; padding: 10px 2px; align-items: center; border-radius: 8px; }
@media (hover: hover) { .weak-btn:hover { background: var(--surface-2); } }
.weak-word { font-weight: 600; font-size: 14px; }
.weak-meta { font-size: 12px; color: var(--text-3); }
.weak-r { font-variant-numeric: tabular-nums; font-weight: 700; font-size: 13px; }
.weak-def { padding: 4px 2px 14px; font-size: 14px; color: var(--text-2); }
.empty-note { color: var(--text-3); font-size: 14px; padding: 8px 0; }

/* ---------- desktop ---------- */
@media (min-width: 900px) {
  .sidebar {
    display: flex; flex-direction: column; width: 240px; flex: none;
    position: sticky; top: 0; height: 100dvh;
    border-right: 1px solid var(--border); background: var(--surface); padding: 20px 14px;
  }
  .brand { display: flex; gap: 10px; align-items: center; padding: 0 8px 20px; }
  .brand-title { font-weight: 700; }
  .brand-sub { font-size: 12px; color: var(--text-3); }
  .nav { display: flex; flex-direction: column; gap: 2px; }
  .nav-item { display: flex; align-items: center; gap: 10px; min-height: 44px; padding: 0 12px; border-radius: 10px; font-weight: 600; color: var(--text-2); }
  .nav-item:hover { background: var(--surface-2); color: var(--text); }
  .nav-item.active { background: var(--accent-soft); color: var(--accent); }
  .sidebar-foot { margin-top: auto; padding: 0 8px; }
  .kbd-help { font-size: 12px; color: var(--text-3); display: grid; gap: 6px; }
  kbd { font-family: inherit; font-size: 11px; font-weight: 600; padding: 1px 6px; border-radius: 5px; border: 1px solid var(--border-2); background: var(--surface-2); color: var(--text-2); }
  .sync-status { font-size: 12px; color: var(--text-3); margin-top: 14px; min-height: 18px; }

  .view-review { height: 100dvh; max-width: 820px; margin: 0 auto; width: 100%; padding: 24px 32px; }
  .topbar { padding: 0 0 8px; }
  .stage { padding: 8px 0; }
  .card { padding: 32px 36px 36px; }
  .card-word { font-size: 34px; margin-top: 28px; }
  .card.front .card-word { font-size: 52px; padding: 0 24px 8vh; }
  .card-answer { font-size: 17px; }
  .answer-short { font-size: 21px; }
  .actions { padding: 12px 0 0; }
  .rate { min-height: 64px; font-size: 15px; }
  .rate-key { display: inline-block; margin-right: 6px; }
  .overlay { align-items: center; }
  .sheet { border-radius: 20px; padding: 20px 24px 24px; }
  .sheet-grip { display: none; }
  .toast { bottom: 32px; }
}
@media (max-width: 899px) {
  /* phones: progress is a desktop view; if opened by URL it still renders in one column */
  .view-progress { padding: 16px; padding-top: calc(16px + env(safe-area-inset-top)); }
  .lrow { grid-template-columns: 28px 1fr 1.2fr 44px; }
  .lrow .col-r { display: none; }
  .counters { gap: 4px; font-size: 12px; flex-wrap: nowrap; }
  .counter { padding: 4px 8px; gap: 5px; white-space: nowrap; }
  .counter-total { display: none; }
  .counters { min-width: 0; }
  .rate { padding: 0 2px; font-size: 13.5px; white-space: nowrap; }
}
@media (max-width: 379px) {
  .counter .cl { display: none; }
}

/* ---------- list ---------- */
.only-phone { display: inline-grid; }
.view-list { padding: calc(8px + env(safe-area-inset-top)) 16px calc(24px + env(safe-area-inset-bottom)); max-width: 900px; margin: 0 auto; width: 100%; }
.list-head { position: sticky; top: 0; z-index: 5; background: var(--bg); padding: 4px 0 10px; display: grid; gap: 10px; }
.list-title { display: flex; align-items: center; gap: 6px; }
.list-title h1 { margin: 0; font-size: 22px; }
.list-title .muted { margin-left: auto; }
.search {
  width: 100%; min-height: 44px; padding: 0 14px; border-radius: 12px; font-size: 16px; /* 16px: no zoom on iOS */
  background: var(--surface); border: 1px solid var(--border-2); color: var(--text); outline: none;
}
.search:focus { border-color: var(--accent); }
.chips-scroll { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; margin: 0 -16px; padding: 0 16px; }
.chips-scroll::-webkit-scrollbar { display: none; }
.chip-sm { min-height: 36px; min-width: 40px; padding: 0 12px; font-size: 14px; flex: none; justify-content: center; }
.list-filter-row { display: flex; gap: 8px; align-items: center; justify-content: space-between; }
.btn-sm { min-height: 36px; padding: 0 12px; font-size: 14px; flex: none; }
.list-legend { margin: 0 2px; }
.list-legend .st { display: inline-block; margin-right: 6px; vertical-align: -1px; }
.lgroup { margin-top: 14px; }
.lgroup-title { display: flex; align-items: center; gap: 10px; font-size: 16px; margin: 0 0 6px; }
.ltopic { font-size: 12px; font-weight: 600; color: var(--text-3); text-transform: uppercase; letter-spacing: .04em; margin: 14px 2px 6px; }
.term { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; margin-bottom: 6px; overflow: hidden; }
.term-head { width: 100%; min-height: 48px; display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 14px; text-align: left; }
.term-word { font-weight: 600; font-size: 15px; line-height: 1.35; }
.term-tags { display: flex; align-items: center; gap: 8px; flex: none; }
.st { width: 10px; height: 10px; border-radius: 50%; background: var(--new); }
.st-learning { background: var(--learning); }
.st-known { background: var(--known); }
.term.open { border-color: var(--border-2); }
.term-body { margin: 0; padding: 2px 14px 14px; border-top: 0; }
.term-body .answer-short { font-size: 16px; }
@media (min-width: 900px) {
  .only-phone { display: none; }
  .view-list { padding: 28px 32px 48px; }
  .list-head { padding-top: 0; }
  .list-title h1 { font-size: 28px; }
  .chips-scroll { flex-wrap: wrap; margin: 0; padding: 0; }
  .term-body .answer-short { font-size: 17px; }
}
