/* Base theme (Färgglad). Every [data-theme] element re-computes these, so
   theme previews work anywhere on the page. Other themes: css/themes.css */
:root,
[data-theme] {
  --font: 'Fredoka', 'Baloo 2', 'Trebuchet MS', system-ui, sans-serif;
  --ink: #2b2d42;
  --muted: #6b7090;
  --card: #ffffff;
  --bg-top: #fff4de;
  --bg-bottom: #e3f1ff;
  --accent: #8f6bff;
  --accent-2: #ff6fb5;
  --sel: #ffe680;
  --sel-ring: #ffb703;
  --related: #eef1ff;
  --good: #1fae62;
  --good-bg: #c6f6d8;
  --bad: #e63956;
  --bad-bg: #ffd1da;
  --gold: #ffc93c;
  --shadow: 0 6px 0 rgba(43, 45, 66, 0.12), 0 12px 24px rgba(43, 45, 66, 0.1);
  --font-head: var(--font);
  --cell: #ffffff;
  --soft: #eef1ff;
  --panel: rgba(255, 255, 255, 0.7);
  --grad: linear-gradient(135deg, var(--accent), var(--accent-2));
  --title-grad: linear-gradient(90deg, var(--d1), var(--d2), var(--d4), var(--d6), var(--d7));
  --box-bg: rgba(255, 255, 255, 0.4);
  --box-bg-alt: rgba(255, 255, 255, 0.22);
  --continue-bg: linear-gradient(135deg, #21c07a, #0fa9c0);
  --check-bg: linear-gradient(160deg, #3ddc84, var(--good));
  --check-shadow: 0 6px 0 #137a43, 0 12px 20px rgba(31, 174, 98, 0.3);
  --lvl-easy: linear-gradient(160deg, #3ddc84, #1fae62);
  --lvl-medium: linear-gradient(160deg, #ffb347, #f7801a);
  --lvl-hard: linear-gradient(160deg, #ff7a9a, #e63956);
  --best-bg: #fff3c4;
  --best-ink: #a06b00;
  --record-bg: linear-gradient(160deg, #fffbea, #fff1c2);
  --toast-bg: var(--ink);
  --blob1: #ffc6e0;
  --blob2: #c7e8ff;
  --blob3: #d9ffcf;
  --entry: initial; /* grown-up themes colour typed answers; others use the digit colour */

  /* One colour per number */
  --d1: #ef476f;
  --d2: #f7801a;
  --d3: #d99a00;
  --d4: #20a75a;
  --d5: #0fa9c0;
  --d6: #3a70f5;
  --d7: #8b5cf6;
  --d8: #e0409a;
  --d9: #a0522d;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  height: 100%;
}

body {
  font-family: var(--font);
  color: var(--ink);
  background: linear-gradient(180deg, var(--bg-top), var(--bg-bottom));
  background-color: var(--bg-top);
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
}

h1,
h2,
.mode-name,
.lb-level,
.record-banner {
  font-family: var(--font-head);
}

button {
  font-family: inherit;
  color: inherit;
  cursor: pointer;
  touch-action: manipulation;
}

button:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- Floating background ---------- */

.sky {
  position: fixed;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 0;
}

.blob {
  position: absolute;
  width: 40vmax;
  height: 40vmax;
  border-radius: 50%;
  filter: blur(60px);
  opacity: 0.35;
  animation: drift 25s ease-in-out infinite;
}

.b1 { background: var(--blob1); top: -15vmax; left: -10vmax; }
.b2 { background: var(--blob2); bottom: -18vmax; right: -12vmax; animation-delay: -8s; }
.b3 { background: var(--blob3); top: 40%; left: 40%; width: 25vmax; height: 25vmax; animation-delay: -15s; }

@keyframes drift {
  0%, 100% { transform: translate(0, 0) rotate(0deg); }
  33% { transform: translate(18px, -24px) rotate(8deg); }
  66% { transform: translate(-14px, 14px) rotate(-6deg); }
}

/* ---------- Screens ---------- */

.screen {
  position: relative;
  z-index: 1;
  height: 100vh;
  height: 100dvh;
}

/* ---------- Home ---------- */

.home {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  padding: max(40px, calc(env(safe-area-inset-top) + 24px)) 16px 32px;
  overflow-y: auto;
  text-align: center;
}

.logo {
  margin: 8px 0 0;
  font-size: clamp(2.6rem, 11vw, 4.6rem);
  font-weight: 700;
  line-height: 1;
  letter-spacing: 1px;
}

.logo-word span {
  display: inline-block;
  text-shadow: 0 4px 0 rgba(43, 45, 66, 0.15);
  animation: hop 2.4s ease-in-out infinite;
}

.logo-word .space {
  width: 0.35em;
}

@keyframes hop {
  0%, 60%, 100% { transform: translateY(0) rotate(0); }
  30% { transform: translateY(-12px) rotate(-6deg); }
}

.tagline {
  margin: 0;
  color: var(--muted);
  font-size: 1.1rem;
  max-width: 28em;
}

.step {
  margin: 12px 0 0;
  font-size: 1.35rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 10px;
}

.step-num {
  display: inline-grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 1.05rem;
}

.continue {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 26px 12px 14px;
  border: none;
  border-radius: 999px;
  background: var(--continue-bg);
  color: #fff;
  font-size: 1.2rem;
  text-align: left;
  box-shadow: var(--shadow);
  animation: breathe 2.2s ease-in-out infinite;
}

.continue small {
  display: block;
  font-size: 0.85rem;
  opacity: 0.9;
}

.continue-icon {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.25);
}

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

.modes {
  display: flex;
  gap: 10px;
  justify-content: center;
  width: 100%;
  max-width: 460px;
}

.mode-card {
  position: relative;
  flex: 1;
  min-width: 0;
  max-width: 210px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 16px 12px 14px;
  border: 4px solid transparent;
  border-radius: 24px;
  background: var(--card);
  box-shadow: var(--shadow);
  transition: transform 0.2s, border-color 0.2s;
}

.mode-card:hover {
  transform: translateY(-3px);
}

.mode-card[aria-checked='true'] {
  border-color: var(--accent);
  transform: translateY(-3px) scale(1.03);
}

.mode-card[aria-checked='true']::after {
  content: '✓';
  position: absolute;
  top: -12px;
  right: -12px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 1.2rem;
  font-weight: 700;
  display: grid;
  place-items: center;
  animation: pop 0.35s cubic-bezier(0.3, 1.6, 0.5, 1);
}

.mode-name {
  font-size: clamp(1.05rem, 4.6vw, 1.5rem);
  font-weight: 700;
}

.mode-sub {
  font-size: clamp(0.75rem, 3.2vw, 0.9rem);
  color: var(--muted);
}

.preview {
  width: 100%;
  aspect-ratio: 1;
  max-width: 130px;
  display: grid;
  gap: 4px;
  padding: 5px;
  border-radius: 14px;
  background: var(--grad);
}

.preview .pbox {
  display: grid;
  gap: 2px;
  padding: 2px;
  border-radius: 6px;
  background: var(--box-bg);
}

.preview .pcell {
  border-radius: 3px;
  background: var(--cell);
}

.preview .pcell.dot {
  background: var(--dc);
}

.levels {
  display: flex;
  gap: 10px;
  justify-content: center;
  width: 100%;
  max-width: 460px;
}

.level {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  flex: 1;
  min-width: 0;
  max-width: 150px;
  padding: 14px 6px 10px;
  border: none;
  border-radius: 22px;
  font-size: 1.2rem;
  font-weight: 600;
  color: #fff;
  box-shadow: var(--shadow);
  transition: transform 0.15s;
}

.level:active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 rgba(43, 45, 66, 0.12);
}

.level:hover {
  transform: translateY(-3px) rotate(-2deg);
}

.level[data-level='easy'] { background: var(--lvl-easy); }
.level[data-level='medium'] { background: var(--lvl-medium); }
.level[data-level='hard'] { background: var(--lvl-hard); }

.level-icon {
  font-size: 2.3rem;
  line-height: 1.1;
}

.level small {
  color: #fff8c8;
  letter-spacing: 2px;
}

/* Player picker */
.players {
  display: flex;
  gap: 10px;
  justify-content: center;
  width: 100%;
  max-width: 460px;
}

.player-card {
  --pc: var(--accent);
  position: relative;
  flex: 1;
  min-width: 0;
  display: flex;
}

.player-btn {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 10px 4px 8px;
  border: 4px solid transparent;
  border-radius: 20px;
  background: var(--card);
  box-shadow: var(--shadow);
  transition: transform 0.15s, border-color 0.2s, background-color 0.2s;
}

.player-icon {
  font-size: 2.1rem;
  line-height: 1.1;
}

.player-name {
  font-size: 1.05rem;
  font-weight: 600;
}

.player-btn[aria-checked='true'] {
  border-color: var(--pc);
  background: color-mix(in srgb, var(--pc) 16%, var(--cell));
  transform: translateY(-3px) scale(1.05);
}

.player-btn[aria-checked='true'] .player-icon {
  animation: hop 1.6s ease-in-out infinite;
}

.player-edit {
  position: absolute;
  top: -10px;
  right: -8px;
  z-index: 2;
  width: 34px;
  height: 34px;
  padding: 0;
  border: 2px solid var(--pc);
  border-radius: 50%;
  background: var(--cell);
  font-size: 0.95rem;
  box-shadow: 0 2px 6px rgba(43, 45, 66, 0.25);
  transition: transform 0.15s;
}

.player-edit:active {
  transform: scale(0.88);
}

.loading-card {
  margin: auto;
  padding: 26px 30px;
  border-radius: 24px;
  background: var(--card);
  color: var(--ink);
  text-align: center;
  font-size: 1.1rem;
  box-shadow: var(--shadow);
}

.loading-card p {
  margin: 10px 0 0;
}

.loading-spin {
  font-size: 2.6rem;
  animation: spin 1.1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Saved games */
.games-btn {
  min-height: 40px;
  margin-top: -4px;
  padding: 6px 18px;
  border: none;
  border-radius: 999px;
  background: var(--card);
  color: var(--ink);
  font-size: 1rem;
  font-weight: 600;
  box-shadow: var(--shadow);
}

.games-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: min(60dvh, 520px);
  margin: 0 0 16px;
  padding: 0;
  overflow-y: auto;
  list-style: none;
  text-align: left;
}

.saved-game {
  --pc: var(--accent);
  display: flex;
  align-items: stretch;
  gap: 6px;
}

.saved-open {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 10px 12px;
  border: none;
  border-left: 5px solid var(--pc);
  border-radius: 14px;
  background: var(--soft);
  color: var(--ink);
  text-align: left;
}

.saved-title {
  font-weight: 600;
}

.saved-meta {
  font-size: 0.85rem;
  color: var(--muted);
}

.saved-bar {
  height: 6px;
  border-radius: 3px;
  background: color-mix(in srgb, var(--pc) 18%, var(--cell));
  overflow: hidden;
}

.saved-bar span {
  display: block;
  height: 100%;
  border-radius: 3px;
  background: var(--pc);
}

.saved-delete {
  flex: none;
  width: 48px;
  padding: 0 4px;
  border: none;
  border-radius: 14px;
  background: var(--soft);
  font-size: 1.1rem;
}

.saved-delete.confirm {
  width: auto;
  background: var(--bad);
  color: #fff;
  font-size: 0.85rem;
  font-weight: 600;
}

.saved-empty {
  color: var(--muted);
  text-align: center;
}

/* Theme button and picker */
.theme-btn {
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  right: 12px;
  z-index: 2;
  width: 44px;
  height: 44px;
  font-size: 1.25rem;
}

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

.theme-card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px;
  border: 3px solid transparent;
  border-radius: 18px;
  background: linear-gradient(180deg, var(--bg-top), var(--bg-bottom));
  color: var(--ink);
  font-family: var(--font);
  box-shadow: var(--shadow);
}

.theme-card[aria-checked='true'] {
  border-color: var(--sel-ring);
}

.tp-board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 3px;
  padding: 3px;
  border-radius: 10px;
  background: var(--grid-thick, var(--grad));
}

.tp-cell {
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  border-radius: 6px;
  background: var(--cell);
  color: var(--entry, var(--dc));
  font-size: 1.05rem;
  font-weight: 700;
}

.tp-cell.g {
  background: var(--given-bg, color-mix(in srgb, var(--dc) 20%, var(--cell)));
  color: var(--dc);
}

.tp-name {
  font-size: 1rem;
  font-weight: 600;
}

/* Avatar picker */
.picker-card {
  --pc: var(--accent);
  margin: auto;
  width: min(92vw, 420px);
  padding: 22px 18px 18px;
  border-radius: 28px;
  background: var(--card);
  text-align: center;
  box-shadow: 0 0 0 4px var(--pc), var(--shadow);
  animation: bounce-in 0.45s cubic-bezier(0.3, 1.6, 0.5, 1);
}

.picker-card h2 {
  margin: 0 0 14px;
  font-size: 1.5rem;
}

.avatar-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(52px, 1fr));
  gap: 8px;
  margin-bottom: 16px;
}

.avatar-option {
  aspect-ratio: 1;
  padding: 0;
  border: none;
  border-radius: 16px;
  background: var(--soft);
  font-size: 1.9rem;
  line-height: 1;
  animation: pop 0.35s cubic-bezier(0.3, 1.6, 0.5, 1) both;
  transition: transform 0.12s;
}

.avatar-option:active {
  transform: scale(0.9);
}

.avatar-option[aria-checked='true'] {
  background: color-mix(in srgb, var(--pc) 22%, var(--cell));
  box-shadow: inset 0 0 0 3px var(--pc);
}

.picker-card .big-btn {
  width: 100%;
}

.players.wiggle {
  animation: shake 0.45s ease;
}

.levels.locked .level {
  filter: grayscale(0.7);
  opacity: 0.55;
}

/* Leaderboard */
.leaderboard {
  width: 100%;
  max-width: 760px;
  margin-top: 18px;
  padding: 16px 12px;
  border-radius: 26px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.lb-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
  padding: 0 4px;
}

.lb-title {
  margin: 0;
  font-size: 1.5rem;
}

.lb-note {
  margin: 2px 4px 0;
  text-align: left;
  font-size: 0.85rem;
  color: var(--muted);
}

.lb-status {
  font-size: 0.85rem;
  color: var(--muted);
}

.lb-tabs {
  display: flex;
  gap: 6px;
  margin: 12px 0;
  padding: 4px;
  border-radius: 999px;
  background: var(--soft);
}

.lb-tab {
  flex: 1;
  min-height: 42px;
  border: none;
  border-radius: 999px;
  background: transparent;
  font-size: 1rem;
  font-weight: 600;
  color: var(--muted);
}

.lb-tab[aria-selected='true'] {
  background: var(--grad);
  color: #fff;
  box-shadow: 0 3px 8px rgba(143, 107, 255, 0.35);
}

.lb-levels {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 10px;
}

.lb-card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  border-radius: 18px;
  background: var(--card);
  text-align: left;
  box-shadow: 0 3px 0 rgba(43, 45, 66, 0.08);
}

.lb-card-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.lb-level {
  font-size: 1.15rem;
  font-weight: 700;
}

.lb-count {
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--soft);
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 600;
}

.lb-best {
  margin: 0;
  padding: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.lb-best li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 8px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--pc) 10%, var(--cell));
}

.lb-best li.me {
  box-shadow: inset 0 0 0 2px var(--pc);
}

.lb-medal {
  font-size: 1.2rem;
}

.lb-player {
  flex: 1;
  font-weight: 600;
}

.lb-time {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.lb-time small {
  margin-left: 4px;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--muted);
}

.lb-empty {
  margin: 4px 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.lb-played {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.lb-chip {
  min-height: 32px;
  padding: 2px 10px;
  border: none;
  border-radius: 999px;
  background: color-mix(in srgb, var(--pc) 14%, var(--cell));
  font-size: 0.9rem;
  font-weight: 600;
}

.lb-chip[aria-pressed='true'] {
  background: var(--pc);
  color: #fff;
  box-shadow: 0 2px 6px color-mix(in srgb, var(--pc) 45%, transparent);
}

.lb-who {
  padding: 0;
  border: none;
  background: none;
  text-align: left;
  font-size: inherit;
}

.lb-date {
  font-weight: 500;
  color: var(--muted);
}

.lb-rank {
  display: inline-grid;
  place-items: center;
  width: 1.3rem;
  height: 1.3rem;
  border-radius: 50%;
  background: var(--soft);
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
}

/* ---------- Game ---------- */

.game {
  display: flex;
  flex-direction: column;
  /* Header, board and controls stay together as one centred group, with the
     spare height split above and below. The top padding keeps the header
     clear of the browser's blurred top edge. */
  justify-content: center;
  padding-top: max(28px, calc(env(safe-area-inset-top) + 16px));
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  /* Space around the board used by header, number pad and actions */
  --chrome: 300px;
  --board-size: min(calc(100vw - 12px), calc(100dvh - var(--chrome)), 640px);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  width: var(--board-size);
  margin: 0 auto;
  padding: 0 0 12px;
}

.round-btn {
  flex: none;
  width: 48px;
  height: 48px;
  border: none;
  border-radius: 50%;
  background: var(--card);
  font-size: 1.4rem;
  box-shadow: var(--shadow);
  transition: transform 0.15s;
}

.round-btn:active {
  transform: scale(0.9);
}

.topbar-title {
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.15;
}

.mini-logo {
  font-size: 1.4rem;
  font-weight: 700;
  background: var(--title-grad);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.timer {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 12px;
  border-radius: 999px;
  background: var(--card);
  box-shadow: 0 3px 0 rgba(43, 45, 66, 0.1);
  font-size: 1.35rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.timer-icon {
  font-size: 1.05rem;
}

.timer-best {
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--best-bg);
  color: var(--best-ink);
  font-size: 0.85rem;
  font-weight: 600;
}

.chip {
  font-size: 0.95rem;
  color: var(--muted);
  font-weight: 500;
}

.play-area {
  flex: 0 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 0 6px;
}

.board-wrap {
  width: var(--board-size);
  flex: none;
}

.board {
  container-type: inline-size;
  width: 100%;
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(var(--boxes-across), 1fr);
  grid-template-rows: repeat(var(--boxes-down), 1fr);
  gap: 1.1%;
  padding: 1.2%;
  border-radius: 18px;
  background: var(--grad);
  box-shadow: var(--shadow);
}

.box {
  display: grid;
  grid-template-columns: repeat(var(--box-cols), 1fr);
  grid-template-rows: repeat(var(--box-rows), 1fr);
  gap: 2px;
  padding: 2px;
  min-width: 0;
  min-height: 0;
  border-radius: 12px;
  background: var(--box-bg);
}

.box.alt {
  background: var(--box-bg-alt);
}

.cell {
  --dc: var(--ink);
  position: relative;
  min-width: 0;
  min-height: 0;
  padding: 0;
  border: none;
  border-radius: 22%;
  background: var(--cell);
  color: var(--entry, var(--dc));
  font-weight: 500;
  font-size: calc(100cqi / var(--n) * 0.56);
  line-height: 1;
  display: grid;
  place-items: center;
  transition: background-color 0.3s, box-shadow 0.2s, transform 0.15s;
  -webkit-touch-callout: none; /* no iOS menu on long press */
}

.cell.given {
  font-weight: 600;
  background: color-mix(in srgb, var(--dc) 20%, var(--cell));
  color: color-mix(in srgb, var(--dc) 82%, black);
  cursor: default;
}

.cell.related:not(.given) {
  background: var(--related);
}

.cell.related.given {
  background: color-mix(in srgb, var(--dc) 26%, var(--related));
}

/* Numbers are medium weight; the ones equal to the selected number (or the
   stamp) are bold. */
.cell.same {
  font-weight: 700;
}

.cell.selected {
  background: var(--sel);
  box-shadow: inset 0 0 0 3px var(--sel-ring), 0 0 12px rgba(255, 183, 3, 0.7);
  transform: scale(1.06);
  z-index: 2;
}

.cell.hinted::after {
  content: '✨';
  position: absolute;
  top: 1px;
  right: 2px;
  font-size: 32%;
}

/* Tectonic: a grid of cells with the grid and region lines drawn on top as
   one SVG (smooth, round joins). --tint is set per cell; grown-up themes
   switch the tints off. */
.board.tectonic {
  --edge: var(--grid-thick, var(--accent));
  --line: color-mix(in srgb, var(--ink) 18%, transparent);
  display: block;
  padding: 1.6%;
}

.tgrid {
  position: relative;
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(var(--cols), 1fr);
  grid-template-rows: repeat(var(--rows), 1fr);
  overflow: hidden;
  border-radius: 12px;
  background: var(--card);
}

/* The outer frame, above the cells and the lines */
.tgrid::after {
  content: '';
  position: absolute;
  inset: 0;
  border: 3.5px solid var(--edge);
  border-radius: inherit;
  pointer-events: none;
}

.tlines {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  overflow: visible;
}

.tlines path {
  fill: none;
  vector-effect: non-scaling-stroke;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.tlines .thin {
  stroke: var(--line);
  stroke-width: 1px;
}

.tlines .thick {
  stroke: var(--edge);
  stroke-width: 3.5px;
}

.board.tectonic .cell {
  --cell: color-mix(in srgb, var(--region-tint, var(--tint)) 14%, var(--card));
  border-radius: 0;
}

.board.tectonic .cell.selected {
  transform: none;
}

/* Givens keep their region's colour, just a little darker */
.board.tectonic .cell.given {
  background: color-mix(in srgb, var(--ink) 7%, var(--cell));
}

/* The selected cell's region: a see-through layer, so the region colour stays */
.board.tectonic .cell.related,
.board.tectonic .cell.related.given {
  background: linear-gradient(var(--region-hl), var(--region-hl)), var(--cell);
}

.board.tectonic {
  --region-hl: color-mix(in srgb, var(--sel) 45%, transparent);
}

.preview.tec {
  position: relative;
  gap: 0;
  padding: 0;
  overflow: hidden;
  border: 2px solid var(--grid-thick, var(--ink));
  border-radius: 8px;
  background: var(--card);
}

.preview .tcell {
  display: grid;
  place-items: center;
  border-radius: 0;
  background: color-mix(in srgb, var(--region-tint, var(--tint)) 16%, var(--card));
  color: var(--dc);
  font-size: 0.7rem;
  font-weight: 700;
}

.preview.tec .tlines .thick {
  stroke: var(--grid-thick, var(--ink));
  stroke-width: 2px;
}

/* Candidate notes: small italic numbers that shrink as more are added */
.notes {
  display: grid;
  grid-template-columns: repeat(var(--cols), 1fr);
  place-items: center;
  align-content: center;
  width: 100%;
  height: 100%;
  padding: 8%;
  font-size: calc(100cqi / var(--n) * var(--nf));
  font-style: italic;
  font-weight: 500;
  line-height: 1.05;
}

.note {
  min-width: 1em;
  border-radius: 40%;
  color: var(--dc);
  text-align: center;
}

.note.hl {
  font-weight: 700;
}

/* Check feedback */
.cell.correct {
  background: var(--good-bg);
  box-shadow: inset 0 0 0 3px var(--good);
  animation: yay 0.5s ease;
}

.cell.wrong {
  background: var(--bad-bg);
  box-shadow: inset 0 0 0 3px var(--bad);
  animation: shake 0.45s ease;
}

.cell.correct::before,
.cell.wrong::before {
  position: absolute;
  top: -8%;
  right: -8%;
  width: 40%;
  height: 40%;
  border-radius: 50%;
  color: #fff;
  font-size: 28%;
  display: grid;
  place-items: center;
  z-index: 3;
}

.cell.correct::before {
  content: '✓';
  background: var(--good);
}

.cell.wrong::before {
  content: '✗';
  background: var(--bad);
}

.cell.pop {
  animation: pop 0.35s cubic-bezier(0.3, 1.6, 0.5, 1);
}

.cell.wiggle,
.pad-btn.wiggle,
.board.wiggle {
  animation: shake 0.4s ease;
}

.cell.wave {
  animation: wave 0.7s ease both;
  animation-delay: calc(var(--i) * 70ms);
}

.cell.rainbow {
  animation: rainbow 1.2s ease both;
  animation-delay: calc(var(--i) * 18ms);
}

@keyframes pop {
  0% { transform: scale(0.4); }
  60% { transform: scale(1.18); }
  100% { transform: scale(1); }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-5px) rotate(-3deg); }
  40% { transform: translateX(5px) rotate(3deg); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(3px); }
}

@keyframes yay {
  0% { transform: scale(1); }
  40% { transform: scale(1.12); }
  100% { transform: scale(1); }
}

@keyframes wave {
  0%, 100% { transform: translateY(0) scale(1); }
  40% { transform: translateY(-22%) scale(1.15) rotate(-8deg); box-shadow: 0 0 0 3px var(--gold), 0 0 16px var(--gold); }
}

@keyframes rainbow {
  0% { transform: scale(1); filter: hue-rotate(0); }
  50% { transform: scale(1.15) rotate(6deg); filter: hue-rotate(180deg); background: #fff3b0; }
  100% { transform: scale(1); filter: hue-rotate(360deg); }
}

/* Controls */
.controls {
  width: var(--board-size);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.pad {
  display: grid;
  grid-template-columns: repeat(var(--n, 9), 1fr);
  gap: 6px;
}

.pad-btn {
  --dc: var(--ink);
  position: relative;
  width: 100%;
  height: min(76px, calc(var(--col) * 1.15));
  min-width: 0;
  padding: 0;
  border: none;
  border-radius: 16px;
  background: var(--dc);
  color: #fff;
  font-size: clamp(1.3rem, calc(40vw / var(--n, 9)), 2.2rem);
  font-weight: 700;
  box-shadow: 0 5px 0 color-mix(in srgb, var(--dc) 70%, black);
  transition: transform 0.1s, box-shadow 0.1s, filter 0.2s;
}

.pad-btn:active:not(:disabled) {
  transform: translateY(4px);
  box-shadow: 0 1px 0 color-mix(in srgb, var(--dc) 70%, black);
}

.pad-btn .left {
  position: absolute;
  top: -7px;
  right: -5px;
  min-width: 22px;
  height: 22px;
  padding: 0 5px;
  border-radius: 11px;
  background: var(--cell);
  color: var(--dc);
  font-size: 0.8rem;
  line-height: 22px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.pad-btn.done {
  background: linear-gradient(160deg, #ffe27a, var(--gold));
  color: color-mix(in srgb, var(--dc) 85%, black);
  box-shadow: 0 5px 0 #d99a00;
  cursor: default;
}

.pad-btn.done .left {
  background: var(--good);
  color: #fff;
}

.pad-btn.done::after {
  content: '⭐';
  position: absolute;
  bottom: -4px;
  left: -4px;
  font-size: 0.9rem;
  animation: twinkle 1.6s ease-in-out infinite;
}

.pad-btn.just-done {
  animation: celebrate 0.8s ease;
}

@keyframes celebrate {
  0% { transform: scale(1) rotate(0); }
  30% { transform: scale(1.3) rotate(-12deg); }
  60% { transform: scale(0.95) rotate(8deg); }
  100% { transform: scale(1) rotate(0); }
}

@keyframes twinkle {
  0%, 100% { transform: scale(1) rotate(0); }
  50% { transform: scale(1.3) rotate(20deg); }
}

.actions {
  /* Five equal columns with the same spacing as the number rows above, so
     the buttons line up with them (exactly so in Tectonic's five columns) */
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}

/* Stamp (Stämpla): the toggle and the chosen number */
.tool.toggle.on {
  background: var(--grad);
  box-shadow: 0 0 0 3px var(--sel-ring), var(--shadow);
}

.tool.toggle.on .tool-label {
  color: #fff;
  font-weight: 600;
}

.pad-btn.stamped,
.note-btn.stamped,
.tool.stamped {
  transform: translateY(-4px);
  box-shadow: 0 0 0 3px var(--sel-ring), 0 6px 14px color-mix(in srgb, var(--sel-ring) 55%, transparent);
}

.board.stamping {
  box-shadow: 0 0 0 3px var(--sel-ring), var(--shadow);
}

/* Second row of buttons for candidate notes: small, outlined and italic */
.pads {
  display: flex;
  flex-direction: column;
  gap: 8px;
  container-type: inline-size;
  /* width of one button column (n columns, 6px gaps) */
  --col: calc((100cqi - (var(--cols-n, 9) - 1) * 6px) / var(--cols-n, 9));
}

.note-btn {
  --dc: var(--ink);
  position: relative;
  width: 100%;
  height: min(52px, calc(var(--col) * 0.8));
  min-width: 0;
  padding: 0;
  border: none;
  border-radius: 12px;
  background: var(--cell);
  color: var(--dc);
  font-size: clamp(0.95rem, calc(28vw / var(--n, 9)), 1.5rem);
  font-style: italic;
  font-weight: 600;
  box-shadow: inset 0 0 0 2px var(--dc), 0 3px 0 color-mix(in srgb, var(--dc) 45%, var(--cell));
  transition: transform 0.1s, opacity 0.2s;
}

.note-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.08em;
}

/* A small pencil just before the number, inside the button: "✏️3" */
.note-btn::before {
  content: '✏️';
  font-size: 0.5em;
  font-style: normal;
  line-height: 1;
  transform: translateY(-0.25em);
}

.note-btn:active:not(.done) {
  transform: translateY(2px);
}

.note-btn.done {
  opacity: 0.3;
  cursor: default;
}

.note-btn.wiggle {
  animation: shake 0.4s ease;
}

.tool {
  flex: 1 1 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 60px;
  padding: 6px 4px;
  border: none;
  border-radius: 18px;
  background: var(--card);
  box-shadow: var(--shadow);
  transition: transform 0.1s;
}

.tool:active,
.check-btn:active {
  transform: translateY(4px);
}

.tool-icon {
  font-size: 1.45rem;
  line-height: 1;
}

.tool-label {
  font-size: 0.78rem;
  color: var(--muted);
  font-weight: 500;
}

.check-btn {
  min-width: 0;
  min-height: 60px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 6px 4px;
  border: none;
  border-radius: 18px;
  background: var(--check-bg);
  color: #fff;
  box-shadow: var(--check-shadow);
  transition: transform 0.1s;
  position: relative;
  overflow: hidden;
}

/* Kolla is resting: greyed out, with a bar that shrinks as the wait runs out. */
.check-btn.cooling {
  filter: grayscale(0.75);
  opacity: 0.75;
}

.check-btn.cooling::after {
  content: '';
  position: absolute;
  left: 0;
  bottom: 0;
  width: var(--cool, 0%);
  height: 4px;
  background: rgba(255, 255, 255, 0.85);
  transition: width 0.25s linear;
}

.check-btn.wiggle {
  animation: shake 0.4s ease;
}

.check-label {
  font-size: 0.95rem;
  font-variant-numeric: tabular-nums;
  font-weight: 700;
}

.check-icon {
  display: inline-grid;
  place-items: center;
  width: 1.6rem;
  height: 1.6rem;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.28);
  font-size: 1rem;
  font-weight: 700;
}

/* Landscape: board on the left, controls on the right */
@media (min-aspect-ratio: 6/5) {
  .game {
    --controls: min(440px, 40vw);
    --board-size: min(calc(100dvh - 96px), calc(100vw - var(--controls) - 60px), 700px);
    padding-top: max(12px, env(safe-area-inset-top));
  }

  .topbar {
    width: calc(var(--board-size) + var(--controls) + 28px);
    max-width: calc(100vw - 24px);
    padding-bottom: 8px;
  }

  .play-area {
    flex-direction: row;
    gap: 28px;
  }

  .controls {
    width: var(--controls);
    gap: 16px;
  }

  /* Answer grid and notes grid side by side */
  .pads {
    flex-direction: row;
    gap: 14px;
  }

  .pad {
    flex: 1;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
  }

  .pad-btn,
  .note-btn {
    aspect-ratio: auto;
    height: min(80px, calc((100dvh - 250px) / var(--pad-rows, 3)));
    max-height: none;
  }

  .pad-btn {
    font-size: clamp(1.4rem, 5dvh, 2.3rem);
  }

  .note-btn {
    font-size: clamp(1rem, 3.6dvh, 1.6rem);
  }

  /* Landscape: four tools in a row, a wide Kolla below them */
  .actions {
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
  }

  .check-btn {
    grid-column: 1 / -1;
    flex-direction: row;
    gap: 8px;
  }

  .check-label {
    font-size: 1.35rem;
  }
}

/* Tall phones in portrait: use spare height for bigger buttons */
@media (max-aspect-ratio: 6/5) and (min-height: 760px) {
  .pad-btn {
    height: min(84px, calc(var(--col) * 1.4));
  }

  .note-btn {
    height: min(60px, calc(var(--col) * 1.05));
  }

  .tool,
  .check-btn {
    min-height: 66px;
  }

  .controls {
    gap: 14px;
  }
}

/* Very small phones */
@media (max-width: 360px) {
  .tool-label {
    display: none;
  }

  .game {
    --chrome: 275px;
  }
}

/* Short screens (phones in landscape): compact start screen */
@media (max-height: 520px) {
  .home {
    gap: 8px;
    padding-top: 12px;
  }

  .player-btn {
    flex-direction: row;
    justify-content: center;
    gap: 6px;
    padding: 6px 4px;
  }

  .player-icon {
    font-size: 1.4rem;
  }

  .logo {
    font-size: 2.4rem;
  }

  .tagline {
    display: none;
  }

  .step {
    margin-top: 4px;
    font-size: 1.1rem;
  }

  .mode-card {
    flex-direction: row;
    gap: 12px;
    padding: 8px 12px;
    max-width: 260px;
  }

  .preview {
    width: 64px;
    flex: none;
  }

  .mode-name {
    font-size: 1.2rem;
  }

  .mode-sub {
    display: none;
  }

  .levels {
    max-width: 640px;
  }

  .level {
    flex-direction: row;
    justify-content: center;
    gap: 8px;
    max-width: 200px;
    padding: 8px 12px;
  }

  .level-icon {
    font-size: 1.6rem;
  }
}

/* ---------- Overlay / win ---------- */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  overflow-y: auto;
  padding: 16px;
  background: rgba(43, 45, 66, 0.45);
  backdrop-filter: blur(4px);
  animation: fade-in 0.3s ease;
}

.win-card {
  margin: auto; /* centred, but scrollable when taller than the screen */
  width: min(92vw, 400px);
  padding: 28px 22px 24px;
  border-radius: 32px;
  background: var(--card);
  text-align: center;
  box-shadow: var(--shadow);
  animation: bounce-in 0.6s cubic-bezier(0.3, 1.6, 0.5, 1);
}

.win-trophy {
  font-size: 4.5rem;
  line-height: 1;
  animation: hop 1.6s ease-in-out infinite;
}

.win-card h2 {
  margin: 8px 0 4px;
  font-size: clamp(1.7rem, 8vw, 2.3rem);
  white-space: nowrap;
  background: var(--title-grad);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.win-stars {
  font-size: 2.6rem;
  letter-spacing: 6px;
}

.win-stars span {
  display: inline-block;
  animation: star-in 0.5s cubic-bezier(0.3, 1.6, 0.5, 1) both;
}

.win-stars span.off {
  filter: grayscale(1);
  opacity: 0.3;
}

/* New personal record: gold card with bouncing rainbow banner */
.win-card.is-record {
  position: relative;
  background: var(--record-bg);
  box-shadow: 0 0 0 5px var(--gold), 0 0 40px 8px rgba(255, 201, 60, 0.75), var(--shadow);
  animation: bounce-in 0.6s cubic-bezier(0.3, 1.6, 0.5, 1), glow 1.6s ease-in-out 0.6s infinite;
}

.win-card.is-record .win-trophy {
  animation: trophy-spin 1.2s cubic-bezier(0.3, 1.4, 0.5, 1) both, hop 1.6s ease-in-out 1.2s infinite;
}

.record-banner {
  margin-top: 6px;
  font-size: clamp(2rem, 10vw, 2.8rem);
  font-weight: 700;
  line-height: 1;
  white-space: nowrap;
}

.record-banner span {
  display: inline-block;
  text-shadow: 0 3px 0 rgba(43, 45, 66, 0.18);
  animation: banner-letter 0.5s cubic-bezier(0.3, 1.6, 0.5, 1) both, hop 1.8s ease-in-out 1s infinite;
}

.record-banner .space {
  width: 0.3em;
}

.win-card.is-record h2 {
  font-size: 1.6rem;
}

.record-detail {
  margin: 6px 0;
  font-size: 1.15rem;
  line-height: 1.4;
}

.record-detail s {
  color: var(--muted);
}

.record-detail b {
  color: var(--good);
  font-size: 1.3em;
}

@keyframes glow {
  0%, 100% { box-shadow: 0 0 0 5px var(--gold), 0 0 30px 6px rgba(255, 201, 60, 0.6), var(--shadow); }
  50% { box-shadow: 0 0 0 5px var(--gold), 0 0 55px 14px rgba(255, 201, 60, 0.95), var(--shadow); }
}

@keyframes trophy-spin {
  0% { transform: scale(0) rotate(-540deg); }
  100% { transform: scale(1.25) rotate(0); }
}

@keyframes banner-letter {
  0% { transform: translateY(-60px) scale(0.2); opacity: 0; }
  100% { transform: translateY(0) scale(1); opacity: 1; }
}

.win-record {
  margin: 4px 0 0;
  font-size: 1.25rem;
  font-weight: 700;
  color: #d98a00;
  animation: pop 0.6s cubic-bezier(0.3, 1.6, 0.5, 1) 0.8s both;
}

.win-info {
  color: var(--muted);
  font-size: 1.05rem;
}

.win-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.big-btn {
  min-height: 56px;
  border: none;
  border-radius: 999px;
  background: var(--grad);
  color: #fff;
  font-size: 1.25rem;
  font-weight: 600;
  box-shadow: var(--shadow);
}

.big-btn.secondary {
  background: var(--soft);
  color: var(--ink);
}

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

@keyframes bounce-in {
  0% { transform: scale(0.3); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

@keyframes star-in {
  0% { transform: scale(0) rotate(-180deg); }
  100% { transform: scale(1) rotate(0); }
}

/* ---------- Toast, sparkles, confetti ---------- */

.toast {
  position: fixed;
  left: 50%;
  top: max(10px, env(safe-area-inset-top));
  z-index: 30;
  width: max-content;
  max-width: calc(100vw - 32px);
  padding: 10px 20px;
  border-radius: 999px;
  background: var(--toast-bg);
  color: #fff;
  font-size: 1.1rem;
  font-weight: 500;
  text-align: center;
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translate(-50%, -20px) scale(0.9);
  transition: opacity 0.25s, transform 0.3s cubic-bezier(0.3, 1.6, 0.5, 1);
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0) scale(1);
}

.toast.good { background: var(--good); }
.toast.bad { background: var(--bad); }
.toast.gold { background: #e0a100; }

.fx {
  position: fixed;
  inset: 0;
  z-index: 25;
  pointer-events: none;
}

.spark {
  position: absolute;
  font-size: 1.3rem;
  animation: spark 0.9s ease-out forwards;
}

@keyframes spark {
  0% { transform: translate(-50%, -50%) scale(0.4); opacity: 1; }
  100% { transform: translate(calc(-50% + var(--dx)), calc(-50% + var(--dy))) scale(1.1) rotate(200deg); opacity: 0; }
}

.confetti {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 21;
  pointer-events: none;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* Short screens (phones in landscape): compact win card */
@media (max-height: 520px) {
  .win-card {
    width: min(92vw, 560px);
    padding: 16px 18px;
  }

  .win-trophy {
    font-size: 2.6rem;
  }

  .win-card h2 {
    margin: 2px 0;
    font-size: 1.6rem;
  }

  .record-banner {
    font-size: 2rem;
  }

  .win-stars {
    font-size: 1.8rem;
  }

  .win-info,
  .record-detail {
    margin: 4px 0;
  }

  .win-actions {
    flex-direction: row;
  }

  .win-actions .big-btn {
    flex: 1;
  }
}
