:root {
  --bg: #0b1120;
  --surface: #151e32;
  --surface-2: #0f172a;
  --border: rgba(255, 255, 255, 0.1);
  --text: #f1f5f9;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-2: #818cf8;
  --good: #22c55e;
  --bad: #ef4444;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: radial-gradient(circle at 20% 0%, #1e1b4b 0%, #0b1120 60%);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", "Noto Sans TC", sans-serif;
  padding: max(12px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) max(18px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

.app {
  max-width: 1040px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 12px 16px;
}

.brand { display: flex; align-items: center; gap: 12px; }

.back-link {
  color: var(--muted);
  text-decoration: none;
  font-size: 0.85rem;
  font-weight: 600;
  padding: 6px 10px;
  border-radius: 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.back-link:hover { color: var(--text); border-color: var(--accent); }
.brand h1 { margin: 0; font-size: 1.2rem; }
.subtitle { margin: 2px 0 0; font-size: 0.8rem; color: var(--muted); }

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

.tabs { display: flex; flex-wrap: wrap; gap: 6px; }

.tab-btn {
  padding: 7px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 700;
  cursor: pointer;
}

.tab-btn.active {
  background: var(--accent);
  color: #082f49;
  border-color: transparent;
}

.icon-pill {
  padding: 7px 11px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
}

.game-panel {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 18px;
}

.game-panel.active { display: flex; }

.panel-head { text-align: center; }
.panel-head h2 { margin: 0; font-size: 1.2rem; }
.panel-head p { margin: 4px 0 0; font-size: 0.88rem; color: var(--muted); }

.hud-bar {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: rgba(15, 23, 42, 0.55);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
}

.hud-pill {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 6px 12px;
  font-size: 0.88rem;
}

.hud-pill strong { color: var(--accent); }

.inline-ctrl {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.85rem;
  color: #cbd5e1;
}

.inline-ctrl input[type="number"],
.select-input {
  padding: 6px 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.88rem;
}

.inline-ctrl input[type="number"] { width: 62px; }
.inline-ctrl input[type="range"] { width: 90px; accent-color: var(--accent); }

.btn {
  padding: 8px 16px;
  border-radius: 10px;
  border: none;
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
}

.btn.primary { background: var(--accent); color: #082f49; }
.btn.ghost { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }

.idle-msg { color: var(--muted); font-weight: 600; text-align: center; }
.status-msg { min-height: 22px; font-weight: 700; font-size: 0.92rem; text-align: center; }
.status-msg.ok { color: var(--good); }
.status-msg.bad { color: var(--bad); }

.progress-track {
  width: min(100%, 480px);
  height: 8px;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 999px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 100%;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
}

/* 1. Spatial Memory */
.sp-board {
  position: relative;
  width: 100%;
  height: min(58vh, 460px);
  min-height: 300px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.sp-card {
  position: absolute;
  perspective: 600px;
  cursor: pointer;
}

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

.sp-card.is-flipped .sp-card-inner { transform: rotateY(180deg); }

.sp-face {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  backface-visibility: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
}

.sp-front {
  background: linear-gradient(145deg, #38bdf8, #0284c7);
  color: #082f49;
  box-shadow: 0 4px 12px rgba(56, 189, 248, 0.35);
}

.sp-back {
  background: #334155;
  border: 2px solid #64748b;
  transform: rotateY(180deg);
}

/* 2. Magic Number */
.mn-stage {
  width: min(100%, 460px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px;
}

.mn-display {
  font-size: 2rem;
  font-weight: 800;
  letter-spacing: 0.14em;
  min-height: 44px;
  text-align: center;
}

.mn-input {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: 0.14em;
  min-height: 36px;
  color: var(--accent);
}

.keypad {
  display: grid;
  grid-template-columns: repeat(3, 74px);
  gap: 8px;
}

.kbtn {
  height: 52px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 1.25rem;
  font-weight: 700;
  cursor: pointer;
}

.kbtn:active { background: var(--accent); color: #082f49; }
.kbtn.action { background: #1e293b; color: var(--muted); }

/* 3. Schulte Grid */
.sc-board {
  display: grid;
  gap: 6px;
  width: min(100%, 440px);
  aspect-ratio: 1;
  background: var(--surface-2);
  padding: 10px;
  border-radius: 14px;
  border: 1px solid var(--border);
}

.sc-cell {
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  background: #1e293b;
  border: 1px solid var(--border);
  font-weight: 800;
  font-size: clamp(0.85rem, 2.5vw, 1.25rem);
  cursor: pointer;
  transition: background 0.12s, transform 0.08s;
}

.sc-cell.completed { background: rgba(34, 197, 94, 0.28); color: #86efac; border-color: rgba(34, 197, 94, 0.5); }
.sc-cell.shake { animation: shake 0.25s ease; background: rgba(239, 68, 68, 0.35); }

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

/* 4. Stroop */
.st-stage {
  width: min(100%, 520px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
}

.st-stem-box {
  padding: 18px 42px;
  border-radius: 14px;
  background: white;
  min-width: 150px;
  text-align: center;
}

#st-stem { font-size: 3.4rem; font-weight: 900; }

.st-options {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.st-opt {
  padding: 16px 8px;
  border-radius: 12px;
  border: 2px solid rgba(255, 255, 255, 0.18);
  font-size: 1.5rem;
  font-weight: 900;
  text-align: center;
  cursor: pointer;
}

/* 5. ANS */
.hidden { display: none !important; }

.ans-setup {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  width: min(100%, 540px);
}

.preset-row { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }

.preset-btn {
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}

.preset-btn.active { border-color: var(--accent); background: rgba(56, 189, 248, 0.15); }

.ans-inputs {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

.ans-inputs label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 0.86rem;
}

.ans-inputs input {
  width: 72px;
  padding: 4px 6px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: #0b1120;
  color: var(--text);
}

.ans-play { width: 100%; display: flex; flex-direction: column; gap: 12px; }

.split-wrap {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  width: 100%;
  height: min(52vh, 380px);
  min-height: 240px;
}

.split-box {
  position: relative;
  background: var(--surface-2);
  border: 2px solid var(--border);
  border-radius: 14px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.4rem;
}

.split-box.selectable { cursor: pointer; border-color: rgba(56, 189, 248, 0.45); }
.split-box.selectable:hover { border-color: var(--accent); }
.ans-sq { position: absolute; background: var(--accent); border-radius: 4px; }

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(2, 6, 23, 0.65);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
}

.modal-card {
  width: min(100%, 380px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 22px;
  text-align: center;
}

.modal-card h3 { margin: 0 0 8px; }
.modal-card p { margin: 0 0 18px; color: var(--muted); line-height: 1.5; }
.modal-actions { display: flex; justify-content: center; gap: 10px; }
