/* ============================================
   Lernin Design System & Component Styles
   Light/Dark tokens, animations, components
   ============================================ */

/* ---------- Reset & Base ---------- */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: var(--font-body, var(--font-sans));
  background: var(--bg);
  color: var(--ink);
  line-height: 1.5;
  min-height: 100vh;
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ---------- Design Tokens ---------- */
:root {
  --bg: #F3F8F4;
  --surface: #FFFFFF;
  --surface-raised: #FFFFFF;
  --surface-hover: #F7FAF8;
  --ink: #0F1810;
  --ink-secondary: #5A6B5C;
  --ink-muted: #8A9A8C;
  --accent: #2E7D32;
  --accent-hover: #1B5E20;
  --accent-soft: rgba(46,125,50,0.08);
  --danger: #C62828;
  --danger-soft: rgba(198,40,40,0.08);
  --warning: #F9A825;
  --warning-soft: rgba(249,168,37,0.08);
  --sand: #C8D6C9;
  --ochre: #E6A23C;
  --moss: #2E7D32;
  --map-bg: #E8EDE9;
  --map-bg-sky: #D0DEDE;
  --map-bg-horizon: #E8EDE9;
  --map-ink: #1A1F1B;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.04);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.08);
  --shadow-lg: 0 8px 24px rgba(0,0,0,0.12);
  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --radius-xl: 28px;
  --space-xs: 4px;
  --space-sm: 8px;
  --space-md: 16px;
  --space-lg: 24px;
  --space-xl: 32px;
  --space-2xl: 48px;
  --font-sans: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
  --font-mono: 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', monospace;
  --font-body: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
  /* Fraunces (a warm, slightly hand-cut serif) carries headings/titles — the
     original identity from the "Organic UI redesign", restored after the
     later UI/UX rewrite silently dropped it. Body/UI text stays on
     --font-body (user-selectable via Settings font picker), independent
     of this. */
  --font-display: 'Fraunces', Georgia, serif;
}

[data-theme="dark"] {
  --bg: #0F1810;
  --surface: #1A221B;
  --surface-raised: #223024;
  --surface-hover: #2A3A2C;
  --ink: #EDEFF1;
  --ink-secondary: #A0B0A2;
  --ink-muted: #6B7B6D;
  --accent: #66BB6A;
  --accent-hover: #81C784;
  --accent-soft: rgba(102,187,106,0.10);
  --danger: #EF5350;
  --danger-soft: rgba(239,83,80,0.10);
  --warning: #FFCA28;
  --map-bg: #14181C;
  --map-bg-sky: #0A0D10;
  --map-bg-horizon: #1E2830;
  --map-ink: #EDEFF1;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.2);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.3);
  --shadow-lg: 0 8px 24px rgba(0,0,0,0.4);
}

/* ---------- Accessibility ---------- */
*:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

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

/* ---------- Animations ---------- */
@keyframes fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

@keyframes slideUp {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}

@keyframes slideDown {
  from { transform: translateY(0); }
  to   { transform: translateY(100%); }
}

@keyframes toastIn {
  from { transform: translateY(100px); opacity: 0; }
  to   { transform: translateY(0); opacity: 1; }
}

@keyframes toastOut {
  from { transform: translateY(0); opacity: 1; }
  to   { transform: translateY(100px); opacity: 0; }
}

@keyframes cardEnter {
  from { opacity: 0; transform: translateX(24px); }
  to   { opacity: 1; transform: translateX(0); }
}

@keyframes cardExit {
  from { opacity: 1; transform: translateX(0); }
  to   { opacity: 0; transform: translateX(-24px); }
}

/* Distinct, deliberately slower/calmer exit for a card that just became a
   leech and is being set aside -- a settling drift-down-and-shrink, not
   the brisk sideways swipe of a normal grade. See playLeechBanish() in
   sound.js and .study-card-wrap.is-exiting-leech below. */
@keyframes cardBanish {
  0%   { opacity: 1; transform: translateY(0) scale(1); }
  60%  { opacity: 0.55; transform: translateY(16px) scale(0.97); }
  100% { opacity: 0; transform: translateY(30px) scale(0.94); }
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.03); }
}

/* ---------- App Shell ---------- */
#root {
  min-height: 100vh;
  min-height: 100dvh;
  padding-bottom: env(safe-area-inset-bottom);
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 50;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--space-md);
  background: var(--bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid rgba(0,0,0,0.04);
}

[data-theme="dark"] .app-header {
  border-bottom-color: rgba(255,255,255,0.04);
}

.app-header-title {
  font-family: var(--font-display);
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.5px;
  color: var(--ink);
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: center;
}

.app-header-actions {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  flex-shrink: 0;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-md);
  border: none;
  background: transparent;
  color: var(--ink-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  transition: background 0.15s, color 0.15s;
}

.icon-btn:hover {
  background: var(--accent-soft);
  color: var(--accent);
}

.icon-btn:active {
  transform: scale(0.94);
}

.header-overflow-wrap {
  position: relative;
}

.header-overflow-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 100;
  min-width: 175px;
  background: var(--surface-raised, var(--surface));
  border: 1px solid rgba(0, 0, 0, 0.08);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  animation: fadeIn 0.15s ease;
}

[data-theme="dark"] .header-overflow-menu {
  border-color: rgba(255, 255, 255, 0.12);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
}

.header-overflow-menu[hidden] {
  display: none;
}

.header-overflow-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 12px;
  border: none;
  background: transparent;
  color: var(--ink);
  font-size: 14px;
  font-weight: 500;
  font-family: inherit;
  border-radius: var(--radius-sm);
  cursor: pointer;
  text-align: left;
  transition: background 0.12s, color 0.12s;
}

.header-overflow-item:hover,
.header-overflow-item:focus-visible {
  background: var(--accent-soft);
  color: var(--accent);
}

.header-overflow-item:active {
  transform: scale(0.98);
}

.overflow-item-icon {
  font-size: 16px;
  line-height: 1;
  flex-shrink: 0;
}

/* Back button - large touch target */
.back-btn {
  width: 48px;
  height: 48px;
  flex-shrink: 0; /* a long title in the header must never squeeze the tap target (it was 22 px wide on a long file name) */
  border-radius: var(--radius-md);
  border: none;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  padding: 0;
  transition: background 0.15s;
}

.back-btn:hover {
  background: var(--accent-soft);
}

.back-btn:active {
  transform: scale(0.94);
}

/* ---------- Hero CTA ---------- */
.hero-cta {
  margin: var(--space-md);
  padding: var(--space-xl);
  border-radius: var(--radius-lg);
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-hover) 100%);
  color: white;
  box-shadow: var(--shadow-md);
  animation: fadeIn 0.4s ease;
}

.hero-cta-title {
  font-family: var(--font-display);
  font-size: 24px;
  font-weight: 700;
  margin-bottom: var(--space-xs);
}

.hero-cta-sub {
  font-size: 15px;
  opacity: 0.9;
  margin-bottom: var(--space-lg);
}

.hero-cta.hero-cta-caught-up {
  padding: var(--space-lg) var(--space-xl);
}

.hero-cta-caught-up .hero-cta-sub {
  margin-bottom: 0;
}

.hero-cta-btn {
  width: 100%;
  padding: 14px;
  border-radius: var(--radius-md);
  border: none;
  background: white;
  color: var(--accent);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
}

.hero-cta-btn:hover {
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

.hero-cta-btn:active {
  transform: translateY(0);
}

[data-theme="dark"] .hero-cta {
  background: linear-gradient(135deg, #14351C 0%, #0D2313 100%);
  border: 1px solid rgba(102, 187, 106, 0.28);
  color: #F0F6F1;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.45);
}

[data-theme="dark"] .hero-cta-title {
  color: #FFFFFF;
}

[data-theme="dark"] .hero-cta-sub {
  color: #A3C4A8;
  opacity: 1;
}

[data-theme="dark"] .hero-cta-btn {
  background: #66BB6A;
  color: #08160B;
  font-weight: 700;
}

[data-theme="dark"] .hero-cta-btn:hover {
  background: #81C784;
}

/* ---------- Stats Strip ---------- */
.stats-strip {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-sm) var(--space-lg);
  margin: 0 var(--space-md) var(--space-md);
  padding: var(--space-md) var(--space-lg);
  background: var(--surface);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
}

.stat-item {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  font-size: 14px;
  color: var(--ink-secondary);
}

.stat-value {
  font-weight: 700;
  color: var(--ink);
  font-size: 16px;
}

.stat-item { white-space: nowrap; }

.stat-link {
  white-space: nowrap;
  margin-left: auto;
  font-size: 14px;
  color: var(--accent);
  text-decoration: none;
  font-weight: 500;
}

.stat-link:hover {
  text-decoration: underline;
}

.stat-freeze-btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-xs);
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid rgba(46, 125, 50, 0.2);
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 13px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s, border-color 0.15s;
}

[data-theme="dark"] .stat-freeze-btn {
  border-color: rgba(102, 187, 106, 0.3);
}

.stat-freeze-btn:hover {
  background: rgba(46, 125, 50, 0.15);
}

[data-theme="dark"] .stat-freeze-btn:hover {
  background: rgba(102, 187, 106, 0.2);
}

.stat-freeze-btn:active {
  transform: scale(0.96);
}

/* ---------- Deck Tiles ---------- */
.deck-list {
  padding: 0 var(--space-md);
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}

.deck-tile {
  position: relative;
  background: var(--surface);
  border-radius: var(--radius-md);
  padding: var(--space-md) var(--space-lg);
  box-shadow: var(--shadow-sm);
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  transition: transform 0.12s, box-shadow 0.12s;
  overflow: hidden;
}

.deck-tile::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 4px;
  border-radius: var(--radius-md) 0 0 var(--radius-md);
  background: var(--sand);
  transition: background 0.3s;
}

.deck-tile.is-mastered::before { background: var(--moss); }
.deck-tile.is-progress::before { background: var(--ochre); }

.deck-tile:active {
  transform: scale(0.98);
}

.deck-tile-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-xs);
}

.deck-tile-title {
  font-family: var(--font-display);
  font-size: 17px;
  font-weight: 600;
  color: var(--ink);
  min-width: 0;                 /* the title gives way to the ⋮ button instead of pushing it out of the tile */
  overflow-wrap: anywhere;      /* a very long word breaks rather than overflowing */
  -webkit-hyphens: auto;
  hyphens: auto;                /* where the browser has a dictionary it breaks at a syllable, not mid-letter */
}

.deck-tile-badge {
  flex-shrink: 0;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  background: var(--accent-soft);
  color: var(--accent);
  line-height: 1.4;
}

.deck-tile-badge.is-archived-badge {
  background: rgba(138, 154, 140, 0.15);
  color: var(--ink-secondary);
}

.deck-tile.is-archived {
  opacity: 0.72;
  border: 1px dashed var(--ink-muted);
}

.deck-tile.is-archived::before {
  background: var(--ink-muted);
}

.archived-decks-toggle {
  display: inline-flex;
  align-items: center;
  gap: var(--space-sm);
  background: none;
  border: none;
  padding: 8px 4px;
  cursor: pointer;
  font-family: inherit;
  color: var(--ink-secondary);
}

.archived-decks-toggle:hover {
  color: var(--ink);
}

/* ---------- Card browser: solitaire-style tiles ---------- */
.card-tile-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(94px, 1fr));
  gap: 10px;
}

.card-tile {
  position: relative;
  aspect-ratio: 5 / 7;
  background: var(--surface);
  border-radius: var(--radius-md);
  border: 1px solid rgba(0,0,0,0.05);
  padding: 9px 8px 8px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.card-tile:hover,
.card-tile:active {
  transform: translateY(-3px);
  box-shadow: var(--shadow-md);
}

/* Dog-ear fold, top-right — the classic playing-card tell */
.card-tile::after {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  width: 13px;
  height: 13px;
  background: linear-gradient(135deg, transparent 50%, var(--bg) 50%);
  border-bottom-left-radius: 3px;
}

.card-tile-corner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 5px;
  flex-shrink: 0;
}

.card-tile-suit {
  display: flex;
  align-items: center;
  color: var(--ink-secondary);
}

.card-tile-state-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
}

.card-tile-text {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-size: 11.5px;
  font-weight: 600;
  line-height: 1.32;
  color: var(--ink);
  overflow: hidden;
}

.card-tile-text-inner {
  display: -webkit-box;
  -webkit-line-clamp: 6;
  -webkit-box-orient: vertical;
  overflow: hidden;
  word-break: break-word;
}

.card-tile.is-suspended {
  opacity: 0.5;
}

.card-tile-heart {
  position: absolute;
  bottom: 6px;
  right: 7px;
  display: flex;
  color: #C4472B;
}

.card-tile-fidelity {
  font-size: 11px;
  line-height: 1;
  margin-left: 2px;
}

.card-fidelity-banner {
  background: var(--warning-soft, rgba(249,168,37,0.1));
  border: 1px solid rgba(249,168,37,0.3);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  margin-bottom: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

[data-theme="dark"] .card-fidelity-banner {
  background: rgba(255,202,40,0.08);
  border-color: rgba(255,202,40,0.25);
}

.card-fidelity-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  font-weight: 600;
  color: #b45309;
}

[data-theme="dark"] .card-fidelity-badge {
  color: #fbbf24;
}

.card-fidelity-note {
  font-size: 12px;
  color: var(--ink-secondary);
  line-height: 1.4;
}

/* --- Tier 4 #5: Two-pass card approval and draft staging --- */
.edit-step-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: var(--space-md);
}

.edit-step-section-header {
  padding: 0 var(--space-xs);
  margin-bottom: 2px;
}

.edit-step-section-title {
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.02em;
  color: var(--ink);
  display: flex;
  align-items: center;
  gap: 6px;
}

.edit-step-section-desc {
  font-size: 12px;
  color: var(--ink-muted);
  margin-top: 2px;
  line-height: 1.4;
}

.edit-card-select-label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-secondary);
  cursor: pointer;
  user-select: none;
}

.edit-card-checkbox {
  width: 16px;
  height: 16px;
  accent-color: var(--moss);
  cursor: pointer;
}

.browser-filter-bar {
  display: flex;
  gap: 8px;
  padding: var(--space-sm) var(--space-md) 0;
  overflow-x: auto;
}

.browser-filter-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink-secondary);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
}

.browser-filter-chip:hover {
  background: var(--surface-hover);
  color: var(--ink);
}

.browser-filter-chip.is-active {
  background: var(--moss);
  border-color: var(--moss);
  color: #fff;
}

[data-theme="dark"] .browser-filter-chip.is-active {
  background: #34d399;
  border-color: #34d399;
  color: #064e3b;
}

.browser-filter-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 16px;
  padding: 0 4px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.08);
  font-size: 10px;
  font-weight: 700;
}

.browser-filter-chip.is-active .browser-filter-count {
  background: rgba(255, 255, 255, 0.25);
  color: inherit;
}

[data-theme="dark"] .browser-filter-chip.is-active .browser-filter-count {
  background: rgba(0, 0, 0, 0.2);
  color: inherit;
}

.deck-menu-btn {
  flex: 0 0 44px;               /* never squeezed by a long title (it used to measure 18 px wide) */
  width: 44px;
  height: 44px;
  margin: -6px -20px -6px 0;    /* a 44 px tap target that borrows the tile's own padding, so it neither makes the tile taller nor takes room from the title */
  border: none;
  background: transparent;
  color: var(--ink-secondary);
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  opacity: 0.5;
  transition: opacity 0.15s, background 0.15s;
  padding: 0;
}

.deck-menu-btn:hover,
.deck-menu-btn:active {
  opacity: 1;
  background: var(--surface-hover);
}

.deck-tile-metrics {
  display: flex;
  align-items: center;
  gap: var(--space-md);
  margin-top: var(--space-sm);
}

.deck-tile-bar {
  flex: 1;
  min-width: 0;
  height: 6px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.08);
  overflow: hidden;
  position: relative;
}

[data-theme="dark"] .deck-tile-bar {
  background: rgba(255, 255, 255, 0.12);
}

.deck-tile-bar-fill {
  height: 100%;
  border-radius: 999px;
  background: var(--moss);
  transition: width 0.4s ease;
}

[data-theme="dark"] .deck-tile-bar-fill {
  background: #5CAE61;
}

/* ---------- Bottom Sheet ---------- */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 100;
  animation: fadeIn 0.2s ease;
}

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 101;
  background: var(--surface);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  padding: var(--space-md) 0 env(safe-area-inset-bottom);
  animation: slideUp 0.3s cubic-bezier(0.32,0.72,0,1);
  max-height: 70vh;
  overflow-y: auto;
}

.sheet-handle {
  width: 36px;
  height: 4px;
  border-radius: 2px;
  background: var(--ink-muted);
  margin: 0 auto var(--space-md);
}

.sheet-action {
  display: flex;
  align-items: center;
  gap: var(--space-md);
  width: 100%;
  padding: 14px var(--space-lg);
  border: none;
  background: none;
  color: var(--ink);
  font-size: 16px;
  text-align: left;
  cursor: pointer;
  transition: background 0.1s;
}

.sheet-action:hover {
  background: var(--surface-hover);
}

.sheet-action.is-primary {
  color: var(--accent);
  font-weight: 600;
}

.sheet-action.is-danger {
  color: var(--danger);
}

.sheet-action.is-danger:hover {
  background: var(--danger-soft);
}

.sheet-action-icon {
  width: 24px;
  text-align: center;
  font-size: 18px;
}

.sheet-divider {
  height: 1px;
  background: rgba(0,0,0,0.06);
  margin: var(--space-xs) var(--space-lg);
}

/* Group captions in the deck menu (Study / Edit / Manage) */
.sheet-group-label {
  padding: var(--space-sm) var(--space-lg) 2px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: var(--ink-secondary);
}

[data-theme="dark"] .sheet-divider {
  background: rgba(255,255,255,0.06);
}

/* ---------- Study Session ---------- */
.study-session {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  animation: fadeIn 0.2s ease;
}

/* Study header with counter, undo, help */
.study-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-sm) var(--space-md);
  border-bottom: 1px solid rgba(0,0,0,0.04);
  min-height: 48px;
}

[data-theme="dark"] .study-header {
  border-bottom-color: rgba(255,255,255,0.04);
}

.study-header-counter {
  font-size: 14px;
  font-weight: 600;
  color: var(--ink-secondary);
  display: flex;
  align-items: center;
  gap: var(--space-xs);
}

.study-header-cap-badge {
  font-size: 11px;
  font-weight: 600;
  color: var(--accent);
  background: var(--accent-soft);
  padding: 2px 7px;
  border-radius: 999px;
  letter-spacing: 0.2px;
}

.study-header-actions {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

.study-undo-btn,
.study-help-btn,
.study-exit-btn {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-md);
  border: none;
  background: transparent;
  color: var(--ink-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  transition: background 0.15s, color 0.15s;
}

.study-undo-btn:hover,
.study-help-btn:hover,
.study-exit-btn:hover {
  background: var(--accent-soft);
  color: var(--accent);
}

.study-undo-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.study-undo-btn:disabled:hover {
  background: transparent;
  color: var(--ink-secondary);
}

/* Context bar below header */
.study-context-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-sm) var(--space-md);
  font-size: 12px;
  color: var(--ink-muted);
  border-bottom: 1px solid rgba(0,0,0,0.04);
}

[data-theme="dark"] .study-context-bar {
  border-bottom-color: rgba(255,255,255,0.04);
}

.study-context-deck {
  max-width: 40%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.study-context-type {
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.study-context-status {
  font-weight: 500;
}

/* ---------- Card Flip ---------- */
.study-card-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--space-md);
  perspective: 1000px;
  overflow: hidden;
}

.study-card-wrap {
  width: 100%;
  max-width: 560px;
  aspect-ratio: 3/4;
  max-height: 70vh;
  max-height: min(70dvh, 100%);   /* never taller than the space the card area really has (landscape phones) */
}

.study-card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.5s cubic-bezier(0.4, 0, 0.2, 1);
}

.study-card-inner.is-flipped {
  transform: rotateY(180deg);
}

.study-card-front,
.study-card-back {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  background: var(--surface-raised);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: var(--space-xl);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.study-card-back {
  transform: rotateY(180deg);
}

.study-card-content {
  flex: 1;
  display: flex;
  flex-direction: column;       /* text block, then the note area underneath it */
  align-items: stretch;
  justify-content: center;
  justify-content: safe center; /* centred, but a long card scrolls from its top instead of cutting it off */
  font-size: 18px;
  line-height: 1.7;
  color: var(--ink);
  word-break: break-word;
}

.study-card-content .katex-display {
  margin: var(--space-md) 0;
}

/* The card text is ONE block, so cloze highlights and inline markup stay in the same line of text */
.study-card-text {
  width: fit-content;
  max-width: 100%;
  min-width: 0;
  margin-inline: auto;
  overflow-x: auto;             /* a very wide formula scrolls inside the card instead of being cut off */
}

/* Hint area inside card */
.study-hint-area {
  margin-top: var(--space-md);
  padding: var(--space-md);
  background: var(--accent-soft);
  border-radius: var(--radius-md);
  border-left: 3px solid var(--accent);
  animation: fadeIn 0.2s ease;
}

.study-hint-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--accent);
  margin-bottom: var(--space-xs);
}

.study-hint-text {
  font-size: 15px;
  color: var(--ink-secondary);
  line-height: 1.6;
}

.study-hint-empty {
  font-size: 14px;
  color: var(--ink-muted);
  font-style: italic;
}

/* Formula card extras */
.formula-extras {
  margin-top: var(--space-lg);
  padding-top: var(--space-lg);
  border-top: 1px solid rgba(0,0,0,0.06);
}

[data-theme="dark"] .formula-extras {
  border-top-color: rgba(255,255,255,0.06);
}

.formula-extra-block {
  margin-bottom: var(--space-md);
}

.formula-extra-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--ink-muted);
  margin-bottom: var(--space-xs);
}

.formula-extra-value {
  font-size: 15px;
  color: var(--ink-secondary);
  line-height: 1.6;
}

/* Card transitions */
.study-card-wrap.is-exiting .study-card-inner {
  animation: cardExit 0.15s ease forwards;
}

.study-card-wrap.is-exiting-leech .study-card-inner {
  animation: cardBanish 0.6s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

.study-card-wrap.is-entering .study-card-inner {
  animation: cardEnter 0.2s ease forwards;
}

/* ---------- Study Controls ---------- */
.study-controls {
  padding: var(--space-md);
  padding-bottom: calc(var(--space-md) + env(safe-area-inset-bottom));
}

/* Pre-flip action buttons */
.study-action-bar {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-sm);
  margin-bottom: var(--space-sm);
}

.study-action-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-xs);
  padding: 14px;
  border-radius: var(--radius-md);
  border: none;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.12s, background 0.15s;
  min-height: 48px;
}

.study-action-btn:active {
  transform: scale(0.97);
}

.study-action-btn.is-explain {
  background: var(--surface-raised);
  color: var(--ink);
  border: 1.5px solid var(--sand);
}

.study-action-btn.is-explain:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.study-action-btn.is-flip {
  background: var(--accent);
  color: white;
}

.study-action-btn.is-flip:hover {
  background: var(--accent-hover);
}

/* Reveal button (legacy, kept for compatibility) */
.study-reveal-btn {
  width: 100%;
  padding: 16px;
  border-radius: var(--radius-md);
  border: none;
  background: var(--accent);
  color: white;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.12s, background 0.15s;
}

.study-reveal-btn:hover {
  background: var(--accent-hover);
}

.study-reveal-btn:active {
  transform: scale(0.98);
}

/* Grade buttons */
.study-grade-bar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--space-sm);
}

.study-grade-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 12px 4px;
  border-radius: var(--radius-md);
  border: none;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s, box-shadow 0.1s;
  min-height: 64px;
}

.study-grade-btn:active {
  transform: scale(0.96);
}

.study-grade-btn[data-grade="again"] {
  background: var(--danger-soft);
  color: var(--danger);
}

.study-grade-btn[data-grade="hard"] {
  background: var(--warning-soft);
  color: #B7791F;
}

.study-grade-btn[data-grade="good"] {
  background: var(--accent-soft);
  color: var(--accent);
}

.study-grade-btn[data-grade="easy"] {
  background: rgba(46,125,50,0.14);
  color: var(--accent-hover);
}

.study-grade-hint {
  font-size: 11px;
  font-weight: 500;
  opacity: 0.7;
  margin-top: 2px;
}

/* Gesture hints */
.study-gesture-hints {
  text-align: center;
  font-size: 12px;
  color: var(--ink-muted);
  margin-top: var(--space-sm);
  line-height: 1.5;
}

/* Keyboard shortcuts overlay */
.study-shortcuts-overlay {
  position: fixed;
  inset: 0;
  z-index: 80;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-lg);
  animation: fadeIn 0.2s ease;
}

.study-shortcuts-card {
  width: 100%;
  max-width: 360px;
  background: var(--surface-raised);
  border-radius: var(--radius-lg);
  padding: var(--space-lg);
  box-shadow: var(--shadow-lg);
}

.study-shortcuts-title {
  font-family: var(--font-display);
  font-size: 18px;
  font-weight: 700;
  color: var(--ink);
  margin-bottom: var(--space-md);
  text-align: center;
}

.study-shortcuts-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}

.study-shortcut-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  color: var(--ink-secondary);
}

.study-shortcut-key {
  font-family: var(--font-mono);
  font-size: 12px;
  background: var(--bg);
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  color: var(--ink);
  border: 1px solid var(--sand);
}

.study-shortcuts-close {
  width: 100%;
  margin-top: var(--space-md);
  padding: 12px;
  border-radius: var(--radius-md);
  border: none;
  background: var(--accent);
  color: white;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

/* ---------- Teach It Sheet ---------- */
.teach-it-sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 70;
  background: var(--surface);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  padding: var(--space-lg);
  padding-bottom: calc(var(--space-lg) + env(safe-area-inset-bottom));
  box-shadow: var(--shadow-lg);
  animation: slideUp 0.3s cubic-bezier(0.32,0.72,0,1);
}

.teach-it-title {
  font-family: var(--font-display);
  font-size: 18px;
  font-weight: 700;
  margin-bottom: var(--space-xs);
  color: var(--ink);
}

.teach-it-sub {
  font-size: 14px;
  color: var(--ink-secondary);
  margin-bottom: var(--space-md);
}

.teach-it-textarea {
  width: 100%;
  min-height: 100px;
  padding: var(--space-md);
  border-radius: var(--radius-md);
  border: 1.5px solid var(--sand);
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
  font-family: var(--font-sans);
  resize: vertical;
  margin-bottom: var(--space-md);
}

.teach-it-textarea:focus {
  border-color: var(--accent);
  outline: none;
}

.teach-it-actions {
  display: flex;
  gap: var(--space-md);
}

.teach-it-actions button {
  flex: 1;
  padding: 12px;
  border-radius: var(--radius-md);
  border: none;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s;
}

.teach-it-skip {
  background: var(--surface-hover);
  color: var(--ink-secondary);
}

.teach-it-continue {
  background: var(--accent);
  color: white;
}

.teach-it-actions button:active {
  transform: scale(0.97);
}

/* ---------- Study Mode Teaching Note & On-Demand Action ---------- */
.study-card-note-area {
  margin-top: var(--space-md);
}

.study-teaching-note {
  margin-top: var(--space-sm);
  padding: 10px 14px;
  background: var(--surface-hover);
  border-radius: var(--radius-md);
  border-left: 3px solid var(--accent);
  text-align: left;
}

.study-teaching-note-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}

.study-teaching-note-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--ink-muted);
}

.study-teaching-note-time {
  font-size: 11px;
  color: var(--ink-muted);
}

.study-teaching-note-text {
  font-size: 13px;
  color: var(--ink-secondary);
  line-height: 1.5;
  font-style: italic;
}

.study-explain-back-btn {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  padding: 6px 0;
  margin-top: var(--space-xs);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: opacity 0.15s;
}

.study-explain-back-btn:hover {
  opacity: 0.8;
}

/* ---------- Leech View Teaching Note ---------- */
.leech-teaching-note {
  margin: 6px 0 12px 0;
  padding: 10px 12px;
  background: var(--surface-hover);
  border-radius: var(--radius-sm);
  border-left: 3px solid var(--accent);
  font-size: 13px;
  color: var(--ink-secondary);
  line-height: 1.5;
}

/* ---------- Card Detail Explanation Block ---------- */
.card-explanation-block {
  background: var(--surface);
  border-radius: var(--radius-md);
  padding: 14px;
  box-shadow: var(--shadow-sm);
}

.card-explanation-text {
  font-size: 15px;
  color: var(--ink);
  line-height: 1.6;
  margin-bottom: 8px;
}

.card-explanation-time {
  font-size: 12px;
  color: var(--ink-muted);
}

/* ---------- Session Summary ---------- */
.session-summary {
  padding: var(--space-xl);
  text-align: center;
  animation: fadeIn 0.4s ease;
}

.session-summary-ring {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  margin: 0 auto var(--space-lg);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.session-summary-ring svg {
  position: absolute;
  inset: 0;
  transform: rotate(-90deg);
}

.session-summary-ring-bg {
  fill: none;
  stroke: var(--sand);
  stroke-width: 8;
}

.session-summary-ring-fg {
  fill: none;
  stroke: var(--accent);
  stroke-width: 8;
  stroke-linecap: round;
  transition: stroke-dashoffset 0.6s ease;
}

.session-summary-score {
  font-size: 32px;
  font-weight: 700;
  color: var(--ink);
}

.session-summary-label {
  font-size: 14px;
  color: var(--ink-muted);
  margin-bottom: var(--space-xl);
}

.session-summary-sweep {
  font-size: 13px;
  font-weight: 600;
  color: var(--accent);
  margin: -12px 0 var(--space-lg);
  animation: helpFadeUp 0.4s cubic-bezier(0.22, 1, 0.36, 1) both;
}

.session-summary-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--space-md);
  margin-bottom: var(--space-xl);
}

.session-summary-stat {
  background: var(--surface);
  padding: var(--space-md);
  border-radius: var(--radius-md);
}

.session-summary-stat-value {
  font-size: 24px;
  font-weight: 700;
  color: var(--ink);
}

.session-summary-stat-label {
  font-size: 12px;
  color: var(--ink-muted);
  margin-top: 2px;
}

.session-summary-btn {
  width: 100%;
  max-width: 320px;
  padding: 14px;
  border-radius: var(--radius-md);
  border: none;
  background: var(--accent);
  color: white;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, opacity 0.15s ease;
}

.session-summary-btn-secondary {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--ink-secondary);
}

.session-summary-btn-secondary:hover {
  background: var(--surface-hover);
  color: var(--ink);
}

/* ---------- Toast ---------- */
.toast-container {
  position: fixed;
  bottom: calc(var(--space-lg) + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  width: max-content;                       /* left:50% limited shrink-to-fit to half the screen (180px on a phone) */
  max-width: calc(100vw - 2 * var(--space-md));
  z-index: 200;
  pointer-events: none;
}

.toast {
  background: var(--surface-raised);
  color: var(--ink);
  padding: 10px 18px;
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
  font-size: 14px;
  font-weight: 500;
  animation: toastIn 0.3s ease;
  pointer-events: none;
  text-align: center;
}

.toast.is-leaving {
  animation: toastOut 0.3s ease forwards;
}

@keyframes toastIn {
  from {
    opacity: 0;
    transform: translateY(8px) scale(0.96);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes toastOut {
  from {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
  to {
    opacity: 0;
    transform: translateY(8px) scale(0.96);
  }
}

/* ---------- Update Prompt ---------- */
.update-prompt {
  display: flex;
  align-items: center;
  gap: var(--space-md);
  padding: 10px 16px;
  background: var(--surface-raised);
  color: var(--ink);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--accent-soft);
  max-width: min(92vw, 440px);
  pointer-events: auto;          /* it holds Refresh / Later; plain toasts let taps through to the page */
}

.update-prompt-text {
  font-size: 14px;
  font-weight: 500;
  line-height: 1.4;
  color: var(--ink);
}

.update-prompt-actions {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  margin-left: auto;
  flex-shrink: 0;
}

.update-prompt-btn {
  background: var(--accent);
  color: #FFFFFF;
  border: none;
  border-radius: var(--radius-sm);
  padding: 6px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s ease, opacity 0.15s ease;
}

.update-prompt-btn:hover {
  background: var(--accent-hover);
}

.update-prompt-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.update-prompt-dismiss {
  background: transparent;
  color: var(--ink-muted);
  border: none;
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: color 0.15s ease;
}

.update-prompt-dismiss:hover {
  color: var(--ink);
}

/* ---------- Card Preview Overlay ---------- */
.card-preview-overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-lg);
  animation: fadeIn 0.2s ease;
}

.card-preview-card {
  width: 100%;
  max-width: 480px;
  max-height: 80vh;
  background: var(--surface-raised);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.card-preview-body {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-xl);
}

.card-preview-actions {
  display: flex;
  gap: var(--space-md);
  padding: var(--space-md) var(--space-lg);
  border-top: 1px solid rgba(0,0,0,0.06);
}

.card-preview-actions button {
  flex: 1;
  padding: 12px;
  border-radius: var(--radius-md);
  border: none;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

.card-preview-close {
  background: var(--surface-hover);
  color: var(--ink-secondary);
}

.card-preview-study {
  background: var(--accent);
  color: white;
}

/* ---------- Misc Utilities ---------- */
.empty-state {
  text-align: center;
  padding: var(--space-2xl) var(--space-md);
  color: var(--ink-muted);
}

.empty-state-icon {
  font-size: 48px;
  margin-bottom: var(--space-md);
  opacity: 0.5;
}

.empty-state-title {
  font-family: var(--font-display);
  font-size: 20px;
  font-weight: 700;
  color: var(--ink);
  margin-bottom: var(--space-sm);
}

.empty-state-text {
  font-size: 15px;
  max-width: 320px;
  margin: 0 auto var(--space-lg);
  line-height: 1.6;
}

.empty-state-actions {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  align-items: center;
}

.btn-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-xs);
  padding: 12px 20px;
  border-radius: var(--radius-md);
  border: none;
  background: var(--accent);
  color: white;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s, background 0.15s;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-primary:active {
  transform: scale(0.97);
}

.btn-secondary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-xs);
  padding: 12px 20px;
  border-radius: var(--radius-md);
  border: 1.5px solid var(--sand);
  background: var(--surface);
  color: var(--ink);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: transform 0.1s, border-color 0.15s;
}

.btn-secondary:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.btn-secondary:active {
  transform: scale(0.97);
}

/* Font selector in settings */
.font-selector {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.font-option {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  background: var(--surface);
  border-radius: var(--radius-md);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: background 0.1s;
}

.font-option:hover {
  background: var(--surface-hover);
}

.font-option input[type="radio"] {
  flex-shrink: 0;
}

/* Form elements */
input, textarea, select {
  font-family: var(--font-body, var(--font-sans));
}

/* Scrollbar styling */
::-webkit-scrollbar {
  width: 6px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: var(--sand);
  border-radius: 3px;
}

/* Short screens (landscape phones): overlays scroll instead of hiding their buttons, and the summary compacts */
.study-shortcuts-overlay { align-items: safe center; overflow-y: auto; }
.study-session { overflow-y: auto; }
@media (max-height: 480px) {
  .session-summary { padding-block: var(--space-md); }
  .session-summary-ring { width: 84px; height: 84px; margin-bottom: var(--space-md); }
  .session-summary-ring svg { width: 100%; height: 100%; }   /* the SVG has a viewBox: let it scale with the ring */
  .session-summary-label { margin-bottom: var(--space-md); }
  .session-summary-stats { margin-bottom: var(--space-md); }
}

/* Landscape phones (a touch screen under 480 px tall): give the card the room.
   The line of arrows under the buttons only repeats the buttons above it (and the swipes are in Help),
   and the counter row and the deck / type / status row share one row instead of taking two. */
@media (pointer: coarse) and (max-height: 480px) {
  .study-gesture-hints { display: none; }

  .study-session {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    grid-template-rows: auto minmax(0, 1fr) auto;
  }
  .study-header { display: contents; }          /* its counter and buttons become cells of the row */
  .study-header-counter { grid-area: 1 / 1; padding: var(--space-xs) 0 var(--space-xs) var(--space-md); white-space: nowrap; }
  .study-context-bar {
    grid-area: 1 / 2;
    min-width: 0;
    justify-content: flex-end;
    gap: var(--space-sm);
    padding: 0 var(--space-sm);
    border-bottom: 0;
  }
  .study-context-deck { max-width: 60%; }
  .study-header-actions { grid-area: 1 / 3; padding: var(--space-xs) var(--space-md) var(--space-xs) 0; }
  .study-card-area { grid-area: 2 / 1 / 3 / 4; box-shadow: inset 0 1px 0 rgba(0,0,0,0.04); }
  .study-controls { grid-area: 3 / 1 / 4 / 4; }
  [data-theme="dark"] .study-card-area { box-shadow: inset 0 1px 0 rgba(255,255,255,0.04); }
}

/* KaTeX display math: scroll sideways inside its own box rather than widening the page */
.katex-display { max-width: 100%; overflow-x: auto; overflow-y: hidden; }

.deck-list > .empty-state { grid-column: 1 / -1; }

/* Responsive */
@media (min-width: 768px) {
  .study-card-wrap {
    max-width: 600px;
  }
  .deck-list {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
  }
}

/* ---------- Manual JSON Import ---------- */
.manual-import-view {
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
}

.manual-import-body {
  padding: var(--space-md);
  max-width: 640px;
  margin: 0 auto;
}

.manual-import-alert {
  background: var(--surface);
  border-radius: var(--radius-md);
  padding: var(--space-lg);
  margin-bottom: var(--space-lg);
  box-shadow: var(--shadow-sm);
  display: flex;
  gap: var(--space-md);
}

.manual-import-alert-icon {
  font-size: 28px;
  flex-shrink: 0;
}

.manual-import-steps {
  margin: var(--space-sm) 0 0 var(--space-md);
  padding-left: var(--space-md);
  color: var(--ink-secondary);
  font-size: 14px;
  line-height: 1.8;
}

.manual-import-section {
  margin-bottom: var(--space-lg);
}

.manual-import-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-sm);
  font-size: 14px;
  font-weight: 600;
  color: var(--ink);
}

.manual-import-copy {
  padding: 4px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--sand);
  background: var(--surface);
  color: var(--accent);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.1s;
}

.manual-import-copy:hover {
  background: var(--accent-soft);
}

.manual-import-prompt,
.manual-import-json {
  width: 100%;
  min-height: 160px;
  padding: var(--space-md);
  border-radius: var(--radius-md);
  border: 1.5px solid var(--sand);
  background: var(--surface);
  color: var(--ink);
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.6;
  resize: vertical;
}

.manual-import-prompt {
  min-height: 200px;
  background: var(--bg);
  color: var(--ink-secondary);
}

.manual-import-json {
  min-height: 180px;
}

.manual-import-json:focus {
  border-color: var(--accent);
  outline: none;
}

.manual-import-hint {
  margin-top: var(--space-sm);
  font-size: 13px;
  color: var(--ink-muted);
  min-height: 20px;
}

.manual-import-hint.is-valid {
  color: var(--moss);
}

.manual-import-hint.is-error {
  color: var(--danger);
}


/* =========================================================================
   Spatial Learning Map — zoom levels, landmarks, toolbar, spatial review
   ========================================================================= */

.map-root {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--map-bg, #14181C);
}

.territory-map-canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: grab;
}
.territory-map-canvas:active { cursor: grabbing; }

.map-overlay-list-btn {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 20;
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid var(--border, rgba(255,255,255,0.12));
  background: var(--surface, rgba(30,34,38,0.92));
  color: var(--ink, #EDEFF1);
  font-size: 13px;
  font-weight: 600;
  backdrop-filter: blur(8px);
  cursor: pointer;
}
.map-overlay-list-btn:hover { background: var(--surface-2, rgba(40,46,52,0.95)); }

.map-breadcrumb {
  position: absolute;
  top: 12px;
  left: 12px;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 10px;
  border-radius: 999px;
  background: var(--surface, rgba(30,34,38,0.88));
  backdrop-filter: blur(8px);
  font-size: 12px;
  color: var(--ink-muted, rgba(237,239,241,0.6));
  max-width: calc(100% - 120px);
  overflow: hidden;
}
.map-crumb {
  background: none;
  border: none;
  color: inherit;
  font: inherit;
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 4px;
  white-space: nowrap;
}
.map-crumb:hover { color: var(--ink, #EDEFF1); background: rgba(255,255,255,0.06); }
.map-crumb-sep { opacity: 0.4; margin: 0 2px; }

.map-floating-toolbar {
  position: absolute;
  bottom: max(16px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  z-index: 20;
  display: flex;
  gap: 6px;
  padding: 6px;
  border-radius: 999px;
  background: var(--surface, rgba(30,34,38,0.92));
  border: 1px solid var(--border, rgba(255,255,255,0.1));
  backdrop-filter: blur(12px);
  box-shadow: 0 8px 24px rgba(0,0,0,0.25);
}
.map-tool-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: transparent;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--ink, #EDEFF1);
}
.map-tool-btn:hover { background: rgba(255,255,255,0.08); }
.map-tool-btn.is-active {
  background: rgba(46,125,50,0.25);
  box-shadow: inset 0 0 0 1.5px rgba(46,125,50,0.5);
}
.map-tool-btn.is-primary {
  background: var(--accent, #2E7D32);
  color: #fff;
  width: auto;
  padding: 0 14px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
}

.map-path-panel {
  position: absolute;
  top: 56px;
  right: 12px;
  z-index: 18;
  width: 180px;
  max-height: 40vh;
  overflow-y: auto;
  padding: 10px;
  border-radius: 12px;
  background: var(--surface, rgba(30,34,38,0.92));
  border: 1px solid var(--border, rgba(255,255,255,0.1));
  backdrop-filter: blur(8px);
}
.map-path-panel.is-collapsed { display: none; }
.map-path-panel-title {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--ink-muted, rgba(237,239,241,0.5));
  margin-bottom: 8px;
}
.map-path-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: 8px 10px;
  margin-bottom: 4px;
  border: none;
  border-radius: 8px;
  background: rgba(255,255,255,0.04);
  color: var(--ink, #EDEFF1);
  font-size: 13px;
  cursor: pointer;
  text-align: left;
}
.map-path-item:hover { background: rgba(255,255,255,0.08); }
.map-path-count {
  font-size: 11px;
  opacity: 0.5;
}

/* Card detail (L3) */
.map-card-detail {
  position: absolute;
  left: 50%;
  bottom: 80px;
  transform: translateX(-50%);
  z-index: 25;
  width: min(420px, calc(100% - 24px));
  max-height: 55vh;
  overflow-y: auto;
  padding: 16px 18px 20px;
  border-radius: 16px;
  background: var(--surface, rgba(28,32,36,0.96));
  border: 1px solid var(--border, rgba(255,255,255,0.12));
  box-shadow: 0 16px 48px rgba(0,0,0,0.35);
  backdrop-filter: blur(16px);
  color: var(--ink, #EDEFF1);
}
.map-detail-close {
  background: none;
  border: none;
  color: var(--ink-muted);
  font-size: 13px;
  cursor: pointer;
  margin-bottom: 10px;
  padding: 0;
}
.map-detail-front {
  font-size: 17px;
  font-weight: 600;
  line-height: 1.4;
  margin-bottom: 10px;
}
.map-detail-back {
  font-size: 14px;
  line-height: 1.5;
  opacity: 0.85;
  margin-bottom: 12px;
}
.map-detail-formula {
  font-size: 15px;
  margin-bottom: 12px;
  opacity: 0.9;
}
.map-detail-rels {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 14px;
}
.map-rel-chip {
  font-size: 11px;
  padding: 4px 8px;
  border-radius: 999px;
  background: rgba(46,125,50,0.15);
  color: var(--ink-muted);
}
.map-detail-study {
  width: 100%;
}

/* Spatial review overlay */
.spatial-review-overlay {
  position: absolute;
  inset: 0;
  z-index: 40;
  pointer-events: none;
}
.spatial-review-overlay > * { pointer-events: auto; }

.spatial-review-counter {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  padding: 6px 14px;
  border-radius: 999px;
  background: rgba(0,0,0,0.55);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  backdrop-filter: blur(6px);
}
.spatial-review-exit {
  position: absolute;
  top: 12px;
  right: 12px;
  padding: 8px 14px;
  border-radius: 999px;
  border: none;
  background: rgba(0,0,0,0.55);
  color: #fff;
  font-size: 13px;
  cursor: pointer;
}

.spatial-review-card-host {
  position: absolute;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  width: min(400px, calc(100% - 24px));
}
.spatial-review-card {
  padding: 20px 18px 16px;
  border-radius: 16px;
  background: var(--surface, rgba(28,32,36,0.96));
  border: 1px solid var(--border, rgba(255,255,255,0.14));
  box-shadow: 0 20px 60px rgba(0,0,0,0.4);
  backdrop-filter: blur(20px);
  color: var(--ink, #EDEFF1);
}
.spatial-review-front {
  font-size: 18px;
  font-weight: 600;
  line-height: 1.4;
  margin-bottom: 12px;
}
.spatial-review-back {
  font-size: 15px;
  line-height: 1.5;
  opacity: 0.9;
  margin-bottom: 14px;
  padding-top: 12px;
  border-top: 1px solid rgba(255,255,255,0.08);
}
.spatial-review-back.is-hidden { display: none; }
.spatial-not-due {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 999px;
  background: rgba(200,160,40,0.2);
  color: #e0c060;
  margin-bottom: 8px;
}
.spatial-flip-btn {
  width: 100%;
  padding: 12px;
  border: none;
  border-radius: 10px;
  background: var(--accent, #2E7D32);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.spatial-grade-bar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}
.spatial-grade-bar button {
  padding: 10px 4px;
  border: none;
  border-radius: 10px;
  background: rgba(255,255,255,0.07);
  color: var(--ink, #EDEFF1);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  line-height: 1.3;
}
.spatial-grade-bar button small {
  font-weight: 400;
  opacity: 0.55;
  font-size: 10px;
}
.spatial-grade-bar button:nth-child(1) { background: rgba(200,60,50,0.2); }
.spatial-grade-bar button:nth-child(2) { background: rgba(200,140,40,0.18); }
.spatial-grade-bar button:nth-child(3) { background: rgba(46,125,50,0.22); }
.spatial-grade-bar button:nth-child(4) { background: rgba(40,140,180,0.2); }

.spatial-summary {
  text-align: center;
  padding: 28px 20px;
}
.spatial-summary-score {
  font-size: 42px;
  font-weight: 700;
  color: var(--accent, #2E7D32);
}
.spatial-summary-label {
  font-size: 13px;
  opacity: 0.6;
  margin-bottom: 16px;
}
.spatial-summary-stats {
  display: flex;
  justify-content: center;
  gap: 16px;
  font-size: 13px;
  margin-bottom: 18px;
  opacity: 0.8;
}

/* Light theme adjustments */
:root:not([data-theme="dark"]) .map-overlay-list-btn,
:root:not([data-theme="dark"]) .map-breadcrumb,
:root:not([data-theme="dark"]) .map-floating-toolbar,
:root:not([data-theme="dark"]) .map-path-panel,
:root:not([data-theme="dark"]) .map-card-detail,
:root:not([data-theme="dark"]) .spatial-review-card {
  background: rgba(255,255,255,0.92);
  color: var(--ink, #1A1F1B);
  border-color: rgba(0,0,0,0.08);
}

/* ---- Help view ---- */
.help-view {
  max-width: 560px;
  margin: 0 auto;
  padding-bottom: calc(var(--space-2xl) + 24px);
  animation: helpFadeUp 0.45s cubic-bezier(0.22, 1, 0.36, 1) both;
}

@keyframes helpFadeUp {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: translateY(0); }
}

.help-hero {
  padding: var(--space-md) var(--space-md) var(--space-sm);
}

#help-watch {
  padding: 0 var(--space-md) var(--space-md);
  animation: helpFadeUp 0.5s 0.1s cubic-bezier(0.22, 1, 0.36, 1) both;
}

.help-watch-stage {
  position: relative;
  width: 100%;
  aspect-ratio: 8 / 5;
  border-radius: var(--radius-md, 12px);
  overflow: hidden;
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}

[data-theme="dark"] .help-watch-stage {
  background: #161616;
}

.help-watch-stage canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.help-watch-play-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: none;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: rgba(0, 0, 0, 0.45);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
}

.help-watch-stage.is-playing .help-watch-play-overlay {
  background: transparent;
}

.help-watch-play-icon {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: var(--accent, #2E7D32);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  padding-left: 4px; /* optically center the triangle */
}

.help-watch-skip {
  position: absolute;
  right: 10px;
  bottom: 10px;
  border: none;
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 600;
  color: #fff;
  background: rgba(0, 0, 0, 0.5);
  cursor: pointer;
}

.help-hero-kicker {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--accent, #2E7D32);
  margin-bottom: 8px;
  animation: helpFadeUp 0.5s 0.05s cubic-bezier(0.22, 1, 0.36, 1) both;
}

.help-hero-title {
  font-family: var(--font-display);
  font-size: 1.55rem;
  line-height: 1.25;
  font-weight: 700;
  color: var(--ink);
  margin: 0 0 12px;
  animation: helpFadeUp 0.55s 0.08s cubic-bezier(0.22, 1, 0.36, 1) both;
}

.help-hero-lead {
  font-size: 14px;
  line-height: 1.65;
  color: var(--ink-secondary);
  margin: 0 0 10px;
  animation: helpFadeUp 0.55s 0.12s cubic-bezier(0.22, 1, 0.36, 1) both;
}

.help-hero-lead-soft {
  opacity: 0.92;
  animation-delay: 0.16s;
}

.help-toc {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 4px var(--space-md) var(--space-md);
  animation: helpFadeUp 0.5s 0.18s cubic-bezier(0.22, 1, 0.36, 1) both;
}

.help-toc a {
  font-size: 12px;
  font-weight: 600;
  text-decoration: none;
  color: var(--ink);
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border, rgba(0,0,0,0.08));
  box-shadow: var(--shadow-sm);
  transition: transform 0.15s ease, background 0.15s ease, border-color 0.15s ease;
}

.help-toc a:hover,
.help-toc a:focus-visible {
  transform: translateY(-1px);
  border-color: var(--accent, #2E7D32);
  outline: none;
}

.help-section {
  padding: var(--space-sm) var(--space-md) var(--space-md);
  scroll-margin-top: 72px;
}

.help-section-title {
  font-family: var(--font-display);
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--ink);
  margin: 0 0 12px;
}

.help-cards {
  display: grid;
  gap: 10px;
}

@media (min-width: 520px) {
  .help-cards {
    grid-template-columns: 1fr 1fr;
  }
}

.help-card {
  background: var(--surface);
  border-radius: var(--radius-md, 12px);
  padding: 14px 14px 16px;
  box-shadow: var(--shadow-sm);
  border: 1px solid transparent;
  transition: transform 0.2s ease, box-shadow 0.2s ease, border-color 0.2s ease;
  animation: helpFadeUp 0.5s cubic-bezier(0.22, 1, 0.36, 1) both;
}

.help-card:nth-child(1) { animation-delay: 0.1s; }
.help-card:nth-child(2) { animation-delay: 0.16s; }

.help-card:hover {
  transform: translateY(-2px);
  border-color: rgba(46, 125, 50, 0.25);
  box-shadow: var(--shadow-md, 0 8px 24px rgba(0,0,0,0.08));
}

.help-card-icon {
  font-size: 1.35rem;
  margin-bottom: 6px;
}

.help-card h3 {
  font-size: 14px;
  font-weight: 700;
  margin: 0 0 8px;
  color: var(--ink);
}

.help-card p {
  font-size: 13px;
  line-height: 1.55;
  color: var(--ink-secondary);
  margin: 0 0 8px;
}

.help-card p:last-child { margin-bottom: 0; }

.help-philosophy-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 8px;
}

.help-philosophy-list li {
  font-size: 13px;
  line-height: 1.55;
  color: var(--ink-secondary);
  background: var(--surface);
  border-radius: var(--radius-md, 12px);
  padding: 12px 14px;
  box-shadow: var(--shadow-sm);
  border-left: 3px solid var(--accent, #2E7D32);
  animation: helpFadeUp 0.45s cubic-bezier(0.22, 1, 0.36, 1) both;
}

.help-philosophy-list li:nth-child(1) { animation-delay: 0.05s; }
.help-philosophy-list li:nth-child(2) { animation-delay: 0.08s; }
.help-philosophy-list li:nth-child(3) { animation-delay: 0.11s; }
.help-philosophy-list li:nth-child(4) { animation-delay: 0.14s; }
.help-philosophy-list li:nth-child(5) { animation-delay: 0.17s; }
.help-philosophy-list li:nth-child(6) { animation-delay: 0.2s; }

.help-philosophy-list strong {
  color: var(--ink);
}

.help-order-list {
  list-style: none;
  counter-reset: help-order;
  margin: 12px 0 0;
  padding: 0;
  display: grid;
  gap: 8px;
}

.help-order-list li {
  counter-increment: help-order;
  position: relative;
  font-size: 13px;
  line-height: 1.55;
  color: var(--ink-secondary);
  background: var(--surface);
  border-radius: var(--radius-md, 12px);
  padding: 12px 14px 12px 40px;
  box-shadow: var(--shadow-sm);
  animation: helpFadeUp 0.45s cubic-bezier(0.22, 1, 0.36, 1) both;
}

.help-order-list li::before {
  content: counter(help-order);
  position: absolute;
  left: 12px;
  top: 12px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--accent, #2E7D32);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}

.help-order-list li:nth-child(1) { animation-delay: 0.05s; }
.help-order-list li:nth-child(2) { animation-delay: 0.08s; }
.help-order-list li:nth-child(3) { animation-delay: 0.11s; }
.help-order-list li:nth-child(4) { animation-delay: 0.14s; }
.help-order-list li:nth-child(5) { animation-delay: 0.17s; }

.help-order-list strong {
  color: var(--ink);
}

.help-order-close {
  font-size: 13px;
  font-style: italic;
  color: var(--ink-muted);
  margin: 10px 2px 0;
}

.help-accordion {
  display: grid;
  gap: 8px;
}

.help-details {
  background: var(--surface);
  border-radius: var(--radius-md, 12px);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  border: 1px solid transparent;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
  animation: helpFadeUp 0.4s cubic-bezier(0.22, 1, 0.36, 1) both;
  animation-delay: calc(0.04s * var(--help-i, 0));
}

.help-details[open] {
  border-color: rgba(46, 125, 50, 0.22);
  box-shadow: var(--shadow-md, 0 6px 18px rgba(0,0,0,0.06));
}

.help-summary {
  list-style: none;
  cursor: pointer;
  padding: 13px 14px;
  font-size: 14px;
  font-weight: 600;
  color: var(--ink);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  user-select: none;
  transition: background 0.15s ease;
}

.help-summary::-webkit-details-marker { display: none; }

.help-summary::after {
  content: '';
  width: 7px;
  height: 7px;
  border-right: 2px solid var(--ink-muted, #888);
  border-bottom: 2px solid var(--ink-muted, #888);
  transform: rotate(45deg);
  transition: transform 0.2s ease;
  flex-shrink: 0;
}

.help-details[open] > .help-summary::after {
  transform: rotate(225deg);
}

.help-summary:hover {
  background: rgba(0, 0, 0, 0.03);
}

[data-theme="dark"] .help-summary:hover {
  background: rgba(255, 255, 255, 0.04);
}

.help-details-body {
  padding: 0 14px 14px;
  font-size: 13px;
  line-height: 1.6;
  color: var(--ink-secondary);
  animation: helpBodyIn 0.25s ease both;
}

@keyframes helpBodyIn {
  from { opacity: 0; transform: translateY(-4px); }
  to   { opacity: 1; transform: translateY(0); }
}

.help-details-body p { margin: 0 0 10px; }
.help-details-body p:last-child { margin-bottom: 0; }

.help-details-body ul,
.help-details-body ol {
  margin: 0 0 10px;
  padding-left: 1.2em;
}

.help-details-body li { margin-bottom: 4px; }

.help-details-body em {
  color: var(--ink);
  font-style: normal;
  font-weight: 600;
}

.help-faq-item .help-summary {
  font-size: 13.5px;
}

.help-closer {
  margin: var(--space-md) var(--space-md) 0;
  padding: 18px 16px 20px;
  border-radius: var(--radius-md, 12px);
  background: linear-gradient(
    145deg,
    rgba(46, 125, 50, 0.12),
    var(--surface) 55%
  );
  border: 1px solid rgba(46, 125, 50, 0.2);
  animation: helpFadeUp 0.5s 0.1s cubic-bezier(0.22, 1, 0.36, 1) both;
}

.help-closer p {
  font-size: 14px;
  line-height: 1.6;
  color: var(--ink-secondary);
  margin: 0 0 10px;
}

.help-closer-em {
  color: var(--ink) !important;
  font-weight: 600;
}

.help-closer-btn {
  width: 100%;
  margin-top: 8px;
}

@media (prefers-reduced-motion: reduce) {
  .help-view,
  .help-hero-kicker,
  .help-hero-title,
  .help-hero-lead,
  .help-toc,
  .help-card,
  .help-philosophy-list li,
  .help-details,
  .help-details-body,
  .help-closer {
    animation: none !important;
  }
  .help-toc a,
  .help-card {
    transition: none;
  }
}

/* ---------- Feature Grid (Encarta-Kids Style, §3.5) ---------- */
.feature-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--space-md);
  padding: 0 var(--space-md);
  margin: 0 0 var(--space-lg);
  max-width: 800px;
}

@media (min-width: 640px) {
  .feature-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

.feature-tile {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
  padding: var(--space-md);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg, 12px);
  cursor: pointer;
  text-align: left;
  user-select: none;
  -webkit-user-select: none;
  transition: transform 0.22s cubic-bezier(0.4, 0, 0.2, 1),
              box-shadow 0.22s cubic-bezier(0.4, 0, 0.2, 1),
              border-color 0.2s ease,
              background 0.2s ease;
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  outline: none;
}

.feature-tile:hover,
.feature-tile:focus-visible,
.feature-tile.is-active {
  transform: translateY(-3px) scale(1.02);
  border-color: rgba(188, 126, 50, 0.5);
  box-shadow: 0 8px 24px rgba(188, 126, 50, 0.18), 0 2px 6px rgba(0, 0, 0, 0.04);
}

[data-theme="dark"] .feature-tile:hover,
[data-theme="dark"] .feature-tile:focus-visible,
[data-theme="dark"] .feature-tile.is-active {
  border-color: rgba(74, 160, 90, 0.6);
  box-shadow: 0 8px 24px rgba(74, 160, 90, 0.25), 0 2px 8px rgba(0, 0, 0, 0.35);
}

.feature-tile-art {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 54px;
  width: 100%;
  margin-bottom: var(--space-xs);
  transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

.feature-tile:hover .feature-tile-art,
.feature-tile:focus-visible .feature-tile-art,
.feature-tile.is-active .feature-tile-art {
  transform: scale(1.05);
}

.feature-tile-content {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.feature-tile-title {
  font-family: var(--font-display);
  font-size: 15px;
  font-weight: 700;
  color: var(--ink);
  line-height: 1.25;
}

.feature-tile-desc {
  font-size: 12px;
  color: var(--ink-muted);
  line-height: 1.35;
}

/* Phones: the four feature tiles become one swipeable row, so the first deck is on the first screen */
@media (max-width: 639px) {
  .feature-grid {
    display: flex;
    gap: var(--space-sm);
    overflow-x: auto;
    scroll-snap-type: x proximity;
    scroll-padding-inline: var(--space-md);   /* snap to the padded edge, not to the screen edge */
    padding-top: 4px;
    padding-bottom: var(--space-sm);
    margin-bottom: var(--space-sm);
    scrollbar-width: none;
  }
  .feature-grid::-webkit-scrollbar { display: none; }
  .feature-tile { flex: 0 0 136px; scroll-snap-align: start; padding: var(--space-sm) var(--space-md); }
  .feature-tile-art { height: 34px; }
  .feature-tile-desc { display: none; }
}

/* Secondary Deck Section Header */
.deck-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--space-md) var(--space-sm);
  margin-top: var(--space-sm);
  max-width: 800px;
}

.deck-section-title {
  font-family: var(--font-display);
  font-size: 16px;
  font-weight: 700;
  color: var(--ink);
  display: flex;
  align-items: center;
  gap: 6px;
}

.deck-section-count {
  font-size: 13px;
  font-weight: 500;
  color: var(--ink-muted);
}

.view-toggle-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm, 6px);
  padding: 4px 10px;
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-secondary);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.view-toggle-btn:hover {
  background: var(--surface-hover, rgba(0, 0, 0, 0.04));
  border-color: var(--ink-muted);
}

/* Mind Map Chooser Items */
.chooser-item-btn {
  transition: background 0.15s, border-color 0.15s, transform 0.12s;
}

.chooser-item-btn:hover,
.chooser-item-btn:focus-visible {
  background: var(--surface-hover, rgba(0, 0, 0, 0.04)) !important;
  border-color: var(--accent) !important;
  transform: translateX(2px);
  outline: none;
}

/* ---------- Document Mind Map: outline list + List/Map button ---------- */

/* Header button that swaps the canvas and the outline. Same 48 px footprint as the back button, so the
   title stays centred; it is hidden (and out of the tab order) until a map is on screen. */
.dmm-view-toggle {
  flex: 0 0 48px;
  width: 48px;
  height: 48px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 0;
  border: none;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  transition: background 0.15s;
}

.dmm-view-toggle:hover {
  background: var(--accent-soft);
}

.dmm-view-toggle:active {
  transform: scale(0.94);
}

.dmm-view-toggle:disabled {
  visibility: hidden;
}

.dmm-view-toggle svg {
  width: 20px;
  height: 20px;
  flex: none;
}

/* "Generated from summary" note: a pill over the canvas, a plain line above the outline. */
.dmm-badge {
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100% - 24px);
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  color: var(--ink-secondary);
  font-size: 11px;
  text-align: center;
}

.dmm-badge--inline {
  position: static;
  transform: none;
  max-width: 100%;
  margin: 0 auto var(--space-md);
}

/* The scroll area is full width (so the scrollbar sits at the window edge); the list inside is a
   readable column, at most 720 px wide, centred on wide screens. */
.dmm-outline {
  height: 100%;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: var(--space-md) max(var(--space-md), calc((100% - 720px) / 2)) calc(var(--space-xl) + env(safe-area-inset-bottom));
  background: var(--bg);
}

.dmm-outline [hidden] {
  display: none !important;
}

.dmm-outline-count {
  margin: 0 0 var(--space-sm);
  padding: 0 var(--space-sm);
  font-size: 13px;
  color: var(--ink-secondary);
}

.dmm-outline-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

/* A branch hangs from a thin line under its parent's dot (same family as the canvas edge colour). */
.dmm-outline-list .dmm-outline-list {
  margin-left: 17px;
  padding-left: var(--space-sm);
  border-left: 2px solid rgba(90, 107, 92, 0.25);
}

[data-theme="dark"] .dmm-outline-list .dmm-outline-list {
  border-left-color: rgba(160, 176, 162, 0.3);
}

.dmm-outline-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 44px;
  padding: 8px 10px 8px 12px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 15px;
  line-height: 1.35;
  text-align: left;
  cursor: pointer;
  transition: background 0.15s;
}

.dmm-outline-row:hover,
.dmm-outline-row[aria-expanded="true"] {
  background: var(--accent-soft);
}

.dmm-outline-row--root {
  font-size: 17px;
  font-weight: 700;
}

/* Same colour per depth as the canvas node, with the same ring the canvas draws around it. */
.dmm-outline-dot {
  flex: none;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  box-shadow: 0 0 0 2px var(--surface-raised);
}

.dmm-outline-title {
  flex: 1 1 auto;
  min-width: 0;
  overflow-wrap: anywhere;
}

.dmm-outline-chev {
  flex: none;
  width: 8px;
  height: 8px;
  margin-right: 4px;
  border-right: 2px solid var(--ink-secondary);
  border-bottom: 2px solid var(--ink-secondary);
  transform: rotate(45deg);
  transition: transform 0.15s;
}

.dmm-outline-row[aria-expanded="true"] .dmm-outline-chev {
  transform: rotate(-135deg);
}

/* Opens under its row, lined up with the title text (12 px padding + 12 px dot + 10 px gap). */
.dmm-outline-detail {
  margin: 2px 0 var(--space-sm) 34px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}

.dmm-outline-detail-text {
  margin: 0 0 10px;
  font-size: 14px;
  line-height: 1.5;
  color: var(--ink-secondary);
}

.dmm-outline-detail-text:last-child {
  margin-bottom: 0;
}

.dmm-outline-actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-sm);
}

.dmm-outline-action {
  min-height: 44px;
  padding: 8px 14px;
  font-size: 13px;
}

/* =========================================================================
   Motion Studio Polish (§3.4) — Loading sway/pulse, thumbnails, post-watch CTA
   ========================================================================= */

/* Rich Generating State */
.ms-generating-widget {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  margin-bottom: 10px;
  box-shadow: var(--shadow-sm);
  animation: msCardFadeIn 0.25s ease-out;
}

.ms-generating-dots {
  display: flex;
  align-items: center;
  gap: 6px;
}

.ms-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  display: inline-block;
  animation: msDotPulse 1.4s ease-in-out infinite, msDotSway 2.4s ease-in-out infinite;
}

.ms-dot-1 {
  background: #D7C8A9; /* Sand */
  animation-delay: 0s, 0s;
}

.ms-dot-2 {
  background: #BC7E32; /* Ochre */
  animation-delay: 0.2s, 0.3s;
}

.ms-dot-3 {
  background: #4B7443; /* Moss */
  animation-delay: 0.4s, 0.6s;
}

@keyframes msDotPulse {
  0%, 100% {
    transform: scale(0.85);
    opacity: 0.6;
  }
  50% {
    transform: scale(1.25);
    opacity: 1;
  }
}

@keyframes msDotSway {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-3px);
  }
}

.ms-generating-text {
  font-size: 13px;
  font-weight: 500;
  color: var(--ink);
  flex: 1;
}

/* Saved Explainers & Thumbnails */
.ms-script-item {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-sm);
  padding: 10px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border, rgba(0, 0, 0, 0.08));
  background: var(--surface);
  margin-bottom: 8px;
  transition: border-color 0.15s, transform 0.15s, box-shadow 0.15s;
}

.ms-script-item:hover {
  border-color: var(--ink-muted);
  transform: translateY(-1px);
}

.ms-item-just-generated {
  border-color: var(--accent) !important;
  box-shadow: 0 0 0 1px var(--accent), 0 4px 14px rgba(188, 126, 50, 0.18) !important;
}

.ms-script-thumb-wrap {
  position: relative;
  width: 88px;
  height: 50px;
  border-radius: var(--radius-sm, 6px);
  overflow: hidden;
  flex-shrink: 0;
  background: #161616;
  border: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

.ms-script-thumb {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
}

.ms-script-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.ms-script-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: flex;
  align-items: center;
  gap: 6px;
}

.ms-script-meta {
  font-size: 12px;
  color: var(--ink-muted);
}

.ms-badge-new {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 2px 7px;
  border-radius: 999px;
  background: rgba(188, 126, 50, 0.18);
  color: var(--accent);
  border: 1px solid rgba(188, 126, 50, 0.4);
  display: inline-flex;
  align-items: center;
  gap: 3px;
  animation: msNewBadgePulse 2.2s ease-in-out infinite;
}

@keyframes msNewBadgePulse {
  0%, 100% {
    transform: scale(1);
    box-shadow: 0 0 0 0 rgba(188, 126, 50, 0.2);
  }
  50% {
    transform: scale(1.05);
    box-shadow: 0 0 8px 2px rgba(188, 126, 50, 0.35);
  }
}

/* Post-Watch "Wanting to Make More" CTA Card */
.ms-post-watch-card {
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-lg, 12px);
  padding: 16px;
  margin: var(--space-md) 0;
  box-shadow: var(--shadow-sm);
  animation: msCardFadeIn 0.3s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

@keyframes msCardFadeIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.ms-post-watch-title {
  font-family: var(--font-display);
  font-size: 15px;
  font-weight: 700;
  color: var(--ink);
  margin-bottom: 4px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.ms-post-watch-prompt {
  font-size: 13px;
  color: var(--ink-secondary);
  margin-bottom: 12px;
  line-height: 1.45;
}

.ms-post-watch-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.ms-post-watch-cta {
  padding: 8px 16px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.ms-back-to-card-btn {
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
}

.ms-suggest-btn {
  background: transparent;
  border: 1px dashed var(--border);
  border-radius: var(--radius-md);
  padding: 7px 12px;
  font-size: 12px;
  font-weight: 500;
  color: var(--ink-secondary);
  cursor: pointer;
  transition: all 0.15s;
}

.ms-suggest-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(188, 126, 50, 0.08);
}

.ms-suggestion-chips {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 10px;
  width: 100%;
}

.ms-suggestion-chip {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 8px 12px;
  text-align: left;
  font-size: 13px;
  font-weight: 500;
  color: var(--ink);
  cursor: pointer;
  transition: all 0.15s;
  display: flex;
  align-items: center;
  gap: 6px;
}

.ms-suggestion-chip:hover {
  border-color: var(--moss);
  background: rgba(75, 116, 67, 0.08);
  transform: translateX(2px);
}

/* Motion Studio -> SRS Bridge */
.ms-study-bridge-btn {
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  background: rgba(75, 116, 67, 0.08);
  border: 1px solid var(--moss, #4B7443);
  color: var(--moss, #4B7443);
  border-radius: var(--radius-md);
  transition: all 0.15s;
}

.ms-study-bridge-btn:hover {
  background: rgba(75, 116, 67, 0.16);
}

.ms-study-bridge-btn:disabled {
  opacity: 0.6;
  cursor: default;
  border-color: var(--border);
  color: var(--ink-muted);
  background: transparent;
}

.ms-create-card-btn {
  font-size: 12px;
  padding: 5px 10px;
  border-radius: var(--radius-sm);
  display: inline-flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
}

.ms-bridge-panel {
  width: 100%;
  margin-top: 12px;
  padding: 14px;
  background: var(--bg);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  box-sizing: border-box;
  animation: msCardFadeIn 0.25s ease-out;
}

.ms-bridge-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}

.ms-bridge-title {
  font-family: var(--font-display);
  font-size: 14px;
  font-weight: 700;
  color: var(--ink);
  display: flex;
  align-items: center;
  gap: 8px;
}

.ms-bridge-pill {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  background: var(--accent-soft);
  color: var(--accent);
  padding: 2px 7px;
  border-radius: 999px;
}

.ms-bridge-close-btn {
  background: none;
  border: none;
  color: var(--ink-muted);
  font-size: 14px;
  cursor: pointer;
  padding: 4px;
  line-height: 1;
}

.ms-bridge-close-btn:hover {
  color: var(--ink);
}

.ms-bridge-sub {
  font-size: 12px;
  color: var(--ink-secondary);
  margin-bottom: 12px;
  line-height: 1.4;
}

.ms-bridge-deck-row {
  margin-bottom: 12px;
}

.ms-bridge-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--ink-muted);
  text-transform: uppercase;
  display: block;
  margin-bottom: 4px;
}

.ms-bridge-deck-select {
  width: 100%;
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  font-size: 13px;
  background: var(--surface);
  color: var(--ink);
  box-sizing: border-box;
}

.ms-bridge-cards-container {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 10px;
}

.ms-bridge-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px;
  box-shadow: var(--shadow-sm);
}

.ms-bridge-card-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}

.ms-bridge-card-num {
  font-size: 11px;
  font-weight: 700;
  color: var(--ink);
}

.ms-bridge-type-badge {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  background: var(--accent-soft);
  color: var(--accent);
  padding: 1px 6px;
  border-radius: 999px;
}

.ms-bridge-remove-card {
  background: none;
  border: none;
  color: var(--ink-muted);
  cursor: pointer;
  font-size: 12px;
  padding: 2px 4px;
}

.ms-bridge-remove-card:hover {
  color: var(--danger);
}

.ms-bridge-input {
  width: 100%;
  box-sizing: border-box;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-family: inherit;
  background: var(--bg);
  color: var(--ink);
  resize: vertical;
}

.ms-bridge-input:focus {
  outline: none;
  border-color: var(--accent);
}

.ms-bridge-add-second-btn {
  background: none;
  border: none;
  color: var(--moss, #4B7443);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  padding: 4px 0;
  margin-bottom: 10px;
  display: inline-block;
}

.ms-bridge-add-second-btn:hover {
  text-decoration: underline;
}

.ms-bridge-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.ms-bridge-save-btn {
  padding: 8px 16px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.ms-bridge-cancel-btn {
  padding: 8px 14px;
  font-size: 13px;
  cursor: pointer;
}

/* ---- Memory Calibration (Settings) ---- */
.calibration-card {
  padding: var(--space-md, 16px);
  background: var(--surface);
  border-radius: var(--radius-md, 12px);
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  gap: 10px;
  border: 1px solid rgba(0, 0, 0, 0.05);
}

[data-theme="dark"] .calibration-card {
  border-color: rgba(255, 255, 255, 0.08);
}

.calibration-badge {
  display: inline-flex;
  align-items: center;
  background: rgba(46, 125, 50, 0.12);
  color: var(--moss, #2E7D32);
  padding: 3px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
}

[data-theme="dark"] .calibration-badge {
  background: rgba(75, 116, 67, 0.25);
  color: #81c784;
}
