:root {
  --navy: #0b2545;
  --navy-2: #13315c;
  --gold: #c9a646;
  --bg: #f4f6f9;
  --card: #ffffff;
  --ink: #1b2433;
  --muted: #5b667a;
  --line: #dde3ec;
  --red: #b3261e;
  --green: #1f7a4d;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink); background: var(--bg);
  display: flex; flex-direction: column; height: 100vh; height: 100dvh;
}
.top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 20px; background: var(--navy); color: #fff;
  border-bottom: 3px solid var(--gold);
}
.brand { display: flex; align-items: baseline; gap: 14px; flex-wrap: wrap; }
.logo { font-weight: 800; font-size: 1.25rem; letter-spacing: .3px; }
.logo::first-letter { color: var(--gold); }
.module { opacity: .85; font-size: .95rem; }
.version {
  font: 600 .8rem ui-monospace, Consolas, monospace; background: var(--gold);
  color: var(--navy); padding: 3px 8px; border-radius: 999px;
}

.layout {
  flex: 1; display: grid; grid-template-columns: minmax(0, 1fr) 340px;
  grid-template-rows: minmax(0, 1fr);
  gap: 16px; padding: 16px; min-height: 0;
}
.leftcol { display: flex; flex-direction: column; gap: 12px; min-height: 0; min-width: 0; }
.stage {
  flex: 1;
  position: relative; background: linear-gradient(#ffffff, #eef3f9);
  border: 1px solid var(--line); border-radius: 14px; overflow: hidden;
  min-height: 0;
}
#sim { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; }
#sim.dragging { cursor: grabbing !important; }
.hint {
  position: absolute; left: 14px; top: 12px; font-size: .85rem; color: var(--muted);
  background: rgba(255,255,255,.8); padding: 4px 10px; border-radius: 8px; pointer-events: none;
}

.panel { display: flex; flex-direction: column; gap: 12px; overflow-y: auto; min-height: 0; padding-right: 2px; }
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px;
}
.card h2 {
  margin: 0 0 8px; font-size: .8rem; text-transform: uppercase; letter-spacing: 1px; color: var(--navy-2);
}
select, button {
  font: inherit; font-size: .9rem; border-radius: 8px; border: 1px solid var(--line);
  padding: 6px 8px; background: #fff; color: var(--ink);
}
.card > select { width: 100%; }
.row { margin-top: 10px; }
.row label { display: block; font-size: .85rem; color: var(--muted); margin-bottom: 2px; }
.row label b { color: var(--ink); }
input[type=range] { width: 100%; accent-color: var(--navy); }

.readouts {
  display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; margin: 0; font-size: .9rem;
}
.readouts dt { color: var(--muted); }
.readouts dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.readouts dt.w { color: var(--red); }
.readouts dt.fb { color: var(--green); }
.readouts .status { color: var(--navy); }

.btns { display: flex; gap: 8px; margin-bottom: 8px; }
.btns button {
  flex: 1; background: var(--navy); color: #fff; border-color: var(--navy); cursor: pointer; font-weight: 600;
}
.btns button:hover { background: var(--navy-2); }
.btns button.ghost { background: #fff; color: var(--navy); }
.chk { display: flex; align-items: center; gap: 8px; font-size: .9rem; margin-top: 6px; }
.chk select { margin-left: auto; }

.theory summary { cursor: pointer; font-weight: 700; color: var(--navy-2); }
.theory p { font-size: .88rem; line-height: 1.45; }
.theory code { background: #f1f4f8; padding: 2px 6px; border-radius: 6px; }

.foot { text-align: center; font-size: .78rem; color: var(--muted); padding: 8px 0 14px; }

@media (max-width: 860px) {
  body { height: auto; }
  .layout { grid-template-columns: 1fr; grid-template-rows: auto; }
  .stage { min-height: 60vh; }
  .panel { max-height: none; }
}

/* v0.2 additions */
.try { border-color: var(--gold); background: #fffaf0; }
.scale { display: flex; justify-content: space-between; font-size: .72rem; color: var(--muted); margin-top: 2px; }
.verdict {
  font-weight: 700; font-size: .9rem; padding: 6px 10px; border-radius: 8px; margin-bottom: 8px;
  background: #eef1f5; color: var(--navy); text-align: center;
}
.verdict.ok { background: #e3f4ea; color: var(--green); }
.verdict.bad { background: #fbe7e5; color: var(--red); }
.verdict.meh { background: #fff4d6; color: #8a6d16; }
.readouts dt.m { color: #7b3fb8; font-weight: 600; }
.btns button { font-size: .85rem; padding: 6px 4px; }
.hint b { color: var(--navy); }
/* spec card — wide stage: right column under the FBD; narrow: strip at the bottom */
.spec { position: absolute; font-size: .76rem; line-height: 1.35; background: rgba(255,255,255,.95);
  border: 1px solid var(--line); border-top: 3px solid var(--gold); padding: 6px 9px; border-radius: 10px;
  color: var(--ink); pointer-events: none; overflow: auto; }
.spec b { color: var(--navy); } .spec .mut { color: var(--muted); }
.spech { font-weight: 700; font-size: .7rem; letter-spacing: .6px; text-transform: uppercase; color: #13315c; margin-bottom: 3px; }
.spec dl { display: grid; grid-template-columns: auto 1fr; gap: 1px 8px; margin: 0; }
.spec dt { color: var(--muted); white-space: nowrap; } .spec dd { margin: 0; }
.spec .sn { margin-top: 4px; }
.spec .hc { margin-top: 5px; padding-top: 4px; border-top: 1px dashed var(--line); font-family: ui-monospace, Consolas, monospace; font-size: .72rem; }
.stage.wide .spec { right: 12px; top: 216px; width: 214px; max-height: calc(100% - 228px); pointer-events: auto; }
.stage.wide.twocol .spec { right: 238px; top: 12px; max-height: calc(100% - 24px); }

/* v0.3 additions */
.mt { margin-top: 8px; width: 100%; }
.mt2 { margin-top: 8px; }
.re-lam { color: #1f7a4d; } .re-mid { color: #8a6d16; } .re-turb { color: #b3261e; }
.gzc { width: 100%; height: 170px; display: block; }
.gzinfo { font-size: .82rem; margin: 6px 0 2px; color: var(--ink); line-height: 1.4; }
.gzlegend { font-size: .72rem; color: var(--muted); margin: 2px 0 0; }
.gzlegend .k { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 4px; vertical-align: -1px; }
.k.nav { background: #0b2545; } .k.pur { background: #7b3fb8; } .k.grn { background: #1f7a4d; } .k.gld { background: #c9a646; }

/* v0.4 additions */
.note { font-size: .75rem; color: var(--muted); margin-top: 4px; }
.warn { font-size: .8rem; background: #e8f2fc; color: #13315c; border-radius: 8px; padding: 6px 8px; margin-bottom: 8px; }
.bail { width: 100%; margin-bottom: 6px; background: #e8f2fc; border-color: #b9d3ee; color: #13315c; font-weight: 600; cursor: pointer; }

/* v0.5 lab notebook */
.topright { display: flex; align-items: center; gap: 10px; }
.labtoggle { background: transparent; color: #fff; border: 1px solid rgba(255,255,255,.4); border-radius: 999px; padding: 4px 12px; cursor: pointer; font-weight: 600; }
.labtoggle:hover { background: rgba(255,255,255,.1); }
.labtoggle.on { background: var(--gold); color: var(--navy); border-color: var(--gold); }
.lab { flex: 0 0 42%; min-height: 0; overflow: auto; background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 10px 12px; display: flex; flex-direction: column; gap: 8px; }
.lab[hidden] { display: none; }
.labhead { display: flex; align-items: center; gap: 8px; }
.labhead select { font-weight: 600; max-width: 60%; }
.labhead .spacer { flex: 1; }
.labcount { font-size: .78rem; color: var(--muted); }
.labbody { flex: 1; min-height: 150px; display: grid; grid-template-columns: minmax(0, 1.35fr) minmax(0, 1fr); gap: 12px; }
.lableft { display: flex; flex-direction: column; gap: 6px; min-height: 0; }
.labsteps ol { margin: 0; padding-left: 20px; font-size: .84rem; line-height: 1.4; }
.labbtns { display: flex; flex-wrap: wrap; gap: 6px; }
.labbtns button { cursor: pointer; font-size: .82rem; }
.labbtns .rec { background: #8a8f98; color: #fff; border-color: #8a8f98; font-weight: 700; }
.labbtns .rec.ready { background: #b3261e; border-color: #b3261e; }
.labbtns .ghost, .labhead .ghost { background: #fff; color: var(--navy); }
.labbtns .danger { color: var(--red); }
.labmsg { font-size: .8rem; min-height: 1.1em; color: var(--muted); }
.labmsg.ok { color: var(--green); } .labmsg.warn { color: #8a6d16; }
.labtablewrap { flex: 1; min-height: 60px; overflow: auto; border: 1px solid var(--line); border-radius: 8px; }
.labtable { width: 100%; border-collapse: collapse; font-size: .78rem; font-variant-numeric: tabular-nums; }
.labtable th { position: sticky; top: 0; background: #eef2f7; color: var(--navy-2); text-align: right; padding: 4px 6px; white-space: nowrap; font-weight: 700; }
.labtable td { text-align: right; padding: 3px 6px; border-top: 1px solid #eef1f5; white-space: nowrap; }
.labtable th:first-child, .labtable td:first-child { text-align: left; }
.labtable tr.unsettled td { color: #8a6d16; font-style: italic; }
.labtable td.empty { text-align: center; color: var(--muted); padding: 14px; }
.labtable .del { border: none; background: none; color: #9aa6b8; cursor: pointer; padding: 0 4px; font-size: 1rem; }
.labtable .del:hover { color: var(--red); }
.labright { display: flex; flex-direction: column; min-height: 0; }
.labchart { width: 100%; flex: 1; min-height: 120px; display: block; }
.labsummary { font-size: .8rem; margin: 6px 0 0; line-height: 1.4; }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin: 0 3px 0 6px; vertical-align: 0; }
.dot.g { background: #1f7a4d; } .dot.r { background: #d6453d; }
@media (max-width: 860px) {
  .lab { flex: none; }
  .labbody { grid-template-columns: 1fr; }
  .labchart { height: 200px; flex: none; }
  .labtablewrap { max-height: 240px; }
}
.labsteps summary { cursor: pointer; font-weight: 700; font-size: .8rem; color: var(--navy-2); }
.labsteps details:not([open]) summary { color: var(--muted); }

/* v0.5.2 salvage banner */
.sunk { position: absolute; left: 14px; top: 40px; max-width: min(460px, calc(100% - 262px));
  display: flex; align-items: center; gap: 8px; background: rgba(255,255,255,.96); border: 1px solid #f0c9c5;
  border-left: 4px solid var(--red); border-radius: 8px; padding: 5px 8px; box-shadow: 0 3px 10px rgba(11,37,69,.12);
  font-size: .78rem; line-height: 1.35; color: var(--ink); }
.sunk button { padding: 4px 10px; font-size: .8rem; }
@media (max-width: 860px) { .sunk { top: 10px; max-width: calc(100% - 28px); } }
.sunk[hidden] { display: none; }
.sunk button { flex: none; background: var(--navy); color: #fff; border-color: var(--navy); font-weight: 700; cursor: pointer; }
@media (max-height: 760px) and (min-width: 861px) { .spec { font-size: .7rem; } }



/* ---------- print: a clean results sheet built at print time (#printHead) ---------- */
.printonly { display: none; }
@media print {
  @page { size: letter landscape; margin: 11mm; }
  html, body { height: auto !important; background: #fff; display: block; }
  .top, .layout, .foot { display: none !important; }
  .printonly { display: block; color: #1b2433; font-size: 9.5pt; }
  .printonly h1 { font-size: 15pt; margin: 0; color: #0b2545; }
  .printonly h2 { font-size: 11pt; margin: 10px 0 4px; color: #13315c; border-bottom: 1px solid #dde3ec; }
  .printonly .pmeta { font-size: 8.5pt; color: #5b667a; margin: 2px 0 8px; }
  .printonly .prow { display: flex; gap: 12px; align-items: flex-start; break-inside: avoid; }
  .printonly .ptank { width: 62%; height: auto; border: 1px solid #dde3ec; border-radius: 6px; }
  .printonly .pspec { flex: 1; font-size: 8.5pt; }
  .printonly .pspec dl { display: grid; grid-template-columns: auto 1fr; gap: 1px 8px; margin: 0; }
  .printonly .pspec dd { margin: 0; } .printonly .pspec dt { color: #5b667a; }
  .printonly .pspec .hc { font-family: ui-monospace, Consolas, monospace; margin-top: 6px; }
  .printonly .pchart { width: 55%; height: auto; border: 1px solid #dde3ec; border-radius: 6px; }
  .printonly .psum { flex: 1; }
  .printonly ol { margin: 0; padding-left: 18px; }
  .printonly .labtable { width: 100%; font-size: 8.5pt; border-collapse: collapse; }
  .printonly .labtable th { background: #eef2f7; position: static; padding: 3px 5px; }
  .printonly .labtable td { padding: 2px 5px; border-top: 1px solid #e3e8ef; }
  .printonly .dot { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

/* short screens (PiKVM / 720p): give the notebook more room; it scrolls inside itself, never under the footer */
@media (max-height: 760px) and (min-width: 861px) {
  .lab { flex-basis: 52%; padding: 8px 10px; gap: 6px; }
  .labbtns button { padding: 4px 8px; }
}

/* tiny screens with the notebook open: never squash the tank below 300 px — the left column scrolls instead */
.leftcol { overflow-y: auto; }
.stage { min-height: 300px; }
.lab { min-height: 230px; }
.sunk { z-index: 3; }
.stage.wide.twocol .sunk { max-width: calc(100% - 490px); }
@media (max-width: 860px) { .stage { min-height: 60vh; } }

/* v0.6.2: rock-steady readouts — a fixed-width value column means labels never re-wrap as numbers change
   (stops the side panel bouncing on iPad). Status gets its own full-width, fixed-height slot. */
.readouts { grid-template-columns: minmax(0, 1fr) 7.6em; }
.readouts dd { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-variant-numeric: tabular-nums; }
.readouts dt { align-self: start; }
.readouts dd.status { grid-column: 1 / -1; text-align: left; white-space: normal; height: 2.7em; line-height: 1.35; overflow: hidden; margin-bottom: 2px; }
.sunk.info { border-color: #b9d3ee; border-left-color: #3a8fd9; }
.sunk button[hidden] { display: none; }

/* v0.6.3 sea state */
#seaWrap { margin-top: 10px; padding-top: 8px; border-top: 1px dashed var(--line, #d6dde8); }
#seaWrap .chk { margin-top: 0; font-weight: 600; }
#seaBody { margin-top: 4px; }
#resBadge { height: 2.1em; display: flex; align-items: center; justify-content: center; margin: 8px 0; }

/* ---------- v0.7: multi-lab site ---------- */
a.logo { color: #fff; text-decoration: none; }
.back { color: rgba(255,255,255,.75); text-decoration: none; font-size: .85rem; border: 1px solid rgba(255,255,255,.3); border-radius: 999px; padding: 2px 10px; }
.back:hover { background: rgba(255,255,255,.12); color: #fff; }
.mut { color: var(--muted); font-weight: 400; }

/* Module 2 · tank draining */
.runbar { position: absolute; right: 12px; top: 10px; display: flex; gap: 6px; z-index: 2; }
.runbar button { cursor: pointer; font-weight: 700; }
.runbar .go { background: var(--navy); color: #fff; border-color: var(--navy); min-width: 104px; }
.runbar .go.on { background: var(--gold); color: var(--navy); border-color: var(--gold); }
.runbar .ghost { background: #fff; color: var(--navy); }
.inlsel { font-size: .75rem; padding: 1px 4px; text-transform: none; letter-spacing: 0; font-weight: 700; color: var(--navy-2); }
.spec2 { font-size: .8rem; line-height: 1.4; }
.spec2 dl { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; margin: 0; }
.spec2 dt { color: var(--muted); white-space: nowrap; } .spec2 dd { margin: 0; }
.spec2 .hc { margin-top: 6px; padding-top: 5px; border-top: 1px dashed var(--line); font-family: ui-monospace, Consolas, monospace; font-size: .74rem; }
.chk.inl { margin: 0; font-size: .8rem; gap: 4px; }
.chk.inl select { margin: 0; padding: 1px 3px; font-size: .8rem; }
.labbtns button:disabled { opacity: .45; cursor: not-allowed; }
.drain .lab { flex-basis: 46%; }
@media (max-width: 860px) { .runbar { top: auto; bottom: 10px; right: 10px; } }

/* ---------- v0.7.1: phone layout ----------
   Header: one compact block. Module 1 stage (.phone, set by the renderer when the stage is < 640 px wide):
   tank on top, full width, cropped to the tank; FBD (drawn on the canvas, left) + spec card (right) in a strip
   underneath, so nothing ever sits on top of the tank. The renderer sets the stage height and --tankh / --strip. */
[hidden] { display: none !important; }
.phone-only { display: none; }
@media (max-width: 600px) {
  .wide-only { display: none; }
  .phone-only { display: inline; }
  .top { padding: 8px 12px; gap: 8px; }
  .brand:has(.back) { display: grid; grid-template-columns: auto minmax(0, 1fr); grid-template-areas: "back logo" "back module";
    column-gap: 10px; row-gap: 0; align-items: center; flex: 1; min-width: 0; }
  .brand .logo { grid-area: logo; font-size: 1.05rem; line-height: 1.25; }
  .brand .back { grid-area: back; font-size: 1.05rem; padding: 3px 11px; color: #fff; }
  .brand .module { grid-area: module; font-size: .72rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .topright { gap: 6px; flex: none; }
  .labtoggle { padding: 5px 11px; font-size: .85rem; }
  .version { font-size: .7rem; padding: 2px 6px; }
  .layout { padding: 10px; gap: 10px; }
}
.stage.phone { flex: none; min-height: 0; }
.stage.phone #sim { touch-action: pan-y; }
.stage.phone .hint { left: 8px; top: 8px; font-size: .74rem; padding: 3px 8px; }
.stage.phone .spec { left: calc(50% + 3px); right: 8px; top: calc(var(--tankh, 300px) + 2px); bottom: auto;
  height: calc(var(--strip, 200px) - 10px); max-height: none; font-size: .7rem; padding: 6px 8px; pointer-events: auto; }
.stage.phone .spec dl { grid-template-columns: auto 1fr; gap: 0 6px; }
.stage.phone .spec .hc { font-size: .66rem; }
.stage.phone .sunk { top: 36px; left: 8px; right: 8px; max-width: none; font-size: .74rem; }
/* phone: notebook must never be wider than the screen (the table scrolls sideways inside its own box) */
@media (max-width: 860px) {
  .labbody { grid-template-columns: minmax(0, 1fr); }
  .lableft, .labright { min-width: 0; }
}
@media (max-width: 600px) {
  .labhead { flex-wrap: wrap; row-gap: 6px; }
  .labhead select { flex: 1 1 100%; max-width: 100%; min-width: 0; width: 100%; }
  .labhead button { white-space: nowrap; }
}
/* ---------- v0.8.0: Module 3 (viscometer) ---------- */
.visco .lab { flex-basis: 46%; }
.mystery { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.mystery button { cursor: pointer; font-weight: 600; }
b.warn { color: var(--red); }
