:root {
  --bg: #f3ede2;
  --surface: #fffcf6;
  --surface-2: #faf4e8;
  --ink: #1f1a14;
  --muted: #6e6457;
  --faint: #a59a8a;
  --line: #e0d6c4;
  --staff: rgba(31, 26, 20, 0.07);
  --accent: #b23a2b;
  --accent-ink: #ffffff;
  --got: #2f7a4d;
  --miss: #b23a2b;
  --shadow: 0 1px 2px rgba(60, 40, 10, 0.06), 0 12px 32px -12px rgba(60, 40, 10, 0.22);
  --radius: 14px;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, "Segoe UI Symbol", serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Segoe UI Symbol", sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15130f;
    --surface: #211d17;
    --surface-2: #1b1813;
    --ink: #f1eadf;
    --muted: #a99f90;
    --faint: #6f6759;
    --line: #362f26;
    --staff: rgba(241, 234, 223, 0.06);
    --accent: #e06a58;
    --accent-ink: #1a0f0c;
    --got: #62b985;
    --miss: #e06a58;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 16px 40px -16px rgba(0, 0, 0, 0.7);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #15130f;
  --surface: #211d17;
  --surface-2: #1b1813;
  --ink: #f1eadf;
  --muted: #a99f90;
  --faint: #6f6759;
  --line: #362f26;
  --staff: rgba(241, 234, 223, 0.06);
  --accent: #e06a58;
  --accent-ink: #1a0f0c;
  --got: #62b985;
  --miss: #e06a58;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 16px 40px -16px rgba(0, 0, 0, 0.7);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
}

button { font: inherit; color: inherit; }

kbd {
  font-family: var(--sans);
  font-size: 0.72em;
  padding: 0.1em 0.45em;
  margin-left: 0.5em;
  border-radius: 4px;
  border: 1px solid currentColor;
  opacity: 0.55;
}

/* ------------------------------------------------------------ top bar */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 24px;
  border-bottom: 1px solid var(--line);
  background: var(--surface-2);
  flex-wrap: wrap;
}

.brand { display: flex; align-items: center; gap: 10px; }

.brand-mark {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-ink);
  display: grid;
  place-items: center;
  font-size: 18px;
}

.brand-name {
  font-family: var(--serif);
  font-size: 20px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.timer { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.presets {
  display: flex;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px;
}

.preset {
  border: 0;
  background: transparent;
  padding: 5px 12px;
  border-radius: 999px;
  cursor: pointer;
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
}

.preset[aria-checked="true"] { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,0.12); }
.preset:disabled { cursor: not-allowed; opacity: 0.5; }

.clock {
  font-variant-numeric: tabular-nums;
  font-size: 26px;
  font-weight: 600;
  min-width: 4.2ch;
  text-align: center;
  letter-spacing: -0.02em;
}

.clock.low { color: var(--accent); }
.clock.paused { opacity: 0.55; }

/* ------------------------------------------------------------- buttons */

.btn {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 10px;
  padding: 8px 16px;
  cursor: pointer;
  font-weight: 600;
  font-size: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.08s ease, background 0.15s ease, border-color 0.15s ease;
}

.btn:active { transform: translateY(1px); }
.btn:focus-visible, .preset:focus-visible, .card:focus-visible, .link:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn-primary { background: var(--ink); border-color: var(--ink); color: var(--bg); }
.btn-ghost { background: transparent; }
.btn-ghost:hover { background: var(--surface); }
.btn-got { background: var(--got); border-color: var(--got); color: #fff; }
.btn-miss { background: transparent; border-color: var(--miss); color: var(--miss); }
.btn-wide { min-width: 150px; padding: 12px 20px; font-size: 15px; }

.link {
  border: 0;
  background: none;
  padding: 0;
  color: var(--muted);
  cursor: pointer;
  font-size: 13px;
  text-decoration: underline;
  text-underline-offset: 2px;
}
.link:hover { color: var(--ink); }

/* -------------------------------------------------------------- layout */

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 260px 1fr;
  min-height: 0;
}

.sidebar {
  border-right: 1px solid var(--line);
  padding: 20px 18px 32px;
  overflow-y: auto;
}

.sidebar-toggle { display: none; }

.topic-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 8px;
}

.topic-head h2 {
  margin: 0;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.topic-bulk { display: flex; gap: 6px; color: var(--faint); font-size: 13px; }

.group-label {
  margin: 18px 0 4px;
  font-family: var(--serif);
  font-size: 15px;
  font-weight: 600;
}

.topic {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px;
  margin: 0 -8px;
  border-radius: 8px;
  cursor: pointer;
  user-select: none;
}
.topic:hover { background: var(--surface); }

.topic input {
  appearance: none;
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  margin: 0;
  flex: none;
  border: 1.5px solid var(--faint);
  border-radius: 5px;
  display: grid;
  place-items: center;
  cursor: pointer;
  background: var(--surface);
}
.topic input::after {
  content: "";
  width: 9px;
  height: 5px;
  border: solid var(--accent-ink);
  border-width: 0 0 2px 2px;
  transform: translate(0, -1px) rotate(-45deg);
  opacity: 0;
}
.topic input:checked { background: var(--accent); border-color: var(--accent); }
.topic input:checked::after { opacity: 1; }
.topic input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* --------------------------------------------------------------- stage */

.stage {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 22px;
  padding: 28px 24px 40px;
  min-width: 0;
}

.stats {
  color: var(--muted);
  font-size: 14px;
  font-variant-numeric: tabular-nums;
  min-height: 1.45em;
}
.stats b { color: var(--ink); font-weight: 600; }
.stats .g { color: var(--got); }
.stats .m { color: var(--miss); }

.card-wrap {
  width: 100%;
  max-width: 640px;
  perspective: 1600px;
}

.card-wrap.enter { animation: deal 0.28s ease-out; }

@keyframes deal {
  from { opacity: 0; transform: translateY(10px) scale(0.985); }
  to { opacity: 1; transform: none; }
}

.card {
  position: relative;
  display: block;
  width: 100%;
  aspect-ratio: 8 / 5;
  min-height: 300px;
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
  text-align: center;
  border-radius: var(--radius);
}
.card:disabled { cursor: default; }

.card-inner {
  position: absolute;
  inset: 0;
  display: block;
  transform-style: preserve-3d;
  transition: transform 0.5s cubic-bezier(0.3, 0.7, 0.2, 1);
}
.card.flipped .card-inner { transform: rotateY(180deg); }
.card.no-anim .card-inner { transition: none; }

.face {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 36px 32px 30px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  overflow: hidden;
}
.back { transform: rotateY(180deg); background: var(--surface-2); }

.staff {
  position: absolute;
  left: 0;
  right: 0;
  top: 22px;
  height: 33px;
  background: repeating-linear-gradient(to bottom, var(--staff) 0 1px, transparent 1px 8px);
  pointer-events: none;
}

.card-topic {
  position: absolute;
  top: 16px;
  left: 20px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  color: var(--accent);
  background: var(--surface);
  padding: 2px 8px 2px 0;
}
.back .card-topic { background: var(--surface-2); }

.card-prompt {
  font-family: var(--serif);
  font-size: clamp(26px, 4.6vw, 40px);
  line-height: 1.18;
  font-weight: 600;
  letter-spacing: -0.01em;
  max-width: 18ch;
  text-wrap: balance;
}

.card-sub, .card-prompt-small {
  color: var(--muted);
  font-size: 15px;
}

.card-cue {
  position: absolute;
  bottom: 16px;
  font-size: 12px;
  color: var(--faint);
}

.card-answer {
  font-family: var(--serif);
  font-size: clamp(26px, 4.4vw, 40px);
  font-weight: 600;
  line-height: 1.2;
  text-wrap: balance;
  max-width: 100%;
}

.card-answer.long { font-size: clamp(20px, 3.2vw, 28px); }
.card-answer:has(.notes) { width: 100%; }

.notes {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(0, 1fr);
  gap: 6px;
  width: 100%;
  max-width: 520px;
  margin: 4px auto 0;
}
.notes .n { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.notes .h {
  font-family: var(--sans);
  font-size: 12px;
  font-weight: 600;
  color: var(--faint);
  letter-spacing: 0.02em;
}
.notes .v {
  font-family: var(--serif);
  font-size: clamp(20px, 3.8vw, 32px);
  font-weight: 600;
  white-space: nowrap;
}

.card-detail {
  font-family: var(--serif);
  font-size: 22px;
  color: var(--ink);
}

.card-note {
  color: var(--muted);
  font-size: 14px;
  max-width: 44ch;
  text-wrap: pretty;
}

.card-detail:empty, .card-note:empty, .card-sub:empty { display: none; }

.empty .card-prompt { font-size: 26px; color: var(--muted); font-weight: 500; }

/* ------------------------------------------------------------ controls */

.controls { display: flex; justify-content: center; min-height: 48px; }
.controls-front, .controls-back { display: flex; gap: 12px; }
.controls-back { display: none; }
.controls.flipped .controls-front { display: none; }
.controls.flipped .controls-back { display: flex; }
.controls.disabled { visibility: hidden; }

/* -------------------------------------------------------------- dialog */

.done {
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--surface);
  color: var(--ink);
  padding: 30px 32px 26px;
  width: min(420px, calc(100vw - 32px));
  text-align: center;
  box-shadow: var(--shadow);
}
.done::backdrop { background: rgba(20, 14, 6, 0.45); }
.done h2 { margin: 0 0 6px; font-family: var(--serif); font-size: 30px; }
.done-score { margin: 0; font-size: 18px; }
.done-detail { margin: 6px 0 22px; color: var(--muted); font-size: 14px; }
.done-actions { display: flex; gap: 10px; justify-content: center; }

/* -------------------------------------------------------------- mobile */

@media (max-width: 760px) {
  .topbar { padding: 10px 16px; }
  .timer { width: 100%; justify-content: space-between; gap: 8px; flex-wrap: nowrap; }
  .preset { padding: 5px 11px; }
  .preset .unit { display: none; }
  .clock { font-size: 22px; flex: 1; }
  .btn { padding: 7px 12px; }

  .layout { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }

  .sidebar { border-right: 0; border-bottom: 1px solid var(--line); padding: 0 16px; overflow: visible; }

  .sidebar-toggle {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    padding: 12px 0;
    border: 0;
    background: none;
    cursor: pointer;
    font-weight: 600;
  }
  .topic-count { color: var(--muted); font-weight: 400; font-size: 13px; }
  .chev {
    margin-left: auto;
    width: 8px;
    height: 8px;
    border: solid var(--muted);
    border-width: 0 2px 2px 0;
    transform: rotate(45deg);
    transition: transform 0.2s ease;
  }
  .sidebar.open .chev { transform: rotate(-135deg); }

  .topic-panel { display: none; padding-bottom: 14px; }
  .sidebar.open .topic-panel { display: block; }
  .topic-head h2 { display: none; }
  .topic-head { justify-content: flex-end; margin: -4px 0 0; }

  .stage { justify-content: flex-start; padding: 16px 16px 32px; gap: 16px; }
  .card { aspect-ratio: auto; min-height: 340px; }
  .face { padding: 44px 18px 34px; }
  .btn-wide { min-width: 0; flex: 1; }
  .controls { width: 100%; max-width: 640px; }
  .controls-front, .controls.flipped .controls-back { width: 100%; }
  kbd { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .card-inner { transition: none; }
  .card-wrap.enter { animation: none; }
}
