:root {
  --primary: #7c3aed;
  --primary-dark: #5b21b6;
  --accent: #f97316;
  --good: #22c55e;
  --bad: #ef4444;
  --bg-1: #fef3c7;
  --bg-2: #fce7f3;
  --text: #1f2937;
  --muted: #6b7280;
  --card: #ffffff;
  --shadow: 0 12px 30px rgba(0, 0, 0, 0.1);
  --radius: 18px;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  font-family: "Comic Sans MS", "Segoe UI", "Microsoft JhengHei", system-ui, sans-serif;
  color: var(--text);
  min-height: 100%;
}

body {
  background: linear-gradient(135deg, var(--bg-1), var(--bg-2));
  min-height: 100vh;
}

.container {
  max-width: 960px;
  margin: 0 auto;
  padding: 24px 20px 48px;
}

header.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 28px;
}

.app-header h1 {
  font-size: 28px;
  margin: 0;
  color: var(--primary-dark);
}

.app-header h1 a {
  color: inherit;
  text-decoration: none;
}

.subtitle {
  color: var(--muted);
  font-size: 14px;
  margin-top: 4px;
}

.toolbar {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.source-switch {
  display: inline-flex;
  background: white;
  border-radius: 12px;
  padding: 4px;
  box-shadow: 0 4px 0 #d1d5db;
}
.source-switch button {
  background: transparent;
  color: var(--muted);
  box-shadow: none;
  padding: 8px 14px;
  font-weight: 700;
  border-radius: 8px;
}
.source-switch button.active {
  background: var(--primary);
  color: white;
  box-shadow: none;
}
.source-switch button:active { transform: none; }

select, input[type="text"] {
  font-size: 16px;
  padding: 10px 14px;
  border: 2px solid #e5e7eb;
  border-radius: 12px;
  background: white;
  outline: none;
  transition: border-color 0.15s;
}

.toolbar #tag-select { width: 180px; }
.toolbar #difficulty-select { width: 150px; }

select:focus, input[type="text"]:focus {
  border-color: var(--primary);
}

button {
  font-family: inherit;
  font-size: 16px;
  font-weight: 700;
  padding: 12px 18px;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  background: var(--primary);
  color: white;
  box-shadow: 0 4px 0 var(--primary-dark);
  transition: transform 0.05s, box-shadow 0.05s;
}

button:hover { filter: brightness(1.05); }
button:active { transform: translateY(2px); box-shadow: 0 2px 0 var(--primary-dark); }
button.ghost { background: white; color: var(--primary-dark); box-shadow: 0 4px 0 #d1d5db; }
button.accent { background: var(--accent); box-shadow: 0 4px 0 #c2410c; }
button.good { background: var(--good); box-shadow: 0 4px 0 #15803d; }
button.bad  { background: var(--bad);  box-shadow: 0 4px 0 #b91c1c; }
button.icon {
  width: 56px; height: 56px; padding: 0; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 28px;
}
button:disabled { opacity: 0.5; cursor: not-allowed; }

/* 主選單卡片 */
.mode-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 20px;
  margin-top: 20px;
}

.mode-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 28px 24px;
  text-align: center;
  text-decoration: none;
  color: var(--text);
  box-shadow: var(--shadow);
  transition: transform 0.15s;
  border: 3px solid transparent;
}

.mode-card:hover {
  transform: translateY(-4px);
  border-color: var(--primary);
}

.mode-card .emoji { font-size: 56px; display: block; margin-bottom: 8px; }
.mode-card h2 { margin: 0 0 6px; color: var(--primary-dark); }
.mode-card p  { margin: 0; color: var(--muted); font-size: 14px; }

/* 練習區共用 */
.stage {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 32px 24px;
  margin-top: 20px;
  text-align: center;
}

.progress-bar {
  height: 12px;
  width: 100%;
  background: #e5e7eb;
  border-radius: 999px;
  overflow: hidden;
  margin-bottom: 20px;
}
.progress-bar .fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), var(--accent));
  width: 0%;
  transition: width 0.25s;
}

.score-line {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
  color: var(--muted);
  margin-bottom: 8px;
}

/* 閃卡 */
.flashcard {
  perspective: 1000px;
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
}
.flashcard-inner {
  position: relative;
  width: 100%;
  height: 280px;
  transition: transform 0.55s;
  transform-style: preserve-3d;
  cursor: pointer;
}
.flashcard.flipped .flashcard-inner { transform: rotateY(180deg); }
.flashcard-face {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
  backface-visibility: hidden;
  padding: 20px;
}
.flashcard-front { background: linear-gradient(135deg, #ddd6fe, #fbcfe8); }
.flashcard-back  { background: linear-gradient(135deg, #bfdbfe, #bbf7d0); transform: rotateY(180deg); }
.flashcard .word { font-size: 44px; font-weight: 800; color: var(--primary-dark); }
.flashcard .pos  { font-style: italic; color: var(--muted); margin-top: 6px; }
.flashcard .meaning { font-size: 28px; font-weight: 700; color: var(--text); }
.flashcard .example { font-size: 16px; color: var(--muted); margin-top: 10px; }

.actions {
  margin-top: 20px;
  display: flex;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
}

/* 家長測試模式 */
.preview-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: white;
  padding: 8px 14px;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
  cursor: pointer;
  user-select: none;
  box-shadow: 0 4px 0 #d1d5db;
  border: 2px solid transparent;
}
.preview-toggle input { width: 18px; height: 18px; cursor: pointer; accent-color: #f59e0b; }
.preview-toggle.on { background: #fef3c7; border-color: #f59e0b; color: #92400e; }
.preview-badge {
  background: #fef3c7;
  border: 2px dashed #f59e0b;
  color: #92400e;
  font-weight: 700;
  padding: 8px 14px;
  border-radius: 12px;
  margin: 0 0 14px;
  text-align: center;
  font-size: 14px;
}

/* 練習範圍提示列 */
.scope {
  display: inline-block;
  background: #ede9fe;
  color: var(--primary-dark);
  font-size: 14px;
  font-weight: 700;
  padding: 6px 14px;
  border-radius: 999px;
  margin: 0 0 14px;
  letter-spacing: 0.5px;
}

/* 拼字 */
.spell-question { font-size: 22px; color: var(--text); margin-bottom: 12px; }
.spell-pad {
  position: relative;
  margin: 0 auto;
  max-width: 560px;
}
.spell-pad::before {
  content: "✏️ 用筆在這裡寫";
  position: absolute;
  top: 8px;
  left: 14px;
  font-size: 13px;
  color: var(--muted);
  pointer-events: none;
  font-weight: 600;
  letter-spacing: 0;
}
input.spell-input {
  font-family: ui-rounded, "SF Pro Rounded", -apple-system, "Hiragino Maru Gothic ProN", "Comic Sans MS", system-ui, sans-serif;
  font-size: 44px;
  font-weight: 800;
  line-height: 1;
  text-align: center;
  letter-spacing: 5px;
  padding: 36px 18px 22px;
  width: 100%;
  height: 140px;
  background-color: #fffbf0;
  background-image:
    linear-gradient(transparent calc(100% - 2px), rgba(99, 102, 241, 0.35) calc(100% - 2px));
  background-size: 100% 100%;
  background-position: 0 0;
  border: 3px dashed #c7d2fe;
  border-radius: 18px;
  color: var(--text);
  outline: none;
  caret-color: var(--primary);
  box-shadow: inset 0 2px 0 rgba(0,0,0,0.02);
  transition: border-color 0.15s, box-shadow 0.15s, background-color 0.15s;
}
input.spell-input::placeholder { color: #cbd5e1; font-size: 24px; letter-spacing: 2px; font-weight: 500; }
input.spell-input:focus {
  border-color: var(--primary);
  border-style: solid;
  background-color: #fff;
  box-shadow: 0 0 0 4px rgba(124, 58, 237, 0.18), inset 0 2px 0 rgba(0,0,0,0.02);
}
.spell-feedback {
  margin-top: 14px;
  font-size: 18px;
  font-weight: 700;
  min-height: 26px;
}
.spell-feedback.good { color: var(--good); }
.spell-feedback.bad  { color: var(--bad); }

/* 專心寫模式 */
body.focus-write input.spell-input {
  border-color: #f59e0b;
  border-style: solid;
  box-shadow: 0 0 0 5px rgba(245, 158, 11, 0.25), inset 0 2px 0 rgba(0,0,0,0.02);
}
body.focus-write .actions button:disabled {
  opacity: 0.25;
  cursor: not-allowed;
}
.focus-actions { gap: 20px; margin-top: 28px; }
.big-done {
  font-size: 24px;
  font-weight: 800;
  padding: 18px 36px;
  background: var(--good);
  color: white;
  box-shadow: 0 6px 0 #15803d;
}
.big-done:active { transform: translateY(3px); box-shadow: 0 3px 0 #15803d; }

/* 句子填空 */
.cloze-sentence {
  font-size: 26px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.5;
  text-align: center;
  margin: 8px 0 8px;
}
.cloze-blank {
  display: inline-block;
  min-width: 120px;
  border-bottom: 4px solid var(--primary);
  color: var(--primary);
  font-weight: 800;
  letter-spacing: 4px;
  margin: 0 4px;
  padding: 0 4px;
}
.cloze-meaning { font-size: 18px; color: var(--muted); text-align: center; margin-bottom: 4px; }
.cloze-options {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  max-width: 560px;
  margin: 18px auto 0;
}
.cloze-option {
  background: #f3f4f6;
  border: 3px solid transparent;
  border-radius: 12px;
  padding: 16px 14px;
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
  cursor: pointer;
  transition: background 0.1s, border-color 0.1s, transform 0.1s;
  box-shadow: 0 4px 0 #d1d5db;
}
.cloze-option:hover:not(:disabled) { background: #e5e7eb; }
.cloze-option:active:not(:disabled) { transform: translateY(2px); box-shadow: 0 2px 0 #d1d5db; }
.cloze-option.correct { background: #dcfce7; border-color: var(--good); color: #15803d; }
.cloze-option.wrong   { background: #fee2e2; border-color: var(--bad); color: #b91c1c; animation: shake 0.3s; }
.cloze-option:disabled { cursor: not-allowed; opacity: 0.6; }
.cloze-feedback {
  margin-top: 14px;
  font-size: 18px;
  font-weight: 700;
  text-align: center;
  min-height: 26px;
}
.cloze-feedback.good { color: var(--good); }
.cloze-feedback.bad  { color: var(--bad); }

/* 配對 */
.match-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  max-width: 720px;
  margin: 0 auto;
}
.match-tile {
  background: #f3f4f6;
  border: 3px solid transparent;
  border-radius: 12px;
  padding: 18px 14px;
  font-size: 20px;
  font-weight: 700;
  cursor: pointer;
  user-select: none;
  transition: background 0.1s, border-color 0.1s, transform 0.1s;
}
.match-tile:hover { background: #e5e7eb; }
.match-tile.selected { border-color: var(--primary); background: #ede9fe; }
.match-tile.correct  { background: #dcfce7; border-color: var(--good); pointer-events: none; }
.match-tile.wrong    { background: #fee2e2; border-color: var(--bad); animation: shake 0.3s; }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}

/* 結束畫面 */
.summary {
  text-align: center;
}
.summary .big {
  font-size: 48px;
  font-weight: 800;
  color: var(--primary-dark);
}
.summary .star {
  font-size: 42px;
  margin: 0 4px;
}

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--muted);
}
.empty-state code {
  background: #fde68a;
  padding: 2px 6px;
  border-radius: 6px;
  color: var(--text);
}

@media (max-width: 480px) {
  .app-header { flex-direction: column; align-items: flex-start; gap: 12px; }
  .flashcard-inner { height: 240px; }
  .flashcard .word { font-size: 36px; }
  .match-grid { grid-template-columns: 1fr; }
  input.spell-input { font-size: 36px; height: 110px; padding: 32px 12px 18px; letter-spacing: 4px; }
}
