/* ---------- Design tokens (Royal Symphony) ---------- */
:root {
  color-scheme: light;

  /* chrome */
  --page-plane: #dbe4ee;
  --surface: #f7f9fb;
  --text-primary: #0b1f33;
  --text-secondary: #3c536b;
  --text-muted: #7c93a8;
  --gridline: #c7d4e0;
  --baseline: #a9bccd;
  --border: rgba(5, 74, 145, 0.12);

  /* categorical series */
  --series-blue: #3e7cb1;
  --series-orange: #f17300;
  --series-teal: #129b5f;
  --series-gold: #d19a10;
  --series-rose: #c65a86;
  --series-emerald: #1f8a4c;
  --series-amethyst: #5b3fa0;
  --series-garnet: #c94040;

  /* sequential (blue), light -> dark, drawn from the brand's own ramp */
  --seq-100: #dbe4ee; /* Alice Blue */
  --seq-400: #81a4cd; /* Wisteria Blue */
  --seq-600: #3e7cb1; /* Steel Blue */
  --seq-800: #054a91; /* Steel Azure */

  /* status - fixed, never themed */
  --status-good: #0ca30c;
  --status-warning: #fab219;
  --status-serious: #ec835a;
  --status-critical: #d03b3b;

  --radius-card: 12px;
  --radius-control: 8px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page-plane: #0a1622;
    --surface: #10202f;
    --text-primary: #f2f6fa;
    --text-secondary: #b9c8d6;
    --text-muted: #7c93a8;
    --gridline: #1c3247;
    --baseline: #2c4054;
    --border: rgba(255, 255, 255, 0.1);

    --series-blue: #3d84c9;
    --series-orange: #d96500;
    --series-teal: #0f9d66;
    --series-gold: #b3860e;
    --series-rose: #cf6693;
    --series-emerald: #279c57;
    --series-amethyst: #8f7cd4;
    --series-garnet: #dd6161;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page-plane: #0a1622;
  --surface: #10202f;
  --text-primary: #f2f6fa;
  --text-secondary: #b9c8d6;
  --text-muted: #7c93a8;
  --gridline: #1c3247;
  --baseline: #2c4054;
  --border: rgba(255, 255, 255, 0.1);

  --series-blue: #3d84c9;
  --series-orange: #d96500;
  --series-teal: #0f9d66;
  --series-gold: #b3860e;
  --series-rose: #cf6693;
  --series-emerald: #279c57;
  --series-amethyst: #8f7cd4;
  --series-garnet: #dd6161;
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  background: var(--page-plane);
  color: var(--text-primary);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}
body { min-height: 100vh; }

.app-shell {
  max-width: 1400px;
  margin: 0 auto;
  padding: 16px clamp(16px, 3vw, 40px) 64px;
}

/* ---------- Header ---------- */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 0 24px;
  flex-wrap: wrap;
}
.app-title {
  font-size: 1.25rem;
  font-weight: 600;
  margin: 0;
}
.app-nav {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}
.app-nav a {
  padding: 8px 14px;
  border-radius: var(--radius-control);
  text-decoration: none;
  color: var(--text-secondary);
  font-size: 0.9rem;
  font-weight: 500;
}
.app-nav a[aria-current="page"] {
  background: var(--surface);
  color: var(--text-primary);
  border: 1px solid var(--border);
}
.app-nav a[data-disabled="true"] {
  color: var(--text-muted);
  pointer-events: none;
}
.theme-toggle {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-secondary);
  border-radius: var(--radius-control);
  padding: 8px 12px;
  font-size: 0.85rem;
  cursor: pointer;
}

/* ---------- Date range control ---------- */
.range-control {
  display: flex;
  gap: 4px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}
.range-control button {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-secondary);
  border-radius: var(--radius-control);
  padding: 6px 14px;
  font-size: 0.85rem;
  cursor: pointer;
}
.range-control button[aria-pressed="true"] {
  background: var(--series-blue);
  color: #fff;
  border-color: var(--series-blue);
}

/* ---------- Stat tiles ---------- */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 24px;
}
.stat-tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  padding: 16px;
}
.stat-tile .label {
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin: 0 0 6px;
}
.stat-tile .value {
  font-size: 1.9rem;
  font-weight: 600;
  margin: 0;
  font-variant-numeric: proportional-nums;
}
.stat-tile .value .unit {
  font-size: 1rem;
  font-weight: 500;
  color: var(--text-secondary);
  margin-left: 4px;
}
.stat-tile .as-of {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin: 4px 0 0;
}

/* ---------- Chart cards ---------- */
.chart-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
  margin-bottom: 24px;
}
@media (min-width: 900px) {
  .chart-grid { grid-template-columns: 1fr 1fr; }
}
@media (min-width: 1800px) {
  .chart-grid { grid-template-columns: 1fr 1fr 1fr; }
}
.chart-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  padding: 16px;
  min-width: 0;
}
.chart-card h2 {
  font-size: 0.95rem;
  font-weight: 600;
  margin: 0 0 4px;
}
.chart-card .subtitle {
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin: 0 0 12px;
}
.chart-card.wide { grid-column: 1 / -1; }

.legend {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
  margin-bottom: 8px;
  font-size: 0.78rem;
  color: var(--text-secondary);
}
.legend .swatch {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.legend .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
}

.viz-svg { width: 100%; height: auto; display: block; overflow: visible; }
.viz-svg text {
  fill: var(--text-muted);
  font-size: 10px;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}
.viz-svg .axis-line { stroke: var(--baseline); stroke-width: 1; }
.viz-svg .gridline { stroke: var(--gridline); stroke-width: 1; }
.viz-svg .end-label { fill: var(--text-primary); font-weight: 600; }

.tooltip {
  position: absolute;
  pointer-events: none;
  background: var(--text-primary);
  color: var(--page-plane);
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 0.75rem;
  line-height: 1.4;
  opacity: 0;
  transition: opacity 0.1s ease;
  white-space: nowrap;
  z-index: 10;
}
.chart-wrap { position: relative; }

/* Hover popup for product/ingredient details - unlike .tooltip above, this
   holds real readable content, so it isn't pointer-events:none and it's
   shown via CSS :hover/:focus-within rather than JS positioning. */
.product-hover {
  position: relative;
  border-bottom: 1px dotted var(--text-secondary);
  cursor: help;
}
.product-hover-card {
  display: none;
  position: absolute;
  left: 0;
  top: 100%;
  margin-top: 6px;
  z-index: 50;
  width: 260px;
  max-width: 70vw;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-control);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.18);
  padding: 10px 12px;
  font-size: 0.78rem;
  line-height: 1.45;
  color: var(--text-primary);
  white-space: normal;
}
.product-hover:hover .product-hover-card,
.product-hover:focus-within .product-hover-card {
  display: block;
}
.product-hover-card .product-hover-brand {
  color: var(--text-secondary);
  font-size: 0.72rem;
  margin-bottom: 4px;
}
.product-hover-card ul {
  margin: 4px 0 0;
  padding-left: 16px;
}

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

/* ---------- Table ---------- */
.table-card { overflow-x: auto; }
table.sessions {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}
table.sessions th {
  text-align: left;
  color: var(--text-secondary);
  font-weight: 600;
  padding: 8px 10px;
  border-bottom: 1px solid var(--gridline);
  white-space: nowrap;
}
table.sessions td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--gridline);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
table.sessions .source-tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
  color: #fff;
}
.source-garmin { background: var(--series-blue); }
.source-hevy { background: var(--series-orange); }
.source-manual { background: var(--text-muted); }

/* ---------- Configurable graphs (gear icon + overlay + series pickers) ---------- */
.chart-card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
}
.gear-btn {
  border: 1px solid var(--border);
  background: var(--page-plane);
  color: var(--text-secondary);
  border-radius: var(--radius-control);
  width: 30px;
  height: 30px;
  flex: none;
  font-size: 0.95rem;
  cursor: pointer;
  line-height: 1;
}
.gear-btn:hover { color: var(--text-primary); }

.graph-stack { display: flex; flex-direction: column; gap: 16px; margin-top: 16px; }

.add-graph-btn {
  border: 1px dashed var(--border);
  background: transparent;
  color: var(--text-secondary);
  border-radius: var(--radius-card);
  padding: 14px;
  font-size: 0.85rem;
  cursor: pointer;
  width: 100%;
}
.add-graph-btn:hover { color: var(--text-primary); border-color: var(--text-secondary); }

.overlay-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(5, 10, 20, 0.45);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 5vh 16px;
  z-index: 100;
  overflow-y: auto;
}
.overlay-backdrop[hidden] { display: none; }
.overlay-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  padding: 20px;
  width: 100%;
  max-width: 480px;
}
.overlay-panel h3 {
  margin: 0 0 4px;
  font-size: 1.05rem;
}
.overlay-panel .subtitle {
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin: 0 0 16px;
}
.series-picker-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}
.series-picker-row .swatch-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex: none;
}
.series-picker-row select {
  flex: 1;
  font: inherit;
  font-size: 0.85rem;
  padding: 7px 8px;
  border-radius: var(--radius-control);
  border: 1px solid var(--border);
  background: var(--page-plane);
  color: var(--text-primary);
}
.series-picker-row select option:disabled { color: var(--text-muted); }
.series-picker-row .cg-multiplier-wrap {
  display: flex;
  align-items: center;
  gap: 4px;
  flex: none;
  font-size: 0.85rem;
  color: var(--text-secondary);
}
.series-picker-row .cg-multiplier-wrap[hidden] { display: none; }
.series-picker-row .cg-multiplier {
  width: 64px;
  font: inherit;
  font-size: 0.85rem;
  padding: 7px 8px;
  border-radius: var(--radius-control);
  border: 1px solid var(--border);
  background: var(--page-plane);
  color: var(--text-primary);
}
.overlay-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-top: 16px;
}
.overlay-actions .left-actions, .overlay-actions .right-actions {
  display: flex;
  gap: 8px;
}
.btn {
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  padding: 8px 16px;
  border-radius: var(--radius-control);
  border: 1px solid var(--border);
  background: var(--page-plane);
  color: var(--text-secondary);
  cursor: pointer;
}
.btn-primary {
  background: var(--seq-400);
  color: #04203a;
  border-color: var(--seq-400);
}
.btn-danger {
  background: transparent;
  color: var(--status-critical);
  border-color: var(--status-critical);
}
.saved-graphs-list { display: flex; flex-direction: column; gap: 8px; }
.saved-graphs-list .row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-control);
  font-size: 0.85rem;
}
.saved-graphs-list .empty-note { padding: 8px 0; }

/* ---------- Undo toast ---------- */
.undo-toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text-primary);
  color: var(--page-plane);
  padding: 10px 16px;
  border-radius: var(--radius-control);
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  gap: 10px;
  z-index: 200;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
}
.undo-toast button {
  background: none;
  border: none;
  color: var(--seq-400);
  font-weight: 600;
  font: inherit;
  cursor: pointer;
  padding: 0;
}
.undo-toast .undo-hint {
  opacity: 0.6;
  font-size: 0.75rem;
}

/* ---------- Threshold status (Blood Work and future threshold-driven pages) ---------- */
.status-good { color: var(--status-good); }
.status-warning { color: var(--status-warning); }
.status-critical { color: var(--status-critical); }
.status-bg-good { background: color-mix(in srgb, var(--status-good) 16%, transparent); }
.status-bg-warning { background: color-mix(in srgb, var(--status-warning) 20%, transparent); }
.status-bg-critical { background: color-mix(in srgb, var(--status-critical) 16%, transparent); }

/* ---------- Login page ---------- */
.login-shell {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  padding: 32px;
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.login-card h1 {
  font-size: 1.25rem;
  margin: 0;
}
.login-card .subtitle {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin: 0 0 8px;
}
.login-card input {
  font: inherit;
  padding: 10px 12px;
  border-radius: var(--radius-control);
  border: 1px solid var(--border);
  background: var(--page-plane);
  color: var(--text-primary);
}
.login-card button {
  font: inherit;
  font-weight: 600;
  padding: 10px 12px;
  border-radius: var(--radius-control);
  border: none;
  background: var(--series-blue);
  color: #fff;
  cursor: pointer;
}
.login-card .error {
  color: var(--status-critical);
  font-size: 0.82rem;
  margin: 0;
}
