:root {
  color-scheme: light;
  --bg: #f6f5f2;
  --surface: #fcfcfb;
  --surface-2: #efeee9;
  --border: #e2e0da;
  --text: #0b0b0b;
  --text-2: #52514e;
  --text-3: #7a7974;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --series-1: #2a78d6;
  --grid: #e6e5df;
  --done: #1f7a3a;
  --danger: #b4231f;
  --radius: 14px;
  --tap: 44px;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #151514;
    --surface: #1a1a19;
    --surface-2: #242422;
    --border: #2f2f2c;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --text-3: #93928a;
    --accent: #3987e5;
    --series-1: #3987e5;
    --grid: #2c2c2a;
    --done: #4cb26a;
    --danger: #ef6b67;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #151514;
  --surface: #1a1a19;
  --surface-2: #242422;
  --border: #2f2f2c;
  --text: #f0efec;
  --text-2: #c3c2b7;
  --text-3: #93928a;
  --accent: #3987e5;
  --series-1: #3987e5;
  --grid: #2c2c2a;
  --done: #4cb26a;
  --danger: #ef6b67;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.4;
  -webkit-text-size-adjust: 100%;
  padding: env(safe-area-inset-top) 0 calc(72px + env(safe-area-inset-bottom));
}
main { max-width: 720px; margin: 0 auto; padding: 12px 16px 24px; }
h1 { font-size: 26px; margin: 8px 0 12px; letter-spacing: -0.01em; }
h2 { font-size: 17px; margin: 0; }
h3 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-3); margin: 14px 0 6px; font-weight: 600; }
a { color: var(--accent); }
p { margin: 6px 0; }
.muted { color: var(--text-2); }
.small { font-size: 13px; }
.hidden, [hidden] { display: none !important; }

/* ---------- Cards ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; margin: 12px 0; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 6px; }
.card-head .count { color: var(--text-2); font-size: 14px; font-variant-numeric: tabular-nums; }
.empty { color: var(--text-3); font-size: 14px; padding: 6px 0; }

/* ---------- Controls ---------- */
button, .btn {
  font: inherit; font-size: 15px; min-height: 36px; padding: 6px 14px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text); cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; text-decoration: none;
}
button.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; }
button.ghost { background: transparent; border-color: transparent; color: var(--accent); padding: 6px 8px; }
button.danger { color: var(--danger); }
button:disabled { opacity: 0.5; cursor: default; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.spread { justify-content: space-between; }
label { display: block; font-size: 14px; color: var(--text-2); margin: 10px 0 4px; }
input, select, textarea {
  font: inherit; font-size: 16px; width: 100%; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
textarea { min-height: 80px; resize: vertical; }
form .actions { margin-top: 14px; display: flex; gap: 8px; justify-content: flex-end; }
.error { color: var(--danger); font-size: 14px; margin-top: 8px; }

/* ---------- Date bar ---------- */
.datebar { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.datebar h1 { margin: 0; font-size: 22px; text-align: center; flex: 1; }
.datebar .sub { display: block; font-size: 13px; color: var(--text-2); font-weight: 400; }
.datebar button { min-width: var(--tap); min-height: var(--tap); font-size: 20px; }

/* ---------- Meals ---------- */
.meal { display: grid; grid-template-columns: 64px 1fr; gap: 4px 10px; padding: 10px 0; border-top: 1px solid var(--border); }
.meal:first-of-type { border-top: 0; }
.meal .type { color: var(--text-3); font-size: 13px; text-transform: uppercase; letter-spacing: 0.05em; padding-top: 2px; }
.meal .name { font-weight: 600; }
.meal .desc { color: var(--text-2); font-size: 13px; }
.meal .row { grid-column: 2; }

/* ---------- Nutrition meters ---------- */
.hero { font-size: 40px; font-weight: 700; letter-spacing: -0.02em; }
.hero .of { font-size: 16px; color: var(--text-2); font-weight: 400; }
.meter { margin: 8px 0; }
.meter .lbl { display: flex; justify-content: space-between; font-size: 14px; }
.meter .lbl span:last-child { color: var(--text-2); font-variant-numeric: tabular-nums; }
.meter .track { height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; margin-top: 4px; }
.meter .fill { height: 100%; border-radius: 4px; background: var(--series-1); }
.kv { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 4px 12px; font-size: 14px; margin-top: 8px; }
.kv div { display: flex; justify-content: space-between; color: var(--text-2); }
.kv div { gap: 8px; }
.kv b { white-space: nowrap; color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }

/* ---------- Exercises ---------- */
.session { margin-top: 10px; }
.item { border-top: 1px solid var(--border); }
.item:first-child { border-top: 0; }
.item-row { display: flex; align-items: flex-start; gap: 10px; padding: 10px 0; }
.check {
  flex: none; width: 28px; height: 28px; min-height: 28px; padding: 0; border-radius: 50%;
  border: 2px solid var(--text-3); background: transparent; margin-top: 1px;
}
.check[aria-pressed="true"] { background: var(--done); border-color: var(--done); color: #fff; }
.check[aria-pressed="true"]::after { content: "✓"; font-size: 15px; font-weight: 700; }
.item-main { flex: 1; min-width: 0; cursor: pointer; }
.item-title { font-weight: 600; }
.item.done .item-title { color: var(--text-2); text-decoration: line-through; }
.item-rx { color: var(--text-2); font-size: 14px; }
.chev { color: var(--text-3); font-size: 18px; padding: 2px 4px; transition: transform .15s; }
.item.open .chev { transform: rotate(90deg); }
.item-detail { padding: 0 0 12px 38px; }
.item-detail .instructions { white-space: pre-wrap; font-size: 14px; }
.videos { display: grid; gap: 8px; margin-top: 10px; }
.video-card { display: flex; gap: 10px; align-items: center; border: 1px solid var(--border); border-radius: 10px; padding: 6px; background: var(--surface); text-align: left; width: 100%; }
.video-card img { width: 112px; height: 63px; object-fit: cover; border-radius: 6px; flex: none; background: var(--surface-2); }
.video-card .vt { font-size: 14px; font-weight: 600; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.video-card .vc { font-size: 12px; color: var(--text-2); }
.player { position: relative; padding-top: 56.25%; border-radius: 10px; overflow: hidden; background: #000; margin-top: 8px; }
.player iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; }
.badge { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 6px; background: var(--surface-2); color: var(--text-2); margin-left: 6px; vertical-align: 2px; font-weight: 500; }

/* ---------- Week ---------- */
.day { display: block; text-decoration: none; color: inherit; }
.day .card { margin: 8px 0; }
.day.today .card { border-color: var(--accent); }
.day-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 12px; font-size: 14px; color: var(--text-2); margin-top: 4px; }
.day-grid b { color: var(--text); font-weight: 600; }

/* ---------- Chart ---------- */
.chart-wrap { position: relative; }
.chart { width: 100%; height: 220px; display: block; }
.chart .grid line { stroke: var(--grid); stroke-width: 1; }
.chart .axis text { fill: var(--text-3); font-size: 11px; }
.chart .bar { fill: var(--series-1); }
.chart .hit { fill: transparent; }
.chart .hit:hover + .bar, .chart .bar.active { opacity: 0.8; }
.chart .goal { stroke: var(--text-2); stroke-width: 1; }
.chart .goal-lbl { fill: var(--text-2); font-size: 11px; }
.tip {
  position: absolute; pointer-events: none; background: var(--text); color: var(--bg); font-size: 13px;
  padding: 6px 8px; border-radius: 8px; white-space: nowrap; transform: translate(-50%, -100%); margin-top: -6px;
}
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; background: transparent; min-height: 32px; }
.seg button[aria-pressed="true"] { background: var(--surface-2); font-weight: 600; }
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; white-space: nowrap; }
th { color: var(--text-3); font-weight: 600; font-size: 12px; }
.table-scroll { overflow-x: auto; }

/* ---------- Settings ---------- */
.token-row { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 0; border-top: 1px solid var(--border); font-size: 14px; }
.token-row:first-child { border-top: 0; }
.secret { font-family: ui-monospace, Menlo, monospace; font-size: 13px; word-break: break-all; background: var(--surface-2); padding: 8px 10px; border-radius: 8px; user-select: all; }
code { font-family: ui-monospace, Menlo, monospace; font-size: 13px; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; word-break: break-all; }
pre { font-family: ui-monospace, Menlo, monospace; font-size: 12px; background: var(--surface-2); padding: 10px; border-radius: 8px; white-space: pre-wrap; word-break: break-all; }
.goals-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0 12px; }

/* ---------- Login ---------- */
.auth { max-width: 380px; margin: 10vh auto 0; }
.auth h1 { text-align: center; }
.auth .logo { display: block; margin: 0 auto 8px; width: 64px; height: 64px; border-radius: 14px; }

/* ---------- Sheet (dialog) ---------- */
dialog {
  border: 0; padding: 0; width: min(560px, 100vw); max-width: 100vw; max-height: 88vh; margin: auto auto 0;
  border-radius: 18px 18px 0 0; background: var(--surface); color: var(--text);
}
@media (min-width: 600px) { dialog { margin: auto; border-radius: 18px; } }
dialog::backdrop { background: rgb(0 0 0 / 0.4); }
.sheet { padding: 16px 16px calc(16px + env(safe-area-inset-bottom)); overflow-y: auto; max-height: 88vh; }
.result { display: block; width: 100%; text-align: left; padding: 10px 0; border: 0; border-bottom: 1px solid var(--border); background: transparent; border-radius: 0; }

/* ---------- Tab bar ---------- */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; display: flex; justify-content: space-around;
  background: color-mix(in srgb, var(--surface) 92%, transparent); backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px); border-top: 1px solid var(--border);
  padding: 6px 0 calc(6px + env(safe-area-inset-bottom));
}
.tabs a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 11px; color: var(--text-3); text-decoration: none; min-height: var(--tap); justify-content: center; }
.tabs a .ti { font-size: 19px; line-height: 1; }
.tabs a[aria-current="page"] { color: var(--accent); }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(84px + env(safe-area-inset-bottom));
  background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 10px; font-size: 14px; max-width: calc(100vw - 32px);
}
@media (forced-colors: active) { .chart .bar { fill: CanvasText; } .meter .fill { background: CanvasText; } }
.connected { color: var(--done); font-weight: 600; }
.rec-dot { flex: none; width: 28px; height: 28px; display: inline-flex; align-items: center; justify-content: center; color: var(--danger); font-size: 16px; }

/* ---------- Desktop ---------- */
/* Phone layout is the default; from 900px the tab bar becomes a sidebar and views spread into columns. */
.cols, .col, .week-grid, .nutri-top, .nutri-bottom, .settings-grid { min-width: 0; }

@media (min-width: 900px) {
  body { padding: 0 0 0 220px; }
  main { max-width: 1240px; padding: 28px 32px 48px; }
  h1 { font-size: 28px; }

  .tabs {
    top: 0; bottom: 0; left: 0; right: auto; width: 220px;
    flex-direction: column; justify-content: flex-start; gap: 2px;
    padding: 24px 12px; border-top: 0; border-right: 1px solid var(--border);
    background: var(--surface); backdrop-filter: none; -webkit-backdrop-filter: none;
  }
  .tabs::before { content: 'Health Hub'; font-weight: 700; font-size: 18px; color: var(--text); padding: 0 12px 18px; }
  .tabs a {
    flex: none; flex-direction: row; justify-content: flex-start; gap: 12px;
    font-size: 15px; min-height: 40px; padding: 0 12px; border-radius: 10px; color: var(--text-2);
  }
  .tabs a:hover { background: var(--surface-2); }
  .tabs a[aria-current="page"] { background: var(--surface-2); color: var(--accent); font-weight: 600; }
  .tabs a .ti { font-size: 16px; width: 20px; text-align: center; }
  .toast { bottom: 24px; left: calc(50% + 110px); }

  .datebar { max-width: 560px; margin: 0 auto 8px; }

  .cols { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 0 20px; align-items: start; }
  .nutri-top { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); gap: 0 20px; align-items: start; }
  .nutri-top .kv { grid-template-columns: 1fr; }
  .nutri-bottom { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 0 20px; align-items: start; }
  .settings-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 0 20px; align-items: start; }
  .week-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 0 16px; }

  .item-main:hover .item-title, .day:hover h2 { color: var(--accent); }
  .video-card:hover { border-color: var(--accent); }
}

@media (min-width: 1200px) {
  /* A real Monday–Sunday row. */
  .week-grid { grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 0 10px; }
  .week-grid .card { padding: 12px; height: calc(100% - 16px); }
  .week-grid .card-head { flex-direction: column; align-items: flex-start; gap: 2px; }
  .week-grid .card-head h2 { font-size: 15px; }
  .week-grid .day-grid { grid-template-columns: 1fr; font-size: 13px; }
}
