:root {
  color-scheme: light;
  --bg: #f4f4f2;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e2e1dc;
  --border-strong: #c9c8c1;
  --text: #0b0b0b;
  --text-2: #52514e;
  --text-3: #7a7973;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --accent-soft: #e6f0fb;
  --plate: #d9dde3;
  --plate-edge: #9aa3ad;
  --grid: #ebeae6;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100; --s5: #e87ba4;
  --cold: #2a78d6; --hot: #e34948;
  --ok: #008300; --warn: #b26b00; --err: #c62828; --info: #2a78d6;
  --warn-bg: #fff6e5; --err-bg: #fdecec; --info-bg: #eef4fc;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 2px 8px rgba(0,0,0,.04);
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111110; --surface: #1a1a19; --surface-2: #232321; --border: #2e2e2c; --border-strong: #45453f;
    --text: #f0efec; --text-2: #c3c2b7; --text-3: #8f8e86;
    --accent: #3987e5; --accent-soft: #1d2b3d;
    --plate: #3a3f46; --plate-edge: #6b7480; --grid: #2a2a28;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181;
    --cold: #3987e5; --hot: #e66767;
    --ok: #3fae3f; --warn: #e0a030; --err: #f07070; --info: #6aa8f0;
    --warn-bg: #2d2412; --err-bg: #331a1a; --info-bg: #17222f;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111110; --surface: #1a1a19; --surface-2: #232321; --border: #2e2e2c; --border-strong: #45453f;
  --text: #f0efec; --text-2: #c3c2b7; --text-3: #8f8e86;
  --accent: #3987e5; --accent-soft: #1d2b3d;
  --plate: #3a3f46; --plate-edge: #6b7480; --grid: #2a2a28;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181;
  --cold: #3987e5; --hot: #e66767;
  --ok: #3fae3f; --warn: #e0a030; --err: #f07070; --info: #6aa8f0;
  --warn-bg: #2d2412; --err-bg: #331a1a; --info-bg: #17222f;
  --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
h1, h2 { margin: 0; font-weight: 600; }
h2 { font-size: 14px; letter-spacing: .01em; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; flex-wrap: wrap; gap: 12px; align-items: center; justify-content: space-between;
  padding: 10px 20px; background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; }
.logo { width: 30px; height: 30px; flex: none; }
.logo rect { fill: var(--accent); }
.logo path { stroke: var(--accent-ink); stroke-width: 2; fill: none; stroke-linecap: round; }
h1 { font-size: 16px; }
.subtitle { margin: 0; color: var(--text-3); font-size: 12px; }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }

select, input[type="number"] {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border-strong); border-radius: 7px;
  padding: 6px 8px; width: 100%; min-width: 0; height: 32px;
}
input[type="number"] { font-variant-numeric: tabular-nums; }
select:focus-visible, input:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
input:invalid, input.bad { border-color: var(--err); }
.preset select { width: auto; max-width: 270px; }
.lang select { width: auto; }

.btn {
  font: inherit; font-weight: 500; height: 32px; padding: 0 12px; border-radius: 7px; cursor: pointer;
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text);
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn:disabled { opacity: .6; cursor: progress; }
.btn-icon { width: 32px; padding: 0; display: inline-grid; place-items: center; }

.seg { display: inline-flex; border: 1px solid var(--border-strong); border-radius: 8px; padding: 2px; background: var(--surface-2); }
.seg button {
  font: inherit; font-weight: 500; border: 0; background: transparent; color: var(--text-2);
  padding: 4px 12px; border-radius: 6px; cursor: pointer; height: 26px;
}
.seg button[aria-checked="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.12); }
.seg-wide { display: flex; margin-bottom: 10px; }
.seg-wide button { flex: 1; }

/* ---------- Layout ---------- */
.layout {
  display: grid; grid-template-columns: minmax(300px, 380px) minmax(0, 1fr); gap: 20px;
  padding: 20px; max-width: 1440px; margin: 0 auto;
}
.inputs { display: flex; flex-direction: column; gap: 14px; }
fieldset {
  margin: 0; padding: 14px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 10px;
}
legend { font-weight: 600; padding: 0 6px; margin-left: -6px; display: flex; align-items: center; gap: 8px; float: left; width: 100%; margin-bottom: 2px; }
legend + * { clear: both; }
.step {
  display: inline-grid; place-items: center; width: 20px; height: 20px; border-radius: 50%;
  background: var(--accent-soft); color: var(--accent); font-size: 12px; font-weight: 700;
}
label { display: grid; grid-template-columns: auto 1fr; align-content: start; column-gap: 4px; row-gap: 4px; font-size: 12px; color: var(--text-2); font-weight: 500; }
label > input, label > select { grid-column: 1 / -1; }
label .u { color: var(--text-3); font-weight: 400; }
label.check { display: flex; flex-direction: row; align-items: center; gap: 8px; align-self: end; height: 32px; }
label.check input { width: 16px; height: 16px; accent-color: var(--accent); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.hint { margin: 0; font-size: 12px; color: var(--text-3); }
.hint:empty { display: none; }
[hidden] { display: none !important; }

.layout-picker { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.layout-picker button {
  font: inherit; font-size: 12px; font-weight: 500; color: var(--text-2);
  display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 8px 4px 6px;
  border: 1px solid var(--border-strong); border-radius: 8px; background: var(--surface); cursor: pointer;
}
.layout-picker svg { width: 60px; height: 36px; }
.layout-picker path { fill: none; stroke: var(--text-3); stroke-width: 3; stroke-linejoin: round; }
.layout-picker button[aria-checked="true"] { border-color: var(--accent); background: var(--accent-soft); color: var(--text); }
.layout-picker button[aria-checked="true"] path { stroke: var(--accent); }

/* ---------- Results ---------- */
.results { display: flex; flex-direction: column; gap: 14px; min-width: 0; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow); min-width: 0;
}
.card > h2 { margin-bottom: 10px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 8px; }
.card-note { margin: -4px 0 10px; color: var(--text-3); font-size: 12px; }
.grid-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 14px; }

.hero { display: flex; gap: 20px; align-items: center; justify-content: space-between; flex-wrap: wrap; }
.hero-label { color: var(--text-2); font-weight: 500; }
.hero-value { font-size: 40px; font-weight: 650; letter-spacing: -.02em; font-variant-numeric: tabular-nums; line-height: 1.15; }
.hero-sub { color: var(--text-3); font-size: 12px; }
.hero-side { display: grid; grid-template-columns: repeat(3, auto); gap: 4px 24px; margin: 0; }
.hero-side div { display: flex; flex-direction: column; }
.hero-side dt { color: var(--text-3); font-size: 12px; }
.hero-side dd { margin: 0; font-size: 18px; font-weight: 600; font-variant-numeric: tabular-nums; }
.hero.invalid .hero-value { color: var(--text-3); }

/* Heat sources editor */
.src { border: 1px solid var(--border); border-radius: 8px; padding: 10px; display: flex; flex-direction: column; gap: 8px; background: var(--surface-2); }
.src-head { display: flex; gap: 6px; align-items: center; }
.src-head input[type="text"] { font: inherit; font-weight: 600; flex: 1; min-width: 0; height: 30px; padding: 4px 8px; border: 1px solid var(--border-strong); border-radius: 7px; background: var(--surface); color: var(--text); }
.icon-btn { flex: none; width: 30px; height: 30px; border: 1px solid var(--border-strong); background: var(--surface); color: var(--text-2); border-radius: 7px; cursor: pointer; font-size: 14px; line-height: 1; }
.icon-btn:hover { color: var(--text); background: var(--surface); border-color: var(--text-3); }
.grid4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.tim-row label { grid-template-columns: 1fr; }
#sources-list { display: flex; flex-direction: column; gap: 10px; }

/* Source results table */
.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
table.data th { text-align: right; font-weight: 500; color: var(--text-3); font-size: 12px; padding: 6px 8px; border-bottom: 1px solid var(--border-strong); white-space: nowrap; }
table.data td { text-align: right; padding: 7px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.data th:first-child, table.data td:first-child { text-align: left; }
table.data td.strong { font-weight: 600; }
.status { font-weight: 600; }
.status.ok { color: var(--ok); }
.status.over { color: var(--err); }

.issues { display: flex; flex-direction: column; gap: 6px; padding: 12px; }
.issue { display: flex; gap: 8px; align-items: flex-start; padding: 7px 10px; border-radius: 7px; font-size: 13px; }
.issue .tag { flex: none; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; padding-top: 1px; min-width: 44px; }
.issue.error { background: var(--err-bg); } .issue.error .tag { color: var(--err); }
.issue.warn { background: var(--warn-bg); } .issue.warn .tag { color: var(--warn); }
.issue.info { background: var(--info-bg); } .issue.info .tag { color: var(--info); }

.drawing svg, .section svg { display: block; width: 100%; height: auto; }
.drawing { max-height: 460px; display: flex; justify-content: center; }
.drawing svg { max-height: 460px; width: auto; max-width: 100%; }
.section-wrap { margin-top: 10px; border-top: 1px solid var(--border); padding-top: 8px; }
.section-title { font-size: 12px; color: var(--text-3); margin-bottom: 4px; }
.section svg { max-height: 150px; }
.temp-legend { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--text-2); font-variant-numeric: tabular-nums; }
.map-wrap { position: relative; }
.map-wrap svg { display: block; width: 100%; height: auto; max-height: 460px; }
.temp-legend .ramp { width: 120px; height: 8px; border-radius: 4px; background: linear-gradient(90deg, var(--cold), var(--hot)); }

svg .plate { fill: var(--plate); stroke: var(--plate-edge); stroke-width: 1; }
svg .dim { stroke: var(--text-3); stroke-width: 1; fill: none; }
svg .dim-text { fill: var(--text-2); font-size: 12px; font-variant-numeric: tabular-nums; }
svg .port { fill: var(--surface); stroke-width: 2; }
svg .port-label { font-size: 11px; font-weight: 600; fill: var(--text); }
svg .header-rect { stroke: none; }
svg .heat-arrow { stroke: var(--hot); stroke-width: 1.5; fill: none; }
svg .heat-head { fill: var(--hot); }
svg .src-rect { fill: color-mix(in srgb, var(--surface) 70%, transparent); stroke: var(--text); stroke-width: 1.5; stroke-dasharray: 4 2; }
svg .draggable { cursor: grab; }
svg .draggable:active { cursor: grabbing; }
svg .src-label { fill: var(--text); font-weight: 600; paint-order: stroke; stroke: var(--surface); stroke-width: 3px; stroke-linejoin: round; pointer-events: none; }
svg .src-label.sub { font-weight: 500; }
svg .src-tag { fill: rgba(255, 255, 255, .9); stroke: rgba(0, 0, 0, .25); stroke-width: 1; }
svg .src-label.plain { fill: #111; stroke: none; }
svg .src-outline { fill: none; stroke: #111; stroke-width: 1.5; }
svg .map-frame { fill: none; stroke: var(--plate-edge); stroke-width: 1; }
svg .map-channel { fill: none; stroke: rgba(255, 255, 255, .28); stroke-linecap: butt; stroke-linejoin: round; }
svg .hot-mark { fill: none; stroke: #111; stroke-width: 2; }
svg .hot-dot { fill: #111; }
.hot-key { color: var(--text-3); margin-left: 6px; }
svg .hot-label { fill: #111; font-size: 12px; font-weight: 700; paint-order: stroke; stroke: rgba(255,255,255,.85); stroke-width: 3px; stroke-linejoin: round; }
svg .channel-cut { fill: var(--accent-soft); stroke: var(--accent); stroke-width: 1; }

.metrics { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 1px; background: var(--border); border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.metric { background: var(--surface); padding: 10px 12px; }
.metric .k { font-size: 12px; color: var(--text-3); }
.metric .v { font-size: 16px; font-weight: 600; font-variant-numeric: tabular-nums; }
.metric .s { font-size: 11px; color: var(--text-3); }

.stack { display: flex; height: 22px; gap: 2px; border-radius: 4px; overflow: hidden; margin-bottom: 10px; }
.stack div { min-width: 2px; }
.stack-legend { display: grid; grid-template-columns: auto 1fr auto auto; gap: 4px 10px; font-size: 13px; align-items: center; }
.stack-legend .sw { width: 10px; height: 10px; border-radius: 2px; }
.stack-legend .val { text-align: right; font-variant-numeric: tabular-nums; }
.stack-legend .pct { text-align: right; color: var(--text-3); font-variant-numeric: tabular-nums; min-width: 38px; }

.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .axis text { fill: var(--text-3); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--border-strong); stroke-width: 1; }
.chart .line { fill: none; stroke: var(--s1); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .area { fill: var(--s1); opacity: .08; }
.chart .op { fill: var(--s1); stroke: var(--surface); stroke-width: 2; }
.chart .op-label { fill: var(--text-2); font-size: 11px; font-weight: 600; paint-order: stroke; stroke: var(--surface); stroke-width: 4px; stroke-linejoin: round; }
.chart .cross { stroke: var(--text-3); stroke-width: 1; stroke-dasharray: 3 3; }
.chart .hover-dot { fill: var(--s1); stroke: var(--surface); stroke-width: 2; }
.chart .axis-title { fill: var(--text-3); font-size: 11px; }
.tooltip {
  position: absolute; pointer-events: none; background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: 6px; padding: 6px 8px; font-size: 12px; box-shadow: 0 4px 12px rgba(0,0,0,.12); white-space: nowrap;
  font-variant-numeric: tabular-nums; z-index: 2;
}
.tooltip b { font-size: 13px; }
.tooltip .muted { color: var(--text-3); }

.notes summary { cursor: pointer; font-weight: 600; }
.notes-body { color: var(--text-2); font-size: 13px; }
.notes-body ul { padding-left: 18px; }
.notes-body li { margin-bottom: 6px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: 8px; font-size: 13px; z-index: 20;
}

@media (max-width: 1000px) {
  .layout { grid-template-columns: 1fr; padding: 16px; }
  .inputs { order: 0; }
}
@media (max-width: 640px) {
  .grid4 { grid-template-columns: 1fr 1fr; }
  .topbar { padding: 10px 16px; position: static; }
  .grid-cards { grid-template-columns: 1fr; }
  .hero-value { font-size: 32px; }
  .hero-side { grid-template-columns: repeat(2, auto); }
  .grid3 { grid-template-columns: 1fr 1fr; }
  .preset select { max-width: 100%; }
  .toolbar { width: 100%; }
}

@media print {
  .topbar .toolbar, .toast { display: none; }
  .topbar { position: static; }
  body { background: #fff; }
  .layout { grid-template-columns: 300px 1fr; padding: 0; gap: 12px; }
  .card, fieldset { box-shadow: none; break-inside: avoid; }
  .notes { display: block; }
  .notes[open] summary ~ *, .notes summary ~ * { display: block; }
}
.map-note { margin: 8px 0 0; }
.temp-legend { flex-wrap: wrap; }
.temp-legend > span { white-space: nowrap; }

/* Pump curve inputs */
.pump { display: flex; flex-direction: column; gap: 10px; }
.pump-table { display: flex; flex-direction: column; gap: 6px; }
.pt-row { display: grid; grid-template-columns: 1fr 1fr 30px; gap: 8px; align-items: center; }
.pt-head { font-size: 12px; color: var(--text-2); font-weight: 500; }
#pump-points { display: flex; flex-direction: column; gap: 6px; }
.pump-op { color: var(--text-2); font-weight: 500; }

/* Multi-series charts */
.chart .line.s2 { stroke: var(--s2); }
.chart .bar { fill: var(--s1); }
.chart .ref-line { stroke: var(--text-2); stroke-width: 1.5; stroke-dasharray: 5 4; }
.dist-summary { margin: -4px 0 10px; font-size: 13px; color: var(--text-2); }
.dist-summary b { color: var(--text); }
.chart .line.s3 { stroke: var(--s3); stroke-dasharray: 5 4; }
.chart .hover-dot.s2 { fill: var(--s2); }
.chart .hover-dot.s3 { fill: var(--s3); }
.chart .series-label { fill: var(--text-2); font-size: 11px; font-weight: 600; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 12px; color: var(--text-2); margin-bottom: 4px; }
.chart-legend span, .tooltip div { display: inline-flex; align-items: center; gap: 6px; }
.tooltip div { display: flex; }
.key { display: inline-block; width: 14px; height: 0; border-top: 2px solid var(--s1); }
.key.s2 { border-color: var(--s2); }
.key.s3 { border-color: var(--s3); border-top-style: dashed; }
