:root {
  color-scheme: light dark;
  --bg: #f6f7fb;
  --panel: #ffffff;
  --ink: #1c2030;
  --muted: #5c6377;
  --accent: #3a6ff7;
  --accent-ink: #ffffff;
  --tile: #ffffff;
  --tile-border: #d7dbea;
  --tile-selected: #3a6ff7;
  --danger: #c2372f;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161f;
    --panel: #1d2130;
    --ink: #eef0f8;
    --muted: #9aa1b8;
    --accent: #7c9bff;
    --accent-ink: #101322;
    --tile: #232840;
    --tile-border: #3a4160;
    --tile-selected: #7c9bff;
    --danger: #ff8a80;
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  background: var(--bg);
  color: var(--ink);
  font-family:
    system-ui,
    -apple-system,
    "Segoe UI",
    Roboto,
    sans-serif;
}

.app {
  width: 100%;
  max-width: 26rem;
  padding: 1.25rem 1rem 2rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.app-header {
  text-align: center;
}

.app-header h1 {
  margin: 0;
  font-size: 2rem;
  letter-spacing: 0.02em;
}

.tagline {
  margin: 0.25rem 0 0;
  color: var(--muted);
  font-size: 0.95rem;
}

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

.stat {
  background: var(--panel);
  border: 1px solid var(--tile-border);
  border-radius: 0.6rem;
  padding: 0.45rem 0.25rem;
  text-align: center;
}

.stat-label {
  display: block;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.stat strong {
  font-size: 1.15rem;
  font-variant-numeric: tabular-nums;
}

.board {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 0.3rem;
  background: var(--panel);
  border: 1px solid var(--tile-border);
  border-radius: 0.75rem;
  padding: 0.5rem;
  /* Keep the fixed 7×12 grid fully visible: on short viewports, bound the
     board width so its 12/7 height fits below the compacted header. */
  width: min(100%, calc((100vh - 16rem) * 7 / 12));
  margin-inline: auto;
}

@media (max-height: 900px) {
  .app {
    padding-top: 0.6rem;
    gap: 0.6rem;
  }

  .app-header h1 {
    font-size: 1.4rem;
  }

  .tagline {
    display: none;
  }
}

.cell {
  appearance: none;
  border: 2px solid var(--tile-border);
  border-radius: 0.5rem;
  background: var(--tile);
  color: var(--ink);
  font-size: 1.1rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  aspect-ratio: 1;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
  transition:
    border-color 120ms ease,
    background 120ms ease,
    transform 120ms ease;
}

.cell:active {
  transform: scale(0.94);
}

.cell.selected {
  border-color: var(--tile-selected);
  background: var(--accent);
  color: var(--accent-ink);
}

.cell.candidate {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 16%, var(--tile));
}

.cell.clearing {
  animation: cell-clear 220ms ease forwards;
}

@keyframes cell-clear {
  40% {
    transform: scale(1.15);
    border-color: var(--accent);
    background: var(--accent);
    color: var(--accent-ink);
  }
  100% {
    transform: scale(0.6);
    opacity: 0;
  }
}

.cell.hint {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 35%, transparent);
}

.cell-hole {
  aspect-ratio: 1;
  border-radius: 0.5rem;
  background: color-mix(in srgb, var(--tile-border) 30%, transparent);
}

.cell-hole.sightline {
  background: color-mix(in srgb, var(--accent) 22%, var(--tile));
}

.message {
  min-height: 1.4em;
  margin: 0;
  text-align: center;
  font-size: 0.95rem;
  color: var(--muted);
}

.message.error {
  color: var(--danger);
}

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

.controls button,
.end button {
  appearance: none;
  border: 1px solid var(--tile-border);
  border-radius: 0.6rem;
  background: var(--panel);
  color: var(--ink);
  font-size: 0.9rem;
  padding: 0.55rem 0.25rem;
  white-space: nowrap;
  cursor: pointer;
  touch-action: manipulation;
}

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

.controls #add-numbers {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}

.controls #add-numbers:disabled {
  background: var(--panel);
  border-color: var(--tile-border);
  color: var(--ink);
  font-weight: 400;
}

.rules {
  background: var(--panel);
  border: 1px solid var(--tile-border);
  border-radius: 0.75rem;
  padding: 0.6rem 1rem;
  font-size: 0.9rem;
  color: var(--muted);
}

.rules summary {
  cursor: pointer;
  color: var(--ink);
  font-weight: 600;
}

.rules ul {
  margin: 0.5rem 0 0.25rem;
  padding-left: 1.2rem;
  line-height: 1.6;
}

.end {
  text-align: center;
  background: var(--panel);
  border: 2px solid var(--accent);
  border-radius: 0.75rem;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  align-items: center;
}

.welcome {
  border-color: var(--tile-border);
}

.end[hidden] {
  display: none;
}

.end-title {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 700;
}

.end-copy {
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.end button {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
  padding: 0.55rem 1.5rem;
}

@media (max-width: 380px) {
  .cell {
    font-size: 0.95rem;
    border-radius: 0.4rem;
  }

  .board {
    gap: 0.22rem;
    padding: 0.4rem;
  }

  .controls {
    grid-template-columns: repeat(2, 1fr);
  }
}

/*
 * The Portal gives custom runtimes a wide 3:2 desktop iframe. Number Match is
 * a tall 12x7 board, so stacking every control above/below the board wastes the
 * horizontal space and makes an otherwise complete game document scroll.
 * On roomy desktop viewports, keep the board on the left and move the compact
 * HUD/rules into a right rail. Mobile and short landscape layouts keep the
 * existing single-column flow.
 */
@media (min-width: 700px) {
  html,
  body {
    height: 100%;
    overflow: hidden;
  }

  body {
    align-items: stretch;
  }

  .app {
    position: relative;
    width: min(100%, 60rem);
    max-width: 60rem;
    height: 100vh;
    min-height: 0;
    margin-inline: auto;
    padding: 0.75rem 1rem;
    display: grid;
    grid-template-columns: minmax(20rem, 1fr) minmax(15rem, 19rem);
    grid-template-areas:
      "board header"
      "board stats"
      "board message"
      "board controls"
      "board rules";
    grid-template-rows: auto auto minmax(0, auto) auto minmax(0, 1fr);
    align-items: start;
    align-content: center;
    justify-content: center;
    column-gap: clamp(1rem, 3vw, 2rem);
    row-gap: 0.5rem;
    overflow: hidden;
  }

  .app-header {
    grid-area: header;
    text-align: left;
  }

  .app-header h1 {
    font-size: 1.5rem;
  }

  .board {
    grid-area: board;
    width: min(100%, 28rem, calc((100vh - 1.5rem) * 7 / 12));
    margin-inline: 0;
    align-self: center;
    justify-self: center;
  }

  .stats { grid-area: stats; }
  .message { grid-area: message; text-align: left; }
  .controls { grid-area: controls; }
  .rules {
    grid-area: rules;
    align-self: start;
  }

  .rules[open] {
    max-height: min(42vh, 18rem);
    overflow: auto;
  }

  .end {
    position: absolute;
    top: 50%;
    left: 50%;
    z-index: 5;
    width: min(25rem, calc(100% - 2rem));
    transform: translate(-50%, -50%);
    box-shadow: 0 1.5rem 4rem color-mix(in srgb, #000 45%, transparent);
  }
}

@media (min-width: 700px) and (min-height: 680px) {
  .tagline {
    display: block;
  }
}

/* Keep status text from reserving vertical space when there is nothing to say. */
.message:empty {
  display: none;
}

/* Welcome and terminal cards should never sit below the 12-row board. */
.app {
  position: relative;
}

.end {
  position: absolute;
  top: 50%;
  left: 50%;
  z-index: 5;
  width: min(25rem, calc(100% - 2rem));
  transform: translate(-50%, -50%);
  box-shadow: 0 1.5rem 4rem color-mix(in srgb, #000 45%, transparent);
}

/* Portrait phones keep the familiar single-column flow, but spend less
   height on chrome so the board and all primary controls stay in view. */
@media (max-width: 520px) and (orientation: portrait) {
  .app {
    max-width: 25rem;
    padding: 0.55rem 0.7rem 0.8rem;
    gap: 0.45rem;
  }

  .app-header h1 {
    font-size: 1.25rem;
    line-height: 1.1;
  }

  .stats {
    gap: 0.35rem;
  }

  .stat {
    border-radius: 0.5rem;
    padding: 0.32rem 0.15rem;
  }

  .stat-label {
    font-size: 0.62rem;
  }

  .stat strong {
    font-size: 1rem;
  }

  .board {
    width: min(100%, max(18.5rem, calc((100svh - 17rem) * 7 / 12)));
    gap: 0.22rem;
    padding: 0.4rem;
  }

  .cell {
    border-width: 1.5px;
    font-size: 1rem;
  }

  .controls {
    gap: 0.35rem;
  }

  .controls button {
    min-height: 2.75rem;
    padding: 0.45rem 0.2rem;
  }

  .rules {
    padding: 0.55rem 0.75rem;
  }
}

/* The Portal chrome reduces the embedded portrait Runtime to roughly 356x678.
   Compact that shorter iframe without creating a second scroll container. */
@media (max-width: 520px) and (orientation: portrait) and (max-height: 720px) {
  .app {
    padding: 0.35rem 0.55rem 0.5rem;
    gap: 0.3rem;
  }

  .app-header {
    display: none;
  }

  .board {
    width: min(100%, 17.5rem);
    gap: 0.18rem;
    padding: 0.32rem;
  }

  .stat {
    padding: 0.2rem 0.1rem;
  }

  .controls {
    gap: 0.3rem;
  }

  .controls button {
    min-height: 2.5rem;
    padding: 0.3rem 0.15rem;
  }

  .rules {
    padding: 0.4rem 0.6rem;
    font-size: 0.82rem;
  }
}

/* A phone in landscape is height-limited, not width-limited. Reuse the
   spare horizontal space instead of shrinking the 12-row board to a sliver. */
@media (orientation: landscape) and (max-height: 600px) and (min-width: 600px) {
  body {
    align-items: center;
  }

  .app {
    max-width: 52rem;
    min-height: 100svh;
    padding: 0.25rem 0.5rem;
    display: grid;
    grid-template-columns: auto minmax(16rem, 20rem);
    grid-template-areas:
      "board header"
      "board stats"
      "board message"
      "board controls"
      "board rules";
    grid-template-rows: auto auto minmax(0, auto) auto 1fr;
    align-items: start;
    align-content: center;
    justify-content: center;
    column-gap: 1rem;
    row-gap: 0.25rem;
  }

  .app-header {
    grid-area: header;
    text-align: left;
  }

  .app-header h1 {
    font-size: 1.05rem;
  }

  .tagline {
    display: none;
  }

  .board {
    grid-area: board;
    width: min(16rem, calc((100svh - 1.5rem) * 7 / 12));
    margin-inline: 0;
    gap: 0.16rem;
    padding: 0.3rem;
    align-self: center;
  }

  .cell {
    border-width: 1px;
    border-radius: 0.35rem;
    font-size: 0.85rem;
  }

  .cell-hole {
    border-radius: 0.35rem;
  }

  .stats {
    grid-area: stats;
    gap: 0.35rem;
  }

  .stat {
    padding: 0.18rem 0.1rem;
  }

  .message {
    grid-area: message;
    text-align: left;
  }

  .controls {
    grid-area: controls;
    gap: 0.35rem;
  }

  .controls button {
    min-height: 2.25rem;
    padding: 0.25rem 0.15rem;
  }

  .rules {
    grid-area: rules;
    padding: 0.3rem 0.55rem;
    font-size: 0.72rem;
  }

  .rules > summary {
    display: none;
  }
}
