/* 15 Puzzle: dark theme, mobile first. */

:root {
  color-scheme: dark;
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-raised: #273449;
  --line: rgba(148, 163, 184, 0.16);
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-light: #7dd3fc;
  --accent-ink: #082f49;
  --accent-soft: rgba(56, 189, 248, 0.14);
  --radius: 12px;
  --shadow: 0 1px 2px rgba(2, 6, 23, 0.4), 0 18px 40px -18px rgba(2, 6, 23, 0.85);
  --speed: 180ms;
  --ease: cubic-bezier(0.2, 0.7, 0.2, 1);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
}

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

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

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background-color: var(--bg);
  background-image: radial-gradient(820px 440px at 50% -10%, rgba(56, 189, 248, 0.12), transparent 70%);
  background-repeat: no-repeat;
  color: var(--text);
  font: 400 16px/1.5 var(--font);
}

button {
  font: inherit;
  color: inherit;
}

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

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

/* ------------------------------------------------------------ page frame */

.app {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 960px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  padding: max(10px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
    max(4px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-bottom: 10px;
}

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

.brand-mark {
  flex: none;
  width: 30px;
  height: 30px;
}

.brand-frame {
  fill: var(--surface);
  stroke: var(--line);
}

.brand-accent {
  fill: var(--accent);
}

.brand-tile {
  fill: #475569;
}

.brand-title {
  margin: 0;
  overflow: hidden;
  font-size: 20px;
  font-weight: 750;
  letter-spacing: -0.01em;
  white-space: nowrap;
  text-overflow: ellipsis;
}

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

.app-footer {
  display: flex;
  justify-content: center;
  padding-top: 2px;
}

.app-footer a {
  display: inline-flex;
  align-items: center;
  min-height: 40px;
  padding: 0 12px;
  border-radius: 10px;
  color: var(--muted);
  font-size: 13px;
  text-decoration: none;
  transition: color var(--speed) ease;
}

.app-footer a:hover {
  color: var(--accent-light);
}

/* ------------------------------------------------------------ layout */

.layout {
  display: grid;
  flex: 1;
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas: "stats" "board" "controls" "help";
  align-content: start;
  gap: 10px;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
}

.stats {
  grid-area: stats;
}

.board {
  grid-area: board;
}

.controls {
  grid-area: controls;
}

.help {
  grid-area: help;
}

/* ------------------------------------------------------------ buttons */

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

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

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

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

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

.btn-ghost {
  background: transparent;
}

.btn-square {
  width: 42px;
  padding: 0;
  font-weight: 700;
  letter-spacing: 0.04em;
}

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

.btn-primary:hover {
  background: var(--accent-light);
}

.btn[aria-pressed="true"] {
  border-color: rgba(56, 189, 248, 0.6);
  background: var(--accent-soft);
  color: var(--accent-light);
}

.btn-label {
  overflow: hidden;
  text-overflow: ellipsis;
}

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

.icon-large {
  width: 28px;
  height: 28px;
  stroke-width: 2.5;
}

/* ------------------------------------------------------------ stats */

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

.stat {
  min-width: 0;
  padding: 7px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.stat dt,
.win-stats dt,
.keys-title {
  color: var(--muted);
  font-size: 11px;
  font-weight: 650;
  letter-spacing: 0.08em;
  line-height: 1.4;
  text-transform: uppercase;
}

.stat dd {
  margin: 0;
  overflow: hidden;
  font-size: 20px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1.3;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.stat-best dd {
  color: var(--accent-light);
  font-size: 17px;
  line-height: 1.53;
}

/* ------------------------------------------------------------ controls */

.controls {
  display: grid;
  gap: 8px;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.segment {
  min-height: 40px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  font-size: 15px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: background-color var(--speed) ease, color var(--speed) ease;
}

.segment:hover {
  background: rgba(148, 163, 184, 0.1);
  color: var(--text);
}

.segment[aria-pressed="true"] {
  background: var(--accent);
  color: var(--accent-ink);
}

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

.actions .btn {
  gap: 6px;
  padding: 0 10px;
  font-size: 14px;
}

/* ------------------------------------------------------------ help */

.help {
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}

.help-text {
  margin: 0;
}

.keys {
  display: none;
  margin-top: 14px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.keys-title {
  margin: 0 0 8px;
}

.keys-list {
  display: grid;
  gap: 7px;
  margin: 0;
  padding: 0;
  list-style: none;
}

/* Baseline alignment keeps a label level with the first line of a two-line key list. */
.keys-list li {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  color: var(--text);
}

.keycaps {
  display: inline-flex;
  flex: none;
  align-items: center;
  gap: 4px;
}

.keycaps-stack {
  flex-direction: column;
  align-items: flex-end;
}

.keycaps-or {
  margin-right: 2px;
  color: var(--muted);
  font-size: 12px;
}

kbd {
  display: inline-grid;
  place-items: center;
  min-width: 24px;
  height: 24px;
  padding: 0 6px;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font: 600 12px/1 var(--font);
}

/* ------------------------------------------------------------ board */

.board {
  --n: 4;
  --gap: 8px;
  --pad: 10px;
  --tile-radius: 10px;
  position: relative;
  justify-self: center;
  width: 100%;
  max-width: min(560px, max(240px, calc(100vh - 316px)));
  max-width: min(560px, max(240px, calc(100dvh - 316px)));
  padding: var(--pad);
  border: 1px solid var(--line);
  border-radius: 18px;
  background: linear-gradient(180deg, #1e293b, #172135);
  box-shadow: var(--shadow);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

.board[data-size="3"] {
  --gap: 10px;
  --tile-radius: 14px;
}

.board[data-size="5"] {
  --gap: 6px;
  --tile-radius: 8px;
}

.grid {
  position: relative;
  aspect-ratio: 1 / 1;
  container-type: inline-size;
}

.cell,
.tile {
  position: absolute;
  top: 0;
  left: 0;
  width: calc((100% - (var(--n) - 1) * var(--gap)) / var(--n));
  height: calc((100% - (var(--n) - 1) * var(--gap)) / var(--n));
  border-radius: var(--tile-radius);
  transform: translate(calc(var(--col) * (100% + var(--gap))), calc(var(--row) * (100% + var(--gap))));
}

.cell {
  background: rgba(2, 6, 23, 0.42);
  box-shadow: inset 0 2px 6px rgba(2, 6, 23, 0.55);
}

.tile {
  display: grid;
  place-items: center;
  margin: 0;
  padding: 0;
  overflow: hidden;
  border: 0;
  background: linear-gradient(180deg, #3b4a63, #2a374e);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.08), 0 6px 14px -6px rgba(2, 6, 23, 0.9);
  color: var(--text);
  font-size: 24px;
  font-size: calc(36cqi / var(--n));
  font-weight: 750;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  cursor: default;
  -webkit-tap-highlight-color: transparent;
  transition: transform var(--speed) var(--ease), scale 150ms ease, background var(--speed) ease,
    color var(--speed) ease, box-shadow var(--speed) ease, filter var(--speed) ease;
}

.tile.is-home {
  background: linear-gradient(180deg, #0f5c86, #0c4a6e);
  box-shadow: inset 0 0 0 1px rgba(56, 189, 248, 0.5), inset 0 1px 0 rgba(255, 255, 255, 0.1),
    0 6px 14px -6px rgba(2, 6, 23, 0.9);
  color: var(--accent-light);
}

.tile.is-movable {
  cursor: pointer;
}

.tile.is-movable:active {
  scale: 0.96;
}

@media (hover: hover) {
  .tile.is-movable:hover {
    filter: brightness(1.14);
  }
}

.tile.is-hint {
  z-index: 1;
  box-shadow: 0 0 0 3px var(--accent), 0 0 26px rgba(56, 189, 248, 0.6);
  animation: hint-pulse 1.1s ease-in-out infinite;
}

.board.is-won .tile {
  background: linear-gradient(180deg, var(--accent-light), var(--accent));
  box-shadow: 0 8px 20px -8px rgba(56, 189, 248, 0.7);
  color: var(--accent-ink);
  animation: tile-pop 520ms var(--ease) both;
  animation-delay: calc(var(--order) * 28ms);
}

.tile-art {
  position: absolute;
  inset: 0;
  display: none;
  width: 100%;
  height: 100%;
}

.board.is-picture .tile {
  background: #111827;
}

.board.is-picture .tile-art {
  display: block;
}

.board.is-picture .tile-number {
  position: absolute;
  top: 7%;
  left: 7%;
  z-index: 1;
  padding: 0.2em 0.45em;
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.62);
  color: #f8fafc;
  font-size: 11px;
  font-size: max(10px, calc(14cqi / var(--n)));
}

.board.is-picture .tile.is-home::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  box-shadow: inset 0 0 0 2px rgba(125, 211, 252, 0.85);
  pointer-events: none;
}

.grid-art {
  position: absolute;
  inset: 0;
  z-index: 2;
  width: 100%;
  height: 100%;
  border-radius: var(--tile-radius);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 250ms ease 150ms, visibility 0s linear 400ms;
}

.board.is-picture.is-won .grid-art {
  opacity: 1;
  visibility: visible;
  transition: opacity 250ms ease 150ms;
}

.board.is-picture.is-won .tile {
  box-shadow: none;
}

@keyframes hint-pulse {
  50% {
    box-shadow: 0 0 0 5px rgba(56, 189, 248, 0.35), 0 0 32px rgba(56, 189, 248, 0.7);
  }
}

@keyframes tile-pop {
  45% {
    scale: 1.07;
  }
}

/* ------------------------------------------------------------ win dialog */

.dialog {
  width: min(360px, calc(100vw - 32px));
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--surface);
  box-shadow: 0 30px 80px -20px rgba(2, 6, 23, 0.95);
  color: var(--text);
}

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

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

.dialog-body {
  display: grid;
  gap: 12px;
  padding: 24px;
  text-align: center;
}

.dialog-badge {
  display: grid;
  place-items: center;
  width: 56px;
  height: 56px;
  margin: 0 auto;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
}

.dialog-title {
  margin: 0;
  font-size: 24px;
  font-weight: 750;
}

.dialog-text {
  margin: 0;
  color: var(--muted);
}

.win-stats {
  display: grid;
  grid-auto-columns: minmax(0, 1fr);
  grid-auto-flow: column;
  gap: 8px;
  margin: 0;
}

.win-stats div {
  padding: 8px;
  border-radius: 10px;
  background: var(--bg);
}

.win-stats div[hidden] {
  display: none;
}

.win-stats dd {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.dialog-record {
  margin: 0;
  color: var(--accent-light);
  font-weight: 600;
}

.dialog-record:empty {
  display: none;
}

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

@keyframes dialog-in {
  from {
    opacity: 0;
    scale: 0.96;
  }
}

/* ------------------------------------------------------------ wide screens */

@media (min-width: 900px), (min-width: 600px) and (orientation: landscape) {
  .app {
    max-width: 920px;
  }

  .layout {
    flex: none;
    margin: auto;
    grid-template-columns: auto minmax(240px, 300px);
    grid-template-rows: auto auto 1fr;
    grid-template-areas: "board stats" "board controls" "board help";
    align-content: center;
    justify-content: center;
    column-gap: 28px;
    row-gap: 16px;
    max-width: none;
  }

  .board {
    width: min(560px, max(240px, calc(100vh - 160px)), calc(100vw - 420px));
    width: min(560px, max(240px, calc(100dvh - 160px)), calc(100vw - 420px));
    max-width: none;
    align-self: start;
  }

  .help {
    text-align: left;
  }

  .keys {
    display: block;
  }
}

@media (pointer: coarse) {
  .keys {
    display: none;
  }
}

/* ------------------------------------------------------------ small screens */

@media (max-width: 359px) {
  .header-actions .btn-label {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }

  .actions .icon {
    display: none;
  }

  .board {
    --gap: 6px;
    --pad: 8px;
  }
}

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