:root {
  --bg: #0b1220;
  --surface: #131c2e;
  --surface-2: #1b2740;
  --line: #24314c;
  --text: #eaf0fb;
  --muted: #8b9ab5;
  --accent: #38bdf8;
  --accent-soft: rgba(56, 189, 248, 0.14);
  --heat: #fb923c;
  --heat-soft: rgba(251, 146, 60, 0.14);
  --danger: #f87171;
  --radius: 18px;
  --shadow: 0 8px 28px rgba(0, 0, 0, 0.32);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6fb;
    --surface: #ffffff;
    --surface-2: #eef2f9;
    --line: #dfe5f0;
    --text: #121a2b;
    --muted: #64748b;
    --accent: #0284c7;
    --accent-soft: rgba(2, 132, 199, 0.12);
    --heat: #ea7317;
    --heat-soft: rgba(234, 115, 23, 0.12);
    --shadow: 0 6px 20px rgba(18, 26, 43, 0.08);
  }
}

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

html { -webkit-text-size-adjust: 100%; }

body {
  overflow-x: hidden;
  background: var(--bg);
  color: var(--text);
  font: 400 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}

button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; touch-action: manipulation; }

.app {
  max-width: 560px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

.topbar { display: flex; align-items: flex-start; gap: 12px; padding: 8px 4px 18px; }
.clock { margin-left: auto; text-align: right; flex: none; padding-top: 2px; }
.clock-time { font-size: 23px; font-weight: 600; letter-spacing: -0.02em; line-height: 1.05; font-variant-numeric: tabular-nums; }
.clock-date { color: var(--muted); font-size: 11px; margin-top: 3px; white-space: nowrap; }
.title h1 { font-size: 30px; font-weight: 650; letter-spacing: -0.02em; }
.status { color: var(--muted); font-size: 13px; margin-top: 2px; display: flex; align-items: center; gap: 6px; }
.status::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--muted); flex: none; }
.status.ok::before { background: #34d399; }
.status.bad::before { background: var(--danger); }

.icon-btn { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; background: var(--surface); border: 1px solid var(--line); flex: none; }
.icon-btn svg { width: 19px; height: 19px; fill: none; stroke: var(--muted); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn:active { transform: scale(0.94); }
.icon-btn.spin svg { animation: spin 0.7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.banner { background: rgba(248, 113, 113, 0.12); border: 1px solid rgba(248, 113, 113, 0.3); color: var(--danger); border-radius: 12px; padding: 10px 14px; font-size: 14px; margin-bottom: 16px; }

.floors { display: flex; flex-direction: column; gap: 26px; flex: 1; }

.floor-head { display: flex; flex-direction: column; align-items: stretch; gap: 9px; padding: 0 4px 11px; }
.floor-title { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 30px; }
.floor-head h2 { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.07em; color: var(--muted); }

.modes { display: flex; gap: 3px; background: var(--surface); border: 1px solid var(--line); border-radius: 999px; padding: 3px; }
.modes button { flex: 1 1 0; min-width: 0; font-size: 12.5px; font-weight: 550; padding: 7px 4px; border-radius: 999px; color: var(--muted); white-space: nowrap; }
.modes button[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); }
.modes button[aria-pressed="true"][data-mode="3"] { background: var(--heat-soft); color: var(--heat); }

.zones { display: flex; flex-direction: column; gap: 10px; }

.zone {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: var(--shadow);
  transition: opacity 0.2s, border-color 0.2s;
}
.zone.off { opacity: 0.58; }
.zone.demand { border-color: var(--accent); }
.zone.demand.heat { border-color: var(--heat); }
.zone.error { border-color: var(--danger); }

/* Sin lectura fresca del equipo, los valores en pantalla son viejos: que se note. */
/* Sin lectura fresca de ese equipo, sus valores son viejos: que se note, y que no se
   le manden ordenes. El reloj sigue vivo: las programaciones viven en la Pi. */
.floor.offline .zones, .floor.offline .modes, .floor.offline .floor-title .switch { opacity: 0.4; }
.floor.offline .zones .switch, .floor.offline .zones .step,
.floor.offline .modes button, .floor.offline .floor-title .switch { pointer-events: none; }
.floor.offline .clock-btn { pointer-events: auto; opacity: 1; }
.floor-title .pill { margin-left: auto; margin-right: 4px; }

.zone-top { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.zone-info { min-width: 0; }
.zone-head { display: flex; align-items: center; gap: 8px; min-width: 0; }
.pill { flex: none; font-size: 10.5px; font-weight: 650; padding: 3px 8px; border-radius: 999px; letter-spacing: 0.02em; }
.pill.cool { background: var(--accent-soft); color: var(--accent); }
.pill.heat { background: var(--heat-soft); color: var(--heat); }
.pill.idle { background: var(--surface-2); color: var(--muted); }
.pill.error { background: rgba(248, 113, 113, 0.15); color: var(--danger); }
.zone-name, .zone-room { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.zone-name { font-size: 17px; font-weight: 600; letter-spacing: -0.01em; }
.zone-room { color: var(--muted); font-size: 13px; margin-top: 3px; font-variant-numeric: tabular-nums; }
.zone-room b { color: var(--text); font-weight: 600; font-size: 15px; }

.switch { width: 50px; height: 30px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--line); position: relative; flex: none; transition: background 0.18s; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: var(--muted); transition: transform 0.18s, background 0.18s; }
.switch[aria-checked="true"] { background: var(--accent-soft); border-color: var(--accent); }
.switch[aria-checked="true"]::after { transform: translateX(20px); background: var(--accent); }
.switch.sm { width: 44px; height: 26px; }
.switch.sm::after { width: 18px; height: 18px; }
.switch.sm[aria-checked="true"]::after { transform: translateX(18px); }
.switch.heat[aria-checked="true"] { background: var(--heat-soft); border-color: var(--heat); }
.switch.heat[aria-checked="true"]::after { background: var(--heat); }
.zone.heat .switch[aria-checked="true"] { background: var(--heat-soft); border-color: var(--heat); }
.zone.heat .switch[aria-checked="true"]::after { background: var(--heat); }

.setpoint { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 16px; }
.step { width: 46px; height: 46px; border-radius: 14px; background: var(--surface-2); border: 1px solid var(--line); display: grid; place-items: center; font-size: 22px; font-weight: 400; color: var(--text); flex: none; }
.step:active { transform: scale(0.93); }
.step:disabled { opacity: 0.35; }
.value { font-size: 34px; font-weight: 600; letter-spacing: -0.03em; font-variant-numeric: tabular-nums; flex: 1; text-align: center; }
.value span { font-size: 17px; font-weight: 500; color: var(--muted); margin-left: 2px; }
.zone.pending .value { color: var(--accent); }

.footer { padding-top: 22px; text-align: center; }
.all-off { width: 100%; margin-bottom: 24px; padding: 14px; border-radius: 14px; background: var(--surface); border: 1px solid var(--line); color: var(--muted); font-weight: 550; font-size: 15px; box-shadow: var(--shadow); }
.all-off:active { transform: scale(0.99); }
.all-off:disabled { opacity: 0.4; }
.who { color: var(--muted); font-size: 11px; margin-top: 14px; opacity: 0.7; }

.skeleton { height: 118px; border-radius: var(--radius); background: var(--surface); border: 1px solid var(--line); animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.55; } }

@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

/* ---------- programación ---------- */

.zone-actions { display: flex; align-items: center; gap: 10px; flex: none; }

.clock-btn { width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; background: var(--surface-2); border: 1px solid var(--line); position: relative; }
.clock-btn svg { width: 17px; height: 17px; fill: none; stroke: var(--muted); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.clock-btn:active { transform: scale(0.93); }
.clock-btn.has svg { stroke: var(--accent); }
.clock-btn.has::after { content: ""; position: absolute; top: -3px; right: -3px; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); border: 2px solid var(--surface); }

.sheet { width: min(520px, calc(100vw - 24px)); max-height: 88dvh; margin: auto; padding: 0; border: 1px solid var(--line); border-radius: 20px; background: var(--bg); color: var(--text); overflow: auto; overscroll-behavior: contain; }
.sheet::backdrop { background: rgba(0, 0, 0, 0.55); backdrop-filter: blur(2px); }
.sheet-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; padding: 20px 20px 14px; position: sticky; top: 0; background: var(--bg); z-index: 1; }
.sheet-head h2 { font-size: 20px; font-weight: 650; letter-spacing: -0.02em; }
#sched-sub { color: var(--muted); font-size: 13px; margin-top: 2px; }

.sched-list { list-style: none; padding: 0 20px; display: flex; flex-direction: column; gap: 10px; }
.sched-list:empty { display: none; }
.sched-item { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 13px 15px; }
.sched-item.off { opacity: 0.55; }
.sched-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.sched-when { min-width: 0; }
.sched-when strong { font-size: 19px; font-weight: 600; font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }
.sched-when span { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; letter-spacing: 0.06em; }
.sched-when span.once { color: var(--heat); font-weight: 600; }
.sched-what { color: var(--text); font-size: 13px; font-weight: 550; text-align: right; flex: 1; white-space: nowrap; }
.sched-foot { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 11px; padding-top: 10px; border-top: 1px solid var(--line); }
.sched-run { color: var(--muted); font-size: 11.5px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sched-run.bad { color: var(--danger); }
.link { font-size: 12px; font-weight: 550; color: var(--accent); padding: 4px 2px; }
.link + .link { margin-left: 14px; }
.link.danger { color: var(--danger); }

.sched-form { padding: 18px 20px calc(20px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 15px; border-top: 1px solid var(--line); margin-top: 18px; }
.field { display: flex; flex-direction: column; gap: 8px; }
.field > label { color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; }
#f-time { font: inherit; font-size: 17px; font-variant-numeric: tabular-nums; color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 11px 13px; width: 100%; }
.days { display: flex; gap: 6px; }
.days button { flex: 1 1 0; min-width: 0; padding: 10px 0; border-radius: 11px; background: var(--surface); border: 1px solid var(--line); color: var(--muted); font-size: 13px; font-weight: 600; }
.days button[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
#f-temp .value { font-size: 26px; }
.form-error { color: var(--danger); font-size: 13px; }
.primary { width: 100%; padding: 15px; border-radius: 14px; background: var(--accent); color: #04121c; font-weight: 650; font-size: 15px; }
.primary:active { transform: scale(0.99); }
@media (prefers-color-scheme: light) { .primary { color: #fff; } }

.repeat-hint { color: var(--muted); font-size: 11.5px; line-height: 1.35; }
#push-btn { margin-bottom: 10px; }
#push-btn.on { color: var(--accent); border-color: var(--accent); }

#f-cancel { align-self: center; padding: 6px 2px; }
.sched-item.editing { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }

.gate { position: fixed; inset: 0; z-index: 10; display: grid; place-items: center; background: var(--bg); padding: 24px; }
.gate-card { width: min(420px, 100%); display: flex; flex-direction: column; gap: 16px; text-align: center; }
.gate-card h1 { font-size: 30px; font-weight: 650; letter-spacing: -0.02em; }
.gate-card p { color: var(--muted); font-size: 14px; }
.gate-card label { color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; display: block; margin-bottom: 8px; }
#gate-code { width: 100%; font: inherit; font-size: 22px; letter-spacing: 0.22em; text-align: center; text-transform: uppercase; color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 14px; margin-bottom: 12px; }
#gate-enroll { display: flex; flex-direction: column; }
.invite-code { font-size: 26px; font-weight: 650; letter-spacing: 0.2em; text-align: center; color: var(--accent); font-variant-numeric: tabular-nums; margin-top: 6px; }
.device-item { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 13px 15px; display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.device-item b { font-size: 15px; font-weight: 600; }
.device-item span { display: block; color: var(--muted); font-size: 11.5px; margin-top: 3px; }
.device-item.me { border-color: var(--accent); }
