/* ═══ Reset & Base ═══ */
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg: #0a0a1a;
  --surface: #12122a;
  --surface2: #1a1a35;
  --surface3: #222245;
  --border: #2a2a50;
  --text: #e8e8f0;
  --text2: #9090b0;
  --text3: #606080;
  --accent: #8b5cf6;
  --accent-h: #7c3aed;
  --accent-dim: #2d1f6b;
  --green: #34d399;
  --green-bg: #0d2a1f;
  --red: #f87171;
  --red-bg: #2d1212;
  --orange: #fbbf24;
  --radius: 16px;
  --radius-sm: 10px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

/* ═══ Screens ═══ */
.screen {
  display: none;
  position: fixed;
  inset: 0;
  flex-direction: column;
}
.screen.active { display: flex; }

/* ═══ HOME ═══ */
#screen-home {
  justify-content: center;
  align-items: center;
  background: var(--bg);
  padding: 24px;
}

.home-container {
  width: 100%;
  max-width: 400px;
}

.logo {
  text-align: center;
  margin-bottom: 40px;
}
.logo-icon {
  width: 80px;
  height: 80px;
  margin: 0 auto 16px;
  background: var(--accent-dim);
  border-radius: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--accent);
}
.logo-icon svg { width: 48px; height: 48px; }
.logo h1 {
  font-size: 32px;
  font-weight: 800;
  letter-spacing: -0.5px;
}
.subtitle {
  color: var(--text2);
  font-size: 15px;
  margin-top: 4px;
}

/* Form */
.home-form { display: flex; flex-direction: column; gap: 16px; }
.field-label {
  font-size: 13px;
  font-weight: 700;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.code-input-group {
  display: flex;
  gap: 10px;
}
.code-input-group input {
  flex: 1;
  min-width: 0;
  height: 52px;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-size: 20px;
  font-weight: 700;
  text-align: center;
  letter-spacing: 4px;
  outline: none;
  transition: border-color 0.2s;
}
.code-input-group input:focus { border-color: var(--accent); }
.code-input-group input::placeholder {
  font-size: 15px;
  letter-spacing: 1px;
  color: var(--text3);
}

.btn-icon {
  width: 52px;
  height: 52px;
  border-radius: var(--radius-sm);
  border: 2px solid var(--border);
  background: var(--surface);
  color: var(--text2);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;
}
.btn-icon:active { background: var(--surface2); }
.btn-icon svg { width: 20px; height: 20px; }

/* Role selector */
.role-selector {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.role-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 20px 12px;
  border-radius: var(--radius);
  border: 2px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: all 0.25s;
}
.role-btn.selected {
  border-color: var(--accent);
  background: var(--accent-dim);
}
.role-btn:active { transform: scale(0.97); }

.role-icon {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface2);
  transition: background 0.2s;
}
.role-btn.selected .role-icon { background: var(--accent); color: #fff; }
.role-icon svg { width: 28px; height: 28px; }

.role-label { font-size: 16px; font-weight: 700; }
.role-desc { font-size: 11px; color: var(--text3); text-align: center; }
.role-btn.selected .role-desc { color: var(--text2); }

/* Primary button */
.btn-primary {
  width: 100%;
  height: 56px;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: #fff;
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s;
  margin-top: 8px;
}
.btn-primary:disabled {
  background: var(--surface2);
  color: var(--text3);
  cursor: not-allowed;
}
.btn-primary:not(:disabled):active {
  background: var(--accent-h);
  transform: scale(0.98);
}

/* ═══ MONITOR SCREENS (baby + parent) ═══ */
.monitor-header {
  background: var(--surface);
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  z-index: 10;
}
.status-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  overflow: hidden;
}
.status-left {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  font-weight: 600;
  overflow: hidden;
  min-width: 0;
}
.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--orange);
  animation: pulse-dot 2s infinite;
}
.status-dot.connected { background: var(--green); }
.status-dot.disconnected { background: var(--red); animation: none; }

@keyframes pulse-dot {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

.room-badge {
  font-size: 12px;
  font-weight: 700;
  color: var(--accent);
  background: var(--accent-dim);
  padding: 4px 10px;
  border-radius: 20px;
  letter-spacing: 1px;
  max-width: 100px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex-shrink: 0;
}

/* ═══ BABY SCREEN ═══ */
.baby-content {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: #000;
}
#baby-preview {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.baby-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(10, 10, 26, 0.6);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s;
}
.baby-overlay.visible { opacity: 1; }

.baby-msg {
  font-size: 16px;
  color: var(--green);
  font-weight: 600;
  margin-top: 20px;
}

/* Sound visualizer */
.sound-visualizer {
  display: flex;
  align-items: end;
  gap: 4px;
  height: 60px;
}
.viz-bar {
  width: 6px;
  border-radius: 3px;
  background: var(--accent);
  height: 10px;
  transition: height 0.1s ease;
}

/* Baby controls */
.baby-controls {
  display: flex;
  justify-content: center;
  gap: 16px;
  padding: 16px;
  padding-bottom: max(16px, var(--safe-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.ctrl-btn {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: var(--surface2);
  color: var(--text2);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;
  gap: 2px;
}
.ctrl-btn svg { width: 22px; height: 22px; }
.ctrl-btn.active {
  border-color: var(--green);
  color: var(--green);
  background: var(--green-bg);
}
.ctrl-btn.danger {
  border-color: var(--red);
  color: var(--red);
  background: var(--red-bg);
}
.ctrl-btn:active { transform: scale(0.92); }
.ctrl-label { font-size: 9px; font-weight: 600; margin-top: 2px; }

/* ═══ PARENT SCREEN ═══ */
.parent-content {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: #000;
}
#parent-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.parent-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 5;
  transition: opacity 0.5s;
}
.parent-overlay.hidden { opacity: 0; pointer-events: none; }

.parent-overlay p { color: var(--text2); font-size: 15px; margin-top: 24px; }

.waiting-animation {
  position: relative;
  width: 100px;
  height: 100px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.pulse-ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 2px solid var(--accent);
  opacity: 0;
  animation: pulse-ring 2.5s infinite;
}
.pulse-ring.delay { animation-delay: 1.25s; }
@keyframes pulse-ring {
  0% { transform: scale(0.5); opacity: 0.6; }
  100% { transform: scale(1.5); opacity: 0; }
}
.waiting-icon { width: 48px; height: 48px; color: var(--accent); z-index: 1; }

/* Sound meter */
.sound-meter {
  position: absolute;
  bottom: 16px;
  left: 16px;
  right: 16px;
  height: 32px;
  background: var(--surface);
  border-radius: 16px;
  overflow: hidden;
  z-index: 10;
  border: 1px solid var(--border);
}
.sound-meter-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--green), var(--orange), var(--red));
  border-radius: 16px;
  transition: width 0.15s ease;
}
.sound-meter-label {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text2);
  display: flex;
  align-items: center;
  z-index: 1;
}

/* Noise alert */
.noise-alert {
  position: absolute;
  top: 16px;
  left: 50%;
  transform: translateX(-50%) translateY(-60px);
  background: var(--red);
  color: #fff;
  padding: 10px 20px;
  border-radius: 30px;
  font-size: 14px;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
  z-index: 20;
  opacity: 0;
  transition: all 0.3s;
  box-shadow: 0 4px 24px rgba(248, 113, 113, 0.4);
}
.noise-alert.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* Parent controls */
.parent-controls {
  display: flex;
  justify-content: center;
  gap: 12px;
  padding: 12px 16px;
  padding-bottom: max(12px, var(--safe-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.parent-controls .ctrl-btn {
  width: auto;
  min-width: 60px;
  height: 50px;
  border-radius: 14px;
  padding: 4px 12px;
}

/* Threshold panel */
.threshold-panel {
  position: absolute;
  bottom: 90px;
  left: 16px;
  right: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  z-index: 30;
  display: none;
}
.threshold-panel.open { display: block; }
.threshold-panel label {
  font-size: 13px;
  font-weight: 700;
  color: var(--text2);
}
.threshold-panel input[type="range"] {
  width: 100%;
  margin: 12px 0 8px;
  accent-color: var(--accent);
}
.threshold-value {
  text-align: center;
  font-size: 20px;
  font-weight: 800;
  color: var(--accent);
}

/* ═══ Animations ═══ */
@keyframes fade-in {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}
.home-container { animation: fade-in 0.4s ease; }
