:root {
  --bg-top: #efe9ff;
  --bg-bottom: #fef6e4;
  --panel: #ffffffee;
  --ink: #33285a;
  --ink-soft: #6a5f8f;
  --accent: #7c5cf5;
  --accent-2: #ff9f6b;
  --accent-3: #35c98b;
  --danger: #e0557a;
  --card-back: #7c5cf5;
  --card-back-2: #5c3fd1;
  --radius: 18px;
  --shadow: 0 8px 20px rgba(51, 40, 90, 0.18);
  --focus-ring: 3px solid #1f6feb;
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  overscroll-behavior: none;
}

body {
  font-family: "Comic Sans MS", "Chalkboard SE", "Baloo 2", "Trebuchet MS", "Segoe UI", sans-serif;
  background: linear-gradient(180deg, var(--bg-top), var(--bg-bottom));
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

button {
  font-family: inherit;
}

.hidden { display: none !important; }

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border: 0;
}

a:focus-visible,
button:focus-visible,
[tabindex]:focus-visible {
  outline: var(--focus-ring);
  outline-offset: 3px;
}

/* ===== Screens & cards (menu chrome) ===== */

.screen {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.card {
  position: relative;
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px 24px;
  text-align: center;
  width: 100%;
  max-width: 480px;
}

.card-wide { max-width: 640px; }

/* "← Alex Lab": a labelled pill so the way back to the portal is obvious. */
.home-link {
  display: flex;
  align-items: center;
  gap: 6px;
  width: max-content;
  min-height: 40px;
  margin: 0 auto 14px 0;
  padding: 0 14px;
  border-radius: 999px;
  border: 2px solid var(--accent);
  background: #fff;
  color: #4a31c9;
  font-size: 15px;
  font-weight: 800;
  line-height: 1;
  text-decoration: none;
  white-space: nowrap;
  box-shadow: var(--shadow);
}
.home-link:hover { background: #f1ecff; }

.title {
  font-size: clamp(28px, 6vw, 40px);
  margin: 8px 0 4px;
  color: var(--accent);
}

.title-sm {
  font-size: clamp(22px, 5vw, 28px);
  margin: 4px 0 16px;
  color: var(--accent);
}

.subtitle {
  color: var(--ink-soft);
  font-size: 18px;
  margin: 0 0 18px;
}

.how-to-list {
  list-style: none;
  padding: 0;
  margin: 0 0 22px;
  text-align: left;
  display: grid;
  gap: 8px;
  font-size: 17px;
}

.btn {
  font-size: 18px;
  font-weight: 700;
  border: none;
  border-radius: 14px;
  padding: 14px 22px;
  min-height: 48px;
  cursor: pointer;
  color: #fff;
  background: var(--accent);
  box-shadow: var(--shadow);
  transition: transform 0.12s ease;
}

.btn:active { transform: scale(0.96); }

.btn-primary { background: var(--accent); }
.btn-secondary { background: var(--ink-soft); margin-top: 10px; width: 100%; }
.btn-large { width: 100%; font-size: 22px; padding: 18px; }

.btn-icon {
  background: #fff;
  color: var(--ink);
  border-radius: 50%;
  width: 44px;
  height: 44px;
  min-height: 44px;
  padding: 0;
  font-size: 20px;
  box-shadow: var(--shadow);
}

.home-footer-row {
  margin-top: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.best-progress {
  color: var(--ink-soft);
  font-size: 15px;
}

a.btn { display: block; text-decoration: none; text-align: center; }

/* ===== Difficulty select ===== */

.difficulty-grid {
  display: grid;
  gap: 14px;
  margin-bottom: 18px;
}

.difficulty-card {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 14px;
  align-items: center;
  text-align: left;
  background: #fff;
  border: 3px solid transparent;
  border-radius: 16px;
  padding: 14px 16px;
  cursor: pointer;
  box-shadow: var(--shadow);
}

.difficulty-card:hover,
.difficulty-card:focus-visible { border-color: var(--accent); }

.difficulty-emoji { font-size: 32px; grid-row: span 2; }
.difficulty-name { font-size: 20px; font-weight: 800; color: var(--ink); }
.difficulty-desc { grid-column: 2; color: var(--ink-soft); font-size: 14px; }

/* ===== Level select ===== */

.tag {
  font-size: 14px;
  background: var(--accent);
  color: #fff;
  padding: 3px 10px;
  border-radius: 999px;
  vertical-align: middle;
  margin-left: 6px;
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(58px, 1fr));
  gap: 10px;
  margin-bottom: 18px;
}

.level-btn {
  aspect-ratio: 1;
  border-radius: 12px;
  border: none;
  background: #fff;
  box-shadow: var(--shadow);
  font-size: 16px;
  font-weight: 800;
  color: var(--ink);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 48px;
}

.level-btn.milestone { background: linear-gradient(160deg, #ffe08a, #ffb84d); }
.level-btn.completed { background: linear-gradient(160deg, #baf3d1, #6bd67a); }
.level-btn.locked { background: #e8e4f5; color: var(--ink-soft); cursor: not-allowed; }
.level-btn .level-check { font-size: 12px; }
.level-btn .level-check:empty { display: none; }

/* Promotion levels: the board grows here (e.g. 3x3 -> 4x4). */
.level-btn { position: relative; }
.level-btn.promotion { outline: 3px solid var(--accent); outline-offset: -3px; }
.level-btn .level-star {
  position: absolute;
  top: -8px;
  right: -8px;
  font-size: 20px;
  line-height: 1;
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.35));
}
.level-btn .level-size { font-size: 11px; font-weight: 800; line-height: 1; }
.level-legend { margin: 0 0 12px; font-size: 14px; font-weight: 700; color: var(--ink-soft); text-align: center; }

/* ===== Gameplay HUD ===== */

#screen-game { flex-direction: column; align-items: stretch; padding: 10px; }

.hud {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  background: var(--panel);
  border-radius: 16px;
  padding: 10px 14px;
  box-shadow: var(--shadow);
  margin-bottom: 10px;
}

.hud-info { display: flex; flex-direction: column; margin-right: auto; }
.hud-level { font-weight: 800; font-size: 16px; }
.hud-theme { font-size: 13px; color: var(--ink-soft); }

.hud-stats {
  display: flex;
  gap: 12px;
  font-size: 14px;
  font-weight: 700;
}

.hud-actions { display: flex; gap: 8px; }
.hud-actions .btn-icon { width: 38px; height: 38px; min-height: 38px; font-size: 17px; }

/* Used-up free peek: clearly switched off until the next game. */
.hud-actions .btn-icon:disabled {
  opacity: 0.35;
  filter: grayscale(1);
  box-shadow: none;
  cursor: not-allowed;
}
.hud-actions .btn-icon:disabled:active { transform: none; }

/* The menu button pauses the game and is the way to resume / restart / leave
   a level, so it is the one filled button in the bar. Fixed colours: same in light and dark mode. */
.hud-actions .btn-pause {
  position: relative;
  width: 40px;
  height: 40px;
  min-height: 40px;
  padding: 0;
  border-radius: 50%;
  background: linear-gradient(145deg, #8b6cff, #5b3fe0);
  box-shadow: 0 4px 12px rgba(91, 63, 224, 0.45);
}
/* The menu symbol: three crisp white bars (hamburger) drawn in CSS. */
.hud-actions .btn-pause::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 18px;
  height: 3px;
  border-radius: 2px;
  background: #fff;
  transform: translate(-50%, -50%);
  box-shadow: 0 -6px 0 #fff, 0 6px 0 #fff;
}

/* ===== Board & cards ===== */

.board {
  flex: 1;
  display: grid;
  gap: 8px;
  align-content: center;
  justify-content: center;
  padding: 4px;
}

.mcard {
  /* --card-size is set by layoutBoard() in game.js so the board always fits. */
  width: var(--card-size, clamp(44px, 15vw, 84px));
  height: var(--card-size, clamp(44px, 15vw, 84px));
  perspective: 600px;
  background: transparent;
  border: none;
  padding: 0;
  cursor: pointer;
}

/* Middle cell of odd boards (3x3, 5x5): shown, but not a card and not clickable. */
.mcard-blocked {
  width: var(--card-size, 56px);
  height: var(--card-size, 56px);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
  border: 3px dashed var(--ink-soft);
  font-size: calc(var(--card-size, 56px) * 0.4);
  opacity: 0.45;
  pointer-events: none;
  user-select: none;
}

.mcard-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transition: transform 0.35s;
  transform-style: preserve-3d;
}

.mcard.flipped .mcard-inner,
.mcard.matched .mcard-inner { transform: rotateY(180deg); }

.mcard-face {
  position: absolute;
  inset: 0;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--card-size, 56px) * 0.5);
  backface-visibility: hidden;
  box-shadow: var(--shadow);
}

.mcard-back {
  background: linear-gradient(160deg, var(--card-back), var(--card-back-2));
  color: #fff;
  font-size: calc(var(--card-size, 56px) * 0.4);
}

.mcard-front {
  background: #fff;
  transform: rotateY(180deg);
}

.mcard.matched .mcard-front {
  background: linear-gradient(160deg, #d3f8e2, #a6efc3);
  outline: 3px solid var(--accent-3);
}

.mcard.mismatch .mcard-inner { animation: shake 0.4s; }

@keyframes shake {
  0%, 100% { transform: rotateY(180deg) translateX(0); }
  25% { transform: rotateY(180deg) translateX(-6px); }
  75% { transform: rotateY(180deg) translateX(6px); }
}

/* ===== Overlays ===== */

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(33, 24, 60, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 20;
}

.overlay-card {
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px 24px;
  text-align: center;
  width: 100%;
  max-width: 420px;
  position: relative;
  /* Never taller than the screen: scroll inside the card if it has to. */
  max-height: calc(100vh - 32px);
  max-height: calc(100dvh - 32px);
  overflow-x: hidden;
  overflow-y: auto;
}

/* Short screens (phones held sideways): tighten the menu so it fits. */
@media (max-height: 480px) {
  .overlay-card { padding: 12px 18px; max-width: 520px; }
  .overlay-card h2 { margin: 0 0 4px; font-size: 20px; }
  .overlay-card .btn { margin-top: 6px; min-height: 44px; padding: 8px 14px; font-size: 16px; }
}

.overlay-card .btn { width: 100%; margin-top: 10px; }

.celebration-icon { font-size: 52px; margin-bottom: 4px; }
.complete-subtitle { font-size: 17px; color: var(--ink-soft); margin: 4px 0; }
.complete-reward {
  font-weight: 800;
  color: var(--accent);
  background: #f3efff;
  border-radius: 10px;
  padding: 8px;
  margin: 8px 0;
}
.complete-summary {
  display: flex;
  justify-content: center;
  gap: 18px;
  font-size: 14px;
  color: var(--ink-soft);
  margin-bottom: 6px;
}
.complete-actions { margin-top: 10px; }

.celebration-fx {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}

.fx-particle {
  position: absolute;
  top: -10%;
  font-size: 22px;
  animation: fall linear forwards;
}

@keyframes fall {
  to { transform: translateY(420px) rotate(200deg); opacity: 0; }
}

/* ===== Memorize countdown (cards hide at 0) ===== */

.peek-countdown {
  position: fixed;
  left: 50%;
  top: calc(14px + env(safe-area-inset-top, 0px));
  transform: translateX(-50%);
  z-index: 15; /* above the HUD, below the pause/complete overlays */
  display: flex;
  align-items: center;
  gap: 12px;
  width: max-content;
  max-width: calc(100vw - 32px);
  padding: 8px 18px 12px;
  border-radius: 18px;
  overflow: hidden;
  /* Fixed colours so it stays readable in dark mode too. */
  background: #fff;
  color: #2a1f4d;
  border: 4px solid #7c5cf5;
  font-weight: 800;
  font-size: clamp(16px, 4.6vw, 22px);
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.3);
  pointer-events: none;
}

.peek-countdown-num {
  min-width: 1.4em;
  text-align: center;
  font-size: 1.7em;
  line-height: 1;
  color: #d6304f;
  font-variant-numeric: tabular-nums;
}

.peek-countdown-bar {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 6px;
  background: #7c5cf5;
  transform-origin: left center;
}

/* ===== Wrong-guess popup (tries left) ===== */

.mistake-toast {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 40;
  width: max-content;
  max-width: calc(100vw - 32px);
  padding: 14px 22px;
  border-radius: 18px;
  /* Fixed colours (not theme variables) so it stays readable in dark mode too. */
  background: #fff;
  color: #2a1f4d;
  border: 4px solid #d6304f;
  font-weight: 800;
  font-size: clamp(18px, 5vw, 24px);
  text-align: center;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.35);
  pointer-events: none;
  animation: mistake-toast-pop 0.2s ease-out;
}

@keyframes mistake-toast-pop {
  from { transform: translate(-50%, -50%) scale(0.8); opacity: 0; }
  to   { transform: translate(-50%, -50%) scale(1); opacity: 1; }
}

/* ===== Learn the card sets ===== */

#screen-learn { align-items: flex-start; }
.learn-intro { margin: 0 0 14px; color: var(--ink-soft); font-weight: 700; }
.learn-set { margin-bottom: 20px; }
.learn-set-title {
  margin: 0 0 10px;
  font-size: 18px;
  text-align: left;
  color: var(--ink);
  border-bottom: 3px solid var(--accent);
  padding-bottom: 4px;
}
.learn-grid {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
  gap: 8px;
}
.learn-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}
/* Same look as a face-up card on the game board (.mcard-front). */
.learn-card-face {
  width: 72px;
  height: 72px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
  background: #fff;
  box-shadow: var(--shadow);
  font-size: 36px;
  line-height: 1;
}
.learn-card-name { font-size: 12px; font-weight: 800; line-height: 1.15; color: var(--ink); overflow-wrap: anywhere; }

/* ===== Responsive ===== */

@media (max-width: 420px) {
  .card { padding: 22px 16px; }
  .hud { padding: 8px 10px; }
  .hud-stats { gap: 8px; font-size: 13px; }
}

@media (min-width: 900px) {
  .board { gap: 12px; }
}

/* ===== Reduced motion ===== */

@media (prefers-reduced-motion: reduce) {
  .mcard-inner { transition: none; }
  .mcard.mismatch .mcard-inner { animation: none; }
  .fx-particle { animation: none; opacity: 0; }
  .btn { transition: none; }
  .mistake-toast { animation: none; }
  .peek-countdown-bar { display: none; }
}

/* ===== Dark mode ===== */

/* Dark theme (the default). theme.js sets data-theme on <html>. */
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg-top: #1c1733;
  --bg-bottom: #2a2050;
  --panel: #241d47ee;
  --ink: #f1ecff;
  --ink-soft: #b9aee0;
}
:root[data-theme="dark"] .btn-icon,
:root[data-theme="dark"] .difficulty-card,
:root[data-theme="dark"] .level-btn,
:root[data-theme="dark"] .learn-card-face,
:root[data-theme="dark"] .mcard-front {
  background: #2f2760;
  color: var(--ink);
}
/* --ink-soft turns pale in dark theme, so white text on it was hard to read. */
:root[data-theme="dark"] .btn-secondary { background: #4b3f8a; color: #fff; }
:root[data-theme="dark"] .level-btn.completed { background: linear-gradient(160deg, #1f6b46, #14803f); color: #fff; }
:root[data-theme="dark"] .level-btn.milestone { background: linear-gradient(160deg, #8a6a1f, #a67a1a); color: #fff; }
:root[data-theme="dark"] .level-btn.locked { background: #221c40; color: var(--ink-soft); }
