:root {
  --bg: #101418;
  --card: #1a2027;
  --border: #232b33;
  --text: #e8edf2;
  --muted: #8b98a5;
  --accent: #3b82f6;
  --danger: #ef4444;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  min-height: 100vh;
  padding-bottom: env(safe-area-inset-bottom);
}
body.gps-active { padding-bottom: calc(16vh + 24px); }
.site-footer { text-align: center; color: var(--muted); font-size: 19px; line-height: 1.5; padding: 22px 16px 14px; }
.site-footer b { color: var(--text); }
.site-version { margin-top: 6px; font-size: 14px; color: var(--muted); }
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  padding: 16px;
  position: sticky;
  top: 0;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  z-index: 10;
}
header h1 { margin: 0; font-size: 22px; }
button {
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 15px;
  cursor: pointer;
}
button:active { opacity: 0.8; }
button:disabled { opacity: 0.5; }
.sun-info { font-size: 17px; color: var(--muted); text-align: center; }
.sun-line { display: inline; }
.sun-line + .sun-line::before { content: " · "; }
@media (max-width: 720px) {
  .sun-line { display: block; }
  .sun-line + .sun-line::before { content: ""; }
}
.mode-bar { display: flex; gap: 6px; width: 100%; justify-content: center; padding: 6px 0 0; flex-wrap: wrap; }
@media (max-width: 720px) {
  .mode-bar { display: none; }
}
.mode-btn { background: var(--card2); color: var(--muted); border: 1px solid var(--border); border-radius: 8px; padding: 6px 14px; font-size: 14px; cursor: pointer; }
.mode-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }
section { padding: 16px; }
section h2 {
  margin: 8px 0 12px;
  font-size: 14px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
#gates-section { display: none; }
@media (max-width: 720px) { #gates-section { display: block; } }
.grid { display: grid; gap: 12px; grid-template-columns: 1fr; }
.sensor-settings { display: none; margin-top: 10px; }
.sensor-settings.open { display: block; }
.sensor-card .btn-ghost { width: 100%; margin-top: 10px; }
@media (min-width: 720px) { .grid { grid-template-columns: repeat(2, 1fr); } }
@media (min-width: 1100px) { .grid { grid-template-columns: repeat(3, 1fr); } }
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px;
}
.card.offline { opacity: 0.55; }
.card h3 { margin: 0 0 12px; font-size: 19px; font-weight: 600; }
.meta { color: var(--muted); font-size: 14px; margin: 0 0 10px; }
.row { display: flex; align-items: center; justify-content: space-between; margin: 10px 0; }
.row label { color: var(--text); font-size: 16px; }
.slider-row { margin: 12px 0; }
.switch { position: relative; width: 52px; height: 30px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .slider {
  position: absolute;
  inset: 0;
  background: #38424d;
  border-radius: 30px;
  cursor: pointer;
  transition: 0.2s;
}
.switch .slider::before {
  content: "";
  position: absolute;
  height: 24px;
  width: 24px;
  left: 3px;
  top: 3px;
  background: #fff;
  border-radius: 50%;
  transition: 0.2s;
}
.switch input:checked + .slider { background: var(--accent); }
.switch input:disabled + .slider { opacity: 0.45; cursor: default; }
input[type="range"]:disabled { opacity: 0.45; }
.switch input:checked + .slider::before { transform: translateX(22px); }
input[type="range"] { width: 100%; accent-color: var(--accent); }
.range-value { font-size: 14px; color: var(--muted); }
.banner {
  margin: 12px 16px 0;
  padding: 12px 14px;
  border-radius: 12px;
  background: #3a1d1d;
  color: #fca5a5;
  border: 1px solid #7f1d1d;
  font-size: 15px;
}
.hidden { display: none; }
.empty { color: var(--muted); font-size: 15px; }
.scenario-card button { width: 100%; }
#security h3 { margin: 14px 0 8px; font-size: 15px; color: var(--muted); }
.sec-box { background: var(--card); border: 1px solid var(--border); border-radius: 12px; padding: 12px; font-size: 14px; }
.security-actions { margin: 2px 0 14px; }
.security-actions button { padding: 8px 14px; font-size: 13px; }
.security-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 6px 0; border-bottom: 1px solid var(--border); }
.security-row:last-child { border-bottom: 0; }
.security-row button { padding: 6px 12px; font-size: 13px; }
.security-clear-btn {
  margin-left: 10px;
  padding: 4px 12px;
  font-size: 12px;
  vertical-align: middle;
}
.sec-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.sec-table th, .sec-table td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); }
.sec-table th { color: var(--muted); font-weight: 600; }
.sensor-big { font-size: 28px; font-weight: 700; margin: 4px 0 10px; }
.mode-value { font-weight: 600; }
.alarm-control { display: flex; gap: 8px; margin-top: 12px; }
.alarm-control button { flex: 1; padding: 9px 10px; font-size: 15px; }
.alarm-control .check-btn { background: var(--card2); color: var(--text); border: 1px solid var(--border); }
.alarm-control .btn-arm { background: #b91c1c; color: #fff; border: 1px solid #ef4444; }
.alarm-control .btn-arm:hover:not(:disabled) { background: #dc2626; }
.alarm-control .btn-disarm { background: #15803d; color: #fff; border: 1px solid #22c55e; }
.alarm-control .btn-disarm:hover:not(:disabled) { background: #16a34a; }
.alarm-control button:disabled { opacity: 0.45; cursor: default; }
.alarm-control .check-btn:hover { border-color: var(--accent); }
.mode-armed { color: #4ade80; }
.card.alarm-armed { background: #3d1f2b; border-color: #e8799a; }
.card.alarm-sos { background: #3a1d1d; border-color: #dc2626; }
.mode-sos { color: #f87171; }
.sensor-big.clickable { cursor: pointer; }
.sensor-big.clickable:active { opacity: 0.7; }
.modal { position: fixed; inset: 0; background: rgba(0,0,0,0.6); display: flex; align-items: center; justify-content: center; z-index: 50; }
.modal.hidden { display: none; }
.modal-card { background: var(--card); border: 1px solid var(--border); border-radius: 14px; padding: 16px; width: min(94vw, 640px); max-height: 88vh; overflow: hidden; display: flex; flex-direction: column; }
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; flex-shrink: 0; }
#switch-body { max-height: 62vh; overflow-y: auto; margin-top: 2px; }
.modal-head h3 { margin: 0; font-size: 17px; }
.modal-head button { background: none; border: 0; color: var(--muted); font-size: 20px; cursor: pointer; padding: 4px 8px; }
.modal-actions { display: flex; align-items: center; gap: 8px; }
.btn-ghost { background: var(--card2); color: var(--text); border: 1px solid var(--border); border-radius: 8px; padding: 6px 12px; font-size: 14px; cursor: pointer; }
.chart-controls { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.chart-range { flex: 1; font-size: 14px; color: var(--muted); white-space: nowrap; }
.chart-periods { display: flex; gap: 6px; }
.chart-periods .btn-ghost.active { background: var(--accent); border-color: var(--accent); color: #fff; }
#chart-svg { position: relative; }
.chart-stats { margin-top: 10px; font-size: 14px; color: var(--muted); text-align: center; }
.chart-tip { position: absolute; background: #101418; border: 1px solid var(--border); border-radius: 8px; padding: 4px 10px; font-size: 13px; color: var(--text); pointer-events: none; z-index: 10; }
.battery-low { color: #f87171; }
.battery-mid { color: #fbbf24; }
.battery-high { color: #4ade80; }
.card.state-on { background: #173a24; border-color: #2f8f52; }
.card.clickable { cursor: pointer; }
.switch-summary { margin: 4px 0 12px; font-size: 15px; color: var(--text); }
.power-row { display: flex; gap: 14px; margin-top: 10px; flex-wrap: wrap; }
.gps-bar { position: fixed; left: 8px; right: 8px; bottom: 8px; z-index: 40; background: #101418; border: 1px solid var(--border); border-radius: 10px; padding: 10px 14px; font-size: 20px; line-height: 1.5; color: var(--text); text-align: center; box-shadow: 0 2px 10px rgba(0,0,0,0.4); max-height: 26vh; overflow: auto; }
.gps-bar .gps-line1 { display: block; font-weight: 600; }
.gps-bar .gps-line2 { display: block; margin-top: 4px; color: var(--muted); }
.confirm-card { max-width: 360px; text-align: center; }
.confirm-text { font-size: 16px; color: var(--text); margin: 10px 0 20px; line-height: 1.4; }
.confirm-buttons { display: flex; gap: 12px; justify-content: center; }
.confirm-buttons .btn-ghost { padding: 10px 24px; font-size: 15px; }
.confirm-ok { background: var(--accent); color: #fff; border: 0; border-radius: 10px; padding: 10px 28px; font-size: 15px; cursor: pointer; }
.confirm-ok:active { opacity: 0.8; }
.power-val { font-size: 14px; color: var(--text); background: #202831; border: 1px solid var(--border); border-radius: 8px; padding: 4px 10px; }
.power-val.clickable { cursor: pointer; }
.power-val.clickable:hover { border-color: var(--accent); }
.offline-list { margin: 8px 0 0; padding-left: 18px; font-size: 14px; color: var(--muted); }
.offline-list li { margin-bottom: 4px; }
.on-text { color: #4ade80; }
.off-text { color: #fbbf24; }
.card.state-off { background: #3a3117; border-color: #b08a1f; }
.card.stale { border-color: #dc2626; box-shadow: 0 0 0 1px #dc2626; }
.stale-badge {
  display: inline-block;
  background: #dc2626;
  color: #fff;
  border-radius: 6px;
  padding: 2px 8px;
  font-size: 13px;
  margin-left: 8px;
  vertical-align: middle;
}
.sensor-limits { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 8px; margin-top: 12px; }
.limit-field {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  color: var(--muted);
  font-size: 14px;
  background: #202831;
  border: 2px solid #54657a;
  border-radius: 12px;
  padding: 6px 8px;
  box-shadow: 0 0 0 1px rgba(255,255,255,0.04);
}
.limit-label { color: var(--muted); }
.limit-row { display: flex; align-items: center; justify-content: center; gap: 6px; }
.limit-btn {
  width: 30px;
  height: 30px;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  font-size: 19px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.limit-btn:disabled { opacity: 0.35; cursor: default; }
.limit-value {
  min-width: 32px;
  text-align: center;
  font-size: 19px;
  font-weight: 700;
  color: var(--text);
}

/* --- Login screen --- */
.login-screen {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(1200px 600px at 50% -10%, rgba(59, 130, 246, 0.15), transparent 60%),
    var(--bg);
  padding: 16px;
}
.login-screen.hidden { display: none; }
.login-card {
  width: min(92vw, 360px);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px 24px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.5);
}
.login-card h1 {
  margin: 0 0 6px;
  font-size: 24px;
  text-align: center;
}
.login-sub {
  margin: 0 0 18px;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
}
.login-card label {
  display: block;
  margin: 12px 0 6px;
  font-size: 13px;
  color: var(--muted);
}
.login-card input {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 12px;
  font-size: 15px;
}
.login-card input:focus { outline: none; border-color: var(--accent); }
.login-error {
  margin-top: 14px;
  background: rgba(239, 68, 68, 0.12);
  border: 1px solid rgba(239, 68, 68, 0.45);
  color: #f87171;
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 13px;
  line-height: 1.5;
}
.login-error.hidden { display: none; }
#login-submit {
  width: 100%;
  margin-top: 18px;
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: 10px;
  padding: 12px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
#login-submit:disabled { opacity: 0.6; cursor: wait; }
.login-hint {
  margin: 16px 0 0;
  text-align: center;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}