/* Demo Home — iPhone layer. Loaded last. EVERY rule is inside a phone-only
   media query, so iPad (>=744px tall/768px wide) and the TV never match.
   Portrait phone:  (max-width: 480px)
   Landscape phone: (orientation: landscape) and (max-height: 480px) and (max-width: 932px) */

/* ================= PHONE PORTRAIT ================= */
@media (max-width: 480px) {
  /* --- Header: row 1 = back/drawer · title · gear; row 2 = season badge · clock --- */
  header.hero {
    display: flex; flex-wrap: wrap; align-items: center;
    gap: 8px 10px;
    padding: calc(8px + env(safe-area-inset-top, 0px)) 14px 6px;
  }
  .hero-title, .hero-right { display: contents; }
  header.hero .drawer-toggle { order: 1; width: 42px; height: 42px; min-width: 42px; min-height: 42px; margin: 0; }
  header.hero h2, .hero-title h2 {
    order: 2; flex: 1 1 0; min-width: 0;
    font-size: 27px; line-height: 1.05;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }
  .hero-right .settings-btn { order: 3; flex: none; width: 44px; height: 44px; min-width: 44px; }
  header.hero::after { content: ""; order: 4; flex: 0 0 100%; height: 0; }
  header.hero .season-badge { order: 5; font-size: 9.5px; padding: 4px 9px; letter-spacing: .07em; }
  header.hero .clock, .hero-right .clock {
    order: 6; flex: 1 1 auto; min-width: 0; margin-left: auto;
    font-size: 11px; text-align: right; padding: 0; line-height: 1.3;
  }
  header.hero .chips { order: 7; flex: 0 0 100%; gap: 6px; min-height: 0; }
  header.hero .alert-rail { order: 8; flex: 0 0 100%; }

  /* --- Status chips: wrap, comfortably tappable --- */
  .chip, button.chip { min-height: 38px; padding: 7px 12px; font-size: 12px; max-width: 100%; }
  .chip.reef, .chip.lights-chip { padding: 7px 11px; white-space: normal; flex: 0 1 auto; }
  .chips > * { max-width: 100%; }

  /* --- Alert banner: text full width, actions in an even row of big buttons --- */
  .alert-banner { padding: 10px 12px; font-size: 13px; line-height: 1.35; border-radius: 14px; gap: 8px; }
  .alert-banner .txt { flex: 1 1 100%; min-width: 0; }
  .alert-banner .acts { flex: 1 1 100%; gap: 6px; }
  .alert-banner .acts .btn { flex: 1 1 0; min-width: 0; height: 44px; min-height: 44px; padding: 0 8px; font-size: 12px; white-space: nowrap; }

  /* --- Page padding, single column everywhere --- */
  .panel { padding: 6px 12px calc(16px + env(safe-area-inset-bottom, 0px)); }
  .card { padding: 14px 14px; border-radius: 18px; }
  .cards { gap: 10px; }
  .grid-overview, .grid-2, .grid-3 { grid-template-columns: minmax(0, 1fr) !important; }
  .grid-overview > .card, .grid-overview > .card.metric { grid-column: 1 / -1; }
  .ov-wrap { gap: 14px; }
  .ov-grid2, .ov-grid3, .ov-cam { grid-template-columns: minmax(0, 1fr); }
  .ov-health .health-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .ov-health .reach-list { grid-template-columns: minmax(0, 1fr); }
  .reef-hero, .outlets { grid-template-columns: minmax(0, 1fr); }
  .energy-now { font-size: 38px; }

  /* Tap targets */
  button.btn, .btn { min-height: 44px; }
  .icon-btn { min-width: 44px; min-height: 44px; }
  .toggle { min-height: 36px; }
  .lp-tool { min-height: 36px; padding-left: 12px; padding-right: 12px; }
  .lp-tools { gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
  .lp-head { flex-wrap: wrap; row-gap: 6px; }
  .quad-open { width: 32px; height: 32px; }

  /* --- Dashboards (quad) row: 5 tiny tiles -> 3 + 2 big tiles --- */
  .quad-row { grid-template-columns: repeat(6, minmax(0, 1fr)) !important; gap: 8px; }
  .quad-row > .quad-tile { grid-column: span 2; height: 116px; }
  .quad-row > .quad-tile:nth-child(4):nth-last-child(2),
  .quad-row > .quad-tile:nth-child(5):nth-last-child(1),
  .quad-row > .quad-tile:first-child:nth-last-child(4),
  .quad-row > .quad-tile:first-child:nth-last-child(4) ~ .quad-tile,
  .quad-row > .quad-tile:first-child:nth-last-child(2),
  .quad-row > .quad-tile:first-child:nth-last-child(2) ~ .quad-tile { grid-column: span 3; }
  .quad-row > .quad-tile:first-child:nth-last-child(1) { grid-column: 1 / -1; }
  .quad-name { font-size: 12.5px; }

  /* --- Living/Bedroom TV hero: 2x2 -> stacked full-width cards so labels fit --- */
  .th-row, .th-row.th-2x2 { grid-template-columns: minmax(0, 1fr) !important; gap: 10px; }
  .th-row .th-card, .th-row.th-2x2 .th-card { height: 150px; }
  .tv-hero .th-head .model { font-size: 21px; }
  .tv-hero .th-head .model small { display: block; margin: 2px 0 0; }
  .lt-grid { gap: 8px; }
  .lt { min-height: 44px; }

  /* --- Xbox overview tile --- */
  .xbox-tile .art { width: 88px; height: 88px; flex: none; }
  .xbox-tile .x-title { font-size: 22px; line-height: 1.1; }
  .xbox-tile .hero { gap: 12px; min-height: 0; padding: 0; display: flex; flex-wrap: nowrap; }
  .xbox-tile .hero > :not(.art) { min-width: 0; }

  /* --- Office TV strip --- */
  .office-tv-strip { gap: 10px; }
  .office-tv-strip .otv-glyph { width: 52px; height: 52px; flex: none; }
  .office-tv-strip .otv-offplate { width: 52px; height: 52px; }
  .office-tv-strip .otv-cap { display: none; }
  .office-tv-strip .otv-st { flex: 1 1 0; min-width: 0; }

  /* --- Light/scene wells --- */
  .lp-wells { grid-template-columns: repeat(auto-fit, minmax(96px, 1fr)); }
  .lp-ico { min-width: 40px; min-height: 40px; }

  /* --- Sonos now-playing card --- */
  .card.nowplaying .transport { flex-wrap: wrap; gap: 8px; }
  .card.nowplaying .sonos-tv-row { flex-wrap: wrap; gap: 8px; }
  .rr-grid { gap: 8px; }

  /* --- Movie Night overlay posters --- */
  .mn-grid { grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)) !important; gap: 8px; }

  /* --- Bottom sheets / overlays: full width on phone --- */
  .lights-overlay .lights-sheet,
  .xbox-overlay .xbox-sheet { width: calc(100% - 8px) !important; }

  /* --- Toast & now bar --- */
  .toast { left: 12px; right: 12px; }
  .nowbar { left: 8px; right: 8px; padding: 4px 12px 8px; }

  /* Belt & braces: never scroll sideways */
  .stage, .viewport, .panel { overflow-x: hidden; }
  .panel img, .panel video, .panel svg { max-width: 100%; }
}

/* ---------- very narrow phones (iPhone SE / mini) ---------- */
@media (max-width: 380px) {
  header.hero h2, .hero-title h2 { font-size: 24px; }
  .chip, button.chip { font-size: 11.5px; padding: 7px 10px; }
  .alert-banner .acts .btn { font-size: 11px; padding: 0 6px; }
}

/* ================= PHONE LANDSCAPE ================= */
@media (orientation: landscape) and (max-height: 480px) and (max-width: 932px) {
  /* Compact one-line header so content gets the screen. */
  header.hero {
    padding: 6px calc(14px + env(safe-area-inset-right, 0px)) 4px calc(14px + env(safe-area-inset-left, 0px));
    gap: 4px 12px;
    align-items: center;
  }
  header.hero h2 { font-size: 22px; }
  .hero-title { gap: 10px; }
  header.hero .season-badge { font-size: 9.5px; padding: 3px 8px; }
  .hero-right { gap: 8px; }
  .hero-right .clock { font-size: 11px; }
  .hero-right .settings-btn { width: 38px; height: 38px; min-width: 38px; }
  /* Chips: wrap but tight */
  .chips { gap: 6px; min-height: 0; }
  .chip, button.chip { padding: 5px 10px; font-size: 11px; min-height: 32px; }
  /* Alert banner: one line, compact actions */
  .alert-banner { padding: 6px 10px; font-size: 12.5px; align-items: center; flex-wrap: nowrap; border-radius: 12px; }
  .alert-banner .txt { min-width: 0; }
  .alert-banner .acts { flex-wrap: nowrap; flex: none; }
  .alert-banner .acts .btn { height: 34px; min-height: 34px; padding: 0 10px; font-size: 11px; }

  /* Shorter bottom tab bar */
  .tabbar { padding: 3px calc(8px + env(safe-area-inset-right, 0px)) calc(3px + env(safe-area-inset-bottom, 0px)) calc(8px + env(safe-area-inset-left, 0px)); }
  .tabbar button { min-height: 40px; font-size: 10.5px; }
  .tab-pill { top: 3px; height: 40px; }
  .nowbar { bottom: calc(48px + 8px); }

  /* Content: side safe areas, two columns where cards are small */
  .panel { padding: 6px calc(14px + env(safe-area-inset-right, 0px)) 14px calc(14px + env(safe-area-inset-left, 0px)); }
  .card { padding: 14px 16px; }
  .grid-2, .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-2 > .span-all, .grid-3 > .span-all, .grid-2 > .span-2, .grid-3 > .span-2 { grid-column: 1 / -1; }
  .ov-grid3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .mood-card.span-all .mood-grid, .mood-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .mood-tile { aspect-ratio: 16 / 9; min-height: 0; }
  .th-row.th-2x2 .th-card { height: 140px; }
  .lp-hero { height: 130px; min-height: 130px; }
  .stage, .viewport, .panel { overflow-x: hidden; }
}

/* ===== Collapsing header while scrolled (body.phone-compact set by phone.js) ===== */
@media (max-width: 480px) {
  body.phone-compact header.hero .chips,
  body.phone-compact header.hero .alert-rail,
  body.phone-compact header.hero .clock,
  body.phone-compact header.hero .season-badge,
  body.phone-compact header.hero::after { display: none !important; }
  body.phone-compact header.hero { padding-bottom: 8px; }
  body.phone-compact header.hero h2 { font-size: 24px; }
}
@media (orientation: landscape) and (max-height: 480px) and (max-width: 932px) {
  body.phone-compact header.hero .chips,
  body.phone-compact header.hero .alert-rail { display: none !important; }
  body.phone-compact header.hero { padding-bottom: 6px; }
}

/* ===== round 2 tweaks ===== */
@media (max-width: 480px) {
  .chip.nest-chip, button.climate-chip.nest-chip { padding: 6px 12px; font-size: 13px; gap: 7px; min-height: 40px; }
  .nest-chip .ct { font-size: 16px; }
  .th-row .th-card.th-mn, .th-row.th-2x2 .th-card.th-mn { height: 184px; }
}
@media (orientation: landscape) and (max-height: 480px) and (max-width: 932px) {
  .chip.nest-chip, button.climate-chip.nest-chip { padding: 4px 10px; font-size: 12px; gap: 6px; min-height: 32px; }
  .nest-chip .ct { font-size: 15px; }
}
@media (max-width: 480px) {
  /* xbox.js sets inline width/height on .art, so override needs !important */
  .xbox-tile .hero > .art { width: 92px !important; height: 92px !important; flex: none; }
  .xbox-tile .art .glyph svg { width: 44px; height: 44px; }
  .xbox-tile .hero > .copy { flex: 1 1 0; min-width: 0; }
}
@media (orientation: landscape) and (max-height: 480px) and (max-width: 932px) {
  .xbox-tile .hero > .art { width: 110px !important; height: 110px !important; flex: none; }
  .xbox-tile .hero > .copy { flex: 1 1 0; min-width: 0; }
}

/* ===== Big-phone landscape (901-932px wide, e.g. iPhone Pro Max): use the
   bottom tab bar instead of the 220px sidebar, same as <=900px phones ===== */
@media (orientation: landscape) and (max-height: 480px) and (min-width: 901px) and (max-width: 932px) {
  .app { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  .sidebar { display: none; }
  .tabbar {
    display: flex; position: relative;
    background: rgba(13,19,28,0.88);
    -webkit-backdrop-filter: blur(22px); backdrop-filter: blur(22px);
    border-top: 1px solid var(--line);
    z-index: 5; gap: 0;
  }
  .tab-pill {
    position: absolute; left: 0; width: 64px;
    border-radius: 14px; background: rgba(114,217,248,0.16);
    box-shadow: inset 0 3px 0 var(--sky); pointer-events: none;
    transition: transform .42s var(--ease), width .42s var(--ease);
  }
  .tabbar button {
    flex: 1; appearance: none; border: 0; background: transparent;
    color: var(--muted); font-family: "Montserrat", sans-serif;
    font-weight: 750; letter-spacing: 0.04em;
    border-radius: 14px; position: relative; z-index: 1; text-transform: uppercase;
  }
  .tabbar button.active { color: var(--sky); }
  .nowbar { left: var(--glass-gap, 14px); }
  .grid-overview, .grid-3, .grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .mood-card.span-all .mood-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .mood-card.span-all .mood-tile { aspect-ratio: 16 / 9; min-height: 0; }
  .dogcam-overlay .dogcam-sheet { left: 50%; width: min(780px, calc(100% - 32px)); }
  .climate-overlay .climate-sheet { left: 50%; }
  .climate-overlay.movienight-overlay .climate-sheet.mn-sheet { width: calc(100% - 28px); }
  .xbox-overlay .xbox-sheet { left: 50%; }
}
@media (max-width: 480px) {
  .lights-sheet .light-grid.compact, .lights-sheet .light-grid { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 480px) {
  /* The Xbox art row also uses header.hero; it must not get the header's row-break. */
  .xbox-tile .hero::after { display: none; }
}

@media (orientation: landscape) and (max-height: 480px) and (max-width: 932px) {
  /* Bottom sheets must fit a short screen and scroll inside */
  .climate-overlay .climate-sheet { max-height: calc(100dvh - 12px); overflow-y: auto; -webkit-overflow-scrolling: touch; }
}
