/* EIS Sensors — instrument UI.
   Colour roles come from the validated reference palette; both modes are
   selected, not flipped. Series slots 1-3 are used for pH / ORP / membrane
   and clear the all-pairs CVD gate in both modes (worst pair dE 9.2 light,
   9.4 dark). Slot 3 sits below 3:1 on the light surface, so every curve is
   direct-labelled at its end -- identity never rests on colour alone. */

:root {
  color-scheme: light;
  --surface:        #fcfcfb;
  --plane:          #f9f9f7;
  --ink:            #0b0b0b;
  --ink-2:          #52514e;
  --ink-muted:      #898781;
  --grid:           #e1e0d9;
  --axis:           #c3c2b7;
  --hairline:       rgba(11, 11, 11, .10);

  --series-1:       #2a78d6;   /* pH */
  --series-2:       #eb6834;   /* ORP */
  --series-3:       #1baf7a;   /* мембрана (разность) */
  --series-4:       #eda100;   /* EC */

  --good:           #0ca30c;
  --warning:        #fab219;
  --serious:        #ec835a;
  --critical:       #d03b3b;
  --success-text:   #006300;

  --r-sm: 6px;
  --r-md: 10px;
  --r-lg: 14px;
  --sh-1: 0 1px 2px rgba(11,11,11,.05), 0 1px 1px rgba(11,11,11,.04);
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --surface:   #1a1a19;
    --plane:     #0d0d0d;
    --ink:       #ffffff;
    --ink-2:     #c3c2b7;
    --ink-muted: #898781;
    --grid:      #2c2c2a;
    --axis:      #383835;
    --hairline:  rgba(255,255,255,.10);
    --series-1:  #3987e5;
    --series-2:  #d95926;
    --series-3:  #199e70;
    --series-4:  #c98500;
    --success-text: #0ca30c;
    --sh-1: 0 1px 2px rgba(0,0,0,.4);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --surface:   #1a1a19;
  --plane:     #0d0d0d;
  --ink:       #ffffff;
  --ink-2:     #c3c2b7;
  --ink-muted: #898781;
  --grid:      #2c2c2a;
  --axis:      #383835;
  --hairline:  rgba(255,255,255,.10);
  --series-1:  #3987e5;
  --series-2:  #d95926;
  --series-3:  #199e70;
  --series-4:  #c98500;
  --success-text: #0ca30c;
  --sh-1: 0 1px 2px rgba(0,0,0,.4);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--plane);
  color: var(--ink);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
.muted { color: var(--ink-2); }
.small { font-size: .85rem; }
code, .code-block { font-family: var(--mono); font-size: .86em; }
.code-block {
  background: var(--plane); border: 1px solid var(--hairline);
  border-radius: var(--r-sm); padding: 10px 12px; overflow-x: auto;
  color: var(--ink-2); margin: 12px 0;
}

/* ---------- chrome ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding: 12px 20px;
  background: var(--surface); border-bottom: 1px solid var(--hairline);
  position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 620; letter-spacing: -.01em; }
.brand-sub { font-weight: 400; color: var(--ink-muted); font-size: .84rem; }
@media (max-width: 720px) { .brand-sub { display: none; } }
.topbar-right { display: flex; align-items: center; gap: 10px; }

.conn-chip {
  font-size: .78rem; padding: 3px 9px; border-radius: 999px;
  border: 1px solid var(--hairline); display: inline-flex; align-items: center; gap: 6px;
  color: var(--ink-2); white-space: nowrap;
}
.conn-chip::before {
  content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--ink-muted);
}
.conn-on::before  { background: var(--good); }
.conn-demo::before{ background: var(--series-4); }
.conn-off::before { background: var(--critical); }

.wrap { max-width: 1240px; margin: 0 auto; padding: 22px 20px 60px; }
section { margin-bottom: 30px; }
h2 { font-size: 1.06rem; font-weight: 620; margin: 0 0 12px; letter-spacing: -.01em; }
h3 { font-size: .92rem; font-weight: 600; margin: 0; }

.card {
  background: var(--surface); border: 1px solid var(--hairline);
  border-radius: var(--r-lg); padding: 16px 18px; box-shadow: var(--sh-1);
}

.btn {
  font: inherit; font-size: .9rem; font-weight: 500; cursor: pointer;
  border: 1px solid transparent; border-radius: var(--r-md);
  padding: 8px 15px; background: var(--series-1); color: #fff;
  transition: filter .12s ease, opacity .12s;
}
.btn:hover:not(:disabled) { filter: brightness(1.08); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-alt { background: transparent; color: var(--ink); border-color: var(--axis); }
.btn-alt:hover:not(:disabled) { background: var(--plane); filter: none; }
.btn-ghost { background: transparent; color: var(--ink-2); border-color: transparent; padding: 6px 9px; font-size: 1.05rem; }
.btn-ghost:hover { background: var(--plane); filter: none; }

.field { display: inline-flex; flex-direction: column; gap: 4px; font-size: .8rem; color: var(--ink-2); }
.field input, .field select {
  font: inherit; font-size: .9rem; color: var(--ink);
  background: var(--plane); border: 1px solid var(--axis);
  border-radius: var(--r-sm); padding: 7px 9px; min-width: 92px;
}
.field input:focus, .field select:focus { outline: 2px solid var(--series-1); outline-offset: -1px; }
#bridgeUrl { min-width: 260px; }
.row, .controls-row { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.controls-row { align-items: flex-end; }

.connect h2 { margin-top: 0; }

/* ---------- live tiles ---------- */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(168px, 1fr)); gap: 12px; }
.tile {
  background: var(--surface); border: 1px solid var(--hairline);
  border-radius: var(--r-lg); padding: 13px 16px; box-shadow: var(--sh-1);
}
.tile .k { font-size: .78rem; color: var(--ink-2); }
.tile .v { font-size: 1.95rem; font-weight: 600; line-height: 1.15; margin-top: 3px; letter-spacing: -.02em; }
.tile .v .unit { font-size: .95rem; font-weight: 400; color: var(--ink-2); letter-spacing: 0; }
.tile .u { font-size: .78rem; color: var(--ink-muted); margin-top: 3px; }
#tilePh .v { color: var(--series-1); }

.demo-controls { margin-top: 12px; display: flex; flex-wrap: wrap; gap: 18px; align-items: center; }
.demo-badge {
  font-size: .7rem; font-weight: 700; letter-spacing: .06em;
  background: var(--series-4); color: #1a1a19; padding: 3px 8px; border-radius: var(--r-sm);
}
.slider-field { display: flex; align-items: center; gap: 9px; font-size: .84rem; color: var(--ink-2); }
.slider-field input[type=range] { width: 150px; accent-color: var(--series-1); }
.slider-field b { font-variant-numeric: tabular-nums; color: var(--ink); min-width: 3.4em; }
.demo-controls p { flex-basis: 100%; margin: 0; }

/* ---------- verdict ---------- */
.verdict { margin-bottom: 14px; }
.verdict-head { display: flex; gap: 13px; align-items: flex-start; }
.verdict-headline { font-size: 1.2rem; font-weight: 620; letter-spacing: -.01em; }
.status-ic {
  flex: none; width: 26px; height: 26px; border-radius: 50%;
  display: inline-grid; place-items: center; font-size: .9rem; font-weight: 700;
  background: var(--plane); border: 2px solid var(--ink-muted); color: var(--ink-muted);
  margin-top: 2px;
}
.status-ic.good     { border-color: var(--good);     color: var(--good); }
.status-ic.watch    { border-color: var(--warning);  color: var(--warning); }
.status-ic.warn     { border-color: var(--serious);  color: var(--serious); }
.status-ic.bad      { border-color: var(--critical); color: var(--critical); }

.verdict-budget { margin-top: 14px; }
.budget-bar {
  height: 8px; border-radius: 4px; background: var(--grid); overflow: hidden;
}
.budget-bar > div { height: 100%; border-radius: 4px; background: var(--series-1); transition: width .3s ease; }
.budget-bar > div.warn { background: var(--serious); }
.budget-bar > div.bad  { background: var(--critical); }
.budget-lbl { margin-top: 5px; }
.verdict-actions { margin-top: 14px; display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }

/* ---------- health cards ---------- */
.health-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(285px, 1fr)); gap: 12px; }
.hcard {
  background: var(--surface); border: 1px solid var(--hairline);
  border-left: 3px solid var(--ink-muted);
  border-radius: var(--r-md); padding: 13px 15px; box-shadow: var(--sh-1);
}
.hcard.good  { border-left-color: var(--good); }
.hcard.watch { border-left-color: var(--warning); }
.hcard.warn  { border-left-color: var(--serious); }
.hcard.bad   { border-left-color: var(--critical); }
.hcard-top { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
.hcard-title { font-weight: 600; font-size: .93rem; }
.hcard-state {
  font-size: .72rem; font-weight: 600; letter-spacing: .03em; text-transform: uppercase;
  display: inline-flex; align-items: center; gap: 4px; white-space: nowrap;
}
.hcard.good  .hcard-state { color: var(--success-text); }
.hcard.watch .hcard-state { color: var(--ink-2); }
.hcard.warn  .hcard-state { color: var(--serious); }
.hcard.bad   .hcard-state { color: var(--critical); }
.hcard-sub { font-size: .78rem; color: var(--ink-muted); margin-top: 1px; }
.hcard-val {
  font-size: 1.5rem; font-weight: 600; margin: 8px 0 2px; letter-spacing: -.02em;
}
.hcard-delta { font-size: .8rem; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.hcard-delta .up   { color: var(--serious); }
.hcard-delta .down { color: var(--series-1); }
.hcard-msg { font-size: .86rem; color: var(--ink-2); margin-top: 8px; }
.hcard details { margin-top: 8px; }
.hcard summary {
  cursor: pointer; font-size: .78rem; color: var(--ink-muted);
  list-style: none; user-select: none;
}
.hcard summary::-webkit-details-marker { display: none; }
.hcard summary::before { content: "＋ "; }
.hcard details[open] summary::before { content: "－ "; }
.hcard details p { font-size: .82rem; color: var(--ink-muted); margin: 6px 0 0; }

.fine { margin-top: 14px; }
.fine > summary {
  cursor: pointer; font-size: .87rem; color: var(--ink-2); padding: 6px 0; user-select: none;
}
.fine-body { font-size: .87rem; color: var(--ink-2); max-width: 74ch; }
.fine-body p { margin: 8px 0; }

/* ---------- charts ---------- */
/* minmax(0,…) rather than 1fr: a bare 1fr floors at min-content, and the
   calibration form's input row would then squeeze the chart beside it. */
.chart-pair { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; }
@media (max-width: 940px) { .chart-pair { grid-template-columns: 1fr; } }
.chart-card { display: flex; flex-direction: column; }
.chart-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; }
.chart-wrap { position: relative; width: 100%; }
canvas { display: block; width: 100%; touch-action: none; }
#nyquist { height: 330px; }
#bodeMag, #bodePhase { height: 132px; }
#ecChart, #driftChart, #trendJunction, #trendCap { height: 210px; }
.bode-sub { margin: 4px 0 2px; }

.legend { display: flex; flex-wrap: wrap; gap: 14px; }
.legend .item { display: inline-flex; align-items: center; gap: 6px; font-size: .8rem; color: var(--ink-2); }
.legend .key { width: 14px; height: 3px; border-radius: 2px; flex: none; }
.legend .key.dot { width: 9px; height: 9px; border-radius: 50%; }

.tip {
  position: absolute; pointer-events: none; opacity: 0;
  transform: translate(-50%, calc(-100% - 10px));
  background: var(--surface); border: 1px solid var(--axis); border-radius: var(--r-sm);
  box-shadow: 0 4px 14px rgba(0,0,0,.14); padding: 7px 10px;
  font-size: .8rem; white-space: nowrap; font-variant-numeric: tabular-nums;
  transition: opacity .09s linear; z-index: 8; color: var(--ink);
}
.tip b { font-weight: 600; }
.tip .sw { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; }

.ec-calc { display: flex; flex-direction: column; gap: 12px; }
.ec-out { font-size: 1.05rem; }
.ec-out b { font-size: 1.4rem; font-weight: 600; color: var(--series-4); }

.cal-history { margin-top: 12px; max-height: 190px; overflow-y: auto; }
.cal-history table { width: 100%; border-collapse: collapse; font-size: .82rem; font-variant-numeric: tabular-nums; }
.cal-history th {
  text-align: left; font-weight: 500; color: var(--ink-muted); font-size: .76rem;
  border-bottom: 1px solid var(--hairline); padding: 5px 8px 5px 0;
}
.cal-history td { padding: 5px 8px 5px 0; border-bottom: 1px solid var(--hairline); color: var(--ink-2); }

.log {
  height: 210px; overflow-y: auto; font-family: var(--mono); font-size: .78rem;
  line-height: 1.55; color: var(--ink-2);
}
.log div { white-space: pre-wrap; word-break: break-word; }
.log .t { color: var(--ink-muted); }
.log .err { color: var(--critical); }
.log .ok { color: var(--success-text); }

.footer { display: flex; justify-content: space-between; gap: 14px; flex-wrap: wrap; padding-top: 18px; border-top: 1px solid var(--hairline); }
