:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --surface-2: #e9ebef;
  --text: #111318;
  --muted: #646b78;
  --border: #dde0e6;
  --accent: #e8a317;          /* włączone światło / gniazdko */
  --accent-text: #3d2a00;
  --gate: #2f7de1;
  --danger: #d93636;
  --ok: #1f9d55;
  --radius: 20px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --surface: #1a1d23;
    --surface-2: #242830;
    --text: #f2f3f5;
    --muted: #9aa1ad;
    --border: #2c313a;
    --accent: #f5b83d;
    --accent-text: #2b1d00;
    --gate: #4a92f0;
    --danger: #f05252;
    --ok: #34c472;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  padding: env(safe-area-inset-top) 0 calc(env(safe-area-inset-bottom) + 24px);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}
main { padding: 0 16px; max-width: 640px; margin: 0 auto; }
button { font: inherit; color: inherit; cursor: pointer; }
code { font-size: .9em; }

/* ---------- nagłówek i menu */
.top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px 8px; max-width: 640px; margin: 0 auto;
}
.top h1 { font-size: 28px; margin: 0; letter-spacing: -.02em; }
.top-actions { display: flex; align-items: center; gap: 8px; }
.sync { font-size: 13px; color: var(--muted); }
.sync.err { color: var(--danger); }
.icon-btn {
  width: 44px; height: 44px; border-radius: 50%; border: 0;
  background: var(--surface); font-size: 22px; line-height: 1;
}
.menu {
  position: absolute; right: 16px; top: calc(env(safe-area-inset-top) + 64px); z-index: 10;
  width: min(300px, calc(100vw - 32px));
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  padding: 8px; box-shadow: 0 12px 32px rgba(0,0,0,.25);
}
.menu-user { margin: 8px 12px; color: var(--muted); font-size: 14px; }
.menu-item {
  display: block; width: 100%; text-align: left; padding: 12px; border: 0; border-radius: 10px;
  background: none; text-decoration: none; color: var(--text);
}
.menu-item:active { background: var(--surface-2); }
.menu-item.danger { color: var(--danger); }
.menu-hint { margin: 4px 12px 8px; font-size: 13px; color: var(--muted); }

/* ---------- kafelki urządzeń */
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.tile {
  position: relative; display: flex; flex-direction: column; justify-content: space-between;
  min-height: 132px; padding: 16px; border-radius: var(--radius);
  background: var(--surface); border: 1px solid var(--border); text-align: left;
  transition: transform .08s ease, background-color .2s ease;
  user-select: none; -webkit-user-select: none;
}
button.tile:active { transform: scale(.97); }
.tile-icon { font-size: 30px; line-height: 1; }
.tile-name { font-weight: 600; font-size: 16px; margin-top: 12px; overflow-wrap: anywhere; }
.tile-state { font-size: 14px; color: var(--muted); margin-top: 2px; }
.tile.on { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.tile.on .tile-state { color: var(--accent-text); opacity: .8; }
.tile.gate { grid-column: span 2; min-height: 96px; flex-direction: row; align-items: center;
             justify-content: flex-start; gap: 16px; }
.tile.gate .tile-name { margin-top: 0; font-size: 18px; }
.tile.gate .tile-icon {
  width: 56px; height: 56px; border-radius: 50%; display: grid; place-items: center;
  background: var(--gate); color: #fff; font-size: 26px; flex: none;
}
.tile.gate.busy .tile-icon { animation: pulse 1s ease-in-out infinite; }
.tile.leak.alarm { background: var(--danger); border-color: var(--danger); color: #fff; }
.tile.leak.alarm .tile-state { color: #fff; }
.tile.offline { opacity: .55; }
.tile.busy { pointer-events: none; }
.tile-badge {
  position: absolute; top: 14px; right: 14px; font-size: 12px; color: var(--muted);
  display: flex; gap: 6px; align-items: center;
}
.tile.on .tile-badge, .tile.alarm .tile-badge { color: inherit; opacity: .8; }
.tile-badge.low { color: var(--danger); font-weight: 600; opacity: 1; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); display: inline-block; }
@keyframes pulse { 50% { opacity: .7; } }

/* ---------- alarm, komunikaty */
.alert {
  margin: 4px 0 16px; padding: 16px; border-radius: var(--radius);
  background: var(--danger); color: #fff; font-weight: 600; font-size: 17px;
  animation: pulse 1.6s ease-in-out infinite;
}
.notice {
  padding: 12px 14px; border-radius: 12px; background: var(--surface); color: var(--muted);
  font-size: 14px; border: 1px solid var(--border);
}
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(env(safe-area-inset-bottom) + 20px); z-index: 20;
  max-width: calc(100vw - 32px); padding: 12px 18px; border-radius: 14px;
  background: var(--text); color: var(--bg); font-size: 15px; box-shadow: 0 8px 24px rgba(0,0,0,.3);
}

/* ---------- zdarzenia */
.section-title { font-size: 15px; color: var(--muted); font-weight: 600; margin: 28px 4px 8px; }
.events { list-style: none; margin: 0; padding: 0; background: var(--surface); border-radius: 16px;
          border: 1px solid var(--border); overflow: hidden; }
.events li { display: flex; justify-content: space-between; gap: 12px; padding: 12px 14px;
             border-top: 1px solid var(--border); font-size: 14px; }
.events li:first-child { border-top: 0; }
.events time { color: var(--muted); white-space: nowrap; font-size: 13px; }
.events .leak { color: var(--danger); font-weight: 600; }
.events .empty { color: var(--muted); }

/* ---------- logowanie */
.login { max-width: 360px; margin: 12vh auto 0; padding: 0 16px; text-align: center; }
.login-logo {
  width: 72px; height: 72px; margin: 0 auto; border-radius: 20px; display: grid; place-items: center;
  background: var(--accent); color: var(--accent-text); font-size: 40px;
}
.login h1 { margin: 12px 0 24px; }
.login form { display: grid; gap: 14px; text-align: left; }
.login label { display: grid; gap: 6px; font-size: 14px; color: var(--muted); }
.login input {
  font: inherit; font-size: 17px; padding: 14px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.btn-primary {
  margin-top: 6px; padding: 15px; border: 0; border-radius: 12px; font-weight: 600; font-size: 17px;
  background: var(--accent); color: var(--accent-text);
}
.form-error { color: var(--danger); margin: 0; }
