:root {
  --paper: #F2E4CE;
  --ink: #1C2027;
  --taxi: #E8B21C;
  --red: #C93F2A;
  --brick: #A9532B;
  --park: #2F6E63;
  --steel: #6F7B8A;
  --cream: #F0F3EF;
  --hudh: clamp(88px, 17vh, 176px);
  --rail: clamp(8px, 2vw, 26px);
  --display: "Graduate", "Rockwell", "Courier New", serif;
  --text: "Archivo", "Helvetica Neue", Arial, sans-serif;
  --mono: "DM Mono", "Courier New", monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--text);
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

body { touch-action: none; }

#stage {
  position: fixed;
  inset: 0;
  overflow: hidden;
  background: var(--paper);
}

#gl {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  outline: none;
}

#grain {
  position: absolute;
  inset: 0;
  z-index: 3;
  pointer-events: none;
  opacity: 0.22;
  mix-blend-mode: multiply;
  background-image: radial-gradient(circle at 1px 1px, rgba(28, 32, 39, 0.34) 0.8px, rgba(28, 32, 39, 0) 1.35px);
  background-size: 4px 4px;
}

[hidden] { display: none !important; }

/* ---------- title card ---------- */

#title {
  position: absolute;
  inset: 0;
  z-index: 40;
  overflow: hidden;
  background: var(--paper);
  transition: opacity 0.32s ease, transform 0.55s cubic-bezier(0.55, 0.01, 0.19, 1);
  will-change: transform, opacity;
}

#title.title-out {
  opacity: 0;
  transform: translateY(-7%) scale(0.995);
  pointer-events: none;
}

.grid-slab {
  position: absolute;
  left: -28%;
  right: -28%;
  top: 28%;
  bottom: -16%;
  z-index: 0;
  opacity: 0.5;
  transform: perspective(560px) rotateX(61deg);
  transform-origin: 50% 100%;
  background-image:
    repeating-linear-gradient(90deg, rgba(28, 32, 39, 0) 0 52px, rgba(28, 32, 39, 0.26) 52px 54px),
    repeating-linear-gradient(0deg, rgba(28, 32, 39, 0) 0 42px, rgba(28, 32, 39, 0.26) 42px 44px);
  -webkit-mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 0), #000 26%, #000 100%);
  mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 0), #000 26%, #000 100%);
}

.poster-band {
  position: absolute;
  left: -3%;
  right: -3%;
  bottom: 0;
  height: 50%;
  z-index: 1;
  pointer-events: none;
}

.band-skyline {
  position: absolute;
  left: 0;
  bottom: 22%;
  width: 100%;
  height: auto;
  opacity: 0.92;
}

.band-city {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 57%;
  object-fit: cover;
  object-position: 50% 100%;
}

.band-bridge {
  position: absolute;
  right: 3%;
  bottom: 9%;
  width: min(38%, 520px);
  height: min(42vh, 340px);
  object-fit: contain;
  object-position: 100% 100%;
}

.title-copy {
  position: absolute;
  top: 3.5%;
  left: 50%;
  transform: translateX(-50%);
  z-index: 4;
  width: min(94vw, 1600px);
  display: flex;
  flex-direction: column;
  align-items: center;
  pointer-events: none;
}

.title-letters {
  display: block;
  height: min(40vh, 520px);
  width: auto;
  max-width: 92vw;
  filter: contrast(1.02);
}

.title-caption {
  margin: clamp(6px, 1.2vh, 14px) 0 0;
  font-family: var(--text);
  font-weight: 600;
  font-size: clamp(8px, 1.05vw, 15px);
  line-height: 1.35;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  text-align: center;
  width: max-content;
  max-width: 94vw;
  padding: 3px 8px;
  background: var(--paper);
}

@media (min-width: 700px) {
  .title-caption { white-space: nowrap; }
}

.title-badges {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  gap: clamp(4px, 1.1vw, 10px);
  margin-top: clamp(5px, 1.2vh, 12px);
  padding: 3px 7px;
  background: var(--paper);
}

.title-badges .badge {
  position: relative;
  margin: 0;
  width: clamp(36px, 4.6vw, 72px);
  flex: 0 0 auto;
}

.title-badges .badge img {
  display: block;
  width: 100%;
  height: auto;
}

.title-badges .badge:not(.is-live) img {
  opacity: 0.47;
  filter: grayscale(0.62) contrast(0.85);
}

.title-badges .badge.is-live figcaption {
  position: absolute;
  left: 50%;
  bottom: -12px;
  transform: translateX(-50%);
  font-family: var(--text);
  font-weight: 700;
  font-size: 7px;
  letter-spacing: 0.16em;
  color: var(--red);
}

.title-car {
  position: absolute;
  left: 51%;
  top: 59%;
  transform: translateX(-50%) rotate(-7deg);
  width: min(33vw, 560px);
  max-width: 62vw;
  height: auto;
  z-index: 3;
  pointer-events: none;
}

.start-btn {
  position: absolute;
  right: clamp(18px, 9vw, 150px);
  top: 56%;
  width: clamp(86px, 11vw, 180px);
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
  z-index: 5;
  pointer-events: auto;
  transition: transform 0.12s ease;
}

.start-btn img {
  display: block;
  width: 100%;
  height: auto;
  transform: rotate(8deg);
}

.start-btn:hover { transform: scale(1.035); }
.start-btn:active { transform: scale(0.965); }

.press-line {
  display: block;
  margin: 4px auto 0;
  width: max-content;
  max-width: 100%;
  white-space: nowrap;
  padding: 2px 5px;
  border: 1px solid var(--ink);
  background: var(--paper);
  font-family: var(--text);
  font-weight: 700;
  font-size: clamp(7px, 0.85vw, 11px);
  letter-spacing: 0.14em;
  color: var(--ink);
  text-align: center;
}

.title-best {
  position: absolute;
  top: 3.2%;
  right: 3.2%;
  margin: 0;
  z-index: 6;
  font-family: var(--mono);
  font-weight: 500;
  font-size: clamp(9px, 0.95vw, 14px);
  letter-spacing: 0.06em;
  color: var(--ink);
  text-align: right;
}

.title-foot {
  position: absolute;
  left: 50%;
  bottom: 2.2%;
  transform: translateX(-50%);
  margin: 0;
  z-index: 6;
  width: max-content;
  max-width: 92vw;
  font-family: var(--text);
  font-weight: 600;
  font-size: clamp(7px, 0.82vw, 11px);
  letter-spacing: 0.13em;
  text-transform: uppercase;
  text-align: center;
  color: var(--ink);
  opacity: 0.78;
}

#wipe {
  position: absolute;
  inset: 0;
  z-index: 50;
  background: var(--paper);
  transform: translateY(-102%);
  pointer-events: none;
  will-change: transform;
}

/* ---------- hud ---------- */

#hud {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 20;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.3s ease 0.12s;
}

#help {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  left: var(--rail);
  z-index: 21;
  max-width: min(56vw, 440px);
  padding: 4px 7px;
  border: 1px solid var(--ink);
  background: var(--paper);
  color: var(--ink);
  font-family: var(--text);
  font-weight: 600;
  font-size: clamp(7px, 0.7vw, 12px);
  line-height: 1.25;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.touch-help { display: none; }

#stage.is-touch .desk-help { display: none; }
#stage.is-touch .touch-help { display: inline; }

#stage.game-on #hud { opacity: 1; }

.hud-band {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: clamp(5px, 1vw, 15px);
  min-height: var(--hudh);
  padding: 6px var(--rail) calc(7px + env(safe-area-inset-bottom));
  background: var(--paper);
  border-top: 3px solid var(--taxi);
}

.plate {
  position: relative;
  height: calc(var(--hudh) - 18px);
  flex: 0 0 auto;
}

.plate > img {
  display: block;
  height: 100%;
  width: auto;
}

.plate-cover {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--paper);
  font-family: var(--mono);
  font-weight: 500;
  background: #2B2723;
  border-radius: 3px;
  overflow: hidden;
  line-height: 1;
}

.laps-cover {
  left: 6.2%;
  right: 35%;
  top: 26%;
  bottom: 16%;
  font-size: calc(var(--hudh) * 0.29);
  gap: 2px;
}

.laps-cover em {
  font-style: normal;
  font-size: 0.34em;
  opacity: 0.78;
  transform: translateY(3%);
}

.timer-cover {
  left: 6.4%;
  right: 4.4%;
  top: 24.5%;
  bottom: 19%;
  font-size: calc(var(--hudh) * 0.185);
  letter-spacing: 0.04em;
}

.speed-cover {
  left: 6%;
  right: 5.6%;
  top: 31%;
  bottom: 18%;
  background: var(--paper);
  display: block;
  padding: 4% 1%;
}

.speed-segs {
  display: flex;
  gap: 2.4%;
  width: 100%;
  height: 100%;
}

.speed-segs i {
  flex: 1 1 0;
  display: block;
  border: 1.5px solid var(--ink);
  background: transparent;
}

.speed-segs i.on { background: var(--taxi); }
.speed-segs i.hot { background: var(--red); }

.speed-num {
  position: absolute;
  right: 4.2%;
  top: 6%;
  font-family: var(--mono);
  font-weight: 500;
  font-size: calc(var(--hudh) * 0.11);
  letter-spacing: 0.04em;
  color: var(--ink);
  line-height: 1;
}

.route-strip {
  flex: 1 1 240px;
  min-width: 150px;
  max-width: 720px;
  height: calc(var(--hudh) - 24px);
}

.route-strip > img,
.route-strip > canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  object-fit: fill;
}

#boroughNow {
  position: absolute;
  left: 2.5%;
  top: 4%;
  z-index: 2;
  padding: 2px 5px;
  background: var(--paper);
  border: 1px solid var(--ink);
  color: var(--ink);
  font-family: var(--text);
  font-weight: 700;
  font-size: clamp(5px, 0.62vw, 8px);
  letter-spacing: 0.1em;
  white-space: nowrap;
}

.mini-badges {
  display: flex;
  align-items: center;
  gap: 2px;
  flex: 0 0 auto;
  height: clamp(34px, 5.2vh, 54px);
}

.mini-badges img {
  height: 100%;
  width: auto;
  display: block;
}

.mini-badges img.current {
  outline: 2px solid var(--red);
  outline-offset: 1px;
  border-radius: 50%;
}

#minimap {
  position: absolute;
  right: clamp(8px, 1.6vw, 24px);
  bottom: calc(var(--hudh) + 8px);
  width: clamp(92px, 12vw, 176px);
  aspect-ratio: 837 / 890;
  pointer-events: none;
  border: 2px solid var(--ink);
  background: var(--paper);
}

#minimap > img,
#minimap > canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

.map-label {
  position: absolute;
  left: 5%;
  bottom: 3.5%;
  font-family: var(--text);
  font-weight: 700;
  font-size: clamp(6px, 0.72vw, 8px);
  letter-spacing: 0.13em;
  color: var(--ink);
  background: var(--paper);
  padding: 1px 3px;
}

#toast {
  position: absolute;
  top: 13%;
  left: 50%;
  transform: translate(-50%, -8px);
  z-index: 30;
  padding: 7px 13px;
  background: var(--paper);
  border: 2px solid var(--ink);
  font-family: var(--text);
  font-weight: 700;
  font-size: clamp(9px, 1.05vw, 12px);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink);
  opacity: 0;
  transition: opacity 0.18s ease, transform 0.18s ease;
  pointer-events: none;
  white-space: nowrap;
}

#toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

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

#touch { display: none; }

#stage.is-touch #touch { display: block; }

.tbtn {
  position: absolute;
  z-index: 24;
  border: 2px solid var(--ink);
  background: var(--paper);
  color: var(--ink);
  font-family: var(--text);
  font-weight: 700;
  font-size: 10px;
  letter-spacing: 0.08em;
  border-radius: 50%;
  pointer-events: auto;
  touch-action: none;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tbtn:active,
.tbtn.down {
  background: var(--taxi);
}

.tleft, .tright {
  width: clamp(58px, 12vw, 84px);
  aspect-ratio: 1;
  bottom: calc(var(--hudh) + 14px + env(safe-area-inset-bottom));
}

.tleft { left: clamp(10px, 2.5vw, 26px); }
.tright { right: clamp(10px, 2.5vw, 26px); }

.tleft span, .tright span {
  width: 0;
  height: 0;
  border-top: 9px solid transparent;
  border-bottom: 9px solid transparent;
}

.tleft span { border-right: 14px solid var(--ink); }
.tright span { border-left: 14px solid var(--ink); }

.tbrake, .tdrift {
  width: clamp(52px, 9vw, 68px);
  aspect-ratio: 1;
  font-size: clamp(7px, 1.4vw, 9px);
}

.tbrake { right: clamp(16px, 3vw, 30px); bottom: calc(var(--hudh) + 90px + env(safe-area-inset-bottom)); }
.tdrift { left: clamp(16px, 3vw, 30px); bottom: calc(var(--hudh) + 90px + env(safe-area-inset-bottom)); }

/* ---------- finish / pause ---------- */

#finish, #pause {
  position: absolute;
  inset: 0;
  z-index: 45;
  background: rgba(242, 228, 206, 0.985);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(12px, 3vw, 36px);
  overflow: auto;
}

.finish-card {
  position: relative;
  width: min(760px, 94vw);
  background: var(--paper);
  border: 3px solid var(--ink);
  padding: clamp(18px, 3.4vw, 42px) clamp(16px, 3.4vw, 44px) clamp(14px, 2.6vw, 30px);
  max-height: 94vh;
  overflow: auto;
}

.finish-card::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  height: 7px;
  background: repeating-linear-gradient(90deg, var(--taxi) 0 14px, var(--ink) 14px 28px);
}

.finish-kicker {
  margin: 0 0 4px;
  font-family: var(--text);
  font-weight: 700;
  font-size: clamp(8px, 1vw, 11px);
  letter-spacing: 0.16em;
  color: var(--red);
}

.finish-card h2 {
  margin: 0;
  font-family: var(--display);
  font-weight: 400;
  font-size: clamp(36px, 6vw, 64px);
  line-height: 0.9;
  letter-spacing: 0.01em;
}

.finish-rule {
  height: 5px;
  background: var(--taxi);
  margin: clamp(12px, 2vh, 22px) 0 clamp(10px, 1.8vh, 18px);
}

.finish-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: clamp(8px, 1.6vw, 22px);
}

.finish-grid .label,
.finish-board .label {
  display: block;
  font-family: var(--text);
  font-weight: 700;
  font-size: clamp(7px, 0.82vw, 11px);
  letter-spacing: 0.13em;
  color: var(--ink);
  opacity: 0.72;
  margin-bottom: 3px;
}

.finish-grid b {
  font-family: var(--mono);
  font-weight: 500;
  font-size: clamp(19px, 2.4vw, 28px);
  line-height: 1;
}

.finish-board {
  margin-top: clamp(12px, 2vh, 22px);
  border-top: 1px solid rgba(28, 32, 39, 0.3);
  padding-top: clamp(8px, 1.4vh, 14px);
}

#boardList {
  list-style: none;
  margin: 4px 0 0;
  padding: 0;
  font-family: var(--mono);
  font-size: clamp(10px, 1.05vw, 13px);
}

#boardList li {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 2px 0;
  border-bottom: 1px dotted rgba(28, 32, 39, 0.28);
}

#boardList li span:first-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#boardList a { color: var(--ink); }

#boardList .board-empty { border-bottom: 0; opacity: 0.65; }

.finish-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: clamp(14px, 2.2vh, 24px);
}

.ink-btn {
  font-family: var(--text);
  font-weight: 700;
  font-size: clamp(9px, 0.95vw, 11px);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--paper);
  background: var(--ink);
  border: 2px solid var(--ink);
  padding: 10px 16px;
  cursor: pointer;
  pointer-events: auto;
}

.ink-btn:hover { background: var(--red); border-color: var(--red); }
.ink-btn.ghost { color: var(--ink); background: transparent; }
.ink-btn.ghost:hover { background: var(--taxi); border-color: var(--ink); }
.ink-btn.small { padding: 6px 10px; font-size: clamp(7px, 0.78vw, 9px); margin-top: 8px; }

.finish-hint {
  margin: clamp(10px, 1.8vh, 18px) 0 0;
  font-size: clamp(7px, 0.8vw, 9px);
  line-height: 1.45;
  letter-spacing: 0.05em;
  opacity: 0.62;
}

.pause-card {
  width: min(420px, 90vw);
  background: var(--paper);
  border: 3px solid var(--ink);
  padding: 26px;
  text-align: center;
}

.pause-card h2 {
  margin: 0 0 8px;
  font-family: var(--display);
  font-weight: 400;
  font-size: clamp(30px, 5vw, 46px);
}

.pause-card p {
  margin: 0 0 18px;
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

/* ---------- responsive ---------- */

@media (max-width: 900px), (orientation: portrait) {
  .route-strip,
  .mini-badges {
    display: none;
  }

  .hud-band {
    justify-content: space-between;
  }

  #minimap {
    width: clamp(86px, 21vw, 128px);
  }

  .title-car {
    top: 57%;
    left: 43%;
    width: min(58vw, 360px);
    max-width: 64vw;
    transform: translateX(-50%) rotate(-6deg);
  }

  .start-btn {
    right: 3.5%;
    top: 57%;
    width: clamp(80px, 21vw, 118px);
  }

  .title-best {
    top: auto;
    bottom: 2.4%;
    right: 3%;
  }

  .title-foot { display: none; }
}

@media (orientation: portrait) {
  :root { --hudh: clamp(86px, 13vh, 150px); }

  .plate { height: calc(var(--hudh) - 24px); }
}

@media (max-width: 560px) and (orientation: portrait) {
  :root { --hudh: 94px; }

  .plate { height: 62px; }

  .hud-band { gap: 5px; padding-left: 8px; padding-right: 8px; }

  .title-car {
    top: 58%;
    width: 58vw;
    max-width: 62vw;
  }

  .title-best { font-size: 9px; }
}

@media (max-width: 380px) and (orientation: portrait) {
  :root { --hudh: 88px; }
  .plate { height: 56px; }
  .hud-band { gap: 4px; padding-left: 6px; padding-right: 6px; }
}

#stage.is-touch #minimap {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  bottom: auto;
  right: max(10px, env(safe-area-inset-right));
  width: clamp(78px, 20vw, 118px);
}

#stage.is-touch #help { max-width: calc(100vw - 150px); }

#boroughRail {
  display: none;
  position: fixed;
  top: max(34px, calc(env(safe-area-inset-top) + 24px));
  right: calc(max(10px, env(safe-area-inset-right)) + clamp(78px, 20vw, 118px) + 12px);
  z-index: 22;
  width: clamp(96px, 28vw, 148px);
  flex-wrap: wrap;
  justify-content: center;
  align-items: flex-start;
  gap: 2px;
  padding: 3px 4px;
  background: var(--paper);
  border: 1px solid var(--ink);
}

@media (max-width: 900px), (orientation: portrait) {
  #boroughRail { display: flex; }
}

#boroughRail img {
  width: calc((100% - 8px) / 5);
  height: auto;
  display: block;
}

#boroughRail figure { margin: 0; width: calc((100% - 8px) / 5); position: relative; }
#boroughRail figure img { width: 100%; }
#boroughRail figure figcaption {
  display: none;
  position: absolute;
  left: 50%;
  bottom: -6px;
  transform: translateX(-50%);
  font-family: var(--text);
  font-weight: 700;
  font-size: 4.5px;
  letter-spacing: 0.12em;
  color: var(--red);
}

#boroughRail figure.current figcaption { display: block; }
#boroughRail figure.current img { outline: 2px solid var(--red); outline-offset: 1px; border-radius: 50%; }
#boroughRail .rail-note {
  flex-basis: 100%;
  margin: 2px 0 0;
  text-align: center;
  font-family: var(--text);
  font-weight: 700;
  font-size: clamp(4.5px, 0.7vw, 6.5px);
  letter-spacing: 0.1em;
  color: var(--ink);
}

@media (max-height: 520px) {
  :root { --hudh: 76px; }

  .title-copy { top: 2%; }
  .title-letters { height: min(31vh, 150px); }
  .title-caption { margin-top: 3px; font-size: 6.5px; letter-spacing: 0.07em; }
  .title-badges { margin-top: 3px; padding: 2px 5px; }
  .title-badges .badge { width: 28px; }
  .title-car { top: 49%; width: min(27vw, 230px); }
  .start-btn { top: 46%; width: 66px; }
  .title-best { font-size: 7px; }
  .press-line { font-size: 6px; padding: 1px 3px; }
}

@media (pointer: coarse) {
  #stage.is-touch #touch { display: block; }
}
