/* SpeedRecall — single stylesheet. Dark by default, light follows the system. */

:root {
  --bg: #161618;
  --surface: #1e1e22;
  --subtle: #27272c;
  --hover: #313138;
  --border: #36363e;
  --border-strong: #4a4a56;
  --text: #f4f4f5;
  --muted: #a1a1aa;
  --dim: #71717a;
  --accent: #38bdf8;
  --accent-ink: #04121c;
  --accent-tint: rgba(56, 189, 248, 0.16);
  --ok: #10b981;
  --ok-tint: rgba(16, 185, 129, 0.14);
  --bad: #f59e0b;
  --bad-tint: rgba(245, 158, 11, 0.14);
  --shadow: 0 24px 60px -12px rgba(0, 0, 0, 0.6);

  --radius-sm: 8px;
  --radius: 12px;
  --radius-lg: 18px;
  --tap: 48px;
  --max: 640px;

  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  --font-cjk-sc: "PingFang SC", "Hiragino Sans GB", "Noto Sans CJK SC", "Source Han Sans SC", "Microsoft YaHei", sans-serif;
  --font-cjk-jp: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans CJK JP", "Yu Gothic", "Meiryo", sans-serif;
  --font-cjk-kr: "Apple SD Gothic Neo", "Noto Sans CJK KR", "Malgun Gothic", sans-serif;
  --font-arabic: "Geeza Pro", "Noto Naskh Arabic", "Segoe UI", "Amiri", serif;
  --font-yoruba: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", "Lucida Sans Unicode", sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;

  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f4f5;
    --surface: #ffffff;
    --subtle: #e4e4e7;
    --hover: #d4d4d8;
    --border: #d4d4d8;
    --border-strong: #a1a1aa;
    --text: #18181b;
    --muted: #52525b;
    --dim: #71717a;
    --accent: #0284c7;
    --accent-ink: #ffffff;
    --accent-tint: rgba(2, 132, 199, 0.14);
    --ok: #059669;
    --ok-tint: rgba(5, 150, 105, 0.1);
    --bad: #d97706;
    --bad-tint: rgba(217, 119, 6, 0.08);
    --shadow: 0 24px 60px -16px rgba(24, 24, 27, 0.15);
    color-scheme: light;
  }
}

/* ---------- base ---------- */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

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

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

input, button, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; }
button:disabled { cursor: default; opacity: 0.55; }

:focus { outline: none; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap;
}

[hidden] { display: none !important; }

code { font-family: var(--font-mono); font-size: 0.92em; color: var(--text); background: var(--subtle); padding: 0.05em 0.3em; border-radius: 4px; }

/* Script-appropriate fonts, driven by the deck's lang attribute */
:lang(zh) { font-family: var(--font-cjk-sc); }
:lang(ja) { font-family: var(--font-cjk-jp); }
:lang(ko) { font-family: var(--font-cjk-kr); }
:lang(ar), :lang(fa), :lang(ur) { font-family: var(--font-arabic); }
:lang(yo) { font-family: var(--font-yoruba); }
:lang(zh-Latn) { font-family: var(--font); }

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

.app {
  max-width: var(--max);
  margin: 0 auto;
  min-height: 100dvh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding:
    calc(env(safe-area-inset-top, 0px) + 10px)
    calc(env(safe-area-inset-right, 0px) + 16px)
    calc(env(safe-area-inset-bottom, 0px) + 16px)
    calc(env(safe-area-inset-left, 0px) + 16px);
  position: relative;
  overflow-x: hidden;
}

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

.deck-button {
  flex: 1;
  min-width: 0;
  min-height: var(--tap);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
  text-align: left;
}
.deck-button:hover { border-color: var(--border-strong); }
.deck-button__text { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.deck-button__name { font-weight: 600; font-size: 0.95rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.deck-button__meta { font-size: 0.8rem; color: var(--dim); }
.chev { flex: none; color: var(--muted); }

.icon-button {
  flex: none;
  width: var(--tap);
  height: var(--tap);
  min-width: 48px;
  min-height: 48px;
  display: inline-grid;
  place-items: center;
  border-radius: var(--radius);
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 1rem;
  transition: color 140ms ease, background 140ms ease, border-color 140ms ease;
}
.icon-button:hover { color: var(--text); border-color: var(--border-strong); }
.icon-button[aria-pressed="true"] {
  color: var(--accent);
  background: var(--accent-tint);
  border-color: var(--accent);
}

.practice-hint-row {
  display: flex;
  align-items: center;
  padding: 4px 2px;
}
.checkbox-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.88rem;
  color: var(--text);
  cursor: pointer;
}
.checkbox-label input[type="checkbox"] {
  accent-color: var(--accent);
  width: 16px;
  height: 16px;
  cursor: pointer;
}

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

.stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 14px;
  padding: 12px 0 4px;
  min-height: 0;
}

.stats {
  display: flex;
  justify-content: center;
  gap: 18px;
  font-size: 0.82rem;
  color: var(--dim);
  font-variant-numeric: tabular-nums;
}
.stats b { color: var(--text); font-weight: 600; }

.card {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: clamp(150px, 34dvh, 300px);
  padding: 28px 20px;
  border-radius: var(--radius-lg);
  background: var(--surface);
  border: 2px solid var(--border);
  text-align: center;
  transition: border-color 90ms ease, background-color 90ms ease;
}
.card.is-correct { border-color: var(--ok); background: linear-gradient(var(--ok-tint), var(--ok-tint)), var(--surface); }
.card.is-miss { border-color: var(--bad); background: linear-gradient(var(--bad-tint), var(--bad-tint)), var(--surface); }

.card__badge {
  position: absolute;
  top: 10px;
  right: 14px;
  font-size: 1.1rem;
  font-weight: 700;
}
.card.is-correct .card__badge { color: var(--ok); }
.card.is-miss .card__badge { color: var(--bad); }

.card__prompt {
  font-weight: 500;
  line-height: 1.1;
  overflow-wrap: anywhere;
  user-select: none;
  font-size: clamp(2rem, 9vw, 3.4rem);
}
.card__prompt[data-size="xl"] { font-size: clamp(4.5rem, 24vw, 7.5rem); }
.card__prompt[data-size="lg"] { font-size: clamp(3rem, 14vw, 5rem); }
.card__prompt[data-size="md"] { font-size: clamp(2.2rem, 9vw, 3.4rem); }
.card__prompt[data-size="sm"] { font-size: clamp(1.6rem, 6vw, 2.4rem); }
.card__prompt[data-size="xs"] { font-size: clamp(1.25rem, 4.5vw, 1.8rem); line-height: 1.3; }
.card__prompt[data-size="message"] { font-size: 1.35rem; font-weight: 600; }
.card__prompt[data-size="cloze"] {
  font-size: clamp(1.25rem, 3.5vw, 1.95rem);
  line-height: 2.2;
  max-width: 820px;
  margin: 0 auto;
  word-break: normal;
  overflow-wrap: normal;
}

.card__hint { color: var(--muted); font-size: 1rem; min-height: 1.45em; }

.card__reveal {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  margin-top: 6px;
  padding: 8px 18px;
  border-radius: var(--radius);
  background: var(--subtle);
}
.card__reveal-label { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--dim); }
.card__answer { font-size: 1.6rem; font-weight: 600; color: var(--text); }

.card.is-miss .card__reveal-label { color: #64748b; }
.card.is-miss .card__answer { color: var(--bad); }

.answer-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 48px;
  padding-bottom: max(8px, env(safe-area-inset-bottom, 0px));
}

@media (max-width: 560px) {
  .answer-row {
    position: sticky;
    bottom: 0;
    z-index: 15;
    background: var(--bg);
    padding: 8px 0 max(12px, env(safe-area-inset-bottom, 0px));
  }
}

.answer-input {
  flex: 1;
  min-width: 0;
  height: 56px;
  min-height: 48px;
  padding: 0 16px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 2px solid var(--border-strong);
  font-size: 1.35rem; /* ≥16px stops iOS zooming on focus */
  text-align: center;
}
.answer-input::placeholder { color: var(--dim); font-size: 1rem; }
.answer-input:focus { border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-tint); outline: none; }
.answer-input.is-correcting { border-color: var(--bad); }
.answer-input.is-correcting:focus { box-shadow: 0 0 0 4px var(--bad-tint); }
.answer-input:disabled { opacity: 0.5; }

.reveal-button {
  flex: none;
  min-width: 84px;
  height: 56px;
  min-height: 48px;
  padding: 0 14px;
  border-radius: var(--radius);
  background: var(--subtle);
  border: 1px solid var(--border);
  color: var(--muted);
  font-weight: 600;
  font-size: 0.95rem;
}
.reveal-button:hover:not(:disabled) { color: var(--text); border-color: var(--border-strong); }

.help-line { text-align: center; font-size: 0.82rem; color: var(--dim); min-height: 1.2em; }

/* 150ms lateral nudge on error */
.nudge { animation: lateralNudge 150ms ease; }
@keyframes lateralNudge {
  20%, 60% { transform: translateX(-6px); }
  40%, 80% { transform: translateX(6px); }
}

/* Phones: anchor near the top so the keyboard never covers the input */
@media (max-width: 560px) {
  .stage { justify-content: flex-start; padding-top: clamp(12px, 5dvh, 48px); }
}

/* Short screens / keyboard open: keep prompt and input together */
@media (max-height: 560px) {
  .stage { justify-content: flex-start; gap: 8px; padding-top: 8px; }
  .stats { display: none; }
  .card { min-height: 120px; padding: 14px 16px; }
  .card__prompt[data-size="xl"] { font-size: clamp(3rem, 18vw, 5rem); }
  .help-line { display: none; }
}

/* ---------- sheets (dialogs) ---------- */

.sheet {
  margin: auto;
  width: min(100% - 24px, 600px);
  max-height: min(88dvh, 860px);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.sheet::backdrop { background: rgba(5, 8, 14, 0.6); backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px); }

@media (max-width: 560px) {
  .sheet {
    margin: auto 0 0;
    width: 100%;
    max-width: 100%;
    max-height: 92dvh;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    border-bottom: 0;
  }
}

.sheet__inner { display: flex; flex-direction: column; max-height: inherit; }
.sheet__header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 12px 12px 20px;
  border-bottom: 1px solid var(--border);
}
.sheet__header h2 { font-size: 1.1rem; font-weight: 650; }
.sheet__body { padding: 16px 20px 20px; overflow-y: auto; overscroll-behavior: contain; display: flex; flex-direction: column; gap: 18px; }
.sheet__footer {
  padding: 12px 20px calc(12px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--border);
}
.sheet__footer .primary-button { width: 100%; }

.section-title { font-size: 0.8rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.07em; color: var(--muted); }
.note { font-size: 0.88rem; color: var(--muted); }

.panel { display: flex; flex-direction: column; gap: 12px; padding-bottom: 18px; border-bottom: 1px solid var(--border); }
.panel:last-child { border-bottom: 0; padding-bottom: 0; }

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

.field-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field__label { font-size: 0.82rem; color: var(--muted); font-weight: 500; }
.field--check { flex-direction: row; align-items: center; gap: 10px; min-height: var(--tap); font-size: 0.92rem; color: var(--muted); grid-column: 1 / -1; }
.field--check input { width: 20px; height: 20px; accent-color: var(--accent); }

.field select,
.field input[type="text"],
.field input[type="password"],
.field textarea {
  width: 100%;
  min-height: var(--tap);
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--bg);
  border: 1px solid var(--border-strong);
  font-size: 1rem;
}
.field textarea { font-family: var(--font-mono); font-size: 0.9rem; resize: vertical; tab-size: 12; }
.field select:focus, .field input:focus, .field textarea:focus { border-color: var(--accent); outline: none; box-shadow: 0 0 0 3px var(--accent-tint); }

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

.primary-button, .secondary-button {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: var(--tap);
  padding: 0 18px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 0.95rem;
}
.primary-button { background: var(--accent); color: var(--accent-ink); }
.primary-button:hover:not(:disabled) { filter: brightness(1.08); }
.secondary-button { background: var(--subtle); border: 1px solid var(--border-strong); color: var(--text); cursor: pointer; }
.secondary-button:hover { background: var(--hover); }
.secondary-button.danger, .text-button.danger { color: var(--bad); }
.file-button:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }

.text-button { align-self: flex-start; min-height: var(--tap); font-size: 0.9rem; font-weight: 600; }

.chip {
  min-height: 34px;
  min-width: 52px;
  padding: 0 12px;
  border-radius: 999px;
  background: var(--subtle);
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 600;
  position: relative;
}
.chip::after { content: ""; position: absolute; inset: -5px 0; } /* 44px hit area */
.chip:hover { color: var(--text); border-color: var(--border-strong); }

.status { font-size: 0.88rem; color: var(--muted); }
.status.ok { color: var(--ok); }
.status.error { color: var(--bad); }

/* ---------- unit picker ---------- */

.units-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: -6px; }
.units-head__actions { display: flex; gap: 8px; }

.unit-list { display: flex; flex-direction: column; gap: 16px; }
.unit-group__head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.unit-group__head h4 { font-size: 0.95rem; font-weight: 650; }

.unit-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }

.unit {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  min-height: 76px;
  padding: 10px 12px 14px;
  border-radius: var(--radius);
  background: var(--bg);
  border: 2px solid var(--border);
  text-align: left;
  overflow: hidden;
}
.unit:hover { border-color: var(--border-strong); }
.unit[aria-pressed="true"] { border-color: var(--accent); background: linear-gradient(var(--accent-tint), var(--accent-tint)), var(--bg); }
.unit[aria-pressed="true"]::before {
  content: "✓";
  position: absolute; top: 8px; right: 10px;
  font-size: 0.85rem; font-weight: 700; color: var(--accent);
}
.unit__name { font-weight: 600; font-size: 0.92rem; padding-right: 18px; }
.unit__count { font-size: 0.78rem; color: var(--dim); }
.unit__preview {
  font-size: 0.95rem; color: var(--muted);
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
  overflow-wrap: anywhere;
}
.unit__bar {
  position: absolute; left: 0; bottom: 0; height: 4px;
  width: calc(var(--mastery, 0) * 100%);
  background: var(--ok);
}

/* ---------- toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 20px);
  transform: translate(-50%, 20px);
  max-width: min(92vw, 520px);
  padding: 12px 16px;
  border-radius: var(--radius);
  background: var(--text);
  color: var(--bg);
  font-size: 0.92rem;
  font-weight: 500;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease, transform 160ms ease;
  z-index: 100;
}
/* ---------- topbar actions & reverse mode ---------- */
.topbar-actions { display: flex; align-items: center; gap: 8px; }
#btnReverse[aria-pressed="true"] {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--accent-tint);
}

.practice-fields-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  margin-top: 6px;
}
.swap-fields-btn {
  margin-bottom: 2px;
  min-width: 38px;
  width: 38px;
  height: 38px;
  flex: none;
}
.practice-deck-actions-bar {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 8px;
}
.btn-compact {
  padding: 6px 12px;
  font-size: 0.82rem;
  min-height: 32px;
}
.deck-actions-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 14px;
}

/* ---------- cloze cards & slots ---------- */
.cloze-sentence {
  display: inline;
  line-height: 1.35;
  text-wrap: balance;
  font-size: clamp(1.4rem, 5.5vw, 2.2rem);
  font-weight: 500;
}
.cloze-slot {
  display: inline-block;
  vertical-align: baseline;
  position: relative;
  min-width: 4rem;
  margin: 0 4px;
  padding: 0 8px;
  border-bottom: 3px solid var(--accent);
  border-radius: 4px 4px 0 0;
  background: var(--subtle);
  color: var(--text);
  font-weight: 600;
  transition: all 120ms ease;
  user-select: none;
}
.cloze-slot.is-active {
  border-bottom-color: var(--accent);
  box-shadow: 0 2px 8px var(--accent-tint);
}
.cloze-slot.is-correct {
  border-bottom-color: var(--ok);
  background: var(--ok-tint);
  color: var(--ok);
}
.cloze-slot.is-miss {
  border-bottom-color: var(--bad);
  background: var(--bad-tint);
  color: var(--bad);
}
.cloze-slot__inner {
  white-space: pre-wrap;
  min-height: 1.2em;
  display: inline-block;
}
.cloze-slot__placeholder {
  color: var(--dim);
  opacity: 0.6;
}

/* ---------- deck editor sheet ---------- */
.sheet--editor {
  max-width: 960px;
  width: 95vw;
}
.sheet__inner--editor {
  max-height: 90dvh;
  display: flex;
  flex-direction: column;
}
.editor-body {
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: hidden;
  flex: 1;
  min-height: 0;
}
.editor-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
}
.editor-toolbar__actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
}
.editor-toolbar__stats {
  font-size: 0.82rem;
  color: var(--dim);
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}
.editor-note {
  font-size: 0.8rem;
  color: var(--dim);
  line-height: 1.4;
}
.editor-table-wrap {
  flex: 1;
  min-height: 240px;
  max-height: 52dvh;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
}
.editor-table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
  font-size: 0.9rem;
}
.editor-table thead {
  position: sticky;
  top: 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border-strong);
  z-index: 5;
}
.editor-table th {
  padding: 8px 10px;
  font-size: 0.76rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--dim);
  font-weight: 600;
}
.editor-table td {
  padding: 4px 6px;
  border-bottom: 1px solid var(--border);
}
.editor-table tr:hover td {
  background: var(--subtle);
}
.editor-input {
  width: 100%;
  padding: 6px 8px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  color: var(--text);
  font-size: 0.88rem;
}
.editor-input:hover {
  border-color: var(--border);
}
.editor-input:focus {
  background: var(--surface);
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-tint);
  outline: none;
}
.editor-input.is-invalid {
  border-color: var(--bad);
  background: var(--bad-tint);
}
.editor-del-btn {
  width: 28px;
  height: 28px;
  display: inline-grid;
  place-items: center;
  border-radius: 6px;
  color: var(--dim);
  font-size: 0.82rem;
}
.editor-del-btn:hover {
  color: var(--bad);
  background: var(--bad-tint);
}
.chip--accent {
  background: var(--accent-tint);
  color: var(--accent);
  border-color: var(--accent);
  font-weight: 600;
}
.chip--publish {
  background: var(--ok-tint);
  color: var(--ok);
  border-color: var(--ok);
  font-weight: 600;
}
.editor-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

/* ---------- tabbed settings sheet ---------- */

.settings-nav {
  padding: 12px 18px 0;
  border-bottom: 1px solid var(--border);
}

.segmented-control {
  display: flex;
  gap: 4px;
  background: var(--subtle);
  padding: 4px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

.segment-btn {
  flex: 1;
  padding: 7px 10px;
  font-size: 0.82rem;
  font-weight: 600;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  text-align: center;
  white-space: nowrap;
  transition: all 140ms ease;
}

.segment-btn:hover {
  color: var(--text);
}

.segment-btn.active {
  background: var(--surface);
  color: var(--text);
  border-color: var(--border-strong);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.settings-tab-panel {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.panel-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.deck-picker-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.deck-summary-box,
.storage-stats-box {
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  background: var(--subtle);
  border: 1px solid var(--border);
  font-size: 0.85rem;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.deck-summary-box b,
.storage-stats-box b {
  color: var(--text);
}

/* ---------- locked stage containment ---------- */
:root.locked-stage,
body.locked-stage {
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  position: fixed;
  width: 100%;
}

.stage.is-locked,
.app.is-locked {
  height: 100%;
  max-height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
}

/* ---------- prompt optical centering ---------- */
.card__prompt-stage {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 120px;
  width: 100%;
  position: relative;
}

/* ---------- dynamic cloze engine ---------- */
.cloze-sentence {
  display: inline;
  line-height: 1.8;
  font-size: clamp(1.4rem, 5vw, 2.2rem);
  text-align: center;
  word-break: normal;
  overflow-wrap: normal;
}

.cloze-token {
  display: inline;
  white-space: nowrap;
}

.cloze-view-segmented .cloze-token {
  display: inline-block;
  margin: 0 0.25em;
}

.cloze-token--punct,
.cloze-view-segmented .cloze-token--punct {
  margin-left: 0 !important;
}

.cloze-token--before-punct,
.cloze-view-segmented .cloze-token--before-punct,
.cloze-view-segmented .cloze-token:has(+ .cloze-token--punct) {
  margin-right: 0 !important;
}

.cloze-view-native .cloze-token {
  display: inline;
  margin: 0;
}

.cloze-token--target {
  display: inline-block;
  vertical-align: baseline;
}

.cloze-input {
  display: inline-block;
  vertical-align: baseline;
  box-sizing: border-box;
  height: 1.55em;
  padding: 0 0.35em;
  margin: 0 0.15em;
  border: 2px solid var(--accent);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  font-size: inherit;
  text-align: center;
  line-height: 1.2;
  flex: none;
  transition: border-color 90ms ease, box-shadow 90ms ease;
}

.cloze-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-tint);
  outline: none;
}

.cloze-input.is-correcting {
  border-color: var(--bad);
}

.cloze-input.is-correcting:focus {
  box-shadow: 0 0 0 3px var(--bad-tint);
}

/* ---------- hold-to-unlock progress ring & flashes ---------- */
.progress-ring {
  width: 44px;
  height: 44px;
  transform: rotate(-90deg);
  display: inline-block;
  vertical-align: middle;
}

.progress-ring__circle-bg {
  stroke: var(--subtle);
}

.progress-ring__circle {
  transition: stroke-dashoffset 150ms ease;
  stroke: var(--accent);
  stroke-linecap: round;
}

.card.is-accent-flash,
.card.flash-accent,
body[data-route="reflex"] .card.is-accent-flash,
.route-reflex .card.is-accent-flash {
  animation: accentFlash 220ms ease-out;
}

@keyframes accentFlash {
  0% {
    border-color: var(--accent);
    box-shadow: 0 0 24px var(--accent-tint), inset 0 0 12px var(--accent-tint);
    background-color: var(--accent-tint);
  }
  100% {
    border-color: var(--border);
    box-shadow: none;
    background-color: transparent;
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0ms !important; animation: none !important; }
}

/* ---------- Route-Specific Staging & Layouts ---------- */

/* Route 1: Reflex Sprint */
body[data-route="reflex"] .card,
.route-reflex .card {
  border: none;
  background: transparent;
  box-shadow: none;
}

body[data-route="reflex"] .card__badge,
body[data-route="reflex"] .reveal-button,
body[data-route="reflex"] .help-line,
.route-reflex .card__badge,
.route-reflex .reveal-button,
.route-reflex .help-line {
  display: none !important;
}

body[data-route="reflex"] .topbar,
body[data-route="reflex"] .stats,
.route-reflex .topbar,
.route-reflex .stats {
  opacity: 0.15;
  transition: opacity 200ms ease;
}
body[data-route="reflex"] .topbar:hover,
body[data-route="reflex"] .stats:hover {
  opacity: 1;
}

body[data-route="reflex"] .answer-input,
.route-reflex .answer-input {
  border: none;
  border-bottom: 2px solid var(--accent);
  border-radius: 0;
  background: transparent;
  box-shadow: none;
  text-align: center;
}

body[data-route="reflex"] .answer-input:focus,
.route-reflex .answer-input:focus {
  border-bottom-color: var(--accent);
  box-shadow: none;
}

.ghost-input {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: default;
  pointer-events: auto;
  z-index: 10;
}

/* Route 2 & 3: Contextual & Variable Cloze */
body[data-route="contextual-cloze"] .answer-row,
body[data-route="contextual-cloze"] .help-line,
body[data-route="variable-cloze"] .answer-row,
body[data-route="variable-cloze"] .help-line,
body.route-contextual-cloze .answer-row,
body.route-contextual-cloze .help-line,
body.route-variable-cloze .answer-row,
body.route-variable-cloze .help-line,
body.has-cloze .answer-row,
body.has-cloze .help-line {
  display: none !important;
}

body[data-route="contextual-cloze"] .stage,
body[data-route="variable-cloze"] .stage,
body.has-cloze .stage {
  justify-content: flex-start;
  padding-top: max(24px, 8vh);
}

body[data-route="contextual-cloze"] .card,
body[data-route="variable-cloze"] .card,
body.has-cloze .card {
  max-width: 860px;
  width: 100%;
  margin: 0 auto;
}

.cloze-sentence {
  font-size: clamp(1.25rem, 3.5vw, 2rem);
  line-height: 2.2;
  letter-spacing: 0.01em;
  text-align: center;
}

.cloze-input {
  display: inline-block;
  vertical-align: baseline;
  box-sizing: border-box;
  min-width: 3ch;
  height: 1.55em;
  padding: 0 0.35em;
  margin: 0 0.15em;
  border: 2px solid var(--accent);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  font-size: inherit;
  text-align: center;
  line-height: 1.2;
  flex: none;
  transition: border-color 90ms ease, box-shadow 90ms ease;
}

.cloze-input--latin {
  min-width: 3ch;
}

.cloze-input--cjk {
  min-width: 2em;
}

.cloze-gloss {
  margin-top: 10px;
  font-size: 0.9rem;
  color: var(--muted);
  font-style: normal;
  opacity: 0.85;
}

/* Route 4: Kid / Sensory Flash Stage */
body[data-route="sensory-flash"] .topbar,
body[data-route="sensory-flash"] .stats,
body[data-route="sensory-flash"] .answer-row,
body[data-route="sensory-flash"] .help-line,
body[data-route="sensory-flash"] .home-hub,
.route-sensory-flash .topbar,
.route-sensory-flash .stats,
.route-sensory-flash .answer-row,
.route-sensory-flash .help-line,
.route-sensory-flash .home-hub {
  display: none !important;
}

body[data-route="sensory-flash"],
.route-sensory-flash {
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

body[data-route="sensory-flash"] .stage,
.route-sensory-flash .stage {
  padding: 0;
  gap: 0;
  justify-content: center;
  align-items: center;
  height: 100%;
}

body[data-route="sensory-flash"] .card,
.route-sensory-flash .card {
  border: none;
  background: transparent;
  box-shadow: none;
  width: 100%;
  max-width: 600px;
  padding: 0 24px;
  min-height: auto;
}

body[data-route="sensory-flash"] .card__prompt,
.route-sensory-flash .card__prompt {
  font-size: clamp(3.2rem, 14vw, 6.5rem);
  text-align: center;
  letter-spacing: -0.02em;
  line-height: 1.15;
}

.sensory-tap-surface {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 66.6dvh;
  z-index: 20;
  background: transparent;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 28px);
  box-sizing: border-box;
}

.sensory-tap-surface__hint {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 18px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 500;
  letter-spacing: 0.02em;
  opacity: 0.75;
  pointer-events: none;
  transition: opacity 150ms ease;
  animation: sensoryHintPulse 3s ease-in-out infinite;
}

@keyframes sensoryHintPulse {
  0%, 100% { opacity: 0.55; transform: translateY(0); }
  50% { opacity: 0.9; transform: translateY(-3px); }
}

.sensory-tap-surface__cue {
  font-size: 0.75rem;
  color: var(--accent);
}

.sensory-hud {
  position: fixed;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 16px);
  left: calc(env(safe-area-inset-left, 0px) + 20px);
  font-size: 0.8rem;
  color: var(--dim);
  opacity: 0.45;
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
  z-index: 10;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
}

.pacing-countdown-bar {
  width: 100%;
  height: 6px;
  background: var(--subtle);
  border-radius: 999px;
  overflow: hidden;
  margin-top: 14px;
}

.pacing-countdown-bar__fill {
  height: 100%;
  width: 100%;
  background: var(--accent);
  transform-origin: left;
  animation: pacingDeplete 2000ms linear forwards;
}

@keyframes pacingDeplete {
  0% { transform: scaleX(1); }
  100% { transform: scaleX(0); }
}

.safety-lock-glyph {
  position: fixed;
  top: calc(env(safe-area-inset-top, 0px) + 12px);
  right: calc(env(safe-area-inset-right, 0px) + 16px);
  width: 48px;
  height: 48px;
  min-width: 48px;
  min-height: 48px;
  border-radius: 50%;
  background: var(--surface);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  cursor: pointer;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.safety-lock-ring {
  position: absolute;
  top: 0;
  left: 0;
  width: 48px;
  height: 48px;
  transform: rotate(-90deg);
  pointer-events: none;
}

.safety-lock-ring__bg {
  stroke: var(--subtle);
  opacity: 0.35;
}

.safety-lock-ring__progress {
  stroke: var(--accent);
  stroke-dasharray: 132;
  stroke-dashoffset: 132;
  transition: stroke-dashoffset 0ms linear;
}

.safety-lock-glyph.is-holding .safety-lock-ring__progress {
  stroke-dashoffset: 0;
  transition: stroke-dashoffset 2000ms linear;
}

.safety-lock-glyph .safety-lock-icon {
  color: var(--dim);
  transition: color 150ms ease;
  z-index: 2;
}

.safety-lock-glyph.is-holding .safety-lock-icon {
  color: var(--accent);
}

/* Route 5: Strict Production */
.route-strict-production .answer-input,
body[data-route="strict-production"] .answer-input {
  border: 2px solid var(--accent);
  letter-spacing: 0.05em;
}

.route-strict-production .answer-input.is-composing,
body[data-route="strict-production"] .answer-input.is-composing {
  border-bottom: 2px dashed var(--accent);
}

.ime-commit-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 6px 12px;
  min-height: 48px;
  min-width: 48px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--accent);
  background: var(--accent-tint);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  white-space: nowrap;
}

/* ---------- Home Hub & Quick Resume Hero ---------- */
.home-hub {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 24px;
  padding: 16px 0 calc(24px + env(safe-area-inset-bottom, 0px));
  overflow-y: auto;
  overscroll-behavior: contain;
}

.home-hub__hero {
  width: 100%;
}

.hero-card {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 24px;
  border-radius: var(--radius-lg);
  background: var(--surface);
  border: 2px solid var(--border-strong);
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: transform 120ms ease, border-color 120ms ease;
}

.hero-card:hover {
  border-color: var(--accent);
  transform: translateY(-2px);
}

.hero-card__badge {
  align-self: flex-start;
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ok);
  background: var(--ok-tint);
}

.hero-card__title {
  font-size: clamp(1.4rem, 5vw, 1.85rem);
  font-weight: 700;
  line-height: 1.2;
}

.hero-card__meta {
  font-size: 0.95rem;
  color: var(--muted);
}

.hero-card__cta {
  width: 100%;
  margin-top: 8px;
  min-height: 48px;
}

.home-hub__secondary {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.home-hub__browse-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.home-deck-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 12px;
}

.home-deck-card {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 14px;
  border-radius: var(--radius);
  background: var(--subtle);
  border: 1px solid var(--border);
  text-align: left;
  cursor: pointer;
  min-height: 48px;
}

.home-deck-card:hover {
  border-color: var(--border-strong);
}

.home-deck-card__name {
  font-weight: 600;
  font-size: 0.95rem;
}

.home-deck-card__meta {
  font-size: 0.8rem;
  color: var(--dim);
}

/* ---------- Milestone Celebration Screen ---------- */
.sheet--milestone .sheet__inner {
  text-align: center;
}

.milestone-body {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  padding: 24px 20px;
}

.milestone-sub {
  font-size: 0.95rem;
  color: var(--muted);
}

.milestone-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  width: 100%;
}

.milestone-stat {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 14px 8px;
  background: var(--subtle);
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

.milestone-stat__value {
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--accent);
}

.milestone-stat__label {
  font-size: 0.75rem;
  text-transform: uppercase;
  color: var(--dim);
  letter-spacing: 0.05em;
}

/* ---------- Module 3: In-Drill Header Toggles ---------- */
.topbar__actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

/* Route 4 Sensory Mode Suppression Guardrail */
body[data-route="sensory-flash"] .topbar,
.route-sensory .topbar {
  display: none !important;
  pointer-events: none !important;
}

/* ---------- Module 1: The 4-Tab Settings Hub ---------- */
.settings-nav {
  margin-bottom: 16px;
}

.tablist {
  display: flex;
  gap: 6px;
  background: var(--bg-surface, #1e1e22);
  padding: 4px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  overflow-x: auto;
}

.tab-button {
  flex: 1;
  padding: 8px 12px;
  font-size: 0.88rem;
  font-weight: 600;
  border-radius: calc(var(--radius) - 2px);
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  text-align: center;
  white-space: nowrap;
  transition: all 0.15s ease;
}

.tab-button:hover {
  color: var(--fg);
  background: rgba(255, 255, 255, 0.05);
}

.tab-button.is-active,
.tab-button[aria-selected="true"] {
  background: #2563eb !important;
  color: #ffffff !important;
}

.tab-panel[hidden] {
  display: none !important;
}

.deck-summary-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  margin-bottom: 12px;
}

.deck-summary-card__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 4px;
}

.deck-summary-card__title {
  font-size: 1rem;
  font-weight: 650;
  margin: 0;
}

.deck-summary-card__metrics {
  font-size: 0.85rem;
  color: var(--dim);
  margin: 0;
}

.deck-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
}

.backup-metrics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: 10px;
  margin-bottom: 14px;
}

.metric-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.metric-card__label {
  font-size: 0.74rem;
  color: var(--dim);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.metric-card__value {
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--fg);
}

.metric-card__sub {
  font-size: 0.74rem;
  color: var(--dim);
}

.danger-zone {
  border-color: rgba(239, 68, 68, 0.3) !important;
  background: rgba(239, 68, 68, 0.03) !important;
}

.danger-text {
  color: var(--error, #ef4444) !important;
}

/* ---------- Module 2: Role Matrix in Practice Modal ---------- */
.role-matrix-group {
  margin-top: 10px;
}

.role-matrix {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 6px;
}

.role-field {
  flex: 1;
}

.role-swap-btn {
  align-self: flex-end;
  margin-bottom: 2px;
  width: 44px;
  height: 44px;
  font-size: 1.25rem;
  font-weight: 700;
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}

.role-swap-btn:hover {
  border-color: var(--border-strong);
  background: rgba(255, 255, 255, 0.06);
}

.session-hint-check {
  margin-top: 10px;
}

/* ---------- Module 4: Tabular Deck Builder Modal ---------- */
.sheet--deck-builder .sheet__inner--wide {
  max-width: 920px;
  width: 95vw;
}

.grid-actions-ribbon {
  display: flex;
  gap: 8px;
  margin: 12px 0;
  flex-wrap: wrap;
}

.grid-paste-area {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 12px;
}

.deck-grid-container {
  max-height: 400px;
  overflow-y: auto;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-top: 8px;
  background: var(--bg);
}

.deck-grid-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}

.deck-grid-table th {
  background: var(--bg-surface, #1e1e22);
  padding: 8px 10px;
  text-align: left;
  font-size: 0.76rem;
  text-transform: uppercase;
  color: var(--dim);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 2;
}

.deck-grid-table td {
  padding: 4px 6px;
  border-bottom: 1px solid var(--border);
}

.deck-grid-table input {
  width: 100%;
  padding: 6px 8px;
  font-size: 0.88rem;
  background: var(--bg);
  border: 1px solid transparent;
  border-radius: 4px;
  color: var(--fg);
  font-family: inherit;
}

.deck-grid-table input:focus {
  border-color: var(--accent);
  outline: none;
  background: rgba(255, 255, 255, 0.04);
}

.deck-grid-table .btn-delete-row {
  background: transparent;
  border: none;
  color: var(--dim);
  cursor: pointer;
  font-size: 1rem;
  padding: 4px 6px;
  border-radius: 4px;
}

.deck-grid-table .btn-delete-row:hover {
  color: var(--error, #ef4444);
  background: rgba(239, 68, 68, 0.12);
}
