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

:root {
  --bg: #0b0b0f;
  --card: #141419;
  --card-border: #1e1e28;
  --text: #e2e2ef;
  --muted: #6e6e85;
  --accent: #5865f2;
  --hr: #ff6b6b;
  --spo2: #51cf66;
  --deep: #4263eb;
  --core: #748ffc;
  --rem: #da77f2;
  --awake-stage: #ffd43b;
  --grade-a: #2ecc71;
  --grade-b: #f1c40f;
  --grade-c: #e67e22;
  --grade-d: #e74c3c;
  --teal: #1abc9c;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.page {
  max-width: 480px;
  margin: 0 auto;
  padding: 16px 12px 40px;
}

.dashboard {
  display: contents;
}

/* Header */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  padding: 0 4px;
}

header h1 {
  font-size: 1.2rem;
  font-weight: 700;
  letter-spacing: -0.3px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.last-updated {
  font-size: 0.65rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.last-updated .dot {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--grade-a);
  margin-right: 5px;
  vertical-align: middle;
  animation: blink 2s infinite;
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

.badge {
  padding: 5px 14px;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  background: rgba(136,136,160,0.12);
  color: var(--muted);
}
.badge.awake { background: rgba(46,204,113,0.12); color: var(--grade-a); }
.badge.asleep { background: rgba(88,101,242,0.12); color: var(--accent); }

/* Grade card */
.grade-card {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 20px !important;
}

.grade-circle {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  border: 3px solid var(--muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.grade-letter {
  font-size: 1.8rem;
  font-weight: 800;
  line-height: 1;
}

.grade-score {
  font-size: 0.65rem;
  color: var(--muted);
  margin-top: 2px;
}

.grade-details { flex: 1; min-width: 0; }
.grade-details h2 { font-size: 0.95rem; font-weight: 600; margin-bottom: 8px; }

.grade-notes {
  font-size: 0.75rem;
  color: var(--muted);
  line-height: 1.6;
}

.grade-notes .note {
  display: flex;
  justify-content: space-between;
  padding: 2px 0;
}

.grade-notes .note .tag {
  font-weight: 600;
  color: var(--text);
}

/* Vitals row */
.vitals-row {
  display: flex;
  align-items: center;
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: 14px;
  padding: 16px 0;
  margin-bottom: 12px;
}

.vital {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.vital-val {
  font-size: 1.6rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.vital-val.pulse { animation: pulse 0.8s ease; }

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.08); }
}

#live-hr { color: var(--hr); }
#live-spo2 { color: var(--spo2); }

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

.vital-divider {
  width: 1px;
  height: 32px;
  background: var(--card-border);
}

/* Cards */
.card {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: 14px;
  padding: 16px;
  margin-bottom: 12px;
}

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

.card-header h2 {
  font-size: 0.95rem;
  font-weight: 600;
}

.card-header select,
.card-header input[type="date"] {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--card-border);
  border-radius: 8px;
  padding: 5px 10px;
  font-size: 0.75rem;
}

/* Sleep grid */
.sleep-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.sleep-tile {
  background: var(--bg);
  border-radius: 10px;
  padding: 10px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.sleep-tile .label {
  font-size: 0.6rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  font-weight: 600;
}

.sleep-tile .val {
  font-size: 1.1rem;
  font-weight: 700;
  margin-top: 3px;
}

/* Timeline */
.timeline-wrap {
  margin-top: 14px;
}

.sleep-timeline {
  display: flex;
  height: 28px;
  border-radius: 8px;
  overflow: hidden;
  background: var(--bg);
}

.sleep-timeline .block {
  height: 100%;
  min-width: 2px;
  position: relative;
  transition: opacity 0.2s;
}

.sleep-timeline .block:hover { opacity: 0.7; }

.sleep-timeline .block.deep { background: var(--deep); }
.sleep-timeline .block.core { background: var(--core); }
.sleep-timeline .block.rem { background: var(--rem); }
.sleep-timeline .block.awake { background: var(--awake-stage); }
.sleep-timeline .block.in_bed { background: var(--card-border); }

.sleep-timeline .block:hover::after {
  content: attr(data-label);
  position: absolute;
  bottom: 115%;
  left: 50%;
  transform: translateX(-50%);
  background: var(--card);
  border: 1px solid var(--card-border);
  padding: 3px 8px;
  border-radius: 6px;
  font-size: 0.65rem;
  white-space: nowrap;
  z-index: 10;
  pointer-events: none;
}

.sleep-legend {
  display: flex;
  gap: 12px;
  margin-top: 8px;
  flex-wrap: wrap;
}

.sleep-legend .item {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 0.65rem;
  color: var(--muted);
  font-weight: 500;
}

.sleep-legend .dot {
  width: 8px;
  height: 8px;
  border-radius: 3px;
}

/* Charts */
.chart-wrap {
  height: 180px;
  position: relative;
}

/* No data */
.no-data {
  grid-column: 1 / -1;
  text-align: center;
  color: var(--muted);
  padding: 20px;
  font-size: 0.8rem;
}

/* ===== Desktop: side-by-side layout ===== */
@media (min-width: 768px) {
  .page {
    max-width: 1100px;
    padding: 24px 32px 40px;
  }

  header { margin-bottom: 20px; }
  header h1 { font-size: 1.4rem; }

  .dashboard {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
  }

  .col-left, .col-right {
    display: flex;
    flex-direction: column;
    gap: 12px;
    /* Lock both columns to viewport height minus header */
    height: calc(100vh - 100px);
    overflow: hidden;
  }

  /* Remove bottom margin since gap handles spacing */
  .col-left .card,
  .col-left .vitals-row,
  .col-right .card {
    margin-bottom: 0;
  }

  /* Left: sleep report card stretches to fill remaining space */
  .col-left .card:last-child {
    flex: 1;
    overflow: auto;
    display: flex;
    flex-direction: column;
  }

  .col-left .card:last-child .sleep-grid {
    flex: 1;
  }

  .col-left .card:last-child .timeline-wrap {
    margin-top: auto;
    padding-top: 14px;
  }

  /* Right: both chart cards split space evenly */
  .col-right .card {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  /* Chart canvas fills its card, with fixed aspect via container */
  .col-right .chart-wrap {
    flex: 1;
    height: 0;
    min-height: 0;
    position: relative;
  }

  /* Bigger grade circle on desktop */
  .grade-circle {
    width: 96px;
    height: 96px;
  }

  .grade-letter { font-size: 2.2rem; }
  .grade-score { font-size: 0.75rem; }
  .grade-notes { font-size: 0.8rem; }

  /* Bigger vitals */
  .vital-val { font-size: 1.8rem; }
  .vital-label { font-size: 0.7rem; }
}
