:root {
  --bg: #0b1020;
  --text: #eef2ff;
  --muted: rgba(226, 232, 255, .62);
  --glass: rgba(255, 255, 255, .07);
  --glass-border: rgba(255, 255, 255, .12);
  --a: #38bdf8;
  --b: #818cf8;
  --glow: rgba(56, 189, 248, .45);
  --radius: 26px;
  color-scheme: dark;
}

body.mode-COOL { --a: #22d3ee; --b: #3b82f6; --glow: rgba(34, 211, 238, .5); }
body.mode-HEAT { --a: #fb923c; --b: #f43f5e; --glow: rgba(251, 113, 133, .5); }
body.mode-DRY, body.mode-SMART_DRY { --a: #2dd4bf; --b: #0ea5e9; --glow: rgba(45, 212, 191, .45); }
body.mode-FAN_ONLY { --a: #a3e635; --b: #10b981; --glow: rgba(163, 230, 53, .4); }
body.mode-AUTO { --a: #c084fc; --b: #6366f1; --glow: rgba(192, 132, 252, .45); }
body.off { --a: #64748b; --b: #334155; --glow: rgba(100, 116, 139, .25); }

* { box-sizing: border-box; }

html, body { margin: 0; min-height: 100%; }

body {
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

body.loading .screen { opacity: 0; }

.backdrop { position: fixed; inset: 0; overflow: hidden; z-index: -1; }
.backdrop i {
  position: absolute;
  width: 60vmax; height: 60vmax;
  border-radius: 50%;
  filter: blur(90px);
  opacity: .35;
  transition: background 1.2s ease;
  animation: drift 22s ease-in-out infinite alternate;
}
.backdrop i:nth-child(1) { background: var(--a); top: -25vmax; left: -15vmax; }
.backdrop i:nth-child(2) { background: var(--b); bottom: -30vmax; right: -20vmax; animation-delay: -8s; }
.backdrop i:nth-child(3) { background: var(--a); top: 40%; left: 55%; width: 35vmax; height: 35vmax; opacity: .18; animation-delay: -14s; }

@keyframes drift {
  from { transform: translate(0, 0) scale(1); }
  to { transform: translate(6vmax, 4vmax) scale(1.12); }
}

.screen {
  max-width: 560px;
  margin: 0 auto;
  padding: max(20px, env(safe-area-inset-top)) 18px 40px;
  transition: opacity .4s ease;
}

.glass {
  background: var(--glass);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius);
  backdrop-filter: blur(22px) saturate(140%);
  -webkit-backdrop-filter: blur(22px) saturate(140%);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .35), inset 0 1px 0 rgba(255, 255, 255, .08);
}

h1 { margin: 0; font-size: 30px; font-weight: 700; letter-spacing: -.02em; }
h2 { margin: 0 0 14px; font-size: 13px; font-weight: 600; letter-spacing: .12em; text-transform: uppercase; color: var(--muted); }
.muted { color: var(--muted); }
.error { color: #fda4af; min-height: 1.2em; margin: 4px 0 0; font-size: 14px; }

button, input { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; -webkit-tap-highlight-color: transparent; }
button:disabled { cursor: progress; opacity: .6; }
button:focus-visible, input:focus-visible, #dial:focus-visible { outline: 2px solid var(--a); outline-offset: 3px; }

input {
  width: 100%;
  padding: 14px 16px;
  margin: 6px 0;
  border-radius: 14px;
  border: 1px solid var(--glass-border);
  background: rgba(0, 0, 0, .25);
  outline: none;
}

.primary {
  width: 100%;
  margin-top: 10px;
  padding: 14px;
  border-radius: 14px;
  font-weight: 600;
  background: linear-gradient(135deg, var(--a), var(--b));
  box-shadow: 0 10px 30px var(--glow);
  transition: transform .15s ease, box-shadow .3s ease;
}
.primary:active { transform: scale(.98); }

/* Login */
#login { display: grid; place-items: center; min-height: 100dvh; padding-block: 16px; }
#login[hidden], #control[hidden] { display: none; }
.login-card { width: min(380px, 100%); padding: 34px 28px; text-align: center; }
.login-card h1 { margin-top: 14px; }
.logo {
  width: 74px; height: 74px; margin: 0 auto;
  border-radius: 24px;
  background: linear-gradient(135deg, var(--a), var(--b));
  box-shadow: 0 14px 40px var(--glow);
  display: grid; place-items: center;
}
.logo svg { width: 40px; height: 40px; }

/* Top bar */
.topbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin: 6px 4px 18px; }
.topbar > div:first-child { min-width: 0; }
.topbar h1 { font-size: clamp(22px, 7vw, 30px); overflow-wrap: anywhere; }
.topbar-actions { display: flex; gap: 8px; flex: none; }
.status { display: flex; align-items: center; gap: 8px; margin: 4px 0 0; font-size: 14px; color: var(--muted); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #34d399; box-shadow: 0 0 10px #34d399; }
.status.busy .dot { background: #fbbf24; box-shadow: 0 0 10px #fbbf24; animation: pulse 1s ease-in-out infinite; }
.status.error .dot { background: #f43f5e; box-shadow: 0 0 10px #f43f5e; }
@keyframes pulse { 50% { opacity: .3; } }

.icon-button {
  width: 44px; height: 44px;
  display: grid; place-items: center;
  border-radius: 14px;
  background: var(--glass);
  border: 1px solid var(--glass-border);
}
.icon-button svg { width: 20px; height: 20px; }

.language {
  height: 44px;
  padding: 0 10px;
  border-radius: 14px;
  border: 1px solid var(--glass-border);
  background: var(--glass);
  color: var(--text);
  font: inherit; font-size: 14px;
  cursor: pointer;
}
.language option { background: #141a2e; color: var(--text); }
.language:focus-visible { outline: 2px solid var(--a); outline-offset: 3px; }
.language[hidden] { display: none; }
.login-card .language { margin-top: 14px; height: 38px; }
.icon-button.spin svg { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Dial */
.hero { padding: 22px 20px 20px; }
.dial-wrap { position: relative; width: min(320px, 100%); margin: 0 auto; aspect-ratio: 1; }
#dial { width: 100%; height: 100%; touch-action: none; cursor: grab; user-select: none; }
#dial.dragging { cursor: grabbing; }
.track { fill: none; stroke: rgba(255, 255, 255, .08); stroke-width: 18; stroke-linecap: round; }
.progress { fill: none; stroke: url(#arc-gradient); stroke-width: 18; stroke-linecap: round; transition: d .25s ease; }
.stop-a { stop-color: var(--a); transition: stop-color 1s; }
.stop-b { stop-color: var(--b); transition: stop-color 1s; }
.knob { fill: #fff; stroke: var(--b); stroke-width: 5; filter: drop-shadow(0 4px 10px rgba(0, 0, 0, .5)); transition: stroke 1s; }
#ticks line { stroke: rgba(255, 255, 255, .18); stroke-width: 2; stroke-linecap: round; }
#ticks line.major { stroke: rgba(255, 255, 255, .4); }
#ticks text { fill: var(--muted); font-size: 11px; text-anchor: middle; dominant-baseline: middle; }

.dial-center {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  pointer-events: none;
}
.dial-center .label { font-size: 13px; color: var(--muted); letter-spacing: .04em; }
#mode-label { text-transform: uppercase; letter-spacing: .14em; font-weight: 600; color: var(--a); transition: color 1s; }
.target { font-size: 76px; font-weight: 200; line-height: 1; margin: 6px 0; letter-spacing: -.04em; font-variant-numeric: tabular-nums; }
.target sup { font-size: 22px; font-weight: 400; vertical-align: 2.1em; margin-left: 2px; color: var(--muted); }
body.off .target { opacity: .35; }

.stepper { display: flex; justify-content: center; align-items: center; gap: 26px; margin-top: 4px; }
.round {
  width: 58px; height: 58px; border-radius: 50%;
  font-size: 28px; font-weight: 300;
  background: var(--glass); border: 1px solid var(--glass-border);
  transition: transform .12s ease, background .2s;
}
.round:active { transform: scale(.92); background: rgba(255, 255, 255, .15); }
.power {
  width: 78px; height: 78px; border-radius: 50%;
  display: grid; place-items: center;
  background: linear-gradient(135deg, var(--a), var(--b));
  box-shadow: 0 0 0 6px rgba(255, 255, 255, .05), 0 14px 40px var(--glow);
  transition: transform .15s ease, box-shadow .6s ease;
}
.power svg { width: 32px; height: 32px; }
.power:active { transform: scale(.94); }
body.off .power { background: rgba(255, 255, 255, .08); box-shadow: 0 0 0 6px rgba(255, 255, 255, .04); }

.readings { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 22px; }
.reading {
  display: flex; align-items: center; gap: 12px;
  padding: 14px; border-radius: 18px;
  background: rgba(0, 0, 0, .18);
}
.reading div { display: flex; flex-direction: column; }
.reading strong { font-size: 22px; font-weight: 500; font-variant-numeric: tabular-nums; }
.reading .muted { font-size: 13px; }
.reading-icon {
  width: 40px; height: 40px; flex: none;
  display: grid; place-items: center;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--a), var(--b));
}
.reading-icon svg { width: 20px; height: 20px; }

/* Panels */
.panel { margin-top: 16px; padding: 20px; }
.mode-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.mode {
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  padding: 16px 6px 12px;
  border-radius: 18px;
  background: rgba(0, 0, 0, .18);
  border: 1px solid transparent;
  font-size: 13px;
  transition: background .25s, border-color .25s, transform .12s;
}
.mode svg { width: 26px; height: 26px; opacity: .8; }
.mode:active { transform: scale(.96); }
.mode[aria-pressed="true"] {
  background: linear-gradient(135deg, color-mix(in srgb, var(--a) 35%, transparent), color-mix(in srgb, var(--b) 35%, transparent));
  border-color: color-mix(in srgb, var(--a) 70%, transparent);
  box-shadow: 0 8px 24px var(--glow);
}
.mode[aria-pressed="true"] svg { opacity: 1; }

.segmented {
  display: flex; flex-wrap: wrap; gap: 4px; padding: 4px;
  border-radius: 16px;
  background: rgba(0, 0, 0, .22);
}
.segmented button {
  flex: 1 0 28%;
  padding: 11px 12px;
  border-radius: 12px;
  font-size: 14px;
  color: var(--muted);
  white-space: nowrap;
  transition: background .25s, color .25s;
}
.segmented button[aria-pressed="true"] {
  color: var(--text);
  background: linear-gradient(135deg, var(--a), var(--b));
  box-shadow: 0 6px 18px var(--glow);
}

.fan-bars { display: inline-flex; gap: 2px; align-items: flex-end; height: 12px; margin-right: 6px; vertical-align: -1px; }
.fan-bars i { width: 3px; border-radius: 2px; background: currentColor; opacity: .3; }
.fan-bars i.on { opacity: 1; }

body.off #tab-control .panel { opacity: .55; }

.footnote { text-align: center; font-size: 13px; margin-top: 18px; }

/* Dialog and toast */
dialog { width: min(400px, calc(100% - 32px)); padding: 26px; color: var(--text); border-radius: var(--radius); }
dialog::backdrop { background: rgba(5, 8, 20, .6); backdrop-filter: blur(4px); }
dialog h2 { font-size: 20px; text-transform: none; letter-spacing: 0; color: var(--text); }
.dialog-actions { display: flex; gap: 10px; margin-top: 8px; }
.dialog-actions button { flex: 1; padding: 14px; border-radius: 14px; margin-top: 0; }
#midea-cancel { background: var(--glass); border: 1px solid var(--glass-border); }

/* Tabs */
#control { padding-bottom: calc(110px + env(safe-area-inset-bottom)); }
.tab[hidden], .tabbar[hidden] { display: none; }
.tab > .panel:first-child { margin-top: 0; }
.tabbar {
  position: fixed; z-index: 10;
  left: 50%; bottom: max(12px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: min(536px, calc(100% - 24px));
  display: grid; grid-template-columns: repeat(5, 1fr);
  padding: 6px;
  border-radius: 24px;
  background: rgba(14, 18, 34, .78);
}
.tabbar button {
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  padding: 8px 2px 7px;
  border-radius: 18px;
  font-size: 11px; color: var(--muted);
  transition: color .25s, background .25s;
}
.tabbar button span { display: grid; }
.tabbar button svg { width: 22px; height: 22px; }
.tabbar button[aria-current="page"] {
  color: var(--text);
  background: linear-gradient(135deg, color-mix(in srgb, var(--a) 40%, transparent), color-mix(in srgb, var(--b) 40%, transparent));
  box-shadow: 0 6px 20px var(--glow);
}

/* Features */
.feature-list { display: flex; flex-direction: column; gap: 4px; }
.feature {
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
  padding: 12px 14px;
  border-radius: 16px;
  background: rgba(0, 0, 0, .16);
  border: 1px solid transparent;
  transition: border-color .25s;
}
.feature.pending { border-color: color-mix(in srgb, #fbbf24 60%, transparent); }
.feature.number, .feature.choice { flex-direction: column; align-items: stretch; }
.feature-text { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.feature-text small { font-size: 12.5px; }
.unsupported .feature { opacity: .45; }
.unsupported summary { list-style: none; cursor: pointer; }
.unsupported summary::-webkit-details-marker { display: none; }
.unsupported summary h2 { margin: 0; display: flex; justify-content: space-between; }
.unsupported summary h2::after { content: "▾"; transition: transform .2s; }
.unsupported[open] summary h2::after { transform: rotate(180deg); }
.unsupported[open] summary { margin-bottom: 14px; }

.switch { position: relative; flex: none; width: 52px; height: 30px; }
.switch input { position: absolute; inset: 0; margin: 0; padding: 0; opacity: 0; cursor: pointer; }
.switch span {
  position: absolute; inset: 0; pointer-events: none;
  border-radius: 99px;
  background: rgba(255, 255, 255, .12);
  transition: background .25s;
}
.switch span::after {
  content: ""; position: absolute; top: 3px; left: 3px;
  width: 24px; height: 24px; border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 6px rgba(0, 0, 0, .4);
  transition: transform .25s cubic-bezier(.3, .9, .4, 1.3);
}
.switch input:checked + span { background: linear-gradient(135deg, var(--a), var(--b)); }
.switch input:checked + span::after { transform: translateX(22px); }
.switch input:focus-visible + span { outline: 2px solid var(--a); outline-offset: 3px; }
.switch input:disabled { cursor: not-allowed; }

.range { display: flex; align-items: center; gap: 12px; }
.range input { flex: 1; margin: 0; padding: 0; border: 0; background: none; accent-color: var(--a); }
.range output { min-width: 48px; text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.segmented.compact button { flex: 1 0 16%; padding: 8px; font-size: 13px; }
.segmented button:disabled { cursor: not-allowed; }

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.action {
  display: flex; align-items: center; gap: 12px; text-align: left;
  padding: 14px; border-radius: 18px;
  background: rgba(0, 0, 0, .18);
  border: 1px solid var(--glass-border);
  transition: transform .12s, background .25s;
}
.action:active { transform: scale(.97); }
.action > span:last-child { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.action small { font-size: 12px; }
.action-icon { width: 40px; height: 40px; flex: none; display: grid; place-items: center; border-radius: 12px; background: linear-gradient(135deg, var(--a), var(--b)); }
.action-icon svg { width: 22px; height: 22px; }
.action.unsupported { opacity: .4; cursor: not-allowed; }
.action.unsupported .action-icon { background: rgba(255, 255, 255, .1); }

/* Diagnostics */
.diag-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 14px; }
.diag-head h2 { margin-bottom: 6px; }
.diag-head p { margin: 0; font-size: 13px; }
.badge { flex: none; padding: 8px 12px; border-radius: 99px; font-size: 13px; font-weight: 600; background: rgba(255, 255, 255, .08); white-space: nowrap; }
.badge.good { background: rgba(52, 211, 153, .16); color: #6ee7b7; }
.badge.bad { background: rgba(244, 63, 94, .2); color: #fda4af; }
.metrics { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.metric {
  display: flex; flex-direction: column; gap: 2px;
  padding: 12px 14px 8px; border-radius: 16px;
  background: rgba(0, 0, 0, .18);
  overflow: hidden;
}
.metric > .muted:first-child { font-size: 12.5px; }
.metric strong { font-size: 24px; font-weight: 500; font-variant-numeric: tabular-nums; }
.metric strong small { font-size: 13px; color: var(--muted); font-weight: 400; }
.metric > small { font-size: 12px; }
.metric.missing strong { opacity: .35; }
.spark { width: 100%; height: 28px; margin-top: 4px; }
.spark-line { fill: none; stroke: var(--a); stroke-width: 1.6; vector-effect: non-scaling-stroke; }
.spark-area { fill: color-mix(in srgb, var(--a) 18%, transparent); }
.flags { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.flag { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 14px; background: rgba(0, 0, 0, .18); font-size: 14px; }
.flag span { flex: 1; }
.flag i { width: 9px; height: 9px; border-radius: 50%; background: rgba(255, 255, 255, .25); flex: none; }
.flag.on i { background: var(--a); box-shadow: 0 0 10px var(--a); }

/* Device */
.facts { display: grid; grid-template-columns: minmax(110px, 38%) 1fr; gap: 8px 14px; margin: 0; font-size: 14px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; overflow-wrap: anywhere; }
#capabilities dt::first-letter { text-transform: uppercase; }
.facts.small { font-size: 12.5px; gap: 4px 10px; margin-top: 8px; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.stat { display: flex; flex-direction: column; gap: 2px; padding: 12px 8px; border-radius: 14px; background: rgba(0, 0, 0, .18); text-align: center; }
.stat strong { font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; }
.stat span { font-size: 11.5px; }

/* Log */
.log-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.chip {
  padding: 10px 14px; border-radius: 99px; font-size: 14px;
  background: linear-gradient(135deg, color-mix(in srgb, var(--a) 30%, transparent), color-mix(in srgb, var(--b) 30%, transparent));
  border: 1px solid color-mix(in srgb, var(--a) 50%, transparent);
}
.chip:active { transform: scale(.97); }
.log-filter button { flex: 1 0 20%; padding: 8px; font-size: 13px; }
.log { list-style: none; margin: 16px 0 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.log-entry {
  padding: 12px 14px; border-radius: 16px;
  background: rgba(10, 14, 28, .6);
  border: 1px solid var(--glass-border);
  border-left: 3px solid var(--a);
}
.log-entry.rx { border-left-color: #34d399; }
.log-entry.error { border-left-color: #f43f5e; }
.log-head { display: flex; align-items: center; gap: 10px; font-size: 14px; }
.log-head strong { flex: 1; font-weight: 600; }
.log-head .muted { font-size: 12px; white-space: nowrap; }
.dir { width: 24px; height: 24px; flex: none; display: grid; place-items: center; border-radius: 8px; font-weight: 700; background: color-mix(in srgb, var(--a) 25%, transparent); color: var(--a); }
.dir.rx { background: rgba(52, 211, 153, .18); color: #34d399; }
.dir.error { background: rgba(244, 63, 94, .2); color: #f43f5e; }
.hex { display: block; margin-top: 8px; font: 12px/1.6 ui-monospace, "Cascadia Code", Consolas, monospace; overflow-wrap: anywhere; }
.hex-header { color: var(--muted); opacity: .7; }
.hex-body { color: var(--text); }
.log-entry details summary { margin-top: 8px; font-size: 13px; color: var(--a); cursor: pointer; }

.toast {
  position: fixed; z-index: 20; left: 50%; bottom: calc(96px + env(safe-area-inset-bottom));
  transform: translate(-50%, 140%);
  max-width: min(480px, calc(100% - 32px));
  padding: 14px 18px;
  border-radius: 16px;
  background: rgba(20, 24, 40, .92);
  border: 1px solid var(--glass-border);
  box-shadow: 0 16px 40px rgba(0, 0, 0, .5);
  font-size: 14px;
  opacity: 0;
  visibility: hidden;
  transition: transform .35s cubic-bezier(.2, .9, .3, 1.2), opacity .25s, visibility .35s;
}
.toast.show { transform: translate(-50%, 0); opacity: 1; visibility: visible; }
.toast.error { border-color: rgba(244, 63, 94, .5); }

@media (max-width: 420px) {
  .stats { grid-template-columns: 1fr 1fr; }
  .actions { grid-template-columns: 1fr; }
}

@media (max-width: 380px) {
  .mode-grid { grid-template-columns: repeat(2, 1fr); }
  .metrics, .flags { grid-template-columns: 1fr; }
  .target { font-size: 64px; }
}

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