:root {
  --bg: radial-gradient(circle at 20% 20%, #f8f2ff 0%, #e7ecff 40%, #f6f8ff 100%);
  --surface: rgba(255, 255, 255, 0.88);
  --border: rgba(15, 23, 42, 0.09);
  --text: #0f172a;
  --muted: #475569;
  --accent: #ff7b2f;
  --accent-2: #5a62ff;
  --good: #16a34a;
  --bad: #e11d48;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  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: 960px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--surface);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 12px 16px;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.06);
}

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

.back-link {
  text-decoration: none;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 600;
  padding: 6px 10px;
  border-radius: 8px;
  background: rgba(15, 23, 42, 0.05);
  transition: background 0.15s, color 0.15s;
}

.back-link:hover {
  background: rgba(15, 23, 42, 0.1);
  color: var(--text);
}

.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;
  gap: 6px;
  flex-wrap: wrap;
}

.tab-btn {
  padding: 8px 13px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: white;
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.15s;
}

.tab-btn.active {
  background: var(--accent-2);
  color: white;
  border-color: transparent;
  box-shadow: 0 4px 12px rgba(90, 98, 255, 0.28);
}

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

.game-panel {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 16px;
  box-shadow: 0 12px 30px rgba(15, 23, 42, 0.08);
}

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

.panel-head {
  text-align: center;
}

.panel-head h2 {
  margin: 0;
  font-size: 1.25rem;
}

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

.hud-grid {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.pill {
  background: white;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  box-shadow: 0 4px 12px rgba(15, 23, 42, 0.04);
}

.pill strong {
  font-size: 0.72rem;
  color: var(--muted);
}

.pill span {
  font-size: 1rem;
  font-weight: 700;
}

.progress {
  width: 100%;
  height: 6px;
  background: rgba(15, 23, 42, 0.08);
  border-radius: 999px;
  overflow: hidden;
  margin-top: 2px;
}

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

.action-bar {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 8px;
}

.btn {
  padding: 9px 16px;
  border-radius: 10px;
  border: none;
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.1s, opacity 0.15s;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn:active:not(:disabled) {
  transform: scale(0.97);
}

.btn.primary {
  background: var(--accent);
  color: white;
  box-shadow: 0 6px 16px rgba(255, 123, 47, 0.25);
}

.btn.secondary {
  background: var(--accent-2);
  color: white;
  box-shadow: 0 6px 16px rgba(90, 98, 255, 0.22);
}

.btn.ghost {
  background: white;
  color: var(--text);
  border: 1px solid var(--border);
}

.diff-select {
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: white;
  color: var(--text);
  font-size: 0.88rem;
  font-weight: 600;
}

.board-box {
  position: relative;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 280px;
}

.idle-msg {
  padding: 40px 20px;
  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); }

/* ---- Game 1: Illusion Match ---- */
.il-grid {
  display: grid;
  gap: 8px;
  width: min(100%, 440px);
  aspect-ratio: 1;
}

.il-cell {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 12px;
  cursor: pointer;
  overflow: hidden;
  transition: transform 0.12s ease;
}

.il-cell:hover { transform: translateY(-2px); }
.il-cell.selected { outline: 4px solid rgba(90, 98, 255, 0.9); outline-offset: -4px; }
.il-cell.wrong { animation: shake 0.26s ease; }

.il-cell .dot {
  position: absolute;
  inset: 20%;
  border-radius: 50%;
}

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

.compare-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.compare-dot {
  position: absolute;
  border-radius: 50%;
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.16);
}

.compare-mark {
  position: absolute;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 26px;
  color: white;
  background: rgba(22, 163, 74, 0.92);
  opacity: 0;
  transition: opacity 0.18s ease, transform 0.22s ease;
}

.compare-mark.bad { background: rgba(225, 29, 72, 0.92); }

/* ---- Game 2: Color Sort ---- */
.hidden { display: none !important; }

.sort-menu {
  width: 100%;
}

.diff-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  max-width: 540px;
  margin: 12px auto;
}

.diff-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 20px 14px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: white;
  cursor: pointer;
  transition: transform 0.15s, border-color 0.15s, box-shadow 0.15s;
}

.diff-card:hover {
  transform: translateY(-2px);
  border-color: var(--accent-2);
  box-shadow: 0 10px 20px rgba(90, 98, 255, 0.14);
}

.diff-card strong { font-size: 1rem; color: var(--text); }
.diff-card span { font-size: 0.85rem; color: var(--muted); }

.sort-play {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.sort-hud {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

.sort-badges {
  display: flex;
  gap: 8px;
  align-items: center;
}

.badge {
  background: white;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 0.85rem;
  font-weight: 600;
}

.sort-board {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  padding: 12px;
  background: rgba(255, 255, 255, 0.7);
  border: 1px solid var(--border);
  border-radius: 14px;
}

.sort-block {
  width: 52px;
  height: 76px;
  border-radius: 10px;
  cursor: grab;
  position: relative;
  transition: transform 0.15s, box-shadow 0.15s;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.12);
}

.sort-block:hover { transform: translateY(-2px); }

.sort-block.fixed {
  cursor: default;
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.8);
}

.sort-block.fixed::after {
  content: '•';
  position: absolute;
  bottom: 4px;
  left: 50%;
  transform: translateX(-50%);
  color: rgba(255, 255, 255, 0.9);
  font-size: 16px;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
}

.sort-block.selected {
  outline: 3px solid #0f172a;
  transform: translateY(-4px) scale(1.04);
}

.sort-block.over {
  outline: 3px dashed var(--accent-2);
}

/* ---- Game 3: Color Perception ---- */
.pc-board {
  display: grid;
  gap: 6px;
  width: min(100%, 440px);
  aspect-ratio: 1;
  background: white;
  padding: 10px;
  border-radius: 14px;
  border: 1px solid var(--border);
}

.pc-swatch {
  width: 100%;
  height: 100%;
  border-radius: 8px;
  cursor: pointer;
  transition: transform 0.1s;
}

.pc-swatch:active { transform: scale(0.95); }

.pc-swatch.hint-pulse {
  animation: pulseHint 0.5s ease 2;
  outline: 3px solid #0f172a;
}

@keyframes pulseHint {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.08); }
}

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

.modal-card {
  width: min(100%, 380px);
  background: white;
  border-radius: 18px;
  padding: 22px;
  text-align: center;
  box-shadow: 0 20px 45px rgba(15, 23, 42, 0.25);
}

.modal-card h3 {
  margin: 0 0 8px;
  font-size: 1.35rem;
}

.modal-card p {
  margin: 0 0 18px;
  color: var(--muted);
  line-height: 1.5;
}

.modal-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
}

@media (max-width: 640px) {
  .hud-grid { grid-template-columns: repeat(2, 1fr); }
  .sort-block { width: 42px; height: 62px; }
}
