:root {
  --bg-dark: #10241d;
  --glass-bg: rgba(16, 28, 34, 0.58);
  --glass-border: rgba(255, 255, 255, 0.22);
  --glass-strong: rgba(18, 28, 36, 0.88);
  --text-main: #f8fafc;
  --text-muted: rgba(241, 245, 249, 0.78);
  --calm-color: #34d399;
  --alert-color: #fbbf24;
  --scatter-color: #fb7185;
  --accent: #38bdf8;
  --radius-pill: 999px;
  --radius-card: 18px;
  --shadow-soft: 0 10px 30px rgba(0, 0, 0, 0.28);
  --font-ui: "Avenir Next", "Nunito", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg-dark);
  color: var(--text-main);
  font-family: var(--font-ui);
  user-select: none;
  -webkit-user-select: none;
}

.app {
  position: relative;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

#habitat-canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.hidden {
  display: none !important;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

/* ==================== Top HUD ==================== */
.hud-top {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  left: max(14px, env(safe-area-inset-left));
  right: max(14px, env(safe-area-inset-right));
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  z-index: 20;
  pointer-events: none;
  flex-wrap: wrap;
}

.hud-top > * {
  pointer-events: auto;
}

.meters-cluster {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.meters-cluster.meters-hidden {
  opacity: 0;
  pointer-events: none;
}

.meter-card {
  min-width: 175px;
  max-width: 240px;
  padding: 9px 13px 11px;
  border-radius: 16px;
  background: var(--glass-bg);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: var(--shadow-soft);
}

.meter-header {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 7px;
  font-size: 0.85rem;
  font-weight: 700;
  letter-spacing: 0.01em;
}

.meter-icon {
  font-size: 1rem;
  line-height: 1;
}

.meter-label {
  color: var(--text-main);
}

.meter-status-tag {
  margin-left: auto;
  font-size: 0.7rem;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: var(--radius-pill);
  background: rgba(255, 255, 255, 0.14);
  color: rgba(255, 255, 255, 0.9);
  white-space: nowrap;
}

.meter-status-tag.tag-alert {
  background: rgba(251, 191, 36, 0.28);
  color: #fde68a;
}

.meter-status-tag.tag-scatter {
  background: rgba(251, 113, 133, 0.32);
  color: #fecdd3;
}

.meter-status-tag.tag-off {
  opacity: 0.55;
}

.meter-track {
  position: relative;
  height: 11px;
  border-radius: var(--radius-pill);
  background: rgba(255, 255, 255, 0.16);
  overflow: visible;
}

.meter-zone {
  position: absolute;
  top: 0;
  bottom: 0;
  pointer-events: none;
  border-radius: 0;
}

.meter-zone-alert {
  background: rgba(251, 191, 36, 0.22);
  border-left: 2px dashed rgba(251, 191, 36, 0.75);
}

.meter-zone-scatter {
  background: rgba(251, 113, 133, 0.25);
  border-left: 2px solid rgba(251, 113, 133, 0.9);
  border-top-right-radius: var(--radius-pill);
  border-bottom-right-radius: var(--radius-pill);
}

.meter-fill {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 0%;
  border-radius: var(--radius-pill);
  background: linear-gradient(90deg, #34d399, #6ee7b7);
  transition: width 70ms linear, background 180ms ease;
}

.meter-fill.state-alert {
  background: linear-gradient(90deg, #f59e0b, #fcd34d);
}

.meter-fill.state-scatter {
  background: linear-gradient(90deg, #f43f5e, #fb7185);
}

.meter-baseline-tick {
  position: absolute;
  top: -3px;
  bottom: -3px;
  width: 3px;
  left: 10%;
  border-radius: 3px;
  background: #ffffff;
  box-shadow: 0 0 6px rgba(0, 0, 0, 0.55);
  transform: translateX(-50%);
  transition: left 250ms ease;
}

/* ==================== Status Cluster (Mood + Hushed Time) ==================== */
.status-cluster {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.hushed-pill {
  display: inline-flex;
  align-items: baseline;
  gap: 8px;
  padding: 9px 15px;
  border-radius: var(--radius-pill);
  background: var(--glass-bg);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: var(--shadow-soft);
}

.hushed-label {
  font-size: 0.76rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.hushed-value {
  font-size: 1rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: #a7f3d0;
}

/* ==================== Round Corner Gauge (Special Visitor) ==================== */
.special-gauge-card {
  position: fixed;
  right: max(16px, env(safe-area-inset-right));
  bottom: max(16px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  padding: 10px 12px 9px;
  border-radius: 22px;
  background: var(--glass-bg);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  box-shadow: var(--shadow-soft);
  z-index: 20;
  transition: border-color 300ms ease, box-shadow 300ms ease, transform 300ms ease;
}

.special-gauge-card.gauge-arrived {
  border-color: rgba(250, 204, 21, 0.85);
  box-shadow: 0 0 26px rgba(250, 204, 21, 0.35), var(--shadow-soft);
}

.gauge-ring-wrap {
  position: relative;
  width: 86px;
  height: 86px;
}

.gauge-svg {
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);
}

.gauge-bg {
  fill: none;
  stroke: rgba(255, 255, 255, 0.16);
  stroke-width: 8;
}

.gauge-progress {
  fill: none;
  stroke: #34d399;
  stroke-width: 8;
  stroke-linecap: round;
  stroke-dasharray: 263.89;
  stroke-dashoffset: 263.89;
  transition: stroke-dashoffset 220ms linear, stroke 250ms ease;
}

.special-gauge-card.gauge-arrived .gauge-progress {
  stroke: #facc15;
}

.gauge-center {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
}

.gauge-icon {
  font-size: 1.45rem;
  line-height: 1;
}

.gauge-time {
  font-size: 0.82rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--text-main);
}

.special-gauge-card.gauge-arrived .gauge-time {
  color: #fde047;
}

.gauge-caption {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--text-muted);
  text-align: center;
  max-width: 112px;
  line-height: 1.2;
}

/* ==================== Live Threshold Bar in Settings ==================== */
.live-threshold-bar {
  position: relative;
  height: 14px;
  border-radius: var(--radius-pill);
  background: rgba(255, 255, 255, 0.14);
  margin: 4px 0 2px;
  overflow: visible;
}

.live-bar-zone {
  position: absolute;
  top: 0;
  bottom: 0;
  pointer-events: none;
}

.live-bar-alert {
  background: rgba(251, 191, 36, 0.24);
  border-left: 2px dashed rgba(251, 191, 36, 0.75);
}

.live-bar-scatter {
  background: rgba(251, 113, 133, 0.28);
  border-top-right-radius: var(--radius-pill);
  border-bottom-right-radius: var(--radius-pill);
}

.live-bar-fill {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 0%;
  border-radius: var(--radius-pill);
  background: linear-gradient(90deg, #34d399, #6ee7b7);
  transition: width 60ms linear, background 150ms ease;
}

.live-bar-fill.state-alert {
  background: linear-gradient(90deg, #f59e0b, #fcd34d);
}

.live-bar-fill.state-scatter {
  background: linear-gradient(90deg, #f43f5e, #fb7185);
}

.live-bar-baseline {
  position: absolute;
  top: -2px;
  bottom: -2px;
  width: 2.5px;
  background: rgba(255, 255, 255, 0.85);
  border-radius: 2px;
  transform: translateX(-50%);
}

.live-bar-thumb-mark {
  position: absolute;
  top: -4px;
  bottom: -4px;
  width: 3.5px;
  background: #fb7185;
  border-radius: 3px;
  box-shadow: 0 0 8px rgba(251, 113, 133, 0.85);
  transform: translateX(-50%);
}

.secondary-btn.special-soft {
  border-color: rgba(250, 204, 21, 0.55);
  background: rgba(250, 204, 21, 0.16);
  color: #fef08a;
}

/* ==================== Mood Pill ==================== */
.mood-pill {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  padding: 10px 18px;
  border-radius: var(--radius-pill);
  background: var(--glass-bg);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: var(--shadow-soft);
  font-size: 0.98rem;
  font-weight: 800;
  letter-spacing: 0.01em;
  transition: transform 220ms ease, border-color 220ms ease;
}

.mood-dot {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: var(--calm-color);
  box-shadow: 0 0 10px var(--calm-color);
}

.mood-pill.mood-alert {
  border-color: rgba(251, 191, 36, 0.7);
}
.mood-pill.mood-alert .mood-dot {
  background: var(--alert-color);
  box-shadow: 0 0 12px var(--alert-color);
}

.mood-pill.mood-scatter {
  border-color: rgba(251, 113, 133, 0.85);
  transform: scale(1.04);
}
.mood-pill.mood-scatter .mood-dot {
  background: var(--scatter-color);
  box-shadow: 0 0 14px var(--scatter-color);
}

.mood-pill.mood-peeking {
  border-color: rgba(56, 189, 248, 0.75);
}
.mood-pill.mood-peeking .mood-dot {
  background: var(--accent);
  box-shadow: 0 0 12px var(--accent);
}

/* ==================== Action Buttons ==================== */
.hud-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 42px;
  padding: 0 13px;
  border-radius: var(--radius-pill);
  border: 1px solid var(--glass-border);
  background: var(--glass-bg);
  color: var(--text-main);
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: var(--shadow-soft);
  transition: transform 120ms ease, background 150ms ease;
}

.icon-btn:hover {
  background: rgba(30, 48, 56, 0.78);
}

.icon-btn:active {
  transform: scale(0.95);
}

.icon-btn[aria-pressed="true"] {
  border-color: rgba(56, 189, 248, 0.85);
  background: rgba(14, 116, 144, 0.55);
}

.tv-restore-btn {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  right: max(14px, env(safe-area-inset-right));
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(16, 28, 34, 0.35);
  color: #fff;
  font-size: 1.05rem;
  cursor: pointer;
  z-index: 25;
  opacity: 0.45;
  transition: opacity 200ms ease;
}

.tv-restore-btn:hover {
  opacity: 1;
}

/* ==================== Calibration Banner ==================== */
.calibration-banner {
  position: fixed;
  bottom: max(22px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 22px;
  border-radius: var(--radius-card);
  background: var(--glass-strong);
  border: 1px solid rgba(52, 211, 153, 0.5);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  box-shadow: var(--shadow-soft);
  z-index: 30;
}

.calibration-spinner {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 3px solid rgba(52, 211, 153, 0.28);
  border-top-color: #34d399;
  animation: spin 0.9s linear infinite;
}

.calibration-copy {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.calibration-copy strong {
  font-size: 0.98rem;
}

.calibration-copy span {
  font-size: 0.82rem;
  color: var(--text-muted);
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ==================== Welcome Overlay ==================== */
.welcome-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: radial-gradient(circle at center, rgba(12, 25, 32, 0.42), rgba(10, 20, 24, 0.72));
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  z-index: 40;
}

.welcome-card {
  width: min(560px, 100%);
  padding: 28px 28px 24px;
  border-radius: 26px;
  background: rgba(16, 28, 34, 0.86);
  border: 1px solid rgba(255, 255, 255, 0.24);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.48);
  text-align: center;
}

.welcome-badge {
  display: inline-block;
  padding: 6px 14px;
  border-radius: var(--radius-pill);
  background: rgba(52, 211, 153, 0.2);
  border: 1px solid rgba(52, 211, 153, 0.45);
  color: #a7f3d0;
  font-weight: 800;
  font-size: 0.88rem;
  margin-bottom: 12px;
}

.welcome-card h1 {
  margin: 0 0 10px;
  font-size: clamp(1.5rem, 3.6vw, 2.05rem);
  line-height: 1.15;
}

.welcome-desc {
  margin: 0 auto 18px;
  max-width: 44ch;
  color: var(--text-muted);
  font-size: 0.96rem;
  line-height: 1.5;
}

.scene-picker-preview {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 20px;
}

.scene-chip {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 13px;
  border-radius: 15px;
  border: 2px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.07);
  color: var(--text-main);
  font-family: inherit;
  text-align: left;
  cursor: pointer;
  transition: border-color 160ms ease, background 160ms ease, transform 120ms ease;
}

.scene-chip:hover {
  background: rgba(255, 255, 255, 0.13);
}

.scene-chip.selected {
  border-color: #34d399;
  background: rgba(52, 211, 153, 0.18);
}

.scene-chip-icon {
  font-size: 1.45rem;
  line-height: 1;
}

.scene-chip-text {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.scene-chip-text strong {
  font-size: 0.92rem;
}

.scene-chip-text small {
  font-size: 0.74rem;
  color: var(--text-muted);
}

.primary-btn {
  width: 100%;
  padding: 15px 22px;
  border-radius: var(--radius-pill);
  border: none;
  background: linear-gradient(135deg, #10b981, #34d399);
  color: #06281e;
  font-family: inherit;
  font-size: 1.08rem;
  font-weight: 800;
  cursor: pointer;
  box-shadow: 0 10px 26px rgba(16, 185, 129, 0.35);
  transition: transform 130ms ease, filter 150ms ease;
}

.primary-btn:hover {
  filter: brightness(1.06);
}

.primary-btn:active {
  transform: scale(0.98);
}

.welcome-note {
  margin: 13px 0 0;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.welcome-warning {
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(245, 158, 11, 0.2);
  border: 1px solid rgba(251, 191, 36, 0.55);
  color: #fde68a;
  font-size: 0.8rem;
  line-height: 1.4;
}

/* ==================== Settings Drawer ==================== */
.settings-drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(370px, 94vw);
  background: rgba(14, 24, 30, 0.94);
  border-left: 1px solid var(--glass-border);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  box-shadow: -16px 0 40px rgba(0, 0, 0, 0.45);
  z-index: 50;
  display: flex;
  flex-direction: column;
}

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: max(18px, env(safe-area-inset-top)) 20px 14px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
}

.drawer-header h2 {
  margin: 0;
  font-size: 1.15rem;
}

.close-btn {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-main);
  font-size: 1rem;
  cursor: pointer;
}

.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px max(24px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.setting-group {
  display: flex;
  flex-direction: column;
  gap: 11px;
}

.setting-group h3 {
  margin: 0;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #94a3b8;
}

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

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 13px;
  background: rgba(255, 255, 255, 0.06);
  cursor: pointer;
}

.toggle-row span {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.toggle-row strong {
  font-size: 0.9rem;
}

.toggle-row small {
  font-size: 0.74rem;
  color: var(--text-muted);
}

.toggle-row input[type="checkbox"] {
  width: 20px;
  height: 20px;
  accent-color: #34d399;
  flex-shrink: 0;
}

.camera-facing-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.05);
  font-size: 0.86rem;
}

.camera-facing-row select {
  background: #1e293b;
  color: #f8fafc;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  padding: 6px 10px;
  font-family: inherit;
}

.slider-row {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 10px 12px;
  border-radius: 13px;
  background: rgba(255, 255, 255, 0.06);
}

.slider-label {
  display: flex;
  justify-content: space-between;
  font-size: 0.88rem;
}

.slider-row input[type="range"] {
  width: 100%;
  accent-color: #34d399;
}

.slider-row small {
  font-size: 0.73rem;
  color: var(--text-muted);
}

.secondary-btn {
  padding: 10px 14px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.09);
  color: var(--text-main);
  font-family: inherit;
  font-size: 0.86rem;
  font-weight: 700;
  cursor: pointer;
}

.secondary-btn:hover {
  background: rgba(255, 255, 255, 0.16);
}

.secondary-btn.danger-soft {
  border-color: rgba(251, 113, 133, 0.45);
  background: rgba(251, 113, 133, 0.15);
}

.test-btn-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.hint-text {
  color: var(--text-muted);
  font-size: 0.75rem;
}

kbd {
  display: inline-block;
  padding: 1px 5px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.14);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.72rem;
}

/* Responsive tweaks for phones in portrait */
@media (max-width: 720px) {
  .hud-top {
    gap: 8px;
  }

  .meter-card {
    min-width: 145px;
    flex: 1 1 145px;
    padding: 8px 10px 9px;
  }

  .status-cluster {
    order: 3;
  }

  .mood-pill {
    padding: 7px 14px;
    font-size: 0.88rem;
  }

  .hushed-pill {
    padding: 7px 12px;
  }

  .special-gauge-card {
    padding: 8px 10px 7px;
  }

  .gauge-ring-wrap {
    width: 72px;
    height: 72px;
  }

  .btn-text {
    display: none;
  }

  .scene-picker-preview {
    grid-template-columns: 1fr;
  }
}
