:root {
  --bg: #0f172a;
  --bg-elev: #1a2540;
  --bg-elev-2: #22304f;
  --text: #f1f5f9;
  --text-dim: #94a3b8;
  --primary: #3b82f6;
  --primary-dim: #1d4ed8;
  --correct: #22c55e;
  --wrong: #ef4444;
  --border: #2b3a5c;
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --heat-0: #22304f;
  --heat-1: #1e3a5f;
  --heat-2: #1d4ed8;
  --heat-3: #3b82f6;
  --heat-4: #7dabfb;
  --chrome-bg: rgba(15, 23, 42, 0.92);
}

:root[data-theme="light"] {
  --bg: #f9f9f7;
  --bg-elev: #ffffff;
  --bg-elev-2: #eef1f6;
  --text: #10131a;
  --text-dim: #5b6472;
  --primary: #2a63d6;
  --primary-dim: #1d4ed8;
  --correct: #0ca30c;
  --wrong: #d03b3b;
  --border: #dbe0e8;
  --heat-0: #eef1f6;
  --heat-1: #cde2fb;
  --heat-2: #6da7ec;
  --heat-3: #2a78d6;
  --heat-4: #184f95;
  --chrome-bg: rgba(249, 249, 247, 0.92);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic", sans-serif;
  overscroll-behavior-y: none;
}

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  padding: calc(10px + var(--safe-top)) 16px 10px;
  background: var(--chrome-bg);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.topbar h1 {
  font-size: 18px;
  margin: 0;
}

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

.header-stats {
  font-size: 13px;
  color: var(--text-dim);
}

.theme-toggle {
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  border-radius: 999px;
  width: 32px;
  height: 32px;
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

main#app {
  flex: 1;
  padding: 14px 14px calc(90px + var(--safe-bottom));
  max-width: 560px;
  width: 100%;
  margin: 0 auto;
}

.view.hidden { display: none; }

.card-panel {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
}

.card-panel h2 {
  margin: 0 0 10px;
  font-size: 15px;
  color: var(--text-dim);
  font-weight: 600;
}

.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.stat-item {
  background: var(--bg-elev-2);
  border-radius: 10px;
  padding: 12px;
  text-align: center;
}

.stat-item .num {
  font-size: 24px;
  font-weight: 700;
}

.stat-item .label {
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 2px;
}

.box-dist {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.box-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--text-dim);
}

.box-row .box-label { width: 84px; flex-shrink: 0; }
.box-bar-track {
  flex: 1;
  height: 10px;
  background: var(--bg-elev-2);
  border-radius: 6px;
  overflow: hidden;
}
.box-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary-dim), var(--primary));
  border-radius: 6px;
}
.box-row .box-count { width: 28px; text-align: right; }

.home-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.btn {
  border: none;
  border-radius: 12px;
  padding: 13px 16px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  color: var(--text);
  background: var(--bg-elev-2);
  transition: transform 0.05s ease;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.6; cursor: default; }

.btn-primary { background: var(--primary); color: white; }
.btn-secondary { background: var(--bg-elev-2); color: var(--text); border: 1px solid var(--border); }
.btn-danger { background: #7f1d1d; color: #fecaca; }
.btn-correct { background: var(--correct); color: white; }
.btn-wrong { background: var(--wrong); color: white; }
.btn-lg { padding: 16px; font-size: 16px; width: 100%; position: relative; }

.badge {
  display: inline-block;
  margin-left: 8px;
  background: rgba(255,255,255,0.25);
  border-radius: 999px;
  padding: 1px 8px;
  font-size: 12px;
}

.mode-select {
  display: flex;
  gap: 8px;
  margin-bottom: 14px;
  overflow-x: auto;
}

.chip {
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--text-dim);
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 13px;
  white-space: nowrap;
  cursor: pointer;
}
.chip.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.progress-line {
  text-align: center;
  color: var(--text-dim);
  font-size: 13px;
  margin-bottom: 10px;
}

.audio-row {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 10px;
}

.audio-toggle {
  padding: 8px 14px;
  font-size: 13px;
}
.audio-toggle.active {
  background: rgba(59, 130, 246, 0.18);
  border-color: var(--primary);
  color: var(--text);
}

.section-panel {
  margin-bottom: 14px;
}

.section-back-btn {
  width: 100%;
  margin-bottom: 14px;
}

.section-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 360px;
  overflow-y: auto;
  margin-top: 4px;
}

.section-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  color: var(--text);
  cursor: pointer;
  text-align: left;
}
.section-item.active {
  border-color: var(--primary);
  background: rgba(59, 130, 246, 0.15);
}
.section-item-label {
  font-weight: 600;
  font-size: 14px;
}
.section-item-range {
  display: block;
  font-weight: 400;
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 2px;
}
.section-item-runs {
  font-size: 13px;
  color: var(--text-dim);
  flex-shrink: 0;
  padding-left: 10px;
  white-space: nowrap;
}

.section-bars {
  max-height: 320px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 6px;
}

.laps-line {
  font-size: 14px;
  color: var(--text-dim);
  margin: 6px 0 10px;
}
.laps-line span {
  font-size: 20px;
  font-weight: 800;
  color: var(--text);
}

.history-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}
.history-head h2 { margin: 0; }

.streak-badge {
  font-size: 13px;
  font-weight: 700;
  color: var(--text);
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 10px;
}

.heatmap {
  display: grid;
  grid-template-columns: repeat(14, 1fr);
  gap: 3px;
}

.heat-cell {
  aspect-ratio: 1 / 1;
  border-radius: 3px;
  background: var(--heat-0);
}
.heat-cell.level-1 { background: var(--heat-1); }
.heat-cell.level-2 { background: var(--heat-2); }
.heat-cell.level-3 { background: var(--heat-3); }
.heat-cell.level-4 { background: var(--heat-4); }
.heat-cell.is-today { box-shadow: 0 0 0 1.5px var(--text) inset; }

.heatmap-legend {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-top: 8px;
  font-size: 11px;
  color: var(--text-dim);
}

.heat-swatch {
  width: 11px;
  height: 11px;
  border-radius: 3px;
  background: var(--heat-0);
  display: inline-block;
}
.heat-swatch.level-1 { background: var(--heat-1); }
.heat-swatch.level-2 { background: var(--heat-2); }
.heat-swatch.level-3 { background: var(--heat-3); }
.heat-swatch.level-4 { background: var(--heat-4); }

.weekly-stats {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.weekly-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
}

.weekly-label { color: var(--text-dim); }

.weekly-value {
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 6px;
}

.wk-diff {
  font-size: 12px;
  font-weight: 700;
  border-radius: 999px;
  padding: 2px 8px;
}
.wk-diff.up { background: rgba(34, 197, 94, 0.18); color: var(--correct); }
.wk-diff.down { background: rgba(239, 68, 68, 0.18); color: var(--wrong); }
.wk-diff.flat { background: var(--bg-elev-2); color: var(--text-dim); }

.achievement-rows {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.ach-row-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 6px;
  font-size: 13px;
}

.ach-row-title { color: var(--text); font-weight: 600; }

.ach-level {
  font-size: 11px;
  font-weight: 700;
  color: var(--primary);
  background: rgba(59, 130, 246, 0.15);
  border-radius: 999px;
  padding: 2px 8px;
}

.ach-row-count {
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 6px;
}

.flashcard {
  position: relative;
  perspective: 1200px;
  height: 320px;
  margin-bottom: 18px;
  cursor: pointer;
  border-radius: 18px;
}

.flashcard-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.35s ease;
}

.flashcard.correct-flash {
  animation: flashCorrectPulse 0.45s ease;
}
.flashcard.wrong-flash {
  animation: flashWrongShake 0.45s ease;
}

@keyframes flashCorrectPulse {
  0% { box-shadow: 0 0 0 0 rgba(34, 197, 94, 0); }
  30% { box-shadow: 0 0 0 10px rgba(34, 197, 94, 0.35); }
  100% { box-shadow: 0 0 0 0 rgba(34, 197, 94, 0); }
}

@keyframes flashWrongShake {
  0% { transform: translateX(0); box-shadow: 0 0 0 0 rgba(239, 68, 68, 0); }
  20% { transform: translateX(-8px); box-shadow: 0 0 0 10px rgba(239, 68, 68, 0.35); }
  40% { transform: translateX(8px); }
  60% { transform: translateX(-5px); }
  80% { transform: translateX(5px); }
  100% { transform: translateX(0); box-shadow: 0 0 0 0 rgba(239, 68, 68, 0); }
}

.flash-result-icon {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 96px;
  font-weight: 900;
  pointer-events: none;
  opacity: 0;
  z-index: 5;
}
.flash-result-icon.correct { color: var(--correct); }
.flash-result-icon.wrong { color: var(--wrong); }
.flash-result-icon.show {
  animation: flashIconPop 0.45s ease;
}

@keyframes flashIconPop {
  0% { opacity: 0; transform: scale(0.4); }
  35% { opacity: 1; transform: scale(1.15); }
  55% { transform: scale(1); }
  100% { opacity: 0; transform: scale(1); }
}

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

.flashcard.no-flip-anim .flashcard-inner {
  transition: none;
}

.flashcard-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  border-radius: 18px;
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  text-align: center;
}

.flashcard-front {
  background: linear-gradient(160deg, var(--bg-elev), var(--bg-elev-2));
}

.flashcard-back {
  background: linear-gradient(160deg, var(--primary-dim), #1e3a8a);
  transform: rotateY(180deg);
}

.word-main {
  font-size: 34px;
  font-weight: 800;
  word-break: break-word;
}

.word-pos {
  color: var(--text-dim);
  font-size: 13px;
  margin-top: 6px;
}

.tap-hint {
  position: absolute;
  bottom: 18px;
  font-size: 12px;
  color: var(--text-dim);
}

.meaning-main {
  font-size: 26px;
  font-weight: 700;
  color: white;
}

.example {
  margin-top: 14px;
  font-size: 14px;
  color: #dbeafe;
  line-height: 1.5;
}

.answer-buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.quiz-word {
  position: relative;
  font-size: 30px;
  font-weight: 800;
  text-align: center;
  margin: 24px 0 18px;
}

.star-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 2;
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid var(--border);
  color: #fbbf24;
  font-size: 20px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.star-btn.starred { background: rgba(251, 191, 36, 0.18); }

.star-btn-left {
  left: 10px;
  right: auto;
  color: #93c5fd;
}

.star-btn-inline {
  position: static;
  display: inline-flex;
  width: 34px;
  height: 34px;
  font-size: 17px;
  vertical-align: middle;
  margin-right: 8px;
  background: var(--bg-elev-2);
}

.result-list {
  list-style: none;
  padding: 0;
  margin: 12px 0;
}
.result-list li {
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
  display: flex;
  justify-content: space-between;
}
.result-list li.wrong-item { color: #fca5a5; }
.result-list li.correct-item { color: #86efac; }

.mybook-pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin-bottom: 14px;
}

.mybook-nav-btn {
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: 50%;
  font-size: 20px;
  line-height: 1;
  flex: none;
}

.mybook-page-label {
  font-size: 14px;
  font-weight: 700;
  color: var(--text-dim);
  min-width: 96px;
  text-align: center;
}

.mybook-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.masu {
  position: relative;
  perspective: 1000px;
  aspect-ratio: 1;
  cursor: pointer;
}

.masu-empty {
  cursor: default;
  border-radius: 12px;
  border: 1px dashed var(--border);
}

.masu-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.35s ease;
}

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

.masu-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  border-radius: 12px;
  border: 1px solid var(--border);
  padding: 6px;
  overflow: hidden;
}

.masu-front {
  background: var(--bg-elev);
}

.masu-back {
  background: linear-gradient(160deg, var(--primary-dim), #1e3a8a);
  transform: rotateY(180deg);
}

.masu-list {
  list-style: none;
  margin: 0;
  padding: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 2px;
}

.masu-list li {
  font-size: 11px;
  line-height: 1.25;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text);
}

.masu-back .masu-list li {
  color: white;
}

.mybook-count-hint {
  text-align: center;
  margin-top: 14px;
}

.write-input, .text-input {
  width: 100%;
  padding: 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--text);
  font-size: 16px;
  margin-bottom: 10px;
}

.write-feedback {
  margin-top: 12px;
  text-align: center;
  font-size: 15px;
  min-height: 22px;
}
.write-feedback.ok { color: var(--correct); }
.write-feedback.ng { color: var(--wrong); }

.quiz-choices {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 10px;
}

.quiz-choice-btn {
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--text);
  border-radius: 12px;
  padding: 14px 16px;
  font-size: 15px;
  text-align: left;
  cursor: pointer;
}
.quiz-choice-btn:disabled { cursor: default; }
.quiz-choice-btn.correct-choice { background: var(--correct); color: white; border-color: var(--correct); }
.quiz-choice-btn.wrong-choice { background: var(--wrong); color: white; border-color: var(--wrong); }

.import-textarea {
  width: 100%;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--text);
  padding: 12px;
  font-size: 14px;
  margin-bottom: 10px;
  resize: vertical;
}

.hint {
  color: var(--text-dim);
  font-size: 12px;
  line-height: 1.5;
}

.import-file-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}

.file-label { display: inline-block; cursor: pointer; }

.import-actions {
  display: flex;
  gap: 10px;
}
.import-actions .btn { flex: 1; }

.word-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 10px;
  max-height: 420px;
  overflow-y: auto;
}

.word-row {
  background: var(--bg-elev-2);
  border-radius: 10px;
  padding: 10px 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.word-row .wr-main { flex: 1; min-width: 0; }
.word-row .wr-word { font-weight: 700; }
.word-row .wr-meaning { color: var(--text-dim); font-size: 13px; }
.word-row .wr-star {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 18px;
  cursor: pointer;
  padding: 4px 6px;
  flex-shrink: 0;
}
.word-row .wr-star.starred { color: #fbbf24; }
.word-row .wr-del {
  background: none;
  border: none;
  color: #fca5a5;
  font-size: 18px;
  cursor: pointer;
  padding: 4px 8px;
}

.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  background: var(--chrome-bg);
  backdrop-filter: blur(10px);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
  z-index: 20;
}

.tab-btn {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-dim);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 0 6px;
  font-size: 11px;
  cursor: pointer;
}

.tab-btn .tab-icon { font-size: 20px; }

.tab-btn.active { color: var(--primary); }

.hidden { display: none !important; }
