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

:root {
  --bg:       #0f1117;
  --surface:  #1a1d27;
  --border:   #2d3148;
  --text:     #e2e8f0;
  --muted:    #64748b;
  --accent:   #6366f1;
  --accent2:  #4f46e5;
  --on:       #22c55e;
  --off:      #475569;
  --danger:   #ef4444;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, sans-serif;
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ── Sidebar ── */
.sidebar {
  width: 220px;
  min-width: 220px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1.25rem 0;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.25rem 1.25rem;
  border-bottom: 1px solid var(--border);
}

.logo { font-size: 1.2rem; font-weight: 700; }

.ws-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--muted);
  transition: background 0.3s;
}
.ws-dot.online  { background: var(--on); }
.ws-dot.offline { background: var(--muted); }

.nav-links { padding: 1rem 0.75rem; display: flex; flex-direction: column; gap: 4px; }

.nav-btn {
  display: flex; align-items: center; gap: 0.6rem;
  background: none; border: none; color: var(--muted);
  padding: 0.55rem 0.75rem; border-radius: 8px;
  font-size: 0.875rem; cursor: pointer; text-align: left; width: 100%;
  transition: background 0.15s, color 0.15s;
}
.nav-btn svg { width: 16px; height: 16px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.nav-btn:hover  { background: var(--border); color: var(--text); }
.nav-btn.active { background: var(--accent); color: #fff; }

.sidebar-section { padding: 1rem 1.25rem 0; flex: 1; overflow-y: auto; }
.section-label { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin-bottom: 0.5rem; }

.room-btn {
  display: block; width: 100%; background: none; border: none;
  color: var(--muted); text-align: left; padding: 0.4rem 0.5rem;
  border-radius: 6px; font-size: 0.85rem; cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.room-btn:hover  { background: var(--border); color: var(--text); }
.room-btn.active { color: var(--accent); font-weight: 600; }

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

/* ── Main ── */
.main { flex: 1; overflow-y: auto; padding: 2rem; }

.view { display: none; }
.view.active { display: block; }

.view-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 1.5rem;
}
.view-header h1 { font-size: 1.4rem; font-weight: 700; }

/* ── Buttons ── */
.btn-primary {
  background: var(--accent); color: #fff; border: none;
  padding: 0.55rem 1.1rem; border-radius: 8px; font-size: 0.875rem;
  font-weight: 600; cursor: pointer; transition: background 0.15s;
}
.btn-primary:hover { background: var(--accent2); }

.btn-ghost {
  background: none; color: var(--muted); border: 1px solid var(--border);
  padding: 0.5rem 1rem; border-radius: 8px; font-size: 0.875rem;
  cursor: pointer; transition: all 0.15s;
}
.btn-ghost:hover { color: var(--text); border-color: var(--text); }

/* ── Device grid ── */
.device-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 1rem;
}

.device-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.1rem;
  cursor: pointer;
  transition: border-color 0.2s, transform 0.1s;
  position: relative;
}
.device-card:hover { border-color: var(--accent); transform: translateY(-1px); }
.device-card.on { border-color: var(--on); }

.device-name { font-weight: 600; font-size: 0.95rem; margin-bottom: 0.25rem; }
.device-room { font-size: 0.75rem; color: var(--muted); margin-bottom: 0.75rem; }

.device-toggle {
  display: flex; align-items: center; justify-content: space-between;
}
.toggle-label { font-size: 0.8rem; font-weight: 600; }
.toggle-label.on  { color: var(--on); }
.toggle-label.off { color: var(--muted); }

.toggle {
  width: 36px; height: 20px; background: var(--off);
  border-radius: 10px; position: relative; cursor: pointer;
  transition: background 0.2s; border: none;
}
.toggle::after {
  content: ''; position: absolute;
  width: 14px; height: 14px; background: #fff;
  border-radius: 50%; top: 3px; left: 3px;
  transition: transform 0.2s;
}
.toggle.on { background: var(--on); }
.toggle.on::after { transform: translateX(16px); }

.device-edit {
  position: absolute; top: 0.6rem; right: 0.6rem;
  background: none; border: none; color: var(--muted);
  font-size: 0.75rem; cursor: pointer; padding: 2px 5px;
  border-radius: 4px;
}
.device-edit:hover { background: var(--border); color: var(--text); }

/* ── Rules list ── */
.rules-list { display: flex; flex-direction: column; gap: 0.75rem; }

.rule-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 1rem 1.25rem;
  display: flex; align-items: center; justify-content: space-between;
}
.rule-info { flex: 1; }
.rule-name { font-weight: 600; margin-bottom: 0.2rem; }
.rule-meta { font-size: 0.8rem; color: var(--muted); }
.rule-actions { display: flex; gap: 0.5rem; align-items: center; }

/* ── Events list ── */
.events-list { display: flex; flex-direction: column; gap: 0.4rem; }

.event-row {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 0.65rem 1rem;
  display: flex; align-items: center; gap: 1rem;
  font-size: 0.85rem;
}
.event-ts { color: var(--muted); min-width: 160px; font-size: 0.78rem; }
.event-type { color: var(--accent); font-weight: 600; min-width: 120px; }
.event-detail { color: var(--text); }

/* ── Modal ── */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
.modal.hidden { display: none; }

.modal-box {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 1.75rem; width: 100%; max-width: 480px;
  max-height: 90vh; overflow-y: auto;
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 1.25rem;
}
.modal-header h2 { font-size: 1.1rem; font-weight: 700; }
.modal-close {
  background: none; border: none; color: var(--muted);
  font-size: 1rem; cursor: pointer; padding: 4px 8px; border-radius: 6px;
}
.modal-close:hover { background: var(--border); color: var(--text); }

.form-grid { display: flex; flex-direction: column; gap: 1rem; }

.form-row { display: flex; flex-direction: column; gap: 0.3rem; }
.form-row label { font-size: 0.8rem; color: var(--muted); }
.form-row.two-col { flex-direction: row; gap: 1rem; }
.form-row.two-col > div { flex: 1; display: flex; flex-direction: column; gap: 0.3rem; }
.form-row.two-col > div label { font-size: 0.8rem; color: var(--muted); }

.hint { color: var(--muted); font-size: 0.75rem; }

input, select, textarea {
  background: var(--bg); border: 1px solid var(--border);
  color: var(--text); border-radius: 8px; padding: 0.6rem 0.75rem;
  font-size: 0.9rem; width: 100%; font-family: inherit;
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--accent);
}
select option { background: var(--bg); }

.cron-help { background: var(--bg); border-radius: 8px; padding: 0.6rem 0.75rem; }
.cron-help p { font-size: 0.78rem; color: var(--muted); line-height: 1.6; }
.cron-help code { color: var(--accent); }

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

/* ── Shutter / Curtain ── */
.shutter-state {
  font-size: 0.8rem; color: var(--muted); margin-bottom: 0.6rem; font-weight: 600;
}
.shutter-controls {
  display: flex; gap: 0.4rem; justify-content: center;
}
.shutter-btn {
  flex: 1; background: var(--border); border: none; color: var(--text);
  border-radius: 6px; padding: 0.45rem 0; font-size: 1rem;
  cursor: pointer; transition: background 0.15s;
}
.shutter-btn:hover { background: var(--accent); }
.shutter-btn.stop { background: var(--off); color: #fff; }
.shutter-btn.stop:hover { background: var(--danger); }

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

.camera-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}

.camera-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.75rem 1rem;
}
.camera-name { font-weight: 600; font-size: 0.95rem; }
.camera-room { font-size: 0.75rem; color: var(--muted); }

.camera-video-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 16/9;
  background: #000;
}
.camera-video-wrap video {
  width: 100%; height: 100%; object-fit: cover;
}
.camera-overlay {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,0.6);
  flex-direction: column; gap: 0.5rem;
  font-size: 0.85rem; color: var(--muted);
}
.camera-overlay.hidden { display: none; }

.camera-footer {
  padding: 0.6rem 1rem;
  display: flex; align-items: center; justify-content: space-between;
  border-top: 1px solid var(--border);
}
.camera-badge {
  font-size: 0.72rem; font-weight: 600; padding: 2px 8px;
  border-radius: 20px; background: var(--border); color: var(--muted);
}
.camera-badge.person { background: #422006; color: #fb923c; }
.camera-badge.motion { background: #1e1b4b; color: #818cf8; }

.nest-status {
  font-size: 0.8rem; color: var(--muted);
}
.nest-status.connected { color: var(--on); }

.nest-alert {
  margin-top: 1rem; padding: 0.75rem 1rem;
  background: #422006; border: 1px solid #f97316;
  border-radius: 8px; font-size: 0.875rem; color: #fed7aa;
}
.nest-alert.hidden { display: none; }

/* ── Responsive ── */
@media (max-width: 640px) {
  .sidebar { width: 60px; min-width: 60px; }
  .sidebar-header .logo, .nav-btn span, .sidebar-section, .sidebar-footer { display: none; }
  .nav-btn { justify-content: center; padding: 0.6rem; }
  .main { padding: 1rem; }
}
