:root {
  --bg: #f5f7fb;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --ok: #16a34a;
  --ok-bg: #dcfce7;
  --warn: #b45309;
  --warn-bg: #fef3c7;
  --danger: #dc2626;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 1px 3px rgba(15, 23, 42, .08);
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5rem; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1.05rem; margin-top: .5rem; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
[hidden] { display: none !important; }

.loading { padding: 3rem; text-align: center; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.strong { font-weight: 600; }
.mono { font-family: ui-monospace, Consolas, monospace; font-size: .85rem; }
.prewrap { white-space: pre-wrap; overflow-wrap: anywhere; }
.grow { flex: 1; min-width: 0; }
.row { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }

/* ---------- Layout ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: .75rem 1.25rem; background: var(--card); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 700; font-size: 1.1rem; color: var(--text); }
.brand:hover { text-decoration: none; }
.topbar nav { display: flex; gap: .25rem; align-items: center; flex-wrap: wrap; }
.topbar nav a, .topbar nav .link {
  padding: .4rem .75rem; border-radius: 8px; color: var(--muted); font-weight: 500;
}
.topbar nav a.active { background: #eff6ff; color: var(--primary); }
.topbar nav a:hover { text-decoration: none; background: var(--bg); }
.container { max-width: 1100px; margin: 0 auto; padding: 1.5rem 1.25rem 4rem; }

.page-head {
  display: flex; justify-content: space-between; align-items: flex-end; gap: 1rem;
  flex-wrap: wrap; margin-bottom: 1rem;
}
.page-head h1 { margin: .15rem 0; }
.page-head input[type=search] { max-width: 280px; }
.back { font-size: .9rem; }
.actions { display: flex; gap: .5rem; flex-wrap: wrap; }

.card {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 1.25rem; margin-bottom: 1rem;
}
.card.narrow { max-width: 520px; }
.card.empty { text-align: center; color: var(--muted); padding: 2rem; }
.card.note { background: #f8fafc; }

/* ---------- Forms ---------- */
.field { display: block; margin-bottom: .85rem; }
.field-label { display: block; font-size: .85rem; font-weight: 600; margin-bottom: .25rem; }
.hint { display: block; color: var(--muted); margin-top: .2rem; }
input, textarea, select {
  width: 100%; font: inherit; color: inherit; padding: .55rem .7rem;
  border: 1px solid #cbd5e1; border-radius: 8px; background: #fff;
}
input:focus, textarea:focus, select:focus, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--primary); outline-offset: 1px; border-color: var(--primary);
}
textarea { resize: vertical; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 1rem; }
.grid4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 0 .75rem; }
.form-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: 1rem; padding-top: 1rem; border-top: 1px solid var(--border); }
.form-error {
  background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; border-radius: 8px;
  padding: .6rem .8rem; margin-bottom: 1rem;
}
.form-error ul { margin: .25rem 0 0; padding-left: 1.2rem; }

.btn, button.icon, button.link {
  font: inherit; cursor: pointer; border-radius: 8px;
}
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .35rem;
  padding: .5rem .95rem; border: 1px solid var(--border); background: #fff; color: var(--text);
  font-weight: 500; white-space: nowrap;
}
.btn:hover { background: #f8fafc; text-decoration: none; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: #fef2f2; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.small { padding: .3rem .6rem; font-size: .85rem; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .6; cursor: progress; }
button.link { background: none; border: 0; }
button.link:hover { background: var(--bg); }
button.icon {
  width: 2.2rem; height: 2.2rem; flex: none; border: 1px solid var(--border); background: #fff;
}
button.icon.danger { color: var(--danger); }

/* ---------- Auth ---------- */
.auth-wrap { min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 1rem; }
.auth-brand { display: flex; align-items: center; gap: .6rem; font-size: 1.4rem; font-weight: 700; margin-bottom: 1.25rem; }
.auth-card { width: 100%; max-width: 400px; }

/* ---------- Lists ---------- */
.list { padding: 0; overflow: hidden; }
.list-row {
  display: flex; justify-content: space-between; align-items: center; gap: 1rem;
  padding: .9rem 1.25rem; border-bottom: 1px solid var(--border); color: inherit;
}
.list-row:last-child { border-bottom: 0; }
a.list-row:hover { background: #f8fafc; text-decoration: none; }
.list-meta { display: flex; gap: 1rem; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.list .empty { padding: 1.5rem; text-align: center; margin: 0; }
.inline-form { padding: 1rem 1.25rem; border-bottom: 1px solid var(--border); background: #f8fafc; }
.meta { display: grid; grid-template-columns: max-content 1fr; gap: .4rem 1.25rem; margin: 0; }
.meta dt { color: var(--muted); }
.meta dd { margin: 0; }

.badge {
  display: inline-block; font-size: .75rem; font-weight: 600; padding: .15rem .5rem;
  border-radius: 999px; background: #eef2ff; color: #3730a3; vertical-align: middle;
}
.badge.ok { background: var(--ok-bg); color: #166534; }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.muted { background: #f1f5f9; color: var(--muted); }
.pill { font-size: .7rem; background: var(--primary); color: #fff; padding: .05rem .45rem; border-radius: 999px; }

.invite-box {
  background: #eff6ff; border: 1px solid #bfdbfe; border-radius: var(--radius);
  padding: 1rem; margin: 1rem 0;
}
.invite-box p { margin: .25rem 0 .6rem; }
.invite-box input { flex: 1; min-width: 200px; }

/* ---------- Week calendar ---------- */
.weeknav { display: flex; align-items: center; gap: .5rem; margin-bottom: .75rem; flex-wrap: wrap; }
.weeknav strong { min-width: 220px; text-align: center; }
.week { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: .6rem; }
.day {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: .6rem; min-height: 150px; display: flex; flex-direction: column; gap: .45rem;
}
.day.today { border-color: var(--primary); box-shadow: 0 0 0 1px var(--primary); }
.day > header { display: flex; justify-content: space-between; align-items: center; font-size: .8rem; font-weight: 600; color: var(--muted); }
.wcard {
  display: block; border: 1px solid var(--border); border-left: 4px solid var(--primary);
  border-radius: 8px; padding: .45rem .55rem; color: inherit; background: #fff;
}
.wcard.done { border-left-color: var(--ok); background: #f7fef9; }
.wcard:hover { text-decoration: none; box-shadow: var(--shadow); }
.wtitle { font-weight: 600; font-size: .9rem; overflow-wrap: anywhere; }
.wmeta { display: flex; gap: .4rem; flex-wrap: wrap; align-items: center; font-size: .75rem; color: var(--muted); margin-top: .2rem; }
.add-link { font-size: .8rem; margin-top: auto; padding: .25rem 0; }
.rest { margin: auto 0; text-align: center; }

/* ---------- Workout editor / view ---------- */
.items { display: flex; flex-direction: column; gap: .75rem; margin-bottom: .75rem; }
.item-edit { border: 1px solid var(--border); border-radius: 10px; padding: .85rem; background: #fbfcfe; }
.item-head { display: flex; gap: .4rem; align-items: center; margin-bottom: .6rem; }
.num {
  flex: none; width: 1.8rem; height: 1.8rem; border-radius: 50%; display: grid; place-items: center;
  background: var(--primary); color: #fff; font-size: .8rem; font-weight: 700;
}
.items-view { list-style: none; padding: 0; margin: 0; counter-reset: item; }
.item-view { counter-increment: item; }
.item-title { font-weight: 700; font-size: 1.05rem; margin-bottom: .4rem; }
.item-title::before { content: counter(item) ". "; color: var(--primary); }
.chips { display: flex; gap: .4rem; flex-wrap: wrap; margin-bottom: .5rem; }
.chip { background: #f1f5f9; border-radius: 6px; padding: .2rem .55rem; font-size: .85rem; }
.chip b { color: var(--muted); font-weight: 600; }
.result { margin-top: .6rem; padding: .6rem .75rem; background: var(--ok-bg); border-radius: 8px; }
.item-view .field { margin: .75rem 0 0; }
details { margin: .4rem 0; }
summary { cursor: pointer; color: var(--muted); font-size: .9rem; }

/* ---------- Toasts ---------- */
#toasts { position: fixed; bottom: 1rem; right: 1rem; display: flex; flex-direction: column; gap: .5rem; z-index: 100; }
.toast {
  background: var(--text); color: #fff; padding: .65rem 1rem; border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, .2); max-width: 340px;
}
.toast-error { background: var(--danger); }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  .week { grid-template-columns: 1fr; }
  .day { min-height: 0; }
  .day > header { font-size: .9rem; }
}
@media (max-width: 640px) {
  .grid2 { grid-template-columns: 1fr; }
  .grid4 { grid-template-columns: 1fr 1fr; }
  .container { padding: 1rem .75rem 4rem; }
  .topbar { padding: .6rem .75rem; }
  .list-row { flex-direction: column; align-items: flex-start; }
  .list-meta { justify-content: flex-start; }
  .weeknav strong { min-width: 0; flex-basis: 100%; order: -1; text-align: left; }
}
