:root {
  --bg: #fff;
  --fg: #111;
  --muted: #666;
  --border: #ddd;
  --card-bg: #f8f8f8;
  --accent: #2563eb;
  --green: #16a34a;
  --yellow: #ca8a04;
  --orange: #ea580c;
  --red: #dc2626;
  --radius: 6px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111;
    --fg: #e5e5e5;
    --muted: #999;
    --border: #333;
    --card-bg: #1a1a1a;
    --accent: #60a5fa;
    --green: #4ade80;
    --yellow: #facc15;
    --orange: #fb923c;
    --red: #f87171;
  }
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--fg);
  line-height: 1.5;
  max-width: 960px;
  margin: 0 auto;
  padding: 1.5rem;
}

h1 { font-size: 1.4rem; margin-bottom: 0.5rem; }
h2 { font-size: 1.15rem; margin-bottom: 0.4rem; }
h3 { font-size: 1rem; margin-bottom: 0.3rem; }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Header / nav */
.header { display: flex; align-items: baseline; gap: 1.5rem; margin-bottom: 1.5rem; border-bottom: 1px solid var(--border); padding-bottom: 0.75rem; }
.header h1 { flex: 1; }
.header-title { color: var(--fg); text-decoration: none; }
.header-title:hover { color: var(--accent); }
.nav-link { cursor: pointer; color: var(--accent); background: none; border: none; font: inherit; }
.nav-link:hover { text-decoration: underline; }

/* Language selector */
.lang-selector { position: relative; }
.lang-toggle {
  display: flex; align-items: center; gap: 0.4rem;
  background: none; border: 1px solid var(--border); border-radius: var(--radius);
  padding: 0.25rem 0.6rem; font-size: 0.85rem; color: var(--fg); cursor: pointer;
  white-space: nowrap;
}
.lang-toggle:hover { border-color: var(--accent); }
.lang-icon { font-size: 1rem; line-height: 1; }
.lang-menu {
  display: none; position: absolute; right: 0; top: calc(100% + 4px);
  background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius);
  min-width: 220px; z-index: 100; box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  max-height: 70vh; overflow-y: auto;
}
.lang-selector.open .lang-menu { display: block; }
.lang-item {
  display: flex; align-items: baseline; gap: 0.5rem; width: 100%;
  padding: 0.45rem 0.75rem; background: none; border: none; border-bottom: 1px solid var(--border);
  color: var(--fg); font: inherit; font-size: 0.85rem; cursor: pointer; text-align: left;
}
.lang-item:last-child { border-bottom: none; }
.lang-item:hover { background: var(--card-bg); }
.lang-item.active { background: var(--card-bg); font-weight: 600; }
.lang-autonym { white-space: nowrap; }
.lang-translated { color: var(--muted); font-size: 0.8rem; font-weight: 300; white-space: nowrap; }

/* Upload area */
.upload-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 3rem 2rem;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s;
}
.upload-zone:hover, .upload-zone.dragover { border-color: var(--accent); }
.upload-zone input[type="file"] { display: none; }
.upload-zone p { color: var(--muted); margin-top: 0.5rem; font-size: 0.9rem; }

/* Device support note */
.upload-device-note { margin-top: 1rem; padding: 0.75rem 1rem; font-size: 0.85rem; color: var(--muted); border: 1px solid var(--border); border-radius: var(--radius); }
.upload-device-note p { margin-bottom: 0.3rem; line-height: 1.6; }
.upload-device-note p:last-child { margin-bottom: 0; }

/* Upload guide */
.upload-guide { margin-top: 1.5rem; padding: 1rem 1.25rem; background: var(--card-bg); border: 1px solid var(--border); border-radius: var(--radius); }
.upload-guide h3 { font-size: 0.95rem; margin-bottom: 0.5rem; }
.upload-guide ol { margin: 0; padding-left: 1.25rem; font-size: 0.85rem; color: var(--fg); line-height: 1.7; }
.upload-guide li { margin-bottom: 0.25rem; }

/* Privacy note */
.upload-privacy { margin-top: 1rem; padding: 1rem 1.25rem; border: 1px solid var(--border); border-radius: var(--radius); }
.upload-privacy h3 { font-size: 0.95rem; margin-bottom: 0.5rem; }
.upload-privacy p { font-size: 0.85rem; color: var(--muted); margin-bottom: 0.4rem; line-height: 1.6; }
.upload-privacy p:last-child { margin-bottom: 0; }
.upload-privacy a { color: var(--accent); }

/* Spinner */
.spinner-wrap { text-align: center; padding: 3rem; }
.spinner {
  display: inline-block; width: 32px; height: 32px;
  border: 3px solid var(--border); border-top-color: var(--accent);
  border-radius: 50%; animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Device info bar */
.device-bar { background: var(--card-bg); border-radius: var(--radius); padding: 0.75rem 1rem; margin-bottom: 1rem; font-size: 0.85rem; color: var(--muted); }

/* Period card */
.period-card { border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 1rem; overflow: hidden; }
.period-header {
  display: flex; align-items: center; gap: 0.75rem; padding: 0.75rem 1rem;
  background: var(--card-bg); user-select: none;
}
.period-header:hover { filter: brightness(0.95); }
.period-date { font-weight: 600; flex: 1; cursor: pointer; }
.period-stat { font-size: 0.85rem; color: var(--muted); cursor: pointer; }
.period-body { padding: 0.5rem 1rem 1rem; display: none; max-height: 0; overflow: hidden; transition: max-height 0.3s ease, padding 0.3s ease; }
.period-card.open .period-body { display: block; max-height: none; padding: 0.5rem 1rem 1rem; }

/* Select all */
.select-all-row { margin-bottom: 0.75rem; }
.select-all-row label { display: flex; align-items: center; gap: 0.5rem; cursor: pointer; font-size: 0.85rem; color: var(--muted); }
.select-all-row input[type="checkbox"] { width: 16px; height: 16px; cursor: pointer; accent-color: var(--accent); }

/* Period checkbox */
.period-checkbox { display: flex; align-items: center; cursor: pointer; flex-shrink: 0; }
.period-checkbox input[type="checkbox"] { width: 16px; height: 16px; cursor: pointer; accent-color: var(--accent); }

/* Excluded period */
.period-card.excluded { opacity: 0.45; }
.period-card.excluded .period-body { display: none; }
.period-card.excluded .period-header { cursor: default; }

/* AHI badge */
.ahi-badge {
  display: inline-block; padding: 0.15rem 0.5rem; border-radius: var(--radius);
  font-weight: 600; font-size: 0.85rem; color: #fff;
}
.ahi-normal { background: var(--green); }
.ahi-mild { background: var(--yellow); color: #000; }
.ahi-moderate { background: var(--orange); }
.ahi-severe { background: var(--red); }

/* Tables */
table { width: 100%; border-collapse: collapse; font-size: 0.85rem; margin-top: 0.5rem; }
th, td { padding: 0.3rem 0.5rem; text-align: left; border-bottom: 1px solid var(--border); }
th { font-weight: 600; color: var(--muted); font-size: 0.8rem; }
td.num { text-align: right; font-variant-numeric: tabular-nums; }

/* Charts */
.chart-wrap { margin-bottom: 1rem; }
.chart-container { position: relative; }
.hero-chart { height: 260px; margin-bottom: 0.75rem; }
.hero-chart canvas { width: 100% !important; height: 100% !important; }
.period-chart { height: 160px; margin: 0.5rem 0; }
.period-chart canvas { width: 100% !important; height: 100% !important; }

/* Session row */
.session-row { cursor: pointer; }
.session-row:hover { background: var(--card-bg); }

/* Zero-duration sessions */
.session-row.zero-duration { opacity: 0.45; }
.session-row.zero-duration td { color: var(--muted); font-size: 0.8rem; }
.session-detail { display: none; }
.session-row.open + .session-detail { display: table-row; }
.session-detail td { padding: 0.75rem; }

/* Event table (nested) */
.event-table { font-size: 0.8rem; margin-top: 0.5rem; }
.event-table th { font-size: 0.75rem; }

/* History list */
.history-item { display: flex; align-items: center; gap: 1rem; padding: 0.5rem 0; border-bottom: 1px solid var(--border); }
.history-item:last-child { border-bottom: none; }
.history-info { flex: 1; }
.history-date { font-size: 0.85rem; color: var(--muted); }
.history-summary { font-size: 0.9rem; }

/* Buttons */
button {
  cursor: pointer; border: 1px solid var(--border); background: var(--card-bg);
  color: var(--fg); padding: 0.35rem 0.75rem; border-radius: var(--radius);
  font: inherit; font-size: 0.85rem;
}
button:hover { filter: brightness(0.92); }
button.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
button.danger { color: var(--red); }

/* Export buttons */
.export-row { display: flex; gap: 0.5rem; margin-top: 1rem; flex-wrap: wrap; }

/* Methodology / disclaimer */
.methodology { margin-top: 1.5rem; font-size: 0.8rem; color: var(--muted); border-top: 1px solid var(--border); padding-top: 0.75rem; }

/* Summary grid */
.summary-grid { display: flex; gap: 1rem; flex-wrap: wrap; margin-bottom: 0.5rem; }
.summary-stat { font-size: 0.85rem; }
.summary-stat .label { color: var(--muted); font-size: 0.75rem; display: block; }
.summary-stat .value { font-weight: 600; }
