:root {
  --bg: #FCE7F0;
  --card: #FFFFFF;
  --radius-lg: 24px;
  --radius-md: 16px;
  --shadow: 0 8px 24px rgba(20, 20, 50, 0.10);
  --primary: #4C4FE0;
  --primary-dark: #3B3DC0;
  --accent: #FFC94A;
  --accent-dark: #E6AE2E;
  --ink: #16163B;
  --muted: #8A8AA3;
  --success: #2FBF71;
  --danger: #FF6B6B;
  --op-addition: #FFB4C6;
  --op-subtraction: #B9F5D8;
  --op-multiplication: #4C4FE0;
  --op-division: #FFC94A;
  --op-percentage: #FF8A65;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: "Baloo 2", "Segoe UI Rounded", ui-rounded, "Inter", system-ui, sans-serif;
}

body {
  padding: 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

h1, h2, h3 { font-family: "Baloo 2", ui-rounded, system-ui, sans-serif; margin: 0 0 8px; }
p { margin: 0 0 12px; color: var(--muted); }

a { color: var(--primary); }

.page {
  width: 100%;
  max-width: 720px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  max-width: 720px;
}

.topbar .brand { font-size: 1.5rem; font-weight: 700; }

.card {
  background: var(--card);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 32px;
  width: 100%;
}

.btn {
  border: none;
  border-radius: var(--radius-md);
  padding: 16px 28px;
  font-size: 1.1rem;
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
  transition: transform 0.08s ease, box-shadow 0.08s ease;
}
.btn:active { transform: scale(0.96); }

.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-dark); }

.btn-accent { background: var(--accent); color: var(--ink); }
.btn-accent:hover { background: var(--accent-dark); }

.btn-outline {
  background: transparent;
  color: var(--primary);
  border: 2px solid var(--primary);
}

.btn-block { width: 100%; }
.btn-row { display: flex; gap: 12px; }

/* --- forms --- */
input[type="text"], input[type="password"], select {
  width: 100%;
  padding: 14px 16px;
  border-radius: var(--radius-md);
  border: 2px solid #E5E1F5;
  font-size: 1.1rem;
  font-family: inherit;
  background: white;
  color: var(--ink);
}
label { font-weight: 700; display: block; margin-bottom: 6px; }
.field { margin-bottom: 16px; }

/* --- operation tiles --- */
.op-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
}

.op-tile {
  border-radius: var(--radius-lg);
  padding: 24px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 10px;
  text-decoration: none;
  color: var(--ink);
}
.op-tile h3 { margin: 0; font-size: 1.3rem; }
.op-tile .tier-badge {
  align-self: flex-start;
  background: rgba(255,255,255,0.6);
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 0.85rem;
  font-weight: 700;
}

.op-addition { background: var(--op-addition); }
.op-subtraction { background: var(--op-subtraction); }
.op-multiplication { background: var(--op-multiplication); color: white; }
.op-multiplication .tier-badge { background: rgba(255,255,255,0.25); color: white; }
.op-division { background: var(--op-division); }
.op-percentage { background: var(--op-percentage); }

/* --- games --- */
.game-tile {
  border-radius: var(--radius-lg);
  padding: 24px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 10px;
  text-decoration: none;
  color: var(--ink);
}
.game-tile.unlocked { background: var(--accent); }
.game-tile.locked { background: #EEEBF7; color: var(--muted); }
.game-tile .game-icon { font-size: 2.2rem; }
.game-tile .game-progress {
  align-self: flex-start;
  background: rgba(255,255,255,0.6);
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 0.85rem;
  font-weight: 700;
}

.flap-button {
  width: 96px;
  height: 96px;
  border-radius: 50%;
  border: none;
  background: var(--primary);
  color: white;
  font-size: 2.2rem;
  box-shadow: var(--shadow);
  cursor: pointer;
  user-select: none;
}
.flap-button:active { transform: scale(0.92); background: var(--primary-dark); }

.equation-banner {
  text-align: center;
  font-size: 1.8rem;
  font-weight: 700;
  background: var(--card);
  border-radius: var(--radius-md);
  padding: 10px 16px;
  box-shadow: var(--shadow);
}

.game-score-badge {
  background: var(--accent);
  color: var(--ink);
  border-radius: 999px;
  padding: 6px 16px;
  font-weight: 700;
}

.game-canvas-wrap {
  position: relative;
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow);
  background: #CFE9F7;
}
.game-canvas-wrap canvas { display: block; width: 100%; height: auto; }

.game-over-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(20, 20, 50, 0.55);
}
.game-over-overlay .card {
  max-width: 320px;
  text-align: center;
}

/* --- keypad --- */
.keypad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  max-width: 360px;
  margin: 0 auto 20px;
}
.keypad button {
  aspect-ratio: 1;
  min-height: 56px;
  border-radius: var(--radius-md);
  border: none;
  background: #F1EFFB;
  color: var(--ink);
  font-size: 1.6rem;
  font-weight: 700;
  cursor: pointer;
}
.keypad button:active,
.keypad button.key-pressed { transform: scale(0.94); background: #E5E1F5; }
.keypad button.key-submit { background: var(--primary); color: white; }
.keypad button.key-submit.key-pressed { background: var(--primary-dark); }
.keypad button.key-backspace { background: var(--accent); color: var(--ink); }
.keypad button.key-backspace.key-pressed { background: var(--accent-dark); }

.passcode-display, .answer-display {
  text-align: center;
  font-size: 2.2rem;
  font-weight: 700;
  letter-spacing: 0.2em;
  min-height: 3rem;
  margin-bottom: 20px;
}

/* --- quiz play --- */
.quiz-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.progress-badge {
  background: var(--accent);
  color: var(--ink);
  border-radius: 999px;
  padding: 6px 16px;
  font-weight: 700;
}
.streak-badge {
  background: var(--success);
  color: white;
  border-radius: 999px;
  padding: 6px 16px;
  font-weight: 700;
}
.question-text {
  font-size: 3rem;
  font-weight: 700;
  text-align: center;
  margin: 24px 0;
}

.feedback-flash {
  text-align: center;
  font-size: 1.4rem;
  font-weight: 700;
  min-height: 2rem;
  margin-bottom: 8px;
}
.feedback-flash.correct { color: var(--success); animation: bounce 0.4s ease; }
.feedback-flash.incorrect { color: var(--danger); animation: shake 0.4s ease; }

@keyframes bounce {
  0% { transform: scale(0.8); }
  50% { transform: scale(1.15); }
  100% { transform: scale(1); }
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-8px); }
  75% { transform: translateX(8px); }
}

/* --- tier-up celebration --- */
.tier-up-banner {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.5);
  background: var(--primary);
  color: white;
  padding: 24px 44px;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  text-align: center;
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  animation: tier-up-pop 1.9s ease forwards;
}
.tier-up-banner h2 { margin: 0 0 4px; font-size: 2rem; }
.tier-up-banner p { margin: 0; color: rgba(255,255,255,0.85); font-weight: 700; }

@keyframes tier-up-pop {
  0%   { opacity: 0; transform: translate(-50%, -50%) scale(0.4); }
  15%  { opacity: 1; transform: translate(-50%, -50%) scale(1.15); }
  25%  { transform: translate(-50%, -50%) scale(1); }
  85%  { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -50%) scale(0.9); }
}

.confetti-piece {
  position: fixed;
  top: -20px;
  width: 10px;
  height: 10px;
  border-radius: 2px;
  z-index: 999;
  pointer-events: none;
  animation: confetti-fall linear forwards;
}
@keyframes confetti-fall {
  0%   { transform: translateY(0) rotate(0deg); opacity: 1; }
  100% { transform: translateY(110vh) rotate(720deg); opacity: 0.9; }
}

/* --- celebratory / results cards --- */
.celebrate-card {
  background: var(--primary);
  color: white;
  text-align: center;
}
.celebrate-card h2 { font-size: 1.8rem; }
.celebrate-card h2.allfives-win-title {
  font-size: 3rem;
  animation: bounce 0.5s ease;
}
.retry-card {
  background: var(--op-division);
  color: var(--ink);
  text-align: center;
}
.retry-card h2 { font-size: 1.8rem; }

/* --- tip card --- */
.tip-card { border-top: 8px solid var(--primary); }
.tip-card .example {
  background: #F1EFFB;
  border-radius: var(--radius-md);
  padding: 16px;
  font-family: ui-monospace, "SF Mono", monospace;
  font-size: 1.05rem;
}

/* --- tables (stats/admin) --- */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid #EEE; }
th { color: var(--muted); font-size: 0.85rem; text-transform: uppercase; }

.badge { display: inline-block; border-radius: 999px; padding: 4px 12px; font-size: 0.8rem; font-weight: 700; }
.badge-op { color: white; }

@media (max-width: 480px) {
  .question-text { font-size: 2.2rem; }
  .card { padding: 20px; }
}

/* --- All Fives dominoes --- */
.domino-tile {
  display: inline-flex;
  flex-shrink: 0;
  background: var(--card);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow);
  padding: 3px;
  gap: 3px;
  margin: 2px;
  cursor: default;
}
.domino-tile.double { outline: 2px solid var(--accent); outline-offset: 1px; }
.domino-tile.selectable { cursor: pointer; transition: transform 0.08s ease; }
.domino-tile.selectable:hover { transform: translateY(-4px); }
.domino-tile.selectable:active { transform: scale(0.95); }

/* Board tiles are smaller than hand tiles, to fit more of the chain on screen at once. */
.domino-tile.on-board { padding: 2px; gap: 2px; margin: 1px; }

/* Corner tile: stacked top/bottom instead of side by side, turning the chain 90°. */
.domino-tile.vertical { flex-direction: column; }
.domino-tile.vertical .domino-half + .domino-half { border-left: none; border-top: 2px solid #E5E1F5; }

.domino-half {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 2px;
  width: 26px;
  height: 26px;
  padding: 3px;
  border-radius: 6px;
  background: #F8F7FD;
}
.on-board .domino-half { width: 20px; height: 20px; gap: 1px; padding: 2px; }
.domino-half + .domino-half { border-left: 2px solid #E5E1F5; }
.domino-half span { border-radius: 50%; background: transparent; }
.pips-1 span:nth-child(5),
.pips-2 span:nth-child(1), .pips-2 span:nth-child(9),
.pips-3 span:nth-child(1), .pips-3 span:nth-child(5), .pips-3 span:nth-child(9),
.pips-4 span:nth-child(1), .pips-4 span:nth-child(3), .pips-4 span:nth-child(7), .pips-4 span:nth-child(9),
.pips-5 span:nth-child(1), .pips-5 span:nth-child(3), .pips-5 span:nth-child(5), .pips-5 span:nth-child(7), .pips-5 span:nth-child(9),
.pips-6 span:nth-child(1), .pips-6 span:nth-child(3), .pips-6 span:nth-child(4), .pips-6 span:nth-child(6), .pips-6 span:nth-child(7), .pips-6 span:nth-child(9) {
  background: var(--ink);
}

.allfives-board-wrap {
  background: #CFE9F7;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 8px;
}
.allfives-board {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-height: 70px;
  max-height: 65vh;
  overflow-y: auto;
  padding: 8px;
}
.allfives-board-row {
  display: flex;
  align-items: center;
}
.allfives-board-row.align-end { justify-content: flex-end; }
.allfives-board-corner {
  display: flex;
}
.allfives-board-corner.align-left { justify-content: flex-start; }
.allfives-board-corner.align-right { justify-content: flex-end; }

.allfives-hand {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  padding: 8px;
}

.allfives-scores {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.allfives-score-you { background: var(--primary); color: white; }

.allfives-log {
  max-height: 110px;
  overflow-y: auto;
  background: var(--card);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow);
  padding: 10px 14px;
  font-size: 0.9rem;
  color: var(--muted);
}
.allfives-log div:last-child { color: var(--ink); font-weight: 700; }

.allfives-win-title {
  font-size: 3rem !important;
  line-height: 1.1;
  animation: win-title-pop 0.5s ease;
}
@keyframes win-title-pop {
  0%   { transform: scale(0.5); opacity: 0; }
  60%  { transform: scale(1.15); opacity: 1; }
  100% { transform: scale(1); }
}

/* --- Tetris --- */
.tetris-layout {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  gap: 16px;
}
.tetris-board-wrap {
  max-width: 300px;
}
.tetris-side {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding-top: 4px;
}
.tetris-next-label {
  font-weight: 700;
  color: var(--ink);
  margin: 0;
}
.tetris-side canvas {
  background: #16163B;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow);
}
.tetris-controls {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 20px;
}
.tetris-btn {
  min-width: 56px;
  min-height: 56px;
  font-size: 1.3rem;
  padding: 0;
  touch-action: none;
  user-select: none;
}

@media (max-width: 480px) {
  .tetris-layout { gap: 8px; }
  .tetris-board-wrap { max-width: 240px; }
  .tetris-side canvas { width: 80px; height: 80px; }
}
