/* The app shell: the frame and its gutters, the sticky header with the mode chip, the floating tab bar that becomes a rail on
   a wide screen, the mode sheet, and the Settings list. Only tokens from theme.css, no raw colours, no inline styles.

   Three layouts, two breakpoints:
     phone    < 640px      20px gutters, a floating bottom tab bar
     tablet   640-1023px   24px gutters, content at most 720px wide, the same bottom tab bar
     desktop  >= 1024px    32px gutters, content at most 1120px, a fixed 248px rail instead of the bottom bar */

/* ---------- frame ---------- */

.shell {
  --gutter: var(--space-20);
  --content-max: 100vw;
  /* keeps the last card clear of the floating tab bar (56px + 16px margin) */
  padding-bottom: calc(var(--space-40) + 56px + var(--space-32) + env(safe-area-inset-bottom, 0px));
}

/* The content column: header contents, status line, screens and footer share it. */
.wrap {
  width: min(100% - 2 * var(--gutter), var(--content-max));
  margin-inline: auto;
}

@media (min-width: 640px) {
  .shell {
    --gutter: var(--space-24);
    --content-max: 720px;
  }
}

@media (min-width: 1024px) {
  .shell {
    --gutter: var(--space-32);
    --content-max: 1120px;
    padding-left: 248px;
    padding-bottom: var(--space-40);
  }
}

main {
  padding-top: var(--space-16);
}

/* Whatever scrolls into view (a field that takes the focus, an anchor) lands clear of the sticky header on top and of the floating
   tab bar below (its 56px, the 16px it floats off the edge, and a little air). On a desktop the rail is at the side, so only the
   header matters. A screen with a save bar needs more at the foot: the bar (up to ~96px with a message) sits above the tab bar.
   The selector is generic (any visible screen holding a .save-bar: Settings and the Hot Water form alike). */
html {
  scroll-padding-top: calc(61px + var(--space-16));
  scroll-padding-bottom: calc(56px + var(--space-16) + var(--space-24) + env(safe-area-inset-bottom, 0px));
}

:root:has(main > section:not([hidden]) .save-bar) {
  scroll-padding-bottom: calc(
    var(--space-16) + 56px + var(--space-12) + 96px + env(safe-area-inset-bottom, 0px)
  );
}

@media (min-width: 640px) {
  html {
    scroll-padding-top: calc(69px + var(--space-16));
  }
}

@media (min-width: 1024px) {
  html {
    scroll-padding-bottom: var(--space-24);
  }

  :root:has(main > section:not([hidden]) .save-bar) {
    scroll-padding-bottom: 112px;
  }
}

/* Settings stays a centred column of 720px on a desktop: a list of rows and forms does not want the width. */
@media (min-width: 1024px) {
  #panel-settings {
    max-width: 720px;
    margin-inline: auto;
  }
}

/* The very first /api/status failed (app.js): one shared message, with a retry, stands in for the screen. */
.status-error {
  padding: 0;
}

main.has-status-error > section {
  display: none !important;
}

main:focus,
.topbar-title:focus,
.topbar-title:focus-visible {
  outline: none;
  box-shadow: none;
}

/* A screen fades in when it is shown (the navigation is instant; this only softens the swap). */
@media (prefers-reduced-motion: no-preference) {
  main > section:not([hidden]) {
    animation: screen-in var(--dur-2) var(--ease-out);
  }
}

@keyframes screen-in {
  from {
    opacity: 0;
  }
}

/* ---------- header ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  background: var(--bg);
  border-bottom: 1px solid transparent;
  transition: border-color var(--dur-1) var(--ease-out);
}

@supports (background: color-mix(in srgb, red 50%, blue)) {
  .topbar {
    background: color-mix(in srgb, var(--bg) 88%, transparent);
    -webkit-backdrop-filter: blur(var(--space-12));
    backdrop-filter: blur(var(--space-12));
  }
}

.topbar.is-scrolled {
  border-bottom-color: var(--border);
}

.topbar-inner {
  container: topbar / inline-size;
  display: flex;
  align-items: center;
  gap: var(--space-12);
  min-height: 60px;
}

@media (min-width: 640px) {
  .topbar-inner {
    min-height: 68px;
  }
}

.topbar-mark {
  display: inline-flex;
  flex: none;
}

.topbar.has-back .topbar-mark {
  display: none;
}

/* On a wide screen the rail carries the brand. */
@media (min-width: 1024px) {
  .topbar-mark {
    display: none;
  }
}

.topbar-title {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  letter-spacing: -0.01em;
}

/* A sub-page on a phone needs the room for its title ("System Specification"): the Demo badge steps aside (the rest of the
   dashboard and the footer still say it) and the title may take two lines. */
@media (max-width: 639px) {
  .topbar.has-back #demo-badge,
  .topbar.has-back #settings-btn {
    display: none;
  }

  .topbar.has-back .topbar-title {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    white-space: normal;
    font-size: var(--text-lg-size);
    line-height: var(--text-lg-line);
  }
}

.topbar-actions {
  display: flex;
  flex: none;
  align-items: center;
  gap: var(--space-8);
}

/* The title is what a phone header is for. When the header (the screen minus its gutters) gets as narrow as a 400px-wide phone
   or less, the Demo badge steps aside (the footer and the sign-in screen still say it), and on the narrowest the brand mark
   too, so "Devices" and "Insights" are never cut to "Dev…". */
@container topbar (max-width: 359px) {
  #demo-badge {
    display: none;
  }
}

@container topbar (max-width: 319px) {
  .topbar-mark {
    display: none;
  }
}

/* A round icon button: 40px to look at, 44px to hit. */
.icon-btn {
  position: relative;
  flex: none;
  width: 40px;
  min-height: 0;
  height: 40px;
  padding: 0;
  border: 0;
  border-radius: var(--radius-full);
  background: transparent;
  box-shadow: none;
  color: var(--icon);
}

.icon-btn::after {
  content: '';
  position: absolute;
  inset: -2px;
}

.icon-btn:hover:not(:disabled) {
  background: var(--fill);
}

.icon-btn[aria-current='page'] {
  background: var(--accent-weak);
  color: var(--accent-weak-text);
}

/* The chevron's visual edge lines up with the gutter. */
#back-btn {
  margin-inline-start: calc(-1 * var(--space-10));
}

/* ---------- the mode chip ---------- */

.mode-chip-btn {
  --chip-bg: var(--mode-shadow-bg);
  position: relative;
  flex: none;
  gap: var(--space-6);
  min-height: 32px;
  padding: 0 var(--space-8) 0 var(--space-12);
  border: 1px solid transparent;
  border-radius: var(--radius-full);
  background: var(--chip-bg);
  box-shadow: none;
  color: var(--mode-shadow-text);
  font-size: var(--text-sm-size);
  line-height: var(--text-sm-line);
  font-weight: var(--weight-semibold);
}

/* A 44px-tall target around the 32px pill (the box is measured from inside its 1px border, hence 7 and 3). */
.mode-chip-btn::after {
  content: '';
  position: absolute;
  inset: -7px -3px;
}

.mode-chip-btn:hover:not(:disabled) {
  background: color-mix(in srgb, currentColor 9%, var(--chip-bg));
}

.mode-chip-btn[data-mode='live'] {
  --chip-bg: var(--mode-live-bg);
  border-color: var(--accent-solid-edge);
  color: var(--mode-live-text);
}

.mode-chip-btn[data-mode='paused'] {
  --chip-bg: var(--mode-paused-bg);
  border-color: color-mix(in srgb, var(--mode-paused-text) 28%, transparent);
  color: var(--mode-paused-text);
}

/* Live is told by a still dot (it does not pulse) and by the word. */
.mode-dot {
  width: var(--space-8);
  height: var(--space-8);
  border-radius: var(--radius-full);
  background: currentColor;
}

.mode-chip-btn .eh-icon {
  flex: none;
}

/* ---------- the quiet status line under the header ---------- */

.statusbar {
  padding-top: var(--space-4);
}

.mode-banner {
  color: var(--text-2);
  font-size: var(--text-xs-size);
  line-height: var(--text-xs-line);
}

.mode-banner strong {
  color: var(--text-2-strong);
  font-weight: var(--weight-medium);
}

.mode-banner:empty::before {
  display: none;
}

.mode-banner::before {
  content: '';
  display: inline-block;
  width: 6px;
  height: 6px;
  margin-right: var(--space-8);
  vertical-align: 1px;
  border-radius: var(--radius-full);
  background: var(--text-3);
}

.mode-banner.mode-live::before {
  background: var(--accent);
}

.mode-banner.mode-paused::before {
  background: var(--warn);
}

#alert-strip {
  display: grid;
  gap: var(--space-12);
  margin-top: var(--space-12);
}

/* The gap does the spacing: a notice's own margin (the .stack rule, the .notice reset) must not add to it or cancel it. */
#alert-strip > * {
  margin: 0;
}

#alert-strip:empty {
  margin: 0;
}

/* ---------- navigation: the floating bar on a phone or tablet ---------- */

/* Floating bottom navigation: 56px, radius 16, frosted glass with a backdrop blur (white in light, so the icons keep their
   contrast over the brand strip and dark cards; see --navbar-glass). The current destination expands into a pill with its
   label; the other items are icon-only (their label stays for assistive technology). */
.rail {
  display: contents;
}

.rail-brand,
.rail-system,
.rail-build {
  display: none;
}

.navbar {
  position: fixed;
  z-index: 20;
  left: 50%;
  bottom: calc(var(--space-16) + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  width: calc(100% - 2 * var(--space-20));
  max-width: 440px;
  height: 56px;
  padding: var(--space-8);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--navbar-glass);
  -webkit-backdrop-filter: blur(var(--navbar-blur));
  backdrop-filter: blur(var(--navbar-blur));
  border-radius: var(--radius-16);
  box-shadow: var(--shadow-lg);
}

.tab {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-6);
  min-width: 40px;
  height: 40px;
  padding: 0 var(--space-10);
  border: 0;
  border-radius: var(--radius-full);
  background: none;
  box-shadow: none;
  color: var(--text-strong);
  font-size: var(--text-xs-size);
  line-height: var(--text-xs-line);
  font-weight: var(--weight-semibold);
  white-space: nowrap;
  cursor: pointer;
  transition:
    background-color var(--dur-1) var(--ease-out),
    color var(--dur-1) var(--ease-out);
}

/* 40px to look at, 44px to hit: an invisible extension all round (the bar is 56px tall, so it stays inside it). */
.tab::after {
  content: '';
  position: absolute;
  inset: -2px;
}

.tab:hover:not([aria-selected='true']) {
  background: var(--fill);
}

.tab .tab-label {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.tab[aria-selected='true'] {
  background: var(--navbar-pill);
  color: var(--accent);
  padding: 0 var(--space-14);
  box-shadow: var(--shadow-sm);
}

.tab[aria-selected='true'] .tab-label {
  position: static;
  width: auto;
  height: auto;
  margin: 0;
  overflow: visible;
  clip-path: none;
}

/* A count on a tab (pending proposals on Agent): the number is decoration, the tab's description says what it means. */
.tab-badge {
  position: absolute;
  top: 0;
  right: 0;
  min-width: 16px;
  height: 16px;
  padding: 0 var(--space-4);
  border-radius: var(--radius-full);
  background: var(--accent-solid);
  box-shadow: 0 0 0 2px var(--surface-raised);
  color: var(--text-on-accent);
  font-size: var(--text-xs-size);
  line-height: 16px;
  font-weight: var(--weight-semibold);
  text-align: center;
  font-variant-numeric: tabular-nums;
}

/* ---------- navigation: the rail on a wide screen ---------- */

@media (min-width: 1024px) {
  .rail {
    display: flex;
    flex-direction: column;
    position: fixed;
    z-index: 40;
    top: 0;
    bottom: 0;
    left: 0;
    width: 248px;
    padding: var(--space-24) var(--space-16) var(--space-20);
    overflow-y: auto;
    background: var(--bg);
    border-right: 1px solid var(--border);
  }

  .rail-brand {
    display: flex;
    align-items: center;
    gap: var(--space-10);
    padding: var(--space-4) var(--space-12) var(--space-24);
    font-size: var(--text-lg-size);
    line-height: var(--text-lg-line);
    font-weight: var(--weight-semibold);
  }

  /* The same tab list, now a vertical stack with labels. */
  .navbar {
    position: static;
    transform: none;
    width: auto;
    max-width: none;
    height: auto;
    padding: 0;
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-start;
    gap: var(--space-4);
    background: none;
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
    border-radius: 0;
    box-shadow: none;
  }

  .tab::after {
    inset: 0;
  }

  .tab {
    justify-content: flex-start;
    gap: var(--space-12);
    height: 44px;
    padding: 0 var(--space-12);
    border-radius: var(--radius-10);
    color: var(--text-2-strong);
    font-size: var(--text-sm-size);
    line-height: var(--text-sm-line);
    font-weight: var(--weight-medium);
  }

  .tab .tab-label,
  .tab[aria-selected='true'] .tab-label {
    position: static;
    width: auto;
    height: auto;
    margin: 0;
    overflow: visible;
    clip-path: none;
  }

  .tab[aria-selected='true'] {
    padding: 0 var(--space-12);
    background: var(--accent-weak);
    box-shadow: none;
    color: var(--accent-weak-text);
  }

  .tab-badge {
    position: static;
    margin-left: auto;
    box-shadow: none;
  }

  .rail-system {
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
    margin-top: var(--space-24);
    padding-top: var(--space-16);
    border-top: 1px solid var(--border);
  }

  .rail-heading {
    margin: 0 0 var(--space-4);
    padding: 0 var(--space-12);
    color: var(--text-2);
    font-size: var(--text-xs-size);
    line-height: var(--text-xs-line);
    font-weight: var(--weight-medium);
  }

  .rail-link {
    display: flex;
    align-items: center;
    gap: var(--space-12);
    height: 40px;
    padding: 0 var(--space-12);
    border-radius: var(--radius-10);
    color: var(--text-2-strong);
    font-size: var(--text-sm-size);
    line-height: var(--text-sm-line);
    font-weight: var(--weight-medium);
    text-decoration: none;
    transition:
      background-color var(--dur-1) var(--ease-out),
      color var(--dur-1) var(--ease-out);
  }

  .rail-link:hover {
    background: var(--fill);
    color: var(--text);
  }

  .rail-link[aria-current='page'] {
    background: var(--accent-weak);
    color: var(--accent-weak-text);
  }

  .rail-build {
    display: block;
    margin: auto 0 0;
    padding: var(--space-16) var(--space-12) 0;
    color: var(--text-2);
    font-size: var(--text-xs-size);
    line-height: var(--text-xs-line);
    overflow-wrap: anywhere;
  }

  /* the build line moved into the rail */
  footer.site #build-info {
    display: none;
  }

  .toasts {
    bottom: var(--space-24);
  }
}

/* ---------- the mode sheet ---------- */

/* Its own dialog (see index.html). The sheet itself (bottom sheet on a phone, centred card from 640px, drag handle,
   scrim, motion) is the shared dialog styling in css/components.css; only the content is styled here. */
#mode-sheet {
  max-width: 480px;
}

@media (min-width: 640px) {
  #mode-sheet {
    max-width: 440px;
  }
}

#mode-sheet-title {
  margin-bottom: var(--space-4);
  font-size: var(--text-xl-size);
  line-height: var(--text-xl-line);
}

#mode-sheet-title:focus,
#mode-sheet-title:focus-visible {
  outline: none;
  box-shadow: none;
}

.sheet-meaning {
  margin: 0 0 var(--space-20);
  color: var(--text-2-strong);
  font-size: var(--text-md-size);
  line-height: var(--text-md-line);
}

/* The mode buttons stack, full width: big targets, and the long "Pause + restore baseline" never wraps. */
.controls {
  display: flex;
  flex-direction: column;
  gap: var(--space-8);
}

.controls button {
  width: 100%;
  min-height: 44px;
  padding: 0 var(--space-16);
}

.controls-note {
  margin: 0;
}

.sheet-blockers {
  margin-top: var(--space-20);
}

.sheet-blockers:empty {
  display: none;
}

.sheet-blockers h3 {
  margin-bottom: var(--space-8);
}

.sheet-blocker-list {
  display: grid;
  gap: var(--space-8);
  margin: 0 0 var(--space-4);
  padding: 0;
  list-style: none;
  font-size: var(--text-sm-size);
  line-height: var(--text-sm-line);
}

.sheet-blocker-list li {
  display: flex;
  align-items: flex-start;
  gap: var(--space-8);
  overflow-wrap: anywhere;
}

.sheet-blocker-list .eh-icon {
  flex: none;
  margin-top: var(--space-2);
  color: var(--warn);
}

.sheet-ready {
  display: flex;
  align-items: center;
  gap: var(--space-8);
  margin: 0;
  color: var(--ok-text);
}

button.sheet-link {
  justify-content: flex-start;
  gap: var(--space-4);
  min-height: 44px;
  padding: 0;
  border: 0;
  background: none;
  box-shadow: none;
  color: var(--accent);
  font-weight: var(--weight-medium);
}

button.sheet-link:hover:not(:disabled) {
  background: none;
  color: var(--accent-hover);
  text-decoration: underline;
}

.sheet-actions {
  margin-top: var(--space-20);
  padding-top: var(--space-16);
  border-top: 1px solid var(--border);
}

#mode-sheet-close {
  width: 100%;
  min-height: 44px;
}

/* ---------- a plain segmented switcher over the cards of a screen (the Agent's, until it is redesigned) ---------- */

.screen-tabs {
  display: flex;
  gap: var(--space-4);
  padding: var(--space-4);
  border-radius: var(--radius-12);
  background: var(--fill);
}

.screen-tab {
  flex: 1 1 0;
  min-width: 0;
  min-height: 40px;
  padding: 0 var(--space-8);
  border: 1px solid transparent;
  border-radius: var(--radius-8);
  background: none;
  box-shadow: none;
  color: var(--text-2-strong);
  font-weight: var(--weight-medium);
  transition:
    background-color var(--dur-1) var(--ease-out),
    color var(--dur-1) var(--ease-out);
}

.screen-tab:hover:not(:disabled):not([aria-selected='true']) {
  background: none;
  color: var(--text);
}

.screen-tab[aria-selected='true'] {
  border-color: var(--border);
  background: var(--surface);
  box-shadow: var(--shadow-xs);
  color: var(--text);
}

/* ---------- Settings: the hub list and its sub-pages ---------- */

.settings-hub {
  display: grid;
  gap: var(--space-24);
}

.setting-group h2 {
  margin: 0 0 var(--space-8) var(--space-4);
  color: var(--text-2);
  font-size: var(--text-sm-size);
  line-height: var(--text-sm-line);
  font-weight: var(--weight-medium);
}

.setting-list {
  margin: 0;
  padding: 0;
  list-style: none;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius-12);
  background: var(--surface);
}

.setting-list > li + li {
  border-top: 1px solid var(--border);
}

.setting-row {
  justify-content: flex-start;
  gap: var(--space-12);
  width: 100%;
  min-height: 60px;
  padding: var(--space-10) var(--space-16);
  border: 0;
  border-radius: 0;
  background: none;
  box-shadow: none;
  color: var(--text);
  font-size: var(--text-md-size);
  line-height: var(--text-md-line);
  font-weight: var(--weight-medium);
  text-align: left;
}

.setting-row:hover:not(:disabled) {
  background: var(--surface-2);
}

.setting-label {
  flex: 1 1 auto;
  min-width: 0;
}

.setting-value {
  flex: none;
  color: var(--text-2);
  font-size: var(--text-sm-size);
  line-height: var(--text-sm-line);
  font-weight: var(--weight-regular);
  white-space: nowrap;
}

.setting-row .eh-chevron {
  flex: none;
  margin-left: calc(-1 * var(--space-4));
  color: var(--text-2);
}

.setting-row.destructive,
.setting-row.destructive .featured {
  color: var(--crit);
}

.status-dots {
  display: inline-flex;
  gap: var(--space-4);
}

.status-dot {
  width: var(--space-8);
  height: var(--space-8);
  border: 1.5px solid var(--text-2);
  border-radius: var(--radius-full);
}

.status-dot.on {
  border-color: var(--ok);
  background: var(--ok);
}

/* A sub-page: the header says which page this is; its sections carry their own h2 titles. */
.settings-page {
  display: grid;
  gap: var(--space-12);
}

/* Appearance: three radios in a bordered list. */
.choice-list {
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius-12);
}

.choice {
  display: flex;
  align-items: center;
  gap: var(--space-12);
  min-height: 60px;
  margin: 0;
  padding: var(--space-10) var(--space-16);
  cursor: pointer;
}

.choice + .choice {
  border-top: 1px solid var(--border);
}

.choice:hover {
  background: var(--surface-2);
}

.choice:has(input:checked) {
  background: var(--accent-weak);
}

.choice input[type='radio'] {
  flex: none;
  width: 20px;
  min-height: 0;
  height: 20px;
  margin: 0;
  padding: 0;
  accent-color: var(--accent-solid);
  box-shadow: none;
}

.choice-text {
  display: grid;
  min-width: 0;
}

.choice-label {
  color: var(--text);
  font-size: var(--text-md-size);
  line-height: var(--text-md-line);
  font-weight: var(--weight-medium);
}

.choice .hint {
  margin: 0;
}

/* ---------- forced colours ---------- */

/* The shell's part of the forced-colour mode (see the foot of css/components.css): the mode chip keeps an edge (its word still
   says Shadow, Live or Paused), the selected tab and the current System link are told by the system's highlight, and the
   floating bar gets an edge now that its glass and shadow are gone. System colour keywords only. */
@media (forced-colors: active) {
  .mode-chip-btn {
    border: 1px solid ButtonText;
  }

  .mode-dot {
    forced-color-adjust: none;
    background: ButtonText;
  }

  .navbar {
    border: 1px solid CanvasText;
    background: Canvas;
  }

  @media (min-width: 1024px) {
    .navbar {
      border: 0;
      background: none;
    }
  }

  .tab[aria-selected='true'],
  .rail-link[aria-current='page'],
  .icon-btn[aria-current='page'] {
    forced-color-adjust: none;
    background: Highlight;
    color: HighlightText;
  }

  .tab-badge {
    border: 1px solid CanvasText;
  }
}
