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

:root {
  --bg: #1a1a2e;
  --bg-card: #16213e;
  --bg-card-hover: #1a2744;
  --text: #eee;
  --text-muted: #8892a4;
  --primary: #2563eb;
  --success: #10b981;
  --danger: #ef4444;
  --warning: #f59e0b;
  --info: #06b6d4;
  --radius: 16px;
  --nav-height: 64px;
  --header-height: 56px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}

/* --- Header --- */
header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--header-height);
  background: var(--bg-card);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  z-index: 100;
  border-bottom: 1px solid rgba(255,255,255,0.05);
}

header h1 {
  font-size: 1.2rem;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.75rem;
  color: var(--text-muted);
}

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

.status.online .dot {
  background: var(--success);
}

/* --- Main --- */
main {
  margin-top: var(--header-height);
  margin-bottom: var(--nav-height);
  padding: 16px;
  min-height: calc(100vh - var(--header-height) - var(--nav-height));
}

.tab-content {
  display: none;
}

.tab-content.active {
  display: block;
}

/* --- Sensor Cards --- */
#sensors-container {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.sensor-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  min-height: 90px;
  transition: background 0.2s;
}

.sensor-card.loading {
  opacity: 0.5;
}

.sensor-name {
  font-size: 0.95rem;
  color: var(--text-muted);
  font-weight: 500;
}

.sensor-value {
  font-size: 2.5rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.sensor-trend {
  font-size: 0.8rem;
  margin-top: 4px;
}

.temp-cold { color: #60a5fa; }
.temp-cool { color: var(--info); }
.temp-ok { color: var(--success); }
.temp-warm { color: var(--warning); }
.temp-hot { color: var(--danger); }

.meta-text {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.8rem;
  margin-top: 16px;
}

/* --- Control Buttons --- */
.shutter-status {
  text-align: center;
  padding: 24px 0;
}

.shutter-icon {
  font-size: 3rem;
  opacity: 0.4;
  letter-spacing: -8px;
}

.shutter-state-text {
  font-size: 1.1rem;
  color: var(--text-muted);
  margin-top: 8px;
}

.control-buttons {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 0 4px;
}

.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  border: none;
  border-radius: var(--radius);
  font-size: 1.2rem;
  font-weight: 700;
  padding: 20px;
  cursor: pointer;
  color: white;
  transition: transform 0.1s, opacity 0.2s;
  min-height: 64px;
  -webkit-user-select: none;
  user-select: none;
}

.btn:active {
  transform: scale(0.97);
}

.btn:disabled {
  opacity: 0.4;
  pointer-events: none;
}

.btn-icon {
  font-size: 1.4rem;
}

.btn-open { background: var(--success); }
.btn-close { background: var(--danger); }
.btn-stop { background: #4b5563; }
.btn-cancel { background: #374151; }
.btn-confirm { background: var(--primary); }
.btn-save { background: var(--primary); }

/* --- Thresholds --- */
.threshold-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 12px;
}

.threshold-label {
  font-size: 0.9rem;
  color: var(--text-muted);
  margin-bottom: 12px;
}

.threshold-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
}

.threshold-value {
  font-size: 2.2rem;
  font-weight: 700;
  min-width: 100px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.btn-round {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.15);
  background: var(--bg-card-hover);
  color: var(--text);
  font-size: 1.5rem;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-user-select: none;
  user-select: none;
}

.btn-round:active {
  background: var(--primary);
  border-color: var(--primary);
}

.threshold-meta {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 10px;
  text-align: center;
}

.threshold-actions {
  margin-top: 16px;
}

/* --- Dialog --- */
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}

.dialog-overlay.hidden {
  display: none;
}

.dialog {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 24px;
  max-width: 320px;
  width: 100%;
  text-align: center;
}

.dialog p {
  font-size: 1.1rem;
  margin-bottom: 20px;
}

.dialog-buttons {
  display: flex;
  gap: 12px;
}

.dialog-buttons .btn {
  flex: 1;
  padding: 14px;
  font-size: 1rem;
}

/* --- Toast --- */
.toast {
  position: fixed;
  bottom: calc(var(--nav-height) + 16px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-card);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 12px;
  padding: 12px 24px;
  font-size: 0.9rem;
  z-index: 500;
  transition: opacity 0.3s;
  max-width: calc(100% - 32px);
  text-align: center;
}

.toast.hidden {
  opacity: 0;
  pointer-events: none;
}

.toast.success { border-color: var(--success); color: var(--success); }
.toast.error { border-color: var(--danger); color: var(--danger); }

/* --- Navigation --- */
nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--nav-height);
  background: var(--bg-card);
  display: flex;
  border-top: 1px solid rgba(255,255,255,0.05);
  z-index: 100;
}

.nav-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.7rem;
  font-weight: 500;
  cursor: pointer;
  padding: 8px 4px;
  -webkit-user-select: none;
  user-select: none;
}

.nav-btn.active {
  color: var(--primary);
}

.nav-icon {
  font-size: 1.3rem;
}

/* --- History Chart --- */
.range-buttons {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}

.range-btn {
  flex: 1;
  padding: 10px 4px;
  border: 1px solid rgba(255,255,255,0.15);
  border-radius: 10px;
  background: var(--bg-card);
  color: var(--text-muted);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}

.range-btn.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.chart-container {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 12px 8px;
  position: relative;
  height: 380px;
}

/* --- Devices --- */
.device-group-label {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1px;
  padding: 16px 4px 6px;
}

.device-group-label:first-child {
  padding-top: 0;
}

.device-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 16px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  cursor: pointer;
  transition: background 0.2s;
  border-left: 4px solid transparent;
}

.device-card:active {
  background: var(--bg-card-hover);
}

.device-card.device-on {
  border-left-color: var(--success);
}

.device-card.device-off {
  border-left-color: #4b5563;
}

.device-card.device-unavailable {
  opacity: 0.4;
  pointer-events: none;
  border-left-color: var(--danger);
}

.device-name {
  font-size: 1rem;
  font-weight: 600;
}

.device-changed {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.device-state {
  font-size: 0.9rem;
  font-weight: 700;
  padding: 6px 16px;
  border-radius: 20px;
  min-width: 60px;
  text-align: center;
}

.device-on .device-state {
  background: var(--success);
  color: white;
}

.device-off .device-state {
  background: #374151;
  color: var(--text-muted);
}

.device-unavailable .device-state {
  background: transparent;
  color: var(--text-muted);
  font-size: 0.75rem;
}

/* --- Cameras --- */
#cameras-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

#cameras-container > .btn {
  grid-column: 1 / -1;
}

.camera-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  overflow: hidden;
}

.camera-label {
  padding: 8px 10px 4px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
}

.camera-img-wrap {
  width: 100%;
  background: #000;
  min-height: 90px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.camera-img {
  width: 100%;
  height: auto;
  display: block;
  transition: opacity 0.3s;
}

.btn-refresh-cam {
  display: block;
  width: 100%;
  padding: 10px;
  background: var(--bg-card-hover);
  border: none;
  color: var(--text-muted);
  font-size: 0.85rem;
  cursor: pointer;
}

.btn-refresh-cam:active {
  background: var(--primary);
  color: white;
}

.camera-card {
  cursor: pointer;
}

/* --- Live Camera Modal --- */
.camera-modal-content {
  width: 100%;
  max-width: 800px;
  max-height: 90vh;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
}

.camera-modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background: var(--bg-card);
}

.camera-modal-header span {
  font-size: 1rem;
  font-weight: 600;
}

.camera-modal-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.4rem;
  cursor: pointer;
  padding: 0 8px;
}

.camera-modal-btn:active {
  color: var(--primary);
}

.camera-modal-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 2rem;
  cursor: pointer;
  line-height: 1;
  padding: 0 4px;
}

.camera-modal-close:active {
  color: var(--danger);
}

.camera-modal-video {
  position: relative;
  width: 100%;
  background: #000;
  min-height: 240px;
}

.camera-modal-video video {
  width: 100%;
  height: auto;
  display: block;
}

.live-loading {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  font-size: 0.95rem;
}

/* --- Safe area (notch phones) --- */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  nav {
    padding-bottom: env(safe-area-inset-bottom);
    height: calc(var(--nav-height) + env(safe-area-inset-bottom));
  }
  main {
    margin-bottom: calc(var(--nav-height) + env(safe-area-inset-bottom));
  }
}
