* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  color-scheme: dark;
  --bg: #0e1016;
  --card: #171a23;
  --raised: #2a2e3b;
  --line: rgb(255 255 255 / 8%);
  --line-strong: rgb(255 255 255 / 16%);
  --text: #f3f4f8;
  --muted: #8d93a5;
  --accent: #ffb54d;
  --accent-hover: #ffc575;
  --accent-ink: #241703;
  --accent-text: #ffd9a3;
  --danger: 255 93 93;
  --danger-text: #ffb4b4;
  --online: #4cd07d;
  --offline: #ff5d5d;
  --pending: #ffc94d;
  --pad: clamp(16px, 3vmin, 32px);
  --stack: clamp(12px, 2.6vh, 28px);
  --gap: clamp(8px, 1.2vmin, 12px);
  --radius-lg: clamp(18px, 2.6vmin, 28px);
  --radius: 14px;
  --radius-sm: 10px;
  --tap: 44px;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  font:
    16px/1.4 -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    sans-serif;
  color: var(--text);
  background: var(--bg);
}

button {
  font: inherit;
  color: inherit;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  cursor: pointer;
}

input {
  font: inherit;
  color: inherit;
}

button:disabled,
input:disabled {
  cursor: not-allowed;
}

[hidden] {
  display: none !important;
}

:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 3px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

/* Page */

.panel {
  min-height: 100dvh;
  padding: max(var(--pad), env(safe-area-inset-top))
    max(var(--pad), env(safe-area-inset-right))
    max(var(--pad), env(safe-area-inset-bottom))
    max(var(--pad), env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  align-items: center;
}

.connecting {
  margin: auto;
  color: var(--muted);
}

/* Signed in: the controller fills the screen above the room selector */

.panel:has(.controller:not([hidden])) {
  height: 100dvh;
  padding: 0;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  align-items: stretch;
}

.controller {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: var(--stack);
  width: min(100%, 680px);
  margin-inline: auto;
  min-height: 0;
  padding: max(var(--pad), env(safe-area-inset-top))
    max(var(--pad), env(safe-area-inset-right)) var(--pad)
    max(var(--pad), env(safe-area-inset-left));
}

.panel:has(.rooms[hidden]) .controller {
  padding-bottom: max(var(--pad), env(safe-area-inset-bottom));
}

/* Top row: room title and device status */

.status-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 28px;
}

.room-title {
  min-width: 0;
  overflow: hidden;
  font-size: clamp(1.2rem, 3vmin, 1.75rem);
  font-weight: 650;
  letter-spacing: -0.01em;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 0 0 auto;
}

.status-note {
  font-size: 14px;
  color: var(--muted);
}

.dots {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--line-strong);
  transition: background 0.3s ease;
}

.dot.on {
  background: var(--online);
  box-shadow: 0 0 8px rgb(76 208 125 / 55%);
}

.dot.off {
  background: transparent;
  box-shadow: inset 0 0 0 2px var(--muted);
}

.dot.offline {
  background: var(--offline);
}

.dot.pending {
  background: var(--pending);
  animation: pulse 1s ease-in-out infinite;
}

@keyframes pulse {
  50% {
    opacity: 0.45;
  }
}

/* Controls: power circle, then the remaining space split into equal bands */

.controls {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: var(--stack);
  min-height: 0;
}

.controls.disabled .band {
  opacity: 0.4;
}

.power-area {
  position: relative;
  z-index: 0;
  display: flex;
  justify-content: center;
  flex: 0 0 auto;
}

.power {
  --glow: rgb(255 222 186);
  --glow-ink: var(--accent-ink);
  --level: 1;
  display: grid;
  place-items: center;
  width: min(100%, 30dvh);
  aspect-ratio: 1;
  border: 2px solid var(--line-strong);
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  transition:
    background 0.4s ease,
    color 0.4s ease,
    border-color 0.4s ease,
    box-shadow 0.4s ease,
    transform 0.15s ease;
}

.power svg {
  width: 32%;
  height: 32%;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
}

.power:active:not(:disabled) {
  transform: scale(0.97);
}

.power.on {
  border-color: transparent;
  background: var(--glow);
  color: var(--glow-ink);
  box-shadow:
    0 0 0 calc(var(--level) * 8px)
      color-mix(in srgb, var(--glow) 12%, transparent),
    0 0 calc(var(--level) * 36px)
      color-mix(in srgb, var(--glow) 30%, transparent);
}

.power.busy {
  animation: pulse 1.2s ease-in-out infinite;
}

.power:focus-visible {
  outline-offset: 6px;
}

.band {
  position: relative;
  z-index: 1;
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
}

.band-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  padding: 0 4px;
}

.band-name {
  font-size: 13px;
  font-weight: 650;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--muted);
}

.band-value {
  font-size: 14px;
  font-weight: 600;
  color: var(--accent-text);
  font-variant-numeric: tabular-nums;
}

.controls.off .band {
  opacity: 0.45;
  filter: saturate(0.35);
}

/* Brightness */

#brightness-wrap {
  --track: linear-gradient(to right, #4a3419, #ffb54d 55%, #ffe9c4);
}

.slider-end.dim {
  left: calc(var(--inset) * 2.5);
  color: rgb(255 220 170 / 85%);
}

.slider-end.bright {
  right: calc(var(--inset) * 2.5);
  color: rgb(70 40 0 / 65%);
}

/* Temperature */

.slider-wrap {
  --pct: 50%;
  --inset: clamp(6px, 1.2vmin, 10px);
  --thumb: clamp(10px, 1.4vmin, 16px);
  position: relative;
  flex: 1;
  border-radius: var(--radius-lg);
  background: var(--track);
  touch-action: none;
  transition:
    opacity 0.3s ease,
    filter 0.3s ease;
}

.slider {
  position: absolute;
  inset: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  background: transparent;
  border-radius: inherit;
  -webkit-appearance: none;
  appearance: none;
  touch-action: none;
  cursor: pointer;
}

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 1px;
  height: 100%;
  background: transparent;
}

.slider::-moz-range-thumb {
  width: 1px;
  height: 100%;
  border: none;
  background: transparent;
}

.slider::-moz-range-track {
  background: transparent;
}

.slider:focus-visible {
  outline: none;
}

.slider-wrap:has(.slider:focus-visible) {
  outline: 3px solid var(--accent);
  outline-offset: 3px;
}

.slider-end {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  font-size: clamp(0.8rem, 1.8vmin, 1rem);
  font-weight: 600;
  pointer-events: none;
}

.slider-end.warm {
  left: calc(var(--inset) * 2.5);
  color: rgb(70 30 0 / 70%);
}

.slider-end.cool {
  right: calc(var(--inset) * 2.5);
  color: rgb(30 40 70 / 60%);
}

.slider-thumb {
  position: absolute;
  top: var(--inset);
  bottom: var(--inset);
  left: clamp(
    calc(var(--inset) + var(--thumb) / 2),
    var(--pct),
    calc(100% - var(--inset) - var(--thumb) / 2)
  );
  width: var(--thumb);
  transform: translateX(-50%);
  border-radius: 999px;
  background: #fff;
  box-shadow: 0 2px 12px rgb(0 0 0 / 35%);
  pointer-events: none;
}

.slider-wrap:has(.slider:disabled),
.temperature.inactive .slider-wrap {
  opacity: 0.4;
  filter: saturate(0.3);
}

.slider:disabled ~ .slider-thumb {
  display: none;
}

/* Color: circles as large as the band allows */

.swatches {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: var(--gap);
  container-type: size;
}

.swatch-cell {
  position: relative;
  place-self: center;
  width: min(100cqh, (100cqw - var(--gap) * 5) / 6);
  aspect-ratio: 1;
}

.swatch {
  display: block;
  width: 100%;
  height: 100%;
  border: 0;
  border-radius: 50%;
  background: var(--swatch);
  box-shadow: inset 0 0 0 1px rgb(255 255 255 / 18%);
  transition:
    transform 0.15s ease,
    box-shadow 0.15s ease;
}

.swatch:active:not(:disabled) {
  transform: scale(0.95);
}

.swatch[aria-pressed="true"] {
  box-shadow:
    0 0 0 3px var(--bg),
    0 0 0 6px #fff;
}

.controls.off .swatch[aria-pressed="true"] {
  box-shadow:
    0 0 0 3px var(--bg),
    0 0 0 5px var(--line-strong);
}

.swatch-edit {
  position: absolute;
  right: -4%;
  bottom: -4%;
  display: grid;
  place-items: center;
  width: clamp(26px, 38%, 40px);
  aspect-ratio: 1;
  padding: 0;
  border: 2px solid var(--bg);
  border-radius: 50%;
  background: var(--raised);
}

.swatch-edit::before {
  content: "";
  position: absolute;
  inset: -8px;
}

.swatch-edit svg {
  width: 50%;
  height: 50%;
  fill: none;
  stroke: #fff;
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Error toast */

.notice {
  position: absolute;
  left: 50%;
  bottom: var(--pad);
  z-index: 2;
  width: min(100% - var(--pad) * 2, 560px);
  transform: translateX(-50%);
  padding: 12px 16px;
  border: 1px solid rgb(var(--danger) / 45%);
  border-radius: var(--radius-sm);
  background: #2a1517;
  box-shadow: 0 8px 24px rgb(0 0 0 / 45%);
  color: var(--danger-text);
  font-size: 14px;
  line-height: 1.5;
  overflow-wrap: anywhere;
}

/* Room selector: a T split along the bottom edge */

.rooms {
  border-top: 1px solid var(--line-strong);
  padding-bottom: env(safe-area-inset-bottom);
}

.room-list {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(0, 1fr);
  max-width: 680px;
  margin-inline: auto;
}

.room-btn {
  height: clamp(56px, 9vh, 88px);
  padding: 0 16px;
  overflow: hidden;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: clamp(1rem, 2.4vmin, 1.3rem);
  font-weight: 600;
  text-overflow: ellipsis;
  white-space: nowrap;
  transition:
    background 0.15s ease,
    color 0.15s ease;
}

.room-btn + .room-btn {
  border-left: 1px solid var(--line-strong);
}

.room-btn[aria-current="true"] {
  background: rgb(255 255 255 / 5%);
  color: var(--text);
}

.room-btn:focus-visible {
  outline-offset: -4px;
}

/* Sign in */

.login-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 20px;
  width: min(100%, 400px);
  padding-block: 24px;
}

.login-icon {
  width: 56px;
  height: 56px;
  fill: none;
  stroke: var(--accent);
  stroke-width: 1.5;
}

.login-section h2 {
  font-size: 28px;
  line-height: 1.2;
  letter-spacing: -0.02em;
}

.login {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
  padding: 24px;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  background: var(--card);
}

.login label {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--muted);
}

.login input {
  width: 100%;
  height: 48px;
  padding: 0 14px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--bg);
}

.login-message {
  font-size: 14px;
  color: var(--muted);
}

.login-message.error {
  color: var(--danger-text);
}

.primary,
.secondary {
  min-height: var(--tap);
  padding: 0 20px;
  border-radius: var(--radius-sm);
  font-weight: 650;
}

.primary {
  border: 1px solid var(--accent);
  background: var(--accent);
  color: var(--accent-ink);
}

.primary:disabled {
  opacity: 0.6;
}

.secondary {
  border: 1px solid var(--line-strong);
  background: transparent;
}

.text-button {
  min-height: var(--tap);
  padding: 0 12px;
  border: 0;
  background: transparent;
  color: var(--accent-text);
  font-weight: 600;
}

@media (hover: hover) {
  .room-btn:hover:not([aria-current="true"]) {
    color: var(--text);
  }

  .primary:hover:not(:disabled) {
    border-color: var(--accent-hover);
    background: var(--accent-hover);
  }
}

/* Custom color picker */

dialog {
  width: min(400px, calc(100vw - 32px));
  margin: auto;
  padding: 24px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-lg);
  background: var(--card);
  color: var(--text);
}

dialog::backdrop {
  background: rgb(0 0 0 / 65%);
}

dialog h2 {
  margin-bottom: 18px;
  font-size: 20px;
}

.color-grid {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 10px;
}

.pick {
  aspect-ratio: 1;
  border: 0;
  border-radius: 50%;
  background: var(--swatch);
  box-shadow: inset 0 0 0 1px rgb(255 255 255 / 18%);
  transition: transform 0.12s ease;
}

.pick:active {
  transform: scale(0.92);
}

.pick[aria-pressed="true"] {
  box-shadow:
    0 0 0 3px var(--card),
    0 0 0 5px #fff;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
