:root {
  color-scheme: light dark;
  --bg: #f7faf9;
  --surface: #ffffff;
  --surface-2: #edf7f5;
  --text: #10201d;
  --muted: #62736f;
  --border: #d8e5e1;
  --accent: #0f766e;
  --accent-2: #2563eb;
  --danger: #b42318;
  --warn: #b7791f;
  --ok: #047857;
  --shadow: 0 16px 45px rgba(16, 32, 29, .08);
}

[data-theme="dark"] {
  --bg: #101715;
  --surface: #17211f;
  --surface-2: #20312d;
  --text: #ecf5f2;
  --muted: #a7bbb5;
  --border: #2b403b;
  --accent: #2dd4bf;
  --accent-2: #60a5fa;
  --shadow: 0 18px 50px rgba(0, 0, 0, .25);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  letter-spacing: 0;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 24px clamp(16px, 4vw, 44px) 12px;
}

h1, h2 { margin: 0; line-height: 1.12; }
h1 { font-size: clamp(28px, 4vw, 44px); }
h2 { font-size: 20px; }
p { color: var(--muted); }

.tabs {
  display: flex;
  gap: 8px;
  padding: 8px clamp(16px, 4vw, 44px) 18px;
  overflow-x: auto;
}

button, input, select {
  font: inherit;
  min-height: 44px;
}

button, select, input {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}

button {
  padding: 10px 14px;
  cursor: pointer;
}

button:hover { border-color: var(--accent); }
.primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.icon-button {
  width: 46px;
  padding: 0;
  font-size: 22px;
}

.tab.active {
  background: var(--text);
  color: var(--bg);
}

main { padding: 0 clamp(16px, 4vw, 44px) 42px; }
.view { display: none; }
.view.active { display: block; }

.band {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: clamp(16px, 3vw, 26px);
  margin-bottom: 18px;
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 16px;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  margin: 12px 0;
}

.state {
  display: inline-flex;
  align-items: center;
  min-height: 32px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 14px;
}
.state.ok { color: var(--ok); }
.state.warn { color: var(--warn); }
.state.error { color: var(--danger); }

.summary-grid, .sensor-grid, .metric-grid, .stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
}

.tile {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px;
  background: var(--surface-2);
  min-height: 96px;
}

.tile span {
  display: block;
  color: var(--muted);
  font-size: 13px;
}

.tile strong {
  display: block;
  margin-top: 8px;
  font-size: 22px;
  line-height: 1.1;
  overflow-wrap: anywhere;
}

.debug-box {
  margin: 12px 0 0;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
  overflow: auto;
  white-space: pre-wrap;
  font-size: 13px;
  line-height: 1.45;
}

.two-col {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 24px;
}

form {
  display: grid;
  gap: 12px;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 14px;
}

input, select {
  width: 100%;
  padding: 9px 11px;
}

.check {
  display: flex;
  align-items: center;
  gap: 10px;
}
.check input { width: auto; min-height: auto; }
.check.compact {
  min-height: 44px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
}

.device-form {
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
}
.device-form h2, .device-form .toolbar { grid-column: 1 / -1; }

.admin-panel {
  margin: 12px 0 18px;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
}

.admin-user-form {
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  align-items: end;
}

.user-list {
  display: grid;
  gap: 8px;
  margin-top: 12px;
}

.user-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 10px;
  align-items: center;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
}

.user-row span {
  color: var(--muted);
  font-size: 13px;
}

.chart-wrap {
  min-height: 360px;
  margin-top: 16px;
}

.analysis-panel {
  margin-top: 18px;
}

.analysis-controls,
.table-toolbar {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 10px;
  align-items: center;
}

.chart-board {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 14px;
  margin-top: 14px;
}

.chart-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  padding: 12px;
}

.chart-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.chart-card-head h3 {
  margin: 0;
  font-size: 16px;
}

.mini-chart-wrap {
  height: 280px;
}

.empty-note {
  color: var(--muted);
  margin: 8px 0 0;
}

.table-toolbar {
  margin-top: 18px;
}

.dashboard-status {
  color: var(--muted);
  font-size: 14px;
  margin: 4px 0 14px;
}

.sensor-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 16px 0 10px;
}

.sensor-tab {
  min-height: 38px;
  padding: 8px 12px;
  font-size: 14px;
}

.sensor-tab.active {
  background: var(--accent-2);
  border-color: var(--accent-2);
  color: #fff;
}

.stat-grid {
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  margin-top: 12px;
}

.stat-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  background: var(--surface);
}

.stat-card span {
  color: var(--muted);
  display: block;
  font-size: 12px;
}

.stat-card strong {
  display: block;
  margin-top: 6px;
  font-size: 18px;
}

.data-table-wrap {
  margin-top: 18px;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 720px;
}

.data-table th,
.data-table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  font-size: 14px;
}

.data-table th {
  color: var(--muted);
  background: var(--surface-2);
  font-weight: 650;
}

.data-table tr:last-child td {
  border-bottom: 0;
}

#toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  border-radius: 8px;
  padding: 12px 16px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease;
  max-width: min(92vw, 520px);
}
#toast.show { opacity: 1; }

.prose {
  max-width: 850px;
}

@media (max-width: 760px) {
  .topbar { align-items: flex-start; }
  .two-col { grid-template-columns: 1fr; }
  .section-title { align-items: flex-start; flex-direction: column; }
  .toolbar > * { flex: 1 1 auto; }
}
