:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f3f2ee;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-ink: #1c5cab;
  --accent-wash: rgba(42, 120, 214, 0.10);

  /* Categorical slots, fixed order (reference palette, light) */
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #6250d6; --s8: #e34948;

  --good: #0ca30c; --warning: #fab219; --serious: #ec835a; --critical: #d03b3b;
  --good-ink: #006300;

  --radius: 10px;
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.04);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321;
    --ink: #f0efec; --ink-2: #c3c2b7; --muted: #898781;
    --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5; --accent-ink: #86b6ef; --accent-wash: rgba(57, 135, 229, 0.14);
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --good-ink: #0ca30c;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321;
  --ink: #f0efec; --ink-2: #c3c2b7; --muted: #898781;
  --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10);
  --accent: #3987e5; --accent-ink: #86b6ef; --accent-wash: rgba(57, 135, 229, 0.14);
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  --good-ink: #0ca30c;
  --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--page); color: var(--ink); font-size: 15px; line-height: 1.5; }
h2 { font-size: 17px; font-weight: 600; margin: 0; }
h3 { font-size: 15px; font-weight: 600; margin: 0; }
.muted { color: var(--muted); font-size: 13px; }
.small { font-size: 13px; margin: 4px 0 16px; }
[hidden] { display: none !important; }

/* ── Top bar ─────────────────────────────────────────────── */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 24px;
  padding: 10px 24px; background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; white-space: nowrap; }
.brand svg { width: 24px; height: 24px; fill: none; stroke: var(--accent); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
.tabs { display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none; }
.tabs a {
  padding: 6px 12px; border-radius: 8px; color: var(--ink-2);
  text-decoration: none; font-weight: 500; white-space: nowrap;
}
.tabs a:hover { background: var(--surface-2); }
.tabs a.on { background: var(--accent-wash); color: var(--accent-ink); }
.user-area { margin-left: auto; display: flex; align-items: center; gap: 12px; white-space: nowrap; }
.freshness { color: var(--muted); font-size: 13px; }
.who { font-size: 13px; color: var(--ink-2); font-weight: 500; }
.btn.small-btn { padding: 4px 8px; font-size: 13px; }

main { max-width: 1180px; margin: 0 auto; padding: 24px; }

.section-head {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: 12px; flex-wrap: wrap; margin: 28px 0 12px;
}
.view > .section-head:first-child { margin-top: 0; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow);
}

/* ── Today ───────────────────────────────────────────────── */
.today-grid { display: grid; grid-template-columns: minmax(240px, 300px) 1fr; gap: 16px; }
.hero { display: flex; flex-direction: column; justify-content: center; gap: 4px; }
.hero .label { color: var(--ink-2); font-size: 13px; }
.hero .value { font-size: 56px; font-weight: 650; line-height: 1.05; }
.hero .value small { font-size: 20px; color: var(--muted); font-weight: 500; }
.hero .meter { height: 8px; border-radius: 4px; background: var(--grid); overflow: hidden; margin: 10px 0 8px; }
.hero .meter > div { height: 100%; border-radius: 4px; }
.status { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; font-size: 14px; }
.status svg { width: 16px; height: 16px; flex: none; }

.tiles { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px; }
.tile .label { color: var(--ink-2); font-size: 13px; }
.tile .value { font-size: 28px; font-weight: 600; line-height: 1.2; margin-top: 2px; }
.tile .value small { font-size: 14px; color: var(--muted); font-weight: 500; margin-left: 2px; }
.tile .sub { color: var(--muted); font-size: 13px; margin-top: 2px; }
.delta-up { color: var(--good-ink); }
.delta-down { color: var(--critical); }

.plan { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 16px; }
.day { display: flex; flex-direction: column; gap: 12px; }
.day-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.day-head .date { color: var(--muted); font-size: 13px; }
.session { border-top: 1px solid var(--grid); padding-top: 12px; }
.session:first-of-type { border-top: 0; padding-top: 0; }
.session-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.session p { margin: 4px 0 8px; color: var(--ink-2); font-size: 14px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px;
  background: var(--surface-2); color: var(--ink-2);
}
.chip .dot { width: 8px; height: 8px; border-radius: 50%; }
.chip.pushed { background: var(--accent-wash); color: var(--accent-ink); }
.steps { list-style: none; margin: 0; padding: 0; font-size: 13.5px; }
.steps li { display: grid; grid-template-columns: 76px 1fr; gap: 8px; padding: 3px 0; }
.steps .kind { color: var(--muted); text-transform: capitalize; }
.steps .what b { font-weight: 600; }
.steps .note { display: block; color: var(--muted); font-size: 12.5px; }
.steps .repeat { grid-template-columns: 1fr; }
.steps .repeat > div:first-child { font-weight: 600; color: var(--ink-2); }
.steps .repeat ul { list-style: none; padding-left: 12px; margin: 2px 0 0; border-left: 2px solid var(--grid); }
.empty-day { color: var(--muted); font-size: 14px; }
.notice {
  border: 1px dashed var(--axis); border-radius: var(--radius);
  padding: 20px; color: var(--ink-2); background: var(--surface);
}

.report { margin-top: 16px; }
.report summary { cursor: pointer; font-weight: 600; }
.markdown { font-size: 14.5px; max-width: 78ch; }
.markdown h1 { font-size: 20px; } .markdown h2 { font-size: 17px; margin-top: 20px; }
.markdown h3 { font-size: 15px; margin-top: 16px; }
.markdown table { border-collapse: collapse; font-size: 13.5px; }
.markdown th, .markdown td { border: 1px solid var(--grid); padding: 4px 8px; }
.markdown code { background: var(--surface-2); padding: 1px 4px; border-radius: 4px; }

/* ── Trends ──────────────────────────────────────────────── */
.segmented { display: inline-flex; background: var(--surface-2); border-radius: 8px; padding: 2px; flex-wrap: wrap; }
.segmented button {
  border: 0; background: transparent; color: var(--ink-2); font: inherit; font-size: 13px;
  padding: 4px 12px; border-radius: 6px; cursor: pointer;
}
.segmented button.on { background: var(--surface); color: var(--ink); font-weight: 600; box-shadow: 0 0 0 1px var(--border); }
.chart-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 16px; }
.chart-card h3 { margin-bottom: 2px; }
.chart-card .sub { color: var(--muted); font-size: 13px; margin-bottom: 8px; }
.chart-box { position: relative; height: 180px; }
.chart-box.tall { height: 280px; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 16px; margin-bottom: 8px; font-size: 13px; color: var(--ink-2); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }

.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th { text-align: left; font-weight: 600; color: var(--ink-2); font-size: 13px; padding: 6px 10px; border-bottom: 1px solid var(--axis); white-space: nowrap; }
.table td { padding: 7px 10px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
.table tr:last-child td { border-bottom: 0; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.scroll-x { overflow-x: auto; }

/* ── Goals ───────────────────────────────────────────────── */
.goal { margin-bottom: 12px; display: grid; grid-template-columns: 56px 1fr; gap: 16px; }
.goal .prio { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.goal .prio input { width: 52px; text-align: center; font-weight: 650; font-size: 18px; }
.goal .prio .muted { font-size: 11px; }
.goal-fields { display: grid; grid-template-columns: 2fr 1.4fr 1fr; gap: 10px 12px; }
.goal-fields .full { grid-column: 1 / -1; }
.field { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.field > span { font-size: 12px; color: var(--muted); font-weight: 500; }
.field.inline { flex-direction: row; align-items: center; gap: 8px; }
.field.inline > span { font-size: 14px; color: var(--ink); font-weight: 400; }
.goal-foot { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 4px 12px; grid-column: 1 / -1; }
input[type=text], input[type=date], input[type=number], input[type=password], input[type=time], textarea {
  font: inherit; font-size: 14px; color: var(--ink); background: var(--page);
  border: 1px solid var(--border); border-radius: 7px; padding: 6px 9px; width: 100%;
}
textarea { resize: vertical; line-height: 1.45; }
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.kv-row, .list-row { display: grid; gap: 8px; align-items: start; margin-bottom: 8px; }
.kv-row { grid-template-columns: minmax(140px, 1fr) 2fr 32px; }
.list-row { grid-template-columns: 1fr 32px; }

/* ── Buttons ─────────────────────────────────────────────── */
.btn {
  font: inherit; font-size: 14px; font-weight: 500; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--ink);
  border-radius: 8px; padding: 6px 14px;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.primary.danger { background: var(--critical); border-color: var(--critical); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--accent-ink); }
.btn.icon { padding: 4px; width: 32px; height: 32px; color: var(--muted); }
.btn.icon:hover { color: var(--critical); }
.actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.actions.end { justify-content: flex-end; }

.run-buttons { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; }
.btn.run { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; padding: 12px 14px; text-align: left; }
.btn.run span { color: var(--muted); font-size: 12.5px; }
.btn.run.danger b { color: var(--critical); }

.console-card { margin-top: 16px; padding: 0; overflow: hidden; }
.console-head { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; border-bottom: 1px solid var(--border); font-weight: 600; }
.console {
  margin: 0; padding: 12px 14px; height: 340px; overflow: auto;
  font: 12.5px/1.5 ui-monospace, "Cascadia Mono", Consolas, monospace;
  background: var(--page); color: var(--ink-2); white-space: pre-wrap; word-break: break-word;
}
.badge { font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px; display: inline-flex; align-items: center; gap: 5px; }
.badge:empty { display: none; }
.badge.running { background: var(--accent-wash); color: var(--accent-ink); }
.badge.succeeded { color: var(--good-ink); background: color-mix(in srgb, var(--good) 14%, transparent); }
.badge.failed { color: var(--critical); background: color-mix(in srgb, var(--critical) 14%, transparent); }

.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.list { padding: 4px 0; max-height: 360px; overflow: auto; }
.list button {
  display: flex; justify-content: space-between; gap: 8px; width: 100%;
  border: 0; background: none; font: inherit; font-size: 14px; color: var(--ink);
  padding: 8px 16px; cursor: pointer; text-align: left;
}
.list button:hover { background: var(--surface-2); }
.list .empty { padding: 12px 16px; color: var(--muted); font-size: 14px; }

dialog {
  border: 1px solid var(--border); border-radius: 14px; background: var(--surface); color: var(--ink);
  padding: 20px; width: min(860px, calc(100vw - 32px)); max-height: calc(100vh - 64px);
}
#confirm-dialog { width: min(460px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(0, 0, 0, 0.4); }
dialog p { color: var(--ink-2); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--ink); color: var(--page); padding: 8px 16px; border-radius: 8px;
  font-size: 14px; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.spinner { width: 10px; height: 10px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Auth & settings ───────────────────────────────────── */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.auth-card { width: min(400px, 100%); padding: 28px; }
.brand.big { font-size: 20px; }
.brand.big svg { width: 30px; height: 30px; }
.auth-card > p { margin: 6px 0 20px; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.center { text-align: center; margin: 4px 0 0; }
.field small { font-weight: 400; }
.form-error { color: var(--critical); font-size: 14px; margin: 10px 0 0; min-height: 1em; }
.form-error:empty { display: none; }
a { color: var(--accent-ink); }
.pw-grid { display: grid; grid-template-columns: 1fr 1fr auto; gap: 12px; align-items: end; }
.rule { border: 0; border-top: 1px solid var(--grid); margin: 16px 0; }
.danger-zone { display: flex; justify-content: space-between; align-items: center; gap: 16px; flex-wrap: wrap; }
.btn.danger-outline { color: var(--critical); border-color: color-mix(in srgb, var(--critical) 45%, transparent); }
.garmin-row { display: flex; justify-content: space-between; align-items: center; gap: 16px; flex-wrap: wrap; }
.garmin-form { display: grid; grid-template-columns: 1fr 1fr auto; gap: 12px; align-items: end; margin-top: 12px; }
.code { font: 600 14px ui-monospace, Consolas, monospace; letter-spacing: .04em; }
.connect-cta { display: flex; justify-content: space-between; align-items: center; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
#delete-dialog input { margin-top: 4px; }

/* ── Small screens ───────────────────────────────────────── */
@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; gap: 8px 16px; padding: 10px 16px; }
  .tabs { order: 3; width: 100%; }
  main { padding: 16px; }
  .today-grid, .two-col { grid-template-columns: 1fr; }
  .tiles { grid-template-columns: 1fr 1fr; }
  .chart-grid, .plan { grid-template-columns: 1fr; }
  .goal { grid-template-columns: 1fr; }
  .goal .prio { flex-direction: row; }
  .goal-fields, .pw-grid, .garmin-form { grid-template-columns: 1fr; }
  .freshness { display: none; }
  .kv-row { grid-template-columns: 1fr 1.3fr 32px; }
}
