:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --text: #1c2530;
  --muted: #667281;
  --line: #dde3ea;
  --brand: #0f6e8c;
  --brand-dark: #0b566e;
  --ok: #1d7a46;
  --warn: #9a6400;
  --warn-bg: #fff7e6;
  --danger: #b42318;
  --radius: 8px;
}

* { box-sizing: border-box; }
body { margin: 0; font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.6rem; margin: 0 0 .5rem; }
h2 { font-size: 1.15rem; margin: 0 0 .75rem; }
h3 { font-size: 1rem; margin: 1.25rem 0 .5rem; }
code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .9em; }
pre { background: #f0f3f6; padding: .75rem; border-radius: 6px; overflow-x: auto; }
pre.small { font-size: .8rem; }

/* Layout */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap;
  padding: .6rem clamp(1rem, 3vw, 1.5rem); background: var(--surface); border-bottom: 1px solid var(--line);
  box-shadow: 0 1px 3px rgba(16, 42, 58, .06); }
.brand { display: flex; align-items: center; gap: 1rem; color: var(--text); }
.brand:hover { text-decoration: none; }
.brand-logo { height: 40px; width: auto; display: block; }
.brand-divider { width: 1px; height: 28px; background: var(--line); }
.brand-name { font-weight: 700; font-size: 1.1rem; color: #1b2135; white-space: nowrap; }
.brand-name span { font-weight: 400; color: var(--muted); margin-left: .3rem; }
.topnav { display: flex; align-items: center; gap: 1.25rem; flex-wrap: wrap; }
.topnav a { color: var(--text); }
.topnav a:hover { color: var(--brand); }
.topnav .who { color: var(--muted); font-size: .85rem; }
.topnav form { margin: 0; }
button.link { background: none; border: 0; color: var(--text); cursor: pointer; font: inherit; padding: 0; }
button.link:hover { color: var(--brand); }
@media (max-width: 560px) {
  .brand { gap: .6rem; }
  .brand-logo { height: 30px; }
  .brand-divider { height: 22px; }
  .brand-name { font-size: .95rem; }
  .brand-name span { display: none; }
}
.page { max-width: 1200px; margin: 0 auto; padding: 1.5rem clamp(1rem, 3vw, 1.5rem) 3rem; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin: 1rem 0; }
.page-head h1, .page-head h2 { margin: 0; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 420px), 1fr)); gap: 1rem; align-items: start; }

/* Cards */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.25rem; margin-bottom: 1rem; }
.card.narrow { max-width: 640px; }
.card.empty { text-align: center; padding: 2.5rem; }
.card.warn { background: var(--warn-bg); border-color: #f1d49b; }
.card.keybox { border-color: var(--brand); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: .5rem; }
.card-head h2 { margin: 0; }
.lead { color: var(--muted); max-width: 70ch; }
.muted { color: var(--muted); }
.crumb { color: var(--muted); margin: 0 0 .25rem; font-size: .9rem; }
.meta { color: var(--muted); margin: .25rem 0 0; }

/* Plant header & tabs */
.plant-header { margin-bottom: .75rem; }
.tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--line); margin-bottom: 1.25rem; overflow-x: auto; }
.tabs a { padding: .55rem .9rem; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs a:hover { color: var(--text); text-decoration: none; }
.tabs a.active { color: var(--brand); border-bottom-color: var(--brand); font-weight: 600; }

/* Tables */
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 1rem; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: .6rem .75rem; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; background: #f8fafb; }
tr:last-child td { border-bottom: 0; }
tr.inactive td { opacity: .55; }
.row-actions { white-space: nowrap; text-align: right; }
.row-actions form { display: inline; }
table.matrix .checks ul, table.matrix .checks div { display: flex; flex-wrap: wrap; gap: .25rem 1rem; list-style: none; margin: 0; padding: 0; }
table.matrix .checks label { font-weight: 400; display: inline-flex; gap: .35rem; align-items: center; }

/* Buttons */
.btn { display: inline-block; padding: .5rem .9rem; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); color: var(--text); font: inherit; cursor: pointer; line-height: 1.2; }
.btn:hover { background: #f0f3f6; text-decoration: none; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-dark); }
.btn.danger { color: var(--danger); }
.btn.small { padding: .3rem .6rem; font-size: .85rem; }
.button-row { display: flex; flex-wrap: wrap; gap: .5rem; }
.button-row form { margin: 0; }
.actions { margin-top: 1rem; display: flex; gap: .5rem; }

/* Forms */
.field { margin-bottom: 1rem; }
.field label { display: block; font-weight: 600; margin-bottom: .3rem; }
.field input[type=text], .field input[type=number], .field input[type=email], .field input[type=password],
.field input[type=time], .field input:not([type]), .field select, .field textarea {
  width: 100%; padding: .5rem .6rem; border: 1px solid #c8d1db; border-radius: 6px; font: inherit; background: #fff;
}
.field select[multiple] { min-height: 6rem; }
.field ul, .field div > div { list-style: none; padding: 0; margin: 0; }
.field ul label, .field div > div label { font-weight: 400; display: inline-flex; gap: .4rem; align-items: center; }
.field .help { display: block; color: var(--muted); margin-top: .25rem; }
.field .error, .errors { color: var(--danger); }
.field.has-error input, .field.has-error select { border-color: var(--danger); }
.req { color: var(--danger); }
.errors { background: #fdecea; padding: .5rem .75rem; border-radius: 6px; margin-bottom: 1rem; }
.errors p { margin: 0; }
form.inline select { padding: .35rem .5rem; border-radius: 6px; border: 1px solid #c8d1db; font: inherit; }

/* Messages */
.messages { list-style: none; padding: 0; margin: 0 0 1rem; }
.msg { padding: .65rem .9rem; border-radius: 6px; margin-bottom: .5rem; background: #e8f1fb; }
.msg.success { background: #e7f5ed; color: var(--ok); }
.msg.error { background: #fdecea; color: var(--danger); }
.msg.warning { background: var(--warn-bg); color: var(--warn); }

/* Tags & statuses */
.tag { display: inline-block; padding: .1rem .5rem; border-radius: 999px; background: #e6f0f4; color: var(--brand-dark); font-size: .8rem; }
.tag.small { font-size: .72rem; }
.status { display: inline-block; padding: .1rem .5rem; border-radius: 999px; font-size: .8rem; background: #eef1f4; color: var(--muted); }
.status-live, .status-active { background: #e7f5ed; color: var(--ok); }
.status-trial, .status-draft { background: var(--warn-bg); color: var(--warn); }
.state { font-weight: 600; font-size: .85rem; }
.state-on { color: var(--ok); }
.state-trip { color: var(--danger); }
.state-off, .state-unknown { color: var(--muted); }
.sev { font-size: .85rem; font-weight: 600; }
.sev-critical { color: var(--danger); }
.sev-major { color: #c4320a; }
.sev-minor { color: var(--warn); }
.sev-info { color: var(--muted); }

/* Plant overview */
.checklist { list-style: none; padding: 0; margin: 0; }
.checklist li { padding: .4rem 0; border-bottom: 1px solid var(--line); }
.checklist li:last-child { border-bottom: 0; }
.checklist .tick { display: inline-block; width: 1.25rem; color: var(--muted); }
.checklist li.done .tick { color: var(--ok); font-weight: 700; }
.details { display: grid; grid-template-columns: max-content 1fr; gap: .35rem 1rem; margin: 0; }
.details dt { color: var(--muted); }
.details dd { margin: 0; }
.unit-grid { display: flex; flex-wrap: wrap; gap: .5rem; }
.unit-chip { border: 1px solid var(--line); border-radius: 6px; padding: .5rem .75rem; display: flex; flex-direction: column; min-width: 110px; }
.unit-chip small { color: var(--muted); }

/* Technology & rule type pickers */
.tech-grid, .rule-type-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 260px), 1fr)); gap: 1rem; }
.tech-card, .rule-type { display: block; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.25rem; color: var(--text); }
.tech-card:hover, .rule-type:hover { border-color: var(--brand); text-decoration: none; box-shadow: 0 2px 8px rgba(15, 110, 140, .12); }
.tech-card h3 { margin: 0 0 .35rem; color: var(--brand-dark); }
.tech-card p { margin: 0; color: var(--muted); }
.rule-type strong { display: block; }
.rule-type small { color: var(--muted); }

.pager { display: flex; align-items: center; gap: 1rem; justify-content: center; margin-top: 1rem; }
form.activate { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
form.activate label { color: var(--muted); font-size: .85rem; display: flex; align-items: center; gap: .35rem; }
form.activate input { padding: .35rem .5rem; border: 1px solid #c8d1db; border-radius: 6px; font: inherit; }

/* Compliance */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 150px), 1fr)); gap: 1rem; margin-bottom: 1rem; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: .9rem 1.1rem; display: flex; flex-direction: column; }
.kpi-label { color: var(--muted); font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; }
.kpi-value { font-size: 1.6rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.kpi-value.small { font-size: 1.05rem; padding-top: .35rem; }
.score { font-weight: 700; font-variant-numeric: tabular-nums; }
.score.good { color: var(--ok); }
.score.fair { color: var(--warn); }
.score.poor { color: var(--danger); }
.nowrap { white-space: nowrap; }
.table-wrap.flat { border: 0; margin: 0; }
form.review { display: flex; gap: .35rem; flex-wrap: wrap; align-items: center; }
form.review select, form.review input { padding: .3rem .4rem; border: 1px solid #c8d1db; border-radius: 6px; font: inherit; font-size: .85rem; }
form.review input { min-width: 10rem; }

.timeline { overflow-x: auto; }
.tl-row { display: flex; align-items: center; min-width: 720px; border-bottom: 1px solid var(--line); }
.tl-row:last-child { border-bottom: 0; }
.tl-label { flex: 0 0 170px; padding: .35rem .5rem .35rem 0; font-size: .85rem; display: flex; flex-direction: column; }
.tl-label small { color: var(--muted); }
.tl-track { position: relative; flex: 1; height: 26px; background: repeating-linear-gradient(90deg, transparent 0 calc(12.5% - 1px), #eef1f4 calc(12.5% - 1px) 12.5%); }
.tl-axis .tl-track { height: 20px; background: none; }
.tl-hour { position: absolute; top: 2px; font-size: .72rem; color: var(--muted); transform: translateX(-50%); }
.tl-hour:first-child { transform: none; }
.tl-bar { position: absolute; top: 5px; bottom: 5px; border-radius: 3px; }
.tl-bar.inferred { opacity: .75; }
.tl-bar.gap, .legend .gap { background: repeating-linear-gradient(45deg, #b8c0c9 0 4px, transparent 4px 8px); top: 0; bottom: 0; opacity: .8; }
.phase-fill { background: #2f7ed8; }
.phase-aerate { background: #1d9a6c; }
.phase-settle { background: #c9a227; }
.phase-decant { background: #8250c4; }
.phase-idle { background: #aab4bf; }
.phase-ambiguous { background: #d9b88f; }
.phase-gap, .phase-unknown { background: #e1e6eb; }
.phase-filtration { background: #2f7ed8; }
.phase-relaxation { background: #c9a227; }
.phase-backwash { background: #8250c4; }
.state-bar-on { background: #0f6e8c; }
.state-bar-trip { background: var(--danger); }
.legend { display: flex; flex-wrap: wrap; gap: .4rem 1rem; margin-top: .75rem; font-size: .8rem; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: .35rem; }
.legend i { display: inline-block; width: 14px; height: 10px; border-radius: 2px; }

/* Chart tokens (reference palette; status colours are reserved for severity/state) */
:root {
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --status-critical: #d03b3b;
  --status-serious: #ec835a;
  --status-warning: #fab219;
  --status-neutral: #aab4bf;
  --chart-grid: #e1e0d9;
  --chart-axis: #c3c2b7;
  --chart-ink-muted: #898781;
}
.chart { width: 100%; height: auto; display: block; overflow: visible; }
.chart .grid { stroke: var(--chart-grid); stroke-width: 1; shape-rendering: crispEdges; }
.chart .tick { fill: var(--chart-ink-muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .end-label { fill: var(--text); font-size: 12px; font-weight: 600; }
.chart .line, .spark .line { fill: none; stroke: var(--series-1); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.spark .line.thin { stroke-width: 1.5; }
.chart .dot, .spark .dot { fill: var(--series-1); stroke: var(--surface); stroke-width: 2; }
.chart .crosshair { stroke: var(--chart-axis); stroke-width: 1; pointer-events: none; }
.chart .hit { fill: transparent; cursor: default; outline: none; }
.chart .hit:focus-visible { fill: rgba(42, 120, 214, .08); }
.chart .bar-hit:hover { fill: rgba(11, 11, 11, .04); }
.spark { display: block; }
.sev-fill-critical { fill: var(--status-critical); background: var(--status-critical); }
.sev-fill-major { fill: var(--status-serious); background: var(--status-serious); }
.sev-fill-minor { fill: var(--status-warning); background: var(--status-warning); }
.sev-fill-info { fill: var(--status-neutral); background: var(--status-neutral); }
.series-1 { background: var(--series-1); }
.series-2 { background: var(--series-2); }

.chart-tip { position: fixed; z-index: 50; pointer-events: none; background: var(--surface); border: 1px solid var(--line);
  box-shadow: 0 4px 16px rgba(16, 42, 58, .14); border-radius: 6px; padding: .45rem .6rem; font-size: .82rem; max-width: 320px; }
.chart-tip-title { color: var(--muted); margin-bottom: .2rem; }
.chart-tip-row { display: flex; align-items: center; gap: .45rem; }
.chart-tip-row strong { font-variant-numeric: tabular-nums; }
.chart-tip-row span:last-child { color: var(--muted); }
.chart-tip-key { display: inline-block; width: 12px; height: 2px; border-radius: 1px; }
.chart-tip-key.sev-fill-critical, .chart-tip-key.sev-fill-major, .chart-tip-key.sev-fill-minor, .chart-tip-key.sev-fill-info { height: 8px; width: 8px; border-radius: 2px; }

.table-view { margin-top: .75rem; }
.table-view summary { cursor: pointer; color: var(--muted); font-size: .85rem; }
.table-view table { margin-top: .5rem; font-size: .85rem; }
th.num, td.num { text-align: right; font-variant-numeric: tabular-nums; }

/* Filters */
.filter-row { display: flex; flex-wrap: wrap; align-items: center; gap: .5rem .75rem; margin-bottom: 1rem; }
.filter-spacer { flex: 1; }
.filters label { display: inline-flex; align-items: center; gap: .35rem; color: var(--muted); font-size: .85rem; }
.filters input, .filters select { padding: .35rem .5rem; border: 1px solid #c8d1db; border-radius: 6px; font: inherit; font-size: .88rem; background: #fff; }
.segmented { display: inline-flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; background: var(--surface); }
.segmented a { padding: .4rem .8rem; color: var(--text); border-right: 1px solid var(--line); font-size: .88rem; }
.segmented a:last-child { border-right: 0; }
.segmented a:hover { background: #f0f3f6; text-decoration: none; }
.segmented a.active { background: var(--brand); color: #fff; font-weight: 600; }
.tab-divider { width: 1px; background: var(--line); margin: .4rem .35rem; flex: 0 0 1px; }

/* KPI additions */
.kpi-sub { color: var(--muted); font-size: .82rem; margin-top: .15rem; }
.delta-up { color: #006300; }
.delta-down { color: var(--danger); }
.card.callout { border-left: 4px solid var(--brand); }
.callout-inline { background: var(--warn-bg); padding: .45rem .7rem; border-radius: 6px; font-size: .88rem; }
.status-text-ok { color: var(--ok); }
.status-text-bad { color: var(--danger); }
.small-note { font-size: .82rem; }

/* Bar lists (horizontal bars in HTML) */
.barlist { display: flex; flex-direction: column; gap: .35rem; }
.barrow { display: grid; grid-template-columns: minmax(0, 14rem) 1fr 3rem; align-items: center; gap: .6rem; color: var(--text); padding: .15rem .25rem; border-radius: 4px; }
a.barrow:hover, .barrow:focus-visible { background: #f3f6f8; text-decoration: none; }
.bl-label { overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; font-size: .88rem; line-height: 1.3; }
.bl-track { display: block; height: 16px; background: #f0f2f4; border-radius: 0 4px 4px 0; position: relative; }
.bl-track.inline { display: inline-block; width: 60%; vertical-align: middle; }
.bl-bar { display: block; height: 100%; background: var(--series-1); border-radius: 0 4px 4px 0; min-width: 2px; }
.bl-value { text-align: right; font-variant-numeric: tabular-nums; font-size: .88rem; }
.hours-cell { min-width: 200px; white-space: nowrap; }
.hours-cell .bl-value { display: inline-block; width: 4.5rem; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: .45rem; vertical-align: baseline; }

/* Duty pair split bar */
.duty-pair { display: grid; grid-template-columns: minmax(0, 13rem) 1fr minmax(0, 13rem); align-items: center; gap: .75rem; margin: .4rem 0; font-size: .88rem; }
.duty-name.right { text-align: right; }
.duty-bar { display: flex; gap: 2px; height: 16px; }
.duty-a { background: var(--series-1); border-radius: 4px 0 0 4px; min-width: 2px; }
.duty-b { background: var(--series-2); border-radius: 0 4px 4px 0; min-width: 2px; }

@media (max-width: 640px) {
  .barrow { grid-template-columns: minmax(0, 8rem) 1fr 2.5rem; }
  .duty-pair { grid-template-columns: 1fr; }
  .duty-name.right { text-align: left; }
  .tab-divider { display: none; }
}

.chart-scroll { overflow-x: auto; }
.chart-scroll .chart { min-width: 560px; }
.step-hint { margin-left: 1.25rem; }
.start-analysis { margin-top: 1rem; display: flex; flex-direction: column; gap: .4rem; align-items: flex-start; }
.inline-form { display: inline; margin-left: .5rem; }
