:root {
  --emerald-dark: #0b3b2e;
  --emerald-mid: #145a46;
  --emerald-bright: #1f7a5c;
  --emerald-light: #e6f2ee;
  --canvas-bg: #f3f6f5;
  --surface: #ffffff;
  --surface-tint: #f8fbfa;
  --glass: rgba(255, 255, 255, 0.88);
  --border: #d2dfda;
  --text-main: #0f241d;
  --text-muted: #4b655b;
  --accent-orange: #d95314;
  --accent-green: #15803d;
  --slider-track: #b4c7c0;
  --target-size: 36px;
}

@media (pointer: coarse), (max-width: 900px) {
  :root {
    --target-size: 44px;
  }
}

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  width: 100%;
  height: 100dvh;
  overflow: hidden;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  background: var(--canvas-bg);
  color: var(--text-main);
  overflow-wrap: break-word;
  -webkit-tap-highlight-color: transparent;
}

body {
  display: flex;
  flex-direction: column;
}

h1, h2, h3 { text-wrap: balance; }
p, li { text-wrap: pretty; }

button:focus-visible,
input[type="range"]:focus-visible {
  outline: 2px solid var(--emerald-bright);
  outline-offset: 2px;
}

.hidden { display: none !important; }

/* Loading Overlay */
.loader-overlay {
  position: fixed;
  inset: 0;
  background: radial-gradient(circle at 30% 25%, #125240 0%, var(--emerald-dark) 70%);
  color: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  z-index: 100;
  padding: 24px;
  text-align: center;
}
.loader-badge {
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 1.4px;
  color: #86efac;
  border: 1px solid rgba(134, 239, 172, 0.35);
  padding: 4px 10px;
  border-radius: 999px;
}
.loader-title { font-size: clamp(1.05rem, 0.9rem + 0.8vw, 1.35rem); font-weight: 700; }
.loader-sub { font-size: 0.85rem; color: #ccece0; }
.progress-track {
  width: min(320px, 82vw);
  height: 8px;
  background: rgba(255, 255, 255, 0.18);
  border-radius: 999px;
  overflow: hidden;
}
.progress-fill {
  width: 0%;
  height: 100%;
  background: linear-gradient(90deg, #34d399, #86efac);
  transition: width 0.15s ease;
}

/* Header */
.topbar {
  flex: 0 0 auto;
  background: linear-gradient(135deg, var(--emerald-dark) 0%, #0f4c3b 100%);
  color: #fff;
  padding: max(8px, env(safe-area-inset-top, 0px)) max(14px, env(safe-area-inset-right, 0px)) 8px max(14px, env(safe-area-inset-left, 0px));
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 14px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
  box-shadow: 0 2px 12px rgba(11, 59, 46, 0.22);
  z-index: 20;
}

.brand { min-inline-size: 0; }
.brand-row { display: flex; align-items: center; gap: 8px; }
.brand-mark {
  display: inline-grid;
  place-items: center;
  width: 26px;
  height: 26px;
  border-radius: 7px;
  background: rgba(134, 239, 172, 0.18);
  border: 1px solid rgba(134, 239, 172, 0.45);
  color: #86efac;
  font-size: 0.78rem;
  font-weight: 800;
  flex: none;
}
.brand-title {
  font-size: clamp(0.9rem, 0.82rem + 0.35vw, 1.08rem);
  font-weight: 700;
  letter-spacing: 0.15px;
  line-height: 1.2;
}
.brand-sub {
  font-size: 0.74rem;
  color: #b7dfd0;
  margin-top: 2px;
}

/* Single-Row Scrollable Tabs */
.tab-bar {
  display: flex;
  flex-wrap: nowrap;
  gap: 4px;
  background: rgba(0, 0, 0, 0.22);
  padding: 4px;
  border-radius: 10px;
  overflow-x: auto;
  scroll-snap-type: inline proximity;
  scrollbar-width: none;
}
.tab-bar::-webkit-scrollbar { display: none; }

.tab-btn {
  position: relative;
  flex: none;
  scroll-snap-align: start;
  border: 0;
  background: transparent;
  color: #d4ebe2;
  padding: 6px 12px;
  min-block-size: var(--target-size);
  border-radius: 7px;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s;
}
.tab-btn:hover { background: rgba(255, 255, 255, 0.1); color: #fff; }
.tab-btn.active {
  background: #fff;
  color: var(--emerald-dark);
}
/* Non-color active indicator (survives forced-colors mode) */
.tab-btn.active::after {
  content: "";
  position: absolute;
  inset: auto 10px 3px;
  border-block-end: 3px solid var(--emerald-bright);
  border-radius: 2px;
}

/* Camera Presets */
.cam-bar {
  display: flex;
  flex-wrap: nowrap;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
}
.cam-bar::-webkit-scrollbar { display: none; }

.chip-btn {
  flex: none;
  border: 1px solid rgba(255, 255, 255, 0.26);
  background: rgba(255, 255, 255, 0.08);
  color: #edf7f3;
  padding: 5px 11px;
  min-block-size: var(--target-size);
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s;
}
.chip-btn:hover,
.chip-btn.active {
  background: var(--emerald-bright);
  border-color: #86efac;
  color: #fff;
}

/* Main Workspace */
.workspace {
  flex: 1 1 0;
  min-height: 0;
  display: flex;
  width: 100%;
  position: relative;
}

.canvas-wrap {
  position: relative;
  flex: 1 1 0;
  min-width: 0;
  min-height: 0;
  background: radial-gradient(circle at 38% 32%, #ffffff 0%, #eaf0ee 100%);
  overflow: hidden;
}

#three-canvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

/* Top HUD Row (Telemetry + Mobile Info Button) */
.hud-top {
  position: absolute;
  top: 10px;
  left: 10px;
  right: 10px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  pointer-events: none;
  z-index: 11;
}

.telemetry-hud {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-inline-size: 0;
  max-width: min(560px, 100%);
}

.status-pill {
  background: var(--glass);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border);
  border-left: 4px solid var(--emerald-dark);
  padding: 6px 11px;
  border-radius: 9px;
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--text-main);
  box-shadow: 0 3px 12px rgba(11, 59, 46, 0.08);
  width: fit-content;
  max-width: 100%;
}

.telemetry-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 10px;
  background: rgba(11, 59, 46, 0.88);
  backdrop-filter: blur(10px);
  color: #e6f5ef;
  padding: 5px 10px;
  border-radius: 8px;
  font-size: 0.71rem;
  box-shadow: 0 3px 10px rgba(11, 59, 46, 0.14);
  width: fit-content;
}
.telem-item { display: inline-flex; align-items: baseline; gap: 4px; white-space: nowrap; }
.telem-k { color: #9dd4c0; text-transform: uppercase; font-size: 0.64rem; letter-spacing: 0.4px; }
.telem-item strong { color: #fff; font-weight: 700; font-variant-numeric: tabular-nums; }

.mobile-info-fab {
  display: none;
  pointer-events: auto;
  flex: none;
  background: var(--emerald-dark);
  color: #fff;
  border: 1px solid rgba(134, 239, 172, 0.4);
  padding: 0 13px;
  min-block-size: var(--target-size);
  border-radius: 999px;
  font-size: 0.76rem;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(11, 59, 46, 0.25);
  white-space: nowrap;
}

/* Floating Bottom Control Dock */
.control-dock {
  position: absolute;
  bottom: max(12px, env(safe-area-inset-bottom, 0px));
  left: 50%;
  transform: translateX(-50%);
  background: var(--glass);
  backdrop-filter: blur(12px);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 7px 10px;
  box-shadow: 0 8px 26px rgba(11, 59, 46, 0.15);
  max-width: calc(100% - 20px);
  z-index: 12;
}

.ctrl-group {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.ctrl-cluster {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 5px;
}

.dock-sep {
  width: 1px;
  height: 24px;
  background: var(--border);
  margin: 0 2px;
}

.ctrl-btn {
  border: 1px solid var(--border);
  background: #f5f8f7;
  color: var(--text-main);
  padding: 6px 11px;
  min-block-size: var(--target-size);
  border-radius: 9px;
  font-size: 0.77rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.14s, color 0.14s, border-color 0.14s;
}
.ctrl-btn:hover { background: var(--emerald-light); border-color: #a9ccc0; }
.ctrl-btn.active {
  background: var(--emerald-dark);
  color: #fff;
  border-color: var(--emerald-dark);
}
.ctrl-btn.mode-peak.active { background: var(--accent-orange); border-color: var(--accent-orange); }
.ctrl-btn.mode-scram.active { background: var(--accent-green); border-color: var(--accent-green); }

/* Brand-Consistent Native Range Slider */
.slider-box {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.76rem;
  font-weight: 700;
  color: var(--text-main);
  padding: 0 6px;
  background: #f3f7f5;
  border: 1px solid var(--border);
  border-radius: 9px;
}
.slider-box label { white-space: nowrap; }
.slider-val {
  min-width: 44px;
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: var(--emerald-dark);
}

.slider-box input[type="range"] {
  --slider-track-size: 0.42rem;
  --slider-thumb-size: 1.15rem;
  appearance: none;
  accent-color: var(--emerald-dark);
  inline-size: 118px;
  block-size: var(--target-size);
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
}
/* WebKit/Blink (separate block from Firefox so browsers never drop the rule) */
.slider-box input[type="range"]::-webkit-slider-runnable-track {
  block-size: var(--slider-track-size);
  border-radius: 999px;
  background: var(--slider-track);
}
.slider-box input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  inline-size: var(--slider-thumb-size);
  block-size: var(--slider-thumb-size);
  margin-block-start: calc((var(--slider-track-size) - var(--slider-thumb-size)) / 2);
  border: 2px solid #fff;
  border-radius: 50%;
  background: var(--emerald-dark);
  box-shadow: 0 1px 4px rgba(11, 59, 46, 0.35);
}
/* Firefox */
.slider-box input[type="range"]::-moz-range-track {
  block-size: var(--slider-track-size);
  border: 0;
  border-radius: 999px;
  background: var(--slider-track);
}
.slider-box input[type="range"]::-moz-range-progress {
  block-size: var(--slider-track-size);
  border-radius: 999px;
  background: var(--emerald-dark);
}
.slider-box input[type="range"]::-moz-range-thumb {
  inline-size: var(--slider-thumb-size);
  block-size: var(--slider-thumb-size);
  border: 2px solid #fff;
  border-radius: 50%;
  background: var(--emerald-dark);
  box-shadow: 0 1px 4px rgba(11, 59, 46, 0.35);
}

/* Side Inspector (Desktop) / Bottom Sheet (Mobile) */
.inspector {
  flex: 0 0 350px;
  height: 100%;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  container: inspector / inline-size;
  z-index: 15;
}

.sheet-handle { display: none; }

.insp-header {
  flex: 0 0 auto;
  background: var(--emerald-light);
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.insp-header h2 { font-size: 0.9rem; color: var(--emerald-dark); }

.close-sheet-btn {
  display: none;
  border: 0;
  background: var(--emerald-dark);
  color: #fff;
  min-inline-size: var(--target-size);
  min-block-size: var(--target-size);
  border-radius: 10px;
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
}

.insp-body {
  flex: 1 1 0;
  min-height: 0;
  overflow-y: auto;
  padding: 12px 14px max(14px, env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.insp-section { display: flex; flex-direction: column; gap: 6px; }

.badge {
  display: inline-block;
  background: var(--emerald-dark);
  color: #fff;
  font-size: 0.69rem;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 999px;
  width: fit-content;
}

.card {
  border: 1px solid var(--border);
  border-radius: 11px;
  padding: 11px 12px;
  background: var(--surface-tint);
}
.detail-title { font-size: 0.96rem; margin: 7px 0 5px; color: var(--text-main); }
.detail-desc { font-size: 0.79rem; color: var(--text-muted); line-height: 1.48; margin-bottom: 10px; }

.spec-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  border-top: 1px solid var(--border);
  padding-top: 9px;
}
@container inspector (inline-size > 380px) {
  .spec-grid { grid-template-columns: 1fr 1fr; }
  .spec-full { grid-column: 1 / -1; }
}

.card-label {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.45px;
  color: var(--text-muted);
  font-weight: 700;
  margin-bottom: 2px;
}
.card-val { font-size: 0.82rem; font-weight: 600; line-height: 1.38; color: var(--text-main); }

/* Hotspot Pills */
.hotspot-pills { display: flex; flex-wrap: wrap; gap: 5px; }
.hs-pill {
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text-main);
  padding: 5px 9px;
  min-block-size: 32px;
  border-radius: 8px;
  font-size: 0.73rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
}
.hs-pill:hover { background: var(--emerald-light); }
.hs-pill.active {
  background: var(--emerald-dark);
  color: #fff;
  border-color: var(--emerald-dark);
}

/* System Groups List (Tab 3) */
.groups-section,
.groups-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.group-card {
  border: 1px solid var(--border);
  border-left: 4px solid var(--emerald-dark);
  border-radius: 10px;
  padding: 9px 11px;
  background: #fff;
  cursor: pointer;
  transition: background 0.14s, border-color 0.14s;
}
.group-card:hover { background: var(--surface-tint); }
.group-card.active { background: var(--emerald-light); border-color: var(--emerald-bright); }
.group-card h4 { font-size: 0.83rem; margin-bottom: 2px; color: var(--text-main); }
.group-card p { font-size: 0.73rem; color: var(--text-muted); margin-bottom: 5px; }
.group-card ul { padding-left: 16px; font-size: 0.74rem; line-height: 1.42; color: var(--text-main); }

/* 3-Step Thermal Flow Legend */
.flow-legend {
  margin-top: auto;
  border-top: 1px solid var(--border);
  padding-top: 10px;
}
.flow-steps { display: grid; gap: 5px; margin-top: 6px; }
.flow-step { padding: 6px 9px; border-radius: 7px; font-size: 0.74rem; font-weight: 600; }
.flow-step.ni { background: #fdf0ed; border: 1px solid #f5c6bc; color: #7c2d12; }
.flow-step.ei { background: #eaf7ef; border: 1px solid #b5e4c7; color: #0b3b2e; }

/* Mobile Portrait & Tablet Layout (<= 900px) */
@media (max-width: 900px) {
  .topbar {
    padding: max(6px, env(safe-area-inset-top, 0px)) 10px 7px;
    gap: 6px;
  }
  .brand-sub { display: none; }
  .tab-bar { width: 100%; }
  .tab-btn { font-size: 0.76rem; padding: 6px 11px; }
  .cam-bar { width: 100%; }

  .hud-top { top: 8px; left: 8px; right: 8px; }
  .status-pill { font-size: 0.72rem; padding: 5px 9px; }
  .telemetry-strip { padding: 4px 8px; gap: 3px 8px; font-size: 0.65rem; }
  .telem-k { font-size: 0.6rem; }
  .mobile-info-fab { display: inline-flex; align-items: center; }

  .control-dock {
    bottom: max(8px, env(safe-area-inset-bottom, 0px));
    left: 8px;
    right: 8px;
    transform: none;
    max-width: none;
    padding: 6px 8px;
    border-radius: 12px;
  }
  .ctrl-group {
    flex-wrap: nowrap;
    justify-content: flex-start;
    overflow-x: auto;
    scrollbar-width: none;
  }
  .ctrl-group::-webkit-scrollbar { display: none; }
  .ctrl-cluster { flex-wrap: nowrap; }
  .dock-sep { display: none; }
  .ctrl-btn { font-size: 0.75rem; padding: 6px 11px; }
  .slider-box { flex: none; }
  .slider-box input[type="range"] { inline-size: 96px; }

  .hs-pill { min-block-size: var(--target-size); padding: 6px 11px; }

  /* Collapsible Bottom Sheet */
  .inspector {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: min(62dvh, 520px);
    border-left: 0;
    border-top: 2px solid var(--emerald-dark);
    border-radius: 18px 18px 0 0;
    box-shadow: 0 -10px 32px rgba(11, 59, 46, 0.24);
    transform: translateY(105%);
    transition: transform 0.24s cubic-bezier(0.22, 1, 0.36, 1);
    z-index: 30;
  }
  .inspector.mobile-open { transform: translateY(0); }
  .sheet-handle {
    display: block;
    width: 40px;
    height: 4px;
    border-radius: 999px;
    background: #b9cdc5;
    margin: 7px auto 2px;
    flex: none;
  }
  .close-sheet-btn { display: inline-flex; align-items: center; justify-content: center; }
}
