:root {
  --bg: #0a0a0c;
  --panel: rgba(22, 22, 27, 0.82);
  --line: rgba(255, 255, 255, 0.12);
  --text: #e9e9ee;
  --muted: #8b8b98;
  --ok: #33d69f;
  --warn: #ffcc44;
  --bad: #ff5f6d;
  --mono: 'JetBrains Mono', ui-monospace, monospace;
  --sans: 'Outfit', system-ui, sans-serif;
  /** Phone toolbar height. Bottom sheets sit on top of it, so they share the measurement. */
  --barh: 54px;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

#stage { position: fixed; inset: 0; }
/* touch-action none: a drag has to orbit the scene, not scroll or zoom the page around it. */
#scene { display: block; width: 100%; height: 100%; cursor: grab; touch-action: none; }
#scene.dragging { cursor: grabbing; }

/* ---- verdict banner ---- */
#verdict {
  position: fixed; top: 0; left: 0; right: 0;
  padding: 12px 18px;
  background: linear-gradient(180deg, rgba(10,10,12,0.95), rgba(10,10,12,0.0));
  display: flex; align-items: baseline; gap: 14px; flex-wrap: wrap;
  pointer-events: none; z-index: 5;
}
#verdictHead { font-size: 1.05rem; font-weight: 700; letter-spacing: -0.01em; }
#verdictHead.bad { color: var(--bad); }
#verdictHead.warn { color: var(--warn); }
#verdictHead.ok { color: var(--ok); }
#verdictSub { font-size: 0.84rem; color: var(--muted); font-family: var(--mono); }

/* ---- left rail: legend + live processes ---- */
#leftRail {
  position: fixed; left: 16px; top: 58px; bottom: 104px; width: 286px;
  z-index: 4; display: flex; flex-direction: column; gap: 10px;
  overflow-y: auto; overscroll-behavior: contain; padding-right: 2px;
}
#leftRail::-webkit-scrollbar { width: 6px; }
#leftRail::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.16); border-radius: 3px; }
#legend, #procs {
  font-size: 0.7rem; font-family: var(--mono);
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: 12px; backdrop-filter: blur(8px); flex: 0 0 auto;
}
#legend h3, #procs h3 {
  margin: 0 0 9px; font-size: 0.62rem; letter-spacing: 0.14em;
  text-transform: uppercase; color: var(--muted); font-weight: 600;
  display: flex; align-items: center; gap: 7px;
}

/* ---- live process rows ---- */
.pr { margin-bottom: 9px; }
.pr:last-child { margin-bottom: 0; }
.pr-head { display: flex; align-items: baseline; gap: 6px; line-height: 1.3; }
.pr-name { color: #cfcfd8; font-weight: 500; white-space: nowrap; }
.pr-val {
  margin-left: auto; color: var(--muted); font-size: 0.66rem;
  white-space: nowrap; text-align: right;
}
.pr-sub { color: var(--muted); font-size: 0.64rem; line-height: 1.35; word-break: break-word; }
.pr-bar {
  height: 4px; border-radius: 2px; background: rgba(255,255,255,0.09);
  margin: 4px 0 3px; overflow: hidden;
}
.pr-fill { display: block; height: 100%; border-radius: 2px; transition: width 0.35s ease; }
.pr-dot {
  width: 7px; height: 7px; border-radius: 50%; flex: 0 0 auto;
  background: var(--muted); display: inline-block;
}
.pr-dot.on { background: var(--ok); }
.pr-dot.warn { background: var(--warn); }
.pr-dot.bad { background: var(--bad); }
/* A cycle tick is a moment, not a state, so it reads as a flash rather than a value. */
.pr-dot.beat { animation: beat 1s ease-out; }
@keyframes beat {
  0% { box-shadow: 0 0 0 0 rgba(51,214,159,0.85); }
  100% { box-shadow: 0 0 0 9px rgba(51,214,159,0); }
}
#procStale { color: var(--warn); }
.lg-row {
  display: flex; align-items: center; gap: 8px; padding: 3px 4px;
  border-radius: 5px; cursor: pointer; line-height: 1.25;
}
.lg-row:hover { background: rgba(255,255,255,0.06); }
.lg-row.muted { opacity: 0.34; }
.lg-dot { width: 11px; height: 11px; border-radius: 50%; flex: 0 0 auto; }
.lg-label { flex: 1; }
.lg-n { color: var(--muted); }

/* ---- right: inspector ---- */
#inspect {
  position: fixed; right: 16px; top: 64px; width: 292px; z-index: 4;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: 14px; backdrop-filter: blur(8px);
  font-family: var(--mono); font-size: 0.73rem; line-height: 1.5;
}
#inspect h3 {
  margin: 0 0 4px; font-family: var(--sans); font-size: 1rem; font-weight: 700;
}
#inspect .ins-status { font-size: 0.68rem; letter-spacing: 0.1em; text-transform: uppercase; margin-bottom: 10px; }
#inspect dl { display: grid; grid-template-columns: auto 1fr; gap: 3px 12px; margin: 0 0 10px; }
#inspect dt { color: var(--muted); }
#inspect dd { margin: 0; text-align: right; }
.ins-why {
  border-top: 1px solid var(--line); padding-top: 9px; margin-top: 9px;
  font-family: var(--sans); font-size: 0.8rem; color: #d3d3dc;
}
.ins-fix {
  margin-top: 9px; padding: 9px; border-radius: 7px;
  background: rgba(255, 204, 68, 0.1); border: 1px solid rgba(255, 204, 68, 0.3);
  font-family: var(--sans); font-size: 0.79rem; color: #ffe6a3;
}
.ins-fix b { color: #fff; }
#inspect .ins-empty { color: var(--muted); font-family: var(--sans); font-size: 0.82rem; }

/* ---- per-coin controls ---- */
.ins-ctl { border-top: 1px solid var(--line); margin-top: 10px; padding-top: 9px; }
.ins-ctl h4 {
  margin: 0 0 8px; font-size: 0.6rem; letter-spacing: 0.13em; text-transform: uppercase;
  color: var(--muted); font-weight: 600; font-family: var(--sans);
}
.ins-toggles { display: flex; gap: 6px; margin-bottom: 8px; flex-wrap: wrap; }
.ins-tog {
  flex: 1 1 auto; min-width: 62px; text-align: center; cursor: pointer;
  padding: 5px 6px; border-radius: 6px; border: 1px solid var(--line);
  background: rgba(255,255,255,0.04); color: var(--muted);
  font-family: var(--sans); font-size: 0.74rem; font-weight: 600; user-select: none;
}
.ins-tog.on { background: rgba(51,214,159,0.16); border-color: var(--ok); color: #9df3d4; }
.ins-tog.off { background: rgba(255,95,109,0.12); border-color: rgba(255,95,109,0.5); color: #ffb3b9; }
.ins-tog:hover { filter: brightness(1.25); }
.ins-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; margin-bottom: 8px; }
.ins-f { display: flex; flex-direction: column; gap: 3px; }
.ins-f label { font-size: 0.62rem; color: var(--muted); font-family: var(--sans); }
.ins-f input, .ins-f select {
  width: 100%; background: rgba(255,255,255,0.06); color: var(--text);
  border: 1px solid var(--line); border-radius: 6px; padding: 5px 7px;
  font-family: var(--mono); font-size: 0.74rem;
}
.ins-f input:focus, .ins-f select:focus { outline: none; border-color: #3d7dff; }
.ins-f.wide { grid-column: 1 / -1; }
.ins-actions { display: flex; gap: 6px; }
.ins-actions button {
  flex: 1; padding: 6px; border-radius: 6px; cursor: pointer;
  border: 1px solid var(--line); background: rgba(255,255,255,0.06); color: var(--text);
  font-family: var(--sans); font-size: 0.76rem; font-weight: 600;
}
.ins-actions button:hover { background: rgba(255,255,255,0.13); }

/* ---- locked variants (shareable read-only view) ---- */
.ro-tag {
  font-family: var(--mono); font-size: 0.54rem; letter-spacing: 0.12em;
  color: #ffcc44; border: 1px solid rgba(255,204,68,0.4); background: rgba(255,204,68,0.1);
  border-radius: 4px; padding: 1px 5px; margin-left: 6px; vertical-align: middle;
}
/* Same shape as a live control, but visibly inert: no pointer, no hover response. */
.ins-tog.ro { cursor: default; }
.ins-tog.ro:hover { filter: none; }
.ins-ro {
  background: rgba(255,255,255,0.03); border: 1px dashed var(--line); border-radius: 6px;
  padding: 5px 7px; font-family: var(--mono); font-size: 0.74rem; color: #c9c9d4;
}
.ins-actions.ro button { cursor: not-allowed; opacity: 0.42; }
.ins-actions.ro button:hover { background: rgba(255,255,255,0.06); }

/* ---- left rail: desk settings ---- */
/* Read-only rows are shared with the public view; the editable controls below only render
   when the panel can reach the strategy page through the bridge. */
#desk {
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: 11px 12px; backdrop-filter: blur(8px);
  font-family: var(--mono); font-size: 0.68rem;
}
#desk h3 {
  margin: 0 0 8px; font-family: var(--sans); font-size: 0.62rem; font-weight: 600;
  letter-spacing: 0.13em; text-transform: uppercase; color: var(--muted);
}
.dk-row {
  display: flex; justify-content: space-between; gap: 12px; padding: 2px 0;
  border-bottom: 1px solid rgba(255,255,255,0.05);
}
.dk-row:last-child { border-bottom: 0; }
.dk-row span { color: var(--muted); }
.dk-row b { font-weight: 500; color: var(--text); }

/* ---- editable desk controls ---- */
.dk-note {
  font-family: var(--mono); font-size: 0.58rem; letter-spacing: 0.04em;
  text-transform: none; color: var(--muted); opacity: 0.8;
}
.dk-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 8px 0 6px; }
.dk-in { display: flex; flex-direction: column; gap: 3px; }
.dk-in label {
  font-family: var(--sans); font-size: 0.62rem; color: var(--muted);
}
.dk-in input {
  width: 100%; box-sizing: border-box; padding: 5px 6px;
  background: rgba(255,255,255,0.05); border: 1px solid var(--line); border-radius: 6px;
  color: var(--text); font-family: var(--mono); font-size: 0.72rem;
}
.dk-in input:focus { outline: none; border-color: #3d7dff; }

.dk-sl { margin: 8px 0 4px; }
.dk-sl label {
  display: flex; justify-content: space-between; align-items: baseline; gap: 8px;
  font-family: var(--sans); font-size: 0.62rem; color: var(--muted); margin-bottom: 5px;
}
.dk-sl label b { font-family: var(--mono); font-size: 0.72rem; color: var(--text); font-weight: 600; }
.dk-sl input[type="range"] {
  -webkit-appearance: none; appearance: none; width: 100%; height: 4px; margin: 0;
  background: rgba(255,255,255,0.13); border-radius: 3px; cursor: pointer;
}
.dk-sl input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none; width: 15px; height: 15px; border-radius: 50%;
  background: #3d7dff; border: 2px solid #0b0f17; cursor: pointer;
}
.dk-sl input[type="range"]::-moz-range-thumb {
  width: 13px; height: 13px; border-radius: 50%;
  background: #3d7dff; border: 2px solid #0b0f17; cursor: pointer;
}
.dk-sl input[type="range"]:focus { outline: none; }
.dk-sl input[type="range"]:focus::-webkit-slider-thumb { box-shadow: 0 0 0 3px rgba(61,125,255,0.3); }

.dk-row.dk-t { align-items: center; padding: 5px 0; }
.dk-tog {
  padding: 3px 10px; border-radius: 999px; cursor: pointer;
  font-family: var(--mono); font-size: 0.64rem; line-height: 1.5;
  background: rgba(255,255,255,0.06); border: 1px solid var(--line); color: var(--muted);
}
.dk-tog.on { background: rgba(51,214,159,0.18); border-color: var(--good); color: #b6f2da; }
.dk-tog.off { background: rgba(255,255,255,0.04); color: var(--muted); }

.dk-sep { height: 1px; background: var(--line); margin: 10px 0 4px; }
.dk-msg {
  margin: 8px 0 0; font-family: var(--sans); font-size: 0.62rem; color: var(--muted);
  min-height: 1.1em;
}
.dk-msg.ok { color: var(--good); }
.dk-msg.err { color: var(--bad); }
.ins-actions button.arm { background: rgba(255,95,109,0.22); border-color: var(--bad); color: #ffd9dc; }
.ins-note {
  margin-top: 8px; font-family: var(--sans); font-size: 0.72rem;
  color: var(--muted); line-height: 1.4;
}
.ins-note.err { color: #ffb3b9; }
.ins-note.ok { color: #9df3d4; }

/* ---- bottom: controller bar ---- */
#controls {
  position: fixed; left: 50%; bottom: 16px; transform: translateX(-50%);
  z-index: 6; display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
  justify-content: center;
  background: var(--panel); border: 1px solid var(--line); border-radius: 11px;
  padding: 8px 10px; backdrop-filter: blur(8px);
}
#controls .sep { width: 1px; height: 20px; background: var(--line); margin: 0 4px; }
button.ctl {
  background: rgba(255,255,255,0.06); color: var(--text);
  border: 1px solid var(--line); border-radius: 7px;
  padding: 6px 11px; font-family: var(--sans); font-size: 0.78rem; font-weight: 500;
  cursor: pointer; white-space: nowrap;
}
button.ctl:hover { background: rgba(255,255,255,0.13); }
button.ctl.on { background: #3d7dff; border-color: #3d7dff; color: #fff; }
#tick { font-family: var(--mono); font-size: 0.68rem; color: var(--muted); padding: 0 6px; }

/* ---- axis caption ---- */
#axisNote {
  position: fixed; left: 50%; bottom: 80px; transform: translateX(-50%);
  z-index: 3; font-family: var(--mono); font-size: 0.68rem; color: var(--muted);
  pointer-events: none; text-align: center;
}

#err {
  position: fixed; left: 50%; top: 50%; transform: translate(-50%,-50%);
  z-index: 9; background: var(--panel); border: 1px solid var(--bad);
  border-radius: 10px; padding: 18px 22px; max-width: 420px; text-align: center;
  font-size: 0.88rem; line-height: 1.55;
}
#err a { color: #6fa8ff; }
[hidden] { display: none !important; }

/* Both only earn their place once the rails collapse into sheets. */
#sheetClose, #btnPanels { display: none; }

/* Tablets and narrow desktops: keep both rails, just tighten them. */
@media (max-width: 1100px) and (min-width: 761px) and (min-height: 521px) {
  #leftRail { width: 240px; left: 10px; }
  #inspect { width: 258px; right: 10px; }
}

/*
 * Phones.
 *
 * The scene is the point, so it keeps the whole screen and the two rails become bottom sheets
 * raised one at a time — the legend/desk rail from a toolbar button, the inspector from tapping
 * a coin. dvh rather than vh because mobile browser chrome slides away and vh does not follow.
 *
 * The short-height arm catches a phone held sideways: wide enough to miss a width breakpoint,
 * far too short for the desktop rails.
 */
@media (max-width: 760px), (max-height: 520px) {
  html, body { height: 100dvh; }

  #verdict { padding: 9px 12px 14px; gap: 4px 10px; }
  #verdictHead { font-size: 0.9rem; }
  #verdictSub { font-size: 0.7rem; }

  /* Reclaimed by the sheets; the caption competes with the toolbar for the same strip. */
  #axisNote { display: none; }

  #leftRail, #inspect {
    position: fixed; left: 0; right: 0; width: auto; top: auto;
    bottom: calc(var(--barh) + env(safe-area-inset-bottom, 0px));
    max-height: 58dvh; overflow-y: auto; -webkit-overflow-scrolling: touch;
    padding: 12px 12px 16px; margin: 0;
    background: rgba(14, 14, 18, 0.97); border: 0; border-top: 1px solid var(--line);
    border-radius: 14px 14px 0 0; box-shadow: 0 -14px 34px rgba(0, 0, 0, 0.55);
    /* Clear the toolbar it sits on as well as its own height, or a short sheet still peeks. */
    transform: translateY(calc(100% + var(--barh) + env(safe-area-inset-bottom, 0px) + 16px));
    transition: transform 0.22s ease; z-index: 7;
  }
  body.rail-open #leftRail, body.inspect-open #inspect { transform: translateY(0); }
  /* A hidden sheet must not swallow taps meant for the scene behind it. */
  body:not(.rail-open) #leftRail, body:not(.inspect-open) #inspect { pointer-events: none; }

  /* Inside a sheet the cards are already framed by the sheet itself. */
  #legend, #procs, #desk {
    background: none; border: 0; border-radius: 0; padding: 0 0 14px; backdrop-filter: none;
    font-size: 0.78rem;
  }
  #desk { border-top: 1px solid var(--line); padding-top: 12px; }
  .dk-row { padding: 5px 0; font-size: 0.76rem; }
  .lg-row { padding: 8px 4px; font-size: 0.82rem; }
  .lg-dot { width: 13px; height: 13px; }

  #inspect { font-size: 0.8rem; }
  #inspect h3 { font-size: 1.15rem; }
  #inspect dl { gap: 5px 12px; }
  .ins-tog { padding: 9px 8px; font-size: 0.82rem; }
  .ins-actions button { padding: 10px; font-size: 0.84rem; }
  .ins-f input, .ins-f select, .ins-ro { padding: 9px 8px; font-size: 0.82rem; }

  /* Shared dismiss affordance for whichever sheet is up. */
  #sheetClose {
    display: none; position: fixed; z-index: 8;
    right: 10px; bottom: calc(var(--barh) + env(safe-area-inset-bottom, 0px) + 58dvh - 42px);
    width: 34px; height: 34px; border-radius: 50%; cursor: pointer;
    border: 1px solid var(--line); background: rgba(30, 30, 38, 0.96); color: var(--text);
    font-size: 1.1rem; line-height: 1; padding: 0;
  }
  body.rail-open #sheetClose, body.inspect-open #sheetClose { display: block; }

  /* One scrollable row, so the toolbar height stays predictable for the sheets above it. */
  #controls {
    left: 0; right: 0; bottom: 0; transform: none; border-radius: 0;
    border-left: 0; border-right: 0; border-bottom: 0;
    height: var(--barh); padding: 0 8px calc(env(safe-area-inset-bottom, 0px)) 8px;
    flex-wrap: nowrap; justify-content: flex-start;
    overflow-x: auto; overflow-y: hidden; -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  #controls::-webkit-scrollbar { display: none; }
  button.ctl { padding: 9px 13px; font-size: 0.82rem; flex: 0 0 auto; }
  #btnPanels { display: block; }
  #controls .sep { flex: 0 0 auto; }
  #tick { flex: 0 0 auto; }
}

/* Extra room for the toolbar's safe area once the sheet is tall on small phones. */
@media (max-width: 760px) and (max-height: 700px) {
  #leftRail, #inspect { max-height: 52dvh; }
  #sheetClose { bottom: calc(var(--barh) + env(safe-area-inset-bottom, 0px) + 52dvh - 42px); }
}
