/* Brand typeface: DejaVu Sans Mono, subset to the characters CharCrash uses
   (full fonts and licence in brand/fonts). */
@font-face {
  font-family: "DejaVu Sans Mono";
  src: url("fonts/DejaVuSansMono.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "DejaVu Sans Mono";
  src: url("fonts/DejaVuSansMono-Bold.woff2") format("woff2");
  font-weight: 700;
  font-style: normal;
  font-display: swap;
}

:root {
  --bg: #0b0e14;
  --panel: #131823;
  --text: #d7dce5;
  --muted: #7d8697;
  --accent: #ffd54a;
  --danger: #ff6b3d;
  --border: #2a3242;
  --accent-text: #ffd54a;
  --accent-glow: rgba(255, 213, 74, 0.45);
  --overlay-bg: rgba(11, 14, 20, 0.85);
  --ctrl-active: #222a3a;
  --backdrop: rgba(0, 0, 0, 0.6);
  --success: #3fb950;
  color-scheme: dark;
  --ad-h: 50px; /* ad slot height; the game shrinks to make room for it */
  --on-accent: #1a1400; /* text on Flag Yellow fills */
  --mono: "DejaVu Sans Mono", Menlo, Consolas, "Liberation Mono", "Courier New", monospace;
}

/* Light theme: used when chosen, or when the OS prefers light and nothing is chosen */
:root[data-theme="light"] {
  --bg: #f3efe6;
  --panel: #fbf9f4;
  --text: #1f2430;
  --muted: #626875;
  --accent: #ffc928;
  --danger: #c2410c;
  --border: #d6d0c2;
  --accent-text: #955b06;
  --accent-glow: rgba(217, 154, 0, 0.25);
  --overlay-bg: rgba(251, 249, 244, 0.9);
  --ctrl-active: #efe8d6;
  --backdrop: rgba(40, 32, 20, 0.35);
  --success: #1a7f37;
  color-scheme: light;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f3efe6;
    --panel: #fbf9f4;
    --text: #1f2430;
    --muted: #626875;
    --accent: #ffc928;
    --danger: #c2410c;
    --border: #d6d0c2;
    --accent-text: #955b06;
    --accent-glow: rgba(217, 154, 0, 0.25);
    --overlay-bg: rgba(251, 249, 244, 0.9);
    --ctrl-active: #efe8d6;
    --backdrop: rgba(40, 32, 20, 0.35);
    --success: #1a7f37;
    color-scheme: light;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--mono);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

.app {
  /* full window minus the sticky ad bar (slot + 12px padding + 1px border + iPhone home bar) */
  height: calc(100dvh - var(--ad-h) - 13px - env(safe-area-inset-bottom));
  max-width: 640px;
  margin: 0 auto;
  padding: 12px 16px 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  user-select: none;
  -webkit-user-select: none;
}

.topbar {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}
.topbar-logo { display: block; line-height: 0; }
.topbar-logo img { height: 28px; width: auto; display: block; }
.topbar-logo:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
/* Show the lockup that matches the ground: chosen theme first, else the OS */
.topbar-logo .on-dark { display: none; }
:root[data-theme="dark"] .topbar-logo .on-light { display: none; }
:root[data-theme="dark"] .topbar-logo .on-dark { display: block; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .topbar-logo .on-light { display: none; }
  :root:not([data-theme="light"]) .topbar-logo .on-dark { display: block; }
}
.topbar-actions { display: flex; gap: 8px; }
.icon-btn {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
}
.icon-btn:hover { border-color: var(--muted); }
.icon-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.icon-glyph { font-size: 18px; font-weight: 700; line-height: 1; }

.hud {
  display: flex;
  gap: 18px;
  font-size: 0.85rem;
  color: var(--muted);
}
.hud b { color: var(--text); font-variant-numeric: tabular-nums; }

.board-wrap {
  flex: 1;
  min-height: 0;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.board { position: relative; }
canvas { display: block; }

.board.shake { animation: shake 450ms cubic-bezier(.36,.07,.19,.97); }
@media (prefers-reduced-motion: reduce) { .board.shake { animation: none; } }
@keyframes shake {
  10%, 90% { transform: translate(-1px, 1px); }
  20%, 80% { transform: translate(3px, -2px); }
  30%, 50%, 70% { transform: translate(-5px, 2px); }
  40%, 60% { transform: translate(5px, -1px); }
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  text-align: center;
  pointer-events: none;
}
.overlay > * { pointer-events: auto; }
.overlay[hidden] { display: none; }
.overlay-big {
  font-size: clamp(2rem, 9vw, 3.5rem);
  font-weight: 700;
  color: var(--accent-text);
  text-shadow: 0 0 18px var(--accent-glow);
}
.overlay-small {
  font-size: 0.85rem;
  color: var(--text);
  background: var(--overlay-bg);
  padding: 4px 10px;
  border-radius: 6px;
  max-width: 90%;
}
.overlay-big:empty, .overlay-small:empty { display: none; }

.btn {
  font: inherit;
  font-size: 1rem;
  padding: 10px 22px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
}
.btn:hover { border-color: var(--muted); }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
  font-weight: 700;
}
.btn-primary:hover { filter: brightness(1.08); border-color: var(--accent); }
.btn[hidden] { display: none; }

.controls {
  display: none;
  width: 100%;
  gap: 12px;
}
.ctrl {
  flex: 1;
  height: 72px;
  font-size: 2rem;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  touch-action: none;
  cursor: pointer;
}
.ctrl.active { background: var(--ctrl-active); border-color: var(--accent); color: var(--accent-text); }

.hint { margin: 0; font-size: 0.75rem; color: var(--muted); }
kbd {
  font-family: var(--mono);
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 0 5px;
  color: var(--text);
}

/* Touch devices: show steering buttons, hide keyboard hint */
/* Pause button: touch devices only (keyboards have Space and P) */
.pause-btn { display: none; }
.pause-btn:disabled { opacity: 0.4; cursor: default; }
.pause-btn:disabled:hover { border-color: var(--border); }

@media (pointer: coarse) {
  .controls { display: flex; }
  .hint { display: none; }
  .pause-btn { display: grid; }
}

/* ---------- Modal ---------- */
.modal {
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--panel);
  color: var(--text);
  padding: 0;
  width: min(360px, calc(100vw - 32px));
  font-family: var(--mono);
}
.modal::backdrop { background: var(--backdrop); }
.modal-body { position: relative; padding: 22px 20px 18px; text-align: center; }
.modal-close { position: absolute; top: 10px; right: 10px; width: 32px; height: 32px; }
.modal-close .icon-glyph { font-size: 20px; }
.modal-title { font-size: 1.1rem; font-weight: 700; color: var(--danger); letter-spacing: 0.08em; text-transform: uppercase; }
.modal-score-label { margin-top: 14px; font-size: 0.75rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.1em; }
.modal-score { font-size: 3.5rem; font-weight: 700; color: var(--accent-text); line-height: 1.1; font-variant-numeric: tabular-nums; }
.badge { margin-top: 4px; font-size: 0.85rem; color: var(--accent-text); }
.badge[hidden] { display: none; }

.stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 18px 0 0;
}
.stats > div {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 6px;
}
.stats dt { font-size: 0.7rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; }
.stats dd { margin: 2px 0 0; font-size: 1.15rem; font-weight: 700; font-variant-numeric: tabular-nums; }

.recent { margin-top: 12px; }
.recent-label { font-size: 0.7rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; }
.recent-list { margin-top: 4px; font-size: 0.85rem; color: var(--text); word-spacing: 4px; }

.modal-actions { display: flex; gap: 10px; margin-top: 18px; }
.modal-actions .btn { flex: 1; }
.btn.copied,
.btn.copied:hover { border-color: var(--success); color: var(--success); }

.share-status { min-height: 1.2em; margin-top: 8px; font-size: 0.8rem; color: var(--muted); white-space: pre-line; overflow-wrap: anywhere; }

/* ---------- Settings ---------- */
.settings-title { color: var(--text); }
.setting {
  border: 0;
  margin: 18px 0 0;
  padding: 0;
  text-align: left;
}
.setting legend {
  padding: 0;
  margin-bottom: 8px;
  font-size: 0.7rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

.car-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 6px;
}
.car-option, .segmented label { position: relative; cursor: pointer; }
.car-option input, .segmented input {
  position: absolute;
  opacity: 0;
  inset: 0;
  margin: 0;
  cursor: pointer;
}
.car-option span {
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  font-size: 1.5rem;
  font-weight: 700;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}
.car-option:hover span { border-color: var(--muted); }
.car-option input:checked + span {
  border-color: var(--accent);
  color: var(--accent-text);
  box-shadow: inset 0 0 0 1px var(--accent);
}
.car-option input:focus-visible + span,
.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.car-name { margin-top: 8px; font-size: 0.8rem; color: var(--muted); text-align: center; min-height: 1.2em; }

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.segmented span {
  display: block;
  padding: 9px 6px;
  text-align: center;
  font-size: 0.9rem;
  background: var(--bg);
  color: var(--muted);
}
.segmented label + label span { border-left: 1px solid var(--border); }
.segmented input:checked + span { background: var(--accent); color: var(--on-accent); font-weight: 700; }

/* ---------- Footer ---------- */
.footer {
  font-size: 0.72rem;
  color: var(--muted);
  user-select: text;
  -webkit-user-select: text;
}
.footer a { color: var(--text); text-decoration: underline; text-underline-offset: 2px; }
.footer a:hover { color: var(--accent-text); }

/* ---------- Sticky ad bar ---------- */
.ad-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  display: flex;
  justify-content: center;
  padding: 6px 0 calc(6px + env(safe-area-inset-bottom));
  background: var(--panel);
  border-top: 1px solid var(--border);
}
.ad-slot {
  width: 320px;
  max-width: 100%;
  height: var(--ad-h);
  display: grid;
  place-items: center;
  overflow: hidden;
}
.ad-placeholder {
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  border: 1px dashed var(--border);
  border-radius: 4px;
  font-size: 0.7rem;
  color: var(--muted);
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

/* Leaderboard size on screens with room for it */
@media (min-width: 768px) and (min-height: 760px) {
  :root { --ad-h: 90px; }
  .ad-slot { width: 728px; }
}

/* ---------- How to play ---------- */
.help { width: min(400px, calc(100vw - 32px)); }
.help-title { color: var(--text); }
.help-list {
  margin: 16px 0 0;
  padding-left: 1.4em;
  text-align: left;
  font-size: 0.85rem;
  line-height: 1.5;
}
.help-list li + li { margin-top: 10px; }
.help-list b { color: var(--text); }
.help-car { color: var(--accent-text); font-weight: 700; }
.help-wall { color: var(--muted); }
.help-extra { margin: 16px 0 0; font-size: 0.75rem; color: var(--muted); line-height: 1.7; }

/* Show keyboard or touch instructions to match the device */
.help-touch { display: none; }
@media (pointer: coarse) {
  .help-keys { display: none; }
  .help-touch { display: inline; }
}
