:root {
  --bg: #0f0f1a;
  --surface: #1a1a2e;
  --surface-hover: #22223a;
  --card-bg: #16213e;
  --card-border: #0f3460;
  --primary: #e94560;
  --primary-hover: #ff6b81;
  --accent: #53d8fb;
  --text: #eaeaea;
  --text-dim: #8b8b9e;
  --success: #2ecc71;
  --warning: #f39c12;
  --danger: #e74c3c;
  --easy: #2ecc71;
  --good: #3498db;
  --hard: #f39c12;
  --again: #e74c3c;
  --radius: 16px;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

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

body {
  font-family:
    'Segoe UI',
    -apple-system,
    BlinkMacSystemFont,
    sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  justify-content: center;
}

#app {
  width: 100%;
  max-width: 680px;
  padding: 24px 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}

header {
  text-align: center;
}

header h1 {
  font-size: 1.8rem;
  font-weight: 700;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.subtitle {
  color: var(--text-dim);
  font-size: 0.9rem;
  margin-top: 4px;
}

/* Controls */
.controls-top {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: center;
  align-items: center;
  width: 100%;
}

.range-selector {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--surface);
  padding: 8px 12px;
  border-radius: var(--radius);
}

.range-selector label {
  color: var(--text-dim);
  font-size: 0.85rem;
}

.range-selector input {
  width: 60px;
  padding: 4px 8px;
  border: 1px solid var(--card-border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.85rem;
  text-align: center;
}

.range-selector span {
  color: var(--text-dim);
}

.mode-toggle {
  display: flex;
  gap: 4px;
  background: var(--surface);
  padding: 4px;
  border-radius: var(--radius);
}

.btn {
  border: none;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 600;
  transition: all 0.2s ease;
  outline: none;
}

.btn-sm {
  padding: 4px 12px;
  border-radius: 8px;
  background: var(--primary);
  color: white;
}

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

.btn-mode {
  padding: 6px 14px;
  border-radius: 12px;
  background: transparent;
  color: var(--text-dim);
}

.btn-mode.active {
  background: var(--primary);
  color: white;
}

.btn-mode:hover:not(.active) {
  color: var(--text);
  background: var(--surface-hover);
}

/* Flashcard */
.flashcard-container {
  perspective: 1200px;
  width: 100%;
  max-width: 420px;
}

.flashcard {
  width: 100%;
  aspect-ratio: 3 / 4;
  max-height: 480px;
  cursor: pointer;
  outline: none;
}

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

.flashcard.flipped .flashcard-inner {
  transform: rotateY(180deg);
}

.flashcard-front,
.flashcard-back {
  position: absolute;
  width: 100%;
  height: 100%;
  backface-visibility: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
  background: linear-gradient(145deg, var(--card-bg), var(--surface));
  border: 2px solid var(--card-border);
  box-shadow: var(--shadow);
  padding: 24px;
  gap: 16px;
}

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

.card-number {
  position: absolute;
  top: 16px;
  left: 20px;
  font-size: 0.85rem;
  color: var(--text-dim);
  font-weight: 600;
}

.card-main {
  font-size: 8rem;
  line-height: 1;
  font-weight: 400;
  text-align: center;
  word-break: break-word;
}

.card-english {
  font-size: 2.4rem;
  font-weight: 700;
  color: var(--accent);
  text-transform: capitalize;
}

.card-hint {
  font-size: 0.8rem;
  color: var(--text-dim);
  position: absolute;
  bottom: 16px;
}

/* Progress */
.progress-bar-container {
  width: 100%;
  max-width: 420px;
  height: 6px;
  background: var(--surface);
  border-radius: 3px;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), var(--accent));
  border-radius: 3px;
  transition: width 0.3s ease;
  width: 0%;
}

.progress-text {
  font-size: 0.85rem;
  color: var(--text-dim);
}

/* Navigation Controls */
.controls-bottom {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: center;
}

.btn-nav {
  padding: 10px 24px;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-size: 1rem;
}

.btn-nav:hover {
  background: var(--surface-hover);
  transform: translateY(-1px);
}

.btn-action {
  padding: 10px 18px;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text-dim);
}

.btn-action:hover {
  background: var(--surface-hover);
  color: var(--text);
}

/* Self-grading */
.self-grade {
  text-align: center;
  width: 100%;
  max-width: 420px;
}

.self-grade p {
  font-size: 0.85rem;
  color: var(--text-dim);
  margin-bottom: 8px;
}

.grade-buttons {
  display: flex;
  gap: 8px;
  justify-content: center;
}

.btn-grade {
  padding: 8px 20px;
  border-radius: 12px;
  color: white;
  font-size: 0.85rem;
}

.btn-again {
  background: var(--again);
}
.btn-again:hover {
  background: #c0392b;
}
.btn-hard {
  background: var(--hard);
}
.btn-hard:hover {
  background: #e67e22;
}
.btn-good {
  background: var(--good);
}
.btn-good:hover {
  background: #27ae60;
}
.btn-easy {
  background: var(--easy);
}
.btn-easy:hover {
  background: #27ae60;
  filter: brightness(1.2);
}

/* Search */
.search-container {
  width: 100%;
  max-width: 420px;
  position: relative;
}

#searchInput {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid var(--card-border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-size: 0.95rem;
  outline: none;
  transition: border-color 0.2s;
}

#searchInput:focus {
  border-color: var(--primary);
}

#searchInput::placeholder {
  color: var(--text-dim);
}

.search-results {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: var(--surface);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  max-height: 240px;
  overflow-y: auto;
  display: none;
  z-index: 10;
}

.search-results.visible {
  display: block;
}

.search-result-item {
  padding: 10px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  transition: background 0.15s;
}

.search-result-item:hover {
  background: var(--surface-hover);
}

.search-result-item:last-child {
  border-bottom: none;
}

.search-kanji {
  font-size: 1.5rem;
}

.search-keyword {
  color: var(--accent);
  font-size: 0.9rem;
}

.search-id {
  color: var(--text-dim);
  font-size: 0.8rem;
}

/* Stats */
.stats-bar {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  justify-content: center;
  padding: 12px 20px;
  background: var(--surface);
  border-radius: var(--radius);
  width: 100%;
  max-width: 420px;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.stat-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-dim);
}

.stat-value {
  font-size: 1.1rem;
  font-weight: 700;
}

#statAgain {
  color: var(--again);
}
#statHard {
  color: var(--hard);
}
#statGood {
  color: var(--good);
}
#statEasy {
  color: var(--easy);
}
#statUnseen {
  color: var(--text-dim);
}

/* Practice Mode */
.practice-container {
  width: 100%;
  max-width: 420px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.practice-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: 0 4px;
}

.practice-kanji-info {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.practice-keyword {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--accent);
  text-transform: capitalize;
}

.practice-number {
  font-size: 0.85rem;
  color: var(--text-dim);
  font-weight: 600;
}

.practice-stroke-info {
  font-size: 0.85rem;
  color: var(--text-dim);
  font-weight: 600;
}

.practice-guide-label {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 0 4px;
  font-size: 0.85rem;
  color: var(--text-dim);
  cursor: pointer;
  user-select: none;
}

.practice-guide-label input {
  width: 1rem;
  height: 1rem;
  accent-color: var(--accent);
  cursor: pointer;
}

.practice-area {
  width: 100%;
  aspect-ratio: 1 / 1;
  max-height: 420px;
}

.sp-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: var(--radius);
  background: linear-gradient(145deg, var(--card-bg), var(--surface));
  border: 2px solid var(--card-border);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.sp-guide {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  transition:
    opacity 0.2s ease,
    visibility 0.2s ease;
}

.sp-wrap--hide-guide .sp-guide {
  visibility: hidden;
  opacity: 0;
  /* No transition while hidden: a fade-out overlaps async load/_buildGuide and flashes the next kanji. */
  transition: none;
}

.sp-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  cursor: crosshair;
}

.practice-hint {
  font-size: 0.85rem;
  color: var(--warning);
  min-height: 1.3em;
  text-align: center;
  transition: opacity 0.3s;
}

.practice-hint.success {
  color: var(--success);
}

.practice-controls {
  display: flex;
  gap: 10px;
  justify-content: center;
}

/* Footer */
footer {
  text-align: center;
  color: var(--text-dim);
  font-size: 0.8rem;
  padding-top: 8px;
  border-top: 1px solid rgba(255, 255, 255, 0.05);
  width: 100%;
}

footer p {
  margin: 4px 0;
}

.shortcut-help {
  font-size: 0.75rem;
}

kbd {
  display: inline-block;
  padding: 2px 6px;
  font-size: 0.7rem;
  font-family: inherit;
  background: var(--surface);
  border: 1px solid var(--card-border);
  border-radius: 4px;
  margin: 0 1px;
}

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

/* Mobile */
@media (max-width: 480px) {
  #app {
    padding: 16px 12px;
    gap: 14px;
  }

  header h1 {
    font-size: 1.4rem;
  }

  .card-main {
    font-size: 6rem;
  }

  .card-english {
    font-size: 1.8rem;
  }

  .flashcard {
    max-height: 380px;
  }

  .controls-top {
    flex-direction: column;
  }

  .btn-nav {
    padding: 10px 18px;
  }

  /* Keep Prev / Shuffle / Reset / Next on one row */
  .controls-bottom {
    flex-wrap: nowrap;
    gap: 6px;
    width: 100%;
    max-width: 420px;
  }

  .controls-bottom .btn {
    flex: 1 1 0;
    min-width: 0;
    padding: 10px 6px;
    font-size: 0.82rem;
  }

  .grade-buttons {
    flex-wrap: wrap;
  }

  .practice-area {
    max-height: 340px;
  }
}
