:root {
  --bg-top: #18325f;
  --bg-mid: #0c1b38;
  --bg-bot: #060d1d;
  --text: #f2f6fc;
  --muted: #8d9bb5;
  --tile: rgb(255 255 255 / .045);
  --tile-line: rgb(255 255 255 / .08);
  --track: #040a17;
  --off: #5b6b86;
  --cool: #3d8bff;
  --heat: #ff8a3d;
  --fan: #2fd1b0;
  --dry: #9b8cff;
  --auto: #f2c14e;
  --danger: #ff5d55;
  --accent: var(--off);
  --accent-2: #7b8aa5;
  --ease-out: cubic-bezier(.23, 1, .32, 1);
  --ease-drawer: cubic-bezier(.32, .72, 0, 1);
  --safe-y: calc(env(safe-area-inset-top, 0px) + env(safe-area-inset-bottom, 0px));
  color-scheme: dark;
}
body[data-mode="COOL"] { --accent: var(--cool); --accent-2: #7cc0ff; }
body[data-mode="HEAT"] { --accent: var(--heat); --accent-2: #ffc07a; --bg-top: #4a2a1c; --bg-mid: #1d1424; }
body[data-mode="FAN"]  { --accent: var(--fan);  --accent-2: #8af0da; --bg-top: #123f48; }
body[data-mode="DRY"]  { --accent: var(--dry);  --accent-2: #c6bdff; --bg-top: #2c2a62; }
body[data-mode="AUTO"] { --accent: var(--auto); --accent-2: #ffe199; --bg-top: #3c3720; }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { background: var(--bg-bot); }
body {
  margin: 0;
  min-height: 100dvh;
  color: var(--text);
  background: radial-gradient(130% 70% at 50% 0%, var(--bg-top) 0%, var(--bg-mid) 50%, var(--bg-bot) 100%) fixed;
  transition: background .6s ease;
  font: 16px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI", Rubik, system-ui, sans-serif;
  padding: env(safe-area-inset-top) 16px env(safe-area-inset-bottom);
  -webkit-user-select: none; user-select: none;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; padding: 0; touch-action: manipulation; -webkit-touch-callout: none; }
button:disabled { opacity: .35; cursor: default; }
svg { width: 24px; height: 24px; display: block; }
[hidden] { display: none !important; }
.sprite { position: absolute; width: 0; height: 0; }

/* ---------- gate ---------- */
.gate { min-height: calc(100dvh - var(--safe-y)); display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; gap: 8px; }
.gate-icon { width: 88px; height: 88px; border-radius: 22px; overflow: hidden; margin-bottom: 10px; box-shadow: 0 12px 40px rgb(0 0 0 / .45); }
.gate-icon img { width: 100%; height: 100%; display: block; }
.gate h1 { margin: 0; font-size: 30px; }
.gate p { margin: 0 0 22px; color: var(--muted); }
.primary { background: linear-gradient(160deg, #4ea0ff, #2a63d6); color: #fff; padding: 17px 28px; border-radius: 18px; font-weight: 600; font-size: 17px; min-width: 250px; box-shadow: 0 10px 30px rgb(61 139 255 / .35); transition: transform 120ms var(--ease-out), opacity .2s; }
.primary:active { transform: scale(.97); }
.gate-form { display: flex; flex-direction: column; gap: 12px; width: min(300px, 100%); }
.code-input {
  font: 600 22px/1 ui-monospace, "SF Mono", Menlo, monospace; /* ≥16px or iOS zooms in on focus */
  letter-spacing: 3px; text-align: center; text-transform: uppercase;
  padding: 16px; border-radius: 16px; color: var(--text); caret-color: var(--accent-2);
  background: rgb(255 255 255 / .06); border: 1px solid var(--tile-line); outline: none;
  user-select: text; -webkit-user-select: text;
  transition: border-color .15s, background .15s;
}
.code-input::placeholder { color: rgb(255 255 255 / .2); }
.code-input:focus { border-color: color-mix(in srgb, var(--accent) 70%, transparent); background: rgb(255 255 255 / .08); }
.gate .primary { min-width: 0; width: 100%; }
#gate-passkey { margin-top: 14px; }
.code-big { direction: ltr; text-align: center; font: 600 34px/1.2 ui-monospace, "SF Mono", Menlo, monospace; letter-spacing: 4px; padding: 18px 0 6px; user-select: text; -webkit-user-select: text; }
.code-note { text-align: center; color: var(--muted); font-size: 14px; margin: 0 0 6px; }
.err { color: var(--danger); min-height: 1.4em; margin: 12px 0 0; font-size: 14px; }

/* ---------- main ---------- */
.app { max-width: 440px; margin: 0 auto; padding: 18px 0 20px; display: flex; flex-direction: column; min-height: calc(100dvh - var(--safe-y)); }

.top { display: flex; align-items: center; justify-content: space-between; }
.room { display: flex; align-items: center; gap: 8px; font-size: 19px; }
.therm { color: var(--accent); width: 22px; height: 22px; transition: color .4s; }
.room-label { color: var(--muted); }
.room-val { font-weight: 700; }
.live { width: 9px; height: 9px; border-radius: 50%; background: #3ad06b; box-shadow: 0 0 10px #3ad06b; margin-inline-start: 4px; }
.live.stale { background: #b9a13a; box-shadow: none; }

.power { width: 60px; height: 60px; border-radius: 50%; display: grid; place-items: center; background: rgb(255 255 255 / .08); color: var(--muted); transition: transform 120ms var(--ease-out), background .3s, box-shadow .3s, color .3s; }
.power svg { width: 28px; height: 28px; }
.power[aria-pressed="true"] { background: radial-gradient(circle at 35% 30%, var(--accent-2), var(--accent)); color: #fff; box-shadow: 0 0 0 6px rgb(255 255 255 / .04), 0 10px 32px color-mix(in srgb, var(--accent) 60%, transparent); }
.power:active { transform: scale(.94); }

/* AC unit + airflow */
.unit-wrap { position: relative; display: flex; flex-direction: column; align-items: center; margin: 26px 0 0; height: 230px; }
.unit { position: relative; z-index: 1; width: 62%; max-width: 250px; height: 74px; border-radius: 16px 16px 22px 22px; background: linear-gradient(180deg, #ffffff 0%, #e9eef6 70%, #d4dbe7 100%); box-shadow: 0 14px 40px rgb(0 0 0 / .45), inset 0 -3px 0 rgb(0 0 0 / .06); }
.unit-display { position: absolute; top: 18px; right: 22px; direction: ltr; display: flex; align-items: center; gap: 8px; font-size: 15px; font-weight: 600; color: var(--accent); transition: color .4s; }
body.off .unit-display { color: #b8c1cf; }
.unit-led { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 8px var(--accent); }
body.off .unit-led { background: #c5ccd8; box-shadow: none; }
.unit-vent { position: absolute; left: 8%; right: 8%; bottom: 12px; height: 7px; border-radius: 4px; background: linear-gradient(180deg, #8a93a3, #6b7484); }

.air { position: absolute; top: 62px; width: 100%; height: 165px; overflow: visible;
  -webkit-mask-image: linear-gradient(180deg, #000 0%, #000 45%, transparent 100%);
          mask-image: linear-gradient(180deg, #000 0%, #000 45%, transparent 100%);
  transition: opacity .6s;
}
.air path { fill: none; stroke: var(--accent-2); stroke-width: 1.6; stroke-linecap: round; stroke-dasharray: 10 18; opacity: .7; animation: flow 1.6s linear infinite; }
.air path:nth-child(2n) { animation-duration: 2s; opacity: .5; }
.air path:nth-child(3n) { animation-duration: 2.4s; }
body.off .air { opacity: 0; }
@keyframes flow { to { stroke-dashoffset: -56; } }
@media (prefers-reduced-motion: reduce) { .air path { animation: none; } }

/* temperature */
.temp-row { display: flex; align-items: flex-end; justify-content: space-between; margin-top: 4px; }
.big { direction: ltr; font-size: clamp(84px, 27vw, 112px); font-weight: 200; line-height: .9; letter-spacing: -2px; font-variant-numeric: tabular-nums; }
.unit-c { font-size: .3em; font-weight: 500; color: var(--accent); margin-left: 6px; letter-spacing: 0; vertical-align: 1.2em; transition: color .4s; }
body.off .big { color: var(--muted); }
.state { padding: 10px 16px; border-radius: 16px; background: var(--tile); border: 1px solid var(--tile-line); font-weight: 600; color: var(--accent); margin-bottom: 10px; transition: color .4s; }

/* slider */
.slider-row { direction: ltr; display: flex; align-items: center; gap: 12px; margin-top: 26px; }
.step { width: 52px; height: 52px; border-radius: 50%; flex: none; display: grid; place-items: center; background: var(--tile); border: 1px solid var(--tile-line); transition: transform 120ms var(--ease-out), background .15s; }
.step:active { transform: scale(.92); background: rgb(255 255 255 / .1); }
.track { flex: 1; }
#slider {
  --p: .5;
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 60px; margin: 0; border-radius: 30px; display: block;
  background:
    linear-gradient(90deg, color-mix(in srgb, var(--accent) 70%, #000), var(--accent)) no-repeat 0 0 / calc(30px + (100% - 60px) * var(--p)) 100%,
    repeating-linear-gradient(90deg, rgb(255 255 255 / .2) 0 1.5px, transparent 1.5px 13px) no-repeat 50% 76% / calc(100% - 64px) 10px,
    var(--track);
  box-shadow: inset 0 2px 8px rgb(0 0 0 / .6);
  touch-action: none;
}
#slider::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 60px; height: 60px; border-radius: 22px;
  background: linear-gradient(180deg, #fff, #e6ebf3);
  box-shadow: 0 4px 16px rgb(0 0 0 / .45), 0 0 24px color-mix(in srgb, var(--accent) 55%, transparent);
  border: 4px solid color-mix(in srgb, var(--accent) 35%, #fff);
}
#slider::-moz-range-thumb { width: 52px; height: 52px; border-radius: 22px; background: #fff; border: 4px solid color-mix(in srgb, var(--accent) 35%, #fff); }
#slider:disabled { opacity: .45; }
.range-labels { direction: ltr; display: flex; justify-content: space-between; padding: 8px 76px 0; color: var(--muted); font-size: 14px; font-weight: 500; }

/* tiles */
.tiles { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 26px; }
.tile { position: relative; text-align: start; padding: 14px 10px 12px; border-radius: 20px; background: var(--tile); border: 1px solid var(--tile-line); display: flex; flex-direction: column; gap: 2px; min-height: 112px; transition: transform 120ms var(--ease-out), background .25s, border-color .25s; }
.tile:active { transform: scale(.97); }
.t-ico { width: 28px; height: 28px; color: var(--accent); margin-bottom: auto; transition: color .4s; }
.t-chev { position: absolute; top: 12px; inset-inline-end: 9px; width: 16px; height: 16px; color: var(--muted); }
.t-dot { position: absolute; top: 15px; inset-inline-end: 12px; width: 8px; height: 8px; border-radius: 50%; background: rgb(255 255 255 / .22); }
.tile[aria-pressed="true"] { background: color-mix(in srgb, var(--accent) 16%, transparent); border-color: color-mix(in srgb, var(--accent) 40%, transparent); }
.tile[aria-pressed="true"] .t-dot { background: var(--accent); box-shadow: 0 0 8px var(--accent); }
.t-label { font-size: 13px; color: var(--muted); margin-top: 12px; }
.tile b { font-size: 15px; font-weight: 700; }
body.off .tile .t-ico { color: var(--muted); }

.foot { margin-top: auto; padding-top: 22px; display: flex; justify-content: center; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.link { color: var(--muted); text-decoration: underline; text-underline-offset: 3px; font-size: 13px; padding: 10px 6px; margin: -10px -6px; transition: opacity .1s; }
.link:active { opacity: .5; }
.spinner { width: 13px; height: 13px; border: 2px solid rgb(255 255 255 / .15); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast { position: fixed; left: 16px; right: 16px; bottom: calc(16px + env(safe-area-inset-bottom)); background: var(--danger); color: #fff; padding: 12px 16px; border-radius: 16px; text-align: center; margin: 0; z-index: 20; user-select: text; -webkit-user-select: text; }
.toast.ok { background: #1f3a2c; border: 1px solid #2f6b4a; color: #c9f5da; }

/* sheet */
.sheet { position: fixed; inset: 0; background: rgb(0 0 0 / .5); display: flex; align-items: flex-end; z-index: 10; opacity: 0; transition: opacity 200ms var(--ease-out); }
.sheet[data-open] { opacity: 1; transition-duration: 300ms; }
.sheet-card { width: 100%; max-width: 440px; margin: 0 auto; background: #111d33; border: 1px solid var(--tile-line); border-bottom: 0; border-radius: 26px 26px 0 0; padding: 8px 16px calc(14px + env(safe-area-inset-bottom, 0px)); outline: none; touch-action: none;
  transform: translateY(100%); transition: transform 200ms var(--ease-out); }
.sheet[data-open] .sheet-card { transform: none; transition: transform 400ms var(--ease-drawer); }
.sheet-handle { width: 38px; height: 5px; border-radius: 3px; background: rgb(255 255 255 / .2); margin: 0 auto 14px; }
.sheet-card h3 { margin: 0 4px 12px; font-size: 15px; color: var(--muted); font-weight: 600; }
.sheet-body { display: flex; flex-direction: column; gap: 8px; }
.opt { display: flex; align-items: center; gap: 12px; padding: 15px 16px; border-radius: 16px; background: var(--tile); border: 1px solid var(--tile-line); font-weight: 600; transition: transform 120ms var(--ease-out), background .15s; }
.opt:active { transform: scale(.98); background: rgb(255 255 255 / .09); }
.opt svg { color: var(--c, var(--accent)); }
.opt.on { background: color-mix(in srgb, var(--c, var(--accent)) 20%, transparent); border-color: color-mix(in srgb, var(--c, var(--accent)) 50%, transparent); }
.opt.danger { color: var(--danger); }
.ghost { width: 100%; padding: 14px; margin-top: 8px; color: var(--muted); border-radius: 16px; transition: background .15s; }
.ghost:active { background: rgb(255 255 255 / .06); }
@media (prefers-reduced-motion: reduce) {
  .sheet-card, .sheet[data-open] .sheet-card { transform: none; opacity: 0; transition: opacity 200ms ease; }
  .sheet[data-open] .sheet-card { opacity: 1; }
}

@media (max-width: 360px) {
  .tiles { grid-template-columns: repeat(2, 1fr); }
  .range-labels { padding-inline: 70px; }
}

/* ---------- mouse + keyboard (Mac popover, desktop browsers) ---------- */
/* Gated by capability so taps on the phone never leave a stuck hover. */
@media (hover: hover) and (pointer: fine) {
  .tile:not(:disabled):hover, .step:not(:disabled):hover, .opt:hover { background: rgb(255 255 255 / .08); }
  .tile[aria-pressed="true"]:not(:disabled):hover { background: color-mix(in srgb, var(--accent) 22%, transparent); }
  .power:hover { color: var(--text); }
  .power[aria-pressed="true"]:hover { filter: brightness(1.08); }
  .ghost:hover, .link:hover { color: var(--text); }
  #slider { cursor: grab; }
  #slider:active { cursor: grabbing; }
}
:focus { outline: none; }
:focus-visible { outline: 2px solid var(--accent-2); outline-offset: 3px; }
#slider:focus-visible { outline-offset: 4px; }

/* Short viewports (the 380×700 Mac popover): tighten the vertical rhythm. */
@media (max-height: 740px) {
  .app { padding-top: 14px; }
  .unit-wrap { height: 180px; margin-top: 18px; }
  .air { height: 120px; }
  .slider-row, .tiles { margin-top: 18px; }
  .tile { min-height: 100px; }
  .foot { padding-top: 14px; }
}
.native .foot { padding-bottom: 4px; }
