:root {
  --bg: #0f1419;
  --panel: rgba(12, 17, 24, 0.62);
  --panel-border: rgba(255, 255, 255, 0.1);
  --text: #f3f5f7;
  --muted: #9aa7b4;
  --accent: #ffb347;
  --accent-2: #4fc3ff;
  --ok: #52d17c;
  --warn: #f2c94c;
  --err: #ef5b5b;
  --radius: 12px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, 'Hiragino Sans', 'Noto Sans JP', sans-serif;
  -webkit-font-smoothing: antialiased;
}

#scene { position: fixed; inset: 0; width: 100%; height: 100%; display: block; }

.hidden { display: none !important; }

button { font: inherit; color: inherit; cursor: pointer; }

.panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

#hud { position: fixed; inset: 0; pointer-events: none; transition: opacity 0.3s; }
#hud > * { transition: opacity 0.35s; }
/* immersive mode: only the fork chooser (when needed) and a minimal control row remain */
body.immersive #hud > :not(.controls):not(#fork):not(.sound-box) { opacity: 0; }
body.immersive .controls > :not(#btn-immersive):not(#btn-look):not(#sim):not(#sim-on) { opacity: 0; pointer-events: none; }
body.immersive .controls > * { transition: opacity 0.35s; }
body.immersive #btn-immersive { opacity: 0.45; }
body.immersive #btn-immersive:hover { opacity: 1; }
body.immersive #fork { transform: scale(0.85); transform-origin: top right; }
#btn-immersive.on { background: rgba(255, 179, 71, 0.25); }
body.immersive #btn-look { opacity: 0.45; }
body.immersive #btn-look:hover, body.immersive #btn-look.on { opacity: 1; }
#btn-look i { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; }
#btn-look.on i { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
#btn-look.loading i { background: var(--warn); }
#btn-look.error i { background: var(--err); }
#hud button, #hud input { pointer-events: auto; }

.leftcol { position: absolute; top: 16px; left: 16px; display: flex; flex-direction: column; gap: 10px; align-items: stretch; }
.metrics {
  display: grid; grid-template-columns: repeat(2, minmax(92px, auto)); gap: 4px 18px;
  padding: 12px 16px;
}
.metric span { font-size: 30px; font-weight: 700; font-variant-numeric: tabular-nums; }
.metric.big { grid-column: span 2; }
.metric.big span { font-size: 54px; line-height: 1; }
.metric small { font-size: 12px; color: var(--muted); margin-left: 3px; }
.metric label { display: block; font-size: 11px; color: var(--muted); letter-spacing: 0.04em; }

.center { padding: 8px 14px 10px; text-align: center; }
.center .grade span { font-size: 44px; font-weight: 800; font-variant-numeric: tabular-nums; }
.center .grade small { font-size: 16px; color: var(--muted); }
.center .row { display: flex; gap: 12px; justify-content: center; font-variant-numeric: tabular-nums; font-size: 17px; font-weight: 600; }
.center .row small { font-size: 11px; color: var(--muted); margin-left: 2px; }
.center .biome { margin-top: 4px; font-size: 12px; color: var(--muted); }
.wind-head { color: #ffb07a; }
.wind-tail { color: #8fe3a4; }

#minimap { position: absolute; top: 16px; right: 16px; width: 220px; height: 220px; border-radius: 50%; }

#fork {
  position: absolute; right: 16px; top: 250px; width: 280px; padding: 10px;
}
.fork-title { font-size: 13px; margin-bottom: 8px; }
.fork-title { white-space: nowrap; }
.fork-title small { color: var(--muted); margin-left: 6px; }
.fork-options { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
#fork.three { width: 380px; }
#fork.three .fork-options { grid-template-columns: 1fr 1fr 1fr; }
.fork-opt {
  display: flex; flex-direction: column; gap: 3px; align-items: flex-start; text-align: left;
  padding: 8px 10px; border-radius: 10px; border: 2px solid transparent;
  background: rgba(255, 255, 255, 0.08); font-size: 12px;
}
.fork-opt b { font-size: 15px; }
.fork-opt.active { border-color: var(--accent); background: rgba(255, 179, 71, 0.16); }
.fo-climb { color: var(--accent); font-weight: 600; }

#climb {
  position: absolute; left: 50%; top: 16px; transform: translateX(-50%);
  width: 320px; padding: 10px 14px;
}
.climb-name { font-weight: 700; font-size: 16px; margin-bottom: 6px; }
.climb-name::before { content: '⛰ '; }
.bar { height: 8px; border-radius: 4px; background: rgba(255, 255, 255, 0.15); overflow: hidden; }
#c-bar { height: 100%; width: 0; background: linear-gradient(90deg, #f29a3a, #e8513c); }
.climb-info { margin-top: 6px; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }

/* course profile at the bottom left, clear of the handlebars and head unit */
.profile-wrap {
  position: absolute; bottom: 62px; left: 16px; width: min(440px, calc(50% - 190px));
  height: 84px; padding: 4px; background: rgba(12, 17, 24, 0.5);
}
#profile { width: 100%; height: 100%; display: block; }

.controls {
  position: absolute; left: 16px; right: 16px; bottom: 16px;
  display: flex; gap: 8px; align-items: center;
}
.chip, .ctl {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--panel); border: 1px solid var(--panel-border); border-radius: 999px;
  padding: 8px 14px; font-size: 13px;
}
.chip i, .device i { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); display: inline-block; }
.chip.connected i, .device.connected i { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.chip.connecting i, .device.connecting i { background: var(--warn); }
.ctl { margin-left: auto; }
.ctl + .ctl { margin-left: 0; }
.sim { display: flex; align-items: center; gap: 8px; background: var(--panel); border-radius: 999px; padding: 6px 14px; font-size: 13px; }
.sim input { width: 160px; }
.sim-on { background: rgba(255, 179, 71, 0.9); color: #1b1307; font-weight: 700; border-color: transparent; }
.sim-off { background: transparent; border: 1px solid var(--panel-border); border-radius: 999px; padding: 3px 10px; font-size: 12px; }
select {
  background: rgba(255, 255, 255, 0.07); border: 1px solid var(--panel-border); border-radius: 8px;
  color: var(--text); padding: 8px 10px; font: inherit; width: 100%;
}
select option { background: #151b22; }

.sound-box {
  position: absolute; right: 16px; bottom: 62px;
  display: flex; align-items: center; gap: 8px;
  background: var(--panel); border: 1px solid var(--panel-border); border-radius: 999px;
  padding: 4px 4px 4px 14px; font-size: 12px; color: var(--muted); max-width: 60vw;
}
.sound-box span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sound-box.music span { color: var(--text); }
.sound-box button { background: rgba(255, 255, 255, 0.08); border: 0; border-radius: 999px; width: 30px; height: 30px; font-size: 14px; }
body.immersive #hud > .sound-box { opacity: 0.55; }

#toasts {
  position: fixed; top: 38%; left: 50%; transform: translateX(-50%);
  display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none; z-index: 5;
}
.toast {
  background: rgba(12, 17, 24, 0.78); border: 1px solid var(--panel-border); border-radius: 14px;
  padding: 10px 22px; text-align: center; animation: toast 4.2s ease forwards;
}
.toast b { display: block; font-size: 22px; }
.toast span { font-size: 13px; color: var(--muted); }
.toast.gold b { color: #ffd24a; }
.toast.climb b { color: var(--accent); }
@keyframes toast {
  0% { opacity: 0; transform: translateY(10px) scale(0.96); }
  8% { opacity: 1; transform: none; }
  85% { opacity: 1; }
  100% { opacity: 0; transform: translateY(-8px); }
}

.overlay {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(180deg, rgba(8, 12, 18, 0.25), rgba(8, 12, 18, 0.7));
  padding: 16px; z-index: 10; overflow-y: auto;
}
.card {
  width: min(560px, 100%); max-height: 100%; overflow-y: auto;
  background: rgba(15, 20, 27, 0.9); border: 1px solid var(--panel-border); border-radius: 18px;
  padding: 26px 28px; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
}
.card.narrow { width: min(360px, 100%); text-align: center; }
h1 { margin: 0; font-size: 34px; letter-spacing: -0.01em; }
h2 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin: 22px 0 10px; }
.card > h2:first-child { font-size: 20px; color: var(--text); text-transform: none; letter-spacing: 0; margin-top: 0; }
.tagline { margin: 4px 0 0; color: var(--muted); }
.hint { font-size: 12px; color: var(--muted); margin: 6px 0 0; }
.warn { margin-top: 16px; padding: 10px 12px; border-radius: 10px; background: rgba(242, 201, 76, 0.12); border: 1px solid rgba(242, 201, 76, 0.35); font-size: 13px; }

.devices { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.device {
  display: grid; grid-template-columns: auto 1fr; align-items: center; gap: 2px 10px; text-align: left;
  padding: 12px 14px; border-radius: 12px; border: 1px solid var(--panel-border); background: rgba(255, 255, 255, 0.05);
}
.device small { grid-column: 2; color: var(--muted); font-size: 12px; }

.seed { display: flex; gap: 8px; }
input[type='text'], input[type='number'] {
  background: rgba(255, 255, 255, 0.07); border: 1px solid var(--panel-border); border-radius: 8px;
  color: var(--text); padding: 8px 10px; font: inherit;
}
.seed input { flex: 1; min-width: 0; }
.grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; font-size: 13px; }
.grid label { display: flex; flex-direction: column; gap: 4px; color: var(--muted); }
.grid label.wide { grid-column: 1 / -1; flex-direction: row; align-items: center; gap: 10px; }
.grid label.wide input[type='range'] { flex: 1; }
.grid input[type='number'] { width: 100%; }

.actions { display: flex; gap: 10px; margin-top: 24px; justify-content: center; }
.primary, .ghost, .small {
  border-radius: 10px; border: 1px solid transparent; padding: 12px 22px; font-weight: 700;
}
.primary { background: var(--accent); color: #1b1307; }
.ghost { background: transparent; border-color: var(--panel-border); }
.small { padding: 6px 10px; font-size: 12px; font-weight: 600; background: var(--accent); color: #1b1307; }
.small.ghost { background: transparent; color: var(--text); }

.summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.summary div { background: rgba(255, 255, 255, 0.05); border-radius: 10px; padding: 10px 12px; }
.summary b { display: block; font-size: 22px; font-variant-numeric: tabular-nums; }
.summary span { font-size: 11px; color: var(--muted); }
.summary .full { grid-column: 1 / -1; font-size: 13px; }

.history { display: flex; flex-direction: column; gap: 8px; max-height: 55vh; overflow-y: auto; }
.history .item { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 10px; background: rgba(255, 255, 255, 0.05); font-size: 13px; }
.history .item .meta { flex: 1; }
.history .item .meta small { display: block; color: var(--muted); }
.history .empty { color: var(--muted); text-align: center; padding: 20px; }

@media (max-width: 820px) {
  .leftcol { top: 8px; left: 8px; gap: 6px; }
  .metrics { padding: 8px 10px; gap: 2px 10px; grid-template-columns: repeat(2, minmax(70px, auto)); }
  .metric span { font-size: 20px; }
  .metric.big span { font-size: 36px; }
  .center { padding: 6px 10px; }
  .center .row { font-size: 14px; gap: 8px; }
  .center .grade span { font-size: 30px; }
  #minimap { width: 130px; height: 130px; top: 8px; right: 8px; }
  #fork { top: 146px; right: 8px; width: 220px; }
  #climb { top: auto; bottom: 150px; width: calc(100% - 32px); }
  .profile-wrap { left: 8px; right: 8px; width: auto; height: 60px; bottom: 96px; }
  .controls { left: 8px; right: 8px; bottom: 10px; flex-wrap: wrap; }
  .sound-box { right: 8px; bottom: 164px; }
  .grid { grid-template-columns: 1fr 1fr; }
  .summary { grid-template-columns: 1fr 1fr; }
  .card { padding: 20px 18px; }
}
