:root {
  color-scheme: light dark;
  --bg: #f2f2f7;
  --surface: #ffffff;
  --surface-2: #e8e8ed;
  --text: #1c1c1e;
  --muted: #6c6c70;
  --border: rgba(60, 60, 67, 0.16);
  --accent: #0a63d6;
  --on-accent: #ffffff;
  --known-fill: #34c759;
  --known-text: #17702d;
  --learning-fill: #ff9f0a;
  --learning-text: #9a5100;
  --marked: #f2a900;
  --danger: #d70015;
  --radius: 18px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.05), 0 6px 20px rgba(0, 0, 0, 0.05);
  --hover: brightness(0.95);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --surface: #1c1c1e;
    --surface-2: #2c2c2e;
    --text: #f5f5f7;
    --muted: #98989f;
    --border: rgba(235, 235, 245, 0.16);
    --accent: #0a84ff;
    --known-fill: #30d158;
    --known-text: #30d158;
    --learning-fill: #ff9f0a;
    --learning-text: #ffb340;
    --marked: #ffd60a;
    --danger: #ff453a;
    --shadow: none;
    --hover: brightness(1.2);
  }
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  display: flex;
  flex-direction: column;
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  padding: 0 env(safe-area-inset-right) 0 env(safe-area-inset-left);
  background: var(--bg);
  color: var(--text);
  font: 17px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
h1, h2, p, figure, blockquote { margin: 0; }
button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
button:disabled { cursor: default; opacity: 0.45; }
:focus-visible { outline: 3px solid color-mix(in srgb, var(--accent) 55%, transparent); outline-offset: 2px; }
code { font: 0.9em ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--surface-2); padding: 1px 5px; border-radius: 6px; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
.center { text-align: center; }

/* Top bar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(env(safe-area-inset-top) + 8px) 16px 8px;
  min-height: 52px;
  background: color-mix(in srgb, var(--bg) 94%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 0.5px solid var(--border);
}
.topbar h1 { flex: 1; min-width: 0; font-size: 17px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-button { display: grid; place-items: center; width: 36px; height: 36px; margin-left: -8px; border-radius: 50%; color: var(--accent); }
.icon-button svg, .row-side svg, .button.icon-only svg {
  width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round;
}
.sync-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--surface);
  border: 0.5px solid var(--border);
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
}
.sync-pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.sync-pill[data-status="synced"] .dot { background: var(--known-fill); }
.sync-pill[data-status="syncing"] .dot, .sync-pill[data-status="connecting"] .dot { background: var(--learning-fill); }
.sync-pill[data-status="error"] .dot { background: var(--danger); }
.sync-pill[data-status="signed-out"] { color: var(--accent); }
.sync-pill[data-status="signed-out"] .dot { background: var(--accent); }

/* Layout */
main {
  display: flex;
  flex: 1;
  flex-direction: column;
  width: 100%;
  max-width: 680px;
  margin: 0 auto;
  padding: 16px 16px calc(32px + env(safe-area-inset-bottom));
}
/* While studying, the card fills the screen and the answer buttons sit at the bottom, within thumb reach. */
main:has(> [data-view="study"]:not([hidden])) { padding-bottom: 0; }
.view[data-view="study"] { flex: 1; }
.view { display: flex; flex-direction: column; gap: 16px; }
.view-heading { font-size: 26px; font-weight: 700; line-height: 1.2; }
.panel { background: var(--surface); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.panel h2 { font-size: 20px; font-weight: 700; margin-bottom: 6px; }
.panel > p + p { margin-top: 8px; }
.panel > .button { margin-top: 14px; }
.notice { padding: 12px 14px; border-radius: 12px; background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); margin-bottom: 16px; }
.footnote { text-align: center; }

/* Choices */
.chips {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
  margin: 0 -16px;
  padding: 2px 16px;
  scroll-padding: 0 16px;
}
.chips::-webkit-scrollbar { display: none; }
/* With a mouse, or room to spare, wrap the categories instead of scrolling them sideways. */
@media (pointer: fine), (min-width: 700px) {
  .chips { flex-wrap: wrap; overflow: visible; margin: 0; padding: 0; }
}
.chip {
  flex: none;
  padding: 7px 14px;
  border-radius: 999px;
  background: var(--surface);
  border: 0.5px solid var(--border);
  font-size: 15px;
  font-weight: 500;
}
.chip[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); border-color: transparent; }
.segmented { display: grid; grid-auto-columns: 1fr; grid-auto-flow: column; gap: 2px; padding: 2px; border-radius: 10px; background: var(--surface-2); }
.segment { padding: 7px 4px; border-radius: 8px; font-size: 14px; font-weight: 500; white-space: nowrap; }
.segment[aria-pressed="true"] { background: var(--surface); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12); font-weight: 600; }

/* Progress */
.overview-line { color: var(--muted); }
.overview-line strong { font-size: 30px; font-weight: 700; color: var(--text); }
.bar { display: flex; height: 8px; margin: 10px 0 8px; border-radius: 999px; overflow: hidden; background: var(--surface-2); }
.bar .known { background: var(--known-fill); transition: width 0.3s; }
.bar .learning { background: var(--learning-fill); transition: width 0.3s; }

/* Buttons */
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  min-height: 50px;
  padding: 12px 18px;
  border-radius: 14px;
  background: var(--surface);
  border: 0.5px solid var(--border);
  font-size: 17px;
  font-weight: 600;
}
.button:active:not(:disabled) { transform: scale(0.98); }
.button.primary { background: var(--accent); color: var(--on-accent); border-color: transparent; }
.button.known { background: color-mix(in srgb, var(--known-fill) 16%, var(--surface)); color: var(--known-text); border-color: color-mix(in srgb, var(--known-fill) 45%, transparent); }
.button.learning { background: color-mix(in srgb, var(--learning-fill) 16%, var(--surface)); color: var(--learning-text); border-color: color-mix(in srgb, var(--learning-fill) 45%, transparent); }
.button.danger { color: var(--danger); }
.button.icon-only { flex: none; width: 50px; padding: 0; color: var(--muted); }
.button.icon-only[aria-pressed="true"] { color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, var(--surface)); border-color: color-mix(in srgb, var(--accent) 40%, transparent); }
.study-row { display: flex; gap: 10px; }
.link { align-self: center; padding: 8px; color: var(--accent); font-size: 15px; }

/* Chapter and section lists */
.list-title { margin: 8px 4px -8px; font-size: 13px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; color: var(--muted); }
.rows { list-style: none; margin: 0; padding: 0; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.rows li + li { border-top: 0.5px solid var(--border); }
.row {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas: "title side" "detail side" "bar side";
  align-items: center;
  gap: 2px 12px;
  width: 100%;
  padding: 14px 16px;
  text-align: left;
}
.row:active:not(:disabled) { background: var(--surface-2); }
.row-title { grid-area: title; font-weight: 600; line-height: 1.3; }
.row-detail { grid-area: detail; font-size: 14px; color: var(--muted); }
.row .bar { grid-area: bar; height: 6px; margin: 6px 0 0; }
.row-side { grid-area: side; display: flex; align-items: center; gap: 4px; font-size: 15px; color: var(--muted); }
.row-side svg { width: 18px; height: 18px; }

/* Studying */
.track { flex: none; height: 4px; border-radius: 999px; overflow: hidden; background: var(--surface-2); }
.track span { display: block; width: 0; height: 100%; background: var(--accent); transition: width 0.25s; }
.card { display: flex; flex: 1; flex-direction: column; gap: 14px; min-height: 260px; padding: 18px 20px 20px; cursor: pointer; }
.card-head { display: flex; align-items: center; gap: 8px; }
.card-context { flex: 1; min-width: 0; font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge { padding: 3px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); font-size: 12px; font-weight: 600; }
.badge[data-status="known"] { background: color-mix(in srgb, var(--known-fill) 16%, transparent); color: var(--known-text); }
.badge[data-status="learning"] { background: color-mix(in srgb, var(--learning-fill) 18%, transparent); color: var(--learning-text); }
.mark { display: grid; place-items: center; width: 40px; height: 40px; margin: -8px -10px -8px -4px; border-radius: 50%; color: var(--muted); }
.mark svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linejoin: round; }
.mark[aria-pressed="true"] { color: var(--marked); }
.mark[aria-pressed="true"] svg { fill: currentColor; }
.kind { font-size: 13px; font-weight: 600; letter-spacing: 0.05em; text-transform: uppercase; color: var(--accent); }
.prompt { font-size: 22px; font-weight: 600; line-height: 1.35; }
.blank { display: inline-block; min-width: 3.5em; margin: 0 2px; border-bottom: 2.5px solid currentColor; }
.fill { padding: 0 4px; border-radius: 6px; background: color-mix(in srgb, var(--accent) 18%, transparent); color: inherit; }
.answer { padding-top: 14px; border-top: 0.5px solid var(--border); font-size: 20px; animation: appear 0.18s ease-out; }
.answer-text { font-weight: 600; }
.verdict { font-size: 22px; font-weight: 700; }
.verdict.true { color: var(--known-text); }
.verdict.false { color: var(--danger); }
.correction { margin-top: 6px; }
.card-extra { display: flex; flex-direction: column; gap: 10px; margin-top: auto; font-size: 14px; animation: appear 0.18s ease-out; cursor: auto; }
.flag { padding: 10px 12px; border-radius: 12px; background: color-mix(in srgb, var(--learning-fill) 16%, transparent); color: var(--learning-text); }
.source { color: var(--muted); }
.source summary { margin-bottom: 4px; font-size: 12px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; cursor: pointer; }
.source blockquote { padding-left: 10px; border-left: 3px solid var(--border); }
.source blockquote p + p { margin-top: 6px; }

/* Comparison tables (nations cards) */
.grid { width: 100%; border-collapse: collapse; font-size: 15px; line-height: 1.35; }
.grid th, .grid td { padding: 8px 6px; border-bottom: 0.5px solid var(--border); text-align: left; vertical-align: top; }
.grid thead th { font-size: 13px; font-weight: 600; color: var(--muted); }
.grid tbody th { font-weight: 600; }
.grid td.unknown { color: var(--muted); }
@media (max-width: 600px) {
  .grid thead { display: none; }
  .grid, .grid tbody, .grid tr, .grid th, .grid td { display: block; }
  .grid tr { padding: 10px 0; border-bottom: 0.5px solid var(--border); }
  .grid tr:first-child { padding-top: 0; }
  .grid tbody th, .grid td { padding: 2px 0; border: 0; }
  .grid tbody th { margin-bottom: 4px; }
  .grid td { display: grid; grid-template-columns: 8.5em 1fr; gap: 8px; }
  .grid td::before { content: attr(data-nation); color: var(--muted); }
}
.actions {
  position: sticky;
  bottom: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 16px 0 calc(16px + env(safe-area-inset-bottom));
  background: linear-gradient(to bottom, transparent, var(--bg) 14px);
}
.rate { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.card-nav { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; margin: -6px 0 -2px; }
.nav-button {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  min-height: 40px;
  padding: 0 8px;
  border-radius: 10px;
  color: var(--accent);
  font-size: 16px;
  font-weight: 500;
}
.nav-button:first-child { justify-self: start; margin-left: -8px; }
.nav-button:last-child { justify-self: end; margin-right: -8px; }
.nav-button svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.keys { text-align: center; }
@media (hover: none) { .keys { display: none; } }
/* On tall desktop monitors a full-height card would be mostly empty, so let it size to its content.
   (iPads keep the full-height card: their main pointer is touch.) */
@media (min-height: 1000px) and (pointer: fine) {
  .view[data-view="study"] { flex: none; }
  .card { flex: none; min-height: 560px; }
}

/* Mouse and trackpad */
@media (hover: hover) {
  .button:hover:not(:disabled), .chip:hover, .sync-pill:hover { filter: var(--hover); }
  .segment:hover:not([aria-pressed="true"]) { background: color-mix(in srgb, var(--surface) 55%, transparent); }
  .row:hover:not(:disabled) { background: color-mix(in srgb, var(--text) 4%, var(--surface)); }
  .icon-button:hover, .mark:hover { background: var(--surface-2); }
  .nav-button:hover:not(:disabled) { background: color-mix(in srgb, var(--accent) 10%, transparent); }
  .link:hover { text-decoration: underline; }
}

/* Summary */
.summary { display: flex; flex-direction: column; gap: 14px; padding: 24px 20px; text-align: center; }
.summary h2 { margin: 0; }
.panel.summary > .button { margin-top: 0; }
.summary-stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat { display: flex; flex-direction: column; padding: 12px; border-radius: 14px; background: var(--surface-2); font-size: 14px; }
.stat strong { font-size: 30px; }
.stat.known strong { color: var(--known-text); }
.stat.learning strong { color: var(--learning-text); }

/* Account */
.form { display: flex; flex-direction: column; gap: 8px; }
.form label { margin-top: 6px; font-size: 14px; font-weight: 600; color: var(--muted); }
.form input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 12px;
  border: 0.5px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 17px;
}
.form .button { margin-top: 4px; }
.form-message { min-height: 1.4em; font-size: 14px; color: var(--muted); }
.form-message.error { color: var(--danger); }

@keyframes appear { from { opacity: 0; transform: translateY(4px); } }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
