:root {
  --bg: #f3f5f9;
  --bg-2: #e8ecf3;
  --surface: rgba(255, 255, 255, 0.72);
  --surface-solid: #ffffff;
  --border: rgba(15, 23, 42, 0.08);
  --text: #0f172a;
  --muted: #64748b;
  --chip: rgba(15, 23, 42, 0.05);
  --chip-hover: rgba(15, 23, 42, 0.09);
  --track: rgba(15, 23, 42, 0.08);
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.04), 0 12px 40px -12px rgba(15, 23, 42, 0.18);
  --accent: #2f8cff;
  --accent-2: #6fd3ff;
  --on-accent: #ffffff;
  --radius: 28px;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Inter", "Segoe UI", system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0a0d14;
    --bg-2: #111622;
    --surface: rgba(22, 27, 38, 0.72);
    --surface-solid: #161b26;
    --border: rgba(255, 255, 255, 0.07);
    --text: #eef2f8;
    --muted: #8b95a8;
    --chip: rgba(255, 255, 255, 0.05);
    --chip-hover: rgba(255, 255, 255, 0.09);
    --track: rgba(255, 255, 255, 0.08);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 20px 60px -20px rgba(0, 0, 0, 0.7);
  }
}

body[data-mode="cool"] { --accent: #2f8cff; --accent-2: #6fd3ff; }
body[data-mode="heat"] { --accent: #ff6a3d; --accent-2: #ffb547; }
body[data-mode="auto"] { --accent: #7c6cff; --accent-2: #c48bff; }
body[data-mode="dry"]  { --accent: #14b8a6; --accent-2: #7ee8d4; }
body[data-mode="fan"]  { --accent: #22a06b; --accent-2: #9be38a; }
body.off { --accent: #94a3b8; --accent-2: #cbd5e1; }

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}
[hidden] { display: none !important; }

.ambient {
  position: fixed; inset: -20%;
  background:
    radial-gradient(40% 35% at 30% 20%, color-mix(in srgb, var(--accent) 28%, transparent), transparent 70%),
    radial-gradient(35% 30% at 75% 75%, color-mix(in srgb, var(--accent-2) 22%, transparent), transparent 70%);
  filter: blur(40px);
  transition: background 0.8s ease;
  pointer-events: none;
  z-index: 0;
}

.shell {
  position: relative; z-index: 1;
  max-width: 480px;
  margin: 0 auto;
  padding: max(20px, env(safe-area-inset-top)) 16px 28px;
  display: flex; flex-direction: column; gap: 16px;
  min-height: 100vh;
}

/* Top bar */
.topbar { display: flex; align-items: center; gap: 12px; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; letter-spacing: -0.01em; }
.brand-mark {
  width: 22px; height: 22px; border-radius: 7px;
  background: linear-gradient(135deg, var(--accent-2), var(--accent));
  box-shadow: 0 4px 12px -2px color-mix(in srgb, var(--accent) 60%, transparent);
  transition: background 0.6s ease;
}
.devices {
  flex: 1; display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none;
  justify-content: flex-end;
}
.devices::-webkit-scrollbar { display: none; }
.device-pill {
  flex: none; border: 1px solid var(--border); background: var(--surface);
  backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
  color: var(--muted); font: inherit; font-size: 13px; font-weight: 550;
  padding: 7px 12px; border-radius: 999px; cursor: pointer;
  display: flex; align-items: center; gap: 6px;
}
.device-pill .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); opacity: 0.5; }
.device-pill .dot.on { background: #22c55e; opacity: 1; box-shadow: 0 0 6px #22c55e; }
.device-pill[aria-current="true"] { color: var(--text); border-color: color-mix(in srgb, var(--accent) 45%, transparent); }

.icon-btn {
  flex: none; width: 36px; height: 36px; border-radius: 50%;
  border: 1px solid var(--border); background: var(--surface); color: var(--muted);
  display: grid; place-items: center; cursor: pointer;
}
.icon-btn:hover { color: var(--text); }
.icon-btn.spin svg { animation: spin 0.8s linear infinite; }
svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn svg { width: 17px; height: 17px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Card */
.card {
  background: var(--surface);
  backdrop-filter: blur(28px) saturate(1.4); -webkit-backdrop-filter: blur(28px) saturate(1.4);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 22px;
}
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
h1 { font-size: 22px; font-weight: 680; letter-spacing: -0.02em; margin: 0; }
.sub { margin: 4px 0 0; color: var(--muted); font-size: 13px; }

.power {
  width: 52px; height: 52px; flex: none; border-radius: 50%; cursor: pointer;
  border: 1px solid var(--border); background: var(--chip); color: var(--muted);
  display: grid; place-items: center;
  transition: background 0.3s, color 0.3s, box-shadow 0.3s, transform 0.15s;
}
.power svg { width: 22px; height: 22px; stroke-width: 2.4; }
.power[aria-pressed="true"] {
  background: linear-gradient(135deg, var(--accent-2), var(--accent));
  color: var(--on-accent); border-color: transparent;
  box-shadow: 0 8px 24px -6px color-mix(in srgb, var(--accent) 70%, transparent);
}
.power:active { transform: scale(0.94); }

/* Dial */
.dial-wrap { position: relative; width: min(100%, 320px); margin: 6px auto 0; aspect-ratio: 1; }
.dial { width: 100%; height: 100%; overflow: visible; touch-action: none; cursor: grab; outline: none; }
.dial:active { cursor: grabbing; }
.dial .track { stroke: var(--track); stroke-width: 18; }
.dial .progress { stroke: url(#arcGrad); stroke-width: 18; transition: d 0.25s ease; filter: drop-shadow(0 4px 10px color-mix(in srgb, var(--accent) 45%, transparent)); }
.dial .knob { fill: var(--surface-solid); stroke: var(--accent); stroke-width: 4; filter: drop-shadow(0 2px 6px rgba(0,0,0,0.25)); transition: cx 0.25s ease, cy 0.25s ease; }
.dial .room-dot { fill: var(--text); opacity: 0.55; transition: cx 0.4s ease, cy 0.4s ease; }
.dial .tick { stroke: var(--muted); stroke-width: 1.5; opacity: 0.35; }
.dial .tick.major { opacity: 0.7; }
.dial .tick-label { fill: var(--muted); font-size: 11px; stroke: none; text-anchor: middle; dominant-baseline: middle; }
.dial:focus-visible .knob { stroke-width: 6; }
body.off .dial { cursor: default; }

.readout {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; pointer-events: none; gap: 2px;
}
.readout-label { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.08em; color: var(--accent); transition: color 0.4s; }
.target { font-size: 76px; font-weight: 300; letter-spacing: -0.05em; line-height: 1; font-variant-numeric: tabular-nums; }
.target sup { font-size: 20px; font-weight: 500; letter-spacing: 0; vertical-align: 38px; margin-left: 2px; color: var(--muted); }
body.off .target { color: var(--muted); }
.temps { display: flex; gap: 14px; margin-top: 8px; font-size: 14px; color: var(--muted); }
.temps span { display: flex; align-items: center; gap: 5px; }
.temps b { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }
.temps svg { width: 15px; height: 15px; }

.step {
  position: absolute; bottom: 0; width: 46px; height: 46px; border-radius: 50%;
  border: 1px solid var(--border); background: var(--surface-solid); color: var(--text);
  font-size: 24px; font-weight: 400; line-height: 1; cursor: pointer;
  box-shadow: var(--shadow); display: grid; place-items: center;
  transition: transform 0.15s;
}
.step:active { transform: scale(0.92); }
.step.minus { left: 14%; }
.step.plus { right: 14%; }

/* Controls */
.controls { display: flex; flex-direction: column; gap: 14px; margin-top: 14px; transition: opacity 0.3s; }
body.off .controls, body.off .step { opacity: 0.45; pointer-events: none; }
.group { display: flex; flex-direction: column; gap: 7px; min-width: 0; }
.group-label { font-size: 12px; font-weight: 600; color: var(--muted); letter-spacing: 0.02em; }
.row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.seg { display: flex; gap: 4px; padding: 4px; background: var(--chip); border-radius: 16px; }
.seg button {
  flex: 1; min-width: 0; border: 0; background: transparent; color: var(--muted);
  font: inherit; font-size: 12.5px; font-weight: 600; padding: 9px 4px; border-radius: 12px;
  cursor: pointer; display: flex; flex-direction: column; align-items: center; gap: 4px;
  transition: background 0.2s, color 0.2s, box-shadow 0.2s;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.seg button:hover { color: var(--text); background: var(--chip-hover); }
.seg button[aria-pressed="true"] {
  background: var(--surface-solid); color: var(--accent);
  box-shadow: 0 1px 2px rgba(0,0,0,0.06), 0 4px 12px -4px rgba(0,0,0,0.15);
}
.seg button svg { width: 20px; height: 20px; }
.seg-modes button { padding: 10px 4px 8px; }

.bars { display: flex; align-items: flex-end; gap: 2px; height: 14px; }
.bars i { width: 3px; border-radius: 2px; background: currentColor; opacity: 0.25; }
.bars i.lit { opacity: 1; }

/* Notice */
.notice h2 { margin: 0 0 6px; font-size: 18px; }
.notice-text { margin: 0; color: var(--muted); line-height: 1.5; font-size: 14px; }
.notice code { background: var(--chip); padding: 2px 6px; border-radius: 6px; font-size: 13px; }

.foot {
  margin-top: auto; display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  font-size: 12px; color: var(--muted); padding: 0 6px;
}
.foot a { color: inherit; }
.foot code { font-size: 11.5px; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px;
  font-size: 13px; font-weight: 550; opacity: 0; pointer-events: none;
  transition: opacity 0.25s, transform 0.25s; z-index: 10; max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.skeleton .target, .skeleton .temps b { opacity: 0.3; }

@media (min-width: 720px) {
  .shell { padding-top: 48px; }
}

.token-form { display: flex; gap: 8px; margin-top: 12px; }
.token-form input {
  flex: 1; min-width: 0; font: inherit; font-size: 15px; color: var(--text);
  background: var(--chip); border: 1px solid var(--border); border-radius: 12px; padding: 11px 14px; outline: none;
}
.token-form input:focus { border-color: var(--accent); }
.token-form button {
  font: inherit; font-weight: 600; font-size: 14px; border: 0; border-radius: 12px; padding: 0 18px; cursor: pointer;
  color: var(--on-accent); background: linear-gradient(135deg, var(--accent-2), var(--accent));
}
.token-error { display: block; margin-top: 8px; color: #ef4444; font-size: 13px; }
