:root {
  --bg-dark: #090d16;
  --bg-card: #131b2e;
  --bg-card-hover: #1b2640;
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --text-dim: #64748b;
  --primary: #6366f1;
  --primary-glow: rgba(99, 102, 241, 0.35);
  --success: #10b981;
  --success-glow: rgba(16, 185, 129, 0.3);
  --danger: #f43f5e;
  --danger-glow: rgba(244, 63, 94, 0.4);
  --warning: #f59e0b;
  --border: rgba(255, 255, 255, 0.08);
  --radius-lg: 16px;
  --radius-md: 12px;
  --radius-sm: 8px;
  --font-main: 'Plus Jakarta Sans', system-ui, -apple-system, sans-serif;
}

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

body {
  font-family: var(--font-main);
  background-color: var(--bg-dark);
  color: var(--text-main);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding-bottom: 30px;
}

#app {
  width: 100%;
  max-width: 600px;
  display: flex;
  flex-direction: column;
}

/* Header */
.top-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  background: rgba(15, 23, 42, 0.85);
  backdrop-filter: blur(12px);
  position: sticky;
  top: 0;
  z-index: 50;
  border-bottom: 1px solid var(--border);
}

.logo-group {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-icon {
  width: 38px;
  height: 38px;
  background: linear-gradient(135deg, #6366f1, #a855f7);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  box-shadow: 0 4px 12px var(--primary-glow);
}

.logo-icon svg {
  width: 22px;
  height: 22px;
}

.logo-title h1 {
  font-size: 1.1rem;
  font-weight: 700;
  line-height: 1.2;
}

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

.nav-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.status-pill {
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(255, 255, 255, 0.05);
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 600;
  border: 1px solid var(--border);
}

.live-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-dim);
}

.status-pill.online .live-dot {
  background: var(--danger);
  box-shadow: 0 0 10px var(--danger);
  animation: pulse-dot 1.5s infinite;
}

@keyframes pulse-dot {
  0% { transform: scale(0.95); opacity: 0.7; }
  50% { transform: scale(1.2); opacity: 1; }
  100% { transform: scale(0.95); opacity: 0.7; }
}

.apk-badge-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  background: linear-gradient(135deg, rgba(99, 102, 241, 0.2), rgba(168, 85, 247, 0.2));
  border: 1px solid rgba(99, 102, 241, 0.4);
  color: #a5b4fc;
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 0.72rem;
  font-weight: 700;
  text-decoration: none;
  transition: all 0.2s;
}

.apk-badge-btn svg {
  width: 14px;
  height: 14px;
}

.apk-badge-btn:hover {
  background: var(--primary);
  color: #fff;
}

.icon-btn {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--border);
  color: var(--text-main);
  width: 38px;
  height: 38px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;
}

.icon-btn svg {
  width: 20px;
  height: 20px;
}

.icon-btn:hover {
  background: var(--bg-card-hover);
}

/* Tab Bar */
.tab-bar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  background: var(--bg-card);
  padding: 6px;
  margin: 12px 16px;
  border-radius: 14px;
  border: 1px solid var(--border);
  gap: 4px;
}

.tab-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 8px 4px;
  border-radius: 10px;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.72rem;
  font-weight: 600;
  transition: all 0.2s;
}

.tab-btn svg {
  width: 18px;
  height: 18px;
}

.tab-btn.active {
  background: var(--primary);
  color: #fff;
  box-shadow: 0 4px 12px var(--primary-glow);
}

/* Content Area */
.content-area {
  padding: 0 16px;
}

.tab-pane {
  display: none;
  animation: fadeIn 0.25s ease-in-out;
}

.tab-pane.active {
  display: block;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Video Card Container */
.video-container {
  width: 100%;
  position: relative;
  border-radius: var(--radius-lg);
  overflow: hidden;
  background: #000;
  aspect-ratio: 16 / 9;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.6);
  border: 1px solid var(--border);
}

#babyVideoPlayer {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

#fenceOverlayCanvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.video-overlay {
  position: absolute;
  inset: 0;
  background: rgba(10, 14, 26, 0.92);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 20px;
  z-index: 10;
  transition: opacity 0.3s;
}

.video-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.overlay-spinner {
  width: 42px;
  height: 42px;
  border: 3px solid rgba(99, 102, 241, 0.2);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 12px;
}

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

#overlayMessage {
  font-weight: 600;
  font-size: 0.95rem;
}

.overlay-sub {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 4px;
}

/* HUD Over Video */
.video-hud {
  position: absolute;
  left: 12px;
  right: 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 5;
  pointer-events: auto;
}

.top-hud {
  top: 12px;
  gap: 8px;
}

.bottom-hud {
  bottom: 12px;
}

.hud-item {
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(8px);
  padding: 4px 8px;
  border-radius: 6px;
  font-size: 0.72rem;
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.hud-label {
  color: var(--text-muted);
}

.posture-badge {
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(8px);
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 0.75rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
}

.badge-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.dot-safe { background: var(--success); box-shadow: 0 0 6px var(--success); }
.dot-danger { background: var(--danger); box-shadow: 0 0 8px var(--danger); animation: pulse-dot 1s infinite; }
.dot-warn { background: var(--warning); box-shadow: 0 0 6px var(--warning); }

.hud-btn {
  background: rgba(0, 0, 0, 0.6);
  border: 1px solid rgba(255, 255, 255, 0.15);
  color: #fff;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  margin-left: 6px;
}

.hud-btn svg {
  width: 16px;
  height: 16px;
}

/* Quick Controls */
.quick-controls {
  display: flex;
  gap: 12px;
  margin-top: 14px;
}

.control-btn {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  color: var(--text-main);
  padding: 12px;
  display: flex;
  align-items: center;
  cursor: pointer;
  font-family: inherit;
  transition: all 0.2s;
}

.control-btn:hover {
  background: var(--bg-card-hover);
}

.ptt-btn {
  flex: 1;
  gap: 12px;
  background: linear-gradient(135deg, rgba(99, 102, 241, 0.15), rgba(168, 85, 247, 0.1));
  border-color: rgba(99, 102, 241, 0.3);
}

.ptt-btn.active {
  background: var(--danger);
  border-color: var(--danger);
  box-shadow: 0 0 20px var(--danger-glow);
}

.ptt-icon {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
}

.ptt-btn.active .ptt-icon {
  background: white;
  color: var(--danger);
}

.ptt-content {
  display: flex;
  flex-direction: column;
  text-align: left;
}

.ptt-title {
  font-weight: 700;
  font-size: 0.85rem;
}

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

.mini-btn {
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  padding: 8px 16px;
  font-size: 0.75rem;
  font-weight: 600;
}

.mini-btn svg {
  width: 20px;
  height: 20px;
}

/* Cards General */
.card {
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  padding: 16px;
  margin-top: 14px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.card-header h3 {
  font-size: 0.92rem;
  font-weight: 700;
}

.badge-tag {
  background: rgba(99, 102, 241, 0.15);
  color: #a5b4fc;
  font-size: 0.7rem;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 12px;
  border: 1px solid rgba(99, 102, 241, 0.3);
}

.text-btn {
  background: transparent;
  border: none;
  color: var(--primary);
  font-family: inherit;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
}

.card-desc {
  font-size: 0.8rem;
  color: var(--text-muted);
  line-height: 1.4;
  margin-bottom: 12px;
}

/* Lullaby Buttons */
.lullaby-buttons {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
}

.lullaby-btn {
  background: var(--bg-dark);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 4px;
  color: var(--text-main);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  font-family: inherit;
  transition: all 0.2s;
}

.lullaby-btn:hover {
  background: var(--bg-card-hover);
  border-color: var(--primary);
}

.btn-emoji {
  font-size: 1.2rem;
}

.btn-name {
  font-size: 0.68rem;
  font-weight: 600;
}

.stop-btn {
  background: rgba(244, 63, 94, 0.1);
  border-color: rgba(244, 63, 94, 0.3);
}

.stop-btn:hover {
  background: var(--danger);
}

/* Mini Vitals Grid */
.vitals-grid-mini {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-top: 14px;
}

.mini-vital-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

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

.vital-val {
  font-size: 1.35rem;
  font-weight: 800;
  color: var(--text-main);
}

.vital-val small {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-dim);
}

.vital-status {
  font-size: 0.7rem;
  font-weight: 600;
}

.status-good { color: var(--success); }
.status-danger { color: var(--danger); }
.status-warn { color: var(--warning); }

/* Waveform Canvas */
.waveform-container {
  width: 100%;
  height: 120px;
  background: #090d16;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  margin-bottom: 12px;
  position: relative;
  overflow: hidden;
}

#breathingWaveCanvas {
  width: 100%;
  height: 100%;
}

.wave-stats {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  color: var(--text-muted);
}

.wave-stats strong {
  color: var(--text-main);
}

/* Switch */
.fence-toggle-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-top: 1px solid var(--border);
  font-size: 0.85rem;
  font-weight: 500;
}

.switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 24px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0; left: 0; right: 0; bottom: 0;
  background-color: #334155;
  transition: .3s;
  border-radius: 24px;
}

.slider:before {
  position: absolute;
  content: "";
  height: 18px;
  width: 18px;
  left: 3px;
  bottom: 3px;
  background-color: white;
  transition: .3s;
  border-radius: 50%;
}

input:checked + .slider {
  background-color: var(--primary);
}

input:checked + .slider:before {
  transform: translateX(20px);
}

/* Rule Items */
.rule-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}

.rule-item:last-child {
  border-bottom: none;
}

.rule-icon {
  font-size: 1.4rem;
}

.rule-info {
  flex: 1;
}

.rule-info h4 {
  font-size: 0.82rem;
  font-weight: 700;
}

.rule-info p {
  font-size: 0.72rem;
  color: var(--text-muted);
}

.rule-badge {
  font-size: 0.68rem;
  padding: 4px 8px;
  border-radius: 8px;
  font-weight: 600;
  background: rgba(16, 185, 129, 0.15);
  color: var(--success);
  border: 1px solid rgba(16, 185, 129, 0.3);
}

/* Timeline */
.timeline-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.timeline-item {
  background: var(--bg-dark);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  display: flex;
  align-items: flex-start;
  gap: 10px;
}

.item-badge-critical {
  border-left: 3px solid var(--danger);
}

.item-badge-warning {
  border-left: 3px solid var(--warning);
}

.item-badge-info {
  border-left: 3px solid var(--primary);
}

.timeline-content h4 {
  font-size: 0.82rem;
  font-weight: 700;
}

.timeline-content p {
  font-size: 0.72rem;
  color: var(--text-muted);
}

.timeline-time {
  font-size: 0.65rem;
  color: var(--text-dim);
  margin-top: 2px;
}

.empty-state {
  text-align: center;
  padding: 24px;
  font-size: 0.8rem;
  color: var(--text-muted);
}

/* Photo Gallery & Timelapse */
.timelapse-box {
  background: #090d16;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 20px;
  text-align: center;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.photo-gallery {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.photo-card {
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1px solid var(--border);
  aspect-ratio: 1;
  background: #000;
}

.photo-card img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
