:root {
  color-scheme: dark light;
  --bg: #0e1014;
  --surface: #171a21;
  --surface-2: #20242e;
  --border: #2b303b;
  --text: #eceef2;
  --muted: #97a0ae;
  --primary: #7c9cff;
  --on-primary: #0b1020;
  --ok: #3ecf8e;
  --on-ok: #06261a;
  --danger: #ff7b7b;
  --radius: 14px;
  --nav-h: 60px;
  --timer-h: 60px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f3f4f7;
    --surface: #ffffff;
    --surface-2: #eceef3;
    --border: #d9dde5;
    --text: #15181e;
    --muted: #5d6675;
    --primary: #3355e0;
    --on-primary: #ffffff;
    --ok: #15935f;
    --on-ok: #ffffff;
    --danger: #c93434;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(var(--nav-h) + var(--safe-b) + 24px);
  -webkit-tap-highlight-color: transparent;
}
body.has-timer { padding-bottom: calc(var(--nav-h) + var(--timer-h) + var(--safe-b) + 24px); }

h1, h2, h3, p { margin: 0; }
h1 { font-size: 1.25rem; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1.05rem; }
[hidden] { display: none !important; }
.muted { color: var(--muted); font-size: 0.9rem; }

button, input, select, textarea { font: inherit; color: inherit; }
button, a { touch-action: manipulation; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

/* ---------- layout ---------- */

#topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  min-height: 56px;
  padding: calc(env(safe-area-inset-top, 0px) + 8px) 16px 8px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
#topbar .actions { display: flex; gap: 8px; }

#view {
  max-width: 560px; margin: 0 auto; padding: 16px;
  display: flex; flex-direction: column; gap: 12px;
}

#tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex;
  height: calc(var(--nav-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  background: var(--surface);
  border-top: 1px solid var(--border);
}
#tabs a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  position: relative;
  color: var(--muted); text-decoration: none; font-size: 0.7rem;
}
#tabs a[aria-current="page"] { color: var(--primary); font-weight: 600; }
#tabs a.live::after {
  content: ""; position: absolute; top: 9px; left: calc(50% + 9px);
  width: 8px; height: 8px; border-radius: 50%; background: var(--ok);
}

.icon { display: inline-flex; }
.icon svg {
  width: 22px; height: 22px;
  fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}

/* ---------- controls ---------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 16px;
  border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface-2); color: var(--text);
  font-weight: 600; text-decoration: none; cursor: pointer;
}
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 0.9rem; }
.btn.big { min-height: 52px; font-size: 1.05rem; }
.btn.block { width: 100%; }
.btn.grow { flex: 1; }
.btn:active, .icon-btn:active, .stepper button:active, .check-btn:active { filter: brightness(1.2); }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px; flex: none;
  border: 0; border-radius: 10px; background: transparent; color: var(--muted); cursor: pointer;
}
.icon-btn:disabled { opacity: 0.3; }

input[type="text"], input[type="search"], input[type="date"], select, textarea {
  width: 100%; min-height: 44px; padding: 8px 12px;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--bg); color: var(--text);
}
textarea { resize: vertical; display: block; }
input::placeholder, textarea::placeholder { color: var(--muted); opacity: 0.8; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field-label { font-size: 0.85rem; font-weight: 600; color: var(--muted); }
.hint { font-size: 0.8rem; color: var(--muted); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.row { display: flex; gap: 8px; align-items: center; }

.check { display: flex; gap: 10px; align-items: flex-start; min-height: 32px; }
.check input { width: 22px; height: 22px; flex: none; accent-color: var(--primary); margin: 1px 0 0; }

.segmented { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 4px; padding: 4px; border-radius: 12px; background: var(--bg); border: 1px solid var(--border); }
.segmented button { min-height: 36px; border: 0; border-radius: 9px; background: transparent; color: var(--muted); font-weight: 600; cursor: pointer; }
.segmented button[aria-pressed="true"] { background: var(--primary); color: var(--on-primary); }

/* ---------- cards ---------- */

.card {
  display: flex; flex-direction: column; gap: 10px;
  padding: 14px;
  background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius);
}
.section-title { margin-top: 8px; font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.session { display: flex; flex-direction: column; gap: 12px; }
.session-meta { font-variant-numeric: tabular-nums; }

.empty { display: flex; flex-direction: column; align-items: stretch; gap: 12px; padding: 48px 8px; text-align: center; }
.empty p { color: var(--muted); }

.link-card { flex-direction: row; align-items: center; text-align: left; color: inherit; text-decoration: none; cursor: pointer; width: 100%; }
.link-card > div { display: flex; flex-direction: column; gap: 4px; min-width: 0; }

.day.next { border-color: var(--primary); }
.day:not(.next) { flex-direction: row; align-items: center; }
.day-info { display: flex; flex-direction: column; gap: 4px; flex: 1; min-width: 0; }
.eyebrow { font-size: 0.75rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: var(--primary); }
.day.next h3 { font-size: 1.5rem; }
.preview { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; }
.preview li { display: flex; justify-content: space-between; gap: 12px; padding: 7px 0; border-top: 1px solid var(--border); }
.preview li span:last-child { flex: none; font-variant-numeric: tabular-nums; }

/* ---------- exercise card ---------- */

.ex-head { display: flex; align-items: center; gap: 8px; }
.ex-head h3 { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.ex-head .icon-btn { margin: -6px -6px -6px 0; }
.ex.complete { border-color: var(--ok); }
.ex.skipped h3 { color: var(--muted); }
.tag { padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); font-size: 0.75rem; font-weight: 600; }
.last-note { padding-left: 10px; border-left: 3px solid var(--primary); font-size: 0.9rem; overflow-wrap: anywhere; }
input.setup { min-height: 38px; font-size: 0.9rem; }

.set-row { display: grid; grid-template-columns: 18px 1.3fr 1fr 44px; gap: 8px; align-items: center; }
.set-head { font-size: 0.8rem; color: var(--muted); text-align: center; }
.set-idx { text-align: center; color: var(--muted); font-weight: 600; font-variant-numeric: tabular-nums; }
.unit-btn { border: 0; background: transparent; color: var(--primary); font-size: 0.8rem; font-weight: 600; min-height: 28px; cursor: pointer; }

.stepper { display: flex; height: 44px; border: 1px solid var(--border); border-radius: 10px; background: var(--bg); overflow: hidden; }
.stepper button { width: 34px; flex: none; border: 0; background: var(--surface-2); color: var(--text); font-size: 1.25rem; cursor: pointer; }
.stepper input { flex: 1; min-width: 0; min-height: 0; padding: 0; border: 0; border-radius: 0; background: transparent; text-align: center; font-weight: 600; font-variant-numeric: tabular-nums; }

.check-btn {
  width: 44px; height: 44px;
  display: inline-flex; align-items: center; justify-content: center;
  border: 2px solid var(--border); border-radius: 50%;
  background: transparent; color: var(--muted); cursor: pointer;
}
.check-btn[aria-pressed="true"] { background: var(--ok); border-color: var(--ok); color: var(--on-ok); }
.set-row.done .stepper { opacity: 0.6; }

/* ---------- program ---------- */

input.day-name { flex: 1; min-width: 0; font-weight: 700; font-size: 1.05rem; }
.plan-item { display: flex; align-items: center; gap: 2px; border-top: 1px solid var(--border); padding-top: 6px; }
.plan-main { flex: 1; min-width: 0; min-height: 44px; display: flex; flex-direction: column; gap: 2px; align-items: flex-start; text-align: left; border: 0; background: transparent; cursor: pointer; padding: 0; }
.plan-main span { overflow-wrap: anywhere; }

/* ---------- rest timer ---------- */

#timer {
  position: fixed; left: 0; right: 0; bottom: calc(var(--nav-h) + var(--safe-b)); z-index: 11;
  display: flex; align-items: center; gap: 8px;
  height: var(--timer-h); padding: 0 12px;
  background: var(--surface-2); border-top: 1px solid var(--border);
  overflow: hidden;
}
#timer > * { position: relative; }
#timer .timer-fill { position: absolute; left: 0; top: 0; bottom: 0; background: var(--primary); opacity: 0.22; transition: width 0.25s linear; }
#timer .timer-label { font-size: 0.75rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
#timer .timer-time { flex: 1; font-size: 1.6rem; font-weight: 700; font-variant-numeric: tabular-nums; }
#timer.over { background: var(--ok); color: var(--on-ok); }
#timer.over .timer-label { color: inherit; }

/* ---------- sheets and toasts ---------- */

.overlay {
  position: fixed; inset: 0; z-index: 30;
  display: flex; align-items: flex-end; justify-content: center;
  background: rgba(0, 0, 0, 0.55);
}
.sheet {
  width: 100%; max-width: 560px; max-height: 88dvh;
  display: flex; flex-direction: column;
  background: var(--surface);
  border-radius: 18px 18px 0 0; border: 1px solid var(--border); border-bottom: 0;
}
.sheet-head { display: flex; align-items: center; gap: 8px; padding: 12px 8px 4px 16px; }
.sheet-head h2 { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.sheet-body { display: flex; flex-direction: column; gap: 12px; padding: 8px 16px calc(16px + var(--safe-b)); overflow-y: auto; }

.pick-list { display: flex; flex-direction: column; }
.pick-item { min-height: 48px; padding: 8px 4px; text-align: left; border: 0; border-top: 1px solid var(--border); background: transparent; cursor: pointer; }
.pick-item.new { color: var(--primary); font-weight: 600; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 40;
  bottom: calc(var(--nav-h) + var(--timer-h) + var(--safe-b) + 16px);
  max-width: calc(100% - 32px); padding: 10px 16px;
  border-radius: 999px; background: var(--text); color: var(--bg);
  font-size: 0.9rem; font-weight: 600; text-align: center;
}

@media (min-width: 600px) {
  .overlay { align-items: center; }
  .sheet { border-radius: 18px; border-bottom: 1px solid var(--border); }
}
