:root {
  --bg: #f3f4f1;
  --surface: #ffffff;
  --ink: #1f2a33;
  --muted: #66727c;
  --line: #dde1dc;
  --slot: #e4e8e3;
  --tile: #ffffff;
  --tile-ink: #1f2a33;
  --tile-edge: #cfd6d0;
  --correct: #2a8c82;
  --correct-edge: #1f6d65;
  --correct-ink: #fff;
  --clue: #e0823d;
  --clue-ink: #20140a;
  --accent: #e0823d;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141a1f;
    --surface: #1c242b;
    --ink: #e9eef0;
    --muted: #94a1ab;
    --line: #2c3740;
    --slot: #212b33;
    --tile: #2b363f;
    --tile-ink: #eef3f5;
    --tile-edge: #1a2228;
    --correct: #2f9a8f;
    --correct-edge: #1f6d65;
    --correct-ink: #fff;
    --clue: #ef9350;
    --clue-ink: #1b1206;
    --accent: #ef9350;
    color-scheme: dark;
  }
}

/* ---------- Colour schemes ----------
   Set on <html> for the game, and on each option in the settings dialog so its
   preview shows that scheme. Pairs were checked with simulated protanopia,
   deuteranopia, tritanopia and achromatopsia. */

[data-palette="default"] {
  --correct: #2a8c82; --correct-edge: #1f6d65; --correct-ink: #fff;
  --clue: #e0823d; --clue-ink: #20140a; --accent: #e0823d;
}
[data-palette="red-green"] {
  --correct: #1b4f7a; --correct-edge: #12385a; --correct-ink: #fff;
  --clue: #e0823d; --clue-ink: #20140a; --accent: #e0823d;
}
[data-palette="blue-yellow"] {
  --correct: #3d3a8c; --correct-edge: #2c2a66; --correct-ink: #fff;
  --clue: #e8455a; --clue-ink: #1b1206; --accent: #e8455a;
}
[data-palette="high-contrast"] {
  --correct: #1f2a33; --correct-edge: #0b1014; --correct-ink: #fff;
  --clue: #c98a2e; --clue-ink: #1b1206; --accent: #c98a2e;
}

@media (prefers-color-scheme: dark) {
  [data-palette="default"] {
    --correct: #2f9a8f; --correct-edge: #1f6d65; --correct-ink: #fff;
    --clue: #ef9350; --clue-ink: #1b1206; --accent: #ef9350;
  }
  [data-palette="red-green"] {
    --correct: #1f6fb2; --correct-edge: #17568a; --correct-ink: #fff;
    --clue: #ef9350; --clue-ink: #1b1206; --accent: #ef9350;
  }
  [data-palette="blue-yellow"] {
    --correct: #26a69a; --correct-edge: #1b7a71; --correct-ink: #10201e;
    --clue: #e8455a; --clue-ink: #1b1206; --accent: #e8455a;
  }
  [data-palette="high-contrast"] {
    --correct: #ffffff; --correct-edge: #cfd8dc; --correct-ink: #1f2a33;
    --clue: #c98a2e; --clue-ink: #1b1206; --accent: #c98a2e;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

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

body {
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  -webkit-tap-highlight-color: transparent;
}

.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;
}

/* ---------- Header ---------- */

.topbar {
  width: 100%;
  max-width: 560px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}

.topbar h1 {
  margin: 0;
  text-align: center;
  font-size: 1.35rem;
  font-weight: 800;
  letter-spacing: .01em;
}

.topbar-side { display: flex; gap: 6px; }
.topbar-side.end { justify-content: flex-end; }

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: 1.05rem;
  font-weight: 800;
  cursor: pointer;
  display: grid;
  place-items: center;
}

main {
  width: 100%;
  max-width: 560px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  padding: 18px 12px 32px;
}

/* ---------- Board ---------- */

.board {
  position: relative;
  width: max(260px, min(100%, 480px, calc(100svh - 240px)));
  aspect-ratio: 1;
  container-type: inline-size;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.cell {
  position: absolute;
  left: 0;
  top: 0;
  width: calc(100% / 7);
  height: calc(100% / 7);
  transform: translate(calc(var(--x) * 100% + var(--dx, 0px)), calc(var(--y) * 100% + var(--dy, 0px)));
}

.slot::before {
  content: "";
  position: absolute;
  inset: 5%;
  border-radius: 12%;
  background: var(--slot);
}

.tile {
  display: block;
  appearance: none;
  border: 0;
  padding: 0;
  margin: 0;
  background: none;
  color: inherit;
  font: inherit;
  cursor: grab;
  z-index: 2;
  outline: none;
  transition: transform .28s cubic-bezier(.3, .7, .3, 1);
}

.face {
  position: absolute;
  inset: 5%;
  border-radius: 12%;
  display: grid;
  place-items: center;
  background: var(--tile);
  color: var(--tile-ink);
  box-shadow: inset 0 -0.6cqw 0 var(--tile-edge), 0 .3cqw .8cqw rgba(20, 30, 40, .10);
  font-weight: 800;
  font-size: 7.2cqw;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  transition: background-color .35s, color .35s, transform .15s, box-shadow .2s;
}

.tile.correct { cursor: default; }
.tile.correct .face {
  background: var(--correct);
  color: var(--correct-ink);
  box-shadow: inset 0 -0.6cqw 0 var(--correct-edge);
}

.tile.selected .face {
  outline: .7cqw solid var(--accent);
  transform: scale(1.05);
}

.tile:focus-visible .face { outline: .7cqw solid var(--accent); }

.tile.dragging {
  transition: none;
  z-index: 5;
  cursor: grabbing;
}
.tile.dragging .face {
  transform: scale(1.1);
  box-shadow: 0 2cqw 4cqw rgba(0, 0, 0, .25);
}

.tile.drop-target .face {
  outline: .7cqw dashed var(--accent);
  transform: scale(.95);
}

.board.won .tile .face {
  animation: hop .5s ease both;
  animation-delay: calc(var(--d) * 45ms);
}

@keyframes hop {
  0%, 100% { transform: none; }
  40% { transform: translateY(-14%) scale(1.06); }
}

/* ---------- Clues ---------- */

/* A clue is a tag in a gap with arrows pointing at the two tiles it adds up.
   Each arrow is drawn pointing up across the whole (square) cell, then rotated. */
.clue { z-index: 1; pointer-events: none; }

.clue .arrow { position: absolute; inset: 0; }
.arrow.R { transform: rotate(90deg); }
.arrow.D { transform: rotate(180deg); }
.arrow.L { transform: rotate(270deg); }

.arrow::before,
.arrow::after {
  content: "";
  position: absolute;
  background: var(--clue);
}
.arrow::before { left: 46.5%; width: 7%; top: 14%; bottom: 50%; }
.arrow::after {
  left: 35%;
  width: 30%;
  top: 1%;
  height: 19%;
  clip-path: polygon(50% 0, 100% 100%, 0 100%);
}

.clue .tag {
  position: absolute;
  inset: 25%;
  border-radius: 24%;
  background: var(--clue);
  color: var(--clue-ink);
  display: grid;
  place-items: center;
  font-weight: 800;
  font-size: 3.9cqw;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

/* ---------- Status & actions ---------- */

.status {
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 28px;
  color: var(--muted);
}

.swaps { margin: 0; }
.swaps strong { color: var(--ink); font-size: 1.2rem; font-variant-numeric: tabular-nums; }

.stars { display: flex; gap: 3px; }
.star {
  width: 20px;
  height: 20px;
  background: var(--line);
  clip-path: polygon(50% 0%, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
  transition: background-color .3s;
}
.star.on { background: var(--accent); }

.actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }

.btn {
  position: relative;
  overflow: hidden;
  min-width: 140px;
  height: 46px;
  padding: 0 18px;
  border-radius: 999px;
  border: 1.5px solid var(--ink);
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.btn.primary { background: var(--ink); color: var(--bg); }
.btn:disabled { opacity: .4; cursor: default; }
.btn:focus-visible, .icon-btn:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.undo-fill {
  position: absolute;
  inset: 0 auto 0 0;
  width: 0;
  background: var(--accent);
  opacity: .3;
  transition: width .1s linear;
}
.undo-label, .undo-time { position: relative; }
.undo-time { min-width: 2.6em; text-align: left; font-variant-numeric: tabular-nums; color: var(--muted); }
.undo:disabled .undo-time { display: none; }

.result {
  width: min(100%, 480px);
  text-align: center;
  padding: 16px 18px 18px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
}
.result h2 { margin: 0 0 4px; font-size: 1.3rem; }
.result p { margin: 0 0 14px; color: var(--muted); }

/* ---------- Dialogs ---------- */

dialog {
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--surface);
  color: var(--ink);
  width: min(92vw, 440px);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .25);
}
dialog::backdrop { background: rgba(10, 16, 22, .45); }

.dialog-body { padding: 20px 22px 22px; }
.dialog-body h2 { margin: 0 0 10px; }
.dialog-body ul { padding-left: 1.1em; line-height: 1.5; }
.dialog-body li + li { margin-top: 6px; }
.dialog-body form { display: flex; justify-content: center; margin-top: 16px; }
.dialog-intro { margin: 0 0 14px; color: var(--muted); }

.chip {
  display: inline-block;
  padding: 0 7px;
  border-radius: 6px;
  background: var(--correct);
  color: var(--correct-ink);
  font-weight: 700;
}
.chip.clue-chip { background: var(--clue); color: var(--clue-ink); }

kbd {
  font: inherit;
  font-size: .85em;
  padding: 1px 5px;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 5px;
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  text-align: center;
  margin-bottom: 18px;
}
.stat-grid strong { display: block; font-size: 1.7rem; font-variant-numeric: tabular-nums; }
.stat-grid span { font-size: .78rem; color: var(--muted); }

.dist h3 { margin: 0 0 8px; font-size: .95rem; }
.dist-row { display: grid; grid-template-columns: 3.2em 1fr; align-items: center; gap: 8px; margin-bottom: 5px; font-size: .85rem; }
.dist-bar {
  min-width: 1.8em;
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--line);
  text-align: right;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.dist-bar.best { background: var(--accent); color: var(--clue-ink); }

/* ---------- Settings ---------- */

.palettes {
  border: 0;
  padding: 0;
  margin: 0;
  display: grid;
  gap: 8px;
}

.palette {
  display: grid;
  grid-template-columns: auto auto 1fr;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border: 1.5px solid var(--line);
  border-radius: 12px;
  cursor: pointer;
}
.palette:has(input:checked) { border-color: var(--ink); }
.palette:has(input:focus-visible) { outline: 3px solid var(--accent); outline-offset: 2px; }
.palette input { margin: 0; accent-color: var(--ink); }

.swatches { display: flex; align-items: center; gap: 6px; }
.swatch-tile,
.swatch-clue {
  display: grid;
  place-items: center;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}
.swatch-tile {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  background: var(--correct);
  color: var(--correct-ink);
  box-shadow: inset 0 -3px 0 var(--correct-edge);
}
.swatch-clue {
  width: 24px;
  height: 24px;
  border-radius: 6px;
  background: var(--clue);
  color: var(--clue-ink);
  font-size: .8rem;
}

.palette-text { display: grid; gap: 1px; }
.palette-text span { color: var(--muted); font-size: .85rem; }
