/* Code Repulsion & Territory Competition. Layout: title with the three scenarios; the learning-cycle bar with scrubber
   and status; the 3D hidden-space stage beside the lateral-weight matrix and the pair inspector; then the Hamming
   radar, the cosine spectrum and the territory gauge. Changing text has a fixed size, so nothing moves while it runs. */
:root {
  color-scheme: dark;
  --bg: #070a13;
  --panel: #0c111d;
  --panel-2: #121a29;
  --line: #1a2335;
  --line-2: #29364f;
  --fg: #e4e9f3;
  --muted: #8b97ad;
  --dim: #56627a;
  --blue: #3b82f6;
  --red: #ef4444;
  --cyan: #22d3ee;
  --amber: #fbbf24;
  --green: #34d399;
  --display: "Literata", "Iowan Old Style", Georgia, serif;
  --sans: "Instrument Sans", "Segoe UI", system-ui, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, "SFMono-Regular", Menlo, monospace;
  --r: 6px;
}
[hidden] { display: none !important; }
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--fg); font: 14px/1.45 var(--sans); }
button, select, input { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--blue); outline-offset: 2px; }

.app { display: grid; gap: 12px; padding-block: 12px 20px;
  padding-left: max(16px, env(safe-area-inset-left, 0px)); padding-right: max(16px, env(safe-area-inset-right, 0px)); }
.top { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 12px 24px; }
.eyebrow { font: 500 11px/1.2 var(--mono); letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
h1 { margin: 4px 0 0; font: 600 24px/1.15 var(--display); text-wrap: balance; }
h1 span { display: block; margin-top: 3px; font: 400 13px/1.3 var(--sans); color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { font: 12.5px var(--sans); padding: 5px 11px; border-radius: 999px; border: 1px solid var(--line-2); background: var(--panel); cursor: pointer; transition: border-color .15s, color .15s; }
.chip:hover { border-color: var(--blue); }
.chip.on { border-color: var(--blue); color: #93c5fd; background: rgba(59,130,246,.1); }
.note { margin: 0; font-size: 13.5px; color: #c7cfdf; max-width: 130ch; min-height: calc(2 * 1.45em); }
.note b { color: var(--fg); }

select, .btn { background: var(--panel-2); border: 1px solid var(--line-2); border-radius: var(--r); padding: 5px 9px; min-height: 32px; max-width: 100%; }
.btn { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; transition: border-color .15s; white-space: nowrap; }
.btn:hover { border-color: var(--blue); }
.btn svg { width: 14px; height: 14px; flex: none; }
.btn.icon { padding: 5px 8px; }
.btn.sm { min-height: 26px; padding: 2px 9px; font-size: 12px; }
.btn.primary { background: rgba(59,130,246,.14); border-color: rgba(59,130,246,.6); color: #93c5fd; }
.seg { display: inline-flex; border: 1px solid var(--line-2); border-radius: var(--r); overflow: hidden; flex: none; }
.segb { background: var(--panel-2); border: 0; padding: 3px 10px; font: 12px var(--mono); cursor: pointer; color: var(--muted); }
.segb + .segb { border-left: 1px solid var(--line-2); }
.segb.on { background: rgba(59,130,246,.16); color: #93c5fd; }

.card { border: 1px solid var(--line); border-radius: 8px; background: var(--panel); padding: 10px 12px 12px; display: grid; gap: 8px; min-width: 0; align-content: start; }
.card-h { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 6px 12px; }
.sec-h { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 10px; margin: 0; font: 500 11px var(--mono); letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.sec-h em { font-style: normal; text-transform: none; letter-spacing: 0; color: var(--dim); }
.cap { margin: 0; font: 11.5px/1.45 var(--mono); color: var(--dim); }
.cap .k { font-size: 13px; }
.k.r { color: var(--red); } .k.a { color: var(--amber); } .k.g { color: var(--green); }
.tag { white-space: nowrap; font: 500 11.5px var(--mono); color: var(--muted); padding: 3px 8px; border-radius: 4px; border: 1px solid var(--line-2); font-variant-numeric: tabular-nums; }
.tag:empty { visibility: hidden; }
.tag.good { color: var(--green); border-color: rgba(52,211,153,.5); }
.tag.warn { color: var(--amber); border-color: rgba(251,191,36,.5); }
.tag.bad { color: var(--red); border-color: rgba(239,68,68,.55); }
canvas { display: block; width: 100%; border-radius: 6px; border: 1px solid var(--line); background: #04060c; }

/* transport */
.tr-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; }
.spacer { flex: 1; }
.gain { display: inline-flex; align-items: center; gap: 10px; font-size: 13px; color: var(--muted); white-space: nowrap; }
.gain input { width: 160px; }
.gain b { font: 500 12.5px var(--mono); color: #93c5fd; min-width: 4.5ch; font-variant-numeric: tabular-nums; }
.clock { font: 500 12.5px var(--mono); color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; min-width: 16ch; text-align: right; }
.clock b { color: #93c5fd; font-weight: 600; display: inline-block; min-width: 4ch; text-align: right; }
.status { height: calc(1.45em); overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font: 12.5px var(--mono); color: var(--muted); }
.status b { color: var(--fg); font-weight: 500; }
.status .r { color: var(--red); } .status .g { color: var(--green); } .status .a { color: var(--amber); }
input[type=range] { -webkit-appearance: none; appearance: none; background: transparent; height: 18px; margin: 0; width: 100%; }
input[type=range]::-webkit-slider-runnable-track { height: 4px; border-radius: 2px; background: var(--line-2); }
input[type=range]::-moz-range-track { height: 4px; border-radius: 2px; background: var(--line-2); }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 15px; height: 15px; margin-top: -5.5px; border-radius: 50%; background: var(--fg); border: 2px solid var(--bg); box-shadow: 0 0 0 1px var(--blue), 0 0 10px rgba(59,130,246,.7); }
input[type=range]::-moz-range-thumb { width: 13px; height: 13px; border-radius: 50%; background: var(--fg); border: 2px solid var(--bg); }

/* stage */
.main { display: grid; grid-template-columns: minmax(0, 1fr) 380px; gap: 12px; align-items: start; }
.stage-wrap { position: relative; height: 560px; border-radius: 6px; overflow: hidden; border: 1px solid var(--line); background: radial-gradient(100% 90% at 50% 45%, #0c1730 0%, #04060c 75%); }
#cvStage { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; background: transparent; cursor: grab; touch-action: none; }
#cvStage.dragging { cursor: grabbing; }
.hint { position: absolute; left: 10px; bottom: 8px; font: 11px var(--mono); color: var(--dim); pointer-events: none; }

/* side */
.side { display: grid; gap: 12px; min-width: 0; }
#cvLat { height: 300px; cursor: pointer; }
.gauge { display: grid; gap: 6px; }
.meter { display: grid; grid-template-columns: 96px minmax(0, 1fr) 52px; gap: 8px; align-items: center; font: 12px var(--mono); font-variant-numeric: tabular-nums; }
.meter > span { color: var(--muted); white-space: nowrap; }
.meter > b { text-align: right; font-weight: 500; }
.meter .t { position: relative; height: 12px; border-radius: 3px; background: var(--panel-2); border: 1px solid var(--line); overflow: hidden; }
.meter .t i { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 2px; }
.meter.day .t i { background: linear-gradient(90deg, rgba(251,191,36,.45), var(--amber)); }
.meter.night .t i { background: linear-gradient(90deg, rgba(59,130,246,.5), #60a5fa); }
.kv { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 3px 12px; font: 12px var(--mono); font-variant-numeric: tabular-nums; }
.kv span { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kv b { font-weight: 500; text-align: right; white-space: nowrap; }
.kv .neg { color: var(--red); } .kv .pos { color: var(--cyan); } .kv .good { color: var(--green); }
#pairCap { height: calc(3 * 1.45em); overflow: hidden; }

/* lower */
.lower { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; align-items: start; }
#cvRadar { height: 260px; }
#cvCos { height: 150px; }
#cvCosH { height: 92px; }
.terr { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.terr div { border: 1px solid var(--line-2); border-radius: 6px; padding: 8px 10px; background: var(--panel-2); display: grid; gap: 2px; min-width: 0; }
.terr span { font: 11px var(--mono); color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.terr b { font: 600 22px/1.1 var(--mono); font-variant-numeric: tabular-nums; }
.terr .good b { color: var(--green); } .terr .warn b { color: var(--amber); } .terr .bad b { color: var(--red); }
.corners { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; }
.corners div { border: 1px solid var(--line); border-radius: 5px; padding: 5px 7px; display: grid; gap: 4px; min-height: 48px; align-content: start; }
.corners code { font: 11.5px var(--mono); color: var(--muted); }
.corners .dots { display: flex; flex-wrap: wrap; gap: 3px; }
.corners .dots i { width: 14px; height: 14px; border-radius: 50%; font: 700 9px/14px var(--mono); font-style: normal; color: #05070c; text-align: center; }
.corners div.clash { border-color: rgba(239,68,68,.6); background: rgba(239,68,68,.08); }
.corners div.held { border-color: rgba(52,211,153,.35); }

@media (max-width: 1150px) {
  .main { grid-template-columns: minmax(0, 1fr); }
  .side { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
  .lower { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 720px) {
  h1 { font-size: 20px; }
  .lower { grid-template-columns: minmax(0, 1fr); }
  .stage-wrap { height: 420px; }
  .gain input { width: 110px; }
  .clock { text-align: left; }
  .note { min-height: 0; }
}
.pg-embedded .top > div:first-child { display: none; }
@media (prefers-reduced-motion: reduce) { .btn, .chip { transition: none; } }
