:root {
  color-scheme: dark;
  --ink: #f6f1ff;
  --muted: #9a94b5;
  --panel: rgba(17, 14, 37, 0.94);
  --line: rgba(177, 163, 231, 0.22);
  --cyan: #66efff;
  --magenta: #ff67c7;
  --lime: #c9ff80;
  --danger: #ff6f91;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  overflow-x: hidden;
  background:
    radial-gradient(circle at 50% -10%, rgba(87, 51, 155, 0.26), transparent 45%),
    linear-gradient(180deg, #0c0a1d 0%, #070712 100%);
  color: var(--ink);
}

button, select { font: inherit; }
button { color: inherit; }
button:not(:disabled), select:not(:disabled) { cursor: pointer; }

#game-shell {
  width: min(1240px, 100%);
  min-height: 100svh;
  margin: 0 auto;
  padding: max(10px, env(safe-area-inset-top)) clamp(10px, 2vw, 28px) max(12px, env(safe-area-inset-bottom));
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto auto;
  gap: 10px;
}

.topbar {
  min-height: 50px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  border-bottom: 1px solid var(--line);
}

.brand { display: inline-flex; align-items: center; gap: 9px; width: max-content; color: inherit; text-decoration: none; }
.brand img { width: 34px; height: 34px; }
.brand span { display: flex; align-items: baseline; gap: 5px; letter-spacing: .1em; }
.brand b { font-size: 15px; }
.brand small { color: var(--cyan); font-size: 10px; letter-spacing: .18em; }

.top-status { display: flex; align-items: center; gap: 9px; font-variant-numeric: tabular-nums; }
.cash-label { color: var(--muted); font-size: 9px; letter-spacing: .18em; }
#cash { font-size: 17px; min-width: 50px; }
.mode-badge { padding: 5px 9px; border: 1px solid rgba(102, 239, 255, .35); border-radius: 999px; color: var(--cyan); font-size: 9px; font-weight: 800; letter-spacing: .14em; }
.mode-badge.live { border-color: rgba(255, 103, 199, .5); color: var(--magenta); animation: pulse-badge 1.2s ease-in-out infinite; }

.text-icon { justify-self: end; border: 0; background: transparent; color: var(--muted); font-size: 10px; font-weight: 800; letter-spacing: .12em; padding: 10px 0 10px 10px; }
.text-icon:hover { color: var(--ink); }

#play-area { min-height: 0; display: flex; flex-direction: column; justify-content: center; }
.arena-heading { display: flex; align-items: end; justify-content: space-between; gap: 20px; padding: 0 3px 7px; }
.arena-heading div { display: flex; align-items: baseline; gap: 8px; }
.arena-heading span { color: var(--magenta); font-size: 8px; letter-spacing: .18em; font-weight: 800; }
.arena-heading strong { font-size: 12px; letter-spacing: .12em; }
.arena-heading p { margin: 0; color: var(--muted); font-size: 11px; }

#canvas-frame {
  width: min(100%, calc((100svh - 370px) * 10 / 7));
  position: relative;
  aspect-ratio: 10 / 7;
  margin: auto;
  overflow: hidden;
  border: 1px solid rgba(155, 135, 226, 0.32);
  border-radius: 15px;
  background: #080817;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .35), inset 0 0 60px rgba(83, 52, 140, .08);
}

#arena {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
}
#arena.selecting { cursor: default; }

.toast {
  position: absolute;
  left: 50%;
  top: 15px;
  translate: -50% -14px;
  opacity: 0;
  padding: 7px 12px;
  border: 1px solid rgba(255, 111, 145, .5);
  border-radius: 999px;
  background: rgba(17, 12, 32, .9);
  color: #ffb0c1;
  font-size: 10px;
  font-weight: 900;
  letter-spacing: .13em;
  pointer-events: none;
  transition: .18s ease;
}
.toast.show { opacity: 1; translate: -50% 0; }
.toast.good { color: var(--lime); border-color: rgba(201, 255, 128, .45); }

.pause-card { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(6, 5, 16, .5); backdrop-filter: blur(2px); color: var(--cyan); font-size: 13px; font-weight: 900; letter-spacing: .32em; }
.pause-card[hidden] { display: none; }

.launch-dock {
  width: min(100%, calc((100svh - 370px) * 10 / 7));
  min-height: 46px;
  margin: 7px auto 0;
  display: flex;
  gap: 7px;
  overflow-x: auto;
  scrollbar-width: thin;
}
.launch-dock[hidden] { display: none; }
.launch-racer {
  flex: 1 0 138px;
  min-height: 44px;
  border: 1px solid color-mix(in srgb, var(--racer-color) 60%, transparent);
  border-radius: 10px;
  background: linear-gradient(135deg, color-mix(in srgb, var(--racer-color) 22%, #111029), rgba(18, 14, 40, .96));
  box-shadow: inset 3px 0 0 var(--racer-color), 0 0 18px color-mix(in srgb, var(--racer-color) 12%, transparent);
  font-size: 12px;
  font-weight: 1000;
  letter-spacing: .12em;
}
.launch-racer small { display: block; margin-bottom: 2px; color: var(--racer-color); font-size: 7px; letter-spacing: .16em; }

.route-panel {
  min-height: 50px;
  display: grid;
  grid-template-columns: auto minmax(130px, 1fr) auto auto auto;
  align-items: center;
  gap: 10px;
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(17, 14, 37, .76);
}
.route-panel[hidden] { display: none; }
.route-identity { display: flex; gap: 9px; align-items: center; min-width: 102px; }
.route-identity i { width: 5px; height: 34px; border-radius: 5px; background: var(--cyan); box-shadow: 0 0 12px currentColor; }
.route-identity div, .route-readout, .route-racer { display: flex; flex-direction: column; }
.route-panel small { color: var(--muted); font-size: 8px; letter-spacing: .13em; }
.route-panel strong { font-size: 11px; letter-spacing: .1em; }
.route-readout { min-width: 72px; }

.compact { border: 1px solid var(--line); border-radius: 8px; padding: 9px 12px; background: #18132e; font-size: 9px; font-weight: 900; letter-spacing: .1em; }
.compact.primary { border-color: rgba(102, 239, 255, .45); color: var(--cyan); }
.compact.danger { color: #ff91aa; }
.compact:disabled { opacity: .35; cursor: not-allowed; }

.racer-bay {
  min-width: 0;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: stretch;
  gap: 8px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(12, 10, 29, .74);
}
.racer-bay-label { min-width: 112px; display: flex; flex-direction: column; justify-content: center; padding: 0 7px; }
.racer-bay-label strong { font-size: 10px; letter-spacing: .18em; }
.racer-bay-label small { max-width: 105px; margin-top: 3px; color: var(--muted); font-size: 7px; line-height: 1.3; letter-spacing: .08em; }
.racer-list { min-width: 0; display: flex; gap: 6px; overflow-x: auto; scrollbar-width: thin; }
.racer-card {
  --racer-color: var(--cyan);
  flex: 0 0 138px;
  min-height: 52px;
  display: grid;
  grid-template-columns: 29px 1fr;
  grid-template-rows: 1fr 1fr;
  align-items: center;
  column-gap: 7px;
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 6px 8px;
  background: rgba(22, 18, 45, .9);
  text-align: left;
}
.racer-card:hover:not(:disabled), .racer-card.selected { border-color: var(--racer-color); box-shadow: 0 0 16px color-mix(in srgb, var(--racer-color) 18%, transparent); }
.racer-card.selected { background: color-mix(in srgb, var(--racer-color) 12%, #15112e); }
.racer-card:disabled { cursor: default; }
.racer-number { grid-row: 1 / 3; display: grid; place-items: center; width: 28px; height: 28px; border: 1px solid var(--racer-color); border-radius: 50%; color: var(--racer-color); font-size: 9px; font-weight: 1000; }
.racer-type { align-self: end; font-size: 9px; font-weight: 900; letter-spacing: .1em; }
.racer-state { align-self: start; color: var(--muted); font-size: 7px; font-weight: 800; letter-spacing: .08em; }
.racer-card.live .racer-state { color: var(--lime); }
.racer-card.ready-line .racer-state { color: var(--racer-color); }

.toolbar { display: grid; grid-template-columns: repeat(4, minmax(64px, 1fr)) minmax(180px, 1.6fr); gap: 7px; }
.tool, .go-button {
  min-height: 54px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: rgba(20, 16, 43, .9);
  font-size: 9px;
  font-weight: 900;
  letter-spacing: .12em;
}
.tool { display: flex; align-items: center; justify-content: center; gap: 6px; color: var(--muted); }
.tool span { color: #d8cdfc; font-size: 16px; font-weight: 400; }
.tool:hover, .tool.active { color: var(--ink); border-color: rgba(102, 239, 255, .48); background: rgba(23, 30, 57, .94); }
.go-button { display: flex; flex-direction: column; justify-content: center; align-items: center; border-color: rgba(255, 103, 199, .55); background: linear-gradient(135deg, #f851b3, #aa4eff); color: white; font-size: 13px; box-shadow: 0 0 22px rgba(236, 75, 190, .16); }
.go-button small { font-size: 7px; letter-spacing: .15em; opacity: .78; }
.go-button.stop { border-color: rgba(255, 129, 129, .65); background: linear-gradient(135deg, #e24772, #8f315d); box-shadow: 0 0 22px rgba(226, 71, 114, .2); }
.go-button:disabled { filter: saturate(.1); opacity: .35; box-shadow: none; cursor: not-allowed; }

.modal { position: fixed; inset: 0; z-index: 10; display: grid; place-items: center; padding: 18px; background: rgba(5, 4, 14, .78); backdrop-filter: blur(8px); }
.modal[hidden] { display: none; }
.modal-card { width: min(680px, 100%); max-height: min(760px, 88svh); overflow: auto; border: 1px solid rgba(155, 135, 226, .35); border-radius: 18px; padding: 22px; background: linear-gradient(160deg, rgba(25, 20, 53, .99), rgba(11, 10, 27, .99)); box-shadow: 0 28px 90px rgba(0, 0, 0, .55); }
.modal-card header { display: flex; justify-content: space-between; align-items: start; }
.eyebrow { margin: 0 0 5px; color: var(--cyan); font-size: 9px; font-weight: 900; letter-spacing: .2em; }
.modal-card h2 { margin: 0; font-size: clamp(25px, 5vw, 40px); letter-spacing: .08em; }
.close-modal { border: 0; background: transparent; color: var(--muted); font-size: 32px; line-height: 1; }
.modal-intro { margin: 12px 0 18px; color: var(--muted); font-size: 12px; line-height: 1.55; }
.shop-list { display: grid; gap: 8px; }
.shop-item { display: grid; grid-template-columns: 1fr auto auto; gap: 14px; align-items: center; padding: 12px; border: 1px solid var(--line); border-radius: 12px; background: rgba(8, 7, 23, .55); }
.shop-item h3 { margin: 0 0 3px; font-size: 13px; letter-spacing: .11em; }
.shop-item p { margin: 0; color: var(--muted); font-size: 10px; }
.shop-item .owned { color: var(--cyan); font-size: 9px; font-weight: 900; letter-spacing: .1em; white-space: nowrap; }
.shop-item button { min-width: 90px; border: 1px solid rgba(201, 255, 128, .4); border-radius: 8px; padding: 9px; background: rgba(52, 73, 49, .3); color: var(--lime); font-size: 9px; font-weight: 900; letter-spacing: .08em; }
.shop-item button:disabled { opacity: .36; cursor: not-allowed; }
.arena-list .shop-item { grid-template-columns: 48px 1fr auto; }
.arena-preview { aspect-ratio: 1; border: 1px solid var(--line); border-radius: 8px; display: grid; place-items: center; color: var(--magenta); background: repeating-linear-gradient(45deg, transparent 0 6px, rgba(111, 94, 162, .1) 6px 7px); font-size: 18px; }

.summary-card { width: min(520px, 100%); text-align: center; }
.summary-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 24px 0; }
.summary-grid div { padding: 16px 6px; border: 1px solid var(--line); border-radius: 10px; background: rgba(7, 7, 20, .48); }
.summary-grid strong, .summary-grid small { display: block; }
.summary-grid strong { color: var(--lime); font-size: 22px; }
.summary-grid small { margin-top: 4px; color: var(--muted); font-size: 7px; letter-spacing: .12em; }
.wide-action { width: 100%; border: 0; border-radius: 10px; padding: 14px; background: linear-gradient(135deg, #45cfe0, #687cf6); color: #080817; font-weight: 1000; letter-spacing: .13em; }

@keyframes pulse-badge { 50% { opacity: .65; } }

@media (max-width: 720px) {
  #game-shell { grid-template-rows: auto minmax(0, 1fr) auto auto auto; gap: 7px; padding-inline: 7px; }
  .topbar { min-height: 44px; }
  .brand img { width: 29px; height: 29px; }
  .brand b { font-size: 12px; }
  .brand small, .text-icon { font-size: 8px; }
  .cash-label { display: none; }
  #cash { min-width: auto; font-size: 14px; }
  .mode-badge { font-size: 7px; padding: 4px 6px; }
  .arena-heading p { max-width: 55%; text-align: right; font-size: 9px; }
  #canvas-frame { width: min(100%, calc((100svh - 370px) * 10 / 7)); border-radius: 10px; }
  .launch-dock { width: min(100%, calc((100svh - 370px) * 10 / 7)); min-height: 42px; }
  .launch-racer { min-height: 40px; }
  .route-panel { grid-template-columns: auto minmax(0, 1fr) auto auto; gap: 6px; min-height: 46px; padding: 5px 6px; }
  .route-readout { display: none; }
  .compact { padding: 8px 7px; font-size: 8px; }
  .route-identity { min-width: 82px; }
  .route-identity i { height: 30px; }
  .racer-bay { grid-template-columns: 1fr; gap: 5px; padding: 5px; }
  .racer-bay-label { min-width: 0; flex-direction: row; justify-content: space-between; align-items: center; padding: 0 3px; }
  .racer-bay-label small { max-width: none; margin: 0; }
  .racer-card { flex-basis: 116px; min-height: 47px; padding: 4px 6px; }
  .toolbar { grid-template-columns: repeat(4, 1fr); gap: 5px; }
  .tool { min-width: 0; min-height: 46px; flex-direction: column; gap: 0; font-size: 7px; letter-spacing: .05em; }
  .tool span { font-size: 14px; line-height: 15px; }
  .go-button { grid-column: 1 / -1; min-height: 45px; flex-direction: row; gap: 9px; font-size: 12px; }
  .summary-grid { grid-template-columns: repeat(2, 1fr); }
  .shop-item { grid-template-columns: 1fr auto; gap: 8px; }
  .shop-item .owned { grid-column: 1; }
  .shop-item button { grid-column: 2; grid-row: 1 / span 2; }
}

@media (max-height: 650px) and (orientation: landscape) {
  #game-shell { min-height: 100svh; grid-template-columns: minmax(0, 1fr) 180px; grid-template-rows: auto auto 1fr auto; }
  .topbar { grid-column: 1 / -1; }
  #play-area { grid-column: 1; grid-row: 2 / 5; }
  #canvas-frame { width: min(100%, calc((100svh - 120px) * 10 / 7)); }
  .launch-dock { width: min(100%, calc((100svh - 120px) * 10 / 7)); min-height: 36px; }
  .launch-racer { min-height: 34px; }
  .arena-heading { position: absolute; left: 16px; z-index: 2; margin-top: 8px; pointer-events: none; }
  .arena-heading p { display: none; }
  .route-panel { grid-column: 2; grid-row: 2; display: flex; flex-direction: column; align-items: stretch; }
  .route-panel[hidden] { display: none; }
  .route-readout { display: flex; }
  .racer-bay { grid-column: 2; grid-row: 3; display: flex; flex-direction: column; min-height: 0; }
  .racer-bay-label { display: none; }
  .racer-list { flex-direction: column; overflow-y: auto; }
  .racer-card { flex: 0 0 45px; width: 100%; min-height: 45px; }
  .toolbar { grid-column: 2; grid-row: 4; grid-template-columns: repeat(2, 1fr); align-self: end; }
  .tool { min-width: 0; min-height: 38px; font-size: 7px; padding: 2px; }
  .go-button { grid-column: 1 / -1; min-height: 42px; }
}

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