/* Physio Tracker — mobile-first, one-handed friendly. */

:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --border: #e2e6ec;
  --text: #14181f;
  --text-2: #5b6472;
  --text-3: #8b94a3;
  --accent: #2563c9;
  --accent-soft: #e8f0fd;
  --good: #1f8a52;
  --good-soft: #e4f4ea;
  --warn: #b06a06;
  --warn-soft: #fdf1de;
  --bad: #c02e3c;
  --bad-soft: #fdeaec;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 1px 2px rgba(16,24,40,.05), 0 6px 18px -8px rgba(16,24,40,.18);
  --tap: 46px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1218;
    --surface: #161b23;
    --surface-2: #1d242e;
    --border: #29313d;
    --text: #e8ecf2;
    --text-2: #a3adbc;
    --text-3: #75808f;
    --accent: #6ea3f5;
    --accent-soft: #17263f;
    --good: #58c98b;
    --good-soft: #122b1e;
    --warn: #e5a75a;
    --warn-soft: #2e2314;
    --bad: #f2818c;
    --bad-soft: #33191d;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 22px -10px rgba(0,0,0,.6);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

button, input, textarea, select { font: inherit; color: inherit; }
h1, h2, h3, h4 { margin: 0; line-height: 1.25; font-weight: 650; letter-spacing: -0.01em; }
p { margin: 0 0 .7em; }
p:last-child { margin-bottom: 0; }

.muted { color: var(--text-2); }
.tiny { font-size: .8rem; }
.small { font-size: .875rem; }
.error { color: var(--bad); font-size: .875rem; }
.center { text-align: center; }
.nowrap { white-space: nowrap; }
[hidden] { display: none !important; }

/* ---------------------------------------------------------------- boot */
.boot { position: fixed; inset: 0; display: grid; place-items: center; background: var(--bg); z-index: 200; }
.spinner {
  width: 26px; height: 26px; border-radius: 50%;
  border: 2.5px solid var(--border); border-top-color: var(--accent);
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------------------------------------------------------------- gate */
.gate { position: fixed; inset: 0; display: grid; place-items: center; padding: 22px; z-index: 150; background: var(--bg); }
.gate-card {
  width: 100%; max-width: 380px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 18px; padding: 28px 24px 24px;
  box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; color: var(--accent); }
.brand h1 { font-size: 1.3rem; color: var(--text); }
.brand-mark { width: 26px; height: 26px; }
.brand-sm { font-weight: 650; font-size: 1.05rem; color: var(--text); margin-bottom: 22px; }
.brand-sm .brand-mark { color: var(--accent); }
.gate-card .muted { margin: 8px 0 20px; font-size: .92rem; }

/* -------------------------------------------------------------- layout */
#app { min-height: 100dvh; }

.sidebar { display: none; }

.main {
  padding: 18px 16px calc(84px + var(--safe-b));
  max-width: 780px; margin: 0 auto;
}

.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 60;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-b);
}
.tab {
  appearance: none; background: none; border: 0; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  padding: 9px 4px 8px; color: var(--text-3); font-size: .68rem; font-weight: 550;
  letter-spacing: .01em;
}
.tab svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.9;
           stroke-linecap: round; stroke-linejoin: round; }
.tab.active { color: var(--accent); }

@media (min-width: 900px) {
  #app { display: grid; grid-template-columns: 232px 1fr; }
  .sidebar {
    display: flex; flex-direction: column; padding: 24px 16px;
    border-right: 1px solid var(--border); background: var(--surface);
    position: sticky; top: 0; height: 100dvh;
  }
  .nav-desktop { display: flex; flex-direction: column; gap: 2px; }
  .nav-link {
    appearance: none; background: none; border: 0; cursor: pointer; text-align: left;
    padding: 10px 12px; border-radius: var(--radius-sm); color: var(--text-2);
    font-size: .93rem; font-weight: 550;
  }
  .nav-link:hover { background: var(--surface-2); color: var(--text); }
  .nav-link.active { background: var(--accent-soft); color: var(--accent); }
  .sidebar-foot { margin-top: auto; }
  .tabbar { display: none; }
  .main { padding: 32px 32px 60px; max-width: 860px; margin: 0; }
}

/* --------------------------------------------------------------- cards */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px; margin-bottom: 14px;
  box-shadow: var(--shadow);
}
.card-flat { box-shadow: none; }
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.card-head h2 { font-size: 1.02rem; }
.card-head h3 { font-size: .95rem; }

.page-head { margin-bottom: 18px; }
.page-head h1 { font-size: 1.5rem; margin-bottom: 3px; }
.page-head p { color: var(--text-2); font-size: .9rem; }

.section-label {
  font-size: .74rem; text-transform: uppercase; letter-spacing: .07em;
  font-weight: 680; color: var(--text-3); margin: 22px 0 9px;
}

/* ------------------------------------------------------------- buttons */
.btn {
  appearance: none; cursor: pointer; border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
  border-radius: var(--radius-sm); padding: 0 15px; min-height: var(--tap);
  font-size: .93rem; font-weight: 570; display: inline-flex; align-items: center;
  justify-content: center; gap: 7px; transition: background .12s, border-color .12s, opacity .12s;
  -webkit-tap-highlight-color: transparent;
}
.btn:active { transform: translateY(.5px); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: color-mix(in srgb, var(--accent) 88%, #000); }
.btn-danger { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 35%, var(--border)); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover:not(:disabled) { background: var(--surface-2); }
.btn-block { width: 100%; }
.btn-sm { min-height: 36px; padding: 0 11px; font-size: .85rem; border-radius: 8px; }
.btn-lg { min-height: 54px; font-size: 1rem; border-radius: 13px; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

/* -------------------------------------------------------------- fields */
.field { display: block; margin-bottom: 13px; }
.field > span { display: block; font-size: .82rem; font-weight: 600; color: var(--text-2); margin-bottom: 5px; }
.field input, .field textarea, .field select {
  width: 100%; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 11px 12px; min-height: var(--tap);
  outline: none; transition: border-color .12s, box-shadow .12s;
}
.field textarea { min-height: 88px; resize: vertical; line-height: 1.45; }
.field input:focus, .field textarea:focus, .field select:focus {
  border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.field-row-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }

/* --------------------------------------------------------------- chips */
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  appearance: none; cursor: pointer; border: 1px solid var(--border);
  background: var(--surface); border-radius: 999px; padding: 8px 13px;
  font-size: .86rem; font-weight: 560; min-height: 38px; color: var(--text-2);
  -webkit-tap-highlight-color: transparent;
}
.chip:hover { background: var(--surface-2); }
.chip.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.chip.on-good { background: var(--good); border-color: var(--good); color: #fff; }
.chip.on-warn { background: var(--warn); border-color: var(--warn); color: #fff; }
.chip.on-bad { background: var(--bad); border-color: var(--bad); color: #fff; }

.scale { display: grid; gap: 5px; }
.scale-row { display: flex; gap: 5px; }
.scale-btn {
  flex: 1; appearance: none; cursor: pointer; border: 1px solid var(--border);
  background: var(--surface); border-radius: 9px; min-height: 42px;
  font-size: .9rem; font-weight: 600; color: var(--text-2);
  -webkit-tap-highlight-color: transparent;
}
.scale-btn.on { color: #fff; }
.scale-ends { display: flex; justify-content: space-between; font-size: .72rem; color: var(--text-3); }

.badge {
  display: inline-flex; align-items: center; gap: 5px; border-radius: 999px;
  padding: 3px 9px; font-size: .74rem; font-weight: 640; letter-spacing: .01em;
  background: var(--surface-2); color: var(--text-2); white-space: nowrap;
}
.badge-good { background: var(--good-soft); color: var(--good); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-bad  { background: var(--bad-soft);  color: var(--bad); }
.badge-accent { background: var(--accent-soft); color: var(--accent); }

/* ------------------------------------------------------------ exercise */
.ex-card { border: 1px solid var(--border); border-radius: var(--radius);
           background: var(--surface); margin-bottom: 10px; overflow: hidden; }
.ex-card.is-open { border-color: color-mix(in srgb, var(--accent) 45%, var(--border)); }
.ex-head {
  width: 100%; appearance: none; background: none; border: 0; cursor: pointer;
  display: flex; align-items: center; gap: 12px; padding: 14px 15px; text-align: left;
  -webkit-tap-highlight-color: transparent;
}
.ex-head:hover { background: var(--surface-2); }
.ex-status {
  width: 26px; height: 26px; border-radius: 50%; flex: none;
  border: 2px solid var(--border); display: grid; place-items: center;
}
.ex-status svg { width: 14px; height: 14px; stroke: #fff; stroke-width: 3; fill: none;
                 stroke-linecap: round; stroke-linejoin: round; }
.ex-status.done { background: var(--good); border-color: var(--good); }
.ex-status.partial { background: var(--warn); border-color: var(--warn); }
.ex-status.skipped { background: var(--text-3); border-color: var(--text-3); }
.ex-title { flex: 1; min-width: 0; }
.ex-title strong { display: block; font-weight: 600; font-size: .97rem; }
.ex-title span { display: block; font-size: .8rem; color: var(--text-3); margin-top: 1px;
                 overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ex-body { padding: 4px 15px 16px; border-top: 1px solid var(--border); }
.ex-cue {
  background: var(--surface-2); border-radius: var(--radius-sm);
  padding: 10px 12px; font-size: .86rem; color: var(--text-2); margin: 12px 0;
}

.followup {
  border-left: 3px solid var(--warn); background: var(--warn-soft);
  border-radius: var(--radius-sm); padding: 12px 13px; margin: 12px 0;
}
.followup h4 { font-size: .84rem; color: var(--warn); margin-bottom: 4px;
               text-transform: uppercase; letter-spacing: .05em; }
.followup p { font-size: .9rem; color: var(--text); }
.followup .chips { margin-top: 9px; }

/* -------------------------------------------------------------- recap */
.recap {
  background: linear-gradient(180deg, var(--accent-soft), color-mix(in srgb, var(--accent-soft) 40%, var(--surface)));
  border: 1px solid color-mix(in srgb, var(--accent) 22%, var(--border));
}
.recap p { font-size: .93rem; white-space: pre-wrap; }

.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stat { background: var(--surface-2); border-radius: var(--radius-sm); padding: 12px; }
.stat b { display: block; font-size: 1.35rem; font-weight: 660; letter-spacing: -.02em; }
.stat span { font-size: .74rem; color: var(--text-3); text-transform: uppercase; letter-spacing: .05em; }

.empty { text-align: center; padding: 42px 20px; color: var(--text-3); }
.empty svg { width: 38px; height: 38px; margin-bottom: 12px; stroke: currentColor;
             fill: none; stroke-width: 1.5; }
.empty p { font-size: .92rem; margin-bottom: 16px; }

.list-row {
  display: flex; align-items: flex-start; gap: 12px; padding: 13px 0;
  border-bottom: 1px solid var(--border);
}
.list-row:last-child { border-bottom: 0; }
.list-row .grow { flex: 1; min-width: 0; }

.timeline { border-left: 2px solid var(--border); padding-left: 14px; margin: 10px 0 0; }
.timeline-item { position: relative; padding-bottom: 12px; font-size: .87rem; }
.timeline-item::before {
  content: ''; position: absolute; left: -19px; top: 6px; width: 8px; height: 8px;
  border-radius: 50%; background: var(--border);
}
.timeline-item:last-child { padding-bottom: 0; }

/* -------------------------------------------------------------- modals */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(9,12,18,.5); z-index: 120;
  display: flex; align-items: flex-end; justify-content: center;
  backdrop-filter: blur(2px); animation: fade .15s ease;
}
@keyframes fade { from { opacity: 0; } }
.modal {
  background: var(--surface); width: 100%; max-width: 520px;
  border-radius: 20px 20px 0 0; padding: 20px 18px calc(20px + var(--safe-b));
  max-height: 88dvh; overflow-y: auto; animation: rise .2s cubic-bezier(.2,.8,.3,1);
}
@keyframes rise { from { transform: translateY(22px); opacity: .6; } }
.modal h3 { font-size: 1.08rem; margin-bottom: 4px; }
.modal .modal-sub { color: var(--text-2); font-size: .88rem; margin-bottom: 16px; }
@media (min-width: 640px) {
  .modal-backdrop { align-items: center; }
  .modal { border-radius: 18px; padding-bottom: 20px; }
}

/* -------------------------------------------------------------- toasts */
.toasts {
  position: fixed; left: 0; right: 0; bottom: calc(78px + var(--safe-b)); z-index: 300;
  display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none;
}
@media (min-width: 900px) { .toasts { bottom: 24px; } }
.toast {
  background: #11151c; color: #f2f5f9; border-radius: 999px; padding: 9px 17px;
  font-size: .87rem; font-weight: 540; box-shadow: 0 8px 24px rgba(0,0,0,.28);
  animation: rise .18s ease; max-width: 90vw;
}
.toast.bad { background: var(--bad); }
.toast.good { background: var(--good); }

.sync-state { font-size: .76rem; color: var(--text-3); }
.sync-state.pending { color: var(--warn); }

.offline-bar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 90;
  background: var(--warn); color: #fff; text-align: center;
  font-size: .8rem; font-weight: 600; padding: 5px;
}

/* ------------------------------------------------------------- report */
.report-preview { font-size: .92rem; white-space: pre-wrap; }
.narrative-edit { min-height: 220px; }
.ex-stat-row {
  display: flex; align-items: center; gap: 10px; padding: 10px 0;
  border-bottom: 1px solid var(--border); font-size: .88rem;
}
.ex-stat-row:last-child { border-bottom: 0; }
.bar { flex: none; width: 62px; height: 6px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--good); }

.code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .8rem;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px;
  padding: 9px 11px; word-break: break-all; user-select: all;
}
.drag-handle { cursor: grab; color: var(--text-3); padding: 4px; touch-action: none; }
.dragging { opacity: .45; }
