:root {
  color-scheme: light;
  --surface-1: #fcfcfb;
  --page-plane: #f9f9f7;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --gridline: #e1e0d9;
  --baseline: #c3c2b7;
  --series-1: #2a78d6;
  --border: rgba(11, 11, 11, 0.10);
  --danger: #d03b3b;

  /* Categorical palette - fixed order, used to color-code compounds */
  --cat-1: #2a78d6; /* blue */
  --cat-2: #eb6834; /* orange */
  --cat-3: #1baf7a; /* aqua */
  --cat-4: #eda100; /* yellow */
  --cat-5: #e87ba4; /* magenta */
  --cat-6: #008300; /* green */
  --cat-7: #4a3aa7; /* violet */
  --cat-8: #e34948; /* red */
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --surface-1: #1a1a19;
    --page-plane: #0d0d0d;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --gridline: #2c2c2a;
    --baseline: #383835;
    --series-1: #3987e5;
    --border: rgba(255, 255, 255, 0.10);
    --danger: #e66767;

    --cat-1: #3987e5;
    --cat-2: #d95926;
    --cat-3: #199e70;
    --cat-4: #c98500;
    --cat-5: #d55181;
    --cat-6: #008300;
    --cat-7: #9085e9;
    --cat-8: #e66767;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-1: #1a1a19;
  --page-plane: #0d0d0d;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --gridline: #2c2c2a;
  --baseline: #383835;
  --series-1: #3987e5;
  --border: rgba(255, 255, 255, 0.10);
  --danger: #e66767;

  --cat-1: #3987e5;
  --cat-2: #d95926;
  --cat-3: #199e70;
  --cat-4: #c98500;
  --cat-5: #d55181;
  --cat-6: #008300;
  --cat-7: #9085e9;
  --cat-8: #e66767;
}

/* Compound color slots - assigned by each compound's position, cycling.
   --accent is what dots, swatches, and the chart read; falls back to blue. */
.cat-0 { --accent: var(--cat-1); }
.cat-1 { --accent: var(--cat-2); }
.cat-2 { --accent: var(--cat-3); }
.cat-3 { --accent: var(--cat-4); }
.cat-4 { --accent: var(--cat-5); }
.cat-5 { --accent: var(--cat-6); }
.cat-6 { --accent: var(--cat-7); }
.cat-7 { --accent: var(--cat-8); }

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--page-plane);
  color: var(--text-primary);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 15px;
  line-height: 1.45;
}

.app {
  max-width: 1080px;
  margin: 0 auto;
  /* safe-area insets keep content clear of the notch/home bar when launched from the home screen */
  padding:
    max(24px, env(safe-area-inset-top))
    max(20px, env(safe-area-inset-right))
    max(64px, env(safe-area-inset-bottom))
    max(20px, env(safe-area-inset-left));
}

.app-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 24px;
}

.header-text h1 {
  margin: 0;
  font-size: 26px;
  font-weight: 700;
}

.subtitle {
  margin: 4px 0 0;
  color: var(--text-secondary);
  font-size: 14px;
}

.storage-badge {
  display: inline-block;
  margin-top: 8px;
  font-size: 11px;
  color: var(--text-muted);
}
.storage-badge.is-synced { color: var(--series-1); font-weight: 600; }

.error-banner {
  margin: 0 0 16px;
  padding: 10px 14px;
  border-radius: 8px;
  border: 1px solid var(--danger);
  color: var(--danger);
  background: color-mix(in srgb, var(--danger) 10%, transparent);
  font-size: 13px;
}

.btn:disabled { opacity: 0.6; cursor: not-allowed; }
.btn:disabled:hover { background: transparent; }

.layout {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

.chart-card, .entries-card, .weight-card {
  grid-column: 1 / -1;
}

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

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
}

.card h2 {
  margin: 0 0 14px;
  font-size: 16px;
  font-weight: 600;
}

/* Buttons */
.btn {
  font: inherit;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-primary);
  cursor: pointer;
  padding: 8px 14px;
}
.btn:hover { background: var(--gridline); }
.btn-primary {
  background: var(--series-1);
  border-color: var(--series-1);
  color: #ffffff;
  font-weight: 600;
}
.btn-primary:hover { filter: brightness(1.08); }
.btn-ghost { border-color: transparent; color: var(--text-secondary); }
.btn-icon {
  width: 32px; height: 32px; padding: 0;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 18px; line-height: 1;
}
.btn-pill {
  padding: 6px 12px;
  font-size: 13px;
  border-radius: 999px;
  color: var(--text-secondary);
}
.btn-pill.is-active {
  background: color-mix(in srgb, var(--series-1) 10%, transparent);
  border-color: var(--series-1);
  color: var(--series-1);
  font-weight: 600;
}
.hidden { display: none !important; }

/* Calendar */
.calendar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}
.calendar-header h2 { margin: 0; font-size: 16px; }
.calendar-weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  text-align: center;
  color: var(--text-muted);
  font-size: 12px;
  margin-bottom: 4px;
}
.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
}
.cal-day {
  position: relative;
  aspect-ratio: 1 / 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  border-radius: 8px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-primary);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}
.cal-day:hover { background: var(--gridline); }
.cal-day.is-empty { visibility: hidden; cursor: default; }
.cal-day.is-today { border-color: var(--baseline); font-weight: 700; }
.cal-day.is-selected {
  border-color: var(--series-1);
  background: color-mix(in srgb, var(--series-1) 10%, transparent);
}
.cal-day-dots { display: flex; align-items: center; gap: 3px; }
.cal-day .dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--accent, var(--series-1));
}
.calendar-legend {
  margin-top: 12px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px 14px;
  font-size: 12px;
  color: var(--text-secondary);
}
.legend-item { display: inline-flex; align-items: center; gap: 5px; }
.legend-dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--accent, var(--series-1)); display: inline-block;
}
.legend-today {
  padding: 1px 8px;
  border: 1px solid var(--baseline);
  border-radius: 999px;
}

/* Form */
.entry-form { display: flex; flex-direction: column; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--text-secondary); }
.field .optional { color: var(--text-muted); font-weight: 400; }
.field input, .field select {
  font: inherit;
  padding: 9px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--page-plane);
  color: var(--text-primary);
}
.field input:focus, .field select:focus { outline: 2px solid var(--series-1); outline-offset: 1px; }
.form-actions { display: flex; gap: 10px; margin-top: 2px; }

.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 1; min-width: 0; }
.field-row .field.field-narrow { flex: 0 0 110px; }

.card-hint { margin: -8px 0 14px; font-size: 12px; color: var(--text-muted); }

.stat-tile {
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--gridline);
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.stat-label { font-size: 12px; color: var(--text-secondary); }
.stat-value { font-size: 32px; font-weight: 600; }
.stat-sub { font-size: 12px; color: var(--text-muted); }

/* Chart */
.chart-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}
.chart-subtitle { margin: 2px 0 0; font-size: 13px; color: var(--text-secondary); }
.chart-header-controls { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.compound-select {
  font: inherit;
  font-size: 13px;
  padding: 7px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--page-plane);
  color: var(--text-primary);
}
.range-controls { display: flex; gap: 6px; }
.chart-container { position: relative; width: 100%; }
.chart-svg { width: 100%; height: 280px; display: block; overflow: visible; }
.chart-tooltip {
  position: absolute;
  pointer-events: none;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 12px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.12);
  transform: translate(-50%, -110%);
  white-space: nowrap;
}
.chart-tooltip .tt-date { color: var(--text-secondary); }
.chart-tooltip .tt-value { font-weight: 600; }

.empty-state { color: var(--text-muted); font-size: 13px; }

/* Entries list */
/* Chart SVG marks */
.grid-line { stroke: var(--gridline); stroke-width: 1; }
.axis-baseline { stroke: var(--baseline); stroke-width: 1; }
.axis-label { fill: var(--text-muted); font-size: 11px; }
.today-line { stroke: var(--baseline); stroke-width: 1; }
.today-label { fill: var(--text-muted); font-size: 10px; }
.area-fill { fill: color-mix(in srgb, var(--accent, var(--series-1)) 12%, transparent); stroke: none; }
.level-line { fill: none; stroke: var(--accent, var(--series-1)); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.level-line-projected {
  fill: none; stroke: var(--accent, var(--series-1)); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round;
  stroke-dasharray: 3 4; opacity: 0.55;
}
.dose-marker { fill: var(--accent, var(--series-1)); stroke: var(--surface-1); stroke-width: 2; }
.end-marker { fill: var(--accent, var(--series-1)); stroke: var(--surface-1); stroke-width: 2; }
.tail-marker { fill: var(--text-muted); stroke: var(--surface-1); stroke-width: 2; }
.end-label { fill: var(--text-primary); font-size: 12px; font-weight: 600; }
.hover-line { stroke: var(--baseline); stroke-width: 1; }
.hover-dot { fill: var(--accent, var(--series-1)); stroke: var(--surface-1); stroke-width: 2; }
.usage-bar { fill: var(--accent, var(--series-1)); }

/* Entries list */
.entries-list { display: flex; flex-direction: column; }
.entry-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 4px;
  border-bottom: 1px solid var(--gridline);
}
.entry-row:last-child { border-bottom: none; }
.entry-main { display: flex; flex-direction: column; cursor: pointer; }
.entry-date { font-weight: 600; font-size: 14px; }
.entry-meta { font-size: 12px; color: var(--text-secondary); }
.entry-row-actions { display: flex; gap: 6px; }
.btn-danger { color: var(--danger); border-color: transparent; }
.btn-danger:hover { background: var(--gridline); }

.entry-compound-tag {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-weight: 600;
  color: var(--text-primary);
}
.entry-compound-tag .swatch {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--accent, var(--series-1));
  flex-shrink: 0;
}

/* Compounds card */
.compounds-list { display: flex; flex-direction: column; margin-bottom: 16px; }
.compound-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 4px;
  border-bottom: 1px solid var(--gridline);
}
.compound-row:last-child { border-bottom: none; }
.compound-swatch {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--accent, var(--series-1));
  flex-shrink: 0;
}
.compound-info { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.compound-name { font-weight: 600; font-size: 14px; }
.compound-meta { font-size: 12px; color: var(--text-secondary); }
.compound-row-actions { display: flex; gap: 4px; flex-shrink: 0; }
.compound-row-actions .btn { padding: 6px 10px; font-size: 12px; }
