/* ====================================================================
   CSS Custom Properties (テーマ変数)
   ==================================================================== */
:root {
  --bg:          #0f2318;
  --bg-card:     #1a3a2a;
  --bg-surface:  #213d2d;
  --bg-elevated: #2a5040;
  --accent:      #f0c040;
  --accent-dim:  #c89e20;
  --green-light: #4caf6a;
  --green-mid:   #2e8b57;
  --red:         #e05555;
  --text:        #e8f4ee;
  --text-dim:    #8ab89a;
  --text-muted:  #5a8870;
  --radius:      12px;
  --shadow:      0 4px 20px rgba(0,0,0,0.4);
  --transition:  0.2s ease;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg:          #f0f7f3;
    --bg-card:     #ffffff;
    --bg-surface:  #e8f4ee;
    --bg-elevated: #d0e8da;
    --text:        #1a3a2a;
    --text-dim:    #3a6a4a;
    --text-muted:  #6a9a7a;
    --shadow:      0 4px 20px rgba(0,0,0,0.12);
  }
}

/* ====================================================================
   リセット・ベース
   ==================================================================== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: 16px; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Hiragino Sans',
               'Noto Sans JP', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow-x: hidden;
}

/* ====================================================================
   画面システム（SPA）
   ==================================================================== */
.screen {
  display: none;
  min-height: 100vh;
  padding: 0 0 env(safe-area-inset-bottom);
}
.screen.active { display: flex; flex-direction: column; }

/* ====================================================================
   ホーム画面
   ==================================================================== */
#screen-home {
  max-width: 480px;
  margin: 0 auto;
  width: 100%;
}

#screen-home header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 20px 0;
  gap: 12px;
}

.streak {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-surface);
  padding: 8px 14px;
  border-radius: 20px;
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--accent);
  flex-shrink: 0;
}
.streak-icon { font-size: 1.3rem; }

.level-bar {
  flex: 1;
  min-width: 0;
}
.level-row {
  display: flex;
  justify-content: space-between;
  font-size: 0.78rem;
  color: var(--text-dim);
  margin-bottom: 4px;
}
#user-level { font-weight: 700; color: var(--accent); }
.xp-track {
  height: 8px;
  background: var(--bg-elevated);
  border-radius: 4px;
  overflow: hidden;
}
#xp-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--green-mid), var(--accent));
  border-radius: 4px;
  transition: width 0.4s ease;
  width: 0%;
}

#screen-home main {
  flex: 1;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

#screen-home h1 {
  font-size: 1.7rem;
  font-weight: 800;
  color: var(--text);
  text-align: center;
  letter-spacing: -0.5px;
}

/* 復習バッジ */
.review-badge {
  background: var(--bg-surface);
  border: 1px solid var(--green-mid);
  border-radius: var(--radius);
  padding: 12px 16px;
  text-align: center;
  font-size: 0.9rem;
  color: var(--green-light);
  display: none;
}

/* モードボタン */
.mode-buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.mode-btn {
  background: var(--bg-card);
  border: 2px solid var(--bg-elevated);
  border-radius: var(--radius);
  padding: 18px 12px;
  cursor: pointer;
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 600;
  transition: var(--transition);
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}
.mode-btn .mode-icon { font-size: 1.8rem; }
.mode-btn:hover:not(:disabled) {
  border-color: var(--green-mid);
  background: var(--bg-surface);
  transform: translateY(-2px);
  box-shadow: var(--shadow);
}
.mode-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
.mode-btn:nth-child(1) { grid-column: 1 / -1; background: var(--bg-elevated); border-color: var(--green-mid); }
.mode-badge {
  position: absolute;
  top: 8px;
  right: 8px;
  background: var(--accent);
  color: #000;
  border-radius: 10px;
  padding: 2px 7px;
  font-size: 0.72rem;
  font-weight: 700;
  display: none;
}

/* 統計ミニ */
.stats-mini {
  display: flex;
  justify-content: space-around;
  background: var(--bg-surface);
  border-radius: var(--radius);
  padding: 16px 0;
}
.stats-mini .stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.stats-mini .stat span {
  font-size: 1.6rem;
  font-weight: 800;
  color: var(--accent);
}
.stats-mini .stat small {
  font-size: 0.72rem;
  color: var(--text-dim);
}

#go-stats {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.85rem;
  text-decoration: none;
}
#go-stats:hover { color: var(--text-dim); }

/* ====================================================================
   クイズ画面
   ==================================================================== */
#screen-quiz {
  max-width: 480px;
  margin: 0 auto;
  width: 100%;
  padding-bottom: 20px;
}

.quiz-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 16px 0;
}

.btn-icon {
  background: var(--bg-surface);
  border: none;
  color: var(--text-dim);
  width: 36px;
  height: 36px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 1rem;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: var(--transition);
}
.btn-icon:hover { background: var(--bg-elevated); color: var(--text); }

.progress-bar {
  flex: 1;
  height: 8px;
  background: var(--bg-elevated);
  border-radius: 4px;
  overflow: hidden;
}
.progress-bar > div {
  height: 100%;
  background: var(--green-mid);
  border-radius: 4px;
  transition: width 0.3s ease;
  width: 0%;
}

.quiz-header span { font-size: 0.8rem; color: var(--text-dim); flex-shrink: 0; }

/* タイマーバー */
#timer-bar {
  height: 4px;
  background: var(--bg-elevated);
  margin: 10px 16px 0;
  border-radius: 2px;
  overflow: hidden;
}
#timer-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--red), var(--accent));
  width: 100%;
  transition: width 10s linear;
}

.quiz-main {
  flex: 1;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* 問題カード */
.card {
  background: var(--bg-card);
  border-radius: 16px;
  padding: 28px 24px 24px;
  text-align: center;
  box-shadow: var(--shadow);
}
.word-phonetic {
  font-size: 0.9rem;
  color: var(--text-muted);
  margin-bottom: 4px;
  font-family: 'Courier New', monospace;
}
.word-display {
  font-size: 2.4rem;
  font-weight: 800;
  color: var(--text);
  letter-spacing: -1px;
  line-height: 1.1;
  word-break: break-all;
}
.word-pos {
  font-size: 0.82rem;
  color: var(--text-dim);
  margin-top: 6px;
  font-style: italic;
}

/* 4択ボタン */
.choices {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.choice-btn {
  background: var(--bg-card);
  border: 2px solid var(--bg-elevated);
  border-radius: 10px;
  padding: 14px 10px;
  color: var(--text);
  font-size: 0.9rem;
  cursor: pointer;
  transition: var(--transition);
  line-height: 1.3;
}
.choice-btn:hover:not(:disabled) {
  border-color: var(--green-mid);
  background: var(--bg-surface);
}
.choice-btn.correct {
  background: rgba(76, 175, 106, 0.2);
  border-color: var(--green-light);
  color: var(--green-light);
}
.choice-btn.incorrect {
  background: rgba(224, 85, 85, 0.2);
  border-color: var(--red);
  color: var(--red);
}
.choice-btn:disabled { cursor: default; }

/* タイピング入力 */
.typing-input {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.typing-input input {
  background: var(--bg-card);
  border: 2px solid var(--bg-elevated);
  border-radius: 10px;
  padding: 14px 16px;
  color: var(--text);
  font-size: 1.2rem;
  outline: none;
  transition: var(--transition);
}
.typing-input input:focus { border-color: var(--green-mid); }
.typing-input input.correct { border-color: var(--green-light); background: rgba(76,175,106,0.1); }
.typing-input input.incorrect { border-color: var(--red); background: rgba(224,85,85,0.1); }
.typing-input input::placeholder { color: var(--text-muted); }

.hint {
  font-size: 0.85rem;
  color: var(--accent);
  font-family: 'Courier New', monospace;
  letter-spacing: 2px;
}

.typing-input button {
  padding: 12px;
  border-radius: 10px;
  border: none;
  cursor: pointer;
  font-size: 0.95rem;
  font-weight: 600;
  transition: var(--transition);
}
#typing-submit {
  background: var(--green-mid);
  color: white;
}
#typing-submit:hover { background: var(--green-light); }
#typing-hint-btn {
  background: var(--bg-elevated);
  color: var(--text-dim);
}
#typing-hint-btn:hover { background: var(--bg-surface); color: var(--text); }

/* フィードバック */
.feedback {
  background: var(--bg-card);
  border-radius: 14px;
  padding: 18px;
  box-shadow: var(--shadow);
}
.fb-correct { font-size: 1.5rem; color: var(--green-light); }
.fb-incorrect { font-size: 1.5rem; color: var(--red); }

#feedback-message {
  margin: 8px 0 6px;
  font-size: 0.95rem;
  font-weight: 600;
}

.example-sentence {
  font-size: 0.82rem;
  color: var(--text-dim);
  font-style: italic;
  margin-bottom: 12px;
}

#feedback-next {
  width: 100%;
  padding: 12px;
  background: var(--green-mid);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  transition: var(--transition);
}
#feedback-next:hover { background: var(--green-light); }

/* XPポップアップ */
.xp-popup {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: var(--accent);
  color: #000;
  font-size: 1.5rem;
  font-weight: 800;
  padding: 10px 24px;
  border-radius: 20px;
  pointer-events: none;
  z-index: 100;
  opacity: 0;
}
.xp-popup.pop {
  animation: xpPop 0.9s ease forwards;
}
@keyframes xpPop {
  0%   { opacity: 0; transform: translate(-50%, -50%) scale(0.5); }
  30%  { opacity: 1; transform: translate(-50%, -60%) scale(1.1); }
  70%  { opacity: 1; transform: translate(-50%, -70%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -80%) scale(0.9); }
}

/* ====================================================================
   フラッシュカード画面
   ==================================================================== */
#screen-flashcard {
  max-width: 480px;
  margin: 0 auto;
  width: 100%;
}

.flashcard-main {
  flex: 1;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  justify-content: center;
  align-items: center;
}

/* カードフリップ */
.flashcard {
  width: 100%;
  max-width: 360px;
  height: 220px;
  perspective: 1000px;
  cursor: pointer;
}
.flashcard-inner {
  width: 100%;
  height: 100%;
  position: relative;
  transform-style: preserve-3d;
  transition: transform 0.5s ease;
}
.flashcard.flipped .flashcard-inner {
  transform: rotateY(180deg);
}
.flashcard-front,
.flashcard-back {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  border-radius: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  text-align: center;
  box-shadow: var(--shadow);
}
.flashcard-front {
  background: var(--bg-card);
}
.flashcard-back {
  background: var(--bg-elevated);
  transform: rotateY(180deg);
}
.flashcard-front .word-display { font-size: 2.5rem; }
.word-meaning {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 10px;
}
.tap-hint {
  font-size: 0.78rem;
  color: var(--text-muted);
  margin-top: 12px;
}

/* 自己採点ボタン */
.fc-buttons {
  display: flex;
  gap: 14px;
  width: 100%;
  max-width: 360px;
}
.fc-btn {
  flex: 1;
  padding: 16px;
  border: none;
  border-radius: 12px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  transition: var(--transition);
}
.fc-btn.forgot {
  background: rgba(224,85,85,0.15);
  color: var(--red);
  border: 2px solid var(--red);
}
.fc-btn.forgot:hover { background: rgba(224,85,85,0.3); }
.fc-btn.knew {
  background: var(--green-mid);
  color: white;
}
.fc-btn.knew:hover { background: var(--green-light); }

/* ====================================================================
   結果画面
   ==================================================================== */
#screen-result {
  max-width: 480px;
  margin: 0 auto;
  width: 100%;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.result-content {
  background: var(--bg-card);
  border-radius: 20px;
  padding: 32px 24px;
  width: 100%;
  box-shadow: var(--shadow);
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.result-title {
  font-size: 1.4rem;
  font-weight: 800;
}

.result-score {
  font-size: 3rem;
  font-weight: 800;
  color: var(--accent);
  line-height: 1;
}
.result-score small {
  font-size: 1rem;
  color: var(--text-dim);
  display: block;
  margin-top: 4px;
}

.result-accuracy { font-size: 1rem; color: var(--text-dim); }

.result-xp {
  font-size: 1.2rem;
  font-weight: 700;
  color: var(--accent);
}

.mistakes h3 {
  font-size: 0.9rem;
  color: var(--text-dim);
  text-align: left;
  margin-bottom: 10px;
}
.mistakes ul {
  list-style: none;
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 160px;
  overflow-y: auto;
}
.mistakes li {
  background: var(--bg-surface);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 0.85rem;
}
.mistakes .phonetic {
  font-size: 0.78rem;
  color: var(--text-muted);
  font-family: 'Courier New', monospace;
  margin: 0 4px;
}

.result-buttons {
  display: flex;
  gap: 10px;
}
.result-buttons button {
  flex: 1;
  padding: 14px;
  border: none;
  border-radius: 10px;
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
  transition: var(--transition);
}
#result-retry {
  background: var(--green-mid);
  color: white;
}
#result-retry:hover { background: var(--green-light); }
#result-home {
  background: var(--bg-elevated);
  color: var(--text);
}
#result-home:hover { background: var(--bg-surface); }

/* ====================================================================
   統計画面
   ==================================================================== */
#screen-stats {
  max-width: 480px;
  margin: 0 auto;
  width: 100%;
}

.stats-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 20px 16px 0;
}
.stats-header h2 { font-size: 1.2rem; font-weight: 700; }

.stats-content {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.stat-card {
  background: var(--bg-card);
  border-radius: 14px;
  padding: 20px 16px;
  text-align: center;
  box-shadow: var(--shadow);
}
.stat-value {
  font-size: 2rem;
  font-weight: 800;
  color: var(--accent);
}
.stat-label {
  font-size: 0.78rem;
  color: var(--text-dim);
  margin-top: 4px;
}

.category-progress h3 {
  font-size: 0.9rem;
  color: var(--text-dim);
  margin-bottom: 12px;
}

.cat-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}
.cat-label {
  width: 48px;
  font-size: 0.8rem;
  color: var(--text-dim);
  flex-shrink: 0;
}
.cat-bar-bg {
  flex: 1;
  height: 12px;
  background: var(--bg-elevated);
  border-radius: 6px;
  overflow: hidden;
  position: relative;
}
.cat-bar-mastered,
.cat-bar-learned {
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  border-radius: 6px;
  transition: width 0.5s ease;
}
.cat-bar-mastered { background: var(--accent); z-index: 2; }
.cat-bar-learned  { background: var(--green-mid); z-index: 1; }
.cat-count {
  font-size: 0.78rem;
  color: var(--text-muted);
  width: 36px;
  text-align: right;
  flex-shrink: 0;
}

/* ====================================================================
   レベルアップオーバーレイ
   ==================================================================== */
.levelup-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  animation: fadeIn 0.3s ease;
}
.levelup-overlay.hidden { display: none; }

.levelup-content {
  background: var(--bg-card);
  border-radius: 24px;
  padding: 40px 48px;
  text-align: center;
  box-shadow: 0 0 40px rgba(240,192,64,0.3);
  animation: scaleIn 0.4s ease;
}

.levelup-title {
  font-size: 1rem;
  color: var(--accent);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 2px;
}
.levelup-level {
  font-size: 3.5rem;
  font-weight: 900;
  color: var(--accent);
  margin: 8px 0;
}
.levelup-title2 {
  font-size: 1.1rem;
  color: var(--text-dim);
}

@keyframes fadeIn  { from { opacity: 0; } to { opacity: 1; } }
@keyframes scaleIn { from { transform: scale(0.7); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* ====================================================================
   ユーティリティ
   ==================================================================== */
.hidden { display: none !important; }

/* ====================================================================
   レスポンシブ（タブレット以上）
   ==================================================================== */
@media (min-width: 600px) {
  #screen-home, #screen-quiz, #screen-flashcard,
  #screen-result, #screen-stats {
    padding-top: 20px;
  }
  .flashcard { height: 260px; }
  .word-display { font-size: 2.8rem; }
}

/* スクロールバー */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--bg-elevated); border-radius: 3px; }
