:root {
  --bg:           #0e0e1a;
  --bg-card:      #1a1a2e;
  --text:         #f0f0f5;
  --text-dim:     #9a9aaf;
  --accent:       #ffd86b;
  --accent-deep:  #d4a843;
  --green:        #6bd47a;
  --red:          #ff6b6b;
  --purple:       #b48ead;
  --turquoise:    #00c8b4;
  --train-red:    #d4382c;
  --train-blue:   #2866b0;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { background: var(--bg); color: var(--text); font-family: "Fredoka", "Noto Sans HK", system-ui, sans-serif; height: 100%; overflow-x: hidden; }
main#app { max-width: 760px; margin: 0 auto; padding: 16px; min-height: 100vh; }

/* Topbar */
.topbar { display: flex; align-items: center; justify-content: space-between; padding: 12px 4px; }
.topbar h1 { font-size: 28px; color: var(--accent); letter-spacing: 0.04em; }
.mascot { width: 56px; height: 56px; border-radius: 50%; background: var(--bg-card); object-fit: cover; transition: transform 0.2s; }
.mascot.bounce { animation: hop 0.6s ease; }
@keyframes hop { 0%,100% { transform: translateY(0); } 30% { transform: translateY(-18px) rotate(-8deg); } 60% { transform: translateY(-4px) rotate(4deg); } }

/* Line cards */
.line-cards { display: flex; flex-direction: column; gap: 14px; margin-top: 16px; }
.line-card {
  display: block;
  padding: 24px 20px;
  background: linear-gradient(135deg, color-mix(in srgb, var(--line-color) 25%, var(--bg-card)), var(--bg-card));
  border-left: 8px solid var(--line-color);
  border-radius: 14px;
  color: var(--text);
  text-decoration: none;
  transition: transform 0.15s ease;
}
.line-card:active { transform: scale(0.98); }
.line-card-zh { font-size: 28px; font-weight: 700; color: var(--line-color); }
.line-card-en { font-size: 18px; margin-top: 4px; }
.line-card-progress { font-size: 14px; color: var(--text-dim); margin-top: 8px; font-variant-numeric: tabular-nums; }

/* Story button */
.story-button {
  display: block;
  text-align: center;
  margin: 24px auto;
  padding: 14px 24px;
  background: var(--bg-card);
  color: var(--accent);
  text-decoration: none;
  border-radius: 999px;
  font-size: 18px;
  font-weight: 500;
  position: relative;
}

.line-card-boss {
  position: absolute; top: 14px; right: 18px;
  font-size: 28px; opacity: 0.25; transition: opacity 0.3s;
}
.line-card-boss.defeated { opacity: 1; filter: grayscale(0); }
.line-card { position: relative; }

.story-button { position: relative; }
.story-new-badge {
  position: absolute; top: -6px; right: -6px;
  background: var(--red); color: white; padding: 2px 8px;
  border-radius: 999px; font-size: 11px; font-weight: 700;
  animation: pulse 1.5s infinite;
}
.story-new-badge.hidden { display: none; }
@keyframes pulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.15); } }

.back-link { color: var(--accent); font-size: 28px; text-decoration: none; padding: 0 12px; }

.track { position: relative; padding: 24px 0 40px 0; }
.track-line {
  position: absolute; left: 38px; top: 0; bottom: 0; width: 6px;
  background: var(--line-color, var(--accent)); opacity: 0.4; border-radius: 3px;
}
.station-stop {
  display: flex; align-items: center; gap: 16px;
  padding: 14px 12px; text-decoration: none; color: var(--text);
  position: relative; z-index: 1;
}
.stop-dot {
  width: 28px; height: 28px; border-radius: 50%;
  background: var(--bg-card); border: 4px solid var(--line-color, var(--accent));
  margin-left: 24px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px;
}
.station-stop.completed .stop-dot { background: var(--line-color, var(--accent)); }
.station-stop.active .stop-dot { animation: pulse 1.5s infinite; box-shadow: 0 0 16px var(--line-color); }
.station-stop.locked { opacity: 0.4; pointer-events: none; }

.stop-label { display: flex; flex-direction: column; line-height: 1.2; }
.stop-zh { font-size: 24px; font-weight: 700; color: var(--line-color, var(--accent)); }
.stop-en { font-size: 14px; color: var(--text-dim); }

.boss-stop .boss-dot { width: 56px; height: 56px; margin-left: 10px; font-size: 28px; border-color: var(--red); background: var(--bg-card); }
.boss-stop.locked .boss-dot { filter: grayscale(1); }

.station-position { color: var(--text-dim); font-variant-numeric: tabular-nums; }

.station-card {
  background: var(--bg-card); border-radius: 18px; padding: 32px 24px;
  margin-top: 16px; text-align: center;
  border-top: 8px solid var(--line-color, var(--accent));
}
.phrase-zh { font-size: 96px; font-weight: 700; color: var(--line-color, var(--accent)); line-height: 1; }
.phrase-translit { font-size: 22px; font-family: ui-monospace, "SF Mono", monospace; margin-top: 8px; color: var(--text-dim); }
.phrase-en { font-size: 28px; margin-top: 8px; font-style: italic; }

.play-phrase {
  background: var(--accent); color: var(--bg); border: none; border-radius: 50%;
  width: 64px; height: 64px; font-size: 28px; margin: 16px auto; display: block; cursor: pointer;
}
.play-phrase:active { transform: scale(0.92); }

.breakdown { margin-top: 24px; padding-top: 24px; border-top: 1px dashed var(--text-dim); }
.play-breakdown { background: transparent; color: var(--accent); border: 1px solid var(--accent); border-radius: 999px; padding: 8px 18px; font-size: 16px; cursor: pointer; }
.breakdown-cards { display: flex; flex-wrap: wrap; gap: 12px; justify-content: center; margin-top: 16px; }
.breakdown-card {
  background: var(--bg); border: 2px solid var(--line-color, var(--accent));
  border-radius: 12px; padding: 10px 14px; min-width: 80px;
  transition: transform 0.2s, box-shadow 0.2s;
}
.breakdown-card.highlight {
  transform: scale(1.15); box-shadow: 0 0 24px var(--line-color, var(--accent));
}
.bc-zh { font-size: 32px; font-weight: 700; color: var(--line-color, var(--accent)); }
.bc-en { font-size: 13px; color: var(--text-dim); margin-top: 4px; }

.station-image { width: 100%; max-width: 320px; aspect-ratio: 1; object-fit: cover; border-radius: 16px; margin: 24px auto 0; display: block; background: var(--bg); }
.station-image.placeholder { opacity: 0.4; }

.trivia { margin-top: 16px; padding: 12px 16px; background: var(--bg); border-radius: 12px; font-size: 14px; color: var(--text-dim); text-align: left; }

.quiz-me {
  margin-top: 28px; padding: 18px 36px; background: var(--accent); color: var(--bg);
  border: none; border-radius: 999px; font-size: 22px; font-weight: 700; cursor: pointer;
}
.quiz-me:active { transform: scale(0.96); }

.quiz-prompt { display: flex; align-items: center; gap: 12px; }
.quiz-translit { font-family: ui-monospace, monospace; font-size: 22px; color: var(--accent); }
.play-phrase-mini { background: var(--accent); color: var(--bg); border: none; border-radius: 50%; width: 36px; height: 36px; font-size: 16px; cursor: pointer; }

.quiz-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 16px;
  margin-top: 24px;
}
.quiz-tile {
  background: var(--bg-card); border-radius: 18px; padding: 12px;
  aspect-ratio: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  cursor: pointer; border: 4px solid transparent;
  transition: transform 0.15s ease, border-color 0.2s, box-shadow 0.3s;
}
.quiz-tile img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 12px; }
.quiz-tile.right { border-color: var(--green); box-shadow: 0 0 40px var(--green); animation: bounce 0.6s ease; }
.quiz-tile.wrong { border-color: var(--red); animation: wobble 0.5s ease; }
.quiz-tile.glow  { border-color: var(--green); box-shadow: 0 0 24px var(--green); }
.quiz-tile:active { transform: scale(0.96); }
@keyframes bounce { 0% { transform: scale(1); } 30% { transform: scale(1.18); } 60% { transform: scale(0.94); } 100% { transform: scale(1); } }
@keyframes wobble { 0%,100% { transform: translateX(0); } 25% { transform: translateX(-12px); } 75% { transform: translateX(12px); } }

.quiz-feedback { min-height: 28px; text-align: center; margin-top: 18px; font-size: 18px; color: var(--green); }

/* Prevent iOS rubber-band bounce on the body, allow scroll inside .scroll-region */
html, body { overscroll-behavior: none; touch-action: manipulation; }

/* Ensure tap targets are at least 44×44 (iOS HIG) */
button, a.line-card, a.station-stop, .quiz-tile { min-height: 44px; }

/* Stop double-tap-to-zoom on iPad */
* { touch-action: manipulation; }

/* Phone */
@media (max-width: 480px) {
  .phrase-zh { font-size: 72px; }
  .station-image { max-width: 240px; }
  .quiz-grid { gap: 10px; }
}

/* iPad portrait */
@media (min-width: 768px) {
  main#app { max-width: 880px; }
  .phrase-zh { font-size: 120px; }
  .quiz-grid { gap: 24px; max-width: 720px; margin: 24px auto 0; }
}

/* Boss scene */
.boss-stage-label { color: var(--red); font-weight: 700; letter-spacing: 0.1em; }
.boss-scene { margin-top: 16px; }
.boss-stage { display: flex; flex-direction: column; align-items: center; gap: 16px; }
.boss-stage.hidden { display: none; }
.boss-panel-image { width: 100%; max-width: 640px; aspect-ratio: 3/2; object-fit: cover; border-radius: 18px; background: var(--bg-card); }
.boss-dialog { background: var(--bg-card); padding: 18px 24px; border-radius: 18px; text-align: center; max-width: 640px; width: 100%; }
.bd-zh { font-size: 28px; font-weight: 700; color: var(--accent); }
.bd-translit { color: var(--text-dim); font-family: ui-monospace, monospace; margin-top: 4px; }
.bd-en { margin-top: 6px; font-style: italic; }
.boss-continue {
  background: var(--accent); color: var(--bg); border: none; border-radius: 999px;
  padding: 16px 36px; font-size: 22px; font-weight: 700; cursor: pointer; text-decoration: none; margin-top: 8px;
}
.fight-prompt { color: var(--accent); font-size: 22px; }
.fight-prompt-zh { color: var(--text-dim); font-family: ui-monospace, monospace; }
.gimmick-area { width: 100%; min-height: 420px; }

/* Ordered Recall gimmick (Token Thief) */
.ordered-recall-wrap {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 12px; padding: 16px 0;
}
.or-tile {
  aspect-ratio: 1; background: var(--bg-card); border: 4px solid var(--accent);
  border-radius: 16px; color: var(--accent); font-weight: 700; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  transition: transform 0.15s, opacity 0.3s, background 0.3s;
}
.or-phrase { font-size: 36px; }
.or-translit { font-size: 13px; color: var(--text-dim); font-family: monospace; margin-top: 2px; }
.or-tile:active { transform: scale(0.92); }
.or-tile.or-taken { background: var(--green); border-color: var(--green); color: var(--bg); opacity: 0.7; pointer-events: none; }
.or-tile.or-wrong { animation: wobble 0.4s ease; border-color: var(--red); }
.or-status { color: var(--accent); text-align: center; font-size: 16px; margin-bottom: 8px; }

/* El Borrador — eraser_dash gimmick */
.ed-banner { display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 12px 0; }
.ed-translit { font-size: 28px; font-family: ui-monospace, monospace; color: var(--accent); }
.ed-bar { width: 80%; max-width: 320px; height: 8px; background: var(--bg-card); border-radius: 4px; overflow: hidden; }
.ed-fill { width: 100%; height: 100%; background: var(--red); }
.ed-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; padding: 12px 0; }
.ed-tile {
  background: var(--bg-card); border: 4px solid transparent; border-radius: 16px; aspect-ratio: 1;
  display: flex; align-items: center; justify-content: center; padding: 8px; cursor: pointer;
  transition: opacity 0.3s, border-color 0.2s;
}
.ed-tile img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 12px; }
.ed-tile.right { border-color: var(--green); animation: bounce 0.6s ease; }
.ed-tile.wrong { border-color: var(--red); animation: wobble 0.4s ease; }
.ed-tile.erased { opacity: 0.1; transform: scale(0.85); filter: blur(2px); }

/* Babel Vortex — vortex_pick gimmick */
.vortex { position: relative; height: 360px; }
.vortex-ring {
  position: absolute; inset: 0; margin: auto; width: 320px; height: 320px;
  animation: spin 18s linear infinite;
}
@keyframes spin { from { transform: rotate(0); } to { transform: rotate(360deg); } }
.vortex-word {
  position: absolute; left: 50%; top: 50%;
  transform: translate(-50%, -50%) rotate(var(--angle)) translate(140px) rotate(calc(-1 * var(--angle)));
  background: var(--bg-card); border: 4px solid var(--purple); border-radius: 14px;
  padding: 8px 12px; min-width: 80px; cursor: pointer; transition: transform 0.2s;
}
.vw-zh { font-size: 24px; font-weight: 700; color: var(--purple); }
.vw-translit { font-size: 11px; color: var(--text-dim); font-family: monospace; }
.vortex-word.right { background: var(--green); border-color: var(--green); pointer-events: none; }
.vortex-word.wrong { animation: wobble 0.4s; border-color: var(--red); }
.vortex-callout { text-align: center; font-size: 28px; color: var(--accent); padding: 16px 0; min-height: 60px; }

/* Kanji Shadow — kanji_repair gimmick */
.kr-wrap { display: grid; grid-template-columns: 1fr; gap: 16px; padding: 8px 0; }
.kr-chars, .kr-slots { display: flex; flex-wrap: wrap; gap: 12px; justify-content: center; }
.kr-char {
  font-size: 48px; font-weight: 700; color: var(--accent);
  background: var(--bg-card); border: 3px solid var(--accent); border-radius: 12px;
  padding: 8px 18px; min-width: 72px; text-align: center; user-select: none; cursor: grab;
}
.kr-char.kr-grabbed { transform: translate(-50%, -50%) scale(1.1); opacity: 0.9; pointer-events: none; z-index: 1000; }
.kr-char.kr-placed { cursor: default; }
.kr-slot {
  min-width: 100px; min-height: 80px;
  background: var(--bg); border: 2px dashed var(--text-dim); border-radius: 12px;
  padding: 10px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
}
.kr-slot.kr-filled { border-color: var(--green); border-style: solid; }
.kr-meaning { font-size: 14px; color: var(--text-dim); }

/* Rosetta Breaker — rosetta_stone gimmick (final boss) */
.rs-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; padding: 8px 0; }
@media (min-width: 768px) { .rs-grid { grid-template-columns: repeat(4, 1fr); } }
.rs-card {
  background: var(--bg-card); border: 3px solid var(--accent);
  border-radius: 14px; padding: 14px 8px; min-height: 80px;
  font-size: 22px; cursor: pointer; transition: all 0.2s;
}
.rs-zh { color: var(--accent); font-weight: 700; font-size: 32px; }
.rs-en { color: var(--text); font-style: italic; }
.rs-card.rs-active { background: var(--accent); color: var(--bg); transform: scale(1.05); }
.rs-card.rs-active.rs-zh { color: var(--bg); }
.rs-card.rs-matched { background: var(--green); border-color: var(--green); color: var(--bg); pointer-events: none; }
.rs-card.rs-wrong { animation: wobble 0.4s ease; border-color: var(--red); }

.final-boss-link {
  display: block; text-align: center; margin: 20px auto;
  padding: 18px 28px; background: linear-gradient(135deg, var(--red), var(--purple));
  color: white; text-decoration: none; border-radius: 18px;
  font-weight: 700; font-size: 20px;
  animation: pulse 1.5s infinite;
}

/* Story page */
.panel-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px; padding: 16px 0; }
@media (min-width: 768px) { .panel-grid { grid-template-columns: repeat(3, 1fr); } }
.panel-card { position: relative; background: var(--bg-card); border-radius: 14px; overflow: hidden; cursor: pointer; aspect-ratio: 4/3; }
.panel-thumb { width: 100%; height: 100%; object-fit: cover; }
.panel-title {
  position: absolute; bottom: 0; left: 0; right: 0;
  background: linear-gradient(0deg, rgba(0,0,0,0.85), transparent);
  color: var(--accent); padding: 10px 12px 8px; font-weight: 700; font-size: 14px;
}
.panel-locked-overlay {
  position: absolute; inset: 0; background: rgba(0,0,0,0.85); color: var(--text-dim);
  display: none; align-items: center; justify-content: center; font-size: 32px;
}
.panel-card.locked .panel-thumb { filter: grayscale(1) brightness(0.4); }
.panel-card.locked .panel-locked-overlay { display: flex; }
.panel-card.unseen::after {
  content: "NEW"; position: absolute; top: 6px; right: 6px;
  background: var(--red); color: white; padding: 2px 8px;
  border-radius: 999px; font-size: 11px; font-weight: 700;
}

.panel-modal { position: fixed; inset: 0; background: rgba(0,0,0,0.95); z-index: 1000; display: flex; align-items: center; justify-content: center; padding: 16px; overflow-y: auto; }
.panel-modal.hidden { display: none; }
.panel-modal-inner { max-width: 640px; width: 100%; background: var(--bg-card); border-radius: 18px; padding: 24px; position: relative; }
.panel-close { position: absolute; top: 10px; right: 12px; background: transparent; border: none; color: var(--text); font-size: 36px; cursor: pointer; line-height: 1; }
.panel-modal-image { width: 100%; border-radius: 14px; aspect-ratio: 3/2; object-fit: cover; }
.panel-modal-title { text-align: center; margin-top: 16px; color: var(--accent); font-size: 22px; font-weight: 700; }
.panel-modal-dialog { background: var(--bg); border-radius: 14px; padding: 18px; margin-top: 14px; text-align: center; }
