:root {
  --bg-primary: #08090d;
  --bg-card: #10121a;
  --bg-card-off: #0b0d13;
  --bg-card-on: #191d29;
  --border-card: rgba(255, 255, 255, 0.06);
  --border-on: rgba(255, 184, 48, 0.45);
  --border-selected: #ffb830;
  --text-primary: #f2f4f8;
  --text-secondary: #7f8698;
  --text-muted: #4a5162;
  --accent-gold: #ffb830;
  --accent-gold-glow: rgba(255, 184, 48, 0.28);
  --accent-cyan: #38bdf8;
  --accent-red: #f43f5e;
  --accent-green: #10b981;
  --radius-tile: 10px;
  --radius-pill: 999px;
  --shadow-card: 0 3px 12px rgba(0, 0, 0, 0.35);
  --shadow-on: 0 3px 14px rgba(255, 184, 48, 0.12), 0 2px 5px rgba(0, 0, 0, 0.4);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}

html, body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI", Roboto, sans-serif;
  background-color: var(--bg-primary);
  color: var(--text-primary);
  height: 100vh;
  height: 100dvh;
  margin: 0;
  padding: 0;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
}

/* Master Layout */
.app-layout {
  display: flex;
  height: 100vh;
  height: 100dvh;
  position: relative;
  max-width: 440px;
  margin: 0 auto;
}

/* Main Scrollable Content Area (offset for right static rail; scrolls only if viewport is shorter than content) */
.main-content {
  flex: 1;
  height: 100vh;
  height: 100dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  margin-right: calc(48px + env(safe-area-inset-right, 0px));
  padding: max(12px, env(safe-area-inset-top, 0px)) 8px max(18px, env(safe-area-inset-bottom, 0px)) max(10px, env(safe-area-inset-left, 0px));
  min-width: 0;
}

/* Fixed Static Right Rail (Non-scrollable shortcuts) */
.static-sidebar {
  position: fixed;
  right: 0;
  top: 0;
  bottom: 0;
  width: calc(48px + env(safe-area-inset-right, 0px));
  background: rgba(13, 15, 22, 0.82);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-left: 1px solid var(--border-card);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-evenly;
  padding: max(12px, env(safe-area-inset-top, 0px)) env(safe-area-inset-right, 0px) max(12px, env(safe-area-inset-bottom, 0px)) 0;
  z-index: 100;
}

@media (min-width: 441px) {
  .static-sidebar {
    right: calc((100vw - 440px) / 2);
  }
}

.sidebar-item {
  width: 36px;
  height: 36px;
  border-radius: 9px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.05);
  color: inherit;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  cursor: pointer;
  transition: all 0.15s;
}

.sidebar-item:active,
.sidebar-item.is-active {
  transform: scale(0.9);
  background: rgba(255, 184, 48, 0.22);
  border-color: rgba(255, 184, 48, 0.6);
}

/* Scaled Down Header */
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 2px 10px;
}

.brand h1 {
  font-size: 19px;
  font-weight: 700;
  letter-spacing: -0.3px;
}

.brand p {
  font-size: 10.5px;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: 4px;
  margin-top: 1px;
}

.status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background-color: var(--accent-green);
  display: inline-block;
}

.status-dot.mock {
  background-color: var(--accent-gold);
}

.status-dot.offline {
  background-color: var(--accent-red);
}

.header-actions {
  display: flex;
  gap: 6px;
}

.btn-pill {
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid var(--border-card);
  color: var(--text-primary);
  padding: 6px 12px;
  border-radius: var(--radius-pill);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}

.btn-pill:active {
  background: rgba(255, 255, 255, 0.2);
  transform: scale(0.95);
}

/* Subtle Section Dividers */
.section-divider {
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.07), transparent);
  margin: 12px 2px 10px;
}

/* Room Block Container */
.room-block {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.room-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 2px 2px;
}

.room-title {
  font-size: 10.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.7px;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: 5px;
}

/* Enlarged Living Room Off Button */
.room-off-btn.enlarged {
  font-size: 11.5px;
  font-weight: 700;
  color: #ff6b81;
  background: rgba(244, 63, 94, 0.12);
  border: 1px solid rgba(244, 63, 94, 0.35);
  border-radius: var(--radius-pill);
  padding: 5px 13px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  transition: all 0.15s;
  letter-spacing: 0.3px;
}

.room-off-btn.enlarged:active {
  background: rgba(244, 63, 94, 0.25);
  transform: scale(0.95);
}

/* Centered Column Headers Grid */
.column-headers-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  padding: 0 1px 1px;
}

.column-headers-grid.two-cols {
  grid-template-columns: repeat(2, 1fr);
}

.col-header {
  text-align: center;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 3px;
}

/* 3-in-a-Row Grid */
.tiles-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.tiles-grid.two-cols {
  grid-template-columns: repeat(2, 1fr);
}

/* Split Tile Component (Scaled Down) */
.tile {
  background: var(--bg-card-off);
  border: 1px solid var(--border-card);
  border-radius: var(--radius-tile);
  box-shadow: var(--shadow-card);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
  transition: border-color 0.15s, box-shadow 0.15s, background 0.15s;
}

.tile.is-on {
  background: var(--bg-card-on);
  border-color: var(--border-on);
  box-shadow: var(--shadow-on);
}

.tile.is-selected {
  outline: 2px solid var(--border-selected);
  outline-offset: 1px;
  box-shadow: 0 0 14px rgba(255, 184, 48, 0.35);
}

.tile.is-offline {
  opacity: 0.45;
  filter: grayscale(0.6);
}

/* Tile Top Zone: Selection & Status */
.tile-top {
  padding: 8px 3px 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  cursor: pointer;
  min-height: 56px;
  transition: background 0.12s;
}

.tile-top:active {
  background: rgba(255, 255, 255, 0.04);
}

.tile-icon {
  font-size: 18px;
  line-height: 1;
  margin-bottom: 3px;
  filter: grayscale(0.8);
  opacity: 0.65;
  transition: transform 0.15s, filter 0.15s, opacity 0.15s;
}

.tile.is-on .tile-icon {
  filter: grayscale(0) drop-shadow(0 2px 5px rgba(255, 184, 48, 0.3));
  opacity: 1;
  transform: scale(1.08);
}

.tile-name {
  font-size: 11px;
  font-weight: 500;
  line-height: 1.15;
  color: #8b92a4;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.tile.is-on .tile-name {
  color: #ffffff;
  font-weight: 700;
}

.tile-sub {
  font-size: 9.5px;
  color: var(--text-muted);
  margin-top: 2px;
  font-weight: 500;
}

.tile.is-on .tile-sub {
  color: var(--accent-gold);
  font-weight: 700;
}

/* Tile Divider */
.tile-divider {
  height: 1px;
  background: rgba(255, 255, 255, 0.05);
  width: 100%;
}

.tile.is-on .tile-divider {
  background: rgba(255, 184, 48, 0.18);
}

/* Tile Bottom Zone: Power Trigger */
.tile-bottom {
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  background: rgba(255, 255, 255, 0.015);
  transition: background 0.12s;
  position: relative;
}

.tile.is-offline .tile-bottom {
  pointer-events: none;
  cursor: default;
}

.tile-bottom:active {
  background: rgba(255, 255, 255, 0.08);
}

.tile-toggle-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-size: 10px;
  font-weight: 700;
  padding: 4px 11px;
  border-radius: var(--radius-pill);
  background: rgba(255, 255, 255, 0.04);
  color: #636b7e;
  border: 1px solid rgba(255, 255, 255, 0.05);
  transition: all 0.15s;
  letter-spacing: 0.3px;
}

.tile.is-on .tile-toggle-pill {
  background: var(--accent-gold);
  color: #0b0c10;
  font-weight: 800;
  border-color: var(--accent-gold);
  box-shadow: 0 0 10px var(--accent-gold-glow);
}

/* In-Flight Spinner */
.tile.is-loading .tile-bottom {
  pointer-events: none;
}

.tile.is-loading .tile-toggle-pill {
  visibility: hidden;
}

.tile.is-loading .tile-bottom::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -6px;
  margin-left: -6px;
  width: 12px;
  height: 12px;
  border: 2px solid rgba(255, 255, 255, 0.25);
  border-top-color: #ffffff;
  border-radius: 50%;
  animation: spin 0.5s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Minimalist Contextual Control Tray */
.control-tray {
  background: var(--bg-card);
  border: 1px solid var(--border-card);
  border-radius: var(--radius-tile);
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* Circadian Preset Buttons (Day, Evening, Night) */
.circadian-presets {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 5px;
}

.preset-btn {
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 8px;
  padding: 6px 2px;
  color: var(--text-primary);
  font-size: 10px;
  font-weight: 600;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  cursor: pointer;
  transition: all 0.15s;
}

.preset-btn:active {
  transform: scale(0.96);
  background: rgba(255, 255, 255, 0.15);
}

.preset-btn.btn-day {
  border-left: 2.5px solid var(--accent-cyan);
}

.preset-btn.btn-eve {
  border-left: 2.5px solid var(--accent-gold);
}

.preset-btn.btn-night {
  border-left: 2.5px solid var(--accent-red);
}

.preset-btn span.emoji {
  font-size: 13px;
}

.preset-btn span.label {
  font-size: 9.5px;
  color: #ced4e0;
}

/* Clean Dimmer Group */
.dimmer-group {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 2px 0;
}

.dimmer-val {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-primary);
  min-width: 34px;
  text-align: right;
  flex-shrink: 0;
}

.dimmer-slider {
  -webkit-appearance: none;
  appearance: none;
  touch-action: none;
  flex: 1;
  height: 6px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.12);
  outline: none;
}

.dimmer-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #ffffff;
  box-shadow: 0 1px 4px rgba(0,0,0,0.45);
  cursor: pointer;
}

.dimmer-slider::-moz-range-thumb {
  width: 20px;
  height: 20px;
  border: none;
  border-radius: 50%;
  background: #ffffff;
  box-shadow: 0 1px 4px rgba(0,0,0,0.45);
  cursor: pointer;
}

/* Living Room Color Swatches */
.color-swatches {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 2px 2px 0;
}

.color-dot {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid transparent;
  padding: 0;
  transition: transform 0.15s;
}

.color-dot:active {
  transform: scale(1.18);
}

/* Circadian Follow Header Pill */
.btn-follow {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: rgba(255, 255, 255, 0.06);
  border-color: rgba(255, 255, 255, 0.12);
  color: var(--text-secondary);
}

.follow-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #64748b;
  transition: background-color 0.2s, box-shadow 0.2s;
}

.btn-follow.is-active {
  color: #ffffff;
}

.btn-follow.is-active.phase-day {
  background: rgba(56, 189, 248, 0.16);
  border-color: rgba(56, 189, 248, 0.55);
}
.btn-follow.is-active.phase-day .follow-dot {
  background: var(--accent-cyan);
  box-shadow: 0 0 8px var(--accent-cyan);
}

.btn-follow.is-active.phase-sunset {
  background: rgba(255, 184, 48, 0.18);
  border-color: rgba(255, 184, 48, 0.6);
}
.btn-follow.is-active.phase-sunset .follow-dot {
  background: var(--accent-gold);
  box-shadow: 0 0 8px var(--accent-gold);
}

.btn-follow.is-active.phase-wind_down,
.btn-follow.is-active.phase-night {
  background: rgba(244, 63, 94, 0.18);
  border-color: rgba(244, 63, 94, 0.6);
}
.btn-follow.is-active.phase-wind_down .follow-dot,
.btn-follow.is-active.phase-night .follow-dot {
  background: var(--accent-red);
  box-shadow: 0 0 8px var(--accent-red);
}

/* Per-Tile Loop Micro Badge */
.tile-loop-badge {
  position: absolute;
  top: 4px;
  right: 4px;
  font-size: 9px;
  line-height: 1;
  padding: 2px 4px;
  border-radius: 6px;
  pointer-events: none;
  display: none;
  font-weight: 700;
}

.tile.show-follow-badge .tile-loop-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.tile.in-loop .tile-loop-badge {
  color: var(--accent-cyan);
  background: rgba(56, 189, 248, 0.14);
}

.tile.is-manual-override:not(.is-on):not(.is-offline) {
  border-color: rgba(244, 63, 94, 0.45);
}

.tile.is-manual-override .tile-loop-badge {
  color: #ff8fa3;
  background: rgba(244, 63, 94, 0.22);
}

/* Inline Add-to-Loop Rejoin Pill inside Dimmer Group */
.btn-rejoin-loop {
  display: none;
  align-items: center;
  gap: 3px;
  background: rgba(56, 189, 248, 0.16);
  border: 1px solid rgba(56, 189, 248, 0.45);
  color: #7dd3fc;
  font-size: 10px;
  font-weight: 700;
  padding: 4px 9px;
  border-radius: var(--radius-pill);
  cursor: pointer;
  flex-shrink: 0;
  transition: all 0.15s;
}

.btn-rejoin-loop.is-visible {
  display: inline-flex;
}

.btn-rejoin-loop:active {
  transform: scale(0.94);
  background: rgba(56, 189, 248, 0.3);
}
