:root {
  --bg: #0a0d11;
  --panel: #12171e;
  --panel-2: #171d26;
  --line: #232c38;
  --line-soft: #1b222c;
  --text: #dbe4ee;
  --muted: #8397ab;
  --dim: #64748b;
  --accent: #38bdf8;
  --ch1: #ffd23f;
  --ch2: #22d3ee;
  --math: #c084fc;
  --danger: #f87171;
  --ok: #4ade80;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, p { margin: 0; }

/* ---------- top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  padding: 14px 20px;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, #131922, #0e131a);
}

.brand { display: flex; align-items: center; gap: 12px; }

.brand-mark {
  width: 34px; height: 34px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background:
    radial-gradient(circle at 30% 25%, rgba(56, 189, 248, .35), transparent 60%),
    #0a0f15;
  position: relative;
  overflow: hidden;
}
.brand-mark::after {
  content: "";
  position: absolute; inset: 0;
  background:
    linear-gradient(transparent 49%, rgba(255, 210, 63, .9) 49%, rgba(255, 210, 63, .9) 51%, transparent 51%);
  mask: radial-gradient(circle at 50% 50%, #000 60%, transparent 61%);
}

.brand h1 { font-size: 15px; font-weight: 650; letter-spacing: .2px; }
.brand p { font-size: 11.5px; color: var(--dim); margin-top: 2px; }

.topbar-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.action-group { display: flex; gap: 8px; flex-wrap: wrap; }

.guide-link {
  text-decoration: none;
  color: var(--muted);
  border-style: dashed;
  margin-left: 6px;
}
.guide-link::after { content: " ↗"; opacity: .6; }
.guide-link:hover { color: var(--accent); border-color: #24556f; border-style: solid; }

/* ---------- layout ---------- */

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 372px;
  gap: 18px;
  padding: 18px 20px 28px;
  align-items: start;
  max-width: 1680px;
  margin: 0 auto;
}

.stage { min-width: 0; }

.screen-frame {
  background: linear-gradient(180deg, #171d26, #10151c);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  box-shadow: 0 18px 40px -24px rgba(0, 0, 0, .9);
  /* Keep the graticule square: cap the frame so it never grows taller than
     the viewport, and centre it when the column is wider. */
  width: min(100%, calc((100vh - 250px) * 1.25 + 26px));
  margin: 0 auto;
}

.screen {
  position: relative;
  aspect-ratio: 10 / 8;
  width: 100%;
  border-radius: 6px;
  overflow: hidden;
  background: #05080c;
  box-shadow: inset 0 0 60px rgba(0, 0, 0, .9), 0 0 0 1px #0d1219;
}

.screen canvas { display: block; width: 100%; height: 100%; }

/* ---------- measurements ---------- */

.measurements {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 12px;
}

.meas-card {
  flex: 1 1 260px;
  max-width: 420px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-left-width: 3px;
  border-radius: 8px;
  padding: 9px 11px;
}

.meas-card h3 {
  font-size: 10.5px;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 6px;
}

.meas-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 3px 12px;
}

.meas-grid div {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--text);
}
.meas-grid span { color: var(--dim); }

.hint {
  margin-top: 12px;
  font-size: 11.5px;
  color: var(--dim);
}
kbd {
  font-family: var(--mono);
  font-size: 10.5px;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 1px 5px;
  background: var(--panel-2);
  color: var(--muted);
}

/* ---------- control panels ---------- */

.controls {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
  /* The control stack is taller than the screen: keep it beside the display
     and let it scroll on its own. */
  position: sticky;
  top: 18px;
  max-height: calc(100vh - 36px);
  overflow-y: auto;
  padding-right: 6px;
  scrollbar-width: thin;
  scrollbar-color: #2a3441 transparent;
}

.controls::-webkit-scrollbar { width: 8px; }
.controls::-webkit-scrollbar-thumb {
  background: #2a3441;
  border-radius: 4px;
}
.controls::-webkit-scrollbar-track { background: transparent; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  flex: 0 0 auto;   /* never compress inside the scrolling sidebar */
}

.panel > header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 9px 12px;
  background: var(--panel-2);
  border-bottom: 1px solid var(--line);
}

.panel > header h2 {
  font-size: 10.5px;
  font-weight: 650;
  letter-spacing: .11em;
  text-transform: uppercase;
  color: var(--muted);
}

.panel-body {
  padding: 11px 12px 13px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.subhead {
  font-size: 10px;
  letter-spacing: .11em;
  text-transform: uppercase;
  color: var(--dim);
  border-top: 1px dashed var(--line);
  padding-top: 10px;
  margin-top: 1px;
}

.ch-dot {
  width: 9px; height: 9px;
  border-radius: 50%;
  display: inline-block;
  margin-right: 7px;
  vertical-align: -1px;
}

/* ---------- control primitives ---------- */

.ctl { display: flex; flex-direction: column; gap: 5px; }

.ctl-label {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  font-size: 11.5px;
  color: var(--muted);
}

.ctl-value {
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--text);
}

.ctl-note {
  font-size: 10.5px;
  line-height: 1.5;
  color: var(--dim);
  margin-top: -3px;
}

/* stepper */
.stepper {
  display: grid;
  grid-template-columns: 34px 1fr 34px;
  align-items: stretch;
  border: 1px solid var(--line);
  border-radius: 7px;
  overflow: hidden;
  background: var(--panel-2);
}
.stepper button {
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  transition: background .12s, color .12s;
}
.stepper button:hover:not(:disabled) { background: #1f2733; color: var(--text); }
.stepper button:disabled { color: #3b4654; cursor: default; }
.stepper .readout {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 7px 4px;
  font-family: var(--mono);
  font-size: 12.5px;
  color: var(--text);
  border-left: 1px solid var(--line);
  border-right: 1px solid var(--line);
}

/* segmented */
.segmented {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  border: 1px solid var(--line);
  border-radius: 7px;
  overflow: hidden;
  background: var(--panel-2);
}
.segmented button {
  border: 0;
  border-right: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
  font-family: var(--sans);
  font-size: 11.5px;
  padding: 6px 4px;
  cursor: pointer;
  transition: background .12s, color .12s;
}
.segmented button:last-child { border-right: 0; }
.segmented button:hover { background: #1f2733; color: var(--text); }
.segmented button[aria-pressed="true"] {
  background: #223448;
  color: #e7f3fb;
  box-shadow: inset 0 -2px 0 var(--accent);
}

/* range */
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 18px;
  background: transparent;
  cursor: pointer;
  margin: 0;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 4px;
  border-radius: 2px;
  background: #2a3441;
}
input[type="range"]::-moz-range-track {
  height: 4px;
  border-radius: 2px;
  background: #2a3441;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px; height: 14px;
  margin-top: -5px;
  border-radius: 50%;
  background: #cfe0ee;
  border: 1px solid #6b7a8c;
}
input[type="range"]::-moz-range-thumb {
  width: 14px; height: 14px;
  border-radius: 50%;
  background: #cfe0ee;
  border: 1px solid #6b7a8c;
}
input[type="range"]:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* number + select */
input[type="number"], select {
  width: 100%;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 7px;
  color: var(--text);
  font-family: var(--mono);
  font-size: 12.5px;
  padding: 6px 8px;
}
select { font-family: var(--sans); }
input[type="number"]:focus, select:focus {
  outline: none;
  border-color: var(--accent);
}

.field-row { display: grid; grid-template-columns: 1fr 76px; gap: 8px; align-items: center; }

/* buttons */
.btn {
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  border-radius: 7px;
  padding: 7px 13px;
  font-size: 11.5px;
  font-weight: 550;
  letter-spacing: .04em;
  cursor: pointer;
  transition: background .12s, border-color .12s, color .12s;
}
.btn:hover:not(:disabled) { background: #1f2733; border-color: #2f3a49; }
.btn:disabled { opacity: .42; cursor: default; }
.btn.primary { background: #17334a; border-color: #24556f; color: #d8eefb; }
.btn.run { color: var(--ok); border-color: #24523a; background: #12251b; }
.btn.stop { color: var(--danger); border-color: #5c2b2b; background: #26161a; }

/* toggle switch */
.switch {
  position: relative;
  width: 36px; height: 20px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: #1b232e;
  cursor: pointer;
  padding: 0;
  transition: background .15s, border-color .15s;
}
.switch::after {
  content: "";
  position: absolute;
  top: 2px; left: 2px;
  width: 14px; height: 14px;
  border-radius: 50%;
  background: #5d6b7c;
  transition: transform .15s, background .15s;
}
.switch[aria-pressed="true"] { background: #223448; border-color: #2f5570; }
.switch[aria-pressed="true"]::after { transform: translateX(16px); background: currentColor; }

@media (max-width: 1080px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .screen-frame { width: 100%; }
  .controls {
    position: static;
    max-height: none;
    overflow: visible;
    padding-right: 0;
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    align-items: start;
  }
}
