:root {
  --bg: #14100d;
  --bg2: #1d1814;
  --card: #231d18;
  --line: #3a3029;
  --text: #f6efe8;
  --muted: #a8998b;
  --accent: #ff8a3d;
  --accent2: #ffcf5c;
  --cool: #6cb4ff;
  --ok: #57d18b;
  --bad: #ff5a5a;
  --radius: 20px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif; }
body { min-height: 100vh; overscroll-behavior-y: none; }
button { font: inherit; color: inherit; cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--bad); min-height: 1.2em; font-size: 14px; }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: radial-gradient(circle at 50% 30%, #3a2212 0, var(--bg) 60%); }
.login-card { width: 100%; max-width: 340px; text-align: center; }
.login-card h1 { margin: 12px 0 4px; font-size: 30px; letter-spacing: -0.5px; }
.login-card form { display: grid; gap: 12px; margin-top: 20px; }
.logo-flame { width: 64px; height: 64px; margin: 0 auto; background: url(/icon.svg) center/contain no-repeat; }
input, .input { width: 100%; padding: 14px 16px; border-radius: 14px; border: 1px solid var(--line); background: var(--bg2); color: var(--text); font: inherit; outline: none; }
input:focus { border-color: var(--accent); }

/* Layout */
#app { max-width: 520px; margin: 0 auto; padding: calc(12px + var(--safe-t)) 16px calc(96px + var(--safe-b)); }
.top { display: flex; align-items: center; justify-content: space-between; padding: 6px 2px 12px; }
.stove-name { font-weight: 700; font-size: 18px; }
.conn { display: flex; gap: 6px; align-items: center; font-size: 13px; color: var(--muted); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.dot.ok { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.dot.bad { background: var(--bad); }
.icon-btn { background: var(--card); border: 1px solid var(--line); border-radius: 14px; width: 44px; height: 44px; display: grid; place-items: center; }
.icon-btn.spin svg { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.banner { background: #3a1c1c; border: 1px solid #6b2a2a; color: #ffd2d2; padding: 10px 14px; border-radius: 14px; margin-bottom: 12px; font-size: 14px; }
.banner.info { background: #1f2a3a; border-color: #2f4a6b; color: #d2e6ff; }

/* Hero */
.hero { background: radial-gradient(120% 90% at 50% 0%, #2e2018 0%, var(--card) 70%); border: 1px solid var(--line); border-radius: 28px; padding: 18px 18px 20px; margin-bottom: 14px; }
.dial { position: relative; width: min(78vw, 300px); aspect-ratio: 1; margin: 0 auto; }
.dial-svg { width: 100%; height: 100%; transform: rotate(135deg); }
.ring-bg, .ring { fill: none; stroke-width: 14; stroke-linecap: round; }
.ring-bg { stroke: #3a3029; stroke-dasharray: 443 591; }
.ring { stroke: url(#ringGrad); stroke-dasharray: 0 591; transition: stroke-dasharray .8s ease; }
.ring-knob { fill: #fff; transition: transform .8s ease; transform-origin: 110px 110px; filter: drop-shadow(0 0 4px rgba(0,0,0,.6)); }
.hero.off .ring { stroke: #6b5b4e; }
.dial-center { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.room { font-size: 64px; font-weight: 700; letter-spacing: -2px; line-height: 1; }
.room small { font-size: 22px; font-weight: 500; color: var(--muted); margin-left: 2px; letter-spacing: 0; }
.room-label { color: var(--muted); font-size: 13px; margin-top: 2px; }
.status-pill { margin-top: 10px; padding: 5px 12px; border-radius: 999px; font-size: 13px; font-weight: 600; background: #3a3029; }
.status-pill.on { background: rgba(255,138,61,.18); color: var(--accent2); }
.status-pill.starting, .status-pill.cleaning { background: rgba(255,207,92,.15); color: var(--accent2); }
.status-pill.alarm { background: rgba(255,90,90,.2); color: #ffb3b3; }
.status-pill.standby { background: rgba(108,180,255,.15); color: #bcdcff; }

/* Llama */
.flame { position: relative; width: 26px; height: 30px; margin-bottom: 6px; opacity: 0; transition: opacity .5s; }
.hero.heating .flame { opacity: 1; }
.flame span { position: absolute; bottom: 0; left: 50%; border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%; transform-origin: 50% 100%; animation: flicker 1.4s ease-in-out infinite alternate; }
.flame span:nth-child(1) { width: 22px; height: 28px; margin-left: -11px; background: radial-gradient(circle at 50% 80%, #ffcf5c, #ff6a1a 70%); filter: blur(.3px); }
.flame span:nth-child(2) { width: 14px; height: 18px; margin-left: -7px; background: radial-gradient(circle at 50% 80%, #fff3c4, #ffcf5c 70%); animation-duration: 1s; }
.flame span:nth-child(3) { width: 6px; height: 8px; margin-left: -3px; background: #fff; animation-duration: .8s; }
@keyframes flicker { 0% { transform: scale(1, 1) rotate(-2deg); } 50% { transform: scale(.92, 1.08) rotate(2deg); } 100% { transform: scale(1.04, .95) rotate(-1deg); } }

.target { display: flex; align-items: center; justify-content: center; gap: 26px; margin: -34px 0 18px; position: relative; }
.round { width: 58px; height: 58px; border-radius: 50%; border: 1px solid var(--line); background: var(--bg2); font-size: 28px; line-height: 1; }
.round:active { transform: scale(.94); background: #2c241e; }
.target-val { text-align: center; min-width: 86px; }
.target-num { font-size: 36px; font-weight: 700; }
.target-num.pending { color: var(--accent2); }

.power-btn { width: 100%; display: flex; align-items: center; justify-content: center; gap: 10px; padding: 16px; border-radius: 18px; border: 0; font-weight: 700; font-size: 17px; background: linear-gradient(135deg, var(--accent), #ff6a1a); color: #1a0f07; box-shadow: 0 8px 24px rgba(255,106,26,.25); }
.power-btn.off-action { background: #3a3029; color: var(--text); box-shadow: none; }
.power-btn:disabled { opacity: .5; }

/* Cards */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; }
.card-title { font-weight: 700; margin-bottom: 12px; display: flex; gap: 8px; align-items: baseline; }
.card-title.row { justify-content: space-between; align-items: center; }
.alarm { color: #ffb3b3; margin-bottom: 6px; }
#alarm-card { border-color: #6b2a2a; background: #2a1717; }

.seg { display: flex; background: var(--bg2); border-radius: 14px; padding: 4px; gap: 4px; }
.seg button { flex: 1; border: 0; background: transparent; padding: 10px 0; border-radius: 10px; font-weight: 600; color: var(--muted); min-width: 0; }
.seg button.on { background: linear-gradient(135deg, var(--accent), #ff6a1a); color: #1a0f07; }
.seg button:disabled { opacity: .4; }
.small-seg button { padding: 6px 10px; font-size: 13px; white-space: nowrap; }
.fan { margin-bottom: 14px; }
.fan:last-child { margin-bottom: 0; }
.fan-head { display: flex; justify-content: space-between; font-size: 14px; margin-bottom: 6px; }

.toggle-row { display: flex; justify-content: space-between; align-items: center; padding: 6px 0; }
.switch { width: 52px; height: 32px; border-radius: 999px; background: #3a3029; border: 0; position: relative; transition: background .2s; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 26px; height: 26px; border-radius: 50%; background: #fff; transition: transform .2s; }
.switch.on { background: var(--accent); }
.switch.on::after { transform: translateX(20px); }

.chips { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; }
.chip { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 12px 14px; }
.chip b { display: block; font-size: 20px; }
.chip span { color: var(--muted); font-size: 12px; }

/* Botones */
.btn { border: 1px solid var(--line); background: var(--bg2); padding: 12px 16px; border-radius: 14px; font-weight: 600; }
.btn.primary { background: linear-gradient(135deg, var(--accent), #ff6a1a); color: #1a0f07; border: 0; }
.btn.ghost { background: transparent; }
.btn.danger { background: #3a1c1c; border-color: #6b2a2a; color: #ffd2d2; }
.btn.wide { width: 100%; margin-top: 10px; }
.btn.tiny { padding: 6px 12px; font-size: 13px; }
.btn:active { transform: scale(.98); }
.quick { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.quick .btn { padding: 12px 4px; font-size: 13px; white-space: nowrap; }

/* Gráfica */
.chart-wrap { position: relative; height: 220px; }
.chart-wrap.small { height: 120px; }
.chart { width: 100%; height: 100%; display: block; }
.chart text { fill: var(--muted); font-size: 10px; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); margin-top: 8px; flex-wrap: wrap; }
.lg { display: inline-block; width: 12px; height: 3px; border-radius: 2px; margin-right: 6px; vertical-align: middle; }
.lg-room { background: var(--accent); }
.lg-target { background: var(--cool); }
.lg-heat { background: rgba(255,138,61,.25); height: 10px; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 12px; }
.stats div { background: var(--bg2); border-radius: 12px; padding: 10px; text-align: center; }
.stats b { display: block; font-size: 18px; }
.stats span { font-size: 11px; color: var(--muted); }

/* Programas */
.sched { display: flex; align-items: center; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.sched:last-of-type { border-bottom: 0; }
.sched-time { font-size: 24px; font-weight: 700; min-width: 74px; }
.sched-body { flex: 1; min-width: 0; }
.sched-days { font-size: 12px; color: var(--muted); }
.sched-act { font-weight: 600; }
.days { display: flex; gap: 6px; flex-wrap: wrap; }
.days button { width: 40px; height: 40px; border-radius: 50%; border: 1px solid var(--line); background: var(--bg2); font-weight: 600; }
.days button.on { background: var(--accent); color: #1a0f07; border-color: transparent; }
.once { display: flex; justify-content: space-between; align-items: center; margin-top: 12px; padding: 10px 12px; background: var(--bg2); border-radius: 12px; font-size: 14px; }

/* Más */
.kv { display: grid; grid-template-columns: auto 1fr; gap: 8px 16px; margin: 0; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; }
.reg { display: flex; justify-content: space-between; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.reg code { color: var(--muted); font-size: 11px; display: block; word-break: break-all; }
.reg .v { text-align: right; white-space: nowrap; }
.reg button { margin-left: 8px; }
.eeprom-tag { font-size: 10px; color: var(--accent2); border: 1px solid #5a4a2a; padding: 1px 5px; border-radius: 6px; margin-left: 4px; }
#reg-filter { margin: 8px 0; }

/* Navegación */
.tabs { position: fixed; left: 0; right: 0; bottom: 0; display: flex; justify-content: center; gap: 4px; padding: 8px 8px calc(8px + var(--safe-b)); background: rgba(20,16,13,.92); backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px); border-top: 1px solid var(--line); z-index: 10; }
.tabs button { flex: 1; max-width: 130px; border: 0; background: transparent; color: var(--muted); display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 11px; padding: 6px 0; border-radius: 12px; }
.tabs svg { width: 24px; height: 24px; fill: currentColor; }
.tabs button.on { color: var(--accent2); }

/* Hoja inferior y toast */
.sheet-bg { position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 20; }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; max-width: 520px; margin: 0 auto; background: var(--bg2); border-radius: 24px 24px 0 0; padding: 20px 18px calc(20px + var(--safe-b)); z-index: 21; border: 1px solid var(--line); animation: up .25s ease; max-height: 90vh; overflow: auto; }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
.sheet h3 { margin: 0 0 6px; }
.sheet .field { margin: 14px 0; }
.sheet label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.sheet .actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 18px; }
.toast { position: fixed; left: 50%; bottom: calc(90px + var(--safe-b)); transform: translateX(-50%); background: #2c241e; border: 1px solid var(--line); padding: 10px 16px; border-radius: 14px; font-size: 14px; z-index: 30; max-width: 90vw; box-shadow: 0 10px 30px rgba(0,0,0,.4); }
.toast.bad { background: #3a1c1c; border-color: #6b2a2a; }
