/* ==========================================================================
   Design tokens — Apple system colours, grouped backgrounds, label hierarchy
   ========================================================================== */
:root {
  color-scheme: light dark;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Inter", "Segoe UI", system-ui, Roboto, sans-serif;
  --font-display: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Inter", "Segoe UI", system-ui, sans-serif;

  --bg: #f2f2f7;
  --bg-elev: #ffffff;
  --bg-elev-2: #ffffff;
  --label: #000000;
  --label-2: rgba(60, 60, 67, 0.62);
  --label-3: rgba(60, 60, 67, 0.32);
  --sep: rgba(60, 60, 67, 0.18);
  --fill: rgba(120, 120, 128, 0.12);
  --fill-2: rgba(120, 120, 128, 0.07);
  --fill-strong: rgba(120, 120, 128, 0.2);
  --grid: rgba(60, 60, 67, 0.08);
  --nav-bg: rgba(242, 242, 247, 0.78);
  --alert-bg: rgba(250, 250, 252, 0.86);
  --card-border: transparent;

  --blue: #007aff;  --green: #34c759; --indigo: #5856d6; --orange: #ff9500;
  --pink: #ff2d55;  --purple: #af52de; --red: #ff3b30;   --teal: #30b0c7;
  --cyan: #32ade6;  --mint: #00c7be;   --yellow: #ffcc00;

  --green-text: #248a3d;
  --red-text: #d70015;
  --cos: #248a3d;
  --sin: #c93400;

  --accent: var(--blue);

  --shadow-card: 0 0.5px 1px rgba(0, 0, 0, 0.04), 0 4px 14px rgba(0, 0, 0, 0.05);
  --shadow-float: 0 1px 3px rgba(0, 0, 0, 0.05), 0 12px 32px rgba(0, 0, 0, 0.08), 0 30px 60px rgba(0, 0, 0, 0.06);

  --spring: cubic-bezier(0.32, 1.25, 0.5, 1);
  --ease: cubic-bezier(0.25, 0.1, 0.25, 1);
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --bg-elev: #1c1c1e;
    --bg-elev-2: #2c2c2e;
    --label: #ffffff;
    --label-2: rgba(235, 235, 245, 0.62);
    --label-3: rgba(235, 235, 245, 0.32);
    --sep: rgba(84, 84, 88, 0.62);
    --fill: rgba(118, 118, 128, 0.24);
    --fill-2: rgba(118, 118, 128, 0.14);
    --fill-strong: rgba(118, 118, 128, 0.36);
    --grid: rgba(235, 235, 245, 0.07);
    --nav-bg: rgba(18, 18, 20, 0.72);
    --alert-bg: rgba(44, 44, 46, 0.86);
    --card-border: rgba(255, 255, 255, 0.07);

    --blue: #0a84ff;  --green: #30d158; --indigo: #5e5ce6; --orange: #ff9f0a;
    --pink: #ff375f;  --purple: #bf5af2; --red: #ff453a;   --teal: #40c8e0;
    --cyan: #64d2ff;  --mint: #63e6e2;   --yellow: #ffd60a;

    --green-text: #30d158;
    --red-text: #ff6961;
    --cos: #30d158;
    --sin: #ff9f0a;

    --shadow-card: none;
    --shadow-float: 0 20px 50px rgba(0, 0, 0, 0.5);
  }
}

/* Derived accent shades must be re-declared wherever --accent changes. */
:root, [style*="--accent"] {
  --accent-text: color-mix(in srgb, var(--accent) 78%, #000);
  --accent-fill: color-mix(in srgb, var(--accent) 88%, #000);
}
@media (prefers-color-scheme: dark) {
  :root, [style*="--accent"] { --accent-text: var(--accent); --accent-fill: color-mix(in srgb, var(--accent) 80%, #000); }
}

/* ==========================================================================
   Base
   ========================================================================== */
*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--label);
  font-family: var(--font);
  font-size: 17px;
  line-height: 1.35;
  letter-spacing: -0.008em;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-tap-highlight-color: transparent;
}
body.studying { overflow: hidden; overscroll-behavior: none; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
a { color: inherit; text-decoration: none; }
svg { display: block; }
:focus-visible { outline: 3px solid color-mix(in srgb, var(--accent) 55%, transparent); outline-offset: 2px; border-radius: 10px; }

.touch-only { display: inline; }
.fine-only { display: none; }
@media (hover: hover) and (pointer: fine) {
  .touch-only { display: none; }
  .fine-only { display: inline; }
}

/* ==========================================================================
   Navigation bar (translucent once content scrolls beneath it)
   ========================================================================== */
.navbar {
  position: sticky;
  top: 0;
  z-index: 20;
  padding-top: env(safe-area-inset-top);
  border-bottom: 0.5px solid transparent;
  transition: background-color 0.25s var(--ease), border-color 0.25s var(--ease);
}
.navbar.scrolled {
  background: var(--nav-bg);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-bottom-color: var(--sep);
}
.navbar-inner {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  height: 44px;
  max-width: 1080px;
  margin: 0 auto;
  padding: 0 max(16px, env(safe-area-inset-left));
}
.nav-title {
  font-weight: 600;
  font-size: 17px;
  opacity: 0;
  transform: translateY(6px);
  transition: opacity 0.25s var(--ease), transform 0.35s var(--ease-out);
  white-space: nowrap;
}
.navbar.scrolled .nav-title { opacity: 1; transform: none; }
.nav-back {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  height: 44px;
  margin-left: -8px;
  padding: 0 8px;
  color: var(--accent-text);
  font-size: 17px;
  border-radius: 10px;
}
.nav-back svg { width: 20px; height: 20px; }
.nav-back:active { opacity: 0.4; }

/* ==========================================================================
   Layout
   ========================================================================== */
.content {
  max-width: 1080px;
  margin: 0 auto;
  padding: 0 max(16px, env(safe-area-inset-left)) calc(48px + env(safe-area-inset-bottom));
}
.content.narrow { max-width: 760px; }
@media (min-width: 768px) {
  .content { padding-left: 32px; padding-right: 32px; }
}

.eyebrow {
  margin-top: 6px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--label-2);
}
.large-title {
  margin: 2px 0 18px;
  font-family: var(--font-display);
  font-size: 34px;
  font-weight: 700;
  line-height: 1.1;
  letter-spacing: -0.022em;
}
@media (min-width: 768px) { .large-title { font-size: 44px; } }

.section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin: 32px 4px 12px;
}
.section-head h2 { margin: 0; font-family: var(--font-display); font-size: 22px; font-weight: 700; letter-spacing: -0.016em; }
.section-head > span { font-size: 15px; color: var(--label-2); }

.footnote { margin: 32px 0 0; text-align: center; font-size: 13px; color: var(--label-3); }

.panel { background: var(--bg-elev); border-radius: 20px; }

.link { font-size: 15px; color: var(--accent-text); padding: 6px 2px; }
.link.danger { color: var(--red-text); }
.link:active { opacity: 0.4; }

/* view transitions */
.enter-forward { animation: push-in 0.5s var(--ease-out); }
.enter-back { animation: pop-in 0.5s var(--ease-out); }
.study.enter-forward { animation: sheet-in 0.5s var(--ease-out); }
@keyframes push-in { from { opacity: 0; transform: translateX(32px); } }
@keyframes pop-in { from { opacity: 0; transform: translateX(-32px); } }
@keyframes sheet-in { from { opacity: 0; transform: translateY(40px) scale(0.98); } }

/* ==========================================================================
   Buttons & controls
   ========================================================================== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 50px;
  padding: 0 20px;
  border-radius: 14px;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.01em;
  user-select: none;
  -webkit-user-select: none;
  transition: transform 0.25s var(--spring), filter 0.2s, background-color 0.2s, opacity 0.2s;
}
.btn svg { width: 20px; height: 20px; flex: none; }
.btn:active { transform: scale(0.96); filter: brightness(0.92); }
.btn-xl { min-height: 56px; border-radius: 16px; }
.btn-primary { background: var(--accent-fill); color: #fff; }
.btn-tinted { background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent-text); }
.btn-gray { background: var(--fill); color: var(--label); }
.btn-again { background: color-mix(in srgb, var(--red) 14%, transparent); color: var(--red-text); }
.btn-good { background: color-mix(in srgb, var(--green) 16%, transparent); color: var(--green-text); }
@media (hover: hover) {
  .btn:hover { filter: brightness(1.05); }
  .btn-gray:hover, .btn-tinted:hover, .btn-again:hover, .btn-good:hover { filter: brightness(0.97); }
}

kbd {
  margin-left: 4px;
  padding: 2px 7px;
  border-radius: 6px;
  font: 600 12px/1.4 var(--font);
  background: color-mix(in srgb, currentColor 12%, transparent);
  opacity: 0.8;
}
@media (hover: none), (pointer: coarse) { kbd { display: none; } }

.icon-btn {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--fill);
  color: var(--label-2);
  transition: transform 0.25s var(--spring);
}
.icon-btn svg { width: 17px; height: 17px; }
.icon-btn:active { transform: scale(0.9); }

/* iOS-style switch */
.switch {
  -webkit-appearance: none;
  appearance: none;
  position: relative;
  flex: none;
  width: 51px;
  height: 31px;
  margin: 0;
  border-radius: 16px;
  background: var(--fill-strong);
  cursor: pointer;
  transition: background-color 0.25s var(--ease);
}
.switch::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 27px;
  height: 27px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.15), 0 1px 1px rgba(0, 0, 0, 0.16);
  transition: transform 0.3s var(--spring);
}
.switch:checked { background: var(--green); }
.switch:checked::after { transform: translateX(20px); }

/* progress ring */
.ring { flex: none; }
.ring circle { fill: none; stroke-width: 7; }
.ring-track { stroke: var(--fill); }
.ring-fill { stroke: var(--accent); stroke-linecap: round; transition: stroke-dashoffset 1s var(--ease-out); }
.ring text { font: 700 14px var(--font); fill: var(--label); letter-spacing: -0.02em; }

.bar { height: 5px; border-radius: 3px; background: var(--fill); overflow: hidden; flex: 1; }
.bar > span { display: block; height: 100%; border-radius: inherit; background: var(--c); transition: width 0.6s var(--ease-out); }

/* app-icon style tile */
.app-icon {
  display: grid;
  place-items: center;
  flex: none;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  color: #fff;
  background: linear-gradient(165deg, color-mix(in srgb, var(--c) 68%, #fff) 0%, var(--c) 60%, color-mix(in srgb, var(--c) 88%, #000) 100%);
  box-shadow: 0 2px 8px color-mix(in srgb, var(--c) 30%, transparent), inset 0 0.5px 0 rgba(255, 255, 255, 0.4);
}
.app-icon svg { width: 26px; height: 26px; }
.app-icon.lg { width: 72px; height: 72px; border-radius: 18px; }
.app-icon.lg svg { width: 42px; height: 42px; }
.glyph { font-family: KaTeX_Main, "Times New Roman", serif; font-size: 30px; line-height: 1; white-space: nowrap; }
.glyph i { font-family: KaTeX_Math, "Times New Roman", serif; }
.glyph sup { font-size: 0.6em; vertical-align: 0.7em; }
.glyph.sm { font-size: 16px; }
.app-icon.lg .glyph { font-size: 42px; }
.app-icon.lg .glyph.sm { font-size: 25px; }

/* ==========================================================================
   Home
   ========================================================================== */
.hero {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 16px;
  padding: 18px;
  border-radius: 22px;
  background: var(--bg-elev);
  box-shadow: var(--shadow-card);
}
.hero-title { font-family: var(--font-display); font-size: 20px; font-weight: 700; letter-spacing: -0.016em; }
.hero-sub { margin-top: 2px; font-size: 15px; color: var(--label-2); }
.hero-btn { grid-column: 1 / -1; }
@media (min-width: 640px) {
  .hero { grid-template-columns: auto 1fr auto; padding: 20px 22px; }
  .hero-btn { grid-column: auto; }
}

.deck-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(156px, 1fr));
  gap: 12px;
}
@media (min-width: 768px) {
  .deck-grid { grid-template-columns: repeat(auto-fill, minmax(232px, 1fr)); gap: 16px; }
}
.tile {
  display: flex;
  flex-direction: column;
  min-height: 168px;
  padding: 14px;
  border-radius: 20px;
  background: var(--bg-elev);
  box-shadow: var(--shadow-card);
  transition: transform 0.35s var(--spring), box-shadow 0.35s var(--ease);
}
.tile:active { transform: scale(0.97); }
@media (hover: hover) {
  .tile:hover { transform: translateY(-3px); box-shadow: var(--shadow-float); }
}
@media (min-width: 768px) { .tile { padding: 18px; min-height: 184px; } }
.tile-top { display: flex; justify-content: space-between; align-items: flex-start; }
.tile-done { width: 24px; height: 24px; border-radius: 50%; display: grid; place-items: center; background: var(--green); color: #fff; }
.tile-done svg { width: 14px; height: 14px; }
.tile-title { margin-top: 12px; font-size: 17px; font-weight: 600; letter-spacing: -0.012em; }
.tile-sub { flex: 1; margin-top: 2px; font-size: 13px; line-height: 1.3; color: var(--label-2); }
.tile-foot { display: flex; align-items: center; gap: 10px; margin-top: 14px; }
.tile-count { font-size: 12px; font-weight: 600; color: var(--label-2); font-variant-numeric: tabular-nums; }

/* ==========================================================================
   Deck
   ========================================================================== */
.deck-hero { display: flex; align-items: center; gap: 16px; margin: 4px 0 20px; }
.deck-hero .large-title { margin: 0; }
.deck-desc { margin: 4px 0 0; font-size: 15px; color: var(--label-2); }

.study-panel { padding: 18px; display: grid; gap: 16px; }
.stat-row { display: flex; align-items: center; gap: 14px; }
.stat-title { font-size: 17px; font-weight: 600; }
.stat-sub { font-size: 15px; color: var(--label-2); }
.actions { display: grid; gap: 10px; }
@media (min-width: 560px) { .actions { grid-template-columns: repeat(auto-fit, minmax(0, 1fr)); } }
.switch-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 14px;
  border-top: 0.5px solid var(--sep);
  font-size: 17px;
  cursor: pointer;
}

.list { overflow: hidden; }
.row { position: relative; }
.row + .row::before {
  content: "";
  position: absolute;
  top: 0;
  left: 16px;
  right: 0;
  height: 0.5px;
  background: var(--sep);
}
.row-main {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 60px;
  padding: 10px 16px;
  text-align: left;
  transition: background-color 0.2s;
}
.row-main:active { background: var(--fill-2); }
@media (hover: hover) { .row-main:hover { background: var(--fill-2); } }
.row-thumb { flex: none; width: 72px; display: block; }
.row-thumb .uc { width: 46px; margin: 0 auto; }
.row-text { flex: 1; min-width: 0; display: block; }
.row-prompt { display: block; font-size: 13px; color: var(--label-2); }
.row-q {
  display: block;
  margin-top: 1px;
  font-size: 16px;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  white-space: nowrap;
}
.row-q::-webkit-scrollbar { display: none; }
.row-check { flex: none; width: 20px; color: var(--green-text); opacity: 0; }
.row-check svg { width: 18px; height: 18px; }
.row.known .row-check { opacity: 1; }
.chev { flex: none; width: 14px; color: var(--label-3); transition: transform 0.35s var(--spring); }
.chev svg { width: 14px; height: 14px; }
.row.open .chev { transform: rotate(90deg); }
.row-detail { display: grid; grid-template-rows: 0fr; transition: grid-template-rows 0.4s var(--ease-out); }
.row.open .row-detail { grid-template-rows: 1fr; }
.row-detail-inner { overflow: hidden; min-height: 0; }
.row-answer { padding: 4px 16px 20px; text-align: center; }
.row-answer .vis { max-width: 320px; }

/* ==========================================================================
   Card content (shared by study cards and the deck list)
   ========================================================================== */
.prompt { margin-bottom: 10px; font-size: 15px; color: var(--label-2); }
.prompt strong { display: block; font-size: 20px; font-weight: 700; color: var(--label); letter-spacing: -0.015em; }
.hint { margin-top: 12px; font-size: 13px; color: var(--label-3); }
.math-lg { font-size: clamp(20px, 5.4vw, 27px); }
.math-lg .katex-display { margin: 0.25em 0; padding: 4px 2px; overflow-x: auto; overflow-y: hidden; }
.echo {
  margin: 0 auto 12px;
  padding-bottom: 12px;
  border-bottom: 0.5px solid var(--sep);
  font-size: 15px;
  color: var(--label-2);
  overflow-x: auto;
  overflow-y: hidden;
  white-space: nowrap;
}
.note { max-width: 46ch; margin: 14px auto 0; font-size: 14px; line-height: 1.45; color: var(--label-2); }
.answer-name { margin-top: 2px; font-size: 17px; font-weight: 600; }

.vis { margin: 4px auto 12px; max-width: 360px; }
.vis-circle, .vis-astc, .vis-quadrants { max-width: 188px; }
@media (max-height: 820px) { .face .vis-plot { max-width: 290px; } }

.face-body, .row-answer { container-type: inline-size; }
@container (min-width: 540px) {
  .split { display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); gap: 20px; align-items: center; }
  .split > .vis { width: 100%; max-width: 360px; margin: 0 auto; }
  .split > div > .props:first-child { margin-top: 0; }
  .split .props-3 { grid-template-columns: 1fr; text-align: left; }
}

.props {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-top: 12px;
  text-align: left;
}
.props-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); text-align: center; }
.prop { min-width: 0; padding: 10px 12px; border-radius: 14px; background: var(--fill-2); }
.prop.wide { grid-column: 1 / -1; }
.prop .k { font-size: 12px; font-weight: 600; color: var(--label-2); }
.prop .v { margin-top: 3px; font-size: 17px; overflow-x: auto; overflow-y: hidden; white-space: nowrap; scrollbar-width: none; }
.prop .v::-webkit-scrollbar, .echo::-webkit-scrollbar, .math-lg .katex-display::-webkit-scrollbar { display: none; }
.echo, .math-lg .katex-display { scrollbar-width: none; }
.prop .cap { margin-top: 2px; font-size: 12px; color: var(--label-2); }
.prop.tone-cos .k { color: var(--cos); }
.prop.tone-sin .k { color: var(--sin); }

/* SVG: function plots */
.plot { width: 100%; height: auto; }
.plot-bg { fill: var(--fill-2); }
.plot-grid line { stroke: var(--grid); stroke-width: 1; }
.plot-axes line { stroke: var(--label-3); stroke-width: 1.3; }
.plot-asym line { stroke: var(--label-3); stroke-width: 1.2; stroke-dasharray: 5 5; }
.plot-curve { fill: none; stroke: var(--accent); stroke-width: 2.8; stroke-linecap: round; stroke-linejoin: round; }
.plot.mini .plot-curve { stroke-width: 7; }
.plot.mini .plot-axes line { stroke-width: 3; }
.plot.mini .plot-grid { display: none; }
.plot-dot { fill: var(--accent); }
.plot-labels text { font: 500 10px var(--font); fill: var(--label-2); }

/* SVG: unit circle */
.uc { width: 100%; height: auto; overflow: visible; }
.uc-axis { stroke: var(--label-3); stroke-width: 1.4; }
.uc-ring { fill: none; stroke: var(--label-2); stroke-width: 2.2; }
.uc-tick { fill: var(--label-3); }
.uc-wedge { fill: color-mix(in srgb, var(--accent) 18%, transparent); stroke: var(--accent); stroke-width: 1.4; }
.uc-theta { font: italic 20px KaTeX_Math, "Times New Roman", serif; fill: var(--accent); }
.uc-ray { stroke: var(--accent); stroke-width: 4; stroke-linecap: round; }
.uc-pt { fill: var(--accent); stroke: var(--bg-elev); stroke-width: 3; }
.uc-dash { stroke: var(--label-3); stroke-width: 1.6; stroke-dasharray: 4 4; }
.uc-cos { stroke: var(--cos); stroke-width: 6; stroke-linecap: round; }
.uc-sin { stroke: var(--sin); stroke-width: 6; stroke-linecap: round; }
.uc-letter { font: 700 40px var(--font-display); fill: var(--accent); }
.uc-word { font: 600 15px var(--font); fill: var(--label-2); }
.uc-roman { font: 600 28px "Times New Roman", serif; fill: var(--label-2); }

/* ==========================================================================
   Study
   ========================================================================== */
.study {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
  overflow: hidden;
}
.study-bar {
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  align-items: center;
  width: 100%;
  max-width: 720px;
  height: 56px;
  margin: 0 auto;
  padding: 0 16px;
}
.study-title { text-align: center; line-height: 1.2; }
.st-t { font-size: 17px; font-weight: 600; }
.st-s { font-size: 13px; color: var(--label-2); font-variant-numeric: tabular-nums; }
.progress {
  width: calc(100% - 32px);
  max-width: 688px;
  height: 4px;
  margin: 0 auto;
  border-radius: 2px;
  background: var(--fill);
  overflow: hidden;
}
.progress span { display: block; width: 0; height: 100%; border-radius: inherit; background: var(--accent); transition: width 0.5s var(--ease-out); }

.stage {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px 16px 30px;
}
.stack {
  position: relative;
  width: 100%;
  max-width: 640px;
  height: 100%;
  max-height: 580px;
  perspective: 1800px;
}
.ghost {
  position: absolute;
  inset: 0;
  border-radius: 28px;
  background: var(--bg-elev);
  border: 0.5px solid var(--card-border);
  box-shadow: var(--shadow-card);
  transform-origin: 50% 100%;
  transition: opacity 0.3s var(--ease), transform 0.4s var(--spring);
}
.ghost.g1 { transform: translateY(10px) scale(0.955); opacity: 0.7; }
.ghost.g2 { transform: translateY(20px) scale(0.91); opacity: 0.4; }
.stack[data-depth="1"] .g2, .stack[data-depth="0"] .ghost { opacity: 0; }

.card-wrap {
  position: absolute;
  inset: 0;
  z-index: 2;
  touch-action: pan-y;
  user-select: none;
  -webkit-user-select: none;
  cursor: pointer;
  transition: transform 0.5s var(--spring), opacity 0.3s var(--ease);
}
.card-wrap.dragging { transition: none; cursor: grabbing; }
.card-wrap.fly { transition: transform 0.3s cubic-bezier(0.4, 0, 1, 1), opacity 0.3s ease-in; }
.card-wrap.enter { animation: card-in 0.5s var(--ease-out); }
@keyframes card-in { from { opacity: 0; transform: translateY(26px) scale(0.95); } }

.card {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transition: transform 0.6s cubic-bezier(0.3, 1.12, 0.5, 1);
}
.card.flipped { transform: rotateY(180deg); }
.face {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  border-radius: 28px;
  background: var(--bg-elev);
  border: 0.5px solid var(--card-border);
  box-shadow: var(--shadow-float);
  overflow: hidden;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
}
.back { transform: rotateY(180deg); }
.card:not(.flipped) .back, .card.flipped .front { pointer-events: none; }
.face-head { display: flex; justify-content: space-between; align-items: center; padding: 18px 22px 0; }
.chip { font-size: 12px; font-weight: 700; letter-spacing: 0.07em; text-transform: uppercase; color: var(--accent-text); }
.face-body {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 10px 22px;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
}
.face-content { width: 100%; margin: auto 0; text-align: center; }
.face-foot { padding: 4px 22px 18px; text-align: center; font-size: 13px; color: var(--label-3); }

.stamp {
  position: absolute;
  top: 16px;
  padding: 5px 12px;
  border: 2.5px solid currentColor;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  background: var(--bg-elev);
  pointer-events: none;
}
.stamp-good { right: 18px; color: var(--green-text); transform: rotate(8deg); opacity: var(--good, 0); }
.stamp-again { left: 18px; color: var(--red-text); transform: rotate(-8deg); opacity: var(--again, 0); }

.controls {
  position: relative;
  width: 100%;
  max-width: 672px;
  margin: 0 auto;
  padding: 0 16px calc(16px + env(safe-area-inset-bottom));
}
.reveal { width: 100%; }
.reveal, .grade { transition: opacity 0.25s var(--ease), transform 0.4s var(--spring); }
.grade {
  position: absolute;
  top: 0;
  left: 16px;
  right: 16px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  opacity: 0;
  transform: translateY(10px) scale(0.98);
  pointer-events: none;
}
.controls.flipped .reveal { opacity: 0; transform: scale(0.98); pointer-events: none; }
.controls.flipped .grade { opacity: 1; transform: none; pointer-events: auto; }
.study.finished .controls { display: none; }

.done { width: 100%; max-width: 420px; margin: auto; text-align: center; animation: card-in 0.6s var(--ease-out); }
.done-badge {
  display: grid;
  place-items: center;
  width: 88px;
  height: 88px;
  margin: 0 auto 18px;
  border-radius: 50%;
  background: var(--green);
  color: #fff;
  box-shadow: 0 12px 30px color-mix(in srgb, var(--green) 40%, transparent);
  animation: badge-pop 0.6s var(--spring);
}
.done-badge svg { width: 46px; height: 46px; stroke-dasharray: 22; stroke-dashoffset: 22; animation: draw 0.45s 0.3s var(--ease-out) forwards; }
@keyframes badge-pop { from { transform: scale(0.4); opacity: 0; } }
@keyframes draw { to { stroke-dashoffset: 0; } }
.done h2 { margin: 0 0 4px; font-family: var(--font-display); font-size: 28px; font-weight: 700; letter-spacing: -0.02em; }
.done p { margin: 0; color: var(--label-2); }
.done-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 24px 0; }
.done-stats div { padding: 14px 6px; border-radius: 16px; background: var(--bg-elev); }
.done-stats b { display: block; font-family: var(--font-display); font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; }
.done-stats span { font-size: 12px; color: var(--label-2); }
.done-actions { display: grid; gap: 10px; }

@media (max-height: 640px) {
  .stage { padding-top: 12px; padding-bottom: 24px; }
  .face-head { padding-top: 14px; }
  .vis-circle { max-width: 150px; }
}

/* ==========================================================================
   Alert (iOS style)
   ========================================================================== */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(0, 0, 0, 0.3);
  animation: fade-in 0.2s var(--ease);
}
.alert {
  width: min(290px, 100%);
  border-radius: 16px;
  overflow: hidden;
  text-align: center;
  background: var(--alert-bg);
  -webkit-backdrop-filter: blur(30px) saturate(180%);
  backdrop-filter: blur(30px) saturate(180%);
  animation: alert-in 0.35s var(--spring);
}
.alert-body { padding: 20px 16px 18px; }
.alert h3 { margin: 0 0 4px; font-size: 17px; font-weight: 600; }
.alert p { margin: 0; font-size: 13px; line-height: 1.35; }
.alert-actions { display: flex; border-top: 0.5px solid var(--sep); }
.alert-actions button { flex: 1; height: 46px; font-size: 17px; color: var(--blue); }
.alert-actions button + button { border-left: 0.5px solid var(--sep); }
.alert-actions button:active { background: var(--fill); }
.alert-actions .cancel { font-weight: 600; }
.alert-actions .destructive { color: var(--red); }
@keyframes fade-in { from { opacity: 0; } }
@keyframes alert-in { from { opacity: 0; transform: scale(1.12); } }

/* ==========================================================================
   Explain button & sheet
   ========================================================================== */
body.modal-open { overflow: hidden; }

.explain-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 30px;
  margin: -6px -8px -6px 0;
  padding: 0 12px 0 10px;
  border-radius: 15px;
  font-size: 14px;
  font-weight: 600;
  color: var(--accent-text);
  background: color-mix(in srgb, var(--accent) 14%, transparent);
  transition: transform 0.25s var(--spring), filter 0.2s;
}
.explain-btn::after { content: ""; position: absolute; inset: -7px -4px; } /* 44px touch target */
.explain-btn svg { width: 16px; height: 16px; }
.explain-btn:active { transform: scale(0.94); }
@media (hover: hover) { .explain-btn:hover { filter: brightness(0.95); } }
.explain-row { margin-top: 16px; min-height: 44px; font-size: 15px; }

.sheet-backdrop {
  align-items: end;
  padding: 0;
  background: rgba(0, 0, 0, 0.35);
}
.sheet {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-height: calc(100dvh - 40px - env(safe-area-inset-top));
  border-radius: 22px 22px 0 0;
  background: var(--bg);
  box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.18);
  overflow: hidden;
  animation: sheet-up 0.45s var(--ease-out);
  transition: transform 0.35s var(--ease-out), opacity 0.25s var(--ease);
}
.sheet.dragging { transition: none; }
.sheet-backdrop.closing { animation: fade-out 0.26s var(--ease) forwards; }
.sheet-backdrop.closing .sheet { transform: translateY(100%) !important; }
@keyframes sheet-up { from { transform: translateY(100%); } }
@keyframes fade-out { to { background: rgba(0, 0, 0, 0); } }

@media (min-width: 640px) {
  .sheet-backdrop { align-items: center; padding: 32px; }
  .sheet {
    width: min(620px, 100%);
    max-height: min(86vh, 900px);
    border-radius: 22px;
    box-shadow: var(--shadow-float);
    animation: alert-in 0.4s var(--spring);
  }
  .sheet-backdrop.closing .sheet { transform: scale(0.96) !important; opacity: 0; }
}

.sheet-head {
  position: relative;
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 22px 20px 14px;
  touch-action: none;
  cursor: grab;
}
.sheet-grabber {
  position: absolute;
  top: 7px;
  left: 50%;
  width: 36px;
  height: 5px;
  margin-left: -18px;
  border-radius: 3px;
  background: var(--label-3);
}
@media (min-width: 640px) { .sheet-grabber { display: none; } .sheet-head { cursor: default; } }
.sheet-titles { flex: 1; min-width: 0; }
.sheet-titles h3 { margin: 2px 0 0; font-family: var(--font-display); font-size: 22px; font-weight: 700; letter-spacing: -0.016em; line-height: 1.2; }
.sheet-titles h3 strong { font-weight: inherit; }
.sheet-close { flex: none; }

.sheet-body {
  flex: 1;
  min-height: 0;
  padding: 0 20px calc(28px + env(safe-area-inset-bottom));
  overflow-y: auto;
  overscroll-behavior: contain;
  container-type: inline-size;
}
.recap {
  padding: 14px 16px 16px;
  border-radius: 18px;
  background: var(--bg-elev);
  text-align: center;
}
.recap-label {
  margin-top: 6px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--label-2);
  text-align: left;
}
.recap-label:first-child { margin-top: 0; }
.recap .math-lg { font-size: clamp(18px, 4.6vw, 23px); }
.recap .vis-plot { max-width: 300px; }
.recap .vis-circle, .recap .vis-astc { max-width: 160px; }

.sheet-section {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 22px 4px 8px;
  font-size: 17px;
  font-weight: 700;
  color: var(--accent-text);
}
.sheet-section svg { width: 19px; height: 19px; }
.explain { margin: 0 4px; font-size: 16px; line-height: 1.55; }
.explain p { margin: 0 0 12px; }
.explain .katex { font-size: 1.08em; }
.explain-math { margin: 4px 0 14px; padding: 4px 12px; border-radius: 14px; background: var(--bg-elev); overflow-x: auto; scrollbar-width: none; }
.explain-math .katex-display { margin: 0.6em 0; }
.callout {
  display: grid;
  gap: 2px;
  margin: 16px 0 0;
  padding: 12px 14px;
  border-radius: 14px;
  border-left: 4px solid var(--accent);
  background: color-mix(in srgb, var(--accent) 10%, var(--bg-elev));
  font-size: 15px;
  line-height: 1.45;
}
.callout strong { font-size: 12px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--accent-text); }

/* ==========================================================================
   Accounts
   ========================================================================== */
.title-row { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; }
.title-row .large-title { margin-bottom: 18px; }
.avatar {
  display: grid;
  place-items: center;
  flex: none;
  width: 38px;
  height: 38px;
  margin-bottom: 20px;
  border-radius: 50%;
  font: 600 17px var(--font-display);
  color: #fff;
  background: linear-gradient(160deg, #8e8e93, #636366);
  transition: transform 0.25s var(--spring);
}
.avatar:not(.guest) { background: linear-gradient(160deg, #5ac8fa, var(--blue)); }
.avatar.guest { color: var(--blue); background: color-mix(in srgb, var(--blue) 14%, transparent); }
.avatar svg { width: 22px; height: 22px; }
.avatar:active { transform: scale(0.92); }
@media (min-width: 768px) { .avatar { width: 44px; height: 44px; margin-bottom: 26px; } }
.avatar.lg { width: 56px; height: 56px; margin: 0; font-size: 24px; }

.sync-banner {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  margin-bottom: 14px;
  padding: 14px 16px;
  border-radius: 18px;
  text-align: left;
  background: var(--bg-elev);
  box-shadow: var(--shadow-card);
  transition: transform 0.3s var(--spring);
}
.sync-banner:active { transform: scale(0.985); }
.sync-icon {
  display: grid;
  place-items: center;
  flex: none;
  width: 40px;
  height: 40px;
  border-radius: 11px;
  color: #fff;
  background: linear-gradient(160deg, #5ac8fa, var(--blue));
}
.sync-icon svg { width: 24px; height: 24px; }
.sync-text { flex: 1; min-width: 0; display: grid; gap: 1px; }
.sync-text strong { font-size: 16px; font-weight: 600; }
.sync-text span { font-size: 14px; color: var(--label-2); }
.sync-banner .chev { color: var(--label-3); }

.footnote .link { padding: 0; font-size: inherit; }

.account-view { padding-top: 2px; }
.sheet-lead { margin: 0 4px 16px; font-size: 15px; line-height: 1.45; color: var(--label-2); }
.sheet-lead strong { color: var(--label); }

.field-group { border-radius: 14px; background: var(--bg-elev); overflow: hidden; }
.field {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 50px;
  padding: 0 16px;
  cursor: text;
}
.field + .field::before {
  content: "";
  position: absolute;
  top: 0;
  left: 16px;
  right: 0;
  height: 0.5px;
  background: var(--sep);
}
.field-label { flex: none; width: 82px; font-size: 16px; }
.field input {
  flex: 1;
  min-width: 0;
  height: 50px;
  padding: 0;
  border: 0;
  outline: 0;
  background: none;
  color: var(--label);
  font: 16px var(--font); /* 16px+ stops iOS zooming in */
}
.field input::placeholder { color: var(--label-3); }
.field:focus-within .field-label { color: var(--accent-text); }

.form-error {
  margin: 10px 4px 0;
  font-size: 14px;
  line-height: 1.4;
  color: var(--red-text);
}
.form-note { margin: 12px 4px 0; font-size: 13px; line-height: 1.45; color: var(--label-2); text-align: center; }
.form-submit { width: 100%; margin-top: 16px; }
.btn.wide { width: 100%; }
.btn-destructive { background: var(--red); color: #fff; }

.spinner {
  display: none;
  width: 18px;
  height: 18px;
  border: 2.5px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
.form-submit.loading .spinner { display: block; }
.form-submit:disabled { opacity: 0.75; }
@keyframes spin { to { transform: rotate(360deg); } }

.auth-links {
  display: grid;
  justify-items: center;
  gap: 6px;
  margin-top: 18px;
  font-size: 15px;
  color: var(--label-2);
}
.auth-links .link { padding: 4px 2px; }

.sheet-message {
  display: grid;
  justify-items: center;
  gap: 4px;
  margin-bottom: 18px;
  padding: 22px 18px;
  border-radius: 16px;
  background: var(--bg-elev);
  text-align: center;
  font-size: 15px;
  line-height: 1.45;
}
.sheet-message p { margin: 0; }
.sheet-message .muted { color: var(--label-2); font-size: 14px; }
.sheet-message.warning { background: color-mix(in srgb, var(--red) 10%, var(--bg-elev)); }
.message-icon {
  display: grid;
  place-items: center;
  width: 52px;
  height: 52px;
  margin-bottom: 8px;
  border-radius: 50%;
  color: #fff;
  background: var(--green);
}
.message-icon svg { width: 28px; height: 28px; }

.account-card { display: flex; align-items: center; gap: 14px; padding: 4px 4px 18px; }
.account-id { display: grid; gap: 2px; min-width: 0; }
.account-id strong { font-size: 18px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.sync-status { font-size: 14px; color: var(--label-2); }
.sync-status.error { color: var(--red-text); }
.account-stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 18px; }
.account-stats div { padding: 12px; border-radius: 14px; background: var(--bg-elev); text-align: center; }
.account-stats b { display: block; font: 700 24px var(--font-display); font-variant-numeric: tabular-nums; }
.account-stats span { font-size: 12px; color: var(--label-2); }

.menu { border-radius: 14px; background: var(--bg-elev); overflow: hidden; }
.menu-row {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 50px;
  padding: 0 16px;
  text-align: left;
  font-size: 16px;
  color: var(--accent-text);
}
.menu-row + .menu-row::before {
  content: "";
  position: absolute;
  top: 0;
  left: 16px;
  right: 0;
  height: 0.5px;
  background: var(--sep);
}
.menu-row svg { width: 20px; height: 20px; flex: none; }
.menu-row span:first-of-type { flex: 1; }
.menu-row .chev { flex: none; color: var(--label-3); }
.menu-row .chev svg { width: 14px; height: 14px; }
.menu-row.danger { color: var(--red-text); justify-content: center; }
.menu-row.danger span { flex: none; }
.menu-row:active { background: var(--fill-2); }
.menu-row:disabled { opacity: 0.5; }
.delete-link { display: block; margin: 18px auto 0; font-size: 14px; }

.toast {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: calc(24px + env(safe-area-inset-bottom));
  z-index: 200;
  width: fit-content;
  margin: 0 auto;
  text-align: center;
  padding: 12px 18px;
  border-radius: 22px;
  font-size: 15px;
  font-weight: 500;
  color: var(--label);
  background: var(--alert-bg);
  -webkit-backdrop-filter: blur(30px) saturate(180%);
  backdrop-filter: blur(30px) saturate(180%);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.18);
  opacity: 0;
  transform: translateY(20px);
  pointer-events: none;
  transition: opacity 0.3s var(--ease), transform 0.45s var(--spring);
}
.toast.show { opacity: 1; transform: none; }

/* ==========================================================================
   Reduced motion
   ========================================================================== */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
