/* Agri Monitor
   Ý tưởng: bảng đồng hồ đo của trạm khí tượng + giấy ghi của máy ghi nhiệt-ẩm.
   Cam là bút nhiệt độ, xanh dương là bút độ ẩm. Mọi thứ khác giữ yên lặng. */

:root {
  color-scheme: light;
  --ground: #eef2ed;
  --surface: #fbfcfa;
  --ink: #142a1c;
  --ink-2: #485a4e;
  --ink-3: #66766b;
  --rule: #d5ddd5;
  --rule-strong: #b4c1b6;
  --grid-minor: rgba(30, 112, 64, 0.075);
  --grid-major: rgba(30, 112, 64, 0.17);
  --band: rgba(20, 42, 28, 0.06);
  --band-edge: rgba(20, 42, 28, 0.22);
  --temp: #eb6834;
  --hum: #2a78d6;
  --good: #0ca30c;
  --good-text: #006300;
  --warn: #fab219;
  --crit: #d03b3b;
  --selected-bg: #142a1c;
  --selected-ink: #fbfcfa;
  --shadow-float: 0 8px 24px rgba(20, 42, 28, 0.14);
  --side-bg: #142a1c;
  --side-border: #142a1c;
  --side-ink: #eef5ef;
  --side-ink-2: rgba(238, 245, 239, 0.7);
  --side-hover: rgba(238, 245, 239, 0.07);
  --side-active: rgba(238, 245, 239, 0.13);
  --side-accent: #7fd18c;
  --font: "Be Vietnam Pro", "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  --r-panel: 14px;
  --r-ctl: 8px;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --ground: #0d1410;
    --surface: #141d17;
    --ink: #e5ede7;
    --ink-2: #b3c1b7;
    --ink-3: #8a998f;
    --rule: #26332b;
    --rule-strong: #3a4a3f;
    --grid-minor: rgba(150, 214, 170, 0.05);
    --grid-major: rgba(150, 214, 170, 0.12);
    --band: rgba(229, 237, 231, 0.06);
    --band-edge: rgba(229, 237, 231, 0.22);
    --temp: #d95926;
    --hum: #3987e5;
    --good-text: #2fbf3a;
    --selected-bg: #e5ede7;
    --selected-ink: #141d17;
    --shadow-float: 0 8px 24px rgba(0, 0, 0, 0.45);
    --side-bg: #09100c;
    --side-border: #1d2922;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--ground);
  color: var(--ink);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, p, dl, dd, figure { margin: 0; }

a { color: inherit; }

:focus-visible { outline: 2px solid var(--hum); outline-offset: 2px; border-radius: 4px; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.wrap { max-width: 1160px; margin: 0 auto; padding: 0 28px; }

.panel {
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--r-panel);
}

/* ---------------------------------------------------------------- app shell + sidebar */

.app {
  display: grid;
  grid-template-columns: 236px minmax(0, 1fr);
  min-height: 100vh;
  /* tô nền cả cột sidebar khi trang dài hơn màn hình */
  background: linear-gradient(to right, var(--side-bg) 236px, transparent 236px);
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 30px;
  padding: 22px 14px;
  background: var(--side-bg);
  color: var(--side-ink);
  border-right: 1px solid var(--side-border);
}
.sidebar :focus-visible { outline-color: var(--side-accent); }

.sidebar-logo {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 4px 8px;
  color: var(--side-ink);
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.01em;
  text-decoration: none;
}
.sidebar-logo img { display: block; width: 36px; height: 36px; flex: none; }

.sidebar-nav { display: flex; flex-direction: column; gap: 4px; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 10px;
  color: var(--side-ink-2);
  font-size: 15px;
  font-weight: 500;
  text-decoration: none;
}
.nav-item:hover { background: var(--side-hover); color: var(--side-ink); }
.nav-item[aria-current="page"] { background: var(--side-active); color: var(--side-ink); }
.nav-icon { width: 20px; height: 20px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.6; }
.nav-item[aria-current="page"] .nav-icon { stroke: var(--side-accent); }

.content { min-width: 0; }

/* ---------------------------------------------------------------- top bar */

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 14px 24px;
  padding: 18px 0;
  border-bottom: 1px solid var(--rule);
}

.device-picker { display: inline-flex; align-items: center; gap: 10px; font-size: 14px; color: var(--ink-2); }

.select { position: relative; display: inline-block; }
.select::after {
  content: "";
  position: absolute;
  right: 13px; top: 50%;
  width: 7px; height: 7px;
  border-right: 1.6px solid var(--ink-2);
  border-bottom: 1.6px solid var(--ink-2);
  transform: translateY(-70%) rotate(45deg);
  pointer-events: none;
}
.select select {
  appearance: none;
  -webkit-appearance: none;
  font: inherit;
  font-size: 15px;
  font-weight: 500;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--rule-strong);
  border-radius: var(--r-ctl);
  padding: 7px 36px 7px 12px;
  min-width: 170px;
  cursor: pointer;
}

.conn { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; color: var(--ink-2); }
.conn-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--ink-3); flex: none; }
.conn[data-state="online"] .conn-dot { background: var(--good); box-shadow: 0 0 0 3px rgba(12, 163, 12, 0.16); }
.conn[data-state="stale"] .conn-dot { background: var(--warn); }
.conn[data-state="error"] .conn-dot { background: var(--crit); }

.banner {
  margin-top: 16px;
  padding: 12px 16px;
  border-radius: var(--r-ctl);
  border: 1px solid var(--crit);
  background: color-mix(in srgb, var(--crit) 8%, var(--surface));
  font-size: 15px;
}

/* ---------------------------------------------------------------- verdict (hero) */

.verdict { padding: 52px 0 30px; }

.verdict h1 {
  font-size: clamp(30px, 3.9vw, 46px);
  line-height: 1.12;
  font-weight: 600;
  letter-spacing: -0.025em;
  max-width: 24ch;
  text-wrap: balance;
}

.lead {
  margin-top: 14px;
  max-width: 62ch;
  font-size: 18px;
  line-height: 1.55;
  color: var(--ink-2);
}

/* ---------------------------------------------------------------- instruments */

.instruments {
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--r-panel);
}

.gauge { padding: 26px 32px 24px; min-width: 0; }
.gauge + .gauge { border-left: 1px solid var(--rule); }

.gauge-title {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 16px;
  font-weight: 500;
  color: var(--ink-2);
}

.key { display: inline-block; width: 16px; height: 3px; border-radius: 2px; flex: none; }
.key-temp { background: var(--temp); }
.key-hum { background: var(--hum); }

.reading { display: flex; align-items: baseline; gap: 6px; margin-top: 10px; }
.reading-value {
  font-size: clamp(52px, 6.4vw, 72px);
  font-weight: 500;
  line-height: 1;
  letter-spacing: -0.035em;
}
.reading-unit { font-size: 24px; font-weight: 500; color: var(--ink-2); }

.delta { margin-top: 8px; min-height: 1.55em; font-size: 14px; color: var(--ink-2); }

.ruler { display: block; width: 100%; height: 62px; margin-top: 18px; overflow: visible; }
.ruler .tick { stroke: var(--ink-3); stroke-width: 1; }
.ruler .tick-major { stroke: var(--ink-2); }
.ruler .baseline { stroke: var(--ink-2); stroke-width: 1; }
.ruler .tick-label { fill: var(--ink-3); font-size: 12px; font-variant-numeric: tabular-nums; }
.ruler .marker { transition: transform 0.5s cubic-bezier(0.3, 0.7, 0.2, 1); }
.ruler .marker-line { stroke: var(--ink); stroke-width: 2; stroke-linecap: round; }
.ruler .marker-head { fill: var(--ink); }

.status { display: flex; align-items: flex-start; gap: 9px; margin-top: 14px; font-size: 15px; }
.status-icon { width: 18px; height: 18px; flex: none; margin-top: 2px; }
.status-icon .ring { fill: none; stroke-width: 2; }
.status[data-level="good"] .ring { stroke: var(--good); }
.status[data-level="good"] .glyph { stroke: var(--good); }
.status[data-level="warning"] .fill { fill: var(--warn); }
.status[data-level="critical"] .fill { fill: var(--crit); }
.status .glyph { fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.status[data-level="warning"] .glyph { stroke: #1c1a10; }
.status[data-level="critical"] .glyph { stroke: #ffffff; }
.status strong { font-weight: 600; }
.status[data-level="unknown"] { color: var(--ink-3); }

.instruments-foot {
  grid-column: 1 / -1;
  padding: 11px 32px;
  border-top: 1px solid var(--rule);
  font-size: 14px;
  color: var(--ink-3);
}

.instruments-foot:empty { display: none; }

.instruments.is-stale .reading-value,
.instruments.is-stale .ruler { opacity: 0.45; }

/* ---------------------------------------------------------------- history / recorder */

.history { margin-top: 60px; }

.history-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 16px;
  margin-bottom: 14px;
}
.history-head h2 { margin-right: auto; }

h2 { font-size: 22px; font-weight: 600; letter-spacing: -0.015em; line-height: 1.3; }

.range {
  display: inline-flex;
  margin: 0;
  padding: 3px;
  border: 1px solid var(--rule);
  border-radius: 10px;
  background: var(--surface);
  min-width: 0;
}
.range label { position: relative; }
.range input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.range span {
  display: block;
  padding: 6px 13px;
  border-radius: 7px;
  font-size: 14px;
  font-weight: 500;
  color: var(--ink-2);
  white-space: nowrap;
}
.range input:hover + span { background: var(--band); }
.range input:checked + span { background: var(--selected-bg); color: var(--selected-ink); }
.range input:focus-visible + span { outline: 2px solid var(--hum); outline-offset: 1px; }

.button {
  display: inline-flex;
  align-items: center;
  padding: 7px 14px;
  border: 1px solid var(--rule-strong);
  border-radius: var(--r-ctl);
  background: var(--surface);
  font-size: 14px;
  font-weight: 500;
  text-decoration: none;
}
.button:hover { background: color-mix(in srgb, var(--ink) 5%, var(--surface)); }
.button[aria-disabled="true"] { color: var(--ink-3); border-color: var(--rule); background: transparent; pointer-events: none; }

.recorder {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--r-panel);
  transition: opacity 0.2s;
}
.recorder.is-loading { opacity: 0.55; }
.recorder:focus-visible { outline-offset: 3px; border-radius: var(--r-panel); }

.strip { padding: 16px 0 0; }
.strip + .strip { border-top: 1px solid var(--rule); }

.strip-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 6px 26px;
  padding: 0 22px;
}
.strip-head h3 {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  font-size: 15px;
  font-weight: 600;
  margin-right: 6px;
}

.stats { display: flex; flex-wrap: wrap; gap: 4px 20px; font-size: 14px; }
.stats div { display: flex; gap: 6px; }
.stats dt { color: var(--ink-3); }
.stats dd { font-weight: 500; font-variant-numeric: tabular-nums; }

.band-key { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; color: var(--ink-3); margin-left: auto; }
.band-swatch {
  width: 18px; height: 11px;
  background: var(--band);
  border-top: 1px solid var(--band-edge);
  border-bottom: 1px solid var(--band-edge);
}

.strip-chart { display: block; width: 100%; height: 176px; touch-action: pan-y; }
.strip:last-of-type .strip-chart { height: 200px; }

.strip-chart .grid-minor { stroke: var(--grid-minor); stroke-width: 1; }
.strip-chart .grid-major { stroke: var(--grid-major); stroke-width: 1; }
.strip-chart .band { fill: var(--band); }
.strip-chart .band-edge { stroke: var(--band-edge); stroke-width: 1; }
.strip-chart .axis-label { fill: var(--ink-3); font-size: 12px; font-variant-numeric: tabular-nums; }
.strip-chart .end-label { fill: var(--ink); font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; }
.strip-chart .empty-label { fill: var(--ink-3); font-size: 14px; }
.strip-chart .pen { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.strip-chart .spread { opacity: 0.13; transition: opacity 0.8s ease 0.9s; }
.strip-chart .spread.is-hidden { opacity: 0; }
.strip-chart .end-dot { stroke: var(--surface); stroke-width: 2; }
.strip-chart .crosshair { stroke: var(--ink-2); stroke-width: 1; }
.strip-chart .hover-dot { stroke: var(--surface); stroke-width: 2; }
.strip-chart .hit { fill: transparent; cursor: crosshair; }

[data-metric="temperature"] .pen { stroke: var(--temp); }
[data-metric="temperature"] .spread,
[data-metric="temperature"] .end-dot,
[data-metric="temperature"] .hover-dot { fill: var(--temp); }
[data-metric="humidity"] .pen { stroke: var(--hum); }
[data-metric="humidity"] .spread,
[data-metric="humidity"] .end-dot,
[data-metric="humidity"] .hover-dot { fill: var(--hum); }

.tooltip {
  position: absolute;
  top: 0; left: 0;
  z-index: 5;
  min-width: 200px;
  padding: 10px 13px 11px;
  background: var(--surface);
  border: 1px solid var(--rule-strong);
  border-radius: 10px;
  box-shadow: var(--shadow-float);
  font-size: 13px;
  pointer-events: none;
}
.tt-time { color: var(--ink-2); margin-bottom: 6px; }
.tt-row { display: grid; grid-template-columns: 14px auto 1fr; align-items: baseline; gap: 2px 8px; }
.tt-row + .tt-row { margin-top: 4px; }
.tt-key { width: 14px; height: 3px; border-radius: 2px; align-self: center; }
.tt-value { font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; }
.tt-name { color: var(--ink-2); }
.tt-spread { grid-column: 2 / -1; color: var(--ink-3); font-variant-numeric: tabular-nums; }

.hint { margin-top: 10px; font-size: 14px; color: var(--ink-3); max-width: 80ch; }

/* ---------------------------------------------------------------- lower */

.lower {
  display: grid;
  grid-template-columns: minmax(0, 1.5fr) minmax(0, 1fr);
  gap: 28px;
  margin-top: 60px;
}
.lower h2 { margin-bottom: 14px; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 15px; }
th {
  text-align: left;
  font-weight: 500;
  font-size: 14px;
  color: var(--ink-3);
  padding: 12px 18px 10px;
  border-bottom: 1px solid var(--rule);
}
td { padding: 9px 18px; border-bottom: 1px solid var(--rule); font-variant-numeric: tabular-nums; }
tbody tr:last-child td { border-bottom: 0; }
.num { text-align: right; }
td.num .flag {
  display: inline-block;
  width: 7px; height: 7px;
  border-radius: 50%;
  margin-right: 8px;
  vertical-align: 2px;
}
.flag-warning { background: var(--warn); }
.flag-critical { background: var(--crit); }

.device-info { padding: 6px 20px; }
.device-info div {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 11px 0;
  border-bottom: 1px solid var(--rule);
  font-size: 15px;
}
.device-info div:last-child { border-bottom: 0; }
.device-info dt { color: var(--ink-3); }
.device-info dd { text-align: right; font-weight: 500; overflow-wrap: anywhere; }

/* ---------------------------------------------------------------- footer */

.footer {
  margin-top: 64px;
  padding: 18px 0 32px;
  border-top: 1px solid var(--rule);
  font-size: 14px;
  color: var(--ink-3);
}

/* ---------------------------------------------------------------- responsive */

@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; background: none; }
  .sidebar {
    position: static;
    height: auto;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 16px;
    border-right: 0;
    border-bottom: 1px solid var(--side-border);
  }
  .sidebar-logo { padding: 2px 0; }
  .sidebar-logo img { width: 32px; height: 32px; }
  .sidebar-nav { flex-direction: row; }
  .nav-item { padding: 8px 12px; }
  .wrap { padding: 0 16px; }
  .instruments { grid-template-columns: 1fr; }
  .gauge { padding: 22px 20px; }
  .gauge + .gauge { border-left: 0; border-top: 1px solid var(--rule); }
  .instruments-foot { padding: 11px 20px; }
  .lower { grid-template-columns: 1fr; gap: 44px; }
  .verdict { padding: 36px 0 24px; }
  .lead { font-size: 17px; }
  .strip-head { padding: 0 16px; }
  .band-key { margin-left: 0; }
  .strip-chart { height: 150px; }
  .strip:last-of-type .strip-chart { height: 172px; }
}

@media (max-width: 480px) {
  .select select { min-width: 0; max-width: 52vw; }
  .history-head h2 { width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}

@media (forced-colors: active) {
  .key, .band-swatch, .conn-dot { forced-color-adjust: none; }
}
