/* Warm, paper-and-ink palette - calm rather than "app-stark". Light mode
   reads like washi paper; dark mode reads like ink at night, not a stark
   black UI. */
:root {
  color-scheme: light dark;
  --bg: #f7f2ea;
  --surface: #fffdf8;
  --surface-alt: #efe7d8;
  --text: #33302a;
  --muted: #8c8474;
  --accent: #4f7469;
  --accent-contrast: #faf7f0;
  --accent-soft: rgba(79, 116, 105, 0.14);
  --border: #e8dfd0;
  --good: #6f9a6e;
  --learning: #7fa2c9;
  --warn: #cf9d4f;
  --bad: #c17b5e;
  --mastered: #4f7469;
  --new: #cbc0ac;
  --shadow: rgba(120, 100, 70, 0.12);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #201e1a;
    --surface: #2a2723;
    --surface-alt: #34302a;
    --text: #f2ece0;
    --muted: #a89f8f;
    --accent: #8fb99f;
    --accent-contrast: #1c2620;
    --accent-soft: rgba(143, 185, 159, 0.16);
    --border: #3a352e;
    --good: #8fb99f;
    --learning: #8fb8d9;
    --warn: #dbb26a;
    --bad: #d4917a;
    --mastered: #8fb99f;
    --new: #5c574c;
    --shadow: rgba(0, 0, 0, 0.3);
  }
}

/* Explicit overrides (win over the media query above via higher specificity)
   so the in-app toggle can force a theme regardless of system preference. */
:root[data-theme="light"] {
  --bg: #f7f2ea;
  --surface: #fffdf8;
  --surface-alt: #efe7d8;
  --text: #33302a;
  --muted: #8c8474;
  --accent: #4f7469;
  --accent-contrast: #faf7f0;
  --accent-soft: rgba(79, 116, 105, 0.14);
  --border: #e8dfd0;
  --good: #6f9a6e;
  --learning: #7fa2c9;
  --warn: #cf9d4f;
  --bad: #c17b5e;
  --mastered: #4f7469;
  --new: #cbc0ac;
  --shadow: rgba(120, 100, 70, 0.12);
}

:root[data-theme="dark"] {
  --bg: #201e1a;
  --surface: #2a2723;
  --surface-alt: #34302a;
  --text: #f2ece0;
  --muted: #a89f8f;
  --accent: #8fb99f;
  --accent-contrast: #1c2620;
  --accent-soft: rgba(143, 185, 159, 0.16);
  --border: #3a352e;
  --good: #8fb99f;
  --learning: #8fb8d9;
  --warn: #dbb26a;
  --bad: #d4917a;
  --mastered: #8fb99f;
  --new: #5c574c;
  --shadow: rgba(0, 0, 0, 0.3);
}

* {
  box-sizing: border-box;
}

/* Generic fallback so any element can use class="hidden" without needing its
   own paired selector. More specific .hidden pairings elsewhere (.view.hidden
   etc.) still work the same - they just become redundant, not conflicting -
   EXCEPT id-selector display rules, which still need a class (not bare id)
   to stay overridable by this. */
.hidden {
  display: none;
}

html, body {
  height: 100%;
  overscroll-behavior: none;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  min-height: 100dvh;
  transition: background-color 0.2s ease, color 0.2s ease;
}

.kana-font {
  font-family: "Hiragino Mincho ProN", "Yu Mincho", "Noto Serif JP", serif;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.9rem 1.1rem;
}

.app-header h1 {
  font-size: 1.3rem;
  margin: 0;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.1rem;
}

.icon-btn {
  background: none;
  border: none;
  font-size: 1.3rem;
  color: var(--muted);
  padding: 0.5rem;
  line-height: 1;
  cursor: pointer;
  border-radius: 999px;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.icon-btn:active {
  background: var(--surface-alt);
  color: var(--text);
}

.view {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  padding: 0.25rem 1.1rem 1.1rem;
  gap: 1.1rem;
  overflow-y: auto;
}

.view.hidden {
  display: none;
}

button {
  font: inherit;
}

.primary-btn {
  background: var(--accent);
  color: var(--accent-contrast);
  border: none;
  border-radius: 999px;
  padding: 0.85rem 1.4rem;
  font-weight: 600;
  font-size: 1rem;
  cursor: pointer;
  box-shadow: 0 4px 14px var(--shadow);
  transition: transform 0.1s ease, box-shadow 0.15s ease, opacity 0.15s ease;
}

.primary-btn.big {
  padding: 1.05rem 1.5rem;
  font-size: 1.05rem;
  width: 100%;
}

.primary-btn:active {
  transform: scale(0.97);
  box-shadow: 0 2px 8px var(--shadow);
}

.text-btn {
  background: none;
  border: none;
  color: var(--muted);
  font-weight: 600;
  padding: 0.85rem 0.5rem;
  cursor: pointer;
  border-radius: 0.75rem;
  transition: color 0.15s ease, background-color 0.15s ease;
}

.text-btn:active {
  background: var(--surface-alt);
  color: var(--text);
}

.settings-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.toggle-group {
  display: flex;
  background: var(--surface);
  border-radius: 999px;
  padding: 0.2rem;
}

/* Inside .settings-row (a flex row), groups share width equally. Standalone
   uses (like .set-tabs, a direct child of the flex *column* .view) must not
   inherit flex:1 there - it would grow to fill leftover height instead. */
.settings-row .toggle-group {
  flex: 1;
  min-width: 9rem;
}

.toggle-btn {
  flex: 1;
  padding: 0.45rem 0.6rem;
  border: none;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font-weight: 600;
  font-size: 0.85rem;
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.toggle-btn.active {
  background: var(--accent);
  color: var(--accent-contrast);
}

.switch-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.6rem;
}

.switch-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  transition: color 0.15s ease;
}

.switch-label.active {
  color: var(--text);
}

.switch {
  position: relative;
  width: 2.75rem;
  height: 1.5rem;
  flex-shrink: 0;
  border-radius: 999px;
  border: none;
  background: var(--surface-alt);
  cursor: pointer;
  padding: 0;
  transition: background-color 0.2s ease;
}

.switch-knob {
  position: absolute;
  top: 0.15rem;
  left: 0.15rem;
  width: 1.2rem;
  height: 1.2rem;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 1px 3px var(--shadow);
  transition: transform 0.2s ease;
}

.switch[data-grading="off"] .switch-knob {
  transform: translateX(1.25rem);
}

.caption {
  color: var(--muted);
  font-size: 0.8rem;
  margin: -0.6rem 0 0;
}

.char-grid {
  display: grid;
  grid-template-columns: repeat(var(--cols, 5), minmax(0, 1fr));
  gap: 0.45rem;
  min-width: 0;
}

.set-tabs {
  margin-top: -0.3rem;
}

.char-cell {
  aspect-ratio: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border-radius: 0.9rem;
  border: none;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  position: relative;
  padding: 0.2rem;
  overflow: hidden;
  transition: transform 0.1s ease, background-color 0.15s ease;
}

.char-cell:active {
  transform: scale(0.94);
  background: var(--surface-alt);
}

.char-cell .kana {
  font-size: 1.4rem;
  line-height: 1;
}

.char-cell .romaji-sm {
  font-size: 0.6rem;
  color: var(--muted);
  margin-top: 0.15rem;
  white-space: nowrap;
}

.char-cell.empty {
  background: transparent;
  cursor: default;
}

/* Mastery shown as a small corner dot rather than a full outline - a ring
   around every cell reads as an alert; a soft dot reads as a status. */
.char-cell::after {
  content: "";
  position: absolute;
  top: 0.35rem;
  right: 0.35rem;
  width: 0.4rem;
  height: 0.4rem;
  border-radius: 50%;
  background: transparent;
}

.char-cell.mastery-new::after { background: var(--new); }
.char-cell.mastery-learning::after { background: var(--learning); }
.char-cell.mastery-good::after { background: var(--warn); }
.char-cell.mastery-mastered::after { background: var(--mastered); }

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.7rem;
  font-size: 0.72rem;
  color: var(--muted);
  margin: 0;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
}

.dot {
  width: 0.55rem;
  height: 0.55rem;
  border-radius: 50%;
  display: inline-block;
}

.dot.mastery-new { background: var(--new); }
.dot.mastery-learning { background: var(--learning); }
.dot.mastery-good { background: var(--warn); }
.dot.mastery-mastered { background: var(--mastered); }

.practice-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 0.25rem;
}

.practice-top .spacer {
  width: 2.2rem;
}

.prompt {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.romaji {
  font-size: 1.3rem;
  font-weight: 600;
}

.stroke-counter {
  font-size: 0.78rem;
  color: var(--muted);
}

.scratch-settings {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.scratch-settings .toggle-group {
  flex: 1;
  min-width: 8rem;
}

.guide-field {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex: 1;
  min-width: 9rem;
}

.guide-input {
  flex: 1;
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.5rem 0.9rem;
  color: var(--text);
  font-size: 0.85rem;
}

.guide-input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.guide-preview {
  font-size: 1.2rem;
  color: var(--muted);
  min-width: 1.5rem;
  text-align: center;
}

.canvas-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
}

#draw-canvas,
#scratch-canvas {
  aspect-ratio: 1;
  background: var(--surface);
  border-radius: 1.5rem;
  touch-action: none;
  box-shadow: 0 6px 24px var(--shadow);
}

#draw-canvas {
  width: min(92vw, 58vh, 480px);
}

/* Scratchpad is a practice sheet, not a single centered glyph - give it more room. */
#scratch-canvas {
  width: min(96vw, 66vh, 640px);
}

.page-nav {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
}

.page-label {
  font-size: 0.85rem;
  color: var(--muted);
  min-width: 5rem;
  text-align: center;
}

.icon-btn:disabled {
  opacity: 0.35;
  cursor: default;
}

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

.fc-panel {
  display: flex;
  flex-direction: column;
  gap: 1.1rem;
  flex: 1;
}

.fc-panel.hidden {
  display: none;
}

.fc-progress {
  text-align: center;
  color: var(--muted);
  font-size: 0.85rem;
  margin: 0;
}

.fc-card {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  min-height: 10rem;
  background: var(--surface);
  border-radius: 1.5rem;
  box-shadow: 0 6px 24px var(--shadow);
  font-size: 3rem;
  font-weight: 700;
  text-align: center;
  padding: 1.5rem;
}

.fc-flip-card {
  cursor: pointer;
  transition: transform 0.15s ease;
}

.fc-flip-card:active {
  transform: scale(0.98);
}

#fc-flip-back {
  color: var(--accent);
  font-size: 2.25rem;
}

.fc-options {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.6rem;
}

.fc-option-btn {
  padding: 1.1rem 0.5rem;
  border: none;
  border-radius: 1rem;
  background: var(--surface);
  color: var(--text);
  font-size: 1.3rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease, transform 0.1s ease;
}

.fc-option-btn:active {
  transform: scale(0.96);
}

.fc-option-btn.correct {
  background: var(--accent-soft);
  color: var(--good);
}

.fc-option-btn.incorrect {
  background: rgba(193, 123, 94, 0.14);
  color: var(--bad);
}

.fc-option-btn:disabled {
  cursor: default;
}

.feedback-panel {
  background: var(--surface);
  border-radius: 1rem;
  padding: 0.9rem 1.1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  box-shadow: 0 2px 10px var(--shadow);
}

.feedback-panel.hidden {
  display: none;
}

.score-badge {
  display: inline-flex;
  align-self: flex-start;
  align-items: center;
  font-size: 1rem;
  font-weight: 700;
  padding: 0.3rem 0.8rem;
  border-radius: 999px;
}

.score-badge.good { color: var(--good); background: var(--accent-soft); }
.score-badge.warn { color: var(--warn); background: rgba(207, 157, 79, 0.14); }
.score-badge.bad { color: var(--bad); background: rgba(193, 123, 94, 0.14); }

.feedback-panel ul {
  margin: 0;
  padding-left: 1.1rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.controls {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) minmax(0, 2fr);
  gap: 0.5rem;
  align-items: center;
}

.controls .primary-btn.hidden,
.controls .text-btn.hidden {
  display: none;
}

#next-btn {
  grid-column: 1 / -1;
}
