/* ── Light themes ────────────────────────────── */
:root {
  --bg: #f4f2ea;
  --ink: #1d2a2e;
  --muted: #506467;
  --line: #d1d0c7;
  --wall: #263235;
  --cell: #fffdf6;
  --cell-alt: #f9f7ef;
  --path: #1a6aa4;
  --path-soft: #c8e8fb;
  --path-done: #1a6644;
  --bad: #a83232;
  --complete-bg: #d4ede0;
  --bad-bg: #f4d8d8;
  --clue-text: #fffdf6;
  --step-color: rgba(29, 42, 46, 0.40);
  --button: #263235;
  --button-text: #fffdf6;
}

:root[data-theme="forest"] {
  --bg: #eff4ec;
  --ink: #1d3025;
  --muted: #3f5743;
  --line: #c5d0c0;
  --wall: #24382b;
  --cell: #fbfff8;
  --cell-alt: #eef7e8;
  --path: #1a7050;
  --path-soft: #d0ebd9;
  --path-done: #7a4218;
  --bad: #a83040;
  --complete-bg: #d6ede0;
  --bad-bg: #f2d6d8;
  --clue-text: #fbfff8;
  --step-color: rgba(29, 48, 37, 0.40);
  --button: #24382b;
  --button-text: #fbfff8;
}

:root[data-theme="candy"] {
  --bg: #fff2f4;
  --ink: #2f2331;
  --muted: #6a5360;
  --line: #decbd6;
  --wall: #2f2331;
  --cell: #fffafd;
  --cell-alt: #f7eef9;
  --path: #b73172;
  --path-soft: #ffd8e9;
  --path-done: #1f6d61;
  --bad: #b03a2b;
  --complete-bg: #d6efeb;
  --bad-bg: #f5d8d4;
  --clue-text: #fffafd;
  --step-color: rgba(47, 35, 49, 0.40);
  --button: #2f2331;
  --button-text: #fffafd;
}

:root[data-theme="slate"] {
  --bg: #f0f3f7;
  --ink: #1c2b3a;
  --muted: #4a6073;
  --line: #c4cdd8;
  --wall: #2a3f52;
  --cell: #f8fafc;
  --cell-alt: #ecf0f5;
  --path: #1d4ed8;
  --path-soft: #dbeafe;
  --path-done: #065f46;
  --bad: #b91c1c;
  --complete-bg: #d1fae5;
  --bad-bg: #fee2e2;
  --clue-text: #f8fafc;
  --step-color: rgba(28, 43, 58, 0.40);
  --button: #2a3f52;
  --button-text: #f8fafc;
}

:root[data-theme="parchment"] {
  --bg: #fdf6e3;
  --ink: #3b2718;
  --muted: #7a5c42;
  --line: #e2cdb5;
  --wall: #4a3220;
  --cell: #fffbf0;
  --cell-alt: #f7f0df;
  --path: #92400e;
  --path-soft: #fef3c7;
  --path-done: #166534;
  --bad: #991b1b;
  --complete-bg: #dcfce7;
  --bad-bg: #fee2e2;
  --clue-text: #fffbf0;
  --step-color: rgba(59, 39, 24, 0.40);
  --button: #4a3220;
  --button-text: #fffbf0;
}

/* ── Dark themes ─────────────────────────────── */
:root[data-theme="midnight"] {
  --bg: #0e1420;
  --ink: #e2eef7;
  --muted: #7a95a8;
  --line: #243040;
  --wall: #b8d0e4;
  --cell: #141e2e;
  --cell-alt: #192436;
  --path: #60a5fa;
  --path-soft: #1a2e50;
  --path-done: #34d399;
  --bad: #f87171;
  --complete-bg: #0f2c22;
  --bad-bg: #2c1218;
  --clue-text: #0e1420;
  --step-color: rgba(226, 238, 247, 0.35);
  --button: #b8d0e4;
  --button-text: #0e1420;
}

:root[data-theme="dusk"] {
  --bg: #180e24;
  --ink: #f0e4d0;
  --muted: #9a7c68;
  --line: #342040;
  --wall: #d8c4a0;
  --cell: #20122e;
  --cell-alt: #26183a;
  --path: #e8a030;
  --path-soft: #38200c;
  --path-done: #5cc88a;
  --bad: #e06060;
  --complete-bg: #183020;
  --bad-bg: #301418;
  --clue-text: #180e24;
  --step-color: rgba(240, 228, 208, 0.35);
  --button: #d8c4a0;
  --button-text: #180e24;
}

:root[data-theme="ocean"] {
  --bg: #081520;
  --ink: #c8e8f4;
  --muted: #5a8898;
  --line: #142838;
  --wall: #90c8dc;
  --cell: #0c1e2e;
  --cell-alt: #102434;
  --path: #22c8e0;
  --path-soft: #0a2838;
  --path-done: #38d890;
  --bad: #e04860;
  --complete-bg: #0a2820;
  --bad-bg: #280c18;
  --clue-text: #081520;
  --step-color: rgba(200, 232, 244, 0.35);
  --button: #90c8dc;
  --button-text: #081520;
}

:root[data-theme="carbon"] {
  --bg: #161616;
  --ink: #e8e0d8;
  --muted: #888078;
  --line: #2e2e2e;
  --wall: #c8c0b8;
  --cell: #1e1e1e;
  --cell-alt: #252525;
  --path: #d08840;
  --path-soft: #2e1c08;
  --path-done: #60c080;
  --bad: #e05050;
  --complete-bg: #162818;
  --bad-bg: #281616;
  --clue-text: #161616;
  --step-color: rgba(232, 224, 216, 0.35);
  --button: #c8c0b8;
  --button-text: #161616;
}

:root[data-theme="arcade"] {
  --bg: #0f0f0f;
  --ink: #f8f840;
  --muted: #60d8ff;
  --line: #3a3a3a;
  --wall: #f8f840;
  --cell: #1a1a1a;
  --cell-alt: #202020;
  --path: #00d880;
  --path-soft: #0a2015;
  --path-done: #ff7020;
  --bad: #ff3c60;
  --complete-bg: #102010;
  --bad-bg: #2a0c14;
  --clue-text: #0f0f0f;
  --step-color: rgba(248, 248, 64, 0.40);
  --button: #f8f840;
  --button-text: #0f0f0f;
}

* {
  box-sizing: border-box;
}

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

body {
  padding: clamp(12px, 3vw, 28px);
}

.app {
  width: 100%;
  max-width: 720px;
  min-width: 0;
  margin: 0 auto;
  display: grid;
  gap: 18px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.controls {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 14px;
}

.topbar-controls {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  flex-wrap: nowrap;
  flex-shrink: 0;
  justify-content: flex-end;
}

h1 {
  margin: 0 0 4px;
  font-size: clamp(1.6rem, 4.5vw, 2.6rem);
  line-height: 1.1;
  letter-spacing: -0.02em;
  white-space: nowrap;
}

.select-row {
  display: flex;
  gap: 6px;
  align-items: stretch;
}

#rerollBtn,
#modeBtn {
  flex-shrink: 0;
  min-width: 40px;
  padding: 0 8px;
  font-size: 1.1rem;
  line-height: 1;
}

p {
  margin: 0;
  color: var(--muted);
  font-size: 0.98rem;
  line-height: 1.4;
}

.select-wrap {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

select,
button {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font: inherit;
}

select {
  min-width: 0;
  padding: 0 34px 0 12px;
  background: var(--cell);
  color: var(--ink);
}

button {
  min-width: 0;
  padding: 0 15px;
  background: var(--button);
  color: var(--button-text);
  cursor: pointer;
}

button:disabled {
  cursor: default;
  opacity: 0.45;
}

.board-wrap {
  width: 100%;
  min-width: 0;
  display: grid;
  place-items: center;
}

.board {
  --size: 5;
  width: 100%;
  max-width: min(68vh, 620px);
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(var(--size), 1fr);
  grid-template-rows: repeat(var(--size), 1fr);
  border: 3px solid var(--wall);
  background: var(--wall);
  gap: 1px;
  touch-action: none;
  user-select: none;
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background-color: var(--cell);
  /* wall gradients — set per-direction via data attributes below */
  --wall-n: none;
  --wall-s: none;
  --wall-e: none;
  --wall-w: none;
  background-image: var(--wall-n), var(--wall-s), var(--wall-e), var(--wall-w);
  color: var(--ink);
  cursor: pointer;
  font-weight: 800;
  font-size: clamp(1rem, calc(18vw / var(--size)), 2.25rem);
  line-height: 1;
}

.cell:nth-child(2n) {
  background-color: var(--cell-alt);
}

.cell.in-path {
  background-color: var(--path-soft);
}

.cell.complete {
  background-color: var(--complete-bg);
}

.cell.bad {
  background-color: var(--bad-bg);
}

.cell::after {
  content: attr(data-step);
  position: absolute;
  right: 5px;
  bottom: 4px;
  color: var(--step-color);
  font-size: clamp(0.55rem, calc(7vw / var(--size)), 0.82rem);
  font-weight: 700;
}

.cell:not(.in-path)::after {
  content: "";
}

.cell[data-wall-n="true"] {
  --wall-n: linear-gradient(to bottom, var(--wall) 5px, transparent 5px);
}

.cell[data-wall-s="true"] {
  --wall-s: linear-gradient(to top, var(--wall) 5px, transparent 5px);
}

.cell[data-wall-e="true"] {
  --wall-e: linear-gradient(to left, var(--wall) 5px, transparent 5px);
}

.cell[data-wall-w="true"] {
  --wall-w: linear-gradient(to right, var(--wall) 5px, transparent 5px);
}

.cell.in-path::before,
.cell.in-path::after {
  content: "";
  position: absolute;
  z-index: 1;
  pointer-events: none;
}

.cell.in-path::before {
  left: 50%;
  top: 50%;
  width: 42%;
  height: 42%;
  transform: translate(-50%, -50%);
  border-radius: 999px;
  background: var(--path);
}

.cell .line-arm {
  position: absolute;
  z-index: 1;
  display: none;
  background: var(--path);
  pointer-events: none;
}

.cell .line-arm.n,
.cell .line-arm.s {
  left: 50%;
  width: 42%;
  height: 50%;
  transform: translateX(-50%);
}

.cell .line-arm.n {
  top: 0;
}

.cell .line-arm.s {
  bottom: 0;
}

.cell .line-arm.e,
.cell .line-arm.w {
  top: 50%;
  width: 50%;
  height: 42%;
  transform: translateY(-50%);
}

.cell .line-arm.e {
  right: 0;
}

.cell .line-arm.w {
  left: 0;
}

.cell[data-line-n="true"] .line-arm.n,
.cell[data-line-e="true"] .line-arm.e,
.cell[data-line-s="true"] .line-arm.s,
.cell[data-line-w="true"] .line-arm.w {
  display: block;
}

.cell.clue {
  z-index: 2;
}

.cell.clue span {
  z-index: 4;
  width: min(64%, 3.6rem);
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  border-radius: 999px;
  background: var(--ink);
  color: var(--clue-text);
}

.cell.in-path.clue span {
  background: var(--path);
}

.cell.complete.clue span {
  background: var(--path-done);
}

.controls {
  align-items: center;
  flex-wrap: wrap;
  justify-content: start;
}

.status {
  flex: 1 1 220px;
  color: var(--muted);
  font-size: 0.95rem;
  line-height: 1.35;
}

.status.good {
  color: var(--path-done);
  font-weight: 700;
}

.status.bad {
  color: var(--bad);
  font-weight: 700;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(10, 14, 18, 0.62);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

.modal[hidden] {
  display: none;
}

.modal-panel {
  width: min(100%, 440px);
  padding: 26px 24px 22px;
  border: 2px solid var(--line);
  border-radius: 12px;
  background: var(--cell);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.36), 0 4px 12px rgba(0, 0, 0, 0.18);
}

.modal-panel h2 {
  margin: 0 0 20px;
  font-size: 2.2rem;
  line-height: 1;
  letter-spacing: -0.01em;
}

.modal-stats {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-bottom: 20px;
}

.modal-stats div {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
  padding: 11px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--cell-alt);
}

.modal-stats span {
  color: var(--muted);
  font-size: 0.65rem;
  font-weight: 800;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.modal-stats strong {
  min-width: 0;
  font-size: 1.05rem;
  line-height: 1.25;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.score-form {
  display: grid;
  gap: 7px;
  margin-bottom: 18px;
}

.score-form label,
.best-heading span {
  color: var(--muted);
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.score-input-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
}

.score-input-row input {
  min-width: 0;
  min-height: 40px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--cell);
  color: var(--ink);
  font: inherit;
}

.best-times {
  display: grid;
  gap: 10px;
  margin-bottom: 18px;
}

.best-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.best-heading strong {
  color: var(--path-done);
  font-size: 0.92rem;
}

.best-times ol {
  display: grid;
  gap: 6px;
  margin: 0;
  padding-left: 0;
  list-style-position: inside;
}

.best-times li {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 6px;
  align-items: baseline;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.25;
}

.best-times li strong {
  color: var(--ink);
}

.best-rank {
  color: var(--muted);
}

.best-name {
  min-width: 0;
  overflow: hidden;
  color: var(--ink);
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.best-score {
  justify-self: end;
  color: var(--muted);
  white-space: nowrap;
}

.best-score.perfect::after {
  content: " perfect";
  color: var(--path-done);
  font-weight: 800;
}

.modal-panel button {
  width: 100%;
}

.puzzle-nav {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 8px;
}

@media (max-width: 560px) {
  body {
    padding: 12px;
  }

  .app {
    gap: 12px;
  }

  .topbar {
    align-items: stretch;
    flex-direction: column;
  }

  .topbar-controls {
    display: grid;
    grid-template-columns: 1fr 1fr;
    justify-content: stretch;
  }

  select {
    width: 100%;
  }

  .controls {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .status {
    grid-column: 1 / -1;
  }

  .score-input-row {
    grid-template-columns: 1fr;
  }
}
