/* =====================================================
   SLEEP COMPANION — Design Tokens & Component Styles
   Night-sky palette optimized for a sleep/health context
   ===================================================== */

@import url('https://api.fontshare.com/v2/css?f[]=general-sans@400,500,600,700&f[]=cabinet-grotesk@700,800&display=swap');

:root {
  /* Type Scale */
  --text-xs:   clamp(0.75rem,  0.7rem  + 0.25vw, 0.875rem);
  --text-sm:   clamp(0.875rem, 0.8rem  + 0.35vw, 1rem);
  --text-base: clamp(1rem,     0.95rem + 0.25vw, 1.125rem);
  --text-lg:   clamp(1.125rem, 1rem    + 0.75vw, 1.5rem);
  --text-xl:   clamp(1.5rem,   1.2rem  + 1.25vw, 2.25rem);

  /* Spacing */
  --space-1:  0.25rem;
  --space-2:  0.5rem;
  --space-3:  0.75rem;
  --space-4:  1rem;
  --space-5:  1.25rem;
  --space-6:  1.5rem;
  --space-8:  2rem;
  --space-10: 2.5rem;
  --space-12: 3rem;
  --space-16: 4rem;
  --space-20: 5rem;

  /* Fonts */
  --font-body: 'General Sans', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-display: 'Cabinet Grotesk', 'General Sans', sans-serif;

  /* Radius */
  --radius-sm: 0.375rem;
  --radius-md: 0.5rem;
  --radius-lg: 0.75rem;
  --radius-xl: 1rem;
  --radius-2xl: 1.25rem;
  --radius-full: 9999px;

  /* Transitions */
  --transition-interactive: 180ms cubic-bezier(0.16, 1, 0.3, 1);
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
}

/* ===== DARK (DEFAULT) ===== */
:root, [data-theme="dark"] {
  --color-bg:              #0c0e1a;
  --color-surface:         #141726;
  --color-surface-2:       #1a1e32;
  --color-surface-3:       #21263e;
  --color-border:          #2a3050;
  --color-divider:         #1e2340;

  --color-text:            #e0e2f0;
  --color-text-muted:      #8890b0;
  --color-text-faint:      #555e80;

  /* Primary — serene blue-violet */
  --color-primary:         #7b8cff;
  --color-primary-hover:   #6b7cef;
  --color-primary-active:  #5b6cdf;
  --color-primary-glow:    rgba(123, 140, 255, 0.12);

  /* Accent palette for charts */
  --color-deep:            #6366f1;
  --color-rem:             #a78bfa;
  --color-light-sleep:     #38bdf8;
  --color-awake:           #f97316;

  /* Semantic */
  --color-success:         #34d399;
  --color-warning:         #fbbf24;
  --color-error:           #f87171;
  --color-info:            #60a5fa;

  /* Shadows */
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.4);
  --shadow-lg: 0 12px 40px rgba(0,0,0,0.5);
  --shadow-glow: 0 0 24px rgba(123, 140, 255, 0.08);
}

/* ===== LIGHT ===== */
[data-theme="light"] {
  --color-bg:              #f4f5fb;
  --color-surface:         #ffffff;
  --color-surface-2:       #f0f1f8;
  --color-surface-3:       #e8e9f4;
  --color-border:          #d0d3e4;
  --color-divider:         #e0e2ef;

  --color-text:            #1a1d2e;
  --color-text-muted:      #5c6080;
  --color-text-faint:      #9498b0;

  --color-primary:         #4f56c8;
  --color-primary-hover:   #4048b8;
  --color-primary-active:  #353ca0;
  --color-primary-glow:    rgba(79, 86, 200, 0.08);

  --color-deep:            #4f46e5;
  --color-rem:             #7c3aed;
  --color-light-sleep:     #0284c7;
  --color-awake:           #ea580c;

  --color-success:         #059669;
  --color-warning:         #d97706;
  --color-error:           #dc2626;
  --color-info:            #2563eb;

  --shadow-sm: 0 1px 3px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.08);
  --shadow-lg: 0 12px 40px rgba(0,0,0,0.12);
  --shadow-glow: 0 0 24px rgba(79, 86, 200, 0.05);
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme]) {
    --color-bg:              #f4f5fb;
    --color-surface:         #ffffff;
    --color-surface-2:       #f0f1f8;
    --color-surface-3:       #e8e9f4;
    --color-border:          #d0d3e4;
    --color-divider:         #e0e2ef;
    --color-text:            #1a1d2e;
    --color-text-muted:      #5c6080;
    --color-text-faint:      #9498b0;
    --color-primary:         #4f56c8;
    --color-primary-hover:   #4048b8;
    --color-primary-active:  #353ca0;
    --color-primary-glow:    rgba(79, 86, 200, 0.08);
    --color-deep:            #4f46e5;
    --color-rem:             #7c3aed;
    --color-light-sleep:     #0284c7;
    --color-awake:           #ea580c;
    --color-success:         #059669;
    --color-warning:         #d97706;
    --color-error:           #dc2626;
    --color-info:            #2563eb;
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.06);
    --shadow-md: 0 4px 16px rgba(0,0,0,0.08);
    --shadow-lg: 0 12px 40px rgba(0,0,0,0.12);
    --shadow-glow: 0 0 24px rgba(79, 86, 200, 0.05);
  }
}

/* ===== LAYOUT ===== */
html, body { height: 100%; overflow: hidden; margin: 0; }

.app {
  display: grid;
  grid-template-columns: 260px 1fr;
  grid-template-rows: 1fr;
  height: 100dvh;
}

.sidebar {
  grid-row: 1 / -1;
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: var(--space-6) var(--space-5);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.sidebar-logo {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) 0;
  margin-bottom: var(--space-4);
}

.sidebar-logo svg {
  width: 36px;
  height: 36px;
  flex-shrink: 0;
}

.sidebar-logo span {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: var(--text-lg);
  color: var(--color-text);
  letter-spacing: -0.02em;
}

.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  list-style: none;
}

.sidebar-nav li button {
  width: 100%;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-lg);
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--color-text-muted);
  text-align: left;
  background: transparent;
  border: none;
  cursor: pointer;
  transition: all var(--transition-interactive);
}

.sidebar-nav li button:hover {
  color: var(--color-text);
  background: var(--color-primary-glow);
}

.sidebar-nav li button.active {
  color: var(--color-primary);
  background: var(--color-primary-glow);
  font-weight: 600;
}

.sidebar-nav li button svg {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}

.sidebar-section-title {
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--color-text-faint);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: var(--space-4) var(--space-4) var(--space-2);
}

.sidebar-bottom {
  margin-top: auto;
  padding-top: var(--space-4);
  border-top: 1px solid var(--color-divider);
}

.theme-toggle {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-lg);
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  width: 100%;
  cursor: pointer;
}

.theme-toggle:hover {
  background: var(--color-primary-glow);
  color: var(--color-text);
}

.main {
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: var(--space-8);
}

.main-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-8);
}

.main-header h1 {
  font-family: var(--font-display);
  font-size: var(--text-xl);
  font-weight: 700;
  color: var(--color-text);
  letter-spacing: -0.02em;
}

.main-header .date-range {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  background: var(--color-surface);
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-full);
  border: 1px solid var(--color-border);
}

/* ===== KPI CARDS ===== */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--space-5);
  margin-bottom: var(--space-8);
}

.kpi-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-2xl);
  padding: var(--space-5) var(--space-6);
  position: relative;
  overflow: hidden;
  transition: box-shadow var(--transition-interactive);
}

.kpi-card:hover {
  box-shadow: var(--shadow-glow);
}

.kpi-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: var(--kpi-accent, var(--color-primary));
  border-radius: var(--radius-2xl) var(--radius-2xl) 0 0;
}

.kpi-label {
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: var(--space-2);
}

.kpi-value {
  font-family: var(--font-display);
  font-size: var(--text-xl);
  font-weight: 800;
  color: var(--color-text);
  font-variant-numeric: tabular-nums lining-nums;
  line-height: 1;
  margin-bottom: var(--space-2);
}

.kpi-delta {
  font-size: var(--text-xs);
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 2px var(--space-2);
  border-radius: var(--radius-full);
}

.kpi-delta.positive { color: var(--color-success); background: rgba(52, 211, 153, 0.1); }
.kpi-delta.negative { color: var(--color-error); background: rgba(248, 113, 113, 0.1); }
.kpi-delta.neutral  { color: var(--color-text-faint); background: var(--color-surface-2); }

/* ===== CHART CARDS ===== */
.charts-grid {
  display: grid;
  gap: var(--space-5);
  margin-bottom: var(--space-8);
}

.charts-grid.two-col { grid-template-columns: 1fr 1fr; }
.charts-grid.full    { grid-template-columns: 1fr; }

.chart-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-2xl);
  padding: var(--space-6);
  transition: box-shadow var(--transition-interactive);
}

.chart-card:hover { box-shadow: var(--shadow-glow); }

.chart-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-5);
}

.chart-title {
  font-size: var(--text-base);
  font-weight: 600;
  color: var(--color-text);
}

.chart-subtitle {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  margin-top: var(--space-1);
}

.chart-legend {
  display: flex;
  gap: var(--space-4);
  flex-wrap: wrap;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

.legend-dot {
  width: 8px;
  height: 8px;
  border-radius: var(--radius-full);
  flex-shrink: 0;
}

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

.chart-container canvas {
  width: 100% !important;
}

/* ===== INSIGHTS PANEL ===== */
.insights-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-5);
  margin-bottom: var(--space-8);
}

.insight-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-2xl);
  padding: var(--space-6);
}

.insight-card h3 {
  font-size: var(--text-base);
  font-weight: 600;
  color: var(--color-text);
  margin-bottom: var(--space-4);
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.insight-item {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-3) 0;
  border-bottom: 1px solid var(--color-divider);
}

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

.insight-icon {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 16px;
}

.insight-icon.good { background: rgba(52, 211, 153, 0.12); color: var(--color-success); }
.insight-icon.warn { background: rgba(251, 191, 36, 0.12); color: var(--color-warning); }
.insight-icon.bad  { background: rgba(248, 113, 113, 0.12); color: var(--color-error); }
.insight-icon.info { background: rgba(96, 165, 250, 0.12); color: var(--color-info); }

.insight-text {
  font-size: var(--text-sm);
  color: var(--color-text);
  line-height: 1.5;
}

.insight-text .highlight {
  font-weight: 600;
  color: var(--color-primary);
}

/* ===== RECOMMENDATIONS ===== */
.recommendations {
  margin-bottom: var(--space-8);
}

.recommendations h2 {
  font-family: var(--font-display);
  font-size: var(--text-lg);
  font-weight: 700;
  color: var(--color-text);
  margin-bottom: var(--space-5);
}

.rec-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--space-5);
}

.rec-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-2xl);
  padding: var(--space-6);
  transition: all var(--transition-interactive);
  position: relative;
}

.rec-card:hover {
  box-shadow: var(--shadow-glow);
  border-color: var(--color-primary);
}

.rec-card .rec-emoji {
  font-size: 28px;
  margin-bottom: var(--space-3);
  display: block;
}

.rec-card h3 {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--color-text);
  margin-bottom: var(--space-2);
}

.rec-card p {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  line-height: 1.5;
}

.rec-card .rec-priority {
  position: absolute;
  top: var(--space-4);
  right: var(--space-4);
  font-size: var(--text-xs);
  font-weight: 600;
  padding: 2px var(--space-2);
  border-radius: var(--radius-full);
}

.rec-priority.high { color: var(--color-error); background: rgba(248,113,113,0.1); }
.rec-priority.medium { color: var(--color-warning); background: rgba(251,191,36,0.1); }
.rec-priority.low { color: var(--color-success); background: rgba(52,211,153,0.1); }

/* ===== SLEEP LOG TABLE ===== */
.sleep-log {
  margin-bottom: var(--space-8);
}

.sleep-log h2 {
  font-family: var(--font-display);
  font-size: var(--text-lg);
  font-weight: 700;
  color: var(--color-text);
  margin-bottom: var(--space-5);
}

.log-table-wrapper {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-2xl);
  overflow: hidden;
}

.log-table {
  width: 100%;
  font-size: var(--text-xs);
  font-variant-numeric: tabular-nums lining-nums;
}

.log-table thead {
  position: sticky;
  top: 0;
  z-index: 1;
}

.log-table th {
  background: var(--color-surface-2);
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: var(--space-3) var(--space-4);
  text-align: left;
  white-space: nowrap;
}

.log-table td {
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--color-divider);
  color: var(--color-text);
  white-space: nowrap;
}

.log-table tbody tr:hover {
  background: var(--color-primary-glow);
}

.log-table tbody tr:last-child td { border-bottom: none; }

.score-badge {
  display: inline-block;
  padding: 2px var(--space-2);
  border-radius: var(--radius-full);
  font-weight: 600;
  font-size: 11px;
  min-width: 32px;
  text-align: center;
}

.score-badge.excellent { background: rgba(52,211,153,0.15); color: var(--color-success); }
.score-badge.good { background: rgba(96,165,250,0.15); color: var(--color-info); }
.score-badge.fair { background: rgba(251,191,36,0.15); color: var(--color-warning); }
.score-badge.poor { background: rgba(248,113,113,0.15); color: var(--color-error); }

/* ===== BEDTIME HEATMAP ===== */
.heatmap-container {
  padding: var(--space-2) 0;
}

.heatmap-row {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-bottom: var(--space-1);
}

.heatmap-label {
  font-size: 11px;
  color: var(--color-text-muted);
  width: 36px;
  text-align: right;
  flex-shrink: 0;
}

.heatmap-cells {
  display: flex;
  gap: 3px;
  flex: 1;
}

.heatmap-cell {
  flex: 1;
  height: 20px;
  border-radius: 3px;
  position: relative;
  cursor: default;
}

.heatmap-cell[data-tooltip]:hover::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--color-surface-3);
  color: var(--color-text);
  font-size: 11px;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  white-space: nowrap;
  z-index: 10;
  box-shadow: var(--shadow-md);
  pointer-events: none;
}

/* ===== PAGE SECTIONS ===== */
.page-section { display: none; }
.page-section.active { display: block; }

.section-title {
  font-family: var(--font-display);
  font-size: var(--text-xl);
  font-weight: 700;
  color: var(--color-text);
  letter-spacing: -0.02em;
  margin-bottom: var(--space-3);
}

.section-subtitle {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  margin-bottom: var(--space-8);
  max-width: 500px;
}

/* ===== FOOTER ===== */
.app-footer {
  padding: var(--space-8) 0 var(--space-4);
  text-align: center;
  font-size: var(--text-xs);
  color: var(--color-text-faint);
}

.app-footer a {
  color: var(--color-text-muted);
  text-decoration: none;
}

.app-footer a:hover {
  color: var(--color-primary);
}

/* ===== MOBILE MENU ===== */
.mobile-menu-btn {
  display: none;
  position: fixed;
  top: var(--space-4);
  left: var(--space-4);
  z-index: 100;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-md);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  align-items: center;
  justify-content: center;
  color: var(--color-text);
}

.mobile-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 90;
}

/* ===== RESPONSIVE ===== */
@media (max-width: 1200px) {
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .charts-grid.two-col { grid-template-columns: 1fr; }
  .rec-grid { grid-template-columns: 1fr 1fr; }
  .insights-grid { grid-template-columns: 1fr; }
}

@media (max-width: 768px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    left: -280px;
    top: 0;
    bottom: 0;
    width: 260px;
    z-index: 95;
    transition: left 0.3s var(--ease-out);
  }
  .sidebar.open { left: 0; }
  .mobile-menu-btn { display: flex; }
  .mobile-overlay.active { display: block; }
  .main { padding: var(--space-6) var(--space-4); padding-top: calc(var(--space-6) + 56px); }
  .main-header { flex-direction: column; gap: var(--space-3); align-items: flex-start; }
  .kpi-grid { grid-template-columns: 1fr 1fr; }
  .rec-grid { grid-template-columns: 1fr; }
  .log-table-wrapper { overflow-x: auto; }
}

@media (max-width: 480px) {
  .kpi-grid { grid-template-columns: 1fr; }
}

/* ===== ANIMATION ===== */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

.kpi-card, .chart-card, .insight-card, .rec-card {
  animation: fadeIn 0.5s var(--ease-out) both;
}

.kpi-card:nth-child(1) { animation-delay: 0.05s; }
.kpi-card:nth-child(2) { animation-delay: 0.1s; }
.kpi-card:nth-child(3) { animation-delay: 0.15s; }
.kpi-card:nth-child(4) { animation-delay: 0.2s; }

/* ===== GOAL PROGRESS ===== */
.goal-bar-container {
  margin-top: var(--space-3);
}

.goal-label {
  display: flex;
  justify-content: space-between;
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  margin-bottom: var(--space-1);
}

.goal-bar {
  height: 6px;
  background: var(--color-surface-3);
  border-radius: var(--radius-full);
  overflow: hidden;
}

.goal-bar-fill {
  height: 100%;
  border-radius: var(--radius-full);
  background: var(--color-primary);
  transition: width 1s var(--ease-out);
}

/* ===== CORRELATION GRID ===== */
.correlation-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-3) 0;
  border-bottom: 1px solid var(--color-divider);
}

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

.corr-label {
  font-size: var(--text-sm);
  color: var(--color-text);
}

.corr-value {
  font-size: var(--text-sm);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.corr-value.strong-pos { color: var(--color-success); }
.corr-value.weak-pos { color: var(--color-info); }
.corr-value.neutral { color: var(--color-text-faint); }
.corr-value.weak-neg { color: var(--color-warning); }
.corr-value.strong-neg { color: var(--color-error); }
