/* High-performance HMI palette: the plant is grey, colour means abnormal. */
:root {
  --bg: #d5d8db;
  --surface: #e7e9eb;
  --surface-2: #f3f4f5;
  --ink: #1a1e23;
  --muted: #555d67;
  --line: #b4b9bf;
  --cell: #c9cdd1;
  --dev: #d49400;
  --dev-rgb: 212, 148, 0;
  --alarm: #c42d26;
  --select: #2a63a8;
  --pfd-filter: grayscale(1) contrast(0.9) opacity(0.55);
  --radius-s: 4px;
  --radius-m: 8px;
  --font: "Barlow", system-ui, sans-serif;
  --font-cond: "Barlow Semi Condensed", "Barlow", system-ui, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #202327; --surface: #2a2e33; --surface-2: #33383e; --ink: #e8eaed; --muted: #a3aab3;
    --line: #4a5058; --cell: #3a3f45; --dev: #e8b021; --dev-rgb: 232, 176, 33; --alarm: #ec5a51; --select: #6ea2e3;
    --pfd-filter: grayscale(1) invert(0.86) contrast(0.85) opacity(0.6);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #202327; --surface: #2a2e33; --surface-2: #33383e; --ink: #e8eaed; --muted: #a3aab3;
  --line: #4a5058; --cell: #3a3f45; --dev: #e8b021; --dev-rgb: 232, 176, 33; --alarm: #ec5a51; --select: #6ea2e3;
  --pfd-filter: grayscale(1) invert(0.86) contrast(0.85) opacity(0.6);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 400 15px/1.45 var(--font);
  font-variant-numeric: tabular-nums;
  -webkit-font-smoothing: antialiased;
}
button, select, input { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--select); outline-offset: 2px; }

/* Header */
.top {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 16px 24px;
  padding: 14px 20px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
.brand h1 { margin: 0; font: 700 21px/1.1 var(--font-cond); letter-spacing: 0.01em; }
.brand p { margin: 2px 0 0; color: var(--muted); font-size: 13.5px; }
.clock { display: flex; flex-direction: column; align-items: center; line-height: 1; }
.clock-time { font: 700 44px/1 var(--font-cond); letter-spacing: 0.02em; }
.clock-date { font: 600 14px/1.2 var(--font-cond); color: var(--muted); margin-bottom: 3px; }
.controls-top { display: flex; gap: 12px; justify-content: flex-end; flex-wrap: wrap; }
.field { display: flex; flex-direction: column; gap: 3px; font-size: 12.5px; color: var(--muted); min-width: 0; }
.field.inline { flex-direction: row; align-items: center; gap: 8px; }
select {
  background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--radius-s);
  padding: 6px 8px; font-size: 14px; max-width: 360px; color: var(--ink);
}

/* Layout */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 380px;
  grid-template-areas: "plant panel" "timeline timeline";
  gap: 14px;
  padding: 14px 20px 24px;
  max-width: 1720px;
  margin: 0 auto;
}
.plant { grid-area: plant; min-width: 0; }
.panel { grid-area: panel; min-width: 0; display: flex; flex-direction: column; gap: 12px; max-height: calc(100vh - 290px); overflow: auto; }
.timeline { grid-area: timeline; min-width: 0; }

/* Diagram */
.diagram-wrap {
  position: relative; background: var(--surface-2); border: 1px solid var(--line);
  border-radius: var(--radius-m); overflow: hidden;
}
#diagram { display: block; width: 100%; height: auto; aspect-ratio: 1896 / 1045; }
#diagram .pfd { filter: var(--pfd-filter); }
.comp { cursor: pointer; outline: none; }
.comp .box { fill: transparent; stroke: var(--line); stroke-width: 2; transition: fill 0.25s, stroke 0.25s; }
.comp .box.nosensor { fill: url(#hatch); stroke-dasharray: 6 6; }
.hatch-line { stroke: var(--line); stroke-width: 3; opacity: 0.5; }
.comp:hover .box, .comp:focus-visible .box { stroke: var(--ink); stroke-width: 3; }
.comp.dev .box { fill: rgba(var(--dev-rgb), var(--sev)); stroke: var(--dev); stroke-width: 4; }
.comp.nuis .box { stroke: var(--alarm); stroke-opacity: 0.5; stroke-dasharray: 10 10; stroke-width: 2.5; }
.comp.nuis.dev .box { stroke: var(--dev); stroke-opacity: 1; stroke-dasharray: none; stroke-width: 4; }
.comp.alarm .box { stroke-opacity: 1; fill: color-mix(in srgb, var(--alarm) 38%, transparent); stroke: var(--alarm); stroke-width: 6; stroke-dasharray: none; animation: pulse 1.2s ease-in-out infinite; }
.comp.selected .box { stroke: var(--select); stroke-width: 7; stroke-dasharray: none; }
@keyframes pulse { 50% { stroke-opacity: 0.35; } }
.badge {
  font: 700 27px var(--font-cond); fill: var(--ink);
  paint-order: stroke; stroke: var(--surface-2); stroke-width: 7px; stroke-linejoin: round;
  pointer-events: none;
}
.comp.alarm .badge { fill: var(--alarm); }
.marker circle { fill: var(--ink); stroke: var(--surface-2); stroke-width: 4; }
.marker text { font: 700 26px var(--font-cond); fill: var(--surface-2); text-anchor: middle; }
.marker.role-root_cause circle { fill: var(--select); }
.marker.unmeasured circle { fill: var(--surface-2); stroke: var(--select); stroke-dasharray: 6 4; }
.marker.unmeasured text { fill: var(--select); }
.marker.pending { opacity: 0.18; }
.marker { transition: opacity 0.3s; }

.loading {
  position: absolute; inset: 0; display: grid; place-items: center;
  background: var(--surface-2); color: var(--muted); font: 600 16px var(--font-cond);
}
.loading.error { color: var(--alarm); padding: 24px; text-align: center; }

.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; padding: 8px 2px 0; font-size: 12.5px; color: var(--muted); align-items: center; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.sw { width: 18px; height: 11px; border-radius: 2px; display: inline-block; border: 2px solid transparent; }
.sw-dev { background: rgba(var(--dev-rgb), 0.6); border-color: var(--dev); }
.sw-alarm { background: var(--alarm); }
.sw-nuis { border: 2px dashed var(--alarm); opacity: 0.6; }
.sw-none { background: repeating-linear-gradient(45deg, var(--line) 0 2px, transparent 2px 5px); border: 1px dashed var(--line); }
.legend .credit { margin-left: auto; font-size: 11.5px; }

/* Panel */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-m); padding: 12px 14px; }
.card h2 { margin: 0 0 6px; font: 700 17px/1.2 var(--font-cond); }
.card h3 { margin: 12px 0 4px; font: 700 14px var(--font-cond); }
.card p { margin: 0 0 8px; }
.card-head { display: flex; justify-content: space-between; gap: 8px; align-items: flex-start; }
.close { border: 0; background: none; font-size: 22px; line-height: 1; cursor: pointer; color: var(--muted); padding: 0 4px; }
.muted { color: var(--muted); }
.fine { color: var(--muted); font-size: 12.5px; margin: 6px 0 0; }
.question { color: var(--muted); font-style: italic; }
.root { font-size: 14.5px; }
.meta-line { font-size: 13px; color: var(--muted); }
.link { border: 0; background: none; padding: 0; color: var(--select); cursor: pointer; text-decoration: underline; text-underline-offset: 2px; }

.steps { list-style: none; margin: 8px 0 0; padding: 0; border-left: 2px solid var(--line); }
.step { position: relative; padding: 2px 0 8px 14px; opacity: 0.45; transition: opacity 0.3s; }
.step.reached { opacity: 1; }
.step-head { display: flex; flex-wrap: wrap; gap: 4px 8px; align-items: baseline; font-size: 14px; }
.step .num {
  position: absolute; left: -11px; top: 2px; width: 20px; height: 20px; border-radius: 50%;
  background: var(--ink); color: var(--surface); font: 700 12px/20px var(--font-cond); text-align: center;
}
.step.role-root_cause .num { background: var(--select); }
.step p { margin: 2px 0 0; font-size: 13.5px; color: var(--muted); }
.step .node { font-family: var(--font-cond); font-weight: 600; }
.actions { margin: 0; padding-left: 18px; font-size: 13.5px; }

.tags { list-style: none; margin: 8px 0 0; padding: 0; display: grid; gap: 10px; }
.tag-head { display: flex; justify-content: space-between; gap: 8px; }
.tag { font: 600 14px var(--font-cond); }
.val { font: 700 15px var(--font-cond); }
.val.is-dev { color: var(--dev); }
.val.is-alarm { color: var(--alarm); }
.desc { font-size: 12.5px; color: var(--muted); }
.spark { width: 100%; height: 44px; display: block; margin-top: 3px; background: var(--surface-2); border-radius: var(--radius-s); }
.spark path { fill: none; stroke: var(--ink); stroke-width: 1.2; vector-effect: non-scaling-stroke; }
.spark .band { fill: var(--cell); opacity: 0.7; }
.spark .limit { stroke: var(--alarm); stroke-dasharray: 4 3; stroke-width: 1; vector-effect: non-scaling-stroke; }
.spark .ph { stroke: var(--select); stroke-width: 2; vector-effect: non-scaling-stroke; }

.now-list, .alarm-list { list-style: none; margin: 0; padding: 0; }
.row-btn {
  width: 100%; display: grid; grid-template-columns: 10px auto 1fr auto; gap: 8px; align-items: center;
  padding: 5px 4px; border: 0; background: none; text-align: left; cursor: pointer; border-radius: var(--radius-s);
}
.row-btn:hover { background: var(--surface-2); }
.row-btn .where { color: var(--muted); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot { width: 10px; height: 10px; border-radius: 50%; }
.dot.is-dev { background: var(--dev); }
.dot.is-alarm { background: var(--alarm); }
.alarm-list li { display: grid; grid-template-columns: 56px auto 1fr; gap: 8px; padding: 3px 0; font-size: 14px; }
.alarm-list .lvl { font: 700 13px var(--font-cond); color: var(--alarm); }
.alarm-list .since { color: var(--muted); text-align: right; font-size: 13px; }

/* Timeline */
.timeline { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-m); padding: 10px 14px 12px; }
.transport { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 8px; }
.transport button {
  border: 1px solid var(--line); background: var(--surface-2); border-radius: var(--radius-s);
  padding: 6px 10px; cursor: pointer; font-weight: 600; min-width: 38px;
}
.transport button:hover { border-color: var(--ink); }
.transport .play { min-width: 84px; background: var(--ink); color: var(--surface); border-color: var(--ink); font-family: var(--font-cond); font-size: 16px; }
.transport .play.on { background: var(--select); border-color: var(--select); }
.zoom { display: flex; gap: 4px; margin-left: 8px; }
.view-label { margin-left: auto; color: var(--muted); font-size: 13px; }

.ribbon-grid { display: grid; grid-template-columns: 170px minmax(0, 1fr); gap: 10px; }
.ribbon-labels { position: relative; margin-top: 26px; }
.rl {
  position: absolute; left: 0; right: 0; display: block; text-align: right; line-height: 15px;
  font: 500 12.5px/15px var(--font-cond); color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.rl-comp { border: 0; background: none; padding: 0; cursor: pointer; text-align: right; color: var(--ink); }
.rl-comp:hover { color: var(--select); }
.rl-lane { font-style: italic; }
#slider { width: 100%; margin: 0 0 6px; height: 20px; accent-color: var(--ink); }
#ribbon { display: block; width: 100%; touch-action: none; }

@media (max-width: 1100px) {
  .layout { grid-template-columns: 1fr; grid-template-areas: "plant" "timeline" "panel"; }
  .panel { max-height: none; }
  .top { grid-template-columns: 1fr auto; }
  .controls-top { grid-column: 1 / -1; justify-content: flex-start; }
}
@media (max-width: 640px) {
  .top, .layout { padding-left: 16px; padding-right: 16px; }
  .clock-time { font-size: 32px; }
  .ribbon-grid { grid-template-columns: 96px minmax(0, 1fr); }
  .rl { font-size: 11px; }
  .view-label { margin-left: 0; width: 100%; }
  .legend .credit { margin-left: 0; }
  select { max-width: 100%; width: 100%; }
  .field.inline select { width: auto; }
  .controls-top .field { flex: 1 1 100%; }
}
@media (prefers-reduced-motion: reduce) {
  .comp.alarm .box { animation: none; }
  * { transition: none !important; }
}
