:root {
  --bg: #0f1115;
  --panel: #181b22;
  --panel-2: #20242d;
  --border: #2b313c;
  --text: #e7ebf0;
  --muted: #9aa4b2;
  --accent: #5b8cff;
  --accent-2: #3f6fe0;
  --right: #34c77b;
  --wrong: #ff5c72;
  --shadow: 0 6px 24px rgba(0,0,0,.35);
  --radius: 14px;
}
[data-theme="light"] {
  --bg: #f4f6fb;
  --panel: #ffffff;
  --panel-2: #eef1f7;
  --border: #dde2ec;
  --text: #1a1f29;
  --muted: #5d6675;
  --shadow: 0 6px 24px rgba(20,30,60,.10);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  min-height: 100vh;
  min-height: 100dvh;          /* normal pages scroll; only study mode locks the viewport */
  display: flex;
  flex-direction: column;
  -webkit-text-size-adjust: 100%;
  overflow-wrap: break-word;
}
.btn { -webkit-tap-highlight-color: transparent; touch-action: manipulation; }

.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 22px;
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--bg); z-index: 10;
}
.logo { font-size: 20px; margin: 0; cursor: pointer; user-select: none; display: inline-flex; align-items: center; gap: 9px; }
.logo-mark { width: 26px; height: 26px; flex-shrink: 0; }
.logo-ext { color: var(--muted); font-weight: 400; }
.icon-btn { display: inline-flex; align-items: center; justify-content: center; padding: 9px 11px; }
.btn svg { display: block; }
.spacer { flex: 1; }

main { width: 100%; max-width: 820px; margin: 0 auto; padding: 22px; flex: 1; }

/* Only study mode locks the viewport: the page can't scroll, the card area
   scrolls internally, and the action buttons stay pinned at the bottom.
   Every other view (home, deck detail, ...) just scrolls the page normally. */
body.studying { height: 100vh; height: 100dvh; overflow: hidden; }
body.studying main { min-height: 0; overflow: hidden; }
body.studying #studyView { display: flex; flex-direction: column; height: 100%; }
.view { animation: fade .18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px);} to {opacity:1; transform:none;} }
.hidden { display: none !important; }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 18px;
  box-shadow: var(--shadow);
}
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.panel-head h2 { margin: 0; font-size: 18px; }
.panel-head .row { margin-top: 0; flex: 0 0 auto; }
.center { text-align: center; }

.row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 12px; }
.row.end { justify-content: flex-end; }
.row.center { justify-content: center; }

.btn {
  background: var(--accent); color: #fff; border: none;
  padding: 10px 16px; border-radius: 10px; font-size: 14px; font-weight: 600;
  cursor: pointer; transition: .15s; white-space: nowrap;
}
.btn:hover { background: var(--accent-2); }
.btn:active { transform: translateY(1px); }
.btn.ghost { background: transparent; color: var(--text); border: 1px solid var(--border); }
.btn.ghost:hover { background: var(--panel-2); }
.btn.danger { color: var(--wrong); border-color: transparent; }
.btn.danger:hover { background: rgba(255,92,114,.12); }
.btn.big { font-size: 16px; padding: 14px 22px; width: 100%; justify-content: center; }
.btn.right { background: var(--right); }
.btn.wrong { background: var(--wrong); }
.btn.back { margin-bottom: 12px; }
.btn.mini { padding: 6px 11px; font-size: 13px; }
#editToggle { font-size: 18px; line-height: 1; letter-spacing: 1px; }
#editToggle.active { background: var(--accent); color: #fff; border-color: transparent; }

.deck-topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.deck-topbar .back { margin-bottom: 0; }
.deck-topbar .row { margin-top: 0; }

.deck-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 10px; }
.deck-item {
  display: flex; align-items: center; gap: 14px;
  background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 12px; padding: 14px 16px; cursor: pointer; transition: .15s;
}
.deck-item:hover { border-color: var(--accent); transform: translateX(2px); }
.deck-item .d-main { flex: 1; min-width: 0; }
.deck-item .d-title { font-weight: 600; }
.deck-item .d-sub { color: var(--muted); font-size: 13px; }
.deck-item .ring { flex-shrink: 0; }
.deck-item .d-actions { flex-shrink: 0; display: flex; gap: 6px; }

.hint { color: var(--muted); font-size: 13px; }
code { background: var(--panel-2); padding: 1px 6px; border-radius: 6px; font-size: 13px; }
.format-help summary, #samplePanel > summary { cursor: pointer; font-weight: 600; }
#samplePanel[open] > summary { margin-bottom: 12px; }
.format-help h4 { margin: 18px 0 6px; font-size: 14px; }
.format-help h4:first-of-type { margin-top: 12px; }
.format-help .hint { margin: 4px 0 8px; }
.format-help pre { background: var(--panel-2); padding: 14px; border-radius: 10px; overflow:auto; margin: 6px 0 4px; }
.format-help pre code { background: none; padding: 0; }

.paste-area {
  width: 100%; min-height: 320px; resize: vertical;
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px; padding: 14px;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 13px;
}

.stats-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-bottom: 16px; }
.stat { background: var(--panel-2); border: 1px solid var(--border); border-radius: 10px; padding: 12px; text-align: center; }
.stat .num { font-size: 24px; font-weight: 700; }
.stat .lbl { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
.big-stats .num { font-size: 30px; }

.study-config { display: flex; gap: 18px; margin: 8px 0 6px; }
.study-config .cfg { flex: 1; min-width: 0; }            /* groups split the row evenly: 50/50 for two */
.study-config #hintsCfg,
.study-config #shuffleCfg { flex: 0 1 auto; }            /* two-option toggles - size to content so Mode/Order get the room */
.study-config .seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; width: 100%; }
/* min-height keeps one- and two-line buttons the same height without relying on
   the browser stretching the button (Firefox doesn't always). */
.study-config .seg button { padding: 8px 10px; line-height: 1.25; min-height: 3.9em; }
.cfg label { display: block; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; margin-bottom: 6px; }
.seg { display: inline-flex; background: var(--panel-2); border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.seg button { background: transparent; color: var(--muted); border: none; padding: 8px 14px; cursor: pointer; font-size: 13px; font-weight: 600; text-align: center; }
.seg button.active { background: var(--accent); color: #fff; }
.seg button:disabled { opacity: .35; cursor: not-allowed; }
.subset-hint { margin: 8px 0 0; }

.cfg-cards { margin-top: 14px; }
.cfg-cards input[type=range] { display: block; width: 100%; accent-color: var(--accent); cursor: pointer; margin-top: 8px; }
.cfg-cards input[type=range]:disabled { opacity: .4; cursor: not-allowed; }
.count-value { text-transform: none; letter-spacing: 0; color: var(--text); font-weight: 600; margin-left: 6px; }

/* ---- study ---- */
.study-top { display: flex; align-items: center; gap: 14px; margin-bottom: 18px; flex-shrink: 0; }
.progress-wrap { flex: 1; height: 8px; background: var(--panel-2); border-radius: 99px; overflow: hidden; }
.progress-bar { height: 100%; width: 0; background: var(--accent); transition: width .25s; }
.counter { color: var(--muted); font-size: 14px; font-variant-numeric: tabular-nums; }

.card-stage { flex: 1; min-height: 0; display: flex; flex-direction: column; margin: 10px 0; overflow-y: auto; }
.flashcard {
  position: relative;
  background: var(--panel); border: 1px solid var(--border); border-radius: 16px;
  flex: 1; min-height: 120px; padding: 32px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; justify-content: center;
}
.face { font-size: 19px; overflow-wrap: break-word; word-break: break-word; }
.face .katex { font-size: 1.05em; }
.face .katex-display { overflow-x: auto; overflow-y: hidden; padding: 4px 0; margin: .6em 0; }
.face.front { font-weight: 600; }
.face .cloze { color: var(--accent); font-weight: 700; }
.divider { height: 1px; background: var(--border); margin: 22px 0; }
.divider.hidden { display: none; }
.card-badge { position: absolute; top: 12px; right: 14px; font-size: 12px; color: var(--muted); }

.face h1,.face h2,.face h3 { margin: .3em 0; }
.face p { margin: .5em 0; }
.face ul,.face ol { margin: .4em 0; padding-left: 1.4em; }
.face pre { background: var(--panel-2); padding: 12px; border-radius: 8px; overflow: auto; }
.face img { max-width: 100%; height: auto; border-radius: 8px; display: block; margin: .6em auto; }
.face svg { max-width: 100%; height: auto; display: block; margin: .6em auto; }
.face blockquote { border-left: 3px solid var(--accent); margin: .5em 0; padding-left: 12px; color: var(--muted); }
.face a { color: var(--accent); }
.face table { border-collapse: collapse; }
.face td, .face th { border: 1px solid var(--border); padding: 4px 10px; }

.type-box { margin-top: 14px; }
.type-box input {
  width: 100%; background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px; padding: 13px 15px; font-size: 16px;
}
.type-box input.match { border-color: var(--right); }
.type-box input.close { border-color: #e0a23f; }
.type-box input.miss  { border-color: var(--wrong); }
.type-hint { font-size: 13px; margin-top: 8px; min-height: 18px; color: var(--muted); }
.type-hint.match { color: var(--right); }
.type-hint.close { color: #e0a23f; }
.type-hint.miss  { color: var(--wrong); }

/* ---- multiple choice ---- */
.choice-box { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
.choice-note { color: var(--muted); font-size: 13px; }
.choice {
  display: flex; gap: 13px; align-items: center; text-align: left;
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 12px;
  padding: 12px 15px; font-size: 16px; cursor: pointer; transition: .12s;
  -webkit-tap-highlight-color: transparent; touch-action: manipulation;
}
.choice:hover:not([disabled]) { border-color: var(--accent); }
.choice.selected { border-color: var(--accent); background: rgba(91,140,255,.14); }
.choice.correct  { border-color: var(--right); background: rgba(52,199,123,.16); }
.choice.wrong    { border-color: var(--wrong); background: rgba(255,92,114,.16); }
.choice.missed   { border-color: var(--right); border-style: dashed; }
.choice[disabled] { cursor: default; }

/* Letter badge - a clean circle that inherits the option's state colour */
.choice-key {
  flex-shrink: 0;
  width: 28px; height: 28px;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700; text-transform: uppercase;
  color: var(--muted);
  border: 1.5px solid var(--border); border-radius: 50%;
  transition: .12s;
}
.choice:hover:not([disabled]) .choice-key { color: var(--text); border-color: var(--accent); }
.choice.selected .choice-key { color: #fff; background: var(--accent); border-color: var(--accent); }
.choice.correct  .choice-key { color: #fff; background: var(--right); border-color: var(--right); }
.choice.wrong    .choice-key { color: #fff; background: var(--wrong); border-color: var(--wrong); }
.choice.missed   .choice-key { color: var(--right); border-color: var(--right); }
.choice-text { flex: 1; }
.choice-text p { margin: 0; }

.study-actions { display: flex; flex-direction: column; gap: 12px; flex-shrink: 0; padding-top: 4px; }
.grade-row { display: flex; gap: 12px; }
.grade-row .btn { flex: 1; }

.foot { text-align: center; color: var(--muted); font-size: 12px; padding: 18px; border-top: 1px solid var(--border); }
.foot a { color: var(--accent); text-decoration: none; margin-left: 6px; }
.foot a:hover { text-decoration: underline; }

@media (max-width: 600px) {
  .topbar { padding: 12px 16px; }
  .logo { font-size: 18px; }
  main { padding: 14px; }
  .panel { padding: 16px; border-radius: 12px; }

  /* Stack header rows so buttons get full width and big tap targets */
  .panel-head { flex-direction: column; align-items: stretch; }
  .panel-head .row { width: 100%; }
  .panel-head .row .btn { flex: 1; }
  .btn { padding: 12px 16px; }

  /* Make room for the per-deck actions: drop the decorative ring, keep buttons tappable */
  .deck-item .ring { display: none; }
  .btn.mini { padding: 9px 12px; }

  /* Keep All decks + Edit + the two export buttons on one line */
  .deck-topbar { gap: 6px; }
  .deck-topbar .row { gap: 6px; }
  .deck-topbar .btn { padding: 8px 8px; font-size: 12px; }

  /* Keep all four stat tiles on one row by shrinking them to fit */
  .stats-grid { gap: 8px; }
  .stat { padding: 10px 6px; }
  .stat .num { font-size: 20px; }
  .stat .lbl { font-size: 11px; }

  /* Lay the toggle groups out as side-by-side columns (Mode / Order, plus Hints
     in flip mode), each a vertical stack of options, so they form one compact
     grid instead of full-width rows. auto-fit keeps them evenly sized whether
     there are two groups or three. Cards (a slider) sits full-width below. */
  .study-config { display: grid; grid-template-columns: repeat(auto-fit, minmax(90px, 1fr)); gap: 14px 10px; align-items: start; }
  .study-config .seg { grid-auto-flow: row; }   /* stack the options vertically */
  .seg button { padding: 10px 6px; }

  /* Roomier cards, smaller fixed chrome */
  .flashcard { min-height: 0; padding: 22px 18px; }
  .face { font-size: 18px; }
  .card-badge { position: static; display: block; margin-bottom: 10px; }

  /* Keep the answer / grade controls within thumb reach at the bottom */
  .study-actions {
    position: sticky; bottom: 0;
    background: var(--bg); padding: 12px 0 14px;
    margin: 0 -14px; padding-left: 14px; padding-right: 14px;
    border-top: 1px solid var(--border);
  }
  .grade-row .btn, #revealBtn { padding: 16px; font-size: 16px; }
  .study-top { gap: 10px; }
  .counter { font-size: 13px; }
}
