/* ============================================================
   HRBR Sleep Monitor Dashboard — Dark Theme
   CSS variables reused from MR60BHA2_vs_AppleWatch_Comparison.html
   ============================================================ */

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #242835;
  --border: #2e3345;
  --text: #e4e6ef;
  --text-muted: #8b8fa3;
  --accent: #6c8cff;
  --accent-glow: rgba(108, 140, 255, 0.15);
  --green: #34d399;
  --green-bg: rgba(52, 211, 153, 0.1);
  --red: #f87171;
  --red-bg: rgba(248, 113, 113, 0.1);
  --amber: #fbbf24;
  --amber-bg: rgba(251, 191, 36, 0.1);
  --purple: #a78bfa;
  --deep-blue: #4338ca;
  --radius: 10px;
  --radius-sm: 6px;
}

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

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}

/* ---- Header ---- */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
  gap: 0.75rem;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
}

.header h1 {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -0.5px;
  background: linear-gradient(135deg, #6c8cff, #a78bfa);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.connection-badge {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.25rem 0.75rem;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 500;
  background: var(--surface2);
  border: 1px solid var(--border);
}

/* Device selector dropdown */
#device-selector {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.2rem 0.5rem;
  font-size: 0.75rem;
  font-family: inherit;
  cursor: pointer;
  outline: none;
  min-width: 140px;
}

#device-selector:focus { border-color: var(--accent); }
#device-selector option { background: var(--surface); color: var(--text); }

.connection-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--red);
  transition: background 0.3s;
}

.connection-dot.connected { background: var(--green); }
.connection-dot.reconnecting { background: var(--amber); animation: pulse 1.5s infinite; }

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

.header-right {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  font-size: 0.8rem;
  color: var(--text-muted);
  flex-shrink: 0;
}

/* ---- Main Layout ---- */
.main {
  padding: 1.25rem;
  max-width: 1400px;
  margin: 0 auto;
}

/* ---- Stat Cards Row ---- */
.stat-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.stat-card .label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
}

.stat-card .value {
  font-size: 1.75rem;
  font-weight: 700;
  line-height: 1.2;
}

.stat-card .unit {
  font-size: 0.85rem;
  font-weight: 400;
  color: var(--text-muted);
}

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

.value-hr { color: var(--red); }
.value-br { color: var(--accent); }
.value-dist { color: var(--green); }
.value-lux { color: var(--amber); }

/* Presence indicator */
.presence-indicator {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.presence-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--border);
  transition: background 0.3s;
}

.presence-dot.active {
  background: var(--green);
  box-shadow: 0 0 8px rgba(52, 211, 153, 0.5);
}

/* ---- Chart Panels ---- */
.chart-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  margin-bottom: 1rem;
}

.chart-panel .chart-title,
.cmd-panel .chart-title {
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
}

.chart-container {
  position: relative;
  width: 100%;
  height: 200px;
}

.chart-container.tall {
  height: 160px;
}

/* ---- Two-Column Layout ---- */
.two-col {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  margin-bottom: 1rem;
}

@media (max-width: 900px) {
  .two-col { grid-template-columns: 1fr; }
}

/* ---- Phase Data Panel ---- */
.phase-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 0.75rem;
}

.phase-item {
  text-align: center;
  padding: 0.75rem;
  background: var(--surface2);
  border-radius: var(--radius-sm);
}

.phase-item .phase-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  margin-bottom: 0.25rem;
}

.phase-item .phase-value {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--purple);
}

/* ---- Sleep Hypnogram ---- */
.sleep-summary-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 0.75rem;
  margin-top: 0.75rem;
}

.sleep-stat {
  text-align: center;
  padding: 0.75rem;
  background: var(--surface2);
  border-radius: var(--radius-sm);
}

.sleep-stat .sleep-stat-label {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
}

.sleep-stat .sleep-stat-value {
  font-size: 1.1rem;
  font-weight: 700;
  margin-top: 0.15rem;
}

.stage-awake { color: var(--red); }
.stage-rem { color: var(--green); }
.stage-light { color: var(--accent); }
.stage-deep { color: var(--deep-blue); }

/* ---- Stage Bar ---- */
.stage-bar {
  display: flex;
  height: 8px;
  border-radius: 4px;
  overflow: hidden;
  margin-top: 0.75rem;
  background: var(--surface2);
}

.stage-bar-segment {
  height: 100%;
  transition: width 0.5s ease;
}

/* ---- Command Panel ---- */
.cmd-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  margin-bottom: 1rem;
}

.cmd-row {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  align-items: center;
}

.cmd-btn {
  padding: 0.4rem 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface2);
  color: var(--text);
  font-size: 0.8rem;
  cursor: pointer;
  transition: all 0.2s;
  font-family: inherit;
}

.cmd-btn:hover {
  border-color: var(--accent);
  background: var(--accent-glow);
}

.cmd-btn.danger:hover {
  border-color: var(--red);
  background: var(--red-bg);
}

.cmd-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.cmd-input {
  padding: 0.4rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface2);
  color: var(--text);
  font-size: 0.8rem;
  width: 100px;
  outline: none;
  font-family: inherit;
  color-scheme: dark;
}

.cmd-input:focus {
  border-color: var(--accent);
}

/* ---- Device Info Panel ---- */
.device-info {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 0.5rem;
}

.info-item {
  display: flex;
  flex-direction: column;
  padding: 0.5rem 0;
}

.info-item .info-label {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
}

.info-item .info-value {
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text);
}

/* ---- Log / Messages ---- */
.log-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  max-height: 200px;
  overflow-y: auto;
}

.log-entry {
  font-family: 'JetBrains Mono', 'Fira Code', monospace;
  font-size: 0.72rem;
  color: var(--text-muted);
  padding: 0.15rem 0;
  border-bottom: 1px solid rgba(46, 51, 69, 0.5);
}

.log-entry .log-time {
  color: var(--accent);
  margin-right: 0.5rem;
}

.log-entry .log-topic {
  color: var(--amber);
  margin-right: 0.5rem;
}

/* ---- Footer ---- */
.footer {
  text-align: center;
  padding: 1rem;
  color: var(--text-muted);
  font-size: 0.7rem;
  border-top: 1px solid var(--border);
  margin-top: 1rem;
}

/* ---- Scrollbar ---- */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ============================================================
   Section Accordion (Collapsible Panels)
   ============================================================ */

.section-collapsible {
  margin-bottom: 1rem;
}

.section-collapsible:not(.chart-panel):not(.cmd-panel) {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.section-header .chart-title {
  margin-bottom: 0;
}

.section-chevron {
  font-size: 0.7rem;
  color: var(--text-muted);
  transition: transform 0.3s;
  flex-shrink: 0;
  padding-left: 0.5rem;
}

.section-collapsible.collapsed .section-chevron {
  transform: rotate(-90deg);
}

.section-body {
  overflow: hidden;
  transition: max-height 0.35s ease, opacity 0.25s ease, margin-top 0.25s ease;
  max-height: 2000px;
  opacity: 1;
  margin-top: 0.75rem;
}

.section-collapsible.collapsed .section-body {
  max-height: 0;
  opacity: 0;
  margin-top: 0;
}

/* ============================================================
   Hamburger Menu & Mobile Nav
   ============================================================ */

.hamburger {
  display: none;
  flex-direction: column;
  gap: 4px;
  background: none;
  border: none;
  cursor: pointer;
  padding: 10px;
  min-width: 44px;
  min-height: 44px;
  justify-content: center;
  align-items: center;
  -webkit-tap-highlight-color: transparent;
  flex-shrink: 0;
}

.hamburger span {
  width: 20px;
  height: 2px;
  background: var(--text);
  border-radius: 1px;
  transition: transform 0.3s, opacity 0.3s;
}

.hamburger.open span:nth-child(1) { transform: rotate(45deg) translate(4px, 4px); }
.hamburger.open span:nth-child(2) { opacity: 0; }
.hamburger.open span:nth-child(3) { transform: rotate(-45deg) translate(4px, -4px); }

.mobile-nav {
  display: none;
  flex-direction: column;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0.25rem 1.5rem;
  position: sticky;
  top: 52px;
  z-index: 99;
}

.mobile-nav.open { display: flex; }

.mobile-nav a {
  color: var(--text);
  text-decoration: none;
  padding: 0.75rem 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
  min-height: 44px;
  display: flex;
  align-items: center;
  -webkit-tap-highlight-color: transparent;
}

.mobile-nav a:last-child { border-bottom: none; }

.mobile-nav a:active {
  color: var(--accent);
}

/* ============================================================
   Playback Control Bar
   ============================================================ */

.playback-bar {
  background: var(--surface2);
  border-bottom: 1px solid var(--border);
  padding: 0.5rem 1.5rem;
  position: sticky;
  top: 52px;
  z-index: 98;
}

.playback-controls {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  max-width: 1400px;
  margin: 0 auto;
  flex-wrap: wrap;
}

.pb-btn {
  padding: 0.35rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-size: 0.8rem;
  min-width: 44px;
  min-height: 36px;
  font-family: inherit;
  transition: all 0.2s;
}

.pb-btn:hover {
  border-color: var(--accent);
  background: var(--accent-glow);
}

.pb-scrubber {
  flex: 1;
  min-width: 100px;
  accent-color: var(--accent);
  height: 6px;
  cursor: pointer;
}

.pb-time {
  font-family: 'JetBrains Mono', 'Fira Code', monospace;
  font-size: 0.75rem;
  color: var(--text-muted);
  white-space: nowrap;
}

.pb-speed {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.3rem;
  font-size: 0.75rem;
  font-family: inherit;
  cursor: pointer;
}

.pb-label {
  font-family: 'JetBrains Mono', 'Fira Code', monospace;
  font-size: 0.75rem;
  color: var(--accent);
  font-weight: 600;
  white-space: nowrap;
}

.pb-status {
  font-size: 0.7rem;
  color: var(--green);
  font-weight: 600;
  text-transform: uppercase;
  white-space: nowrap;
}

/* ============================================================
   Session Browser Modal
   ============================================================ */

.modal-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal-content {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  max-width: 500px;
  width: 100%;
  max-height: 80vh;
  overflow-y: auto;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.session-item {
  padding: 0.75rem;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  margin-bottom: 0.5rem;
  cursor: pointer;
  transition: border-color 0.2s;
}

.session-item:hover {
  border-color: var(--accent);
}

.session-date {
  font-weight: 600;
  font-size: 0.9rem;
  margin-bottom: 0.25rem;
}

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

/* ============================================================
   Mobile Responsive Breakpoints
   ============================================================ */

/* Tablet: 768px and below */
@media (max-width: 768px) {
  .hamburger { display: flex; }

  .header {
    padding: 0.75rem 1rem;
    flex-wrap: wrap;
  }

  .header-left {
    gap: 0.5rem;
    flex: 1;
    min-width: 0;
  }

  .header h1 { font-size: 1rem; }

  .header-right {
    gap: 0.5rem;
    font-size: 0.7rem;
  }
  .header-right span { display: none; }  /* Hide Msgs/Last counters, keep History button */

  .main { padding: 0.75rem; }

  .stat-row {
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 0.5rem;
  }

  .stat-card { padding: 0.75rem; }
  .stat-card .value { font-size: 1.4rem; }

  .chart-container { height: 180px; }
  .chart-container.tall { height: 140px; }

  .phase-grid { gap: 0.5rem; }

  /* Playback bar adjustments */
  .playback-bar { padding: 0.5rem 1rem; }
  .playback-controls { gap: 0.5rem; }
  .pb-scrubber { min-width: 100%; order: 10; margin-top: 0.25rem; }

  /* Touch targets */
  .cmd-btn, .pb-btn { min-height: 44px; min-width: 44px; }

  /* Connection badges wrap */
  .connection-badge { font-size: 0.7rem; padding: 0.2rem 0.5rem; }
  #device-selector { min-width: 100px; font-size: 0.7rem; }
}

/* Phone: 480px and below */
@media (max-width: 480px) {
  .header h1 { font-size: 0.9rem; }

  .stat-row {
    grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
  }

  .stat-card .value { font-size: 1.1rem; }
  .stat-card .label { font-size: 0.6rem; }

  .chart-container { height: 150px; }

  .phase-grid { grid-template-columns: 1fr 1fr 1fr; }

  .sleep-summary-grid {
    grid-template-columns: repeat(auto-fit, minmax(90px, 1fr));
  }

  .device-info { grid-template-columns: 1fr; }

  .modal-content { width: 95%; padding: 1rem; }

  .section-collapsible:not(.chart-panel):not(.cmd-panel) {
    padding: 0.75rem;
  }

  .chart-panel { padding: 0.75rem; }
}

/* Landscape phone */
@media (max-height: 500px) and (orientation: landscape) {
  .header { position: relative; padding: 0.5rem 1rem; }
  .playback-bar { position: relative; top: 0; }
  .mobile-nav { position: relative; top: 0; }
  .chart-container { height: 120px; }
  .chart-container.tall { height: 100px; }
  .stat-row { grid-template-columns: repeat(6, 1fr); }
}
