/* ---------- Design tokens ---------- */
:root {
  color-scheme: dark;

  --page-plane:      #100c0a;
  --surface-1:       rgba(30, 23, 20, 0.70);
  --surface-2:       rgba(255, 238, 214, 0.045);
  --text-primary:    #f4ead9;
  --text-secondary:  #c8b8a4;
  --text-muted:      #8c7c6a;
  --border:          rgba(201, 162, 90, 0.16);
  --gridline:        #2b221b;

  /* sequential (chart line) — validated: contrast ≥3:1 vs surface in both modes */
  --series-1:        #d1a54c;
  --series-1-wash:   rgba(209, 165, 76, 0.14);

  /* status — fixed, never themed */
  --status-good:     #0ca30c;
  --status-warning:  #fab219;
  --status-serious:  #ec835a;
  --status-critical: #d03b3b;

  /* decorative brand accents only — never used to encode data */
  --wine-500:        #6d1c37;
  --wine-400:        #9c2c52;
  --wine-glow:       rgba(109, 28, 55, 0.38);
  --gold-300:        #ecdcae;
  --gold-400:        #d9bc6c;
  --gold-line:       rgba(201, 162, 90, 0.4);

  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 8px;
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-display: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
}

@media (prefers-color-scheme: light) {
  :root {
    color-scheme: light;
    --page-plane:     #f8f2e8;
    --surface-1:      rgba(255, 255, 255, 0.78);
    --surface-2:      rgba(70, 45, 20, 0.035);
    --text-primary:   #241a12;
    --text-secondary: #5c4c3c;
    --text-muted:     #8c7c6a;
    --border:         rgba(120, 80, 30, 0.15);
    --gridline:        #e7dcc7;
    --series-1:       #a97c1f;
    --series-1-wash:  rgba(169, 124, 31, 0.10);
    --wine-glow:      rgba(109, 28, 55, 0.12);
    --gold-line:      rgba(120, 80, 30, 0.3);
  }
}

* { box-sizing: border-box; }

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

body {
  font-family: var(--font);
  background: var(--page-plane);
  color: var(--text-primary);
  position: relative;
}

/* ---------- Ambient background ---------- */
.ambient {
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  background:
    radial-gradient(680px 420px at 12% -8%, var(--wine-glow), transparent 60%),
    radial-gradient(620px 420px at 100% 8%, rgba(217, 188, 108, 0.08), transparent 62%),
    radial-gradient(900px 600px at 50% 110%, rgba(0, 0, 0, 0.35), transparent 60%);
}

/* fine grain — keeps the dark plane from reading flat/digital */
.ambient::after {
  content: "";
  position: absolute;
  inset: 0;
  opacity: 0.05;
  mix-blend-mode: overlay;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='120' height='120'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>");
}

@media (prefers-color-scheme: light) {
  .ambient::after { opacity: 0.035; }
}

/* ---------- Header ---------- */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.5rem 1.75rem 1rem;
  max-width: 960px;
  margin: 0 auto;
}

.header-right { display: flex; align-items: center; gap: 0.75rem; }

.brand { display: flex; align-items: center; gap: 0.75rem; }
.brand-mark { color: var(--gold-400); flex-shrink: 0; filter: drop-shadow(0 0 6px rgba(217, 188, 108, 0.25)); }
header h1 {
  font-family: var(--font-display);
  font-size: 1.55rem;
  font-weight: 500;
  margin: 0;
  letter-spacing: 0.01em;
  background: linear-gradient(120deg, var(--text-primary) 35%, var(--gold-400) 120%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.brand-sub {
  margin: 0.15rem 0 0;
  font-size: 0.76rem;
  color: var(--text-muted);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.status {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.8rem;
  padding: 0.35rem 0.7rem 0.35rem 0.55rem;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-secondary);
}
.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
}
.status-connected .status-dot { background: var(--status-good); box-shadow: 0 0 0 3px color-mix(in srgb, var(--status-good) 25%, transparent); animation: pulse 2.4s ease-in-out infinite; }
.status-disconnected .status-dot { background: var(--status-critical); }
.status-connecting .status-dot { background: var(--status-warning); animation: pulse 1.2s ease-in-out infinite; }

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 currentColor; opacity: 1; }
  50% { opacity: 0.55; }
}

/* ---------- Layout ---------- */
main {
  padding: 0.5rem 1.75rem 2.5rem;
  max-width: 960px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

/* ---------- Hero (CO2) ---------- */
.hero {
  position: relative;
  background: var(--surface-1);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.75rem 1.75rem 1.25rem;
  overflow: hidden;
  transition: border-color 0.4s ease;
}

.hero::after {
  content: "";
  position: absolute;
  top: 0; left: 1.75rem; right: 1.75rem;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--gold-line), transparent);
}

.hero::before {
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(360px 200px at 100% 0%, var(--hero-glow, rgba(217, 188, 108, 0.08)), transparent 70%);
  pointer-events: none;
  transition: background 0.6s ease;
}

.hero-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;
}

.hero-label {
  font-size: 0.85rem;
  color: var(--text-secondary);
  font-weight: 550;
}

.hero-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.78rem;
  font-weight: 600;
  padding: 0.3rem 0.65rem;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--badge-color, var(--text-secondary));
}
.hero-badge-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: currentColor;
}

.hero-value {
  position: relative;
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  margin: 0.35rem 0 0.15rem;
  font-variant-numeric: proportional-nums;
}
.hero-value span:first-child {
  font-size: clamp(3rem, 9vw, 4.25rem);
  font-weight: 650;
  line-height: 1;
  letter-spacing: -0.02em;
  transition: color 0.4s ease;
}
.hero-unit {
  font-size: 1.1rem;
  color: var(--text-muted);
  font-weight: 500;
}

.sparkline-hero-wrap { position: relative; width: 100%; height: 46px; margin-top: 0.5rem; opacity: 0.9; }
.hero-meta { font-size: 0.78rem; color: var(--text-muted); margin-top: 0.35rem; }

/* status-driven hero coloring — icon+label pairing carries meaning, color is reinforcement */
.hero[data-level="good"]      { --hero-glow: rgba(12, 163, 12, 0.14); }
.hero[data-level="warning"]   { --hero-glow: rgba(250, 178, 25, 0.16); border-color: color-mix(in srgb, var(--status-warning) 35%, var(--border)); }
.hero[data-level="critical"]  { --hero-glow: rgba(208, 59, 59, 0.20); border-color: color-mix(in srgb, var(--status-critical) 45%, var(--border)); animation: heroDanger 1.8s ease-in-out infinite; }
.hero[data-level="good"] .hero-value span:first-child      { color: var(--text-primary); }
.hero[data-level="warning"] .hero-value span:first-child   { color: var(--status-warning); }
.hero[data-level="critical"] .hero-value span:first-child  { color: var(--status-critical); }
.hero[data-level="good"]      .hero-badge { --badge-color: var(--status-good); background: color-mix(in srgb, var(--status-good) 14%, var(--surface-2)); }
.hero[data-level="warning"]   .hero-badge { --badge-color: var(--status-warning); background: color-mix(in srgb, var(--status-warning) 16%, var(--surface-2)); }
.hero[data-level="critical"]  .hero-badge { --badge-color: var(--status-critical); background: color-mix(in srgb, var(--status-critical) 18%, var(--surface-2)); }

@keyframes heroDanger {
  0%, 100% { box-shadow: 0 0 0 0 rgba(208, 59, 59, 0.0); }
  50% { box-shadow: 0 0 0 6px rgba(208, 59, 59, 0.10); }
}

/* ---------- Stat tiles ---------- */
.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem;
}

.tile {
  background: var(--surface-1);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 1.1rem 1.25rem;
  transition: border-color 0.3s ease, transform 0.3s ease;
}
.tile:hover { border-color: var(--gold-line); transform: translateY(-1px); }

.tile-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.tile-label { color: var(--text-secondary); font-size: 0.85rem; font-weight: 550; }
.sparkline { flex-shrink: 0; opacity: 0.85; }

.tile-value {
  font-variant-numeric: proportional-nums;
  font-size: 1.85rem;
  font-weight: 650;
  letter-spacing: -0.01em;
  margin-top: 0.4rem;
}
.tile-unit { font-size: 0.95rem; color: var(--text-muted); margin-left: 0.3rem; font-weight: 500; }
.tile-updated { color: var(--text-muted); font-size: 0.72rem; margin-top: 0.35rem; }

/* ---------- Chart ---------- */
.chart-section {
  position: relative;
  background: var(--surface-1);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.25rem 1.25rem 1rem;
  overflow: hidden;
}
.chart-section::before {
  content: "";
  position: absolute;
  top: 0; left: 1.25rem; right: 1.25rem;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--gold-line), transparent);
}

.chart-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin-bottom: 0.85rem;
}

.chart-controls { display: flex; gap: 0.75rem; flex-wrap: wrap; }

.select-wrap { display: flex; flex-direction: column; gap: 0.2rem; }
.select-wrap label { font-size: 0.7rem; color: var(--text-muted); }
.select-wrap select {
  background: var(--surface-2);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.4rem 0.6rem;
  font-family: var(--font);
  font-size: 0.85rem;
}

.ghost-button {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.45rem 0.9rem;
  font-size: 0.8rem;
  font-family: var(--font);
  cursor: pointer;
  transition: background 0.2s ease, color 0.2s ease;
}
.ghost-button:hover { background: var(--surface-2); color: var(--text-primary); }
.ghost-button[aria-pressed="true"] { background: color-mix(in srgb, var(--series-1) 16%, var(--surface-2)); color: var(--series-1); border-color: color-mix(in srgb, var(--series-1) 40%, var(--border)); }

.chart-canvas-wrap { width: 100%; }

.section-title { margin: 0; font-family: var(--font-display); font-size: 1.1rem; font-weight: 500; letter-spacing: 0.01em; }
.section-sub { margin: 0.3rem 0 0; font-size: 0.78rem; color: var(--text-muted); }

.commission-form {
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
}
.commission-form input {
  flex: 1;
  min-width: 220px;
  background: var(--surface-2);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.55rem 0.75rem;
  font-family: var(--font);
  font-size: 0.85rem;
}
.commission-form input::placeholder { color: var(--text-muted); }

.commission-result {
  margin: 0.75rem 0 0;
  font-size: 0.82rem;
  color: var(--text-muted);
  min-height: 1.1em;
}
.commission-result.pending { color: var(--status-warning); }
.commission-result.success { color: var(--status-good); }
.commission-result.error { color: var(--status-critical); }

.table-view { margin-top: 0.75rem; max-height: 320px; overflow: auto; border-top: 1px solid var(--border); }
.table-view table { width: 100%; border-collapse: collapse; font-size: 0.82rem; }
.table-view th {
  text-align: left;
  color: var(--text-muted);
  font-weight: 550;
  padding: 0.5rem 0.4rem;
  position: sticky;
  top: 0;
  background: var(--page-plane);
}
.table-view td {
  padding: 0.4rem 0.4rem;
  border-top: 1px solid var(--gridline);
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}

/* ---------- Alert banner ---------- */
.alert-banner {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  background: var(--status-critical);
  color: white;
  text-align: center;
  padding: 0.75rem;
  font-weight: 600;
  font-size: 0.9rem;
  animation: flash 1.1s ease-in-out infinite;
}
.alert-banner.hidden { display: none; }

@keyframes flash {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.75; }
}

/* ---------- Footer ---------- */
footer {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.72rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 1rem 1.75rem 2rem;
}

.hidden { display: none !important; }

/* ---------- Lighting ---------- */
.lamp-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 0.9rem;
  margin-bottom: 1rem;
}

.lamp-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 0.9rem 1rem;
  transition: border-color 0.25s ease, transform 0.25s ease;
}
.lamp-card:hover { border-color: var(--gold-line); transform: translateY(-1px); }

.lamp-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.lamp-name { font-weight: 600; font-size: 0.95rem; }
.lamp-kind { display: block; color: var(--text-muted); font-size: 0.72rem; margin-top: 0.15rem; text-transform: uppercase; letter-spacing: 0.04em; }

.lamp-controls {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-top: 0.75rem;
}
.lamp-controls input[type="color"] {
  width: 36px;
  height: 28px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: none;
}
.lamp-controls input[type="range"] { flex: 1; accent-color: var(--series-1); }

.lamp-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 0.75rem;
  gap: 0.5rem;
}

.checkbox-label {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.78rem;
  color: var(--text-secondary);
}

.switch { position: relative; display: inline-block; width: 40px; height: 22px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch-slider {
  position: absolute; inset: 0; cursor: pointer;
  background: var(--border);
  border-radius: 999px;
  transition: background 0.2s ease;
}
.switch-slider::before {
  content: "";
  position: absolute;
  height: 16px; width: 16px;
  left: 3px; bottom: 3px;
  background: white;
  border-radius: 50%;
  transition: transform 0.2s ease;
}
.switch input:checked + .switch-slider { background: var(--series-1); }
.switch input:checked + .switch-slider::before { transform: translateX(18px); }

.test-alert-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.6rem;
  margin-top: 0.9rem;
  padding-top: 0.9rem;
  border-top: 1px solid var(--border);
}
.test-alert-row .section-sub { margin: 0; }
.test-alert-buttons { display: flex; gap: 0.5rem; }
.test-alert-buttons button:disabled { opacity: 0.6; cursor: default; }

.add-lamp-details { margin-top: 0.5rem; }
.add-lamp-details summary {
  cursor: pointer;
  color: var(--text-secondary);
  font-size: 0.85rem;
  padding: 0.4rem 0;
}

.add-lamp-form {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: end;
  margin-top: 0.75rem;
}
.add-lamp-form .select-wrap input {
  background: var(--surface-2);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.4rem 0.6rem;
  font-family: var(--font);
  font-size: 0.85rem;
}

.schedule-list { display: flex; flex-direction: column; gap: 0.5rem; margin-bottom: 0.5rem; }
.schedule-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.5rem 0.75rem;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  color: var(--text-secondary);
}

/* ---------- Login page ---------- */
.login-main {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}
.login-card {
  position: relative;
  background: var(--surface-1);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 2rem;
  width: 100%;
  max-width: 320px;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  box-shadow: 0 24px 60px -24px rgba(0, 0, 0, 0.5);
}
.login-card::before {
  content: "";
  position: absolute;
  top: 0; left: 2rem; right: 2rem;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--gold-line), transparent);
}
.login-card label { font-size: 0.8rem; color: var(--text-secondary); margin-top: 0.5rem; }
.login-card input {
  background: var(--surface-2);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.55rem 0.75rem;
  font-family: var(--font);
  font-size: 0.9rem;
}
.login-submit {
  margin-top: 1rem;
  background: linear-gradient(135deg, var(--wine-500), var(--wine-400));
  border-color: transparent;
  color: #f8ecd8;
  font-weight: 600;
  box-shadow: 0 6px 18px -6px rgba(109, 28, 55, 0.55);
  transition: filter 0.2s ease, transform 0.2s ease;
}
.login-submit:hover { filter: brightness(1.1); background: linear-gradient(135deg, var(--wine-500), var(--wine-400)); color: #f8ecd8; transform: translateY(-1px); }

/* ---------- Settings panel ---------- */
.icon-button { padding: 0.45rem; line-height: 0; }

.settings-overlay {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}
.settings-card {
  position: relative;
  background: var(--surface-1);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.5rem;
  width: 100%;
  max-width: 360px;
  box-shadow: 0 24px 60px -24px rgba(0, 0, 0, 0.5);
}
.settings-card::before {
  content: "";
  position: absolute;
  top: 0; left: 1.5rem; right: 1.5rem;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--gold-line), transparent);
}
.settings-card form { display: flex; flex-direction: column; gap: 0.4rem; }
.settings-card label { font-size: 0.8rem; color: var(--text-secondary); margin-top: 0.5rem; }
.settings-card input {
  background: var(--surface-2);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.55rem 0.75rem;
  font-family: var(--font);
  font-size: 0.9rem;
}

@media (max-width: 480px) {
  .hero-value span:first-child { font-size: 3rem; }
}
