/* ==========================================================================
   Gridlock — interface styles
   Dark industrial computer core. Mobile-first, scales up to a desktop layout
   with side panels flanking a fixed, square playfield.
   ========================================================================== */

:root {
  --bg: #05070b;
  --panel: #0a0e15;
  --panel-2: #0e1420;
  --line: #1d2b45;
  --line-hot: #2b4a72;
  --cyan: #3ff0ff;
  --violet: #c07bff;
  --amber: #ffb44d;
  --gold: #ffd166;
  --red: #ff3b5c;
  --green: #5cf2c0;
  --text: #dbe6f5;
  --dim: #6d7d95;

  --radius: 14px;
  --tap: 44px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);

  --font: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

#app {
  position: relative;
  height: 100%;
  width: 100%;
  /* A faint sweep of light so the black never looks flat. */
  background:
    radial-gradient(ellipse at 50% -10%, rgba(63, 240, 255, 0.09), transparent 55%),
    radial-gradient(ellipse at 90% 110%, rgba(192, 123, 255, 0.07), transparent 50%),
    var(--bg);
}

/* ── Screens ─────────────────────────────────────────────────────────────── */
.screen {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  padding: calc(var(--safe-t) + 12px) calc(var(--safe-r) + 12px) calc(var(--safe-b) + 12px)
    calc(var(--safe-l) + 12px);
}

.screen.is-active {
  display: flex;
  animation: screen-in 0.28s ease both;
}

@keyframes screen-in {
  from {
    opacity: 0;
    transform: translateY(10px) scale(0.995);
  }
}

/* ── Loading ─────────────────────────────────────────────────────────────── */
#screen-loading {
  align-items: center;
  justify-content: center;
  gap: 22px;
}

.loading-core {
  display: flex;
  gap: 10px;
}

.loading-core span {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  background: var(--cyan);
  box-shadow: 0 0 18px var(--cyan);
  animation: pulse-dot 1.05s ease-in-out infinite;
}

.loading-core span:nth-child(2) {
  animation-delay: 0.14s;
  background: var(--violet);
  box-shadow: 0 0 18px var(--violet);
}

.loading-core span:nth-child(3) {
  animation-delay: 0.28s;
  background: var(--amber);
  box-shadow: 0 0 18px var(--amber);
}

@keyframes pulse-dot {
  0%, 100% { transform: scale(0.6); opacity: 0.35; }
  50% { transform: scale(1.15); opacity: 1; }
}

.loading-text {
  color: var(--dim);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  font-size: 12px;
  margin: 0;
}

/* ── Menu ────────────────────────────────────────────────────────────────── */
.menu {
  align-items: center;
  justify-content: center;
}

.menu-inner {
  width: min(440px, 100%);
  display: flex;
  flex-direction: column;
  gap: clamp(20px, 5vh, 44px);
  text-align: center;
}

.logo {
  margin: 0;
  font-size: clamp(40px, 13vw, 68px);
  font-weight: 800;
  letter-spacing: 0.16em;
  display: flex;
  justify-content: center;
}

.logo span {
  color: var(--text);
  text-shadow: 0 0 24px rgba(63, 240, 255, 0.55);
  animation: logo-flicker 5s ease-in-out infinite;
}

.logo span:nth-child(4n + 1) { animation-delay: 0.4s; }
.logo span:nth-child(4n + 2) { animation-delay: 1.1s; color: var(--cyan); }
.logo span:nth-child(4n + 3) { animation-delay: 2.2s; }
.logo span:nth-child(4n) { animation-delay: 3.1s; }

@keyframes logo-flicker {
  0%, 92%, 100% { opacity: 1; }
  94% { opacity: 0.35; }
  96% { opacity: 1; }
}

.tagline {
  margin: 8px 0 0;
  color: var(--dim);
  font-size: 13px;
  letter-spacing: 0.22em;
  text-transform: uppercase;
}

.menu-actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.menu-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.menu-foot {
  color: var(--dim);
  font-size: 12px;
  letter-spacing: 0.08em;
}

.menu-foot .dot {
  margin: 0 8px;
  opacity: 0.5;
}

/* ── Buttons ─────────────────────────────────────────────────────────────── */
.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: linear-gradient(180deg, rgba(29, 43, 69, 0.35), rgba(10, 14, 21, 0.6));
  color: var(--text);
  font: inherit;
  font-weight: 600;
  letter-spacing: 0.06em;
  min-height: var(--tap);
  padding: 11px 18px;
  border-radius: var(--radius);
  cursor: pointer;
  transition: transform 0.12s ease, border-color 0.16s ease, background 0.16s ease;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
}

.btn:hover:not(:disabled) {
  border-color: var(--line-hot);
  background: linear-gradient(180deg, rgba(43, 74, 114, 0.4), rgba(10, 14, 21, 0.7));
}

.btn:active:not(:disabled) {
  transform: scale(0.975);
}

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

.btn-primary {
  border-color: rgba(63, 240, 255, 0.5);
  background: linear-gradient(180deg, rgba(63, 240, 255, 0.22), rgba(63, 240, 255, 0.05));
  color: #eafcff;
  box-shadow: 0 0 26px rgba(63, 240, 255, 0.16);
}

.btn-primary:hover {
  border-color: var(--cyan);
  background: linear-gradient(180deg, rgba(63, 240, 255, 0.32), rgba(63, 240, 255, 0.08));
}

.btn-ghost {
  background: transparent;
  border-color: transparent;
  color: var(--dim);
}

.btn-ghost:hover {
  color: var(--text);
  background: rgba(29, 43, 69, 0.3);
}

.btn-danger {
  border-color: rgba(255, 59, 92, 0.45);
  color: #ffd7de;
  background: linear-gradient(180deg, rgba(255, 59, 92, 0.18), transparent);
}

.btn-label {
  font-size: 17px;
}

.btn-sub {
  font-size: 11px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--dim);
}

.btn-primary .btn-sub {
  color: rgba(234, 252, 255, 0.7);
}

.btn-icon {
  appearance: none;
  width: var(--tap);
  height: var(--tap);
  border-radius: 12px;
  border: 1px solid var(--line);
  background: rgba(10, 14, 21, 0.7);
  color: var(--text);
  font-size: 17px;
  cursor: pointer;
  display: grid;
  place-items: center;
  flex: none;
}

.btn-icon:active {
  transform: scale(0.94);
}

/* ── Sheets (level select, settings, stats, help) ────────────────────────── */
.sheet {
  gap: 14px;
}

.sheet-head {
  display: flex;
  align-items: center;
  gap: 14px;
  flex: none;
}

.sheet-head h2 {
  margin: 0;
  font-size: 20px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  font-weight: 700;
}

.sheet-meta {
  margin-left: auto;
  color: var(--dim);
  font-size: 12px;
  letter-spacing: 0.1em;
}

.sheet-body,
.level-grid {
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 20px;
}

.sheet-body {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
}

.sub-head {
  margin: 18px 0 2px;
  font-size: 12px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--dim);
}

/* ── Level select ────────────────────────────────────────────────────────── */
.level-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px;
  max-width: 900px;
  width: 100%;
  margin: 0 auto;
  align-content: start;
}

.level-card {
  position: relative;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: linear-gradient(160deg, rgba(29, 43, 69, 0.32), rgba(10, 14, 21, 0.7));
  padding: 12px 14px 14px;
  text-align: left;
  color: inherit;
  font: inherit;
  cursor: pointer;
  min-height: 108px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  transition: transform 0.12s ease, border-color 0.16s ease;
}

.level-card:hover:not(.is-locked) {
  border-color: var(--line-hot);
  transform: translateY(-2px);
}

.level-card.is-locked {
  opacity: 0.4;
  cursor: not-allowed;
}

.level-card .num {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.18em;
  color: var(--dim);
}

.level-card .name {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.02em;
}

.level-card .meta {
  margin-top: auto;
  font-size: 11px;
  color: var(--dim);
  font-family: var(--mono);
  letter-spacing: 0.04em;
}

.level-card .rank {
  position: absolute;
  top: 10px;
  right: 12px;
  font-weight: 800;
  font-size: 18px;
  letter-spacing: 0.02em;
}

.level-card .perfect {
  position: absolute;
  bottom: 12px;
  right: 12px;
  font-size: 13px;
  color: var(--gold);
  text-shadow: 0 0 12px rgba(255, 209, 102, 0.6);
}

.rank-S\+ { color: var(--gold); text-shadow: 0 0 14px rgba(255, 209, 102, 0.6); }
.rank-S { color: var(--cyan); text-shadow: 0 0 14px rgba(63, 240, 255, 0.5); }
.rank-A { color: var(--green); }
.rank-B { color: var(--amber); }
.rank-C { color: var(--dim); }

/* ── Settings rows ───────────────────────────────────────────────────────── */
.row {
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 54px;
  padding: 6px 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(10, 14, 21, 0.55);
  cursor: pointer;
}

.row > span:first-child {
  flex: 1;
  font-weight: 600;
}

.row input[type="checkbox"] {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.switch {
  width: 46px;
  height: 26px;
  border-radius: 999px;
  background: rgba(29, 43, 69, 0.9);
  border: 1px solid var(--line);
  position: relative;
  flex: none;
  transition: background 0.18s ease, border-color 0.18s ease;
}

.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--dim);
  transition: transform 0.18s ease, background 0.18s ease;
}

.row input:checked + .switch {
  background: rgba(63, 240, 255, 0.25);
  border-color: rgba(63, 240, 255, 0.6);
}

.row input:checked + .switch::after {
  transform: translateX(20px);
  background: var(--cyan);
  box-shadow: 0 0 12px var(--cyan);
}

.row-stack {
  flex-wrap: wrap;
}

.segmented {
  display: flex;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
}

.segmented button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--dim);
  font: inherit;
  font-size: 13px;
  padding: 8px 14px;
  min-height: 38px;
  cursor: pointer;
}

.segmented button.is-on {
  background: rgba(63, 240, 255, 0.16);
  color: var(--cyan);
}

.danger-zone {
  margin-top: 22px;
  padding: 16px;
  border: 1px dashed rgba(255, 59, 92, 0.3);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.danger-zone p {
  margin: 0;
  color: var(--dim);
  font-size: 13px;
}

/* ── Statistics ──────────────────────────────────────────────────────────── */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
}

.stat-tile {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(10, 14, 21, 0.55);
  padding: 14px;
}

.stat-tile b {
  display: block;
  font-size: 22px;
  font-family: var(--mono);
  letter-spacing: -0.01em;
}

.stat-tile span {
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--dim);
}

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

.achievement {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(10, 14, 21, 0.55);
  opacity: 0.45;
}

.achievement.is-earned {
  opacity: 1;
  border-color: rgba(255, 209, 102, 0.35);
}

.achievement .mark {
  width: 34px;
  height: 34px;
  border-radius: 9px;
  display: grid;
  place-items: center;
  background: rgba(29, 43, 69, 0.6);
  flex: none;
  font-size: 16px;
}

.achievement.is-earned .mark {
  background: rgba(255, 209, 102, 0.16);
  color: var(--gold);
}

.achievement b {
  display: block;
  font-size: 14px;
}

.achievement small {
  color: var(--dim);
  font-size: 12px;
}

/* ── Help ────────────────────────────────────────────────────────────────── */
.help {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 12px;
}

.help-card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(10, 14, 21, 0.55);
  padding: 16px;
}

.help-card.wide {
  grid-column: 1 / -1;
}

.help-card h3 {
  margin: 12px 0 6px;
  font-size: 15px;
  letter-spacing: 0.06em;
}

.help-card p {
  margin: 0;
  font-size: 13px;
  color: #aab8cc;
}

.help-card b {
  color: var(--text);
}

.help-card kbd {
  font-family: var(--mono);
  font-size: 11px;
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 1px 5px;
  background: rgba(29, 43, 69, 0.5);
}

/* Animated demonstrations — pure CSS so they cost nothing to run. */
.demo {
  position: relative;
  height: 74px;
  border-radius: 10px;
  background: #080b11;
  border: 1px solid var(--line);
  overflow: hidden;
}

.demo i {
  position: absolute;
  display: block;
}

.demo-player {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--cyan);
  box-shadow: 0 0 14px var(--cyan);
  top: 29px;
}

.demo-move .demo-player {
  animation: demo-run 3s ease-in-out infinite;
}

.demo-move .demo-wall {
  width: 14px;
  height: 44px;
  border-radius: 4px;
  background: var(--line);
  right: 34px;
  top: 15px;
}

@keyframes demo-run {
  0% { left: 14px; }
  35% { left: calc(100% - 90px); }
  50% { left: calc(100% - 90px); top: 29px; }
  75% { left: calc(100% - 90px); top: 6px; }
  100% { left: 14px; top: 29px; }
}

.demo-node {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #8fd7ff;
  box-shadow: 0 0 8px #8fd7ff;
  top: 34px;
  animation: demo-eat 2.4s linear infinite;
}

.demo-nodes .demo-node:nth-child(1) { left: 45%; animation-delay: 0.5s; }
.demo-nodes .demo-node:nth-child(2) { left: 62%; animation-delay: 0.9s; }
.demo-nodes .demo-node:nth-child(3) { left: 79%; animation-delay: 1.3s; }

.demo-nodes .demo-player {
  animation: demo-sweep 2.4s linear infinite;
}

@keyframes demo-sweep {
  0% { left: 10px; }
  100% { left: calc(100% - 26px); }
}

@keyframes demo-eat {
  0%, 60% { opacity: 1; transform: scale(1); }
  70% { opacity: 0; transform: scale(2.4); }
  100% { opacity: 0; }
}

.demo-drone {
  width: 16px;
  height: 16px;
  border-radius: 5px;
  top: 29px;
}

.demo-drone.d1 { background: var(--red); box-shadow: 0 0 12px var(--red); animation: demo-chase 2.6s ease-in-out infinite; }
.demo-drone.d2 { background: var(--amber); box-shadow: 0 0 12px var(--amber); animation: demo-chase 2.6s ease-in-out infinite 0.35s; }
.demo-drones .demo-player { animation: demo-flee 2.6s ease-in-out infinite; }

@keyframes demo-chase {
  0%, 100% { left: 14px; }
  50% { left: 55%; }
}

@keyframes demo-flee {
  0%, 100% { left: calc(100% - 30px); }
  50% { left: 72%; }
}

.demo-module {
  width: 20px;
  height: 20px;
  left: 26px;
  top: 27px;
  background: var(--amber);
  box-shadow: 0 0 18px var(--amber);
  clip-path: polygon(50% 0, 93% 25%, 93% 75%, 50% 100%, 7% 75%, 7% 25%);
  animation: demo-throb 1.4s ease-in-out infinite;
}

@keyframes demo-throb {
  0%, 100% { transform: scale(0.86); }
  50% { transform: scale(1.12); }
}

.demo-drone.scared {
  background: #4b7bff;
  box-shadow: 0 0 14px #4b7bff;
  animation: demo-scared 2.2s ease-in-out infinite;
}

@keyframes demo-scared {
  0%, 100% { left: 60%; }
  50% { left: calc(100% - 28px); }
}

.demo-block {
  width: 46px;
  height: 46px;
  left: calc(50% - 23px);
  top: 14px;
  border: 2px solid var(--line-hot);
  border-radius: 6px;
  border-top-color: var(--violet);
  animation: demo-rotate 2.8s cubic-bezier(0.7, 0, 0.3, 1) infinite;
}

@keyframes demo-rotate {
  0%, 30% { transform: rotate(0deg); }
  55%, 100% { transform: rotate(90deg); }
}

.demo-terminal {
  width: 14px;
  height: 14px;
  left: 22px;
  top: 30px;
  background: var(--violet);
  box-shadow: 0 0 16px var(--violet);
  transform: rotate(45deg);
  animation: demo-throb 1.1s ease-in-out infinite;
}

.demo-seal {
  width: 40px;
  height: 40px;
  left: 26px;
  top: 17px;
  border: 2px dashed rgba(255, 209, 102, 0.6);
  border-radius: 6px;
  animation: demo-breach 3s ease-in-out infinite;
}

@keyframes demo-breach {
  0%, 45% { opacity: 1; transform: scale(1); }
  60%, 100% { opacity: 0; transform: scale(1.35); }
}

.demo-bonus {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  left: 96px;
  top: 32px;
  background: var(--gold);
  box-shadow: 0 0 14px var(--gold);
  animation: demo-throb 1.3s ease-in-out infinite;
}

/* ── Game screen ─────────────────────────────────────────────────────────── */
.game {
  padding: calc(var(--safe-t) + 6px) calc(var(--safe-r) + 6px) calc(var(--safe-b) + 6px) calc(var(--safe-l) + 6px);
  gap: 6px;
}

.playfield {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 6px;
  align-items: center;
}

.board-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  width: 100%;
  display: flex;
}

#board {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

.board-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  padding-bottom: 8px;
  gap: 6px;
}

.combo-badge {
  font-family: var(--mono);
  font-size: 13px;
  letter-spacing: 0.14em;
  padding: 5px 12px;
  border-radius: 999px;
  background: rgba(75, 123, 255, 0.2);
  border: 1px solid rgba(223, 232, 255, 0.5);
  color: #dfe8ff;
  animation: combo-pop 0.3s ease;
}

@keyframes combo-pop {
  from { transform: scale(0.6); opacity: 0; }
}

.power-bar {
  width: min(210px, 60%);
  height: 5px;
  border-radius: 999px;
  background: rgba(255, 180, 77, 0.16);
  overflow: hidden;
}

.power-bar i {
  display: block;
  height: 100%;
  background: var(--amber);
  box-shadow: 0 0 12px var(--amber);
  transform-origin: left center;
}

/* HUD — a compact strip on phones, side columns on desktop. */
.hud {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  flex: none;
  padding: 4px 8px;
  overflow: hidden;
}

.hud-brand {
  display: none;
}

.readout {
  display: flex;
  flex-direction: column;
  line-height: 1.15;
  min-width: 0;
}

.readout span {
  font-size: 9px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--dim);
  white-space: nowrap;
}

.readout b {
  font-family: var(--mono);
  font-size: 16px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.readout.small b {
  font-size: 13px;
}

.hud-secondary {
  justify-content: flex-end;
  gap: 16px;
}

.hud-pause {
  order: 3;
  font-size: 12px;
  letter-spacing: 0.1em;
}

.shift-meter {
  position: relative;
  width: 46px;
  height: 46px;
  flex: none;
  display: grid;
  place-items: center;
}

.shift-meter svg {
  position: absolute;
  inset: 0;
  transform: rotate(-90deg);
}

.shift-meter circle {
  fill: none;
  stroke-width: 4;
}

.shift-meter .track {
  stroke: rgba(192, 123, 255, 0.16);
}

.shift-meter .fill {
  stroke: var(--violet);
  stroke-linecap: round;
  stroke-dasharray: 119.4;
  stroke-dashoffset: 119.4;
  transition: stroke-dashoffset 0.1s linear;
}

.shift-meter.is-ready .fill {
  stroke: #e0b3ff;
  filter: drop-shadow(0 0 6px rgba(224, 179, 255, 0.9));
}

.shift-meter.is-ready {
  animation: shift-ready 1.6s ease-in-out infinite;
}

@keyframes shift-ready {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.06); }
}

.shift-label {
  font-size: 8px;
  letter-spacing: 0.12em;
  color: var(--dim);
  font-weight: 700;
}

.shift-meter.is-ready .shift-label {
  color: #e0b3ff;
}

/* ── D-pad ───────────────────────────────────────────────────────────────── */
.dpad {
  flex: none;
  display: grid;
  grid-template-columns: repeat(3, 58px);
  grid-template-rows: repeat(2, 52px);
  gap: 6px;
  justify-content: center;
  align-self: center;
  padding-bottom: 4px;
}

.dpad[hidden] {
  display: none;
}

.dpad button {
  appearance: none;
  border: 1px solid var(--line);
  background: rgba(14, 20, 32, 0.75);
  border-radius: 12px;
  cursor: pointer;
  position: relative;
}

.dpad button::after {
  content: "";
  position: absolute;
  inset: 0;
  margin: auto;
  width: 0;
  height: 0;
  border: 9px solid transparent;
}

.dpad button.is-down {
  background: rgba(63, 240, 255, 0.2);
  border-color: rgba(63, 240, 255, 0.55);
}

.dpad [data-dir="u"] { grid-column: 2; grid-row: 1; }
.dpad [data-dir="l"] { grid-column: 1; grid-row: 2; }
.dpad [data-dir="d"] { grid-column: 2; grid-row: 2; }
.dpad [data-dir="r"] { grid-column: 3; grid-row: 2; }

.dpad [data-dir="u"]::after { border-bottom-color: var(--text); transform: translateY(-4px); }
.dpad [data-dir="d"]::after { border-top-color: var(--text); transform: translateY(4px); }
.dpad [data-dir="l"]::after { border-right-color: var(--text); transform: translateX(-4px); }
.dpad [data-dir="r"]::after { border-left-color: var(--text); transform: translateX(4px); }

/* ── Overlays ────────────────────────────────────────────────────────────── */
.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: calc(var(--safe-t) + 16px) 16px calc(var(--safe-b) + 16px);
  background: rgba(5, 7, 11, 0.82);
  backdrop-filter: blur(6px);
  z-index: 20;
  animation: screen-in 0.2s ease both;
}

.overlay[hidden] {
  display: none;
}

.card {
  width: min(420px, 100%);
  max-height: 100%;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: linear-gradient(170deg, rgba(29, 43, 69, 0.5), rgba(8, 11, 17, 0.95));
  padding: 26px 22px 22px;
  text-align: center;
  box-shadow: 0 30px 90px rgba(0, 0, 0, 0.6);
}

.card h2 {
  margin: 0;
  font-size: 22px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.card-sub {
  margin: 6px 0 18px;
  color: var(--dim);
  font-size: 13px;
  letter-spacing: 0.06em;
}

.card-actions {
  display: flex;
  flex-direction: column;
  gap: 9px;
  margin-top: 18px;
}

.rank-badge {
  width: 76px;
  height: 76px;
  margin: 0 auto 12px;
  border-radius: 22px;
  display: grid;
  place-items: center;
  font-size: 34px;
  font-weight: 800;
  border: 1px solid var(--line-hot);
  background: rgba(10, 14, 21, 0.8);
  animation: rank-in 0.5s cubic-bezier(0.2, 1.4, 0.4, 1) both;
}

@keyframes rank-in {
  from { transform: scale(0.4) rotate(-12deg); opacity: 0; }
}

.result-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 0;
  text-align: left;
}

.result-grid > div {
  border: 1px solid var(--line);
  border-radius: 11px;
  padding: 9px 12px;
  background: rgba(10, 14, 21, 0.6);
}

.result-grid dt {
  font-size: 9px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--dim);
}

.result-grid dd {
  margin: 2px 0 0;
  font-family: var(--mono);
  font-size: 16px;
  font-variant-numeric: tabular-nums;
}

.result-flags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  margin-top: 14px;
}

.flag {
  font-size: 11px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  padding: 5px 11px;
  border-radius: 999px;
  border: 1px solid currentColor;
}

.flag-gold { color: var(--gold); }
.flag-cyan { color: var(--cyan); }
.flag-green { color: var(--green); }

/* ── Toasts ──────────────────────────────────────────────────────────────── */
.toasts {
  position: absolute;
  top: calc(var(--safe-t) + 12px);
  left: 0;
  right: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
  z-index: 40;
}

.toast {
  border: 1px solid rgba(255, 209, 102, 0.4);
  background: rgba(10, 14, 21, 0.92);
  border-radius: 12px;
  padding: 10px 16px;
  display: flex;
  gap: 10px;
  align-items: center;
  animation: toast-in 0.3s ease both;
  max-width: min(360px, calc(100% - 32px));
}

.toast.is-out {
  animation: toast-out 0.3s ease both;
}

@keyframes toast-in {
  from { transform: translateY(-14px); opacity: 0; }
}

@keyframes toast-out {
  to { transform: translateY(-14px); opacity: 0; }
}

.toast b {
  font-size: 13px;
}

.toast small {
  display: block;
  color: var(--dim);
  font-size: 11px;
}

/*
 * Portrait phones: pin the board to a square so the leftover height is real
 * space the HUD and D-pad can occupy, instead of an oversized canvas with the
 * board floating in the middle of it and the controls pushed off screen.
 */
@media (orientation: portrait) and (max-width: 859px) {
  .board-wrap {
    flex: 0 1 auto;
    width: 100%;
    aspect-ratio: 1 / 1;
    max-height: 100%;
  }

  /* Board up top, controls down by the thumbs, slack in between. */
  .playfield {
    justify-content: flex-start;
  }

  .dpad {
    margin-top: auto;
  }
}

/* ── Desktop layout: side panels flanking a square board ─────────────────── */
@media (min-width: 860px) and (min-height: 560px) {
  .playfield {
    flex-direction: row;
    align-items: stretch;
    gap: 14px;
    padding: 6px;
  }

  .hud {
    flex-direction: column;
    align-items: flex-start;
    justify-content: flex-start;
    width: 190px;
    gap: 18px;
    padding: 16px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: linear-gradient(180deg, rgba(29, 43, 69, 0.24), rgba(10, 14, 21, 0.5));
  }

  .hud-brand {
    display: block;
    font-size: 13px;
    font-weight: 800;
    letter-spacing: 0.34em;
    color: var(--cyan);
    text-shadow: 0 0 18px rgba(63, 240, 255, 0.5);
  }

  .hud-secondary {
    align-items: flex-start;
  }

  .hud-pause {
    order: 0;
    width: 100%;
    height: 40px;
  }

  .readout b {
    font-size: 22px;
  }

  .readout.small b {
    font-size: 16px;
  }

  .shift-meter {
    width: 78px;
    height: 78px;
    align-self: center;
  }

  .shift-label {
    font-size: 10px;
  }

  .board-wrap {
    flex: 1;
  }

  .dpad {
    display: none;
  }
}

/* Very short viewports (landscape phones): shrink the chrome, keep the board. */
@media (max-height: 480px) {
  .hud {
    gap: 10px;
    padding: 2px 6px;
  }

  .readout b { font-size: 13px; }
  .readout span { font-size: 8px; }
  .shift-meter { width: 34px; height: 34px; }
  .dpad { grid-template-rows: repeat(2, 40px); grid-template-columns: repeat(3, 46px); }
}

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