:root {
  --bg: #060a14;
  --glass: rgba(14, 22, 40, 0.62);
  --glass-strong: rgba(12, 19, 36, 0.9);
  --line: rgba(148, 176, 255, 0.14);
  --text: #e6ecff;
  --muted: #8d9ac0;
  --accent: #facc15;
  --accent2: #38bdf8;
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text);
  font-family: Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif; font-size: 14px; }
button { font: inherit; color: inherit; cursor: pointer; }

#scene { position: fixed; inset: 0; touch-action: none; }
#scene canvas { display: block; }
.labels { position: absolute; inset: 0; pointer-events: none; }

.glass {
  background: var(--glass);
  backdrop-filter: blur(16px) saturate(140%);
  -webkit-backdrop-filter: blur(16px) saturate(140%);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.05);
}

/* ---------- loading ---------- */
#loading { position: fixed; inset: 0; display: grid; place-content: center; text-align: center; background: var(--bg);
  z-index: 50; transition: opacity 0.6s; color: var(--muted); }
#loading.hidden { opacity: 0; pointer-events: none; }
.spinner { font-size: 52px; animation: bounce 0.9s ease-in-out infinite alternate; }
@keyframes bounce { from { transform: translateY(0) rotate(0); } to { transform: translateY(-24px) rotate(180deg); } }

/* ---------- player labels ---------- */
.plabel { font-size: 11px; font-weight: 700; letter-spacing: 0.02em; padding: 2px 7px; border-radius: 999px;
  background: rgba(8, 12, 24, 0.72); border: 1px solid var(--c); color: #fff; white-space: nowrap;
  transform: translateY(-4px); transition: opacity 0.2s; }
.plabel .full { display: none; font-weight: 500; color: #cbd5f5; }
.plabel.hl { background: var(--c); color: #0b1020; box-shadow: 0 0 18px var(--c); }
.plabel.hl .full { display: inline; color: #0b1020; }
.plabel.opp { font-size: 10px; opacity: 0.85; border-color: rgba(239, 68, 68, 0.6); color: #fecaca; }

/* ---------- panel ---------- */
#panel { position: fixed; left: 16px; top: 16px; bottom: 16px; width: 318px; overflow-y: auto; padding: 16px 16px 12px;
  z-index: 10; scrollbar-width: thin; scrollbar-color: rgba(255,255,255,.15) transparent; }
#sheetHandle, #sheetClose, #mbar, #sheetBackdrop { display: none; }
.brand { display: flex; gap: 12px; align-items: center; margin-bottom: 8px; }
.brand .logo { font-size: 30px; width: 46px; height: 46px; display: grid; place-items: center; border-radius: 14px;
  background: linear-gradient(135deg, rgba(250,204,21,.25), rgba(56,189,248,.25)); border: 1px solid var(--line); }
.brand h1 { margin: 0; font-size: 18px; font-weight: 800; letter-spacing: -0.01em;
  background: linear-gradient(90deg, #fff, #a5c8ff); -webkit-background-clip: text; background-clip: text; color: transparent; }
.brand p { margin: 2px 0 0; color: var(--muted); font-size: 12px; }

section { padding: 12px 0; border-top: 1px solid var(--line); }
section:first-of-type { border-top: 0; }
h2 { font-size: 11px; text-transform: uppercase; letter-spacing: 0.12em; color: var(--muted); margin: 0 0 10px;
  display: flex; justify-content: space-between; align-items: baseline; }
h2 .hint { text-transform: none; letter-spacing: 0; font-weight: 500; font-size: 11px; opacity: 0.8; }
h3 { font-size: 11px; color: #aab6da; margin: 10px 0 6px; font-weight: 600; }

.seg { display: grid; grid-template-columns: repeat(6, 1fr); gap: 4px; background: rgba(0,0,0,.25); padding: 4px; border-radius: 12px; }
.rot { border: 0; background: transparent; padding: 8px 0; border-radius: 9px; font-weight: 700; color: var(--muted); transition: all .15s; }
.rot:hover { color: #fff; background: rgba(255,255,255,.06); }
.rot.active { background: linear-gradient(135deg, #facc15, #f59e0b); color: #1a1300; box-shadow: 0 4px 14px rgba(250,204,21,.35); }

.zonemap { margin-top: 10px; border-radius: 12px; overflow: hidden; border: 1px solid var(--line); background: rgba(217, 114, 47, 0.14); }
.netbar { text-align: center; font-size: 10px; letter-spacing: 0.3em; color: #cbd5f5; padding: 3px 0;
  background: repeating-linear-gradient(90deg, rgba(255,255,255,.15) 0 2px, transparent 2px 7px); border-bottom: 2px solid #fff; }
#zonegrid { display: grid; grid-template-columns: repeat(3, 1fr); }
.zcell { position: relative; padding: 9px 4px 8px; text-align: center; border-right: 1px dashed rgba(255,255,255,.12); border-bottom: 1px dashed rgba(255,255,255,.12); transition: background .2s; }
.zcell:nth-child(3n) { border-right: 0; }
.zcell:nth-child(n+4) { border-bottom: 0; }
.zcell:nth-child(-n+3) { border-bottom: 1.5px solid rgba(255,255,255,.35); }
.zcell em { position: absolute; left: 6px; top: 4px; font-style: normal; font-size: 10px; color: rgba(255,255,255,.4); }
.zcell b { display: inline-block; min-width: 38px; padding: 3px 6px; border-radius: 999px; font-size: 12px; background: rgba(0,0,0,.35); border: 1.5px solid var(--c); color: var(--c); }
.zcell.hl { background: color-mix(in srgb, var(--c) 22%, transparent); }
.zcell.hl b { background: var(--c); color: #0b1020; }
.zcell .srv { position: absolute; right: 4px; top: 3px; font-size: 9px; font-style: normal; color: #fbbf24; text-transform: uppercase; letter-spacing: .05em; }

.rowinfo { margin-top: 8px; }
.overlap { line-height: 1.6; padding: 8px 10px; border-radius: 10px; background: rgba(255,255,255,.04); border: 1px solid var(--line); color: #cfd8f6; }
.zcell.partner { outline: 2px dashed rgba(255,255,255,.7); outline-offset: -4px; }
.zcell .rel { display: block; margin-top: 3px; font-style: normal; font-size: 9.5px; color: #fff; opacity: .85; white-space: nowrap; }
.plabel.partner { outline: 2px dashed #fff; outline-offset: 2px; }
.rulelabel { font-size: 11px; font-weight: 600; padding: 3px 8px; border-radius: 8px; white-space: nowrap; color: #0b1020; pointer-events: none;
  box-shadow: 0 4px 14px rgba(0,0,0,.4); }
.rulelabel b { font-weight: 800; }
.rulelabel.ok { background: #4ade80; }
.rulelabel.bad { background: #f87171; }
.rowinfo.small { font-size: 12px; color: var(--muted); }
.badge { display: inline-block; font-size: 12px; padding: 4px 9px; border-radius: 8px; }
.badge.ok { background: rgba(74, 222, 128, .12); color: #86efac; border: 1px solid rgba(74,222,128,.3); }
.badge.bad { background: rgba(248, 113, 113, .12); color: #fca5a5; border: 1px solid rgba(248,113,113,.3); }
.badge.neutral { background: rgba(148,163,184,.1); color: #cbd5e1; border: 1px solid rgba(148,163,184,.25); }

.scn-list { display: grid; gap: 4px; }
.scn { text-align: left; border: 1px solid transparent; background: rgba(255,255,255,.03); padding: 9px 11px; border-radius: 10px; color: #cfd8f6; transition: all .15s; }
.scn:hover { background: rgba(255,255,255,.07); }
.scn.active { border-color: rgba(56,189,248,.5); background: linear-gradient(90deg, rgba(56,189,248,.2), rgba(56,189,248,.04)); color: #fff; font-weight: 600; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { display: inline-flex; align-items: center; gap: 6px; border: 1px solid rgba(255,255,255,.1); background: rgba(255,255,255,.04);
  padding: 6px 10px; border-radius: 999px; font-weight: 700; font-size: 12px; transition: all .15s; }
.chip i { width: 10px; height: 10px; border-radius: 50%; background: var(--c); box-shadow: 0 0 8px var(--c); }
.chip span { display: none; }
.chip.active { background: var(--c); color: #0b1020; border-color: var(--c); box-shadow: 0 0 16px color-mix(in srgb, var(--c) 50%, transparent); }
.chip.active i { background: #0b1020; box-shadow: none; }
.chip.clear { color: var(--muted); font-weight: 500; }
.quick { display: flex; gap: 6px; margin-top: 8px; }
.quick button, .btn { border: 1px solid var(--line); background: rgba(255,255,255,.04); border-radius: 8px; padding: 5px 10px; font-size: 12px; color: #cbd5f5; }
.quick button:hover, .btn:hover:not(:disabled) { background: rgba(255,255,255,.1); }
.btn:disabled { opacity: .4; cursor: default; }

.switch { display: flex; align-items: center; gap: 10px; padding: 6px 0; cursor: pointer; user-select: none; color: #cfd8f6; }
.switch input { display: none; }
.switch span { width: 34px; height: 20px; border-radius: 999px; background: rgba(255,255,255,.12); position: relative; transition: background .2s; flex: none; }
.switch span::after { content: ''; position: absolute; left: 3px; top: 3px; width: 14px; height: 14px; border-radius: 50%; background: #fff; transition: transform .2s; }
.switch input:checked + span { background: linear-gradient(90deg, #38bdf8, #6366f1); }
.switch input:checked + span::after { transform: translateX(14px); }
.random { display: flex; align-items: center; justify-content: space-between; gap: 8px; }

.credits { margin-top: 8px; font-size: 11px; color: var(--muted); line-height: 1.7; border-top: 1px solid var(--line); padding-top: 10px; }
kbd { font-family: inherit; font-size: 10px; padding: 1px 5px; border-radius: 4px; background: rgba(255,255,255,.1); border: 1px solid rgba(255,255,255,.15); }

/* ---------- phase card ---------- */
#phase { position: fixed; top: 16px; right: 16px; width: 400px; padding: 14px 16px 16px; z-index: 9; }
#phase.flash { animation: flash .5s ease; }
@keyframes flash { from { box-shadow: 0 0 0 2px rgba(250,204,21,.6), 0 10px 40px rgba(0,0,0,.35); } }
.phase-top { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); }
.phase-top #scenarioName { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pill { font-weight: 700; font-size: 11px; padding: 2px 8px; border-radius: 999px; background: rgba(250,204,21,.15); color: var(--accent); border: 1px solid rgba(250,204,21,.3); }
#phaseTitle { font-size: 20px; margin: 8px 0 6px; color: #fff; font-weight: 800; letter-spacing: -0.01em; }
#phaseText { margin: 0; line-height: 1.55; color: #cfd8f6; font-size: 13.5px; }
#phaseRule { margin: 10px 0 0; padding: 8px 10px; border-radius: 10px; font-size: 13px; line-height: 1.5; color: #d9f99d;
  background: rgba(74,222,128,.08); border: 1px solid rgba(74,222,128,.3); }
.tag { display: inline-block; padding: 0 6px; border-radius: 6px; font-weight: 700; font-size: 12px; line-height: 1.5;
  color: var(--c); background: color-mix(in srgb, var(--c) 16%, transparent); border: 1px solid color-mix(in srgb, var(--c) 45%, transparent); }
.tag.opp { font-weight: 600; }
.chev { display: none; }

/* ---------- camera views ---------- */
#views { position: fixed; right: 16px; bottom: 118px; display: flex; flex-direction: column; gap: 4px; padding: 6px; z-index: 9; }
#views button { border: 0; background: transparent; color: var(--muted); padding: 7px 12px; border-radius: 9px; font-size: 12px; font-weight: 600; text-align: left; }
#views button:hover { color: #fff; background: rgba(255,255,255,.06); }
#views button.active { color: #0b1020; background: #cfe0ff; }

/* ---------- transport ---------- */
#transport { position: fixed; left: 350px; right: 16px; bottom: 16px; display: grid; grid-template-columns: auto 1fr auto;
  gap: 18px; align-items: center; padding: 12px 16px; z-index: 9; }
.controls { display: flex; gap: 6px; align-items: center; }
.iconbtn { width: 38px; height: 38px; border-radius: 12px; border: 1px solid var(--line); background: rgba(255,255,255,.05); display: grid; place-items: center; transition: all .15s; flex: none; }
.iconbtn:hover { background: rgba(255,255,255,.12); }
.iconbtn:active { transform: scale(.94); }
.iconbtn svg { width: 20px; height: 20px; fill: currentColor; }
.iconbtn.play { width: 50px; height: 50px; border-radius: 50%; border: 0; background: linear-gradient(135deg, #facc15, #f59e0b); color: #1a1300; box-shadow: 0 6px 22px rgba(250,204,21,.4); }
.iconbtn.play svg { width: 24px; height: 24px; }
.iconbtn.active { background: #cfe0ff; color: #0b1020; border-color: #cfe0ff; }
body:not(.explain-on) #phase { display: none; }

.timeline { display: flex; align-items: center; gap: 12px; min-width: 0; }
.track { position: relative; flex: 1; height: 28px; display: flex; align-items: center; }
#ticks { position: absolute; left: 9px; right: 9px; top: 0; bottom: 0; pointer-events: none; }
.tick { position: absolute; top: 50%; width: 12px; height: 12px; margin: -6px 0 0 -6px; border-radius: 50%; border: 2px solid rgba(255,255,255,.35);
  background: #0e1628; pointer-events: auto; padding: 0; transition: all .15s; }
.tick.past { border-color: var(--accent); }
.tick.current { background: var(--accent); transform: scale(1.25); box-shadow: 0 0 10px rgba(250,204,21,.7); }
.tick:hover { transform: scale(1.4); }
.time { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 12px; white-space: nowrap; }

input[type=range] { -webkit-appearance: none; appearance: none; width: 100%; background: transparent; margin: 0; height: 28px; }
input[type=range]::-webkit-slider-runnable-track { height: 5px; border-radius: 99px;
  background: linear-gradient(90deg, var(--accent) var(--p, 0%), rgba(255,255,255,.14) var(--p, 0%)); }
input[type=range]::-moz-range-track { height: 5px; border-radius: 99px; background: rgba(255,255,255,.14); }
input[type=range]::-moz-range-progress { height: 5px; border-radius: 99px; background: var(--accent); }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 18px; height: 18px; border-radius: 50%; background: #fff; margin-top: -6.5px;
  box-shadow: 0 0 0 4px rgba(250,204,21,.25), 0 2px 8px rgba(0,0,0,.4); }
input[type=range]::-moz-range-thumb { width: 18px; height: 18px; border: 0; border-radius: 50%; background: #fff; box-shadow: 0 0 0 4px rgba(250,204,21,.25); }
#scrub { position: relative; z-index: 1; }
#speed::-webkit-slider-runnable-track { background: rgba(255,255,255,.14); }

.speed { width: 190px; }
.speed-head { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); }
.speed-head b { color: #fff; font-variant-numeric: tabular-nums; }
#speed { height: 22px; }
.speed-presets { display: flex; gap: 4px; }
.speed-presets button { flex: 1; border: 1px solid var(--line); background: rgba(255,255,255,.04); border-radius: 7px; padding: 3px 0; font-size: 12px; color: var(--muted); }
.speed-presets button.active { background: rgba(56,189,248,.2); color: #fff; border-color: rgba(56,189,248,.5); }

/* ---------- tablet ---------- */
@media (max-width: 1150px) and (min-width: 761px) {
  #phase { width: 330px; }
  #transport { grid-template-columns: auto 1fr; }
  .speed { grid-column: 1 / -1; width: auto; display: flex; align-items: center; gap: 12px; }
  .speed-head { gap: 8px; }
  .speed-presets { width: 140px; }
  #views { bottom: 170px; }
}

/* ---------- phone ---------- */
@media (max-width: 760px) {
  body { font-size: 15px; }
  #mbar { display: flex; position: fixed; top: calc(8px + var(--safe-t)); left: 8px; right: 8px; z-index: 12; padding: 6px; gap: 6px; align-items: center; border-radius: 14px; }
  .mtitle { flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; border: 0; background: transparent; padding: 6px 8px; text-align: left; font-weight: 700; font-size: 15px; }
  .mtitle #mScenario { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .mtitle::after { content: '▾'; color: var(--muted); font-size: 12px; }
  #menuBtn { width: 42px; height: 42px; }
  #menuBtn svg { fill: none; }

  #phase { top: calc(70px + var(--safe-t)); left: 8px; right: 8px; width: auto; padding: 10px 14px 12px; border-radius: 14px; cursor: pointer; }
  #phaseTitle { font-size: 17px; margin: 6px 0 4px; }
  #phaseText { font-size: 13.5px; max-height: 30vh; overflow-y: auto; }
  .phase-top #scenarioName { display: none; }
  .phase-top { justify-content: space-between; }
  .chev { display: block; width: 18px; height: 18px; margin-left: auto; opacity: .6;
    background: no-repeat center/12px url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8'%3E%3Cpath d='M1 6l5-4 5 4' stroke='%23cbd5f5' stroke-width='1.8' fill='none' stroke-linecap='round'/%3E%3C/svg%3E"); transition: transform .2s; }
  #phase.collapsed #phaseText { display: none; }
  #phaseRule { font-size: 12.5px; }
  #phase.collapsed .chev { transform: rotate(180deg); }
  #phase.collapsed #phaseTitle { margin-bottom: 0; }

  #views { flex-direction: row; left: 8px; right: auto; bottom: calc(150px + var(--safe-b)); padding: 4px; border-radius: 12px; }
  #views button { padding: 7px 10px; font-size: 12px; }

  #transport { left: 8px; right: 8px; bottom: calc(8px + var(--safe-b)); grid-template-columns: 1fr auto; grid-template-areas: "tl tl" "ctl spd";
    gap: 6px 10px; padding: 8px 12px 10px; border-radius: 16px; }
  .timeline { grid-area: tl; gap: 8px; }
  .time { font-size: 11px; }
  .controls { grid-area: ctl; gap: 6px; min-width: 0; }
  /* Small buttons give up width before the bar overflows on narrow phones. */
  .controls .iconbtn:not(.play) { flex: 0 1 44px; width: auto; min-width: 32px; }
  .speed { grid-area: spd; width: auto; display: flex; flex-direction: column; justify-content: center; gap: 4px; }
  .speed-head { display: none; }
  #speed { display: none; }
  .speed-presets button { padding: 8px 10px; font-size: 13px; }
  .iconbtn { width: 44px; height: 44px; }
  .iconbtn.play { width: 56px; height: 56px; }
  .track { height: 36px; }
  .tick { width: 14px; height: 14px; margin: -7px 0 0 -7px; }
  input[type=range] { height: 36px; }
  input[type=range]::-webkit-slider-thumb { width: 22px; height: 22px; margin-top: -8.5px; }

  /* Bottom sheet */
  #sheetBackdrop { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 19; opacity: 0; pointer-events: none; transition: opacity .25s; }
  #panel { left: 0; right: 0; top: auto; bottom: 0; width: auto; max-height: 86vh; max-height: 86dvh; border-radius: 22px 22px 0 0; z-index: 20;
    background: var(--glass-strong); padding: 6px 18px calc(18px + var(--safe-b)); transform: translateY(105%); transition: transform .32s cubic-bezier(.2,.8,.2,1);
    overscroll-behavior: contain; }
  body.sheet-open #panel { transform: translateY(0); }
  body.sheet-open #sheetBackdrop { opacity: 1; pointer-events: auto; }
  #sheetHandle { display: flex; justify-content: center; padding: 8px 0 10px; position: sticky; top: -6px; z-index: 2; }
  #sheetHandle span { width: 42px; height: 5px; border-radius: 99px; background: rgba(255,255,255,.25); }
  #sheetClose { display: grid; margin-left: auto; }
  #sheetClose svg { fill: none; }
  .brand .logo { width: 40px; height: 40px; font-size: 24px; }
  .rot { padding: 11px 0; font-size: 15px; }
  .scn { padding: 12px 13px; font-size: 15px; }
  .scn-list { grid-template-columns: 1fr 1fr; }
  .chip { padding: 9px 13px; font-size: 13px; }
  .quick button { padding: 8px 12px; font-size: 13px; }
  .switch { padding: 9px 0; }
  .credits { display: none; }
  .plabel { font-size: 10px; padding: 1px 6px; }
}

@media (max-width: 380px) {
  .scn-list { grid-template-columns: 1fr; }
  #views button { padding: 6px 8px; font-size: 11px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
