:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --surface-2: #efede8;
  --text: #1c1b19;
  --muted: #6b6861;
  --border: #e2dfd8;
  --accent: #d2551e;
  --accent-soft: #fbe9df;
  --good: #2f7d4f;
  --good-soft: #e3f1e8;
  --warn: #a86a00;
  --warn-soft: #fbf0d9;
  --bad: #b3352b;
  --fitness: #2b6cb0;
  --fatigue: #c2410c;
  --form: #6d28d9;
  --ride: #d2551e;
  --indoor: #b4432a;
  --strength: #2b6cb0;
  --mobility: #2f7d4f;
  --hike: #8a6d1f;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(0 0 0 / 5%);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #151514;
    --surface: #1f1f1d;
    --surface-2: #2a2a27;
    --text: #f1efea;
    --muted: #a19d94;
    --border: #34332f;
    --accent: #f07a45;
    --accent-soft: #3a2419;
    --good: #5cc488;
    --good-soft: #1c3226;
    --warn: #e2a63b;
    --warn-soft: #3a2e14;
    --bad: #ef7b70;
    --fitness: #6ea8f0;
    --fatigue: #f28a55;
    --form: #a78bfa;
    --ride: #f07a45;
    --indoor: #e2694c;
    --strength: #6ea8f0;
    --mobility: #5cc488;
    --hike: #d4b35a;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
}
button, input, select, textarea { font: inherit; color: inherit; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
}
.topbar h1 { font-size: 22px; margin: 0; letter-spacing: -0.01em; }
.icon-btn {
  border: 0; background: var(--surface); border-radius: 999px;
  width: 36px; height: 36px; display: grid; place-items: center;
  box-shadow: var(--shadow); cursor: pointer;
}
.icon-btn.spinning svg { animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

main { max-width: 640px; margin: 0 auto; padding: 4px 16px 24px; }

.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 5;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--surface); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button {
  border: 0; background: none; padding: 8px 0 6px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  color: var(--muted); font-size: 11px;
}
.tabbar svg { width: 22px; height: 22px; }
.tabbar button.active { color: var(--accent); }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; margin: 12px 0; box-shadow: var(--shadow);
}
.card h2 { font-size: 15px; margin: 0 0 8px; color: var(--muted); font-weight: 600; letter-spacing: 0.01em; }
.card h3 { font-size: 16px; margin: 12px 0 4px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.row { display: flex; align-items: center; gap: 8px; }
.spread { display: flex; align-items: center; justify-content: space-between; gap: 8px; }

/* Week navigation */
.weeknav { display: flex; align-items: center; justify-content: space-between; margin: 4px 0; }
.weeknav button { border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 6px 12px; cursor: pointer; }
.weeknav .label { font-weight: 600; }

/* Form card */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat .v { font-size: 26px; font-weight: 650; font-variant-numeric: tabular-nums; line-height: 1.1; }
.stat .k { font-size: 12px; color: var(--muted); }
.pill { display: inline-block; font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px; }
.pill.good { background: var(--good-soft); color: var(--good); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.bad { background: var(--accent-soft); color: var(--bad); }
.pill.neutral { background: var(--surface-2); color: var(--muted); }

/* Markdown */
.md p { margin: 6px 0; }
.md ul { margin: 6px 0; padding-left: 20px; }
.md li { margin: 2px 0; }
.md h1, .md h2, .md h3 { font-size: 15px; margin: 12px 0 4px; color: var(--text); }
.md strong { font-weight: 650; }
.collapsed .md { max-height: 7.5em; overflow: hidden; mask-image: linear-gradient(#000 60%, transparent); -webkit-mask-image: linear-gradient(#000 60%, transparent); }
.linkbtn { border: 0; background: none; color: var(--accent); padding: 4px 0; cursor: pointer; font-weight: 600; }

/* Days */
.day { margin: 14px 0 6px; }
.day-head { display: flex; align-items: baseline; gap: 8px; font-weight: 650; }
.day-head .date { color: var(--muted); font-weight: 400; font-size: 14px; }
.day.today .day-head { color: var(--accent); }
.rest { color: var(--muted); font-size: 14px; padding: 4px 0 0 2px; }

.item {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  margin: 6px 0; overflow: hidden; box-shadow: var(--shadow);
}
.item > summary, .item > .item-head {
  list-style: none; display: grid; grid-template-columns: 6px 1fr auto; gap: 10px; align-items: center;
  padding: 10px 12px 10px 0; cursor: pointer;
}
.item > summary::-webkit-details-marker { display: none; }
.item .bar { align-self: stretch; border-radius: 0 3px 3px 0; background: var(--c, var(--muted)); }
.item .name { font-weight: 600; }
.item .meta { font-size: 13px; color: var(--muted); }
.item .body { padding: 0 14px 12px 16px; border-top: 1px solid var(--border); }
.item .actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.item.done .name { text-decoration: line-through; text-decoration-color: var(--muted); }
.check { width: 24px; height: 24px; border-radius: 999px; border: 2px solid var(--border); display: grid; place-items: center; }
.done .check { background: var(--good); border-color: var(--good); color: #fff; }
.actual .check { border-color: transparent; }

.t-Ride { --c: var(--ride); }
.t-VirtualRide { --c: var(--indoor); }
.t-WeightTraining { --c: var(--strength); }
.t-Yoga { --c: var(--mobility); }
.t-Hike, .t-Walk { --c: var(--hike); }

.btn {
  border: 1px solid var(--border); background: var(--surface); border-radius: 10px;
  padding: 8px 14px; font-weight: 600; cursor: pointer;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn:disabled { opacity: 0.5; cursor: default; }

/* Forms */
label { display: block; font-size: 13px; color: var(--muted); margin: 10px 0 4px; }
input, select, textarea {
  width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface-2); font-size: 16px;
}
textarea { min-height: 80px; resize: vertical; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.seg { display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; flex-wrap: wrap; }
.seg button { flex: 1; border: 0; background: none; padding: 7px 6px; border-radius: 8px; font-size: 14px; cursor: pointer; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: var(--shadow); }

/* Charts */
.chart { width: 100%; height: auto; display: block; touch-action: pan-y; }
.chart text { fill: var(--muted); font-size: 10px; font-family: inherit; }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); margin-top: 6px; flex-wrap: wrap; }
.legend i { display: inline-block; width: 10px; height: 3px; border-radius: 2px; margin-right: 5px; vertical-align: middle; }
.readout { font-size: 13px; min-height: 1.4em; font-variant-numeric: tabular-nums; }

table { width: 100%; border-collapse: collapse; font-size: 14px; font-variant-numeric: tabular-nums; }
th, td { text-align: right; padding: 6px 4px; border-bottom: 1px solid var(--border); }
th:first-child, td:first-child { text-align: left; }
th { color: var(--muted); font-weight: 600; font-size: 12px; }

.empty { text-align: center; padding: 28px 12px; color: var(--muted); }
.error { color: var(--bad); }
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(84px + env(safe-area-inset-bottom)); z-index: 10;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: 14px;
  box-shadow: 0 4px 16px rgb(0 0 0 / 20%);
}
.skeleton { height: 90px; border-radius: var(--radius); background: var(--surface-2); margin: 12px 0; animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.5; } }
@media (prefers-reduced-motion: reduce) { .skeleton, .icon-btn.spinning svg { animation: none; } }
