:root {
  --bg: #0b0e13;
  --panel: #151a22;
  --panel2: #1c232e;
  --line: #2a3443;
  --text: #e8edf4;
  --muted: #8593a8;
  --light: #ffa62b;
  --ac: #38bdf8;
  --tv: #a855f7;
  --ok: #22c55e;
  --bad: #ef4444;
  --rain: #60a5fa;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; background: #000; overflow: hidden; }
body { font-family: "Sarabun", "Noto Sans Thai", "Leelawadee UI", system-ui, sans-serif; color: var(--text); user-select: none; }
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; padding: 0; }
svg.ic { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }

#stage {
  position: absolute; left: 50%; top: 50%;
  width: 800px; height: 480px;
  transform: translate(-50%, -50%) scale(var(--s, 1));
  background: var(--bg); overflow: hidden; border-radius: 6px;
  box-shadow: 0 0 0 1px #222;
}

/* ---------- App shell ---------- */
.app { display: grid; grid-template-columns: 60px 1fr; grid-template-rows: 40px 1fr; height: 100%; }
.rail { grid-row: 1 / 3; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: 4px 0; }
.rail button { height: 58px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: 10px; color: var(--muted); border-left: 3px solid transparent; }
.rail button.on { color: var(--light); border-left-color: var(--light); background: #ffa62b12; }
.rail .sp { flex: 1; }
.top { display: flex; align-items: center; gap: 10px; padding: 0 10px; border-bottom: 1px solid var(--line); font-size: 13px; }
.top h1 { font-size: 15px; margin: 0; font-weight: 600; }
.top .sp { flex: 1; }
.top .wx { display: flex; align-items: center; gap: 4px; color: var(--muted); }
.top .wx b { color: var(--text); font-weight: 600; }
.top .clock { font-variant-numeric: tabular-nums; font-weight: 600; font-size: 15px; }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 3px 9px; border-radius: 99px; font-size: 11px; font-weight: 700; letter-spacing: .3px; background: var(--panel2); color: var(--muted); white-space: nowrap; flex: none; }
.chip svg.ic { width: 14px; height: 14px; }
.chip.rain { background: #60a5fa2b; color: var(--rain); animation: pulse 1.2s infinite; }
.chip.ok { background: #22c55e22; color: var(--ok); }
.chip.bad { background: #ef444426; color: var(--bad); }
.chip.tv { background: #a855f72b; color: var(--tv); }
.chip.ac { background: #38bdf826; color: var(--ac); }
.chip.lt { background: #ffa62b26; color: var(--light); }
.demo-btn { padding: 4px 10px; border: 1px dashed var(--line); border-radius: 6px; font-size: 11px; color: var(--muted); }
@keyframes pulse { 50% { opacity: .55; } }

#content { padding: 8px; min-height: 0; overflow: hidden; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; }
.home { display: grid; grid-template-columns: 246px minmax(0, 1fr); grid-template-rows: minmax(0, 1fr); gap: 8px; height: 100%; }
.plan-card { position: relative; }
.plan-card svg { position: absolute; left: 4px; top: 4px; width: calc(100% - 8px); height: calc(100% - 8px); }
#side { min-width: 0; height: 100%; }

/* ---------- Floor plan ---------- */
.room { fill: #1a212b; stroke: #586579; stroke-width: 7; cursor: pointer; transition: fill .2s; }
.room.sel { stroke: var(--light); fill: #222b38; }
.deco { pointer-events: none; }
.furn { fill: none; stroke: #3b4657; stroke-width: 2.5; }
.rlabel { fill: #93a1b6; font-size: 21px; text-anchor: middle; }
.glow { opacity: 0; transition: opacity .45s; }
.glow.on { opacity: 1; }
.bulb circle { fill: #10151c; stroke: #556277; stroke-width: 2.5; transition: all .3s; }
.bulb text { fill: #8593a8; font-size: 21px; font-weight: 700; text-anchor: middle; }
.bulb.on circle { fill: var(--light); stroke: #ffd699; filter: drop-shadow(0 0 10px #ffa62b); }
.bulb.on text { fill: #2a1800; }
.badge rect { fill: #10151c; stroke: #556277; stroke-width: 2; }
.badge text { fill: #8593a8; font-size: 22px; font-weight: 700; text-anchor: middle; }
.badge.ac.on rect { fill: #0c3a52; stroke: var(--ac); } .badge.ac.on text { fill: #bfe9ff; }
.badge.tv.on rect { fill: #3b1763; stroke: var(--tv); } .badge.tv.on text { fill: #ead6ff; }
.tvbar { fill: #3b4657; transition: fill .3s; } .tvbar.on { fill: var(--tv); filter: drop-shadow(0 0 8px #a855f7); }
.badge.lock rect { fill: #0f3a20; stroke: var(--ok); } .badge.lock text { fill: #b5f5cb; }
.badge.lock.bad rect { fill: #4a1414; stroke: var(--bad); } .badge.lock.bad text { fill: #ffc9c9; }
.doorline { stroke: var(--ok); stroke-width: 12; stroke-linecap: round; transition: stroke .3s; }
.doorline.bad { stroke: var(--bad); }
.bdoor-t { font-size: 20px; font-weight: 700; text-anchor: middle; fill: var(--ok); }
.bdoor-t.bad { fill: var(--bad); }
#rainfx { display: none; }
#rainfx.on { display: block; }
#rainfx line { stroke: var(--rain); stroke-width: 3; stroke-linecap: round; animation: fall .7s linear infinite; }
#rainfx rect { fill: #60a5fa22; }
#rainfx text { fill: var(--rain); font-size: 24px; font-weight: 800; text-anchor: middle; animation: pulse 1s infinite; }
@keyframes fall { from { transform: translateY(-40px); opacity: 0; } 30% { opacity: 1; } to { transform: translateY(60px); opacity: 0; } }
.wash-t { fill: #8593a8; font-size: 19px; font-weight: 700; text-anchor: middle; }
.wash-t.on { fill: var(--ac); }

/* ---------- Dashboard ---------- */
.dash { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 128px 128px 1fr; gap: 8px; height: 100%; }
.dash .card { padding: 10px 12px; display: flex; flex-direction: column; min-width: 0; text-align: left; }
.dash .wide { grid-column: 1 / 3; }
.ttl { font-size: 11px; color: var(--muted); letter-spacing: .5px; text-transform: uppercase; display: flex; align-items: center; gap: 6px; font-weight: 600; }
.ttl .sp { flex: 1; }
.big { font-size: 32px; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; }
.big small { font-size: 14px; color: var(--muted); font-weight: 500; margin-left: 2px; }
.sub { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.row { display: flex; align-items: center; gap: 8px; }
.grow { flex: 1; min-width: 0; }
canvas { display: block; }
.lockbtn { flex: 1; margin-top: 6px; border-radius: 9px; display: flex; align-items: center; justify-content: center; gap: 8px; font-weight: 800; font-size: 17px; letter-spacing: 1px; background: #22c55e22; color: var(--ok); border: 1.5px solid var(--ok); }
.lockbtn.bad { background: #ef444426; color: var(--bad); border-color: var(--bad); }
.lockbtn:active { transform: scale(.98); }
.bar { height: 8px; border-radius: 9px; background: var(--panel2); overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--ac); border-radius: 9px; transition: width .6s; }
.scenes { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; flex: 1; margin-top: 8px; }
.scene { border-radius: 9px; background: var(--panel2); border: 1px solid var(--line); font-size: 13px; font-weight: 600; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; }
.scene:active, .tile:active, .rnd:active, .seg button:active { filter: brightness(1.3); }
.chips { display: flex; gap: 5px; flex-wrap: wrap; }

/* ---------- Room view / tiles ---------- */
.view { display: flex; flex-direction: column; gap: 8px; height: 100%; }
.vhead { display: flex; align-items: center; gap: 8px; height: 40px; flex: none; }
.vhead h2 { margin: 0; font-size: 18px; flex: 1; }
.backbtn { width: 40px; height: 40px; border-radius: 9px; background: var(--panel); border: 1px solid var(--line); display: grid; place-items: center; }
.tiles { display: grid; gap: 8px; grid-template-columns: repeat(var(--n, 4), 1fr); }
.tile { height: 96px; border-radius: 10px; background: var(--panel); border: 1.5px solid var(--line); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; color: var(--muted); transition: all .25s; position: relative; }
.tile svg.ic { width: 28px; height: 28px; }
.tile b { font-size: 15px; color: var(--text); }
.tile span { font-size: 11px; }
.tile em { font-style: normal; font-size: 10px; font-weight: 800; letter-spacing: 1px; }
.tile.on { background: radial-gradient(circle at 50% 30%, #ffa62b55, #ffa62b14 70%); border-color: var(--light); color: var(--light); box-shadow: 0 0 18px #ffa62b40; }
.tile.tv.on { background: radial-gradient(circle at 50% 30%, #a855f755, #a855f714 70%); border-color: var(--tv); color: var(--tv); box-shadow: 0 0 18px #a855f740; }
.tile.ok { border-color: var(--ok); color: var(--ok); background: #22c55e18; }
.tile.bad { border-color: var(--bad); color: var(--bad); background: #ef444420; }
.tile.info { cursor: default; }
.tile.rainy { border-color: var(--rain); color: var(--rain); background: #60a5fa20; }
.lights-page .tile { height: 100%; }
.lights-page .tile svg.ic { width: 40px; height: 40px; }
.lights-page .tile b { font-size: 20px; }
.lights-page .tile span { font-size: 13px; }

/* ---------- AC ---------- */
.accard { padding: 10px 12px; display: flex; flex-direction: column; gap: 8px; flex: 1; min-height: 0; }
.accard .pwr { width: 40px; height: 40px; border-radius: 50%; border: 1.5px solid var(--line); display: grid; place-items: center; color: var(--muted); }
.accard.on .pwr { background: var(--ac); border-color: var(--ac); color: #04222f; }
.accard .body { display: flex; flex-direction: column; gap: 8px; flex: 1; justify-content: space-around; transition: opacity .3s; }
.accard:not(.on) .body { opacity: .3; pointer-events: none; }
.ac-temp { display: flex; align-items: center; justify-content: center; gap: 18px; }
.deg { font-size: 46px; font-weight: 700; line-height: 1; min-width: 110px; text-align: center; font-variant-numeric: tabular-nums; }
.accard.on .deg { color: var(--ac); }
.deg small { font-size: 18px; }
.rnd { width: 52px; height: 52px; border-radius: 50%; background: var(--panel2); border: 1.5px solid var(--line); font-size: 28px; line-height: 1; font-weight: 500; }
.seg { display: flex; background: var(--panel2); border-radius: 9px; padding: 3px; gap: 3px; }
.seg button { flex: 1; height: 36px; border-radius: 7px; font-size: 13px; font-weight: 600; color: var(--muted); }
.seg button.on { background: var(--ac); color: #04222f; }
.seg-l { font-size: 10px; color: var(--muted); letter-spacing: .5px; margin-bottom: -4px; }

/* ---------- Pages ---------- */
.full { height: 100%; }
.cols2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; height: 100%; }
.metrics { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.metric { padding: 8px 12px; }
.metric .big { font-size: 28px; }
.metric.v .big { color: #facc15; } .metric.a .big { color: #fb7185; } .metric.w .big { color: var(--light); } .metric.k .big { color: var(--ok); }
.chart-card { flex: 1; padding: 8px 10px; display: flex; flex-direction: column; min-height: 0; }
.chart-card .cv { flex: 1; min-height: 0; position: relative; }
.chart-card canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.log { flex: 1; overflow: auto; font-size: 12px; margin-top: 6px; }
.log div { display: flex; gap: 8px; padding: 5px 0; border-bottom: 1px solid var(--line); }
.log time { color: var(--muted); font-variant-numeric: tabular-nums; flex: none; }
.pad { padding: 10px 12px; display: flex; flex-direction: column; gap: 8px; min-height: 0; }
.stat { display: flex; align-items: center; gap: 10px; padding: 9px 10px; background: var(--panel2); border-radius: 9px; font-size: 13px; }
.stat .grow b { display: block; font-size: 14px; }
.stat .grow span { font-size: 11px; color: var(--muted); }
.steps { display: flex; gap: 4px; }
.steps div { flex: 1; text-align: center; font-size: 11px; color: var(--muted); padding-top: 8px; border-top: 4px solid var(--panel2); }
.steps div.done { border-top-color: #38bdf877; color: var(--text); }
.steps div.cur { border-top-color: var(--ac); color: var(--ac); font-weight: 700; }
.btn { height: 44px; padding: 0 18px; border-radius: 9px; background: var(--panel2); border: 1px solid var(--line); font-weight: 700; font-size: 14px; display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
.btn.pri { background: var(--ac); color: #04222f; border-color: var(--ac); }
.btn.warn { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn.sm { height: 30px; padding: 0 12px; font-size: 12px; }

/* ---------- Overlays ---------- */
.overlay { position: absolute; inset: 0; background: #000a; display: grid; place-items: center; z-index: 20; }
.dialog { width: 340px; padding: 18px; text-align: center; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; }
.dialog h3 { margin: 6px 0 4px; font-size: 18px; }
.dialog p { margin: 0 0 14px; font-size: 13px; color: var(--muted); }
.dialog .row { justify-content: center; }
.dialog .btn { flex: 1; }
.pop { position: absolute; right: 8px; top: 44px; width: 250px; padding: 10px; z-index: 15; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 10px 30px #000a; display: flex; flex-direction: column; gap: 6px; }
.pop .sw { display: flex; align-items: center; gap: 8px; padding: 9px 10px; border-radius: 8px; background: var(--panel2); font-size: 13px; text-align: left; }
.pop .sw i { width: 34px; height: 20px; border-radius: 20px; background: #3b4657; position: relative; flex: none; transition: background .2s; }
.pop .sw i::after { content: ""; position: absolute; left: 2px; top: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: left .2s; }
.pop .sw.on i { background: var(--ok); } .pop .sw.on i::after { left: 16px; }
#toast { position: absolute; left: 50%; bottom: 14px; transform: translate(-50%, 30px); opacity: 0; transition: all .3s; z-index: 30; pointer-events: none; padding: 8px 16px; border-radius: 99px; font-size: 13px; font-weight: 600; background: #e8edf4; color: #10151c; white-space: nowrap; }
#toast.show { transform: translate(-50%, 0); opacity: 1; }
#toast.rain { background: var(--rain); color: #06162e; }

/* ---------- Login ---------- */
.login { display: grid; grid-template-columns: 1fr 360px; height: 100%; }
.brand { padding: 40px; display: flex; flex-direction: column; justify-content: center; background: radial-gradient(circle at 20% 30%, #ffa62b30, transparent 55%), radial-gradient(circle at 80% 90%, #38bdf822, transparent 50%); }
.brand .logo { width: 56px; height: 56px; border-radius: 14px; background: var(--light); color: #2a1800; display: grid; place-items: center; margin-bottom: 14px; }
.brand .logo svg.ic { width: 32px; height: 32px; stroke-width: 2; }
.brand h1 { margin: 0; font-size: 34px; }
.brand p { margin: 4px 0 0; color: var(--muted); font-size: 14px; }
.brand .clock { font-size: 54px; font-weight: 700; margin-top: 26px; font-variant-numeric: tabular-nums; line-height: 1; }
.lform { background: var(--panel); border-left: 1px solid var(--line); padding: 16px 28px; display: flex; flex-direction: column; gap: 10px; justify-content: center; }
.tabs { display: flex; background: var(--panel2); border-radius: 9px; padding: 3px; }
.tabs button { flex: 1; height: 34px; border-radius: 7px; font-size: 13px; font-weight: 600; color: var(--muted); }
.tabs button.on { background: var(--light); color: #2a1800; }
.dots { display: flex; justify-content: center; gap: 14px; height: 26px; align-items: center; }
.dots i { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--muted); }
.dots i.f { background: var(--light); border-color: var(--light); }
.dots.err { animation: shake .35s; } .dots.err i { border-color: var(--bad); }
@keyframes shake { 25% { transform: translateX(-8px); } 75% { transform: translateX(8px); } }
.keys { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.keys button { height: 56px; border-radius: 10px; background: var(--panel2); border: 1px solid var(--line); font-size: 22px; font-weight: 600; }
.keys button:active { background: var(--light); color: #2a1800; }
.lform input { width: 100%; height: 48px; border-radius: 9px; border: 1px solid var(--line); background: var(--panel2); color: var(--text); font: inherit; font-size: 16px; padding: 0 14px; outline: none; user-select: text; }
.lform input:focus { border-color: var(--light); }
.lform label { font-size: 12px; color: var(--muted); margin-bottom: -6px; }
.lform .btn.go { background: var(--light); color: #2a1800; border-color: var(--light); height: 50px; font-size: 16px; }
.hint { font-size: 11px; color: var(--muted); text-align: center; }
.err-t { color: var(--bad); font-size: 12px; text-align: center; min-height: 16px; }
