:root {
  --bg: #07080d;
  --surface: rgba(255, 255, 255, 0.06);
  --surface-2: rgba(255, 255, 255, 0.10);
  --border: rgba(255, 255, 255, 0.11);
  --text: #f3f5fb;
  --muted: #98a2b8;
  --accent: #38bdf8;
  --accent-2: #a855f7;
  --ok: #34d399;
  --warn: #fbbf24;
  --danger: #f87171;
  --radius: 18px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

body[data-theme="midnight"] { --bg: #000000; --accent: #60a5fa; }
body[data-theme="ocean"] { --bg: #040a12; --accent: #22d3ee; --accent-2: #3b82f6; }
body[data-theme="sunset"] { --bg: #0d0708; --accent: #fb7185; --accent-2: #f59e0b; }
body[data-theme="forest"] { --bg: #050c08; --accent: #34d399; --accent-2: #10b981; }
body[data-theme="sakura"] { --bg: #0d070b; --accent: #f472b6; --accent-2: #c084fc; }
body[data-theme="neon"] { --bg: #05030c; --accent: #c084fc; --accent-2: #22d3ee; }
body[data-theme="gold"] { --bg: #0b0906; --accent: #fbbf24; --accent-2: #f59e0b; }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  font-size: 15px;
  line-height: 1.4;
  overflow: hidden;
}

.ambient {
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(65% 45% at 15% 5%, color-mix(in srgb, var(--accent) 22%, transparent), transparent 70%),
    radial-gradient(65% 45% at 85% 95%, color-mix(in srgb, var(--accent-2) 18%, transparent), transparent 70%);
  z-index: 0;
  transition: background 0.6s ease;
}

.app-shell {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(12px + var(--safe-top)) 16px 10px;
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.topbar-left, .topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
}

.brand-dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
  background: var(--accent);
  box-shadow: 0 0 12px var(--accent);
  transition: background 0.3s, box-shadow 0.3s;
}

.brand-dot.off {
  background: #4b5563;
  box-shadow: none;
}

.topbar-title {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.conn-badge {
  font-size: 11px;
  color: var(--ok);
}
.conn-badge.offline { color: var(--danger); }

.icon-btn {
  border: 0;
  background: var(--surface);
  color: var(--text);
  width: 36px;
  height: 36px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  cursor: pointer;
}
.icon-btn:active { transform: scale(0.94); }

.pill-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}
.sleep-pill {
  border-color: color-mix(in srgb, var(--accent) 45%, transparent);
  color: var(--accent);
}

.view {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 14px 16px 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  -webkit-overflow-scrolling: touch;
}

.tabbar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  padding: 8px 8px calc(8px + var(--safe-bottom));
  background: rgba(10, 12, 19, 0.85);
  backdrop-filter: blur(22px);
  -webkit-backdrop-filter: blur(22px);
  border-top: 1px solid var(--border);
}

.tab-btn {
  border: 0;
  background: transparent;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 6px 4px;
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  border-radius: 12px;
}
.tab-btn.active {
  color: var(--accent);
}
.tab-btn:active { background: var(--surface); }

/* Cards & layout */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.card-title {
  font-size: 15px;
  font-weight: 650;
}
.muted { color: var(--muted); font-size: 13px; }
.small { font-size: 12px; }
.row { display: flex; align-items: center; gap: 10px; }
.between { justify-content: space-between; }
.wrap { flex-wrap: wrap; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.grid-2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }

/* Master power hero */
.hero-power {
  position: relative;
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 18px;
  background: linear-gradient(135deg, color-mix(in srgb, var(--accent) 18%, transparent), rgba(255,255,255,0.04));
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  cursor: pointer;
  text-align: left;
  color: var(--text);
}
.hero-power.is-off {
  background: rgba(255,255,255,0.035);
}
.hero-power:active { transform: scale(0.985); }
.hero-power-title { font-size: 18px; font-weight: 700; }
.hero-power-sub { font-size: 13px; color: var(--muted); margin-top: 2px; }
.power-orb {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--accent);
  color: #061018;
  box-shadow: 0 0 24px color-mix(in srgb, var(--accent) 55%, transparent);
  flex-shrink: 0;
  transition: all 0.25s ease;
}
.hero-power.is-off .power-orb {
  background: rgba(255,255,255,0.12);
  color: var(--muted);
  box-shadow: none;
}

/* Buttons & switches */
.btn {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 14px;
  padding: 11px 14px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.btn:active { transform: scale(0.97); }
.btn.primary {
  background: var(--accent);
  color: #050a12;
  border-color: transparent;
}
.btn.danger {
  background: rgba(248, 113, 113, 0.16);
  color: #fca5a5;
  border-color: rgba(248, 113, 113, 0.3);
}
.btn.sm { padding: 7px 11px; font-size: 12px; border-radius: 10px; }
.btn.full { width: 100%; }

.switch {
  position: relative;
  width: 48px;
  height: 28px;
  border-radius: 999px;
  background: rgba(255,255,255,0.16);
  border: 0;
  cursor: pointer;
  transition: background 0.2s;
  flex-shrink: 0;
}
.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.2s;
}
.switch.on { background: var(--accent); }
.switch.on::after { transform: translateX(20px); }

/* Sliders */
.slider-box { display: flex; flex-direction: column; gap: 6px; }
.slider-head { display: flex; justify-content: space-between; font-size: 13px; }
.slider-val { color: var(--accent); font-weight: 600; font-variant-numeric: tabular-nums; }
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 8px;
  border-radius: 999px;
  background: rgba(255,255,255,0.14);
  outline: none;
  touch-action: pan-y;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,0.45);
  border: 2px solid var(--accent);
}

/* Color swatches & chips */
.swatch-row {
  display: flex;
  gap: 10px;
  overflow-x: auto;
  padding: 2px 0;
  scrollbar-width: none;
}
.swatch-row::-webkit-scrollbar { display: none; }
.color-dot {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.22);
  flex-shrink: 0;
  cursor: pointer;
}
.color-dot:active { transform: scale(0.9); }

.chip-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.chip {
  border: 1px solid var(--border);
  background: rgba(255,255,255,0.05);
  color: var(--text);
  padding: 8px 12px;
  border-radius: 12px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 7px;
}
.chip.active {
  background: color-mix(in srgb, var(--accent) 22%, transparent);
  border-color: var(--accent);
  color: #fff;
}
.chip-bar {
  width: 14px;
  height: 14px;
  border-radius: 999px;
  flex-shrink: 0;
}

/* Device cards */
.dev-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  cursor: pointer;
}
.dev-card:active { background: var(--surface-2); }
.dev-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.dev-name { font-weight: 650; font-size: 14px; }
.dev-status { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dev-status.err { color: var(--danger); }
.dev-strip {
  height: 6px;
  border-radius: 999px;
  background: rgba(255,255,255,0.1);
}

/* ARM 36-LED live bar */
.led-bar {
  display: grid;
  grid-template-columns: repeat(36, 1fr);
  gap: 2px;
  padding: 8px;
  border-radius: 12px;
  background: rgba(0,0,0,0.45);
  border: 1px solid rgba(255,255,255,0.08);
}
.led-cell {
  height: 18px;
  border-radius: 3px;
  background: #222;
}

/* Scenes */
.scene-card {
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 13px;
  background: var(--surface);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  cursor: pointer;
}
.scene-card.active {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 16%, rgba(255,255,255,0.04));
}
.scene-swatch {
  width: 34px;
  height: 34px;
  border-radius: 11px;
  flex-shrink: 0;
  border: 1px solid rgba(255,255,255,0.18);
}

/* Problems banner */
.problem-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 14px;
  background: rgba(248, 113, 113, 0.12);
  border: 1px solid rgba(248, 113, 113, 0.25);
}

/* Bottom sheet */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(0,0,0,0.62);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.sheet-backdrop[hidden] { display: none; }
.sheet {
  width: 100%;
  max-width: 560px;
  max-height: 82dvh;
  background: #10131e;
  border-top: 1px solid var(--border);
  border-radius: 22px 22px 0 0;
  padding: 10px 16px calc(18px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.sheet-handle {
  width: 38px;
  height: 4px;
  border-radius: 999px;
  background: rgba(255,255,255,0.22);
  margin: 0 auto;
}
.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.sheet-title { font-size: 16px; font-weight: 700; }
.sheet-body { overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }

/* PIN overlay */
.pin-overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(7, 8, 13, 0.96);
  display: grid;
  place-items: center;
  padding: 20px;
}
.pin-overlay[hidden] { display: none; }
.pin-card {
  width: 100%;
  max-width: 340px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 22px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  text-align: center;
}
.pin-logo { font-size: 24px; font-weight: 800; color: var(--accent); }
.pin-sub { font-size: 13px; color: var(--muted); }
.pin-input, .text-input {
  width: 100%;
  background: rgba(0,0,0,0.35);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px 14px;
  color: var(--text);
  font-size: 16px;
  outline: none;
}
.pin-input { text-align: center; letter-spacing: 0.2em; }
.pin-err { color: var(--danger); font-size: 13px; min-height: 18px; }

/* Toast */
.toast-host {
  position: fixed;
  left: 50%;
  bottom: calc(74px + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
  width: calc(100% - 32px);
  max-width: 420px;
}
.toast {
  background: rgba(20, 24, 36, 0.95);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 11px 14px;
  border-radius: 14px;
  font-size: 13px;
  text-align: center;
  box-shadow: 0 8px 24px rgba(0,0,0,0.45);
  pointer-events: auto;
}
.toast.warn { border-color: rgba(251, 191, 36, 0.45); color: #fde68a; }
