:root {
  color-scheme: dark;
  --bg-deep: #090714;
  --panel: #130f26;
  --panel-2: #1e183a;
  --border: #33295e;
  --text: #f8fafc;
  --muted: #a5b4fc;
  --cyan: #00f5d4;
  --blue: #00bbf9;
  --magenta: #f72585;
  --gold: #ffd166;
  --coral: #ff4d6d;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg-deep);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  user-select: none;
  -webkit-user-select: none;
}

#app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: radial-gradient(circle at 50% 0%, #1d153b 0%, var(--bg-deep) 75%);
}

/* --- Top Telemetry Bar (outside canvas) --- */
#hud {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: 1.4fr 0.75fr 0.75fr 0.9fr 0.9fr 1.5fr;
  gap: 2px;
  background: var(--bg-deep);
  border-bottom: 2px solid var(--cyan);
}

.meter {
  min-width: 0;
  padding: 3px 6px 4px;
  background: var(--panel);
  text-align: center;
}

.meter span {
  display: block;
  overflow: hidden;
  color: var(--muted);
  font-size: 8px;
  font-weight: 800;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.meter strong {
  display: block;
  overflow: hidden;
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px;
  font-weight: 900;
  line-height: 1.25;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.meter-nitro {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 3px;
  padding-bottom: 5px;
}

#nitroBar {
  position: relative;
  height: 9px;
  overflow: hidden;
  border: 1.5px solid var(--border);
  border-radius: 999px;
  background: #0b0818;
}

#nitroFill {
  width: 50%;
  height: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--cyan), var(--blue));
  transition: width 70ms linear;
}

body[data-shockwave-ready="1"] #nitroFill,
body[data-shockwave="1"] #nitroFill {
  background: linear-gradient(90deg, var(--cyan), var(--magenta), var(--gold));
}

/* --- Playfield Stage --- */
#stage {
  position: relative;
  container-type: size;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background: #06040d;
  padding: 2px;
}

#game {
  display: block;
  width: min(100cqw, calc(100cqh * 16 / 9));
  height: min(100cqh, calc(100cqw * 9 / 16));
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: 960 / 540;
  border-radius: 4px;
  background: #0c091e;
  outline: none;
  touch-action: manipulation;
  cursor: pointer;
}

#game:focus-visible {
  box-shadow: 0 0 0 2px var(--cyan);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* --- Bottom Control Panel (outside canvas) --- */
#panel {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 5px 8px 6px;
  background: rgba(19, 15, 38, 0.98);
  border-top: 2px solid var(--border);
}

.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  flex-wrap: wrap;
}

#trackSelect {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-wrap: wrap;
}

.action-group {
  display: flex;
  align-items: center;
  gap: 5px;
  flex-wrap: wrap;
}

button {
  min-height: 27px;
  padding: 4px 10px;
  border: 1.5px solid var(--border);
  border-radius: 7px;
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
  font-size: 10.5px;
  font-weight: 800;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color 100ms ease, transform 60ms ease, border-color 100ms ease;
}

button:hover:not(:disabled) {
  background: #2b2252;
  border-color: var(--cyan);
}

button:active:not(:disabled) {
  transform: translateY(1px);
}

button:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}

button:disabled {
  opacity: 0.42;
  cursor: not-allowed;
}

#trackSelect button {
  min-height: 25px;
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 9.5px;
}

#trackSelect button[aria-pressed="true"] {
  background: var(--gold);
  border-color: #b47d12;
  color: #1c1303;
}

#play {
  min-width: 88px;
  background: var(--magenta);
  border-color: #ff7aa8;
  color: #ffffff;
  font-size: 11.5px;
}

#play:hover:not(:disabled) {
  background: #ff3d96;
}

.bottom-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

#touchPad {
  display: flex;
  align-items: center;
  gap: 4px;
  flex: 0 0 auto;
}

#touchPad button {
  min-height: 25px;
  padding: 2px 7px;
  font-size: 9.5px;
}

#touchNitro {
  background: #0b4f6c;
  border-color: var(--cyan);
  color: var(--cyan);
}

.hint {
  margin: 0;
  flex: 1 1 auto;
  color: var(--muted);
  font-size: 9.5px;
  line-height: 1.25;
  text-align: right;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.hint kbd {
  padding: 1px 3px;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--bg-deep);
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 9px;
}

@media (max-height: 540px) {
  .meter {
    padding: 2px 4px;
  }
  .meter span {
    display: none;
  }
  #panel {
    padding: 3px 6px 4px;
    gap: 2px;
  }
  button {
    min-height: 22px;
    padding: 2px 7px;
    font-size: 9.5px;
  }
}

/* Ultra-compact mode for 16:9 landscape iframe embedded on a 360px phone screen */
@media (max-height: 300px), (max-width: 560px) {
  #hud {
    border-bottom-width: 1px;
  }
  .meter {
    padding: 1px 2px;
  }
  .meter span {
    display: none;
  }
  .meter strong {
    font-size: 9px;
  }
  #nitroBar {
    height: 6px;
  }
  #stage {
    padding: 1px;
  }
  #panel {
    padding: 2px 4px;
    gap: 2px;
    border-top-width: 1px;
  }
  .top-bar {
    gap: 3px;
    flex-wrap: nowrap;
    overflow-x: auto;
  }
  #trackSelect {
    gap: 2px;
    flex-wrap: nowrap;
  }
  .action-group {
    gap: 2px;
    flex-wrap: nowrap;
  }
  button,
  #trackSelect button,
  #touchPad button {
    min-height: 18px;
    padding: 1px 4px;
    font-size: 8px;
    border-radius: 4px;
  }
  #play {
    min-width: 44px;
    font-size: 8.5px;
  }
  .bottom-bar {
    gap: 4px;
  }
  .hint {
    font-size: 7.5px;
  }
}
