/* ==========================================================================
   Tokens
   Colours come from a validated data-visualisation palette: a blue<->red
   diverging pair with a grey midpoint for temperature, a blue sequential ramp
   for everything else. Dark mode is a separately chosen set of steps, not a
   flipped copy of the light one.
   ========================================================================== */

:root {
  color-scheme: light;

  --surface-page:   #f9f9f7;
  --surface-card:   #fcfcfb;
  --surface-sunk:   #f2f1ec;

  --ink-1: #0b0b0b;
  --ink-2: #52514e;
  --ink-3: #898781;

  --rule:     #e1e0d9;
  --rule-strong: #c3c2b7;
  --ring:     rgba(11, 11, 11, 0.10);

  --accent:      #2a78d6;
  --accent-deep: #184f95;
  --accent-wash: #e8f0fc;

  --good:     #0ca30c;
  --warning:  #fab219;
  --critical: #d03b3b;

  /* Temperature ramp: cold -> neutral -> hot. Grey at the midpoint, never a hue. */
  --t0: #0d366b;
  --t1: #2a78d6;
  --t2: #4a8ce0;
  --t3: #8a887f;
  --t4: #e4776f;
  --t5: #d93b3a;
  --t6: #9c2120;

  /* Scene */
  --room-top:    #f7f5ef;
  --room-bottom: #eeece4;
  --sky-top:     #cfe0f2;
  --sky-bottom:  #e7eef6;
  --floor:       #e3e0d6;
  --ground:      #d9d7cc;
  --wall:        #e8e6dd;
  --hatch:       #cdcabd;
  --cabinet:     #fbfaf7;
  --cabinet-ink: #b6b4aa;
  --fins:        #eceae4;
  --fin-line:    #d3d0c5;
  --metal:       #d6d3c8;
  --water:       #4a8ce0;

  --radius: 14px;
  --gutter: 16px;
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;

    --surface-page: #0d0d0d;
    --surface-card: #1a1a19;
    --surface-sunk: #232322;

    --ink-1: #ffffff;
    --ink-2: #c3c2b7;
    --ink-3: #898781;

    --rule: #2c2c2a;
    --rule-strong: #383835;
    --ring: rgba(255, 255, 255, 0.10);

    --accent:      #3987e5;
    --accent-deep: #9ec5f4;
    --accent-wash: #16273d;

    --good:     #0ca30c;
    --warning:  #fab219;
    --critical: #e66767;

    --t0: #2a78d6;
    --t1: #3987e5;
    --t2: #86b6ef;
    --t3: #a3a199;
    --t4: #f0a5a4;
    --t5: #e66767;
    --t6: #d03b3b;

    --room-top:    #24241f;
    --room-bottom: #1c1c19;
    --sky-top:     #16233a;
    --sky-bottom:  #1d2836;
    --floor:       #232320;
    --ground:      #1f1f1c;
    --wall:        #26261f;
    --hatch:       #35352c;
    --cabinet:     #2b2b28;
    --cabinet-ink: #4c4c46;
    --fins:        #24241f;
    --fin-line:    #3a3a33;
    --metal:       #46463f;
    --water:       #86b6ef;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-page: #0d0d0d;
  --surface-card: #1a1a19;
  --surface-sunk: #232322;
  --ink-1: #ffffff;
  --ink-2: #c3c2b7;
  --ink-3: #898781;
  --rule: #2c2c2a;
  --rule-strong: #383835;
  --ring: rgba(255, 255, 255, 0.10);
  --accent: #3987e5;
  --accent-deep: #9ec5f4;
  --accent-wash: #16273d;
  --critical: #e66767;
  --t0: #2a78d6; --t1: #3987e5; --t2: #86b6ef; --t3: #a3a199;
  --t4: #f0a5a4; --t5: #e66767; --t6: #d03b3b;
  --room-top: #24241f; --room-bottom: #1c1c19;
  --sky-top: #16233a; --sky-bottom: #1d2836;
  --floor: #232320; --ground: #1f1f1c; --wall: #26261f; --hatch: #35352c;
  --cabinet: #2b2b28; --cabinet-ink: #4c4c46;
  --fins: #24241f; --fin-line: #3a3a33; --metal: #46463f; --water: #86b6ef;
}

/* ==========================================================================
   Page frame
   ========================================================================== */

* { box-sizing: border-box; }

/* `hidden` is only a user-agent `display:none`, so any author display rule
   outranks it and the element stays painted. Make it win. */
[hidden] { display: none !important; }

body {
  margin: 0;
  padding: 0 var(--gutter) 64px;
  background: var(--surface-page);
  color: var(--ink-1);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.55;
  -webkit-text-size-adjust: 100%;
}

main, .page-head, .page-foot { max-width: 1120px; margin-inline: auto; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip-path: inset(50%); white-space: nowrap;
}

.page-head { padding: 56px 0 28px; }
.eyebrow {
  margin: 0 0 8px; font-size: 13px; font-weight: 600; letter-spacing: 0.08em;
  text-transform: uppercase; color: var(--accent);
}
h1 {
  margin: 0 0 14px;
  font-size: clamp(30px, 5.2vw, 46px);
  line-height: 1.1; letter-spacing: -0.02em;
}
.lede { margin: 0; max-width: 62ch; font-size: clamp(16px, 2.2vw, 18px); color: var(--ink-2); }
.lede em { font-style: normal; color: var(--ink-1); font-weight: 600; }

.section-title {
  margin: 0 0 8px; font-size: clamp(20px, 3vw, 25px);
  letter-spacing: -0.01em;
}
.section-lede { margin: 0 0 20px; max-width: 70ch; color: var(--ink-2); }

section { margin-top: 44px; }

/* ==========================================================================
   The scene
   ========================================================================== */

.scene-wrap {
  position: relative;
  background: var(--surface-card);
  border: 1px solid var(--ring);
  border-radius: var(--radius);
  padding: 8px 8px 0;
  overflow: hidden;
}

/* An <svg> takes its aspect ratio from the viewBox wherever CSS leaves an axis
   open, so pin both axes rather than letting it size itself. */
#scene { display: block; width: 100%; height: auto; }

.scene-legend {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 18px;
  padding: 10px 10px 12px; border-top: 1px solid var(--rule); margin-top: 4px;
}
.legend-ramp { display: flex; align-items: center; gap: 8px; }
.legend-cap { font-size: 12px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.ramp-bar {
  display: block; width: 168px; height: 10px; border-radius: 5px;
  background: linear-gradient(90deg, var(--t0), var(--t1), var(--t2), var(--t3), var(--t4), var(--t5), var(--t6));
  box-shadow: inset 0 0 0 1px var(--ring);
}
.legend-note { margin: 0; font-size: 12.5px; color: var(--ink-3); max-width: 58ch; }

/* --- scene paint ---------------------------------------------------------- */
.room-top { stop-color: var(--room-top); }
.room-bottom { stop-color: var(--room-bottom); }
.sky-top { stop-color: var(--sky-top); }
.sky-bottom { stop-color: var(--sky-bottom); }

.floor  { fill: var(--floor); }
.ground { fill: var(--ground); }
.floor-line, .ground-line { stroke: var(--rule-strong); stroke-width: 1.5; }
.sun { fill: var(--warning); }

.furniture { fill: none; stroke: var(--rule-strong); stroke-width: 2; stroke-linejoin: round; }
.furniture rect { fill: var(--surface-sunk); }

.wall { fill: var(--wall); }
.hatch-line { stroke: var(--hatch); stroke-width: 1.4; }
.wall-edge { stroke: var(--rule-strong); stroke-width: 2; }

.cabinet { fill: var(--cabinet); stroke: var(--cabinet-ink); stroke-width: 2; }
.cabinet-foot { fill: var(--metal); }

.fins { fill: var(--fins); }
.fin-lines line { stroke: var(--fin-line); stroke-width: 1.2; }

.grille line, .grille circle { stroke: var(--cabinet-ink); stroke-width: 2; fill: none; }
.grille-fan circle { stroke: var(--cabinet-ink); stroke-width: 1.4; opacity: 0.7; }

.blower circle { fill: var(--surface-sunk); stroke: var(--cabinet-ink); stroke-width: 2; }
.blades line { stroke: var(--ink-3); stroke-width: 2; stroke-linecap: round; }
.fan > circle { fill: var(--surface-sunk); stroke: var(--cabinet-ink); stroke-width: 2; }
.fan .blades path { fill: var(--metal); stroke: var(--cabinet-ink); stroke-width: 1.2; }
.fan .hub { fill: var(--cabinet-ink); stroke: none; }

.compressor-shell { fill: var(--metal); stroke: var(--cabinet-ink); stroke-width: 2; }
.compressor-cap { fill: var(--surface-sunk); stroke: var(--cabinet-ink); stroke-width: 2; }
.compressor-seam { stroke: var(--cabinet-ink); stroke-width: 1.4; opacity: 0.8; }

.valve-stroke { fill: none; stroke: var(--cabinet-ink); stroke-width: 2; }
.valve-fill { fill: var(--metal); stroke: var(--cabinet-ink); stroke-width: 2; stroke-linejoin: round; }
.valve-stem { stroke: var(--cabinet-ink); stroke-width: 2.5; stroke-linecap: round; }

.louver { stroke: var(--cabinet-ink); stroke-width: 6; stroke-linecap: round; }
.drain-pan { fill: var(--metal); opacity: 0.75; }
.drain-line { fill: none; stroke: var(--water); stroke-width: 2.5; opacity: 0.35; stroke-linecap: round; stroke-linejoin: round; }

/* --- pipes ---------------------------------------------------------------- */
.pipe-shell { fill: none; stroke: var(--rule-strong); stroke-linecap: round; stroke-linejoin: round; }
.pipe-core  { fill: none; stroke: var(--fins); stroke-linecap: round; stroke-linejoin: round; }
.pipe-insul { fill: none; stroke: var(--surface-sunk); stroke-linecap: round; stroke-linejoin: round; opacity: 0.9; }

/* --- air ------------------------------------------------------------------ */
.air-stream path {
  fill: none; stroke-width: 3; stroke-linecap: round;
  stroke-dasharray: 10 16;
}
.air-warm    path { stroke: var(--t4); opacity: 0.55; }
.air-cool    path { stroke: var(--t1); opacity: 0.7; }
.air-ambient path { stroke: var(--t3); opacity: 0.5; }
.air-hot     path { stroke: var(--t5); opacity: 0.65; }

/* --- labels --------------------------------------------------------------- */
.label { font-family: var(--font); fill: var(--ink-2); font-size: 13px; }
.label-title { fill: var(--ink-3); font-size: 13px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; }
.part-label { fill: var(--ink-1); font-size: 14px; font-weight: 600; }
.part-sub   { fill: var(--ink-3); font-size: 12px; }
.pipe-label { fill: var(--ink-3); font-size: 11.5px; letter-spacing: 0.02em; }
.air-label  { fill: var(--ink-3); font-size: 11.5px; font-style: italic; }
.leader { stroke: var(--rule-strong); stroke-width: 1.2; }
.label-plate { fill: var(--surface-card); opacity: 0.88; }
.scene-caption { fill: var(--ink-3); font-size: 12px; font-weight: 600; letter-spacing: 0.14em; text-transform: uppercase; }

.scene-readout rect { fill: var(--surface-card); stroke: var(--ring); stroke-width: 1; }
.scene-readout-label { fill: var(--ink-3); font-size: 11px; letter-spacing: 0.05em; text-transform: uppercase; }
.scene-readout-value { fill: var(--ink-1); font-size: 17px; font-weight: 600; font-variant-numeric: tabular-nums; }

/* --- selection ------------------------------------------------------------ */
.highlight {
  fill: none; stroke: var(--accent); stroke-width: 3; opacity: 0;
  transition: opacity 180ms ease;
  pointer-events: none;
}
.highlight.on { opacity: 1; }

.hit { fill: transparent; cursor: pointer; outline: none; }
.hit:focus-visible { stroke: var(--accent); stroke-width: 3; stroke-dasharray: 5 4; }

/* --- flags ---------------------------------------------------------------- */
.scene-flags { display: grid; gap: 10px; margin-top: 12px; }
.flag {
  display: flex; gap: 10px; align-items: flex-start;
  margin: 0; padding: 12px 14px; border-radius: 10px;
  font-size: 14px; line-height: 1.5;
  background: var(--surface-card); border: 1px solid var(--ring);
  color: var(--ink-2);
}
.flag strong { color: var(--ink-1); }
.flag-icon { flex: none; font-size: 12px; line-height: 1.7; }
.flag-warning { border-left: 4px solid var(--warning); }
.flag-warning .flag-icon { color: var(--warning); }
.flag-info { border-left: 4px solid var(--accent); }
.flag-info .flag-icon { color: var(--accent); }

/* ==========================================================================
   Controls
   ========================================================================== */

.control-grid {
  display: grid; gap: 18px;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
}
.control {
  background: var(--surface-card); border: 1px solid var(--ring);
  border-radius: var(--radius); padding: 14px 16px 12px;
}
.control-wide { grid-column: 1 / -1; }
.control label {
  display: flex; justify-content: space-between; align-items: baseline; gap: 10px;
  font-size: 14px; font-weight: 600; margin-bottom: 10px;
}
.control output {
  font-variant-numeric: tabular-nums; font-weight: 600; color: var(--accent);
  font-size: 15px;
}
.control-note { margin: 8px 0 0; font-size: 12.5px; color: var(--ink-3); line-height: 1.45; }

input[type="range"] {
  width: 100%; margin: 0; accent-color: var(--accent);
  height: 24px;
}

.segmented { display: flex; gap: 0; border: 1px solid var(--rule-strong); border-radius: 9px; overflow: hidden; }
.segmented button {
  flex: 1 1 0; min-width: 0;
  padding: 8px 6px; border: 0; border-right: 1px solid var(--rule-strong);
  background: var(--surface-card); color: var(--ink-2);
  font: inherit; font-size: 13.5px; cursor: pointer;
}
.segmented button:last-child { border-right: 0; }
.segmented button[aria-checked="true"] { background: var(--accent); color: #fff; font-weight: 600; }
.segmented button:hover:not([aria-checked="true"]) { background: var(--surface-sunk); }

.button-row { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.btn {
  padding: 9px 16px; border-radius: 9px; border: 1px solid var(--rule-strong);
  background: var(--surface-card); color: var(--ink-1);
  font: inherit; font-size: 14px; font-weight: 600; cursor: pointer;
}
.btn:hover { background: var(--surface-sunk); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-deep); border-color: var(--accent-deep); }
.btn-small { padding: 6px 12px; font-size: 13px; }

.inline-select { display: inline-flex; align-items: center; gap: 7px; font-size: 13.5px; color: var(--ink-2); }
.inline-select select {
  font: inherit; font-size: 13.5px; padding: 7px 9px; border-radius: 8px;
  border: 1px solid var(--rule-strong); background: var(--surface-card); color: var(--ink-1);
}

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ==========================================================================
   Stage cards
   ========================================================================== */

.stage-cards {
  display: grid; gap: 14px;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
}
.stage-card {
  position: relative; text-align: left;
  background: var(--surface-card); border: 1px solid var(--ring);
  border-radius: var(--radius); padding: 14px 16px 16px;
  font: inherit; color: inherit; cursor: pointer;
  border-top: 3px solid var(--stage-colour, var(--accent));
}
.stage-card:hover { background: var(--surface-sunk); }
.stage-card[aria-pressed="true"] {
  box-shadow: 0 0 0 2px var(--accent);
  background: var(--surface-card);
}
.stage-num {
  display: inline-flex; align-items: center; justify-content: center;
  width: 22px; height: 22px; border-radius: 50%;
  background: var(--stage-colour, var(--accent)); color: #fff;
  font-size: 12px; font-weight: 700; margin-bottom: 8px;
}
.stage-card h3 { margin: 0 0 4px; font-size: 16px; }
.stage-where { margin: 0 0 10px; font-size: 12px; color: var(--ink-3); }
.stage-figs { display: grid; gap: 4px; }
.stage-fig { display: flex; justify-content: space-between; gap: 8px; font-size: 13px; }
.stage-fig dt { color: var(--ink-3); }
.stage-fig dd { margin: 0; font-variant-numeric: tabular-nums; font-weight: 600; color: var(--ink-1); }

.stage-detail {
  margin-top: 16px; padding: 18px 20px;
  background: var(--surface-card); border: 1px solid var(--ring); border-radius: var(--radius);
  border-left: 4px solid var(--stage-colour, var(--accent));
}
.stage-detail h3 { margin: 0 0 8px; font-size: 18px; }
.stage-detail p { margin: 0 0 10px; color: var(--ink-2); max-width: 72ch; }
.stage-detail p:last-child { margin-bottom: 0; }
.stage-detail strong { color: var(--ink-1); }

/* ==========================================================================
   Metrics
   ========================================================================== */

.metric-grid {
  display: grid; gap: 14px;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
}
.metric {
  background: var(--surface-card); border: 1px solid var(--ring);
  border-radius: var(--radius); padding: 14px 16px;
}
.metric-label { margin: 0 0 6px; font-size: 12.5px; color: var(--ink-3); }
.metric-value { margin: 0; font-size: 24px; font-weight: 600; letter-spacing: -0.01em; }
.metric-value .unit { font-size: 14px; font-weight: 500; color: var(--ink-3); margin-left: 3px; }
.metric-note { margin: 4px 0 0; font-size: 12px; color: var(--ink-3); }
.metric-bar { height: 6px; border-radius: 3px; background: var(--surface-sunk); margin-top: 9px; overflow: hidden; }
.metric-bar span { display: block; height: 100%; border-radius: 3px; background: var(--accent); transition: width 200ms ease; }

/* ==========================================================================
   Chart
   ========================================================================== */

.chart-card {
  background: var(--surface-card); border: 1px solid var(--ring);
  border-radius: var(--radius); padding: 16px 16px 8px;
}
.chart-head { display: flex; flex-wrap: wrap; gap: 12px; justify-content: space-between; align-items: flex-start; }
.chart-title { margin: 0 0 2px; font-size: 16px; }
.chart-sub { margin: 0 0 6px; font-size: 12.5px; color: var(--ink-3); }
.chart-body { position: relative; }
#ph-chart { display: block; width: 100%; height: auto; }

.axis-line { stroke: var(--rule-strong); stroke-width: 1; }
.grid-line { stroke: var(--rule); stroke-width: 1; }
.axis-text { fill: var(--ink-3); font-size: 11px; font-family: var(--font); font-variant-numeric: tabular-nums; }
.axis-title { fill: var(--ink-2); font-size: 12px; font-family: var(--font); font-weight: 600; }
.dome { fill: var(--accent); fill-opacity: 0.08; stroke: var(--rule-strong); stroke-width: 1; }
.dome-text { fill: var(--ink-3); font-size: 11px; font-family: var(--font); font-style: italic; }
.cycle-path { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.cycle-leg { fill: none; stroke: transparent; stroke-width: 14; cursor: pointer; }
.cycle-leg-hi { fill: none; stroke: var(--accent-deep); stroke-width: 4.5; stroke-linecap: round; opacity: 0; }
.cycle-leg-hi.on { opacity: 1; }
.cycle-pt { stroke: var(--surface-card); stroke-width: 2; cursor: pointer; }
.cycle-pt-label { fill: var(--ink-1); font-size: 12px; font-weight: 700; font-family: var(--font); }

.chart-tooltip {
  position: absolute; pointer-events: none; z-index: 5;
  background: var(--surface-card); border: 1px solid var(--rule-strong);
  border-radius: 8px; padding: 8px 10px; font-size: 12.5px; line-height: 1.5;
  box-shadow: 0 6px 20px rgba(0,0,0,0.14); min-width: 150px;
  transform: translate(-50%, -100%);
}
.chart-tooltip .tt-title { font-weight: 700; margin-bottom: 3px; }
.chart-tooltip .tt-row { display: flex; justify-content: space-between; gap: 12px; color: var(--ink-2); }
.chart-tooltip .tt-row b { color: var(--ink-1); font-variant-numeric: tabular-nums; }

.chart-table { padding: 4px 0 12px; overflow-x: auto; }
.chart-table table { border-collapse: collapse; width: 100%; font-size: 13px; min-width: 460px; }
.chart-table th, .chart-table td { text-align: right; padding: 7px 10px; border-bottom: 1px solid var(--rule); }
.chart-table th:first-child, .chart-table td:first-child { text-align: left; }
.chart-table thead th { color: var(--ink-3); font-weight: 600; font-size: 12px; }
.chart-table td { font-variant-numeric: tabular-nums; }

/* ==========================================================================
   Prose + footer
   ========================================================================== */

.prose-grid {
  display: grid; gap: 16px;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
}
.prose-grid article {
  background: var(--surface-card); border: 1px solid var(--ring);
  border-radius: var(--radius); padding: 18px 20px;
}
.prose-grid h3 { margin: 0 0 8px; font-size: 16.5px; }
.prose-grid p { margin: 0; color: var(--ink-2); font-size: 14.5px; }
.prose-grid em { font-style: normal; font-weight: 600; color: var(--ink-1); }

.page-foot {
  margin-top: 52px; padding-top: 20px; border-top: 1px solid var(--rule);
  color: var(--ink-3); font-size: 13px;
}
.page-foot p { margin: 0 0 8px; max-width: 78ch; }
.foot-links a { color: var(--accent); }

/* ==========================================================================
   Motion
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
  .highlight, .metric-bar span { transition: none; }
}

@media (max-width: 720px) {
  .page-head { padding-top: 36px; }
  .scene-wrap { padding: 6px 6px 0; }
  .legend-note { font-size: 12px; }
  .button-row { gap: 8px; }
  .btn { padding: 8px 13px; font-size: 13.5px; }
}

.outlet-slot { fill: var(--surface-sunk); stroke: var(--cabinet-ink); stroke-width: 1.4; }
.pan-water { fill: var(--water); opacity: 0; }
.legend-note em { font-style: normal; font-weight: 600; color: var(--ink-2); }

.stage-card { display: flex; flex-direction: column; }
.stage-figs { margin-top: auto; padding-top: 4px; }

/* --- narrow screens: the cutaway is a wide drawing, so let it be panned
       rather than shrunk to the point where nothing can be read ------------ */
.scene-scroll { overflow: visible; }
.scene-hint { display: none; }

@media (max-width: 880px) {
  .scene-scroll {
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
  }
  .scene-scroll #scene { width: 860px; max-width: none; }
  .scene-hint {
    display: block; margin: 8px 2px 0; padding: 0;
    font-size: 12.5px; color: var(--ink-3);
  }
}
