:root {
  --field: #050810;
  --panel: rgba(12, 20, 36, 0.94);
  --panel-solid: #0c1424;
  --panel-line: #2d405d;
  --text: #e2e8f0;
  --text-bright: #eef3ff;
  --muted: #9eabc0;
  --cyan: #67e8f9;
  --cyan-hot: #22d3ee;
  --add: #4ade80;
  --subtract: #fb7185;
  --multiply: #facc15;
  --divide: #c084fc;
  --ink: #050810;
  --radius: 14px;
  --tile-cap: 76px;
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  background: var(--field);
  color: var(--text);
  font-family: "Baloo 2", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

button { color: inherit; font: inherit; }
button:focus-visible, a:focus-visible { outline: 3px solid var(--cyan-hot); outline-offset: 3px; }

/* Shared topbar — mirrors the Twistris/DMC hub-back pill */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; max-width: 1100px; margin: 0 auto;
}
.hub-back {
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 44px; padding: 0 14px; border: 1px solid var(--panel-line);
  border-radius: 999px; color: #dfe7f5; background: #111c2d;
  font-size: 13px; font-weight: 800; text-decoration: none;
  transition: border-color .15s ease, background .15s ease, transform .15s ease;
}
.hub-back:hover { border-color: var(--cyan-hot); background: #16243a; transform: translateY(-1px); }
.wordmark {
  font-family: "Press Start 2P", monospace; font-size: 12px;
  color: var(--cyan); letter-spacing: 1px;
}

#app { max-width: 1100px; margin: 0 auto; padding: 0 16px 28px; }

/* ---- Buttons ---- */
.btn {
  display: block; width: 100%; min-height: 52px; margin: 10px 0;
  border: 2px solid var(--panel-line); border-radius: var(--radius);
  background: var(--panel-solid); color: var(--text-bright);
  font-size: 20px; font-weight: 800; cursor: pointer;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.55);
  transition: transform .12s ease, border-color .12s ease;
}
.btn:hover { transform: translateY(-2px); border-color: var(--cyan-hot); }
.btn:active { transform: translateY(1px); box-shadow: 0 2px 0 rgba(0, 0, 0, 0.55); }
.btn-primary { border-color: var(--cyan); color: var(--ink); background: var(--cyan); }
.btn-primary:hover { border-color: var(--cyan-hot); background: var(--cyan-hot); }
.icon-btn {
  min-width: 44px; min-height: 44px; border: 1px solid var(--panel-line);
  border-radius: 12px; background: var(--panel-solid); cursor: pointer;
  font-size: 18px; line-height: 1;
}
.icon-btn:hover { border-color: var(--cyan-hot); }

/* ---- Menu ---- */
.menu { max-width: 420px; margin: 6vh auto 0; text-align: center; }
.menu-title {
  font-family: "Press Start 2P", monospace; font-size: clamp(20px, 5vw, 34px);
  color: var(--text-bright); line-height: 1.5; margin: 0 0 8px;
  text-shadow: 0 0 24px rgba(34, 211, 238, 0.35);
}
.menu-sub { color: var(--muted); margin: 0 0 22px; }
.menu-progress { color: var(--muted); font-weight: 700; margin: 14px 0 4px; }
.menu-stars { color: var(--multiply); font-weight: 800; }

/* ---- Level select ---- */
.level-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(88px, 1fr));
  gap: 12px; max-width: 560px; margin: 20px auto;
}
.level-card {
  position: relative; min-height: 76px; border: 2px solid var(--panel-line);
  border-radius: var(--radius); background: var(--panel-solid);
  color: var(--text-bright); font-size: 24px; font-weight: 800; cursor: pointer;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.55);
}
.level-card:hover:not(:disabled) { border-color: var(--cyan-hot); transform: translateY(-2px); }
.level-card:disabled { color: var(--muted); cursor: not-allowed; opacity: 0.55; }
.level-card .stars { display: block; font-size: 12px; letter-spacing: 2px; color: var(--multiply); }
.level-card.done { border-color: var(--add); }
.screen-heading {
  display: flex; align-items: center; gap: 12px; margin: 8px 0 4px;
}
.screen-heading h1 {
  font-family: "Press Start 2P", monospace; font-size: 16px; color: var(--text-bright); margin: 0;
}

/* ---- Game screen ---- */
.game-header {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  margin: 4px 0 10px;
}
.game-header .level-label { font-family: "Press Start 2P", monospace; font-size: 13px; color: var(--text-bright); margin: 0; }
.game-header .timer { color: var(--muted); font-weight: 700; font-variant-numeric: tabular-nums; }

.total-bar {
  display: flex; align-items: baseline; justify-content: center; gap: 10px;
  margin: 6px 0 14px;
}
.total-label { font-family: "Press Start 2P", monospace; font-size: 10px; color: var(--muted); }
.total-value {
  font-size: clamp(40px, 9vw, 64px); font-weight: 800; color: var(--text-bright);
  min-width: 2ch; text-align: center; line-height: 1;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 0 22px rgba(34, 211, 238, 0.4);
}
.total-value.invalid { color: var(--subtract); }

.stage { position: relative; touch-action: none; margin: 0 auto; max-width: 100%; }
.grid {
  --gap: 12px;
  --tile: min(var(--tile-cap), calc((100vw - 36px - (var(--cols, 4) - 1) * var(--gap)) / var(--cols, 4)));
  position: relative; display: grid; gap: var(--gap); justify-content: center;
  margin: 0 auto; width: max-content;
}
.tile {
  width: var(--tile, 76px); height: var(--tile, 76px);
  display: flex; align-items: center; justify-content: center;
  border-radius: 16px; border: 3px solid var(--ink);
  font-size: calc(var(--tile, 76px) * 0.34); font-weight: 800; color: var(--ink);
  box-shadow: 0 5px 0 rgba(0, 0, 0, 0.6);
  user-select: none; -webkit-user-select: none; cursor: pointer;
}
.tile.add { background: var(--add); }
.tile.subtract { background: var(--subtract); }
.tile.multiply { background: var(--multiply); }
.tile.divide { background: var(--divide); }
.tile.selected {
  transform: scale(1.08); border-color: var(--cyan-hot);
  box-shadow: 0 0 0 3px rgba(34, 211, 238, 0.5), 0 5px 0 rgba(0, 0, 0, 0.6);
}
.drag-line { position: absolute; inset: 0; pointer-events: none; overflow: visible; }
.drag-line polyline {
  fill: none; stroke: var(--cyan); stroke-width: 6; stroke-linecap: round;
  stroke-linejoin: round; opacity: 0.85; filter: drop-shadow(0 0 6px rgba(34, 211, 238, 0.7));
}

.targets {
  display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; margin: 18px auto 6px;
}
.targets-label {
  width: 100%; text-align: center; font-family: "Press Start 2P", monospace;
  font-size: 10px; color: var(--muted); margin-bottom: 2px;
}
.target {
  min-width: 58px; padding: 8px 12px 6px; text-align: center;
  border: 2px solid var(--cyan); border-radius: 12px; background: var(--panel);
  color: var(--cyan); font-size: 22px; font-weight: 800;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.6);
}
.target .dots { display: block; font-size: 9px; letter-spacing: 3px; color: var(--muted); }
.target.matched { border-color: var(--add); color: var(--add); opacity: 0.85; }
.target.flash { animation: target-pop 0.4s ease; }

/* ---- Overlays ---- */
.overlay {
  position: fixed; inset: 0; z-index: 30; display: flex; align-items: center;
  justify-content: center; background: rgba(5, 8, 16, 0.74);
  padding: max(20px, env(safe-area-inset-top)) max(20px, env(safe-area-inset-right)) max(20px, env(safe-area-inset-bottom)) max(20px, env(safe-area-inset-left));
}
.modal {
  width: min(420px, 100%); border: 2px solid var(--panel-line);
  border-radius: 18px; background: var(--panel); padding: 22px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
  max-height: calc(100dvh - 40px); overflow-y: auto;
}
.modal h2 { font-family: "Press Start 2P", monospace; font-size: 15px; color: var(--text-bright); margin: 0 0 14px; text-align: center; }
.modal .fact { color: var(--muted); font-size: 15px; line-height: 1.5; text-align: center; margin: 12px 0; }
.modal .big-stars { text-align: center; font-size: 34px; letter-spacing: 6px; color: var(--multiply); margin: 6px 0; }
.modal .rules { color: var(--muted); line-height: 1.55; font-size: 15px; }
.modal .rules strong { color: var(--text-bright); }
.option-row { display: flex; align-items: center; justify-content: space-between; margin: 12px 0; font-weight: 700; }

.toast {
  position: fixed; left: 50%; bottom: 9%; transform: translateX(-50%);
  z-index: 40; padding: 10px 18px; border-radius: 999px;
  border: 1px solid var(--subtract); background: var(--panel);
  color: var(--subtract); font-weight: 800; pointer-events: none;
  animation: toast-in 0.2s ease;
}

/* ---- FX ---- */
.pop { animation: target-pop 0.35s ease; }
.shake { animation: shake 0.3s ease; }
.particle {
  position: fixed; z-index: 50; width: 8px; height: 8px; border-radius: 50%;
  pointer-events: none;
}
@keyframes target-pop { 40% { transform: scale(1.25); } }
@keyframes shake {
  20% { transform: translateX(-6px); } 40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); } 80% { transform: translateX(4px); }
}
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 8px); } }
@keyframes particle-fly { to { transform: translate(var(--dx), var(--dy)) scale(0.2); opacity: 0; } }

/* Desktop scales the board up; portrait gets the viewport */
@media (min-width: 900px) { :root { --tile-cap: 92px; } }
@media (max-width: 480px) { .grid { --gap: 9px; } }

@media (prefers-reduced-motion: reduce) {
  .pop, .shake, .target.flash, .toast { animation: none; }
  .particle { display: none; }
  .btn, .hub-back, .level-card, .tile { transition: none; }
}
