:root {
  color-scheme: dark;
  --page: #0b1020;
  --surface: #141b30;
  --surface-2: #1b2540;
  --surface-3: #222e4d;
  --line: #354365;
  --line-bright: #63739e;
  --text: #f3f6ff;
  --muted: #aab4d0;
  --accent: #77e1c1;
  --accent-ink: #081a17;
  --warm: #ffbd69;
  --danger: #ff7d8e;
  --focus: #b9f7e5;
  --shadow: 0 22px 60px rgb(0 0 0 / 30%);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  min-width: 280px;
  min-height: 100vh;
  color: var(--text);
  background:
    radial-gradient(circle at 8% 4%, rgb(119 225 193 / 10%), transparent 26rem),
    radial-gradient(circle at 92% 24%, rgb(156 130 255 / 10%), transparent 24rem),
    var(--page);
}

button, input { font: inherit; }
button { color: inherit; }
button:not(:disabled), input:not(:disabled) { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .42; }

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

.skip-link {
  position: fixed;
  top: 8px;
  left: 8px;
  z-index: 100;
  padding: .7rem 1rem;
  color: var(--accent-ink);
  background: var(--accent);
  border-radius: .5rem;
  transform: translateY(-150%);
}
.skip-link:focus { transform: none; }

.site-header,
.game-shell,
footer {
  width: min(1180px, calc(100% - 32px));
  margin-inline: auto;
}

.site-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1.5rem;
  padding: clamp(2rem, 6vw, 4.5rem) 0 clamp(1.5rem, 4vw, 3rem);
  border-bottom: 1px solid var(--line);
}

.eyebrow,
.section-kicker,
.mini-label {
  margin: 0 0 .5rem;
  color: var(--accent);
  font-size: .72rem;
  font-weight: 800;
  letter-spacing: .16em;
}

h1, h2, h3, p { overflow-wrap: anywhere; }
h1 {
  margin: 0;
  font-size: clamp(2rem, 6vw, 4.25rem);
  line-height: .95;
  letter-spacing: -.055em;
}
h2 { margin: 0; font-size: 1.1rem; letter-spacing: -.02em; }
h3 { margin: 1.5rem 0 .5rem; }
.lead { margin: 1rem 0 0; color: var(--muted); font-size: clamp(.95rem, 2vw, 1.12rem); }

.icon-button,
.action-button,
.direction-button,
.close-button {
  border: 1px solid var(--line-bright);
  border-radius: .75rem;
  background: var(--surface-2);
  transition: border-color .16s ease, background-color .16s ease, transform .16s ease;
}
.icon-button { min-height: 44px; padding: .7rem 1rem; white-space: nowrap; }
.icon-button:hover, .action-button:hover:not(:disabled), .direction-button:hover:not(:disabled) {
  border-color: var(--accent);
  background: var(--surface-3);
}
.action-button:active:not(:disabled), .direction-button:active:not(:disabled) { transform: translateY(1px); }

.game-shell {
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) minmax(300px, .8fr);
  gap: clamp(1.25rem, 4vw, 3.5rem);
  padding-block: clamp(1.5rem, 5vw, 3.5rem);
}

.board-column,
.control-column { min-width: 0; }
.section-heading,
.card-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.state-badge,
.selected-chip,
.save-indicator {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: .28rem .65rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  background: rgb(11 16 32 / 55%);
  font-size: .72rem;
  font-weight: 700;
  white-space: nowrap;
}
.state-badge::before,
.save-indicator::before {
  width: .42rem;
  height: .42rem;
  margin-right: .4rem;
  border-radius: 50%;
  background: var(--accent);
  content: "";
}
.state-badge.is-clear { color: var(--accent); border-color: var(--accent); }

.board-wrap {
  width: min(100%, 570px);
  margin: 1.25rem auto 0;
  padding: clamp(.65rem, 2.5vw, 1.15rem);
  border: 1px solid var(--line);
  border-radius: clamp(1rem, 3vw, 1.6rem);
  background: linear-gradient(145deg, var(--surface-2), #11182b);
  box-shadow: var(--shadow);
}

.game-board {
  position: relative;
  width: 100%;
  aspect-ratio: 4 / 5;
  border: 2px solid var(--line-bright);
  border-bottom-color: transparent;
  border-radius: .8rem .8rem .25rem .25rem;
  background: #0e1528;
  touch-action: none;
  overscroll-behavior: contain;
  isolation: isolate;
}

.game-board::before,
.game-board::after {
  position: absolute;
  bottom: -2px;
  z-index: 3;
  width: 25%;
  height: 2px;
  background: var(--line-bright);
  content: "";
}
.game-board::before { left: 0; }
.game-board::after { right: 0; }

.board-grid {
  position: absolute;
  inset: 0;
  z-index: -1;
  opacity: .42;
  background-image:
    linear-gradient(to right, transparent calc(100% - 1px), var(--line) 1px),
    linear-gradient(to bottom, transparent calc(100% - 1px), var(--line) 1px);
  background-size: 25% 20%;
}

.exit-marker {
  position: absolute;
  top: 100%;
  left: 25%;
  z-index: -1;
  display: grid;
  width: 50%;
  height: clamp(34px, 8vw, 52px);
  place-items: center;
  color: var(--warm);
  border-inline: 1px dashed var(--line-bright);
  background: linear-gradient(to bottom, rgb(255 189 105 / 14%), transparent);
  font-size: .65rem;
  font-weight: 900;
  letter-spacing: .24em;
}
.exit-marker span { transform: translateY(.5rem); }

.pieces-layer { position: absolute; inset: 0; }
.piece {
  --gap: clamp(3px, .9vw, 6px);
  position: absolute;
  left: calc(var(--piece-x) * 25% + var(--gap));
  top: calc(var(--piece-y) * 20% + var(--gap));
  z-index: 2;
  display: grid;
  width: calc(var(--piece-w) * 25% - var(--gap) * 2);
  height: calc(var(--piece-h) * 20% - var(--gap) * 2);
  padding: 0;
  place-items: center;
  overflow: hidden;
  color: #f8f8ff;
  border: 1px solid rgb(255 255 255 / 22%);
  border-radius: clamp(.45rem, 2vw, .8rem);
  background: #59688f;
  box-shadow: inset 0 1px 0 rgb(255 255 255 / 14%), 0 5px 12px rgb(0 0 0 / 25%);
  transform: translate3d(0, 0, 0);
  transition: left .17s ease, top .17s ease, box-shadow .17s ease, transform .17s ease, filter .17s ease;
  user-select: none;
  -webkit-user-select: none;
  -webkit-user-drag: none;
  -webkit-tap-highlight-color: transparent;
  backface-visibility: hidden;
  contain: layout paint style;
  cursor: grab;
}
.piece-pattern { position: absolute; inset: 0; opacity: .55; pointer-events: none; }
.piece-name { position: relative; z-index: 1; font-size: clamp(.56rem, 2vw, .82rem); font-weight: 900; letter-spacing: .12em; }
.piece--target {
  color: var(--accent-ink);
  border-color: #b9f7e5;
  background: var(--accent);
}
.piece--target .piece-pattern {
  inset: 14%;
  border: 2px solid rgb(8 26 23 / 42%);
  background: repeating-linear-gradient(135deg, transparent 0 9px, rgb(8 26 23 / 15%) 9px 12px);
}
.piece--domino-v { background: #746ab0; }
.piece--domino-v .piece-pattern { background: repeating-linear-gradient(0deg, transparent 0 15%, rgb(255 255 255 / 16%) 15% 18%); }
.piece--domino-h { color: #1d1723; background: var(--warm); }
.piece--domino-h .piece-pattern { background: repeating-linear-gradient(90deg, transparent 0 13%, rgb(29 23 35 / 14%) 13% 16%); }
.piece--single { background: #465878; }
.piece--single .piece-pattern {
  inset: 24%;
  border: 2px solid rgb(255 255 255 / 22%);
  transform: rotate(45deg);
}
.piece.is-selected { z-index: 4; box-shadow: 0 0 0 3px var(--warm), 0 8px 20px rgb(0 0 0 / 42%); }
.piece.is-hint { z-index: 5; animation: hint-pulse 1s ease-in-out infinite alternate; }
.piece.is-dragging { z-index: 8; transition: none; will-change: transform; cursor: grabbing; }
.piece.is-escaping { z-index: 9; transform: translateY(62%); opacity: 0; transition: transform .48s cubic-bezier(.4, 0, .2, 1), opacity .36s ease .14s; }
.game-board.is-solved .piece:not(.piece--target) { filter: saturate(.45) brightness(.75); }

@keyframes hint-pulse {
  from { box-shadow: 0 0 0 2px var(--warm), 0 0 0 5px rgb(255 189 105 / 12%); }
  to { box-shadow: 0 0 0 4px var(--warm), 0 0 0 10px rgb(255 189 105 / 4%); }
}

@media (hover: hover) and (pointer: fine) {
  .piece:hover { filter: brightness(1.08); }
}

.selection-help { margin: clamp(3rem, 8vw, 4.5rem) 0 0; color: var(--muted); text-align: center; font-size: .85rem; }
.control-column { display: grid; align-content: start; gap: .9rem; }
.status-card,
.control-card {
  padding: clamp(1rem, 3vw, 1.35rem);
  border: 1px solid var(--line);
  border-radius: 1rem;
  background: rgb(20 27 48 / 88%);
  box-shadow: 0 12px 30px rgb(0 0 0 / 14%);
}
.stats-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 1px; margin: 1rem 0 0; overflow: hidden; border: 1px solid var(--line); border-radius: .7rem; background: var(--line); }
.stats-grid div { padding: .8rem; background: var(--surface-2); }
.stats-grid dt { color: var(--muted); font-size: .68rem; }
.stats-grid dd { margin: .18rem 0 0; font-size: 1.35rem; font-weight: 850; font-variant-numeric: tabular-nums; }
.selected-chip { color: var(--warm); }

.direction-pad {
  display: grid;
  grid-template: repeat(3, 48px) / repeat(3, 48px);
  justify-content: center;
  gap: 6px;
  margin: 1rem 0;
}
.direction-button { padding: 0; font-size: 1.25rem; font-weight: 800; }
.direction-button:not(:disabled) { color: var(--accent); border-color: var(--accent); }
.direction-button.up { grid-area: 1 / 2; }
.direction-button.left { grid-area: 2 / 1; }
.direction-button.down { grid-area: 3 / 2; }
.direction-button.right { grid-area: 2 / 3; }
.pad-center { grid-area: 2 / 2; border-radius: 50%; background: radial-gradient(circle, var(--accent) 0 3px, var(--surface-3) 4px 100%); }

.toggle-row { display: flex; align-items: center; gap: .75rem; padding: .7rem .8rem; border: 1px solid var(--line); border-radius: .7rem; background: var(--surface-2); }
.toggle-row input { width: 1.2rem; height: 1.2rem; accent-color: var(--accent); }
.toggle-row span { display: grid; gap: .12rem; }
.toggle-row strong { font-size: .82rem; }
.toggle-row small { color: var(--muted); font-size: .68rem; }

.button-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: .6rem; margin-top: .9rem; }
.action-button { min-height: 44px; padding: .65rem .8rem; font-size: .8rem; font-weight: 750; }
.action-button.accent { color: var(--accent-ink); border-color: var(--accent); background: var(--accent); }
.action-button.danger { color: #2b0a10; border-color: var(--danger); background: var(--danger); }
.solver-status { min-height: 2.8em; margin: .8rem 0; color: var(--muted); font-size: .8rem; line-height: 1.45; }
.solution-actions { display: grid; grid-template-columns: 1fr 1fr; gap: .55rem; }
#autoplay-button { grid-column: 1 / -1; }
.text-button { grid-column: 1 / -1; min-height: 34px; color: var(--muted); border: 0; background: transparent; text-decoration: underline; text-underline-offset: .2em; }
.solver-spinner { width: 20px; height: 20px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

footer { display: flex; justify-content: space-between; gap: 2rem; padding: 1.5rem 0 2.5rem; color: var(--muted); border-top: 1px solid var(--line); font-size: .75rem; line-height: 1.5; }
footer p { max-width: 52rem; margin: 0; }
footer a { color: var(--accent); white-space: nowrap; }

.modal {
  width: min(520px, calc(100% - 28px));
  padding: 0;
  color: var(--text);
  border: 1px solid var(--line-bright);
  border-radius: 1rem;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.modal::backdrop { background: rgb(4 7 15 / 76%); backdrop-filter: blur(4px); }
.modal form { padding: clamp(1.2rem, 5vw, 2rem); }
.modal-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; }
.modal ol { padding-left: 1.3rem; color: var(--muted); line-height: 1.65; }
.modal p { color: var(--muted); line-height: 1.6; }
.modal kbd { padding: .12rem .34rem; color: var(--text); border: 1px solid var(--line-bright); border-radius: .3rem; background: var(--surface-3); box-shadow: 0 1px 0 var(--line-bright); font-size: .8em; }
.close-button { width: 42px; height: 42px; font-size: 1.5rem; }
.compact-modal { max-width: 440px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: .6rem; margin-top: 1.5rem; }
.clear-modal { text-align: center; }
.clear-symbol { margin: 0 !important; color: var(--accent) !important; font-size: 3.5rem; line-height: 1 !important; }
.clear-assistance { margin: .7rem auto 0; padding: .55rem .8rem; border: 1px solid var(--line); border-radius: .65rem; background: var(--surface-2); font-size: .82rem; }
.clear-modal[data-mode="hint"] .clear-symbol,
.clear-modal[data-mode="hint"] .mini-label { color: var(--warm) !important; }
.clear-modal[data-mode="autoplay"] .clear-symbol,
.clear-modal[data-mode="autoplay"] .mini-label { color: #a99aff !important; }
.clear-modal .dialog-actions { justify-content: center; }
.noscript { margin: 1rem; padding: 1rem; color: #2b0a10; background: var(--danger); }

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

@media (max-width: 780px) {
  .game-shell { grid-template-columns: 1fr; }
  .board-wrap { width: min(100%, 520px); }
  .control-column { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .status-card, .solution-card { grid-column: 1 / -1; }
}

@media (max-width: 500px) {
  .site-header, .game-shell, footer { width: min(100% - 24px, 1180px); }
  .site-header { align-items: center; }
  .lead { max-width: 15rem; }
  .icon-button { padding-inline: .75rem; }
  .control-column { grid-template-columns: 1fr; }
  .status-card, .solution-card { grid-column: auto; }
  .board-wrap { padding: .55rem; }
  .selection-help { margin-top: 2.8rem; }
  footer { flex-direction: column; gap: .75rem; }
}

@media (prefers-color-scheme: light) {
  :root {
    color-scheme: light;
    --page: #f2f4fa;
    --surface: #ffffff;
    --surface-2: #e7ebf5;
    --surface-3: #dce2f0;
    --line: #c4ccdf;
    --line-bright: #8793b0;
    --text: #11182b;
    --muted: #59647e;
    --accent: #39b897;
    --accent-ink: #071713;
    --warm: #e69a3c;
    --focus: #087f65;
    --shadow: 0 22px 60px rgb(39 50 81 / 18%);
  }
  body { background: radial-gradient(circle at 8% 4%, rgb(57 184 151 / 12%), transparent 26rem), var(--page); }
  .game-board { background: #d9deea; }
  .status-card, .control-card { background: rgb(255 255 255 / 88%); }
  .piece--target { color: #071713; }
}

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