:root {
  color-scheme: dark;
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #273449;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #fdba74;
  --accent-hover: #fec99a;
  --accent-ink: #1c1206;
  --accent-soft: rgba(253, 186, 116, 0.14);
  --line: #334155;
  --line-strong: #64748b;
  --cell-bg: #0f172a;
  --cross: #8391a7;
  --danger: #f87171;
  --revealed: #cbd5e1;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(2, 6, 23, 0.35), 0 12px 32px rgba(2, 6, 23, 0.32);
  --ease: cubic-bezier(0.2, 0.7, 0.2, 1);
  --fast: 150ms;
  --slow: 250ms;
  --font: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --x-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10'%3E%3Cpath d='M1.6 1.6l6.8 6.8M8.4 1.6l-6.8 6.8' stroke='%23000' stroke-width='1.8' stroke-linecap='round'/%3E%3C/svg%3E");
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: radial-gradient(1000px 460px at 50% -180px, rgba(253, 186, 116, 0.1), transparent 70%) no-repeat, var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
}

button {
  font: inherit;
  color: inherit;
}

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

.icon {
  width: 18px;
  height: 18px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* ---------- Buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-width: 40px;
  min-height: 40px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color var(--fast) var(--ease), border-color var(--fast) var(--ease),
    color var(--fast) var(--ease), transform var(--fast) var(--ease), opacity var(--fast) var(--ease);
}

.btn:hover:not(:disabled) {
  background: var(--surface-2);
  border-color: var(--line-strong);
}

.btn:active:not(:disabled) {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.4;
  cursor: default;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.btn-lang {
  padding: 0 10px;
  font-weight: 700;
  letter-spacing: 0.04em;
}

/* ---------- Header and page layout ---------- */

.topbar {
  width: 100%;
  max-width: 1120px;
  margin: 0 auto;
  padding: 12px 16px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.logo {
  width: 32px;
  height: 32px;
  flex: none;
  border-radius: 8px;
  box-shadow: 0 0 0 1px rgba(253, 186, 116, 0.25);
}

.brand h1 {
  margin: 0;
  font-size: 21px;
  font-weight: 800;
  letter-spacing: -0.01em;
}

.top-actions {
  display: flex;
  gap: 8px;
}

.layout {
  flex: 1;
  width: 100%;
  max-width: 1120px;
  margin: 0 auto;
  padding: 4px 16px 16px;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  align-content: start;
  align-items: start;
  gap: 16px;
}

.card {
  background: var(--surface);
  border: 1px solid rgba(148, 163, 184, 0.12);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.play {
  min-width: 0;
  padding: 12px 10px 10px;
}

/* ---------- Status line ---------- */

.status {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 32px;
  margin: 0 2px 10px;
}

.title-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.puzzle-title {
  margin: 0;
  overflow: hidden;
  font-size: 17px;
  font-weight: 700;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.chip {
  flex: none;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 12px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.stats {
  display: flex;
  align-items: center;
  gap: 14px;
  color: var(--muted);
  font-weight: 650;
  font-variant-numeric: tabular-nums;
}

.stat {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.stat .icon {
  width: 16px;
  height: 16px;
}

.stat-mistakes {
  color: var(--danger);
}

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

.board-box {
  overflow: auto;
  overscroll-behavior: contain;
  border-radius: 8px;
}

.board {
  --cell: 32px;
  width: max-content;
  margin: 0 auto;
  font-variant-numeric: tabular-nums;
}

.board-top {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  background: var(--surface);
}

.corner {
  position: sticky;
  left: 0;
  z-index: 1;
  flex: none;
  width: var(--rcw, 0px);
  background: var(--surface);
}

.col-clues {
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell));
  align-items: end;
  padding: 0 2px;
}

.board-body {
  display: flex;
  align-items: flex-start;
}

.row-clues {
  position: sticky;
  left: 0;
  z-index: 1;
  flex: none;
  display: grid;
  grid-template-rows: repeat(var(--rows), var(--cell));
  padding: 2px 0;
  background: var(--surface);
}

.clue {
  color: var(--text);
  font-size: clamp(10px, calc(var(--cell) * 0.46), 20px);
  font-weight: 650;
  transition: opacity var(--slow) var(--ease), color var(--fast) var(--ease), background-color var(--fast) var(--ease);
}

.clue-col {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  height: 100%;
  padding: 4px 0 6px;
  border-radius: 6px 6px 0 0;
  line-height: 1.18;
}

.clue-row {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.55em;
  padding: 0 0.6em 0 0.5em;
  border-radius: 6px 0 0 6px;
  white-space: nowrap;
}

.clue.done {
  opacity: 0.3;
}

.clue.hl {
  color: var(--accent);
  background: rgba(253, 186, 116, 0.09);
}

/* Zoomed cells are large already; compact clues leave more of the grid in view on a phone. */
.board-box.zoomed .clue {
  font-size: 12px;
}

.board-box.zoomed .clue-row {
  gap: 0.3em;
  padding: 0 0.45em 0 0.3em;
}

.board-box.zoomed .clue-row.long {
  font-size: 11px;
}

/* Fitted boards whose long row clues would overflow even with the smallest cells. */
.board-box.compact .clue {
  font-size: 10px;
}

.board-box.compact .clue-row {
  gap: 0.3em;
  padding: 0 0.35em 0 0.2em;
}

.board-box.compact .clue-col {
  padding: 2px 0 3px;
  line-height: 1.1;
}

.board-box.zoomed .clue-col {
  padding: 2px 0 4px;
  line-height: 1.1;
}

.board.won .clue.hl {
  color: var(--text);
  background: transparent;
}

.cells {
  position: relative;
  z-index: 0;
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell));
  grid-template-rows: repeat(var(--rows), var(--cell));
  border: 2px solid var(--line-strong);
  border-radius: 3px;
  background: var(--cell-bg);
  cursor: pointer;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  outline: none;
  transition: border-color var(--slow) var(--ease);
}

/* The ring sits on the border: an outer ring would hide under the sticky clue lanes. */
.cells:focus-visible {
  border-color: var(--accent);
}

.cell {
  position: relative;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}

.cell.sep-r {
  border-right: 2px solid var(--line-strong);
}

.cell.sep-b {
  border-bottom: 2px solid var(--line-strong);
}

.cell.end-r {
  border-right-width: 0;
}

.cell.end-b {
  border-bottom-width: 0;
}

.cell::before,
.cell::after {
  content: '';
  position: absolute;
  opacity: 0;
  transform: scale(0.55);
  pointer-events: none;
}

.cell::before {
  inset: 2px;
  border-radius: 3px;
  background: var(--accent);
  transition: opacity var(--fast) var(--ease), transform var(--fast) var(--ease),
    background-color var(--slow) var(--ease), inset var(--slow) var(--ease), border-radius var(--slow) var(--ease);
}

.cell::after {
  inset: 24%;
  background: var(--cross);
  -webkit-mask: var(--x-mask) center / contain no-repeat;
  mask: var(--x-mask) center / contain no-repeat;
  transition: opacity var(--fast) var(--ease), transform var(--fast) var(--ease);
}

.cell.filled::before,
.cell.crossed::after {
  opacity: 1;
  transform: none;
}

.cell.wrong {
  background: rgba(248, 113, 113, 0.22);
}

.cell.wrong::before,
.cell.wrong::after {
  background: var(--danger);
}

.cell.mistake {
  animation: flash 700ms ease-out;
}

.band {
  position: absolute;
  pointer-events: none;
  background: rgba(253, 186, 116, 0.08);
  opacity: 0;
  transition: opacity var(--fast) var(--ease);
}

.band-row {
  left: 0;
  right: 0;
  top: calc(var(--cell) * var(--hr, 0));
  height: var(--cell);
}

.band-col {
  top: 0;
  bottom: 0;
  left: calc(var(--cell) * var(--hc, 0));
  width: var(--cell);
}

.cells.hover .band {
  opacity: 1;
}

.cursor {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  width: var(--cell);
  height: var(--cell);
  border: 2px solid var(--accent);
  border-radius: 5px;
  box-shadow: 0 0 0 3px rgba(253, 186, 116, 0.25);
  opacity: 0;
  pointer-events: none;
  transform: translate(calc(var(--cell) * var(--kc, 0)), calc(var(--cell) * var(--kr, 0)));
  transition: transform var(--fast) var(--ease), opacity var(--fast) var(--ease);
}

.cells.kbd .cursor {
  opacity: 1;
}

.board.revealed .cell::before {
  background: var(--revealed);
}

.board.won .cells {
  border-color: transparent;
  cursor: default;
}

.board.won .cell {
  border-width: 0;
}

.board.won .cell::before {
  inset: 0;
  border-radius: 0;
  background: var(--paint, var(--accent));
  transition-delay: var(--delay, 0ms);
}

/* The colours sweep in diagonally and each painted cell pulses once; it stays inside its cell. */
.board.won.animate .cell::before {
  animation: ripple 480ms var(--ease) var(--delay, 0ms) both;
}

.board.won .cell::after {
  opacity: 0;
}

.board.won .band,
.board.won .cursor {
  display: none;
}

.hint {
  margin: 10px 4px 0;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}

/* ---------- Side panel ---------- */

.panel {
  display: grid;
  gap: 14px;
  padding: 12px;
}

.tools {
  display: grid;
  gap: 10px;
}

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg);
}

.seg {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color var(--fast) var(--ease), color var(--fast) var(--ease), box-shadow var(--fast) var(--ease);
}

.seg:hover {
  color: var(--text);
}

.seg[aria-pressed='true'] {
  background: var(--surface-2);
  color: var(--text);
  box-shadow: inset 0 0 0 1px var(--line-strong), 0 1px 2px rgba(2, 6, 23, 0.4);
}

.tool-icon {
  width: 16px;
  height: 16px;
  flex: none;
}

.tool-fill {
  border-radius: 4px;
  background: var(--accent);
}

.tool-cross {
  background: var(--cross);
  -webkit-mask: var(--x-mask) center / contain no-repeat;
  mask: var(--x-mask) center / contain no-repeat;
}

.seg[aria-pressed='true'] .tool-cross {
  background: var(--text);
}

.actions {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.act {
  flex-direction: column;
  gap: 4px;
  min-height: 56px;
  padding: 6px 4px;
  font-size: 12px;
}

.options {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}

.switch {
  position: relative;
  flex: 1 1 200px;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 44px;
  cursor: pointer;
}

.switch input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
}

.switch-track {
  position: relative;
  flex: none;
  width: 42px;
  height: 24px;
  border-radius: 999px;
  background: var(--line);
  transition: background-color var(--fast) var(--ease);
}

.switch-track::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--text);
  transition: transform var(--fast) var(--ease), background-color var(--fast) var(--ease);
}

.switch input:checked + .switch-track {
  background: var(--accent);
}

.switch input:checked + .switch-track::after {
  transform: translateX(18px);
  background: var(--accent-ink);
}

.switch input:focus-visible + .switch-track {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.switch-text {
  display: grid;
  font-size: 14px;
  font-weight: 650;
  line-height: 1.25;
}

.switch-text small {
  color: var(--muted);
  font-size: 12px;
  font-weight: 500;
}

.zoom-plus {
  transition: opacity var(--fast) var(--ease);
}

#zoom[aria-pressed='true'] .zoom-plus {
  opacity: 0;
}

.result {
  display: grid;
  gap: 4px;
  padding: 4px 2px;
  animation: rise 400ms var(--ease) both;
}

.result p {
  margin: 0;
}

.result-title {
  color: var(--accent);
  font-size: 24px;
  font-weight: 800;
}

.result-name {
  font-size: 16px;
  font-weight: 700;
}

.result-time {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.result-badge {
  justify-self: start;
  margin-top: 4px !important;
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 13px;
  font-weight: 700;
}

.result-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 10px;
}

.keys {
  display: none;
  padding-top: 12px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 13px;
}

.keys h3 {
  margin: 0 0 8px;
  color: var(--text);
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.keys dl {
  display: grid;
  gap: 7px;
  margin: 0;
}

.keys dl div {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.keys dt,
.keys dd {
  margin: 0;
}

.keys dd {
  text-align: right;
}

kbd {
  display: inline-block;
  padding: 1px 6px;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  font-size: 12px;
  font-weight: 700;
}

/* ---------- Footer ---------- */

.footer {
  padding: 4px 16px 14px;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}

.footer a {
  display: inline-block;
  padding: 11px 4px;
  color: var(--accent);
  text-decoration: none;
}

.footer a:hover {
  text-decoration: underline;
}

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

.dialog {
  width: min(560px, calc(100vw - 24px));
  max-height: calc(100vh - 32px);
  max-height: calc(100dvh - 32px);
  padding: 0;
  border: 1px solid rgba(148, 163, 184, 0.18);
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 24px 64px rgba(2, 6, 23, 0.6);
}

.dialog[open] {
  animation: rise 220ms var(--ease);
}

.dialog::backdrop {
  background: rgba(2, 6, 23, 0.68);
  backdrop-filter: blur(3px);
}

.dialog-inner {
  padding: 16px;
}

.picker-inner {
  display: grid;
  gap: 18px;
}

.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.dialog-head h2 {
  margin: 0;
  font-size: 19px;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: transparent;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  transition: background-color var(--fast) var(--ease);
}

.icon-btn:hover {
  background: var(--surface-2);
}

.picker-section h3 {
  margin: 0;
  font-size: 15px;
}

.section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.muted {
  margin: 4px 0 10px;
  color: var(--muted);
  font-size: 13px;
}

.section-head .muted {
  margin: 0;
}

.sizes {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.size-btn,
.tile {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
  transition: border-color var(--fast) var(--ease), transform var(--fast) var(--ease), background-color var(--fast) var(--ease);
}

.size-btn:hover,
.tile:hover {
  border-color: var(--line-strong);
  transform: translateY(-1px);
}

.size-btn {
  display: grid;
  justify-items: center;
  align-content: center;
  gap: 2px;
  min-height: 64px;
  padding: 10px 6px;
}

.size-num {
  font-size: 17px;
  font-weight: 800;
}

.size-level {
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

.size-best {
  color: var(--accent);
  font-size: 11px;
  font-weight: 600;
}

.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 8px;
}

.tile {
  display: grid;
  justify-items: center;
  align-content: start;
  gap: 4px;
  min-width: 0;
  padding: 10px 6px 8px;
}

.tile.current {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.thumb {
  width: 56px;
  height: 56px;
  border-radius: 6px;
  background: var(--surface);
  image-rendering: pixelated;
}

.tile-name {
  max-width: 100%;
  overflow: hidden;
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.tile-meta {
  color: var(--muted);
  font-size: 11px;
  text-align: center;
}

.tile.solved .tile-meta {
  color: var(--accent);
}

.confirm {
  width: min(400px, calc(100vw - 32px));
}

.confirm p {
  margin: 4px 0 16px;
  font-size: 15px;
}

.confirm-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  z-index: 50;
  max-width: calc(100vw - 32px);
  padding: 10px 18px;
  border-radius: 999px;
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  font-weight: 700;
  text-align: center;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 16px);
  transition: opacity var(--slow) var(--ease), transform var(--slow) var(--ease);
}

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

/* ---------- Animations ---------- */

@keyframes flash {
  from {
    background-color: rgba(248, 113, 113, 0.6);
  }
  to {
    background-color: transparent;
  }
}

@keyframes ripple {
  0%,
  100% {
    transform: none;
  }
  45% {
    transform: scale(0.72);
  }
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

/* ---------- Responsive ---------- */

@media (max-width: 479px) {
  .topbar {
    padding: 10px 16px;
  }

  .brand {
    gap: 8px;
  }

  .logo {
    width: 28px;
    height: 28px;
  }

  .brand h1 {
    font-size: 18px;
  }

  .top-actions {
    gap: 6px;
  }

  .top-actions .btn {
    padding: 0 10px;
    font-size: 13px;
  }

  .top-actions .icon {
    display: none;
  }
}

@media (min-width: 900px) {
  .layout {
    grid-template-columns: minmax(0, 1fr) 288px;
    gap: 24px;
  }

  .play {
    padding: 16px 20px 12px;
  }

  .panel {
    padding: 16px;
  }

  .actions {
    grid-template-columns: 1fr 1fr;
  }

  .act {
    flex-direction: row;
    gap: 8px;
    min-height: 42px;
    font-size: 14px;
  }

  .options {
    display: grid;
  }

  .switch {
    flex: none;
  }
}

@media (min-width: 900px) and (pointer: fine) {
  .keys {
    display: block;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation: none !important;
    transition: none !important;
    scroll-behavior: auto !important;
  }
}
