:root {
  color-scheme: dark;
  --font-sans: "Manrope", "Segoe UI", sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;

  /* Flat, dense, Vercel/Grafana-style control-room palette. Dark is the
     default surface for this app (a wall/monitor dashboard), light is an
     explicit opt-in via the theme toggle - see :root[data-theme="light"]. */
  --bg: #08090b;
  --surface: #101113;
  --surface-2: #17181b;
  --border: #212226;
  --border-strong: #2c2d32;

  --text: #ededee;
  --text-dim: #9a9ba3;
  --text-soft: #6c6d74;

  --primary: #3b82f6;
  --primary-strong: #60a5fa;
  --primary-soft: rgba(59, 130, 246, 0.14);
  --success: #2fbd6d;
  --success-soft: rgba(47, 189, 109, 0.13);
  --danger: #f2495c;
  --danger-soft: rgba(242, 73, 92, 0.13);
  --amber: #e5a63c;
  --amber-soft: rgba(229, 166, 60, 0.13);

  --control-height: 40px;
  --focus-ring: rgba(59, 130, 246, 0.5);
  --safe-area-top: env(safe-area-inset-top, 0px);
  --safe-area-right: env(safe-area-inset-right, 0px);
  --safe-area-bottom: env(safe-area-inset-bottom, 0px);
  --safe-area-left: env(safe-area-inset-left, 0px);

  --radius-xl: 12px;
  --radius-lg: 10px;
  --radius-md: 8px;
  --radius-sm: 6px;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.45);
  --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.55);

  --header-bg: var(--surface);
  --header-text: var(--text);
  --header-text-dim: var(--text-dim);

  --chart-grid-major: rgba(255, 255, 255, 0.12);
  --chart-grid-minor: rgba(255, 255, 255, 0.06);
  --chart-axis-line: rgba(255, 255, 255, 0.2);
  --chart-text-strong: #d7d8dc;
  --chart-text-soft: #8b8c93;
  --chart-point-fill: #101113;
}

:root[data-theme="light"] {
  color-scheme: light;

  --bg: #f7f7f8;
  --surface: #ffffff;
  --surface-2: #f1f2f4;
  --border: #e2e3e6;
  --border-strong: #cbccd1;

  --text: #17181a;
  --text-dim: #55565e;
  --text-soft: #8a8b92;

  --primary: #1c64f2;
  --primary-strong: #1450c4;
  --primary-soft: #e6efff;
  --success: #0d8a4c;
  --success-soft: #e2f7ea;
  --danger: #d6273b;
  --danger-soft: #fbe7e9;
  --amber: #b3760f;
  --amber-soft: #faf0dc;

  --shadow-sm: 0 1px 2px rgba(16, 17, 19, 0.05);
  --shadow-md: 0 6px 18px rgba(16, 17, 19, 0.08);
  --shadow-lg: 0 16px 36px rgba(16, 17, 19, 0.12);

  --header-bg: var(--surface);
  --header-text: var(--text);
  --header-text-dim: var(--text-dim);

  --chart-grid-major: #e2e3e6;
  --chart-grid-minor: #edeef0;
  --chart-axis-line: #cbccd1;
  --chart-text-strong: #2c2d31;
  --chart-text-soft: #75767d;
  --chart-point-fill: #ffffff;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    color-scheme: light;

    --bg: #f7f7f8;
    --surface: #ffffff;
    --surface-2: #f1f2f4;
    --border: #e2e3e6;
    --border-strong: #cbccd1;

    --text: #17181a;
    --text-dim: #55565e;
    --text-soft: #8a8b92;

    --primary: #1c64f2;
    --primary-strong: #1450c4;
    --primary-soft: #e6efff;
    --success: #0d8a4c;
    --success-soft: #e2f7ea;
    --danger: #d6273b;
    --danger-soft: #fbe7e9;
    --amber: #b3760f;
    --amber-soft: #faf0dc;

    --shadow-sm: 0 1px 2px rgba(16, 17, 19, 0.05);
    --shadow-md: 0 6px 18px rgba(16, 17, 19, 0.08);
    --shadow-lg: 0 16px 36px rgba(16, 17, 19, 0.12);

    --header-bg: var(--surface);
    --header-text: var(--text);
    --header-text-dim: var(--text-dim);

    --chart-grid-major: #e2e3e6;
    --chart-grid-minor: #edeef0;
    --chart-axis-line: #cbccd1;
    --chart-text-strong: #2c2d31;
    --chart-text-soft: #75767d;
    --chart-point-fill: #ffffff;
  }
}

/* Reset */

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
  margin: 0;
}

body {
  font-family: var(--font-sans);
  color: var(--text);
  background: var(--bg);
  line-height: 1.42;
  min-height: 100dvh;
}

body.overlay-open {
  overflow: hidden;
}

.icon {
  width: 1em;
  height: 1em;
  flex-shrink: 0;
  vertical-align: -0.125em;
}

h2,
h3 {
  margin: 0;
}

h1 {
  margin: 0;
  font-size: 1.05rem;
  line-height: 1.2;
  letter-spacing: -0.01em;
}

h2 {
  font-size: 0.92rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  display: inline-flex;
  align-items: center;
  gap: 9px;
}

h2 .icon {
  color: var(--primary);
  background: var(--primary-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  width: 26px;
  height: 26px;
  padding: 5px;
}

#sensors h2 .icon {
  color: var(--success);
  background: var(--success-soft);
}

h3 {
  font-size: 0.9rem;
}

.eyebrow {
  margin: 0;
  text-transform: uppercase;
  font-size: 10px;
  letter-spacing: 0.16em;
  color: var(--primary);
  font-weight: 700;
}

.subhead,
.panel-sub {
  margin: 4px 0 0;
  color: var(--text-dim);
  font-size: 0.78rem;
}

.hidden {
  display: none !important;
}

/* App shell + layout */

.app-shell {
  width: min(1440px, 100%);
  margin: 0 auto;
  padding:
    calc(12px + var(--safe-area-top))
    calc(12px + var(--safe-area-right))
    calc(20px + var(--safe-area-bottom))
    calc(12px + var(--safe-area-left));
  display: grid;
  gap: 12px;
}

.topbar {
  position: sticky;
  top: calc(12px + var(--safe-area-top));
  z-index: 40;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  padding: 10px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 52px;
  background: var(--header-bg);
  box-shadow: var(--shadow-sm);
}

.brand-head {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-logo-shell {
  width: 30px;
  height: 30px;
  flex: 0 0 auto;
  border-radius: var(--radius-sm);
  display: grid;
  place-items: center;
  background: var(--primary-soft);
  border: 1px solid var(--border);
}

.brand-logo {
  width: 17px;
  height: 17px;
  display: block;
}

.topbar h1 {
  font-size: 0.98rem;
  letter-spacing: 0;
}

.topbar h1 {
  color: var(--header-text);
}

.topbar .eyebrow {
  color: var(--header-text-dim);
}

.topbar .subhead {
  color: var(--header-text-dim);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.topbar-buttons {
  display: flex;
  align-items: center;
  gap: 6px;
}

.user-menu {
  position: relative;
}

.user-menu-dropdown {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 40;
  display: grid;
  gap: 6px;
  min-width: 160px;
  padding: 10px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--surface);
  box-shadow: var(--shadow-md);
}

.user-menu-name {
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--text);
  padding: 2px 4px 6px;
  border-bottom: 1px solid var(--border);
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 34px;
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.1);
  font-size: 0.76rem;
  font-weight: 700;
  line-height: 1;
  color: var(--header-text);
}

.status-indicator {
  display: inline-block;
  flex: 0 0 auto;
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: var(--text-soft);
}

.status-pill span:last-child {
  display: inline-flex;
  align-items: center;
  min-height: 16px;
}

.status-syncing .status-indicator {
  background: var(--amber);
}

.status-connected .status-indicator {
  background: var(--success);
}

.status-disconnected .status-indicator,
.status-error .status-indicator {
  background: var(--danger);
}

.install-btn {
  min-height: 34px;
  font-size: 0.74rem;
  padding: 6px 12px;
}

.icon-btn {
  min-height: 34px;
  min-width: 34px;
  padding: 6px;
  font-size: 1rem;
}

.layout {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}

/* Cards / panels */

.panel,
.drawer-section,
.sensor-card,
.schedule-card,
.automation-rule,
.metric-card,
.stat-card,
.chart-card,
.timer-card {
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--surface);
}

.panel {
  padding: 14px;
  box-shadow: var(--shadow-sm);
  display: grid;
  gap: 12px;
}

.panel-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.panel-body {
  min-width: 0;
  display: grid;
  gap: 10px;
}

.panel-hidden {
  display: none !important;
}

.drawer-section {
  padding: 12px;
  display: grid;
  gap: 10px;
  border-radius: var(--radius-md);
}

.drawer-section-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
  flex-wrap: wrap;
}

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

.drawer-hint {
  color: var(--text-soft);
  font-size: 0.72rem;
}

.chart-section {
  margin-top: 2px;
}

/* Buttons */

button,
input,
select {
  font-family: inherit;
}

button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  min-height: var(--control-height);
  padding: 7px 12px;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  color: var(--text);
  background: var(--surface-2);
  transition: background 0.1s ease, border-color 0.1s ease, filter 0.1s ease;
}

button:hover {
  background: var(--border);
  border-color: var(--border-strong);
}

button:active {
  filter: brightness(0.94);
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

button:focus-visible,
input:focus-visible,
select:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 1px;
}

button.primary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

button.primary:hover {
  background: var(--primary-strong);
  border-color: var(--primary-strong);
}

button.ghost {
  background: var(--surface);
  border-color: var(--border);
  color: var(--text-dim);
}

button.ghost:hover {
  background: var(--surface-2);
  border-color: var(--border-strong);
  color: var(--text);
}

button.danger {
  background: var(--danger);
  color: #fff;
  border-color: var(--danger);
}

button.danger:hover {
  filter: brightness(1.06);
}

.mini {
  min-height: 40px;
  padding: 7px 9px;
  font-size: 0.73rem;
}

.mini.active {
  border-color: var(--primary);
  background: var(--primary-soft);
  color: var(--primary);
}

.mini-action {
  min-height: 36px;
  font-size: 0.69rem;
}

/* Relay grid */

.relay-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.relay-tile {
  position: relative;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  border-left: 3px solid var(--border-strong);
  background: var(--surface);
  padding: 16px;
  min-height: 104px;
  min-width: 0;
  display: grid;
  gap: 10px;
  align-content: start;
  text-align: left;
  width: 100%;
  overflow: hidden;
  cursor: pointer;
  transition: border-color 0.1s ease, background 0.1s ease, transform 0.08s ease;
}

.relay-tile.is-on {
  border-left-color: var(--success);
  border-color: var(--success);
  background: var(--success-soft);
}

.relay-tile.is-off {
  border-left-color: var(--border-strong);
}

.relay-tile:hover {
  border-color: var(--border-strong);
}

.relay-tile.is-on:hover {
  border-color: var(--success);
}

.relay-tile:active {
  transform: scale(0.98);
}

.relay-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 6px;
  min-width: 0;
  min-height: 13px;
}

.relay-row-status {
  justify-content: center;
  margin-top: 2px;
}

.relay-name {
  font-size: 0.86rem;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.005em;
  min-width: 0;
  flex: 1 1 auto;
  text-align: center;
  white-space: normal;
  overflow-wrap: break-word;
  word-break: break-word;
}

.status-badge {
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.04em;
  border-radius: var(--radius-sm);
  padding: 3px 7px;
  color: var(--text-dim);
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.status-badge.on {
  color: var(--success);
  background: var(--success-soft);
  border-color: var(--success);
}

.badge {
  font-family: var(--font-mono);
  font-size: 0.65rem;
  border-radius: var(--radius-sm);
  padding: 2px 7px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-dim);
}

.badge.tiny {
  font-size: 0.58rem;
}

.badge.error {
  border-color: var(--danger);
  color: var(--danger);
  background: var(--danger-soft);
}

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

/* Sensors */

.sensor-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 10px;
}

.sensor-card {
  display: grid;
  gap: 10px;
  padding: 12px 14px;
  transition: border-color 0.12s ease;
}

.sensor-card:hover {
  border-color: var(--border-strong);
}

.sensor-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.sensor-title {
  min-width: 0;
  display: grid;
  gap: 2px;
}

.sensor-title strong {
  display: block;
  line-height: 1.2;
}

.sensor-controls {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  flex-wrap: nowrap;
  flex-shrink: 0;
  white-space: nowrap;
}

.sensor-controls .mini {
  min-height: 32px;
  padding: 6px 8px;
}

.sensor-meta,
.metric-meta,
.automation-meta,
.field-note {
  color: var(--text-dim);
  font-size: 0.72rem;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: 10px;
}

.metric-card {
  padding: 12px;
  display: grid;
  gap: 5px;
  border-radius: var(--radius-md);
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.metric-card-header {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--text-soft);
}

.metric-card-header .icon {
  width: 13px;
  height: 13px;
  flex-shrink: 0;
}

.metric-title {
  font-size: 0.62rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-soft);
  font-weight: 700;
}

.metric-value {
  font-family: var(--font-mono);
  font-size: 1.2rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: var(--text);
}

.metric-gauge {
  display: grid;
  gap: 3px;
}

.metric-gauge-track {
  height: 4px;
  border-radius: 999px;
  background: var(--border);
  overflow: hidden;
}

.metric-gauge-fill {
  height: 100%;
  background: var(--primary);
  border-radius: 999px;
  transition: width 0.2s ease;
}

.metric-gauge-bounds {
  display: flex;
  justify-content: space-between;
  font-family: var(--font-mono);
  font-size: 0.6rem;
  color: var(--text-soft);
}

.sparkline {
  width: 100%;
  height: 30px;
}

.sparkline polyline {
  stroke: var(--primary);
  stroke-width: 2;
  fill: none;
}

/* Forms */

.schedule-form,
.automation-form {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.sensor-automation-form {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.field {
  display: grid;
  gap: 5px;
}

.field label {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--text-dim);
}

.checkbox-field {
  display: flex !important;
  align-items: center;
  gap: 8px;
  padding: 9px 11px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  cursor: pointer;
}

.checkbox-field:has(input:checked) {
  border-color: var(--primary);
  background: var(--primary-soft);
  color: var(--primary);
}

.checkbox-field input[type="checkbox"] {
  min-height: 0;
  width: 16px;
  height: 16px;
  accent-color: var(--primary);
}

.input-icon-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.input-icon {
  position: absolute;
  left: 11px;
  width: 15px;
  height: 15px;
  color: var(--text-soft);
  pointer-events: none;
}

.input-icon-wrap input {
  width: 100%;
  padding-left: 34px;
}

.field.full {
  grid-column: 1 / -1;
}

.field-inline {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.weekday-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 7px;
}

.weekday-grid label {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 8px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  font-size: 0.72rem;
}

.weekday-grid input[type="checkbox"] {
  margin: 0;
}

input,
select {
  min-height: var(--control-height);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  padding: 8px 10px;
  background: var(--surface);
  color: var(--text);
}

input::placeholder {
  color: var(--text-soft);
}

select option {
  background: var(--surface);
  color: var(--text);
}

.relay-target-select {
  display: none;
}

.relay-target-buttons {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
  gap: 6px;
}

.relay-target-chip {
  padding: 8px 9px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  text-align: left;
  justify-content: flex-start;
  font-size: 0.74rem;
  font-weight: 700;
  min-width: 0;
  min-height: 0;
  width: 100%;
}

.relay-target-chip-label {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
  flex: 1 1 auto;
}

.relay-target-chip.is-selected {
  border-color: var(--primary);
  background: var(--primary-soft);
}

.schedule-list,
.automation-list {
  display: grid;
  gap: 8px;
}

.schedule-card,
.automation-rule {
  padding: 10px;
  display: grid;
  gap: 5px;
  border-radius: var(--radius-sm);
}

.schedule-card strong,
.automation-rule strong {
  font-size: 0.85rem;
}

.user-list {
  display: grid;
  gap: 6px;
}

.user-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
}

.user-avatar {
  flex: 0 0 auto;
  width: 30px;
  height: 30px;
  border-radius: var(--radius-sm);
  display: grid;
  place-items: center;
  font-family: var(--font-mono);
  font-size: 0.68rem;
  font-weight: 700;
  color: var(--text-dim);
  background: var(--surface);
  border: 1px solid var(--border);
}

.user-avatar.is-admin {
  color: var(--success);
  background: var(--success-soft);
  border-color: var(--success);
}

.user-row-info {
  min-width: 0;
  flex: 1 1 auto;
  display: grid;
  gap: 2px;
}

.user-row-name {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--text);
  min-width: 0;
}

.user-row-name-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
  flex: 0 1 auto;
}

.user-row-meta {
  font-family: var(--font-mono);
  font-size: 0.66rem;
  color: var(--text-soft);
}

.user-row-delete {
  flex: 0 0 auto;
  min-height: 30px;
  min-width: 30px;
  padding: 6px;
}

.user-row-delete:hover {
  color: var(--danger);
  border-color: var(--danger);
  background: var(--danger-soft);
}

.automation-rule-targets {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.automation-rule-condition {
  font-size: 0.82rem;
  color: var(--text);
}

.automation-rule-if {
  font-family: var(--font-mono);
  font-size: 0.65rem;
  font-weight: 700;
  color: var(--text-soft);
  letter-spacing: 0.04em;
}

.automation-rule-arrow {
  color: var(--text-soft);
}

.automation-rule-action {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}

.automation-rule-action.is-on {
  color: var(--success);
  background: var(--success-soft);
  border-color: var(--success);
}

.automation-rule-action.is-off {
  color: var(--text-dim);
  background: var(--surface-2);
}

.pill {
  display: inline-flex;
  align-items: center;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 0.68rem;
  color: var(--text-dim);
}

/* Timer modal (only remaining modal) */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(4, 5, 6, 0.65);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.12s ease;
  z-index: 1010;
}

.modal-backdrop.show {
  opacity: 1;
  pointer-events: auto;
}

.modal {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%) scale(0.99);
  width: min(420px, 92vw);
  max-height: min(90vh, 640px);
  max-height: min(90dvh, 640px);
  opacity: 0;
  pointer-events: none;
  display: grid;
  grid-template-rows: auto 1fr;
  overflow: hidden;
  z-index: 1020;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  transition: opacity 0.12s ease, transform 0.12s ease;
}

.modal.show {
  opacity: 1;
  pointer-events: auto;
  transform: translate(-50%, -50%) scale(1);
}

.modal-header {
  padding: 14px 18px 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
}

.modal-header h2 {
  font-size: 0.92rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.modal-body {
  padding: 14px 18px 18px;
  overflow: auto;
  display: grid;
  gap: 10px;
  /* .modal is `grid-template-rows: auto 1fr` with overflow: hidden - a grid
     item's default min-height: auto keeps it at its content size, so
     without this the 1fr row just grows past the modal's max-height and
     the overflow gets silently clipped instead of scrolling. */
  min-height: 0;
}

.modal-lg {
  width: min(820px, 95vw);
}

.schedule-modal {
  width: min(920px, 95vw);
}

#sensor-modal {
  width: min(760px, 95vw);
  max-height: min(92vh, 820px);
  max-height: min(92dvh, 820px);
}

.schedule-modal-body {
  grid-template-columns: minmax(0, 1.1fr) minmax(0, 0.9fr);
  align-items: start;
}

/* The automation form has more fields than the schedule form, so it reads
   better stacked full-width with the existing-rules list below it, rather
   than squeezed into a side column that ends up much shorter than the form
   and leaves a lot of dead space next to it. */
.sensor-automation-body {
  grid-template-columns: 1fr;
}

.timer-card {
  display: grid;
  gap: 10px;
  padding: 10px;
  border-radius: var(--radius-md);
}

.timer-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.timer-wheel {
  display: grid;
  grid-template-columns: 40px 1fr 40px;
  gap: 8px;
  align-items: stretch;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  padding: 8px;
}

.timer-step {
  min-height: 42px;
  font-size: 1.1rem;
  font-weight: 800;
  border-radius: var(--radius-sm);
}

.timer-value-wrap {
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  display: grid;
  gap: 2px;
  justify-items: center;
  align-content: center;
  padding: 6px;
}

.timer-value {
  font-size: 1.5rem;
  font-weight: 800;
  line-height: 1.1;
}

.timer-value-unit,
.timer-value-human {
  font-size: 0.71rem;
  color: var(--text-dim);
}

.timer-presets {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 7px;
}

.chip {
  min-height: 38px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text-dim);
  font-size: 0.72rem;
}

.timer-chip.is-selected {
  color: #fff;
  background: var(--primary);
  border-color: var(--primary);
}

/* Sensor telemetry chart */

.sensor-history-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.history-pill {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-dim);
  padding: 4px 9px;
  font-size: 0.69rem;
  font-weight: 700;
}

.history-pill.is-empty {
  border-color: var(--amber);
  color: var(--amber);
}

.history-refresh {
  margin-left: auto;
  min-height: 32px;
  padding: 5px 9px;
}

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

.tab {
  min-height: 38px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text-dim);
  font-size: 0.71rem;
}

.tab.active {
  color: #fff;
  background: var(--primary);
  border-color: var(--primary);
}

.sensor-metric-blocks {
  display: grid;
  gap: 14px;
}

.metric-stat-groups {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 12px;
}

.metric-stat-group {
  display: grid;
  gap: 10px;
  padding: 12px;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  background: var(--surface);
}

.metric-block-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.metric-block-title-group {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.metric-block-title {
  font-size: 0.86rem;
  font-weight: 800;
}

.metric-log-toggle.active {
  border-color: var(--primary);
  background: var(--primary-soft);
  color: var(--primary);
}

.chart-stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 7px;
}

.chart-stat {
  padding: 9px 10px;
  display: grid;
  gap: 3px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
}

.chart-stat-title {
  font-size: 0.59rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-soft);
  font-weight: 700;
}

.chart-stat-value {
  font-size: 1.05rem;
  font-weight: 800;
  font-family: var(--font-mono);
}

.chart-stat.is-min {
  border-color: var(--primary);
}

.chart-stat.is-max {
  border-color: var(--danger);
}

.chart-stat.is-latest {
  border-color: var(--success);
  background: var(--success-soft);
}

@media (max-width: 520px) {
  .chart-stats {
    gap: 5px;
  }

  .chart-stat {
    padding: 7px 6px;
  }

  .chart-stat-title {
    font-size: 0.52rem;
    letter-spacing: 0.05em;
  }

  .chart-stat-value {
    font-size: 0.82rem;
    word-break: break-word;
  }
}

.metric-graph {
  width: 100%;
  height: 96px;
  display: block;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.metric-graph-line {
  fill: none;
  stroke: var(--primary);
  stroke-width: 1.6;
  vector-effect: non-scaling-stroke;
}

.metric-graph-fill {
  fill: var(--primary);
  opacity: 0.12;
  stroke: none;
}

.history-table-wrap {
  max-height: 260px;
  overflow-y: auto;
  overflow-x: auto;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}

.history-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.78rem;
}

.history-table th,
.history-table td {
  padding: 7px 10px;
  text-align: left;
  white-space: nowrap;
}

.history-table thead th {
  position: sticky;
  top: 0;
  background: var(--surface-2);
  color: var(--text-soft);
  font-size: 0.62rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-weight: 700;
}

.history-table tbody tr:nth-child(odd) {
  background: var(--surface-2);
}

.history-table td:not(:first-child) {
  font-family: var(--font-mono);
}

/* Toasts */

.toast-stack {
  position: fixed;
  right: 14px;
  bottom: calc(14px + var(--safe-area-bottom));
  z-index: 1030;
  display: grid;
  gap: 8px;
  width: min(360px, calc(100vw - 26px));
  pointer-events: none;
}

.toast-item {
  pointer-events: auto;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--surface);
  box-shadow: var(--shadow-md);
  padding: 10px 11px;
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 9px;
}

.toast-item .toast-icon {
  font-size: 0.94rem;
  margin-top: 1px;
}

.toast-item .toast-title {
  font-size: 0.79rem;
  font-weight: 800;
}

.toast-item .toast-message {
  margin-top: 2px;
  font-size: 0.73rem;
  color: var(--text-dim);
}

.toast-item button {
  min-height: 30px;
  padding: 4px 7px;
  border-radius: var(--radius-sm);
  background: transparent;
}

.toast-item.toast-success {
  border-color: var(--success);
}

.toast-item.toast-success .toast-icon {
  color: var(--success);
}

.toast-item.toast-error {
  border-color: var(--danger);
}

.toast-item.toast-error .toast-icon {
  color: var(--danger);
}

.toast-item.toast-info {
  border-color: var(--primary);
}

.toast-item.toast-info .toast-icon {
  color: var(--primary);
}

/* Web mode: >=1024px gets a sidebar, no bottom nav, wider layout */

@media (min-width: 1024px) {
  .app-shell {
    padding:
      calc(16px + var(--safe-area-top))
      calc(16px + var(--safe-area-right))
      calc(18px + var(--safe-area-bottom))
      calc(16px + var(--safe-area-left));
  }

  .panel {
    padding: 18px;
  }

  .schedule-form,
  .automation-form,
  .sensor-automation-form {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (min-width: 720px) and (max-width: 1023px) {
  .schedule-form,
  .automation-form,
  .sensor-automation-form {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

/* Tablets and up: all relays fit in a single row (matches the fixed
   10-relay board), instead of wrapping into multiple rows like the
   4-per-row phone layout. Names wrap to as many lines as needed rather
   than truncating, so tiles grow to fit whichever relay has the longest
   name in that grid row. */
@media (max-width: 719px) {
  .relay-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
  }

  .relay-tile {
    padding: 8px;
    min-height: 72px;
    gap: 5px;
  }

  .relay-name {
    font-size: 0.66rem;
  }

  .badge.tiny {
    font-size: 0.52rem;
  }

  .status-badge {
    font-size: 9px;
    padding: 2px 5px;
  }
}

@media (min-width: 720px) {
  .relay-grid {
    grid-template-columns: repeat(8, minmax(0, 1fr));
    gap: 6px;
  }

  .relay-tile {
    padding: 10px;
    min-height: 92px;
    gap: 6px;
  }

  .relay-name {
    font-size: 0.76rem;
  }
}

/* Wall-mounted / big-monitor dashboards */
@media (min-width: 1600px) {
  .app-shell {
    max-width: 1800px;
    margin: 0 auto;
  }

  .sensor-grid {
    grid-template-columns: repeat(auto-fit, minmax(340px, 1fr));
  }
}

/* Touchscreens (phones, tablets, POS terminals like Posiflex) need bigger
   tap targets than a mouse pointer does, regardless of screen size. */
@media (pointer: coarse) {
  :root {
    --control-height: 46px;
  }
}

@media (max-width: 860px) {
  .schedule-modal-body,
  .sensor-automation-body {
    grid-template-columns: 1fr;
  }

  .schedule-form,
  .automation-form,
  .sensor-automation-form {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .field.full {
    grid-column: 1 / -1;
  }

  .weekday-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 460px) {
  h1 {
    font-size: 0.92rem;
  }

  .status-pill {
    min-width: 0;
    width: 34px;
    height: 34px;
    padding: 0;
    justify-content: center;
    border-radius: var(--radius-sm);
  }

  .status-pill span:last-child {
    display: none;
  }

  .mini {
    min-height: 36px;
    font-size: 0.68rem;
  }
}

body.touch-compact .relay-grid {
  gap: 6px;
}

body.wall-mode .relay-grid {
  gap: 10px;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
