:root {
  color-scheme: light;
  --bg: #e7eef0;
  --surface: #ffffff;
  --surface-2: #f4f8f9;
  --ink: #142830;
  --muted: #5c7380;
  --line: #d3e0e6;
  --accent: #0c6b6f;
  --accent-strong: #095456;
  --accent-soft: #e5f3f3;
  --ok: #0d7a45;
  --ok-soft: #e6f6ed;
  --warn: #8f5a08;
  --warn-soft: #fff3dc;
  --bad: #b4232c;
  --bad-soft: #fdecee;
  --off: #5d6b74;
  --shadow: 0 12px 32px rgba(20, 40, 48, 0.06);
  --radius: 18px;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #10181d;
  --surface: #1a262d;
  --surface-2: #22313a;
  --ink: #e8f0f3;
  --muted: #9aafb8;
  --line: #31434d;
  --accent: #7fd4cf;
  --accent-strong: #b7ebe8;
  --accent-soft: #1b3336;
  --ok: #7dcea8;
  --ok-soft: #16352a;
  --warn: #f0c27a;
  --warn-soft: #3a2d16;
  --bad: #ffb4b0;
  --bad-soft: #3d2022;
  --off: #b7c4cb;
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.28);
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: "Leelawadee UI", "Segoe UI", Tahoma, sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.5;
}

button, input, select { font: inherit; color: inherit; }

button { cursor: pointer; }

:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.app { min-height: 100vh; }

.sidebar {
  display: none;
  position: sticky;
  top: 0;
  height: 100vh;
  padding: 24px 16px;
  background: var(--surface);
  border-right: 1px solid var(--line);
  flex-direction: column;
  gap: 28px;
}

.brand {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 0 8px;
}

.brand-mark {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
}

html[data-theme="dark"] .brand-mark { color: #102326; }

.brand strong, .brand span { display: block; }
.brand span { color: var(--muted); font-size: 0.85rem; }

.side-nav, .tabbar {
  display: flex;
  gap: 6px;
}

.side-nav { flex-direction: column; }

.side-nav a,
.side-nav button, .tabbar button {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  border: 0;
  background: transparent;
  color: var(--ink);
  border-radius: 12px;
  padding: 12px 14px;
  text-align: left;
  text-decoration: none;
}

.side-nav button.is-active, .tabbar button.is-active {
  background: var(--accent-soft);
  color: var(--accent-strong);
  font-weight: 700;
}

.main {
  padding: 20px 16px 104px;
}

.topbar, .banner, .content {
  width: min(1120px, 100%);
  margin-inline: auto;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 16px;
}

.eyebrow {
  margin: 0;
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.04em;
}

h1, h2, p { margin: 0; }

h1 { font-size: 1.7rem; line-height: 1.2; }
h2 { font-size: 1.15rem; }

.top-actions, .button-row, .section-head, .form-actions, .key-row, .viewer-bar, .viewer-nav {
  display: flex;
  align-items: center;
  gap: 10px;
}

.top-actions { flex-wrap: wrap; justify-content: flex-end; }

.footnote a { color: var(--accent-strong); }

.updated, .hint, .footnote, .section-head p, .viewer-note {
  color: var(--muted);
  font-size: 0.92rem;
}

.icon-button, .btn {
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface);
  min-height: 44px;
  padding: 8px 14px;
}

.icon-button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.icon-sun { display: none; }
html[data-theme="dark"] .icon-moon { display: none; }
html[data-theme="dark"] .icon-sun { display: block; }

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 700;
}

html[data-theme="dark"] .btn.primary { color: #102326; }

a.btn {
  color: var(--accent-strong);
  text-decoration: none;
  display: inline-flex;
  align-items: center;
}

.btn.ghost { background: transparent; }

.btn.danger {
  background: var(--bad);
  border-color: var(--bad);
  color: #fff;
  font-weight: 700;
}

html[data-theme="dark"] .btn.danger { color: #2a1012; }

.btn:disabled, .icon-button:disabled { opacity: 0.55; cursor: not-allowed; }

.btn.start { background: var(--ok); border-color: var(--ok); color: #fff; font-weight: 700; }
html[data-theme="dark"] .btn.start { color: #102318; }

.banner {
  margin-bottom: 16px;
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--bad-soft);
  color: var(--bad);
}

.section-head {
  justify-content: space-between;
  align-items: flex-end;
  gap: 12px;
  margin: 8px 0 16px;
  flex-wrap: wrap;
}

.section-head p { margin-top: 4px; }

.metrics, .pump-grid, .groups, .sensor-grid {
  display: grid;
  gap: 14px;
}

.metrics { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.pump-grid, .groups, .sensor-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }

.card, .panel, .camera-card, .alert-item {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.card, .panel { padding: 16px 18px; }

.card { min-height: 148px; display: flex; flex-direction: column; gap: 8px; }
.card[data-tone="ok"] { box-shadow: inset 4px 0 0 var(--ok), var(--shadow); }
.card[data-tone="warn"] { box-shadow: inset 4px 0 0 var(--warn), var(--shadow); }
.card[data-tone="bad"] { box-shadow: inset 4px 0 0 var(--bad), var(--shadow); }

.card-label, .meta { color: var(--muted); font-size: 0.92rem; }

.metric-value {
  font-size: 2rem;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.03em;
}

.metric-unit { font-size: 1rem; color: var(--muted); margin-left: 4px; }

.pill {
  align-self: flex-start;
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 0.82rem;
  font-weight: 700;
  background: var(--surface-2);
  color: var(--off);
}

.pill.ok { background: var(--ok-soft); color: var(--ok); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.bad { background: var(--bad-soft); color: var(--bad); }

.block-title {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: baseline;
  margin: 22px 0 12px;
}

.summary { color: var(--muted); }

.group {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
}

.group h3 { margin: 0 0 12px; font-size: 1rem; }

.camera-list, .alert-list, .stack { display: grid; gap: 10px; }

.camera-card, .alert-item { padding: 14px; }

.camera-card { display: grid; gap: 10px; }
.camera-card.is-off { opacity: 0.72; }

.card-title {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: flex-start;
}

.card-title strong, .alert-item strong { overflow-wrap: anywhere; }

.actions { display: flex; flex-wrap: wrap; gap: 8px; }

.badge {
  min-width: 1.3rem;
  height: 1.3rem;
  padding: 0 0.35rem;
  border-radius: 999px;
  background: var(--bad);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 700;
  display: inline-grid;
  place-items: center;
}

html[data-theme="dark"] .badge { color: #2a1012; }

.empty {
  padding: 28px 16px;
  text-align: center;
  color: var(--muted);
  background: var(--surface);
  border: 1px dashed var(--line);
  border-radius: var(--radius);
}

.filters { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }

.chip {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 999px;
  min-height: 40px;
  padding: 6px 12px;
}

.chip.is-active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 700;
}

html[data-theme="dark"] .chip.is-active { color: #102326; }

.alert-item { display: grid; gap: 6px; border-left: 5px solid var(--line); text-align: left; }
button.alert-item { width: 100%; color: inherit; }
.alert-item.critical { border-left-color: var(--bad); background: var(--bad-soft); }
.alert-item.warning { border-left-color: var(--warn); background: var(--warn-soft); }
.alert-message { white-space: pre-line; }

.panel { margin-bottom: 18px; display: grid; gap: 10px; }
.panel p { color: var(--muted); }

.key-row { align-items: end; flex-wrap: wrap; }
.grow { flex: 1 1 220px; }

.field { display: grid; gap: 6px; }
.field span { font-size: 0.92rem; color: var(--muted); }

input, select {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 12px;
  min-height: 44px;
  padding: 8px 12px;
}

.check {
  display: flex;
  align-items: center;
  gap: 8px;
}

.check input { width: auto; min-height: 0; }

.form { display: grid; gap: 14px; padding: 20px; }
.form-actions { justify-content: flex-end; }

dialog {
  width: min(520px, calc(100vw - 24px));
  border: 0;
  padding: 0;
  border-radius: 20px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow);
}

dialog::backdrop { background: rgba(10, 18, 22, 0.58); }

#viewer { width: min(960px, calc(100vw - 24px)); }

.viewer-shell { display: flex; flex-direction: column; max-height: min(92vh, 900px); }
.viewer-bar, .viewer-nav { padding: 12px 14px; justify-content: space-between; }
.viewer-stage {
  background: #0c1418;
  min-height: 220px;
  aspect-ratio: 16 / 9;
  display: grid;
  place-items: center;
  overflow: hidden;
}
.viewer-stage img, .viewer-stage video {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #0c1418;
}
.viewer-note { padding: 0 14px 8px; }
.viewer-strip {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  flex: 1;
  padding-bottom: 2px;
}
.viewer-strip .chip { flex: 0 0 auto; }

.footnote { margin-top: 18px; }

.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
  z-index: 5;
}

.tabbar button {
  flex: 1;
  justify-content: center;
  flex-direction: column;
  font-size: 0.78rem;
  padding: 8px 4px;
  position: relative;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 88px;
  transform: translateX(-50%);
  width: min(440px, calc(100vw - 24px));
  background: var(--ink);
  color: var(--bg);
  border-radius: 14px;
  padding: 12px 14px;
  z-index: 20;
  text-align: center;
}

@media (max-width: 1100px) {
  .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 900px) {
  .groups, .pump-grid, .sensor-grid { grid-template-columns: 1fr; }
}

@media (min-width: 900px) {
  .app { display: grid; grid-template-columns: 240px minmax(0, 1fr); }
  .sidebar { display: flex; }
  .tabbar { display: none; }
  .main { padding: 28px 32px 48px; }
  .toast { bottom: 24px; }
}

@media (max-width: 760px) {
  .metrics { grid-template-columns: 1fr; }
  .topbar { flex-direction: column; align-items: stretch; }
  .top-actions { justify-content: space-between; }
  h1 { font-size: 1.45rem; }
  .theme-label, .bell-label {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
  }
  #viewer {
    width: 100%;
    height: 100%;
    max-width: none;
    max-height: none;
    margin: 0;
    border-radius: 0;
  }
  #viewer .viewer-shell {
    height: 100vh;
    height: 100dvh;
    max-height: none;
  }
  #viewer .viewer-stage {
    aspect-ratio: auto;
    flex: 1;
    min-height: 0;
  }
}

.logout-form { margin: 0; }

.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-card {
  width: min(420px, 100%);
}

.login-card .banner { margin-bottom: 16px; }

.top-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.top-actions form { margin: 0; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; }
.field { display: grid; gap: 6px; margin-bottom: 12px; }
.field input, .field select, .layout-row input, .layout-row select {
  font: inherit;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: inherit;
}
.board-wrap { display: grid; place-items: center; margin: 12px 0 20px; }
.board {
  width: min(100%, 640px);
  aspect-ratio: 4 / 3;
  background: #102033;
  color: #e8f2f4;
  border-radius: 18px;
  padding: 14px;
  display: grid;
  grid-template-rows: auto 1fr;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.28);
}
.board header { display: flex; justify-content: space-between; align-items: center; }
.board-grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
.board-cell {
  background: rgba(255, 255, 255, 0.06);
  border-radius: 12px;
  padding: 8px;
  display: grid;
  align-content: center;
  gap: 4px;
}
.board-cell.is-bad, .board.is-alarm { background: #8d1d2c; }
.board.is-alarm .board-cell { background: rgba(0, 0, 0, 0.18); }
.board-icon { color: #9ad7e6; }
.layout-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 8px; }
.layout-row input { width: auto; flex: 1; min-width: 140px; }
.layout-row select { width: 120px; }

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; }
}
