:root {
  --bg: #04050b;
  --ink: #eef0f8;
  --ink-2: #a4abc2;
  --ink-3: #6c7390;
  --glass: rgba(14, 16, 28, 0.52);
  --glass-strong: rgba(14, 16, 28, 0.78);
  --line: rgba(255, 255, 255, 0.08);
  --line-2: rgba(255, 255, 255, 0.16);
  --accent: #ffc58a;
  --accent-2: #8fb4ff;
  --glow: linear-gradient(90deg, #8fb4ff, #d39cff 50%, #ffc58a);
  --radius: 16px;
  --sans: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --serif: 'Instrument Serif', 'Iowan Old Style', Georgia, serif;
  --ease: cubic-bezier(.2, .7, .2, 1);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

canvas#sky {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: grab;
}
canvas#sky.dragging { cursor: grabbing; }

#veil {
  position: fixed;
  inset: 0;
  background: var(--bg);
  pointer-events: none;
  opacity: 0;
  transition: opacity .45s var(--ease);
  z-index: 5;
}
#veil.on { opacity: 1; }

button { font: inherit; color: inherit; }

svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
svg .fill { fill: currentColor; stroke: none; }

.glass {
  background: var(--glass);
  border: 1px solid var(--line);
  backdrop-filter: blur(18px) saturate(140%);
  -webkit-backdrop-filter: blur(18px) saturate(140%);
  border-radius: var(--radius);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.04);
}

/* ---------- HUD layout ---------- */

.hud {
  position: fixed;
  z-index: 10;
  transition: opacity .5s var(--ease), transform .5s var(--ease);
}
body.cinema .hud,
body.cinema .panel { opacity: 0; pointer-events: none; }
body.cinema .hud-bottom { transform: translateY(16px); }
body.cinema canvas#sky { cursor: none; }

.hud-top-left {
  top: max(22px, env(safe-area-inset-top));
  left: max(26px, env(safe-area-inset-left));
  max-width: min(440px, calc(100vw - 52px));
  pointer-events: none;
}

.brand {
  font-weight: 600;
  font-size: 11px;
  letter-spacing: .42em;
  color: var(--ink-2);
  background: var(--glow);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

h1 {
  font-family: var(--serif);
  font-weight: 400;
  font-style: italic;
  font-size: clamp(34px, 4.4vw, 52px);
  line-height: 1;
  margin: 10px 0 6px;
  letter-spacing: -.01em;
  text-shadow: 0 2px 30px rgba(0, 0, 0, .6);
}

.blurb {
  margin: 0;
  color: var(--ink-2);
  font-size: 14px;
  line-height: 1.45;
  text-shadow: 0 1px 12px rgba(0, 0, 0, .8);
}

.stats {
  display: flex;
  gap: 22px;
  margin: 16px 0 0;
}
.stats div { min-width: 0; }
.stats dt {
  font-size: 10px;
  letter-spacing: .16em;
  text-transform: uppercase;
  color: var(--ink-3);
}
.stats dd {
  margin: 3px 0 0;
  font-variant-numeric: tabular-nums;
  font-size: 13px;
  color: var(--ink);
  white-space: nowrap;
}

.hud-top-right {
  top: max(18px, env(safe-area-inset-top));
  right: max(18px, env(safe-area-inset-right));
  display: flex;
  gap: 4px;
  padding: 4px;
}

button.icon {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  border: 0;
  background: transparent;
  display: grid;
  place-items: center;
  color: var(--ink-2);
  cursor: pointer;
  transition: background .2s, color .2s, transform .15s;
}
button.icon:hover { background: rgba(255, 255, 255, 0.07); color: var(--ink); }
button.icon:active { transform: scale(.94); }
button.icon:focus-visible, .chip:focus-visible, .seg button:focus-visible, .pill:focus-visible {
  outline: 2px solid var(--accent-2);
  outline-offset: 2px;
}
button.icon.small { width: 32px; height: 32px; }
button.icon[aria-pressed="true"], button.icon[aria-expanded="true"] { color: var(--accent); }

.hud-top-right button.icon { background: rgba(14, 16, 28, 0.35); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); }
.hud-top-right button.icon:hover { background: rgba(255, 255, 255, 0.1); }

#btn-sound .when-on { display: none; }
#btn-sound[aria-pressed="true"] .when-on { display: block; }
#btn-sound[aria-pressed="true"] .when-off { display: none; }

/* ---------- bottom: scenarios + transport ---------- */

.hud-bottom {
  left: 50%;
  bottom: max(18px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: min(920px, calc(100vw - 32px));
  display: flex;
  flex-direction: column;
  gap: 12px;
}
body.cinema .hud-bottom { transform: translate(-50%, 16px); }

.scenarios {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
  padding: 2px;
  mask-image: linear-gradient(90deg, transparent, #000 12px, #000 calc(100% - 12px), transparent);
  -webkit-mask-image: linear-gradient(90deg, transparent, #000 12px, #000 calc(100% - 12px), transparent);
  padding-inline: 12px;
}
.scenarios::-webkit-scrollbar { display: none; }

.chip {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 34px;
  padding: 0 14px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: rgba(14, 16, 28, 0.45);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  color: var(--ink-2);
  cursor: pointer;
  font-size: 13px;
  white-space: nowrap;
  transition: color .2s, border-color .2s, background .2s, box-shadow .3s;
}
.chip svg { width: 16px; height: 16px; }
.chip:hover { color: var(--ink); border-color: var(--line-2); }
.chip[aria-selected="true"] {
  color: var(--ink);
  border-color: rgba(255, 197, 138, 0.45);
  background: rgba(255, 197, 138, 0.1);
  box-shadow: 0 0 24px rgba(255, 170, 110, 0.15);
}
.chip.ghost { border-style: dashed; }

.transport {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px 8px 8px;
}
.transport .play { color: var(--ink); }
.transport .play .when-playing { display: none; }
body.playing .transport .play .when-playing { display: block; }
body.playing .transport .play .when-paused { display: none; }

.timeline {
  position: relative;
  flex: 1;
  height: 40px;
  margin: 0 10px;
  cursor: pointer;
  touch-action: none;
}
.tl-track {
  position: absolute;
  left: 0; right: 0;
  top: 50%;
  height: 4px;
  margin-top: -2px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.08);
  overflow: hidden;
}
.tl-explored, .tl-fill {
  position: absolute;
  inset: 0 auto 0 0;
  width: 0;
  border-radius: 4px;
}
.tl-explored { background: rgba(255, 255, 255, 0.1); }
.tl-fill { background: var(--glow); box-shadow: 0 0 12px rgba(211, 156, 255, 0.5); }
.tl-thumb {
  position: absolute;
  top: 50%;
  left: 0;
  width: 14px;
  height: 14px;
  margin: -7px 0 0 -7px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 0 0 4px rgba(255, 255, 255, 0.12), 0 0 18px rgba(255, 220, 190, 0.7);
  pointer-events: none;
  transition: transform .15s;
}
.timeline:hover .tl-thumb, .timeline.scrubbing .tl-thumb { transform: scale(1.2); }
.timeline:focus { outline: none; }
.timeline:focus-visible .tl-thumb { box-shadow: 0 0 0 3px var(--accent-2), 0 0 18px rgba(255, 220, 190, 0.7); }

.tl-marks { position: absolute; inset: 0; pointer-events: none; }
.tl-mark {
  position: absolute;
  top: 50%;
  width: 22px;
  height: 22px;
  margin: -11px 0 0 -11px;
  pointer-events: auto;
  cursor: pointer;
  background: none;
  border: 0;
  padding: 0;
}
.tl-mark::before {
  content: '';
  position: absolute;
  left: 50%; top: 50%;
  width: 7px; height: 7px;
  margin: -3.5px 0 0 -3.5px;
  border-radius: 50%;
  background: var(--bg);
  border: 1.5px solid var(--accent);
  transition: transform .2s;
}
.tl-mark.merge::before { border-color: #d39cff; transform: rotate(45deg); border-radius: 1px; }
.tl-mark:hover::before { transform: scale(1.5); }
.tl-mark.merge:hover::before { transform: rotate(45deg) scale(1.5); }
.tl-mark span {
  position: absolute;
  bottom: 24px;
  left: 50%;
  transform: translate(-50%, 4px);
  white-space: nowrap;
  font-size: 11px;
  padding: 4px 8px;
  border-radius: 8px;
  background: var(--glass-strong);
  border: 1px solid var(--line);
  color: var(--ink);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
}
.tl-mark:hover span { opacity: 1; transform: translate(-50%, 0); }

.tl-time {
  width: 78px;
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: var(--ink-2);
  font-size: 13px;
}

.speed {
  display: flex;
  margin-left: 10px;
  padding: 3px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.04);
}
.speed button {
  border: 0;
  background: none;
  color: var(--ink-3);
  font-size: 12px;
  height: 28px;
  min-width: 36px;
  border-radius: 8px;
  cursor: pointer;
  font-variant-numeric: tabular-nums;
  transition: color .2s, background .2s;
}
.speed button:hover { color: var(--ink); }
.speed button[aria-checked="true"] { color: var(--ink); background: rgba(255, 255, 255, 0.1); }

/* ---------- toast + hint ---------- */

.toast {
  position: fixed;
  z-index: 11;
  left: 50%;
  top: 16%;
  transform: translate(-50%, 10px);
  text-align: center;
  pointer-events: none;
  opacity: 0;
  transition: opacity .7s var(--ease), transform .7s var(--ease);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast-title {
  font-family: var(--serif);
  font-style: italic;
  font-size: clamp(30px, 3.6vw, 44px);
  text-shadow: 0 2px 30px rgba(0, 0, 0, .8);
}
.toast-sub {
  margin-top: 4px;
  color: var(--ink-2);
  font-size: 13px;
  letter-spacing: .04em;
  text-shadow: 0 1px 12px rgba(0, 0, 0, .9);
}

.hint {
  position: fixed;
  z-index: 10;
  left: 50%;
  bottom: 150px;
  transform: translateX(-50%);
  font-size: 12px;
  color: var(--ink-2);
  padding: 8px 14px;
  border-radius: 999px;
  background: rgba(14, 16, 28, 0.5);
  border: 1px solid var(--line);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 1s var(--ease);
}
.hint.show { opacity: 1; }

/* ---------- panels ---------- */

.panel {
  position: fixed;
  z-index: 12;
  top: 72px;
  right: max(18px, env(safe-area-inset-right));
  width: 320px;
  max-height: calc(100vh - 72px - 170px);
  overflow-y: auto;
  padding: 16px 18px 18px;
  background: var(--glass-strong);
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, .15) transparent;
  animation: panel-in .35s var(--ease);
}
.panel[hidden] { display: none; }
@keyframes panel-in { from { opacity: 0; transform: translateY(-6px) scale(.985); } }

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: -4px -8px 8px 0;
}
.panel h2 {
  margin: 0;
  font-family: var(--serif);
  font-style: italic;
  font-weight: 400;
  font-size: 24px;
}
.panel h2.sub { font-size: 20px; margin: 20px 0 10px; }
.panel h3 {
  margin: 18px 0 4px;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: .18em;
  text-transform: uppercase;
  color: var(--ink-3);
}

.field-label {
  display: block;
  font-size: 12px;
  color: var(--ink-2);
  margin: 4px 0 8px;
}
.field-note {
  margin: 8px 0 4px;
  color: var(--ink-3);
  font-size: 12px;
  line-height: 1.45;
}

.seg {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 3px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--line);
}
.seg button {
  flex: 1 1 auto;
  border: 0;
  background: none;
  color: var(--ink-2);
  height: 30px;
  padding: 0 10px;
  border-radius: 9px;
  cursor: pointer;
  font-size: 12.5px;
  transition: color .2s, background .2s;
}
.seg button:hover { color: var(--ink); }
.seg button[aria-checked="true"] { background: rgba(255, 255, 255, 0.11); color: var(--ink); }

.slider-row {
  display: grid;
  grid-template-columns: 112px 1fr 52px;
  align-items: center;
  gap: 10px;
  margin: 12px 0 0;
}
.slider-row label { font-size: 12.5px; color: var(--ink-2); }
.slider-row output {
  font-size: 12px;
  color: var(--ink);
  text-align: right;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 22px;
  background: transparent;
  cursor: pointer;
  --pct: 50%;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 3px;
  border-radius: 3px;
  background: linear-gradient(90deg, rgba(255, 210, 170, .8) var(--pct), rgba(255, 255, 255, .1) var(--pct));
}
input[type="range"]::-moz-range-track {
  height: 3px;
  border-radius: 3px;
  background: linear-gradient(90deg, rgba(255, 210, 170, .8) var(--pct), rgba(255, 255, 255, .1) var(--pct));
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px; height: 14px;
  margin-top: -5.5px;
  border-radius: 50%;
  background: #fff;
  border: 0;
  box-shadow: 0 0 0 3px rgba(255, 255, 255, .1), 0 0 10px rgba(255, 220, 190, .5);
}
input[type="range"]::-moz-range-thumb {
  width: 14px; height: 14px;
  border-radius: 50%;
  background: #fff;
  border: 0;
  box-shadow: 0 0 0 3px rgba(255, 255, 255, .1), 0 0 10px rgba(255, 220, 190, .5);
}
input[type="range"]:focus-visible { outline: 2px solid var(--accent-2); outline-offset: 2px; border-radius: 4px; }

.toggle {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 14px;
  font-size: 12.5px;
  color: var(--ink-2);
  cursor: pointer;
}
.toggle input { position: absolute; opacity: 0; pointer-events: none; }
.toggle span {
  flex: none;
  width: 32px; height: 18px;
  border-radius: 9px;
  background: rgba(255, 255, 255, 0.12);
  position: relative;
  transition: background .2s;
}
.toggle span::after {
  content: '';
  position: absolute;
  left: 2px; top: 2px;
  width: 14px; height: 14px;
  border-radius: 50%;
  background: #fff;
  transition: transform .25s var(--ease);
}
.toggle input:checked + span { background: rgba(255, 197, 138, 0.7); }
.toggle input:checked + span::after { transform: translateX(14px); }
.toggle input:focus-visible + span { outline: 2px solid var(--accent-2); outline-offset: 2px; }

.panel-actions { display: flex; gap: 8px; margin-top: 18px; }
.pill {
  flex: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  height: 36px;
  border-radius: 999px;
  border: 1px solid var(--line-2);
  background: rgba(255, 255, 255, 0.04);
  cursor: pointer;
  font-size: 12.5px;
  color: var(--ink);
  transition: background .2s, border-color .2s;
}
.pill svg { width: 16px; height: 16px; }
.pill:hover { background: rgba(255, 255, 255, 0.09); }

/* ---------- fatal ---------- */

.fatal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 16px;
}
.fatal[hidden] { display: none; }
.fatal .glass { max-width: 460px; padding: 28px; }
.fatal h1 { font-size: 34px; }
.fatal p { color: var(--ink-2); line-height: 1.5; }

/* ---------- small screens ---------- */

@media (max-width: 720px) {
  .hud-top-left { top: max(16px, env(safe-area-inset-top)); left: 16px; max-width: calc(100vw - 32px); }
  h1 { margin-top: 6px; }
  .blurb { font-size: 13px; max-width: 70vw; }
  .stats { gap: 16px; margin-top: 10px; }
  .hud-top-right { top: auto; bottom: calc(max(14px, env(safe-area-inset-bottom)) + 150px); right: 10px; flex-direction: column; }
  .hud-top-right button.icon { width: 38px; height: 38px; }
  .hud-bottom { width: calc(100vw - 20px); bottom: max(10px, env(safe-area-inset-bottom)); gap: 10px; }
  .transport { flex-wrap: wrap; row-gap: 0; padding: 6px 10px 8px 6px; }
  .timeline { order: 5; flex-basis: 100%; margin: 0 6px; height: 32px; }
  .tl-time { width: auto; flex: 1; text-align: left; padding-left: 4px; }
  .speed { margin-left: 0; }
  .speed button { min-width: 32px; }
  .panel {
    top: auto;
    left: 10px; right: 10px;
    bottom: calc(max(10px, env(safe-area-inset-bottom)) + 140px);
    width: auto;
    max-height: 52vh;
  }
  .hint { bottom: auto; top: 190px; white-space: normal; text-align: center; width: max-content; max-width: calc(100vw - 32px); }
  .stats div:nth-child(3) { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition-duration: .01ms !important; animation-duration: .01ms !important; }
}
