:root {
  --bg: #04060f;
  --ink: #05070f;
  --panel: #0a1030;
  --panel2: #111a42;
  --edge: #8dffbf;
  --edge2: #38d995;
  --text: #e8f0ff;
  --dim: #8ea2cc;
  --warn: #ffd23f;
  --bad: #ff6b6b;
  --snow: #b9d3f2;
  --font: ui-monospace, "SFMono-Regular", Menlo, Consolas, "Liberation Mono", "Courier New", monospace;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  text-transform: uppercase;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}

body {
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

#app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--sat) var(--sar) var(--sab) var(--sal);
}

#stage {
  position: relative;
  line-height: 0;
  flex: none;
}

#screen {
  display: block;
  width: 640px;
  height: 360px;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #000;
  touch-action: none;
}

/* ---------- touch controls ---------- */
#pad {
  display: none;
  width: 100%;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  pointer-events: none;
}
body.touch #pad { display: flex; }
body.menu-open #pad { visibility: hidden; }

#pad .lean { display: flex; gap: 12px; }

.tbtn {
  pointer-events: auto;
  touch-action: none;
  font-family: var(--font);
  color: var(--text);
  background: rgba(17, 26, 66, 0.85);
  border: 4px solid var(--dim);
  border-radius: 0;
  box-shadow: 4px 4px 0 var(--ink);
  width: 84px;
  height: 84px;
  font-size: 13px;
  font-weight: 700;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 0;
  -webkit-user-select: none;
  user-select: none;
}
.tbtn .arr { font-size: 28px; line-height: 1; }
.tbtn.on { background: rgba(56, 217, 149, 0.45); border-color: var(--edge); transform: translate(2px, 2px); box-shadow: 2px 2px 0 var(--ink); }
.tbtn.act {
  width: 128px;
  height: 128px;
  font-size: 22px;
  border-color: var(--edge);
  color: var(--edge);
  background: rgba(10, 40, 34, 0.85);
}
.tbtn.act.on { background: rgba(141, 255, 191, 0.55); color: var(--ink); }

/* portrait: canvas on top, controls below */
@media (orientation: portrait) {
  #app { justify-content: flex-start; }
  /* keep the sound/menu buttons off the canvas HUD: put them at the bottom */
  /* a row between the canvas and the touch controls, so it never covers the HUD or the buttons */
  #stage { order: 0; }
  #topbar { position: static; order: 1; margin: 8px auto 0; justify-content: center; }
  #pad { order: 2; }
  #overlay { padding-top: calc(12px + var(--sat)); }
  #pad { flex: 1; align-items: center; padding-bottom: calc(16px + var(--sab)); }
}

/* landscape: canvas fills the height, translucent controls overlaid in the corners */
@media (orientation: landscape) {
  #pad {
    position: fixed;
    left: 0; right: 0;
    bottom: 0;
    padding: 0 calc(16px + var(--sar)) calc(14px + var(--sab)) calc(16px + var(--sal));
    align-items: flex-end;
  }
  #pad .tbtn { opacity: 0.6; background: rgba(17, 26, 66, 0.4); }
  #pad .tbtn.on { opacity: 0.9; }
  #pad .tbtn.act { background: rgba(10, 40, 34, 0.35); }
}
@media (orientation: landscape) and (max-height: 420px) {
  .tbtn { width: 66px; height: 66px; }
  .tbtn.act { width: 96px; height: 96px; font-size: 18px; }
}

/* ---------- top bar ---------- */
#topbar {
  position: fixed;
  top: calc(6px + var(--sat));
  right: calc(6px + var(--sar));
  display: flex;
  gap: 6px;
  z-index: 30;
}
#topbar button, .btn {
  font-family: var(--font);
  text-transform: uppercase;
  border-radius: 0;
  cursor: pointer;
}
#topbar button {
  font-size: 11px;
  font-weight: 700;
  color: var(--dim);
  background: rgba(5, 7, 15, 0.7);
  border: 2px solid var(--dim);
  padding: 5px 7px;
  min-width: 44px;
  min-height: 32px;
}
#topbar button:hover, #topbar button:focus-visible { color: var(--text); border-color: var(--text); outline: none; }
#topbar button.off { color: #56648e; border-color: #2a3a6e; text-decoration: line-through; }
#bMenu { display: none; }
body.menu-open #topbar { display: none; }
body.in-game #bMenu { display: block; }

/* ---------- overlay menus ---------- */
#overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: none;
  align-items: center;
  justify-content: center;
  padding: calc(12px + var(--sat)) calc(12px + var(--sar)) calc(12px + var(--sab)) calc(12px + var(--sal));
  background: rgba(4, 6, 15, 0.35);
  overflow-y: auto;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
}
#overlay.show { display: flex; }
#overlay.low { align-items: flex-end; }

.panel {
  width: 100%;
  max-width: 560px;
  margin: auto;
  background: rgba(10, 16, 48, 0.94);
  border: 4px solid var(--edge);
  box-shadow: 8px 8px 0 var(--ink), inset 0 0 0 2px var(--ink);
  padding: 18px 16px;
  font-size: 14px;
  line-height: 1.45;
}
.panel.wide { max-width: 720px; }

.logo {
  font-size: clamp(38px, 12vw, 64px);
  font-weight: 900;
  letter-spacing: 0.08em;
  line-height: 1;
  margin: 2px 0 4px;
  color: var(--edge);
  text-shadow: 4px 4px 0 #1f6f55, 8px 8px 0 var(--ink);
  text-align: center;
}
.tag { text-align: center; color: var(--dim); font-size: 12px; margin-bottom: 18px; letter-spacing: 0.1em; }

h2 {
  margin: 0 0 12px;
  font-size: 20px;
  letter-spacing: 0.08em;
  color: var(--edge);
  border-bottom: 4px solid var(--edge2);
  padding-bottom: 6px;
}
h3 { margin: 16px 0 6px; font-size: 14px; color: var(--warn); letter-spacing: 0.08em; }

.btn {
  display: block;
  width: 100%;
  font-size: 16px;
  font-weight: 800;
  letter-spacing: 0.08em;
  color: var(--text);
  background: var(--panel2);
  border: 4px solid var(--dim);
  box-shadow: 4px 4px 0 var(--ink);
  padding: 12px 10px;
  margin: 10px 0;
  text-align: center;
  min-height: 48px;
}
.btn:hover, .btn:focus-visible { border-color: var(--edge); color: var(--edge); outline: none; }
.btn:active { transform: translate(2px, 2px); box-shadow: 2px 2px 0 var(--ink); }
.btn.primary { border-color: var(--edge); color: var(--ink); background: var(--edge); }
.btn.primary:hover, .btn.primary:focus-visible { background: #b6ffd6; color: var(--ink); }
.btn.small { display: inline-block; width: auto; font-size: 12px; padding: 6px 10px; margin: 0; min-height: 40px; min-width: 40px; box-shadow: 3px 3px 0 var(--ink); }
.btn.danger { border-color: var(--bad); color: var(--bad); }
.btn:disabled { opacity: 0.45; cursor: default; }
.row { display: flex; gap: 10px; align-items: center; }
.row > .btn { flex: 1; }

.seg { display: flex; flex-wrap: wrap; gap: 8px; }
.seg button {
  flex: 1 1 0;
  min-width: 0;
  font-family: var(--font);
  font-weight: 800;
  font-size: 13px;
  color: var(--dim);
  background: var(--ink);
  border: 3px solid #2a3a6e;
  border-radius: 0;
  padding: 8px 4px;
  min-height: 44px;
  cursor: pointer;
  text-transform: uppercase;
}
.seg button small { display: block; font-size: 10px; font-weight: 600; color: var(--dim); }
.seg button.sel { color: var(--ink); background: var(--edge); border-color: var(--edge); }
.seg button.sel small { color: var(--ink); }
.seg.hills button { flex-basis: 30%; font-size: 12px; overflow-wrap: anywhere; }
@media (max-width: 400px) { .seg.hills button { font-size: 10px; } }

input[type=text] {
  font-family: var(--font);
  font-size: 16px;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--text);
  background: var(--ink);
  border: 3px solid var(--dim);
  border-radius: 0;
  padding: 8px 10px;
  width: 100%;
  min-height: 44px;
  user-select: text;
  -webkit-user-select: text;
}
input[type=text]:focus { outline: none; border-color: var(--edge); }
label.lbl { display: block; color: var(--dim); font-size: 12px; margin: 10px 0 4px; letter-spacing: 0.08em; }

.plist { list-style: none; margin: 0; padding: 0; }
.plist li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 0;
  border-bottom: 2px solid #1b2757;
  min-height: 48px;
}
.swatch { width: 14px; height: 22px; flex: none; border: 2px solid var(--ink); }
.plist .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 700; }
.plist input[type=text] { flex: 1; min-width: 0; font-size: 14px; padding: 6px 8px; min-height: 40px; }
.plist select {
  font-family: var(--font); font-size: 12px; font-weight: 700; text-transform: uppercase;
  background: var(--ink); color: var(--text); border: 3px solid #2a3a6e; border-radius: 0; padding: 6px; min-height: 40px;
}
.badge { font-size: 10px; padding: 2px 5px; border: 2px solid var(--dim); color: var(--dim); flex: none; }
.badge.host { border-color: var(--warn); color: var(--warn); }
.badge.off { border-color: var(--bad); color: var(--bad); }
.x { flex: none; }

.code {
  font-size: 44px;
  font-weight: 900;
  letter-spacing: 0.3em;
  color: var(--warn);
  text-align: center;
  margin: 4px 0;
  text-shadow: 4px 4px 0 var(--ink);
}
.link { font-size: 12px; color: var(--dim); word-break: break-all; text-align: center; text-transform: none; }
.muted { color: var(--dim); font-size: 12px; }
.err { color: var(--bad); font-weight: 700; min-height: 1.4em; }
.center { text-align: center; }

table.st { width: 100%; border-collapse: collapse; font-size: 13px; }
table.st th { color: var(--dim); font-size: 11px; text-align: right; padding: 4px 4px; border-bottom: 3px solid var(--edge2); font-weight: 700; }
table.st td { padding: 6px 4px; text-align: right; border-bottom: 2px solid #1b2757; white-space: nowrap; }
table.st th:nth-child(2), table.st td:nth-child(2) { text-align: left; width: 100%; white-space: normal; }
table.st td:first-child, table.st th:first-child { text-align: left; color: var(--warn); font-weight: 800; }
table.st td.tot { font-weight: 900; color: var(--edge); }
table.st tr.me td:nth-child(2) { color: var(--warn); }
table.st .dist { color: var(--dim); font-size: 11px; display: block; }
.nmcell { display: inline-flex; align-items: center; gap: 6px; font-weight: 700; }

.podium { display: flex; align-items: flex-end; justify-content: center; gap: 8px; margin: 10px 0 18px; }
.podium .pl { flex: 1; max-width: 160px; text-align: center; font-size: 12px; font-weight: 800; }
.podium .blk {
  margin-top: 6px;
  border: 4px solid var(--edge);
  background: var(--panel2);
  color: var(--edge);
  font-size: 26px;
  font-weight: 900;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 4px 4px 0 var(--ink);
}
.podium .p1 .blk { height: 96px; border-color: var(--warn); color: var(--warn); }
.podium .p2 .blk { height: 68px; }
.podium .p3 .blk { height: 48px; border-color: #ff9a3c; color: #ff9a3c; }
.podium .pts { color: var(--dim); font-size: 11px; }
.podium .nm2 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.howto p { margin: 6px 0; }
.howto .k { color: var(--warn); font-weight: 800; }
.howto ol { padding-left: 22px; margin: 6px 0; }
.howto li { margin: 5px 0; }

.countdown { color: var(--warn); font-weight: 800; text-align: center; margin-top: 8px; }

@media (max-width: 400px) {
  .panel { padding: 14px 10px; font-size: 13px; }
  .btn { font-size: 15px; }
  .code { font-size: 36px; }
  table.st { font-size: 12px; }
}

/* high scores */
.hsgrid { display: grid; grid-template-columns: 1fr; gap: 4px 18px; }
@media (min-width: 640px) { .hsgrid { grid-template-columns: 1fr 1fr; } }
table.st.hs td:nth-child(4) { text-align: right; }
table.st.hs td:nth-child(2) { overflow-wrap: anywhere; }

/* tour picker */
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 4px 0 8px; }
.chip { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 700; background: #12332c; border: 2px solid var(--edge2); padding: 3px 4px 3px 8px; }
.chip b { color: var(--edge); }
.chipx { font-family: var(--font); font-weight: 800; font-size: 11px; color: var(--bad); background: var(--ink); border: 2px solid var(--bad); border-radius: 0; min-width: 30px; min-height: 30px; cursor: pointer; }
.qp { margin: 0 0 8px; flex-wrap: wrap; }
.qp .btn.small { flex: 1; }
.hnum { display: inline-block; min-width: 20px; margin-right: 6px; text-align: center; background: var(--edge); color: var(--ink); font-size: 12px; }

/* fixed-page menus */
.steps { display: flex; flex-wrap: wrap; gap: 4px 6px; align-items: center; font-size: 11px; font-weight: 800; color: #56648e; margin: -4px 0 10px; letter-spacing: 0.06em; }
.steps span.on { color: var(--edge); }
.steps i { font-style: normal; font-size: 8px; }
.row.nav { margin-top: 12px; }
.row.nav .btn { margin: 0; }
.summary { font-size: 12px; color: var(--text); margin: 12px 0 0; }
.pgr { justify-content: center; margin: 6px 0 0; }
.pgr .btn.small { min-width: 48px; min-height: 40px; }
.pgl { font-size: 12px; font-weight: 800; color: var(--dim); }
.plist.compact li { min-height: 40px; padding: 3px 0; }
.legend { display: flex; gap: 12px; font-size: 10px; font-weight: 800; margin: 2px 0 6px; letter-spacing: 0.08em; }
.legend span::before { content: ''; display: inline-block; width: 10px; height: 10px; margin-right: 4px; vertical-align: -1px; background: currentColor; }
.t-normal { --tc: #8ea2cc; color: #8ea2cc; }
.t-large { --tc: #38d995; color: #38d995; }
.t-flying { --tc: #ffd23f; color: #ffd23f; }
.tgrid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.tile { position: relative; font-family: var(--font); text-transform: uppercase; text-align: left; min-height: 46px; padding: 5px 6px 4px 8px; background: var(--ink); color: var(--text); border: 3px solid var(--tc); border-left-width: 7px; border-radius: 0; box-shadow: 3px 3px 0 #000; cursor: pointer; display: flex; flex-direction: column; justify-content: center; gap: 1px; }
.tile b { font-size: 11px; line-height: 1.15; }
.tile small { font-size: 10px; font-weight: 700; color: var(--tc); }
.tile.sel { background: var(--edge); color: var(--ink); border-color: var(--edge); border-left-color: var(--tc); }
.tile.sel small { color: #12332c; }
.tile:focus-visible { outline: 3px solid #fff; outline-offset: 1px; }
.tile .tn { position: absolute; top: -2px; right: -2px; background: var(--ink); color: var(--edge); font-size: 11px; font-weight: 900; min-width: 18px; text-align: center; padding: 1px 3px; }
.hinfo { margin-top: 8px; padding: 6px 8px; min-height: 44px; font-size: 11px; line-height: 1.35; border: 2px solid #2a3a6e; background: var(--ink); }
.hinfo span { color: var(--dim); }
.hinfo .yb { color: var(--edge2); }
.chips { margin: 0 0 6px; gap: 4px; }
.chip { padding: 2px 2px 2px 6px; font-size: 11px; gap: 4px; }
.chip b { background: var(--edge); color: var(--ink); padding: 0 4px; }
.chipx { min-width: 40px; min-height: 40px; }
.qp { margin: 0 0 6px; }
.qp .btn.small { min-height: 40px; padding: 4px 8px; }
.sel3 { margin: 0 0 8px; }
.sel3 .btn.small { min-width: 48px; min-height: 44px; font-size: 16px; }
.selv { flex: 1; text-align: center; display: flex; flex-direction: column; }
.selv b { font-size: 14px; color: var(--edge); }
.selv small { font-size: 10px; color: var(--dim); }
.seg.tabs { margin: 0 0 6px; }
.hscols { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; margin-top: 8px; }
.hscol { list-style: none; margin: 0; padding: 0; }
.hscol li { display: grid; grid-template-columns: 26px 1fr auto; grid-template-rows: auto auto; column-gap: 4px; padding: 2px 0; border-bottom: 1px solid #1b2757; min-height: 24px; font-size: 12px; }
.hscol .rk { color: var(--warn); font-weight: 800; grid-row: span 2; }
.hscol .hn { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 700; }
.hscol .hv { color: var(--edge); font-weight: 800; text-align: right; }
.hscol .hx { grid-column: 2 / 4; font-size: 9px; color: var(--dim); }
.podium { margin: 4px 0 8px; }
.podium .p1 .blk { height: 64px; }
.podium .p2 .blk { height: 46px; }
.podium .p3 .blk { height: 32px; }
.podium .blk { font-size: 20px; }
@media (max-width: 520px) {
  .tgrid { grid-template-columns: repeat(3, 1fr); gap: 4px; }
  .tile { min-height: 40px; padding: 2px 4px 2px 6px; }
  .hinfo { min-height: 40px; margin-top: 6px; padding: 4px 6px; }
  .legend { margin: 0 0 4px; }
  .steps { margin: -4px 0 6px; }
  .tile b { font-size: 10px; }
  .hscol li { font-size: 11px; grid-template-columns: 22px 1fr auto; }
}
/* short landscape screens (phones): compact everything */
@media (max-height: 520px) and (orientation: landscape) {
  #overlay { padding-top: calc(6px + var(--sat)); padding-bottom: calc(6px + var(--sab)); }
  .panel { padding: 10px 12px; }
  .panel h2 { font-size: 15px; margin-bottom: 6px; padding-bottom: 3px; }
  .tgrid { grid-template-columns: repeat(8, 1fr); gap: 4px; }
  .tile { min-height: 40px; padding: 2px 4px 2px 6px; }
  .tile b { font-size: 9px; }
  .hinfo { margin-top: 5px; min-height: 0; padding: 4px 6px; font-size: 10px; }
  .legend { margin: 0 0 3px; }
  .btn { min-height: 40px; padding: 6px 8px; margin: 6px 0; font-size: 14px; }
  .row.nav { margin-top: 6px; }
  .code { font-size: 28px; margin: 0; }
  .logo { font-size: 34px; }
  .tag { margin-bottom: 6px; }
  .muted { margin: 4px 0; }
  .podium { display: none; }
  table.st td { padding: 2px 4px; }
  table.st th { padding: 2px 4px; }
  table.st .dist { display: inline; margin-left: 4px; }
  .howto p, .howto li { font-size: 12px; margin: 3px 0; }
  .howto h3, h3 { margin: 6px 0 2px; font-size: 12px; }
  .seg.tabs button, .seg button { min-height: 40px; padding: 4px; }
  .countdown { margin-top: 2px; }
  .sel3 { margin: 0 0 4px; }
}

/* desktop side standings (only shown when there is spare room beside the canvas) */
#side { display: none; position: fixed; z-index: 10; background: rgba(10, 16, 48, 0.94); border: 4px solid var(--edge); box-shadow: 6px 6px 0 var(--ink); padding: 10px 10px; overflow: hidden; font-size: 13px; }
#side.show { display: block; }
#side h4 { margin: 0; font-size: 14px; color: var(--edge); letter-spacing: 0.06em; }
#side .sub { font-size: 11px; color: var(--dim); margin: 2px 0 8px; border-bottom: 3px solid var(--edge2); padding-bottom: 6px; }
#side ol { list-style: none; margin: 0; padding: 0; }
#side li { display: grid; grid-template-columns: 26px 12px 1fr auto; gap: 6px; align-items: center; min-height: 30px; border-bottom: 2px solid #1b2757; }
#side li.cur { background: #12332c; outline: 2px solid var(--edge); outline-offset: -2px; }
#side li.cur .rk::before { content: '\25B6'; color: var(--edge); margin-right: 2px; font-size: 9px; }
#side .rk { color: var(--warn); font-weight: 800; text-align: right; }
#side .sw { width: 10px; height: 18px; border: 2px solid var(--ink); }
#side .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 700; }
#side .nm small { display: block; font-size: 10px; color: var(--dim); font-weight: 600; }
#side .tt { font-weight: 900; color: var(--edge); }
#side .gap { margin-top: 8px; font-size: 12px; color: var(--warn); font-weight: 800; }
#side .more { font-size: 11px; color: var(--dim); text-align: center; padding: 2px 0; }
