:root {
  --bg: #07101c;
  --bg-raise: #101a2b;
  --line: #24344d;
  --text: #e7eef8;
  --muted: #93a4bb;
  --teal: #2dd4bf;
  --teal-dim: #115e59;
  --amber: #f5c16c;
  --red: #f97066;
  --green: #3dd68c;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.28);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(1200px 500px at 10% -10%, rgba(45, 212, 191, 0.08), transparent 50%),
    var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, sans-serif;
  line-height: 1.45;
}

h1, h2, h3 { font-weight: 650; letter-spacing: -0.02em; }
h1 { font-size: 1.55rem; margin: 0 0 0.25rem; }
h2 { font-size: 1.05rem; margin: 0; }

a { color: var(--teal); }

.skip-link {
  position: absolute;
  left: -999px;
  top: 0;
}
.skip-link:focus {
  left: 12px;
  top: 12px;
  z-index: 20;
  background: #fff;
  color: #000;
  padding: 8px 12px;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: rgba(7, 16, 28, 0.92);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(10px);
}

.header-inner,
.app-main {
  width: min(1120px, 100%);
  margin: 0 auto;
}

.header-inner { padding: 12px 16px 14px; }

.header-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  font-weight: 700;
  text-decoration: none;
  margin-right: auto;
}

.top-nav { display: none; gap: 4px; flex-wrap: wrap; }
.top-nav a {
  color: var(--muted);
  text-decoration: none;
  padding: 8px 10px;
  border-radius: 999px;
  font-size: 0.92rem;
}
.top-nav a.is-active,
.top-nav a:hover { color: var(--text); background: #182536; }

.device-picker {
  display: grid;
  gap: 4px;
  margin-top: 12px;
  color: var(--muted);
  font-size: 0.8rem;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border-radius: 999px;
  padding: 6px 10px;
  font-size: 0.78rem;
  font-weight: 650;
  background: #182536;
  white-space: nowrap;
}
.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
}
.status-pill.is-on { color: #d1fae5; background: #064e3b; }
.status-pill.is-on .status-dot { background: var(--green); }
.status-pill.is-off { color: #ffe4e6; background: #4c0519; }
.status-pill.is-off .status-dot { background: var(--red); }

.app-main {
  padding: 16px 16px calc(96px + env(safe-area-inset-bottom));
}

.bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 12;
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 2px;
  padding: 6px 6px calc(6px + env(safe-area-inset-bottom));
  background: rgba(10, 18, 32, 0.96);
  border-top: 1px solid var(--line);
}
.bottom-nav a {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-height: 52px;
  justify-content: center;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.58rem;
  border-radius: 12px;
}
.bottom-nav a i { font-size: 1.15rem; }
.bottom-nav a.is-active { color: var(--teal); background: #132033; }

.panel {
  background: var(--bg-raise);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 16px;
  box-shadow: var(--shadow);
}
.panel + .panel,
.panel + .settings-form { margin-top: 12px; }

.page-head {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 10px;
  align-items: flex-end;
  margin-bottom: 14px;
}

.muted { color: var(--muted); margin: 0; }
.section-label {
  margin: 22px 0 10px;
  color: var(--muted);
  font-size: 0.78rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.feature-grid,
.metric-grid,
.trend-grid,
.account-grid {
  display: grid;
  gap: 12px;
}

.feature-card,
.metric-card {
  background: var(--bg-raise);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 16px;
  min-height: 132px;
}
.feature-card.occ { box-shadow: inset 4px 0 0 var(--teal); }
.feature-card.h2s { box-shadow: inset 4px 0 0 var(--amber); }
.feature-card.is-high,
.metric-card.is-high,
.reading-card.is-high,
.feature-card.is-offline,
.reading-card.is-offline { box-shadow: inset 4px 0 0 var(--red); }
.feature-card.is-low,
.metric-card.is-low,
.reading-card.is-low { box-shadow: inset 4px 0 0 #60a5fa; }

.feature-label,
.metric-label {
  color: var(--muted);
  font-size: 0.85rem;
  display: flex;
  gap: 6px;
  align-items: center;
}
.feature-value {
  font-size: clamp(3.2rem, 14vw, 4.6rem);
  font-weight: 720;
  line-height: 0.95;
  letter-spacing: -0.05em;
  font-variant-numeric: tabular-nums;
  margin: 8px 0;
}
.feature-note { color: var(--muted); margin: 0; }
.metric-value {
  font-size: 1.8rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  margin-top: 8px;
}
.metric-unit { color: var(--muted); font-size: 0.85rem; }

.metric-grid { grid-template-columns: 1fr 1fr; }
.readings-panel { margin-top: 0; }
.reading-grid {
  display: flex;
  flex-wrap: nowrap;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
}
.reading-card {
  flex: 1 0 118px;
  min-width: 118px;
  background: rgba(7, 16, 28, 0.55);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 10px 12px;
}
.reading-card.occ { box-shadow: inset 3px 0 0 var(--teal); }
.reading-card.h2s { box-shadow: inset 3px 0 0 var(--amber); }
.reading-card .metric-label,
.reading-card .metric-unit {
  font-size: 0.75rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.reading-card .metric-value {
  font-size: 1.35rem;
  margin: 4px 0 2px;
  line-height: 1.1;
}

.control-panel { margin-top: 14px; }
.panel-head,
.control-grid,
.danger-row,
.pager,
.filter-bar {
  display: grid;
  gap: 10px;
}
.panel-head {
  grid-template-columns: 1fr auto;
  align-items: center;
  margin-bottom: 12px;
}
.control-grid { grid-template-columns: 1fr 1fr; margin-bottom: 10px; }
.danger-row { grid-template-columns: 1fr 1fr; margin-bottom: 10px; }

.actuator-btn,
.pulse-btn,
.btn {
  min-height: 48px;
  touch-action: manipulation;
}
.actuator-btn,
.pulse-btn {
  width: 100%;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: #0c1524;
  color: var(--text);
  text-align: left;
  padding: 10px 12px;
}
.actuator-name,
.pulse-btn {
  display: flex;
  gap: 8px;
  align-items: center;
  font-weight: 650;
}
.actuator-state {
  display: block;
  font-size: 1.15rem;
  font-variant-numeric: tabular-nums;
}
.actuator-sync {
  display: block;
  color: var(--muted);
  font-size: 0.75rem;
  min-height: 1em;
}
.actuator-btn.is-on[data-tone="fan"] { background: #065f46; border-color: #10b981; }
.actuator-btn.is-on[data-tone="alarm"] { background: #9f1239; border-color: #fb7185; }
.actuator-btn.is-on[data-tone="warn"] { background: #9a3412; border-color: #fdba74; }
.actuator-btn.is-pending { outline: 2px dashed var(--amber); outline-offset: 2px; }
.actuator-btn:disabled,
.pulse-btn:disabled,
.btn:disabled { opacity: 0.55; }

.pulse-btn { justify-content: flex-start; min-height: 52px; }
.stop-btn { width: 100%; margin-top: 4px; font-weight: 700; }
.command-hint { margin-top: 10px; }

.pending-banner,
.poll-error {
  border-radius: 12px;
  padding: 10px 12px;
  margin: 0 0 12px;
}
.pending-banner { background: #3f2a08; color: #fde68a; }
.poll-error { background: #4c0519; color: #fecdd3; }

.trend-grid { margin-top: 14px; }
.trend-card h2 { margin-bottom: 8px; }
.spark { width: 100%; height: 72px; display: block; }
.spark path { fill: none; stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; }
.spark-caption { color: var(--muted); font-size: 0.8rem; margin: 6px 0 0; }

.rules-list { display: grid; gap: 12px; }
.rule-card {
  background: var(--bg-raise);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 14px;
}
.rule-card h2 { margin-bottom: 4px; }
.rule-line {
  display: grid;
  gap: 8px;
  margin-top: 12px;
}
.form-check.form-switch {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  margin: 0;
  padding-left: 0;
}
.form-check .form-check-input { margin: 0; float: none; width: 2.6em; height: 1.4em; }

.settings-form,
.filter-bar label {
  display: grid;
  gap: 6px;
}
.settings-form label,
.filter-bar label { color: var(--muted); font-size: 0.9rem; }
.split-fields { display: grid; gap: 10px; }

.form-control,
.form-select {
  background: #0b1220;
  color: var(--text);
  border-color: #314863;
  min-height: 48px;
  font-size: 16px;
}
.form-control:focus,
.form-select:focus {
  background: #0b1220;
  color: var(--text);
  border-color: var(--teal);
  box-shadow: 0 0 0 0.2rem rgba(45, 212, 191, 0.18);
}

.endpoint {
  display: block;
  margin: 10px 0;
  padding: 12px;
  border-radius: 12px;
  background: #0b1220;
  color: var(--amber);
  overflow-wrap: anywhere;
}
.plain-list { margin: 0; padding-left: 1.1rem; color: var(--muted); }
.plain-list li + li { margin-top: 6px; }

.save-bar {
  position: sticky;
  bottom: calc(74px + env(safe-area-inset-bottom));
  margin-top: 12px;
}
.save-bar .btn { width: 100%; min-height: 52px; font-weight: 700; }

.filter-bar { grid-template-columns: 1fr 1fr; margin-bottom: 8px; }
.filter-bar .btn { align-self: end; }
.log-count { margin: 8px 0; }

.log-card {
  background: var(--bg-raise);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 12px;
  margin-bottom: 10px;
}
.log-card strong { display: block; margin-bottom: 6px; }
.log-metrics {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 10px;
  font-variant-numeric: tabular-nums;
}
.table-wrap { display: none; }
.log-table { color: var(--text); }
.log-table thead th {
  color: var(--muted);
  font-weight: 600;
  white-space: nowrap;
}
.pager {
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  margin-top: 12px;
}
#page-label { text-align: center; color: var(--muted); }

.role-pill {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 4px 8px;
  background: #1e293b;
  color: var(--amber);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.user-list { display: grid; gap: 10px; }
.user-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px 10px;
  align-items: center;
  padding-top: 10px;
  border-top: 1px solid var(--line);
}
.user-row.is-off { opacity: 0.72; }
.user-actions { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; }
.role-pill.is-off { color: #fda4af; }

.auth-body {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
}
.auth-card { width: min(440px, 100%); }
.auth-card h1 { margin-top: 8px; }
.credential {
  font-size: 1.15rem;
  background: #0b1220;
  border-radius: 12px;
  padding: 10px 12px;
}
.credential span {
  display: block;
  color: var(--muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.toast-stack {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: calc(78px + env(safe-area-inset-bottom));
  z-index: 30;
  display: grid;
  gap: 8px;
}
.app-toast {
  background: #042f2e;
  color: #ccfbf1;
  border-radius: 12px;
  padding: 12px 14px;
  box-shadow: var(--shadow);
}
.app-toast.bad { background: #4c0519; color: #ffe4e6; }

.btn-outline-light { color: var(--text); border-color: #64748b; }
.btn-outline-warning { color: #fcd34d; border-color: #d97706; }
.btn-outline-secondary { color: #cbd5e1; border-color: #64748b; }
.btn-outline-danger { color: #fda4af; border-color: #e11d48; }
.account-grid .btn { margin-top: 8px; margin-right: 8px; }

.empty-state p { margin-top: 8px; }

@media (min-width: 720px) {
  .feature-grid { grid-template-columns: 1fr 1fr; }
  .metric-grid { grid-template-columns: repeat(3, 1fr); }
  .trend-grid { grid-template-columns: repeat(3, 1fr); }
  .account-grid { grid-template-columns: 1fr 1fr; align-items: start; }
  .rule-line { grid-template-columns: 150px 120px 1fr; align-items: center; }
  .split-fields { grid-template-columns: 1fr 1fr; }
  .filter-bar { grid-template-columns: 180px 180px auto auto; }
  .log-cards { display: none; }
  .table-wrap { display: block; overflow-x: auto; }
  .control-grid.latches { grid-template-columns: repeat(4, 1fr); }
  .toast-stack { left: auto; right: 16px; width: min(360px, calc(100% - 32px)); }
}

@media (min-width: 900px) {
  .top-nav { display: flex; }
  .bottom-nav { display: none; }
  .app-main { padding-bottom: 40px; }
  .save-bar { position: static; }
  .save-bar .btn { width: auto; min-width: 180px; }
  h1 { font-size: 1.85rem; }
}

.chart-panel { margin-top: 14px; }
.chart-wrap { width: 100%; }
.chart-caption { margin: 8px 0 0; }
.chip-row { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.chip {
  border: 1px solid var(--line);
  background: transparent;
  color: var(--text);
  border-radius: 999px;
  padding: 6px 12px;
  min-height: 40px;
}
.chip.is-on {
  background: color-mix(in srgb, var(--swatch, #134e4a) 24%, transparent);
  color: var(--swatch, #ccfbf1);
  border-color: var(--swatch, #0f766e);
}
.chip:disabled { opacity: 0.55; }

.filter-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 12px;
}
.filter-grid label { color: var(--muted); font-size: 0.85rem; }
.filter-grid .btn { align-self: end; min-height: 44px; }

.select-bar { display: flex; flex-wrap: wrap; gap: 8px; margin: 0 0 12px; }
.select-bar .btn { min-height: 44px; }
.pick-label {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
  color: var(--muted);
}
.row-check { width: 1.15rem; height: 1.15rem; }
.log-card.is-picked,
tr.is-picked td { background: rgba(244, 63, 94, 0.14); }
.log-table tbody tr { cursor: pointer; }

.month-bar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 8px;
  align-items: center;
  margin-bottom: 10px;
}
.month-bar h2 { margin: 0; text-align: center; font-size: 1.15rem; }
.calendar-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal-dow { text-align: center; color: var(--muted); font-size: 0.72rem; padding-bottom: 4px; }
.cal-pad { min-height: 8px; }
.cal-day {
  min-height: 58px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: #0b1220;
  color: var(--text);
  padding: 4px;
  text-align: left;
}
.cal-day strong { display: block; font-size: 0.95rem; }
.cal-day small { display: block; color: var(--muted); font-size: 0.62rem; line-height: 1.2; }
.cal-day.is-off { background: #4c0519; }
.cal-day.is-off small { color: #fecdd3; }
.cal-day.is-windows { background: #422006; }
.cal-day.is-windows small { color: #fde68a; }
.cal-day.is-today { outline: 2px solid var(--teal); }
.cal-day.is-selected { border-color: #5eead4; }
.legend { margin: 10px 0 0; }
.mode-picks { display: grid; gap: 8px; margin: 10px 0; }
.window-row {
  display: grid;
  grid-template-columns: 1fr auto 1fr auto;
  gap: 6px;
  align-items: center;
  margin-bottom: 8px;
}
#save-day { margin-top: 8px; min-height: 48px; }

@media (min-width: 720px) {
  .filter-grid { grid-template-columns: repeat(4, minmax(0, 1fr)) auto auto; }
  .mode-picks { grid-template-columns: repeat(3, 1fr); }
  .cal-day { min-height: 72px; }
}

.advice-search { margin-top: 10px; }
.advice-capacity { display: grid; gap: 4px; margin: 12px 0; color: var(--muted); font-size: 0.85rem; max-width: 280px; }
.place-results { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.advice-head { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.advice-head h2 { margin: 0; }
.weather-strip { display: flex; gap: 8px; overflow-x: auto; margin: 12px 0; }
.weather-chip {
  flex: 0 0 auto;
  min-width: 108px;
  background: #0b1220;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 8px 10px;
}
.weather-chip span { display: block; color: var(--muted); font-size: 0.75rem; }
.weather-chip strong { font-size: 1rem; }
.advice-list { display: grid; gap: 10px; margin-top: 12px; }
.advice-card h2 { margin: 0 0 4px; font-size: 1.05rem; }
.advice-band { margin: 0 0 8px; font-size: 1.45rem; font-weight: 750; color: var(--teal); }
.advice-card.is-guide { border-color: #92400e; }
.advice-in { color: #a7f3d0; }
.advice-out { color: #fcd34d; }
.advice-foot { margin-top: 12px; }
#place-save { min-height: 48px; }

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