:root {
  --bg: #0d1026;
  --panel: #1b2047;
  --text: #eef0ff;
  --muted: #9aa3d6;
  --accent: #ff5c8a;
  --done: #3ddc97;
  --current: #ffb84d;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

.app {
  max-width: 520px;
  margin: 0 auto;
  /* Extra room under the arrows so thumbs aren't pressed against the screen edge. */
  padding: max(12px, env(safe-area-inset-top)) 16px calc(env(safe-area-inset-bottom) + 28px);
  display: flex;
  flex-direction: column;
  gap: 12px;
  /* Exactly one screen tall, so the arrows are always visible; the maze takes
     whatever height is left (see .board). */
  height: 100dvh;
  /* Swipes anywhere on the screen steer the player, so don't let them scroll or zoom. */
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.top-actions { display: flex; gap: 6px; }
.icon-button { padding: 8px 10px; font-size: 0.95rem; white-space: nowrap; }
.icon-button[hidden] { display: none; }
h1 { margin: 0; font-size: 1.35rem; letter-spacing: 0.02em; }
.sub { color: var(--muted); font-size: 0.85rem; font-variant-numeric: tabular-nums; }

.hud { display: flex; flex-direction: column; gap: 8px; }
.timer {
  font-size: 2rem;
  font-weight: 700;
  text-align: center;
  font-variant-numeric: tabular-nums;
}
.run-badge {
  margin: -4px 0 0;
  text-align: center;
  font-size: 0.8rem;
  color: var(--current);
}
.timer.pulse { animation: pulse 0.45s ease-out; }
@keyframes pulse {
  0% { transform: scale(1.18); color: var(--done); }
  100% { transform: scale(1); }
}

.splits {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 6px;
}
.splits li {
  background: var(--panel);
  border-radius: 10px;
  padding: 6px 4px;
  text-align: center;
  border: 2px solid transparent;
  font-variant-numeric: tabular-nums;
}
.splits .name { display: block; font-size: 0.7rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
.splits .time { font-size: 0.95rem; font-weight: 600; }
.splits li.current { border-color: var(--current); }
.splits li.done { border-color: var(--done); }

.board {
  position: relative;
  width: 100%;
  flex: 1 1 0;
  min-height: 140px;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
canvas { display: block; border-radius: 12px; transition: filter 0.6s; }

.board-message {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: var(--muted);
  text-align: center;
}
.board-message[hidden] { display: none; }
.board-message p { margin: 0; max-width: 260px; }

.solved-note {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  text-align: center;
  pointer-events: none;
}
.solved-note strong { font-size: 1.6rem; color: var(--done); }
.solved-note .countdown { margin: 0; color: var(--text); }
.solved-note button { margin-top: 10px; pointer-events: auto; }
.board.solved #maze { filter: blur(9px) brightness(0.7); }
.board.solved .solved-note { display: flex; }

.hint {
  margin: -4px 0 0;
  text-align: center;
  font-size: 0.8rem;
  color: var(--muted);
  transition: opacity 0.4s;
}
.hint.gone { opacity: 0; }
/* On touch screens the space goes to the maze instead; the ? button explains the controls. */
@media (hover: none), (pointer: coarse) {
  .hint { display: none; }
}
.mouse-only { display: none; }
@media (hover: hover) and (pointer: fine) {
  .touch-only { display: none; }
  .mouse-only { display: inline; }
  /* On computers the keyboard is the main control; the arrows are a mouse backup. */
  .dpad { grid-template-columns: repeat(3, 60px); grid-template-rows: repeat(2, 50px); gap: 8px; }
}

.dpad {
  display: grid;
  grid-template-columns: repeat(3, 76px);
  grid-template-rows: repeat(2, 64px);
  grid-template-areas: ". up ." "left down right";
  gap: 10px;
  justify-content: center;
  padding: 6px 0;
  touch-action: none; /* swipes across the arrows dash instead of scrolling */
  user-select: none;
  -webkit-user-select: none;
}
.dpad button { font-size: 1.3rem; border-radius: 16px; touch-action: none; }
.dpad button.lit { border-color: var(--accent); color: var(--accent); }
.dpad .up { grid-area: up; }
.dpad .left { grid-area: left; }
.dpad .right { grid-area: right; }
.dpad .down { grid-area: down; }

button {
  background: var(--panel);
  color: var(--text);
  border: 1px solid #2c3370;
  border-radius: 10px;
  padding: 10px 16px;
  font: inherit;
  cursor: pointer;
  touch-action: manipulation;
}
button:active { transform: scale(0.96); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.link { background: none; border: none; color: var(--muted); text-decoration: underline; }

dialog {
  background: var(--panel);
  color: var(--text);
  border: none;
  border-radius: 18px;
  padding: 20px;
  width: min(420px, calc(100vw - 32px));
  text-align: center;
}
dialog::backdrop { background: rgba(5, 6, 18, 0.7); }

.eyebrow { margin: 0; color: var(--muted); text-transform: uppercase; font-size: 0.75rem; letter-spacing: 0.08em; }
.big-time { margin: 4px 0; font-size: 2.6rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.meta { margin: 0 0 14px; color: var(--muted); font-size: 0.85rem; }

.results-table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.results-table th { color: var(--muted); font-weight: 500; font-size: 0.75rem; text-transform: uppercase; padding-bottom: 6px; }
.results-table td { padding: 6px 4px; border-top: 1px solid #2c3370; }
.results-table td:first-child, .results-table th:first-child { text-align: left; }
.results-table td:not(:first-child), .results-table th:not(:first-child) { text-align: right; }

.actions { display: flex; gap: 10px; justify-content: center; margin-top: 16px; }
.share-status { min-height: 1.2em; margin: 8px 0 0; color: var(--done); font-size: 0.85rem; }
.countdown { margin: 4px 0 0; color: var(--muted); font-size: 0.85rem; font-variant-numeric: tabular-nums; }

h2 { margin: 0 0 8px; font-size: 1.3rem; }
.muted { color: var(--muted); }
.small { font-size: 0.8rem; }
.status { margin: 0 0 6px; font-weight: 600; }
.chip {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  background: #2c3370;
  color: var(--muted);
  font-size: 0.7rem;
  vertical-align: middle;
}
.guest-note { margin: 12px 0 0; }
button.link { padding: 0; }
.actions button.link { padding: 10px 0; }
.actions { flex-wrap: wrap; }
button:disabled { opacity: 0.6; }

.welcome { text-align: center; }
.wide { width: 100%; padding: 14px; font-size: 1.05rem; }
.divider { display: flex; align-items: center; gap: 10px; margin: 14px 0; color: var(--muted); font-size: 0.8rem; }
.divider::before, .divider::after { content: ""; flex: 1; border-top: 1px solid #2c3370; }

.name-form { text-align: left; }
.name-form label { display: block; font-size: 0.85rem; color: var(--muted); margin-bottom: 6px; }
.name-form .row { display: flex; gap: 8px; }
.name-form input {
  flex: 1;
  min-width: 0;
  background: var(--bg);
  color: var(--text);
  border: 1px solid #2c3370;
  border-radius: 10px;
  padding: 10px 12px;
  font: inherit;
  font-size: 16px; /* stops iOS zooming into the field */
}
.name-form input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.form-error { min-height: 1.1em; margin: 6px 0 0; color: var(--accent); font-size: 0.8rem; text-align: left; }

.tabs { display: flex; gap: 6px; justify-content: center; margin: 4px 0 12px; }
.tabs button { padding: 6px 12px; font-size: 0.85rem; }
.tabs button[aria-selected="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }
.leaderboard #leaderboard-body { max-height: 60vh; overflow-y: auto; }
.board-table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; font-size: 0.9rem; }
.board-table th { color: var(--muted); font-weight: 500; font-size: 0.7rem; text-transform: uppercase; padding-bottom: 6px; }
.board-table td, .board-table th { padding: 6px 4px; text-align: right; }
.board-table td { border-top: 1px solid #2c3370; }
.board-table td:nth-child(2), .board-table th:nth-child(2) { text-align: left; }
.board-table td:first-child, .board-table th:first-child { text-align: left; width: 2.2em; color: var(--muted); }
.board-table tr.me td { background: rgba(255, 92, 138, 0.12); }
.board-table tr.gap td { text-align: center; color: var(--muted); border-top: none; }
.board-table .squares { letter-spacing: 1px; white-space: nowrap; }
.guest-name { color: var(--muted); }

/* Short screens (phones with browser bars showing): tighten everything above and
   below the maze so it can stay as large as possible. */
@media (max-height: 760px) {
  .app { gap: 8px; padding-top: max(8px, env(safe-area-inset-top)); padding-bottom: calc(env(safe-area-inset-bottom) + 22px); }
  h1 { font-size: 1.15rem; }
  .icon-button { padding: 6px 9px; }
  .timer { font-size: 1.6rem; }
  .splits li { padding: 3px 4px; }
  .splits .time { font-size: 0.85rem; }
  .hint { margin-top: -2px; font-size: 0.75rem; }
  .dpad { grid-template-columns: repeat(3, 72px); grid-template-rows: repeat(2, 54px); gap: 8px; padding: 0; }
}

.howto { list-style: none; margin: 12px 0 16px; padding: 0; text-align: left; display: grid; gap: 8px; font-size: 0.9rem; }
.howto li { display: flex; gap: 10px; align-items: flex-start; line-height: 1.35; }
.howto li span { flex: 0 0 1.4em; text-align: center; }
#help-button { font-weight: 700; min-width: 2.4em; }

h1 { white-space: nowrap; }
/* Narrow phones: drop the 👤 so the title and a 12-letter name fit on one line. */
@media (max-width: 400px) {
  .account-icon { display: none; }
  .top { gap: 8px; }
  .top-actions { gap: 4px; }
}

.privacy-list { text-align: left; margin: 8px 0 0; padding-left: 18px; display: grid; gap: 8px; font-size: 0.88rem; line-height: 1.4; color: var(--text); }
.privacy-list b { color: var(--text); }
button.link.small { font-size: 0.8rem; }

.board-table td.dnf { color: var(--muted); }

/* Phones and tablets: no arrows; a small swipe area marks where to put your thumb. */
.swipe-pad { display: none; }
@media (hover: none), (pointer: coarse) {
  .dpad { display: none; }
  .swipe-pad {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    grid-template-rows: auto auto auto;
    grid-template-areas: ". up ." "left word right" ". down .";
    align-items: center;
    justify-items: center;
    align-self: center;
    width: min(200px, 60%);
    padding: 6px 14px;
    border: 1px solid #2c3370;
    border-radius: 18px;
    background: var(--panel);
    color: #5a63a8;
    font-size: 0.95rem;
    line-height: 1.15;
  }
  .swipe-pad .up { grid-area: up; }
  .swipe-pad .down { grid-area: down; }
  .swipe-pad .left { grid-area: left; justify-self: end; padding-right: 10px; }
  .swipe-pad .right { grid-area: right; justify-self: start; padding-left: 10px; }
  .swipe-pad .word { grid-area: word; font-size: 0.75rem; letter-spacing: 0.14em; text-transform: uppercase; color: #4a5290; }
  /* "Show arrow buttons" setting (in the ? screen): arrows instead of the swipe pad. */
  html.arrows .dpad { display: grid; }
  html.arrows .swipe-pad { display: none; }
}

.setting { display: flex; gap: 10px; align-items: flex-start; text-align: left; margin: 14px 0 0; padding: 10px 12px; border: 1px solid #2c3370; border-radius: 12px; font-size: 0.9rem; cursor: pointer; }
.setting input { width: 18px; height: 18px; margin: 1px 0 0; accent-color: var(--accent); flex: none; }
label.setting.touch-only { display: flex; }
@media (hover: hover) and (pointer: fine) { label.setting.touch-only { display: none; } }

/* Mode switch (Normal | Key Hunt) */
.mode-tabs { display: flex; gap: 6px; justify-content: center; margin-top: -4px; }
.mode-tabs a {
  padding: 5px 14px;
  border-radius: 999px;
  border: 1px solid #2c3370;
  color: var(--muted);
  font-size: 0.85rem;
  text-decoration: none;
  touch-action: manipulation;
}
.mode-tabs a[aria-current="true"] { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
@media (max-height: 760px) { .mode-tabs a { padding: 4px 12px; font-size: 0.8rem; } }

/* Short messages over the board (Key Hunt) */
.toast {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(13, 16, 38, 0.92);
  border: 1px solid #2c3370;
  color: var(--text);
  padding: 7px 14px;
  border-radius: 999px;
  font-size: 0.85rem;
  white-space: nowrap;
  pointer-events: none;
  z-index: 2;
}
.toast[hidden] { display: none; }
.howto[hidden] { display: none; }

/* Finish celebration: confetti drawn over the board */
.fx { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 3; }

/* Profile: your games */
.subhead { margin: 16px 0 6px; font-size: 0.85rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; text-align: left; }
.history-list { max-height: 38vh; overflow-y: auto; text-align: left; border-top: 1px solid #2c3370; }
.history-row { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 9px 2px; border-bottom: 1px solid #2c3370; font-size: 0.9rem; font-variant-numeric: tabular-nums; }
.small-button { padding: 5px 10px; font-size: 0.8rem; white-space: nowrap; }
.rename { margin-top: 14px; text-align: left; }
.rename summary { cursor: pointer; color: var(--muted); font-size: 0.85rem; }
.rename .name-form { margin-top: 10px; }

/* Replay viewer */
.replay-dialog { width: min(460px, calc(100vw - 24px)); padding: 16px; }
.replay-dialog .timer { font-size: 1.6rem; margin: 2px 0 8px; }
.replay-board { width: 100%; aspect-ratio: 1; display: flex; justify-content: center; }
.replay-controls { display: flex; gap: 8px; justify-content: center; margin-top: 12px; }
.replay-controls button { padding: 7px 12px; }
.replay-controls button[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); }
.cheer { margin: -2px 0 8px; color: var(--done); font-weight: 600; }

/* Time to beat */
.timer-row { text-align: center; }
.timer { display: inline-block; }
.timer.warn { color: var(--current); }
.timer.danger { color: #ff3b3b; }
.howto li span.time-limit { flex: none; display: inline; }
.board.timed-out .solved-note strong { color: var(--accent); }
