:root {
  --bg-base: #0a0e17;
  --bg-surface: #111827;
  --bg-elevated: #1a2234;
  --bg-hover: #243049;
  --border: #2a3548;
  --text-primary: #f0f4f8;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --accent: #22d3ee;
  --accent-dim: #0891b2;
  --accent-glow: rgba(34, 211, 238, 0.15);
  --success: #34d399;
  --warning: #fbbf24;
  --danger: #f87171;
  --light-on: #fde68a;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
  --font: 'Inter', system-ui, sans-serif;
  --mono: 'JetBrains Mono', monospace;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: var(--font);
  background: var(--bg-base);
  color: var(--text-primary);
  min-height: 100vh;
  line-height: 1.5;
}

.app {
  display: grid;
  grid-template-columns: 260px 1fr;
  min-height: 100vh;
}

/* Sidebar */
.sidebar {
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1.5rem;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 2rem;
}

.brand-logo {
  width: 44px;
  height: 44px;
  border-radius: 10px;
}

.brand h1 {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.brand p {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.nav { display: flex; flex-direction: column; gap: 0.25rem; flex: 1; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-family: var(--font);
  font-size: 0.9rem;
  font-weight: 500;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.15s;
  text-align: left;
}

.nav-item:hover { background: var(--bg-hover); color: var(--text-primary); }
.nav-item.active { background: var(--accent-glow); color: var(--accent); }

.nav-icon { font-size: 1rem; opacity: 0.8; }

.sidebar-footer { padding-top: 1rem; border-top: 1px solid var(--border); }

.connection-status {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--danger);
}

.status-dot.online { background: var(--success); box-shadow: 0 0 8px var(--success); }
.status-dot.offline { background: var(--danger); }

/* Main */
.main {
  padding: 2rem;
  overflow-y: auto;
  background:
    radial-gradient(ellipse at 20% 0%, rgba(34, 211, 238, 0.06) 0%, transparent 50%),
    var(--bg-base);
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 2rem;
}

.header h2 {
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.03em;
}

.subtitle { color: var(--text-secondary); font-size: 0.95rem; margin-top: 0.25rem; }

.header-stats { display: flex; gap: 0.75rem; }

.stat-pill {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.6rem 1rem;
  text-align: center;
  min-width: 80px;
}

.stat-label {
  display: block;
  font-size: 0.7rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.stat-value {
  font-size: 1.25rem;
  font-weight: 700;
  font-family: var(--mono);
}

.online-text { color: var(--success); }

/* Views */
.view { display: none; }
.view.active { display: block; }

/* Summary cards */
.summary-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.summary-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  transition: border-color 0.2s;
}

.summary-card:hover { border-color: var(--accent-dim); }

.summary-card .label {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
}

.summary-card .value {
  font-size: 2rem;
  font-weight: 700;
  font-family: var(--mono);
  letter-spacing: -0.02em;
}

.summary-card .unit {
  font-size: 1rem;
  color: var(--text-secondary);
  font-weight: 400;
}

.summary-card.temp .value { color: #fb923c; }
.summary-card.humidity .value { color: var(--accent); }
.summary-card.lights .value { color: var(--light-on); }

/* Panel */
.panel {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  box-shadow: var(--shadow);
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.25rem;
}

.panel-header h3 { font-size: 1rem; font-weight: 600; }

.panel-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.chart-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
  color: var(--text-secondary);
  cursor: pointer;
}

.chart-toggle .toggle-slider {
  width: 36px;
  height: 20px;
}

.chart-toggle .toggle-slider::before {
  width: 14px;
  height: 14px;
}

.chart-toggle input:checked + .toggle-slider::before {
  transform: translateX(16px);
}

.hidden { display: none !important; }

.lights-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 0.75rem;
}

.light-quick-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.9rem 1.1rem;
}

.light-quick-name {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-weight: 600;
  font-size: 0.95rem;
}

.light-quick-room {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 0.15rem;
}

.select {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  color: var(--text-primary);
  padding: 0.5rem 0.75rem;
  border-radius: var(--radius-sm);
  font-family: var(--font);
  font-size: 0.85rem;
  cursor: pointer;
}

.chart-container { height: 280px; position: relative; }

/* Device cards */
.devices-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 1.25rem;
}

.device-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  transition: all 0.2s;
}

.device-card:hover {
  border-color: var(--accent-dim);
  transform: translateY(-2px);
  box-shadow: var(--shadow);
}

.device-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 1.25rem;
}

.device-info h4 {
  font-size: 1.1rem;
  font-weight: 600;
  margin-bottom: 0.15rem;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}

.btn-edit {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 0.85rem;
  cursor: pointer;
  padding: 0;
  line-height: 1;
  transition: color 0.15s;
}

.btn-edit:hover {
  color: var(--accent);
}

.device-room {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.device-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.75rem;
  padding: 0.25rem 0.6rem;
  border-radius: 999px;
  font-weight: 500;
}

.device-badge.online {
  background: rgba(52, 211, 153, 0.12);
  color: var(--success);
}

.device-badge.offline {
  background: rgba(248, 113, 113, 0.12);
  color: var(--danger);
}

.device-badge.mode-auto {
  background: rgba(34, 211, 238, 0.12);
  color: var(--accent);
}

.device-badge.mode-manual {
  background: rgba(251, 191, 36, 0.12);
  color: var(--warning);
}

.device-badge.type-sensor {
  background: rgba(52, 211, 153, 0.12);
  color: var(--success);
}

.device-badge.type-control {
  background: rgba(34, 211, 238, 0.12);
  color: var(--accent);
}

.device-caps {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
}

.device-card--sensor .mode-control,
.device-card--sensor .light-control {
  display: none;
}

.device-badges {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.35rem;
}

.btn-delete {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
  width: 24px;
  height: 24px;
  border-radius: 6px;
  font-size: 0.75rem;
  cursor: pointer;
  line-height: 1;
  padding: 0;
  transition: all 0.15s;
}

.btn-delete:hover {
  border-color: var(--danger);
  color: var(--danger);
  background: rgba(248, 113, 113, 0.1);
}

.sensor-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

.sensor-box {
  background: var(--bg-surface);
  border-radius: var(--radius-sm);
  padding: 0.85rem;
  text-align: center;
}

.sensor-box .sensor-label {
  font-size: 0.7rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.sensor-box .sensor-value {
  font-size: 1.5rem;
  font-weight: 700;
  font-family: var(--mono);
  margin-top: 0.25rem;
}

.sensor-box.temp .sensor-value { color: #fb923c; }
.sensor-box.humidity .sensor-value { color: var(--accent); }
.sensor-box.rssi .sensor-value { color: var(--text-secondary); font-size: 1.25rem; }
.sensor-box.relay .sensor-value { color: var(--text-muted); }
.sensor-box.relay .sensor-value.relay-on { color: var(--light-on); }
.unit-small { font-size: 0.75rem; color: var(--text-muted); font-weight: 400; }

.mode-control {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
  background: var(--bg-surface);
  border-radius: var(--radius-sm);
  padding: 0.65rem 0.85rem;
  font-size: 0.85rem;
}

.mode-control select {
  flex: 1;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text-primary);
  padding: 0.35rem 0.5rem;
  border-radius: var(--radius-sm);
  font-family: var(--font);
}

.btn-schedule {
  background: var(--accent-glow);
  border: 1px solid var(--accent-dim);
  color: var(--accent);
  padding: 0.35rem 0.65rem;
  border-radius: var(--radius-sm);
  font-size: 0.8rem;
  cursor: pointer;
  font-family: var(--font);
}

.btn-schedule:disabled { opacity: 0.4; cursor: not-allowed; }

.schedule-info {
  font-size: 0.75rem;
  color: var(--accent);
  margin-bottom: 0.75rem;
  padding: 0.4rem 0.6rem;
  background: var(--accent-glow);
  border-radius: var(--radius-sm);
}

/* Light toggle */
.light-control {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--bg-surface);
  border-radius: var(--radius-sm);
  padding: 0.85rem 1rem;
}

.wifi-led-control {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--bg-surface);
  border-radius: var(--radius-sm);
  padding: 0.85rem 1rem;
  margin-top: 0.5rem;
  font-size: 0.9rem;
}

.light-label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 500;
}

.light-icon { font-size: 1.25rem; }
.light-icon.on { color: var(--light-on); filter: drop-shadow(0 0 6px var(--light-on)); }
.light-icon.off { color: var(--text-muted); }

.toggle {
  position: relative;
  width: 52px;
  height: 28px;
  cursor: pointer;
}

.toggle input { opacity: 0; width: 0; height: 0; }

.toggle-slider {
  position: absolute;
  inset: 0;
  background: var(--bg-hover);
  border-radius: 999px;
  transition: 0.25s;
}

.toggle-slider::before {
  content: '';
  position: absolute;
  width: 22px;
  height: 22px;
  left: 3px;
  bottom: 3px;
  background: var(--text-secondary);
  border-radius: 50%;
  transition: 0.25s;
}

.toggle input:checked + .toggle-slider {
  background: rgba(253, 230, 138, 0.25);
}

.toggle input:checked + .toggle-slider::before {
  transform: translateX(24px);
  background: var(--light-on);
  box-shadow: 0 0 10px var(--light-on);
}

.device-id {
  margin-top: 1rem;
  font-size: 0.7rem;
  color: var(--text-muted);
  font-family: var(--mono);
}

/* Empty state */
.empty-state {
  text-align: center;
  padding: 4rem 2rem;
  color: var(--text-secondary);
}

.empty-state.hidden { display: none; }

.empty-logo {
  width: 80px;
  opacity: 0.4;
  margin-bottom: 1.5rem;
}

.empty-state h3 {
  font-size: 1.25rem;
  color: var(--text-primary);
  margin-bottom: 0.5rem;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal.hidden { display: none; }

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
}

.modal-content {
  position: relative;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  width: min(420px, 92vw);
  box-shadow: var(--shadow);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.modal-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.5rem;
  cursor: pointer;
}

.form-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.form-grid label, .form-row {
  font-size: 0.85rem;
  color: var(--text-secondary);
}

.form-grid input[type="time"] {
  display: block;
  width: 100%;
  margin-top: 0.35rem;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  color: var(--text-primary);
  padding: 0.5rem;
  border-radius: var(--radius-sm);
  font-family: var(--mono);
}

.days-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 1.25rem;
}

.days-row label {
  font-size: 0.8rem;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}

.btn-primary, .btn-secondary {
  padding: 0.5rem 1rem;
  border-radius: var(--radius-sm);
  font-family: var(--font);
  font-size: 0.85rem;
  cursor: pointer;
  border: 1px solid var(--border);
}

.btn-primary {
  background: var(--accent);
  color: var(--bg-base);
  border-color: var(--accent);
  font-weight: 600;
}

.btn-secondary {
  background: var(--bg-surface);
  color: var(--text-secondary);
}

/* Responsive — legacy (replaced below) */
@media (max-width: 768px) {
  /* kept for cascade safety; full rules at end of file */
}

/* Cloud: site select + admin */
.header-actions {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
}

.site-select-wrap {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  min-width: 200px;
}

.site-select-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

.site-select {
  min-width: 200px;
}

.admin-hint {
  color: var(--text-secondary);
  font-size: 0.9rem;
  margin: 0 0 1rem;
  line-height: 1.5;
}

.admin-hint code,
.sites-table code,
.token-box code {
  font-family: var(--font-mono, 'JetBrains Mono', monospace);
  font-size: 0.85em;
  background: var(--bg-elevated);
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
}

.admin-token-row,
.admin-form {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: flex-end;
}

.admin-form label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.85rem;
  color: var(--text-secondary);
  min-width: 200px;
  flex: 1;
}

.admin-form .optional {
  color: var(--text-muted);
  font-size: 0.75rem;
}

.input {
  width: 100%;
  padding: 0.55rem 0.75rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text-primary);
  font-family: var(--font);
  font-size: 0.9rem;
}

.admin-token-status {
  margin-top: 0.75rem;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.admin-token-status.ok {
  color: var(--success);
}

.admin-result {
  margin-top: 1rem;
  padding: 1rem;
  border: 1px solid var(--accent);
  border-radius: var(--radius-sm);
  background: color-mix(in srgb, var(--accent) 8%, var(--bg-elevated));
}

.token-box {
  display: grid;
  gap: 0.5rem;
  margin: 0.75rem 0;
}

.token-box > div {
  display: grid;
  grid-template-columns: 110px 1fr;
  gap: 0.5rem;
  align-items: start;
  font-size: 0.85rem;
}

.token-box span {
  color: var(--text-muted);
}

.token-box code {
  word-break: break-all;
}

.sites-table-wrap {
  overflow-x: auto;
}

.sites-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.sites-table th,
.sites-table td {
  text-align: left;
  padding: 0.65rem 0.5rem;
  border-bottom: 1px solid var(--border);
}

.sites-table th {
  color: var(--text-muted);
  font-weight: 500;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.sites-table .muted {
  color: var(--text-muted);
  text-align: center;
  padding: 1.5rem;
}

.btn-sm {
  padding: 0.35rem 0.65rem;
  font-size: 0.8rem;
}

@media (max-width: 768px) {
  /* superseded by responsive block at end */
}

/* Login */
.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background:
    radial-gradient(ellipse at top, rgba(34, 211, 238, 0.12), transparent 55%),
    var(--bg-base);
}

.login-card {
  width: min(100%, 400px);
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.login-logo {
  width: 64px;
  height: 64px;
  align-self: center;
}

.login-card h1 {
  text-align: center;
  margin: 0;
  font-size: 1.5rem;
}

.login-sub {
  text-align: center;
  color: var(--text-secondary);
  margin: 0 0 0.5rem;
  font-size: 0.9rem;
}

.login-card label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.85rem;
  color: var(--text-secondary);
}

.login-submit {
  width: 100%;
  margin-top: 0.5rem;
  padding: 0.7rem 1rem;
}

.login-error {
  color: var(--danger, #f87171);
  font-size: 0.85rem;
  margin: 0;
}

.user-chip {
  margin-bottom: 0.5rem;
}

.user-chip-name {
  font-weight: 600;
  font-size: 0.9rem;
}

.user-chip-role {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: capitalize;
}

.admin-form-stack {
  flex-direction: column;
  align-items: stretch;
}

.admin-form-stack label {
  width: 100%;
}

.site-assign {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.75rem 1rem;
  margin: 0;
}

.site-assign legend {
  padding: 0 0.35rem;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.checkbox-list {
  display: grid;
  gap: 0.4rem;
}

.checkbox-list label {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
  color: var(--text-secondary);
}

.nav-item.hidden-role {
  display: none !important;
}

.modal-content--wide {
  width: min(520px, 94vw);
}

.password-field {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.password-field .input {
  flex: 1;
}

.password-toggle {
  flex-shrink: 0;
  white-space: nowrap;
  min-width: 4.25rem;
}

.user-actions {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}

#editUserUsername[readonly] {
  opacity: 0.8;
  cursor: default;
}

.login-hint {
  margin: 0.25rem 0 0;
  font-size: 0.75rem;
  color: var(--text-muted);
  line-height: 1.4;
}

.device-badge.mode-auto {
  color: #fbbf24;
}

/* ===== Responsive shell ===== */
html { -webkit-text-size-adjust: 100%; }
body { overflow-x: hidden; }
img, canvas { max-width: 100%; height: auto; }

.mobile-topbar {
  display: none;
  align-items: center;
  gap: 0.75rem;
  margin: -0.25rem 0 1rem;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--border);
}

.mobile-topbar-brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex: 1;
  min-width: 0;
}

.mobile-topbar-logo {
  width: 28px;
  height: 28px;
  border-radius: 6px;
}

.mobile-topbar-status { display: flex; align-items: center; }

.menu-toggle {
  width: 42px;
  height: 42px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  display: inline-flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  flex-shrink: 0;
  padding: 0;
}

.menu-toggle span {
  display: block;
  width: 18px;
  height: 2px;
  background: var(--text-primary);
  border-radius: 1px;
}

.sidebar-backdrop { display: none; }

@media (max-width: 1024px) {
  .summary-grid {
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  }
  .devices-grid,
  .lights-grid {
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  }
}

@media (max-width: 900px) {
  .app {
    grid-template-columns: 1fr;
    min-height: 100dvh;
  }

  .mobile-topbar { display: flex; }

  .sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 190;
    background: rgba(0, 0, 0, 0.55);
    border: none;
    padding: 0;
    margin: 0;
    cursor: pointer;
  }

  .app.nav-open .sidebar-backdrop { display: block; }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(280px, 86vw);
    z-index: 200;
    transform: translateX(-105%);
    transition: transform 0.22s ease;
    border-right: 1px solid var(--border);
    box-shadow: var(--shadow);
    overflow-y: auto;
    flex-direction: column;
    padding: 1.25rem;
  }

  .app.nav-open .sidebar { transform: translateX(0); }

  .sidebar-footer {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-top: auto;
  }

  .brand { margin-bottom: 1.25rem; }
  .nav { flex-direction: column; flex: 1; }

  .main {
    padding: 1rem 1rem 2rem;
    width: 100%;
    min-width: 0;
  }

  .header {
    flex-direction: column;
    align-items: stretch;
    gap: 1rem;
    margin-bottom: 1.25rem;
  }

  .header h2 { font-size: 1.35rem; }

  .header-actions,
  .header-stats {
    width: 100%;
    flex-wrap: wrap;
  }

  .site-select-wrap,
  .site-select {
    width: 100%;
    min-width: 0;
  }

  .stat-pill {
    flex: 1 1 0;
    min-width: 72px;
  }

  .panel { padding: 1rem; }

  .panel-header {
    flex-wrap: wrap;
    gap: 0.75rem;
  }

  .panel-actions {
    width: 100%;
    flex-wrap: wrap;
  }

  .summary-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.75rem;
  }

  .summary-card { padding: 1rem; }
  .summary-card .value { font-size: 1.5rem; }

  .devices-grid,
  .lights-grid { grid-template-columns: 1fr; }

  .admin-form,
  .admin-token-row {
    flex-direction: column;
    align-items: stretch;
  }

  .admin-form label {
    min-width: 0;
    width: 100%;
  }

  .sites-table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0 -0.25rem;
    padding-bottom: 0.25rem;
  }

  .sites-table { min-width: 560px; }

  .modal-content,
  .modal-content--wide {
    width: min(100vw - 1.5rem, 520px);
    max-height: min(90dvh, 900px);
    overflow-y: auto;
    padding: 1.15rem;
  }

  .schedule-grid,
  .form-grid { grid-template-columns: 1fr; }

  .chart-wrap,
  .chart-container { min-height: 220px; }

  body.nav-lock { overflow: hidden; }
}

@media (max-width: 560px) {
  .summary-grid { grid-template-columns: 1fr; }
  .sensor-row { grid-template-columns: 1fr; }
  .mode-control { flex-wrap: wrap; }
  .mode-control select {
    width: 100%;
    flex: 1 1 100%;
  }
  .login-card { padding: 1.35rem 1.1rem; }
  .login-card h1 { font-size: 1.3rem; }
  .password-field { flex-wrap: wrap; }
  .password-toggle { width: 100%; }
  .user-actions {
    flex-direction: column;
    align-items: stretch;
  }
  .user-actions .btn-secondary,
  .user-actions .btn-primary { width: 100%; }
  .modal-actions { flex-direction: column-reverse; }
  .modal-actions .btn-primary,
  .modal-actions .btn-secondary { width: 100%; }
  .header-stats .stat-pill { min-width: calc(50% - 0.4rem); }
}

@media (min-width: 901px) {
  .mobile-topbar { display: none !important; }
  .sidebar-backdrop { display: none !important; }
  .app.nav-open .sidebar { transform: none; }
}
