@import url('https://fonts.googleapis.com/css2?family=Fredoka:wght@500;600;700&family=Inter:wght@400;500;600;700&family=JetBrains+Mono:wght@500&display=swap');

:root {
  --bg-0: #0f0f24;
  --bg-1: #15152a;
  --panel: #1a1a36;
  --panel-2: #242448;
  --border: #31315c;
  --text: #f4f3ff;
  --muted: #9a99c2;
  --accent: #8f7dff;
  --accent-dark: #5f4fd6; /* the "side" of chunky 3D buttons */
  --accent-soft: rgba(143, 125, 255, 0.18);
  --danger: #ff6b6b;
  --teal: #5eead4;
  --pink: #ff8fc7;
  --yellow: #ffd166;
  --font-fun: 'Fredoka', 'Inter', system-ui, sans-serif;
  --radius-lg: 22px;
  --radius-md: 14px;
  --radius-sm: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-0);
  overflow-x: hidden;
  color: var(--text);
  font-family: 'Inter', system-ui, -apple-system, "Segoe UI", sans-serif;
  padding: 32px 16px;
  -webkit-font-smoothing: antialiased;
}

/* Two soft color blobs drifting slowly behind everything. */
body::before,
body::after {
  content: "";
  position: fixed;
  z-index: -1;
  width: 60vmax;
  height: 60vmax;
  border-radius: 50%;
  filter: blur(80px);
  opacity: 0.35;
  pointer-events: none;
}

body::before {
  top: -25vmax;
  left: -20vmax;
  background: radial-gradient(circle, #6d5dfc, transparent 65%);
  animation: drift 22s ease-in-out infinite alternate;
}

body::after {
  bottom: -25vmax;
  right: -20vmax;
  background: radial-gradient(circle, #14b8a6, transparent 65%);
  animation: drift 26s ease-in-out infinite alternate-reverse;
}

@keyframes drift {
  from { transform: translate(0, 0) scale(1); }
  to { transform: translate(8vmax, 6vmax) scale(1.15); }
}

.wrap {
  position: relative; /* anchors the .corner-btn buttons */
  width: 100%;
  max-width: 720px;
}

.corner-btn {
  position: absolute;
  top: 0;
  z-index: 1;
  width: 38px;
  height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--panel-2);
  border: 2px solid var(--border);
  border-radius: 50%;
  box-shadow: 0 3px 0 var(--border);
  color: var(--text);
  font-family: var(--font-fun);
  font-size: 1.05rem;
  line-height: 1;
  cursor: pointer;
  transition: transform 0.1s ease, box-shadow 0.1s ease, background 0.15s ease;
}

.corner-btn:hover {
  background: var(--border);
  transform: translateY(-1px) rotate(-6deg);
}

.corner-btn:active {
  transform: translateY(3px);
  box-shadow: 0 0 0 var(--border);
}

.mute-btn { right: 0; }

.help-btn {
  left: 0;
  font-weight: 700;
  color: var(--yellow);
}

header {
  margin-bottom: 18px;
  /* Room on both sides for the .corner-btn buttons (34px + gap), so a
     wrapped title on a narrow screen can't run under them. */
  padding: 0 48px;
  text-align: center;
}

header h1 {
  margin: 0 0 10px;
  font-family: var(--font-fun);
  font-size: 2.1rem;
  font-weight: 700;
  line-height: 1.1;
  letter-spacing: 0.005em;
  color: #fff;
}

/* A row of small chips under the title; each opens its topic in the info
   popup, so the background text stays out of the way until it's wanted. */
.intro {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  margin: 0 auto 20px;
}

.intro-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 7px 15px 7px 9px;
  background: var(--panel);
  border: 2px solid var(--chip-color, var(--accent));
  border-radius: 999px;
  box-shadow: 0 3px 0 var(--chip-color, var(--accent));
  color: var(--text);
  font-family: var(--font-fun);
  font-size: 0.92rem;
  font-weight: 500;
  cursor: pointer;
  transition: transform 0.1s ease, box-shadow 0.1s ease, background 0.15s ease;
}

.intro-chip[data-topic="mdd"] { --chip-color: var(--pink); }
.intro-chip[data-topic="beck"] { --chip-color: var(--teal); }
.intro-chip[data-topic="studies"] { --chip-color: var(--yellow); }

.intro-chip::before {
  content: "i";
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--chip-color, var(--accent));
  color: var(--bg-0);
  font-size: 0.7rem;
  font-weight: 700;
}

.intro-chip:hover {
  background: var(--panel-2);
  transform: translateY(-2px);
  box-shadow: 0 5px 0 var(--chip-color, var(--accent));
}

.intro-chip:active {
  transform: translateY(3px);
  box-shadow: 0 0 0 var(--chip-color, var(--accent));
}

.game-area {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
  justify-content: center;
  align-items: flex-start;
}

/* Game over: a short shake, so crashing feels like a bump. */
.board-card.shake {
  animation: shake 0.45s ease;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  15% { transform: translateX(-8px) rotate(-1deg); }
  35% { transform: translateX(7px) rotate(1deg); }
  55% { transform: translateX(-5px); }
  75% { transform: translateX(3px); }
}

.board-card {
  position: relative;
  background: var(--panel);
  border: 2px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px;
  box-shadow: 0 6px 0 var(--border), 0 24px 50px -20px rgba(0, 0, 0, 0.6);
  /* Allowed to shrink (the canvas scales down with it) so the board fits on
     the narrowest phones (320px) without the page scrolling sideways. */
  flex: 0 1 auto;
  min-width: 0;
}

/* Overlay that sits directly on the board and blocks play until Start is
   pressed — separate from the fullscreen .overlay used for game-over/info
   so the sidebar (and its "?" info buttons) stays readable underneath. */
/* Wraps just the canvas + its overlays, so the overlays cover the board
   exactly without also covering the touch arrow pad below it. */
.board-frame {
  position: relative;
}

#board {
  display: block;
  touch-action: none; /* swipes steer the snake instead of scrolling the page */
}

.board-overlay {
  position: absolute;
  inset: 0;
  background: rgba(16, 16, 38, 0.9);
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 5;
  animation: fade-in 0.2s ease;
}

.board-overlay.hidden {
  display: none;
}

.board-overlay-content {
  text-align: center;
  max-width: 240px;
}

.board-overlay-content h2 {
  margin: 0 0 8px;
  font-family: var(--font-fun);
  font-size: 1.8rem;
  font-weight: 600;
  animation: bob 1.8s ease-in-out infinite;
}

@keyframes bob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-4px); }
}

.board-overlay-content p {
  margin: 0 0 18px;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.5;
}

.info-btn-demo {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: var(--panel-2);
  border: 1px solid var(--border);
  font-size: 0.62rem;
  vertical-align: middle;
}

canvas {
  display: block;
  background: var(--bg-1);
  border-radius: var(--radius-md);
  max-width: 100%;
  height: auto;
}

.sidebar {
  flex: 1;
  min-width: 220px;
  max-width: 330px;
  background: var(--panel);
  border: 2px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: 0 6px 0 var(--border), 0 24px 50px -20px rgba(0, 0, 0, 0.6);
}

.sidebar h2 {
  margin: 0 0 10px;
  font-family: var(--font-fun);
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
}

.sidebar h2.section-gap {
  margin-top: 18px;
  padding-top: 16px;
  border-top: 2px dashed var(--border);
}

.score-row {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin: 4px 0 18px;
  padding-bottom: 16px;
  border-bottom: 2px dashed var(--border);
}

.score-row .score-value {
  font-family: var(--font-fun);
  font-size: 2.4rem;
  font-weight: 700;
  color: var(--yellow);
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.score-row .score-label {
  color: var(--muted);
  font-size: 0.78rem;
}

.score-row .best-score {
  margin-left: auto;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--panel-2);
  color: var(--muted);
  font-family: var(--font-fun);
  font-size: 0.8rem;
  font-weight: 500;
}

/* Absolutely positioned on .board-card's top border — taking it out of the
   flow means showing/hiding it can't change any element's size (it used to
   sit in the sidebar and push the page around every time it appeared). */
.slow-badge {
  position: absolute;
  top: -12px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 6;
  white-space: nowrap;
  padding: 3px 12px;
  border-radius: 999px;
  font-family: var(--font-fun);
  background: #10283a; /* opaque, since it straddles the card's border */
  border: 2px solid rgba(56, 189, 248, 0.6); /* "Taking a Small Step" blue */
  color: #38bdf8;
  font-size: 0.75rem;
  font-weight: 600;
}

.slow-badge.hidden {
  display: none;
}

#tally-list, #final-tally, #coping-list, #final-coping {
  list-style: none;
  margin: 0 0 4px;
  padding: 0;
  font-size: 0.82rem;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#tally-list li, #final-tally li, #coping-list li, #final-coping li {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
}

#tally-list .count, #final-tally .count, #coping-list .count, #final-coping .count {
  margin-left: auto;
  min-width: 20px;
  padding: 1px 5px;
  border-radius: 999px;
  background: var(--panel-2);
  font-family: var(--font-fun);
  font-weight: 600;
  text-align: center;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

/* Count pops when that pellet was just eaten */
li.bump .count {
  animation: bump 0.45s ease;
  color: var(--text);
}

@keyframes bump {
  0% { transform: scale(1); }
  35% { transform: scale(1.45); }
  100% { transform: scale(1); }
}

/* Same shapes as on the board: squares for distortions, diamonds for coping */
.swatch {
  width: 10px;
  height: 10px;
  border-radius: 3px;
  flex-shrink: 0;
  box-shadow: 0 0 8px 1px currentColor;
}

.swatch-coping {
  width: 9px;
  height: 9px;
  border-radius: 2px;
  margin: 0 1px;
  transform: rotate(45deg);
}

.info-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 15px;
  height: 15px;
  flex-shrink: 0;
  border-radius: 50%;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.62rem;
  line-height: 1;
  cursor: pointer;
  padding: 0;
  transition: background 0.15s ease, color 0.15s ease, transform 0.15s ease;
}

.info-btn:hover {
  background: var(--accent);
  color: #fff;
  transform: scale(1.2);
}

.hint {
  color: var(--muted);
  font-size: 0.76rem;
  margin: 18px 0 0;
  padding-top: 16px;
  border-top: 2px dashed var(--border);
  line-height: 1.5;
}

kbd {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 5px;
  padding: 1px 5px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.72rem;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(8, 8, 14, 0.72);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  animation: fade-in 0.2s ease;
  /* Must beat .board-overlay's z-index (5) — it had none before (defaulted
     to auto/0), so the board's own "Ready?" overlay was painting on top of
     this fullscreen one whenever both existed, making the info modal (and
     the game-over popup) open underneath it and unreadable/unclickable. */
  z-index: 100;
}

/* Info modal can be opened from inside the game-over popup too (the final
   tallies have "?" buttons), so it needs to outrank #game-over specifically,
   not just tie with it. */
#info-modal,
#help-modal {
  z-index: 110;
}

.overlay.hidden {
  display: none;
}

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

.overlay-card {
  position: relative; /* lets a .icon-btn dismiss button sit top-right */
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 28px;
  max-width: 340px;
  width: 100%;
  text-align: center;
  border-width: 2px;
  box-shadow: 0 6px 0 var(--border), 0 30px 80px -20px rgba(0, 0, 0, 0.7);
  animation: pop-in 0.32s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes pop-in {
  from { opacity: 0; transform: scale(0.85) translateY(10px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.overlay-card h2 {
  margin: 0 0 4px;
  padding-right: 24px; /* keep clear of a .icon-btn dismiss button, when present */
  font-family: var(--font-fun);
  font-size: 1.6rem;
  font-weight: 600;
}

.overlay-card > p:first-of-type {
  color: var(--muted);
  font-size: 0.85rem;
  margin: 0 0 18px;
}

.overlay-card h3 {
  margin: 14px 0 6px;
  font-family: var(--font-fun);
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
  text-align: left;
}

.info-card {
  position: relative;
  text-align: left;
}

.info-card h2 {
  margin: 0 0 10px;
  padding-right: 24px; /* keep the title clear of the close button */
  font-size: 1.4rem;
}

.info-card #info-desc {
  margin: 0;
  color: var(--text);
  font-size: 0.88rem;
  line-height: 1.55;
}

.info-example {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 0.85rem;
  font-style: italic;
  line-height: 1.5;
}

.info-example.hidden {
  display: none;
}

.info-effect.hidden {
  display: none;
}

.info-effect {
  margin: 12px 0 0;
  padding-top: 12px;
  border-top: 2px dashed var(--border);
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.5;
}

/* The "Related studies" popup holds more text, so it's wider and scrolls. */
.info-card.wide {
  max-width: 460px;
  max-height: calc(100vh - 32px);
  overflow-y: auto;
}

.beck-diagram-wrap.hidden {
  display: none;
}

.beck-diagram {
  display: block;
  width: 100%;
  height: auto;
  margin: 16px 0 10px;
}

.beck-node {
  cursor: pointer;
  outline: none;
}

.beck-node rect {
  transition: fill 0.15s ease, stroke-width 0.15s ease;
}

.beck-node:hover rect,
.beck-node.active rect {
  fill: color-mix(in srgb, var(--c) 38%, var(--panel));
  stroke-width: 3;
}

.beck-node:focus-visible rect {
  stroke: #fff;
}

/* Two lines tall at all times, so the popup doesn't jump as it changes */
.beck-caption {
  min-height: 3.2em;
  margin: 0;
  padding: 10px 14px;
  border-radius: var(--radius-md);
  background: var(--panel-2);
  border-left: 4px solid var(--c, var(--border));
  font-size: 0.85rem;
  line-height: 1.45;
  box-sizing: content-box;
}

.beck-caption strong {
  font-family: var(--font-fun);
  font-weight: 600;
  color: var(--c);
}

@media (max-width: 420px) {
  .beck-caption {
    min-height: 4.35em; /* three lines on narrow phones */
  }
}

.beck-caption.hint {
  --c: var(--border);
  color: var(--muted);
  font-style: italic;
}

.beck-arrows line,
.beck-diagram marker path {
  stroke: var(--muted);
  stroke-width: 2.2;
  stroke-linecap: round;
}

/* Each box: a tint of its color, a solid outline and "ledge", like the chips */
.beck-boxes rect {
  fill: color-mix(in srgb, var(--c) 16%, var(--panel));
  stroke: var(--c);
  stroke-width: 2;
  filter: drop-shadow(0 3px 0 color-mix(in srgb, var(--c) 45%, transparent));
}

.beck-boxes text {
  fill: var(--text);
  font-family: var(--font-fun);
  font-size: 16px;
  font-weight: 600;
  text-anchor: middle;
  dominant-baseline: central;
}

.info-studies.hidden {
  display: none;
}

.study {
  margin-top: 14px;
  padding: 14px 16px;
  background: var(--panel-2);
  border-radius: var(--radius-md);
}

.overlay-card .study h3 {
  margin: 0 0 8px;
  color: var(--yellow);
}

.study dl {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 6px 12px;
  margin: 0;
  font-size: 0.82rem;
  line-height: 1.45;
}

.study dt {
  font-family: var(--font-fun);
  font-weight: 600;
  color: var(--muted);
}

.study dd {
  margin: 0;
}

@media (max-width: 420px) {
  .study dl {
    grid-template-columns: 1fr;
    gap: 2px;
  }

  .study dd + dt {
    margin-top: 6px;
  }
}

.icon-btn {
  position: absolute;
  top: 10px;
  right: 12px;
  background: none;
  border: none;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  padding: 4px;
}

.icon-btn:hover {
  color: var(--text);
  transform: rotate(90deg);
}

.icon-btn {
  transition: transform 0.2s ease, color 0.15s ease;
}

#final-tally, #final-coping {
  text-align: left;
  background: var(--panel-2);
  border-radius: var(--radius-md);
  padding: 12px 14px;
}

.final-score {
  font-size: 0.85rem;
  color: var(--muted);
  margin: 14px 0 0;
}

#takeaway {
  color: var(--text);
  line-height: 1.5;
}

.final-score span {
  font-family: var(--font-fun);
  color: var(--yellow);
  font-weight: 700;
  font-size: 1.3rem;
}

.help-card {
  max-width: 440px;
  max-height: calc(100vh - 32px);
  overflow-y: auto;
  text-align: left;
}

.help-list {
  margin: 0 0 14px;
  padding: 0;
  list-style: none;
  font-size: 0.88rem;
  line-height: 1.45;
}

.help-list li {
  display: flex;
  align-items: center;
  gap: 12px;
}

.help-list li + li {
  margin-top: 10px;
}

/* Small visual cue per rule: a key, a red square, a teal diamond, a wall hit. */
.help-icon {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border-radius: var(--radius-sm);
  background: var(--panel-2);
  border: 2px solid var(--border);
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
}

.help-icon-bad::after {
  content: "";
  width: 12px;
  height: 12px;
  border-radius: 3px;
  background: #e64545;
}

.help-icon-good::after {
  content: "";
  width: 10px;
  height: 10px;
  background: #5eead4;
  transform: rotate(45deg);
  box-shadow: 0 0 8px rgba(94, 234, 212, 0.7);
}

.help-tip {
  margin: 0 0 16px;
  color: var(--muted);
  font-size: 0.82rem;
}

.help-link {
  display: block;
  margin-bottom: 20px;
  padding: 12px 14px;
  background: var(--accent-soft);
  border-radius: var(--radius-md);
  color: var(--accent);
  font-size: 0.85rem;
  font-weight: 500;
  line-height: 1.45;
  text-decoration: none;
}

.help-link:hover {
  text-decoration: underline;
}

.game-over-card {
  max-width: 380px;
  max-height: calc(100vh - 32px);
  overflow-y: auto;
}

.game-over-card .help-link {
  margin: 16px 0;
  text-align: left;
}

.lesson {
  margin: 16px 0 0;
  padding-top: 14px;
  border-top: 2px dashed var(--border);
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.5;
  text-align: left;
}

/* .final-score span (above) styles the score number — reset its font here */
.final-score .new-best {
  display: inline-block;
  margin-left: 6px;
  font-family: var(--font-fun);
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--yellow);
  color: var(--bg-0);
  font-size: 0.8rem;
  font-weight: 600;
  animation: bob 1.2s ease-in-out infinite;
}

.final-score .new-best.hidden {
  display: none;
}

/* On-screen arrow pad — hidden unless the main input is a touch screen. */
.dpad {
  display: none;
}

@media (pointer: coarse) {
  .dpad {
    display: grid;
    grid-template-columns: repeat(3, 56px);
    grid-template-rows: repeat(3, 48px);
    grid-template-areas:
      ". up ."
      "left pause right"
      ". down .";
    gap: 6px;
    justify-content: center;
    margin-top: 14px;
  }

  .dpad button {
    background: var(--panel-2);
    border: 2px solid var(--border);
    border-radius: var(--radius-md);
    box-shadow: 0 3px 0 var(--border);
    color: var(--text);
    font-size: 1rem;
    touch-action: manipulation; /* no double-tap zoom when tapping quickly */
    user-select: none;
    -webkit-user-select: none;
  }

  .dpad button:active {
    background: var(--accent-soft);
    transform: translateY(3px);
    box-shadow: none;
  }

  .dpad-up { grid-area: up; }
  .dpad-left { grid-area: left; }
  .dpad-pause { grid-area: pause; font-size: 0.75rem; color: var(--muted); }
  .dpad-right { grid-area: right; }
  .dpad-down { grid-area: down; }
}

.help-card .primary-btn,
.game-over-card .primary-btn {
  display: block;
  margin: 0 auto;
}

/* Chunky "3D" buttons that press down when clicked */
#start-btn,
.primary-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-md);
  box-shadow: 0 5px 0 var(--accent-dark);
  padding: 11px 28px;
  font-family: var(--font-fun);
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  cursor: pointer;
  transition: transform 0.1s ease, box-shadow 0.1s ease, filter 0.15s ease;
}

#start-btn {
  padding: 13px 38px;
  font-size: 1.3rem;
}

#start-btn:hover,
.primary-btn:hover {
  filter: brightness(1.1);
  transform: translateY(-2px);
  box-shadow: 0 7px 0 var(--accent-dark);
}

#start-btn:active,
.primary-btn:active {
  transform: translateY(5px);
  box-shadow: 0 0 0 var(--accent-dark);
}

/* Once the sidebar wraps under the board, give the board and sidebar one
   shared width so they still line up. */
@media (max-width: 601px) {
  .sidebar {
    flex-basis: 100%;
    max-width: 330px;
  }
}


/* Respect "reduce motion": keep the look, drop the movement. */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation: none !important;
    transition: none !important;
  }
}
