/* Components. Colours and sizes come from tokens.css only. */

*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font);
  font-size: var(--size-md);
  line-height: 1.4;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}

button, .tab, .chip { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; }
/* 16px or iOS zooms in on focus and never quite zooms back. */
input, select, textarea { font-size: max(16px, var(--size-md)); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.num { font-family: var(--font-num); font-variant-numeric: tabular-nums; }

/* ---- shell -------------------------------------------------------------- */

#app {
  min-height: 100dvh;
  padding-inline: var(--gutter);
  padding-top: calc(env(safe-area-inset-top) + var(--sp-4));
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + var(--sp-8));
  max-width: 640px;
  margin: 0 auto;
}
body[data-mode="session"] #tabbar { display: none; }
body[data-mode="session"] #app { padding-bottom: calc(env(safe-area-inset-bottom) + 96px); }

.screen { display: none; }
.screen.active { display: block; }

#tabbar {
  position: fixed; inset: auto 0 0 0; z-index: 20;
  display: grid; grid-template-columns: repeat(5, 1fr);
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
  border-top: 1px solid var(--line);
}
.tab {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  font-size: 0.6875rem; font-weight: var(--weight-mid); color: var(--fg-mute);
}
.tab svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tab[aria-current="page"] { color: var(--accent); }

/* ---- headings ----------------------------------------------------------- */

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: var(--sp-3); margin-bottom: var(--sp-5); }
.page-title { margin: 0; font-size: var(--size-display); font-weight: var(--weight-black); letter-spacing: -0.02em; line-height: 1.05; }
.page-sub { color: var(--fg-dim); font-size: var(--size-sm); margin-top: 2px; }
.section-label {
  margin: var(--sp-6) 0 var(--sp-2); font-size: var(--size-xs); font-weight: var(--weight-bold);
  letter-spacing: var(--track-label); text-transform: uppercase; color: var(--fg-dim);
  display: flex; justify-content: space-between; align-items: baseline;
}
.section-label .link { text-transform: none; letter-spacing: 0; font-size: var(--size-sm); }
.hint { color: var(--fg-dim); font-size: var(--size-sm); }

/* ---- generic pieces ----------------------------------------------------- */

.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); border: 1px solid var(--line); overflow: hidden; }
.card + .card { margin-top: var(--sp-3); }
.stack { display: grid; gap: var(--sp-3); }

.row {
  width: 100%; min-height: 52px; display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-4); text-align: left; border-bottom: 1px solid var(--line);
}
.row:last-child { border-bottom: 0; }
.row-main { min-width: 0; display: grid; gap: 1px; }
.row-title { font-weight: var(--weight-mid); }
.row-sub { color: var(--fg-dim); font-size: var(--size-sm); }
.row-end { display: flex; align-items: center; gap: var(--sp-2); color: var(--fg-dim); flex-shrink: 0; }
.chev { color: var(--fg-mute); font-size: 1.25rem; line-height: 1; }

.btn {
  min-height: var(--tap); padding: 0 var(--sp-4); border-radius: 999px;
  display: inline-flex; align-items: center; justify-content: center; gap: var(--sp-2);
  font-weight: var(--weight-bold); background: var(--surface-2); color: var(--fg);
}
.btn.primary { background: var(--accent); color: var(--accent-fg); }
.btn.danger { background: var(--danger-soft); color: var(--danger); }
.btn.ghost { background: transparent; color: var(--accent); padding: 0 var(--sp-2); }
.btn.block { width: 100%; }
.btn.sm { min-height: 34px; padding: 0 var(--sp-3); font-size: var(--size-sm); }
.btn:disabled { opacity: 0.45; }
.link { color: var(--accent); font-weight: var(--weight-mid); }

.chip {
  display: inline-flex; align-items: center; gap: 4px; min-height: 30px; padding: 0 var(--sp-3);
  border-radius: 999px; font-size: var(--size-sm); font-weight: var(--weight-mid);
  background: var(--surface-2); color: var(--fg-dim); white-space: nowrap;
}
.chip.on { background: var(--accent); color: var(--accent-fg); }
.chips { display: flex; gap: var(--sp-2); flex-wrap: wrap; }
.chips.scroll { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; margin-inline: calc(-1 * var(--gutter)); padding-inline: var(--gutter); }
.chips.scroll::-webkit-scrollbar { display: none; }

.tag { display: inline-block; font-size: var(--size-xs); font-weight: var(--weight-bold); padding: 2px 7px; border-radius: 5px; background: var(--surface-2); color: var(--fg-dim); }
.tag.pr { background: var(--pr-soft); color: var(--pr-ink); }
.tag.up { background: var(--done-soft); color: var(--done); }
.tag.swap { background: var(--accent-soft); color: var(--accent); }

.empty { text-align: center; padding: var(--sp-8) var(--sp-5); color: var(--fg-dim); display: grid; gap: var(--sp-3); justify-items: center; }
.empty h3 { margin: 0; color: var(--fg); font-size: var(--size-lg); }

.field { display: grid; gap: 6px; margin-bottom: var(--sp-4); }
.field label, .field .label { font-size: var(--size-sm); font-weight: var(--weight-mid); color: var(--fg-dim); }
.input {
  width: 100%; min-height: var(--tap); padding: 0 var(--sp-3); border-radius: var(--radius-sm);
  border: 1px solid var(--line-strong); background: var(--surface);
}
textarea.input { padding: var(--sp-2) var(--sp-3); min-height: 72px; resize: vertical; }
.search { margin-bottom: var(--sp-3); }

/* ---- Train -------------------------------------------------------------- */

.resume { background: var(--accent); color: var(--accent-fg); border: 0; padding: var(--sp-4); display: grid; gap: var(--sp-3); }
.resume .row-sub { color: inherit; opacity: 0.8; }
.resume .btn { background: var(--accent-fg); color: var(--accent); }

.plan { padding: var(--sp-4); display: grid; gap: var(--sp-3); }
.plan-top { display: flex; justify-content: space-between; align-items: flex-start; gap: var(--sp-3); }
.plan-name { margin: 0; font-size: var(--size-lg); font-weight: var(--weight-bold); }
.plan-list { margin: 0; padding: 0; list-style: none; color: var(--fg-dim); font-size: var(--size-sm); display: grid; gap: 2px; }
.plan-list .w { font-family: var(--font-num); color: var(--fg); font-variant-numeric: tabular-nums; }
.plan-foot { display: flex; justify-content: space-between; align-items: center; gap: var(--sp-3); }

/* ---- Session ------------------------------------------------------------ */

.sess-head {
  position: sticky; top: 0; z-index: 10;
  margin: calc(-1 * var(--sp-4)) calc(-1 * var(--gutter)) var(--sp-3);
  padding: calc(env(safe-area-inset-top) + var(--sp-2)) var(--gutter) var(--sp-3);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--line);
  display: grid; gap: var(--sp-2);
}
.sess-bar { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-2); }
.sess-title { font-size: var(--size-xl); font-weight: var(--weight-black); letter-spacing: -0.01em; margin: 0; line-height: 1.1; }
.sess-title-btn { text-align: left; min-width: 0; }
.progressbar { height: 4px; border-radius: 2px; background: var(--surface-2); overflow: hidden; }
.progressbar > div { height: 100%; background: var(--done); border-radius: 2px; transition: width 200ms ease; }
.busy-row { display: flex; gap: var(--sp-2); flex-wrap: wrap; align-items: center; font-size: var(--size-sm); color: var(--fg-dim); }
.busy-row .chip { background: var(--danger-soft); color: var(--danger); }

.block-label { margin: var(--sp-5) 0 var(--sp-2); font-size: var(--size-xs); font-weight: var(--weight-bold); letter-spacing: var(--track-label); text-transform: uppercase; color: var(--fg-dim); }
.block-label:first-of-type { margin-top: var(--sp-2); }

.entry { padding: var(--sp-4); display: grid; gap: var(--sp-3); transition: background 150ms ease; }
.entry.is-done { background: var(--done-soft); border-color: transparent; }
.entry-top { display: grid; grid-template-columns: auto 1fr auto; gap: var(--sp-3); align-items: start; }
.check {
  width: 34px; height: 34px; border-radius: 50%; border: 2px solid var(--line-strong);
  display: grid; place-items: center; margin-top: 1px; flex-shrink: 0;
}
.check svg { width: 18px; height: 18px; stroke: var(--accent-fg); stroke-width: 3; fill: none; opacity: 0; }
.is-done .check { background: var(--done); border-color: var(--done); }
.is-done .check svg { opacity: 1; stroke: var(--surface); }
.entry-name { font-weight: var(--weight-bold); font-size: var(--size-lg); line-height: 1.2; margin: 0; }
.entry-meta { display: flex; flex-wrap: wrap; gap: 4px var(--sp-2); align-items: center; color: var(--fg-dim); font-size: var(--size-sm); margin-top: 3px; }
.more { width: var(--tap); height: var(--tap); margin: -8px -10px 0 0; display: grid; place-items: center; color: var(--fg-dim); font-size: 1.4rem; line-height: 1; }

.scheme { display: inline-flex; align-items: center; gap: 4px; font-family: var(--font-num); color: var(--fg); font-weight: var(--weight-mid); }
.scheme { margin-right: var(--sp-1); }
.scheme input {
  width: 2.6em; height: 30px; text-align: center; border-radius: 7px; border: 0; background: var(--surface-2);
  padding: 0 2px; font-family: var(--font-num); font-weight: var(--weight-bold); font-size: 16px;
}
.scheme input.reps { width: 4.2em; }
.scheme input:focus { outline: 2px solid var(--accent); outline-offset: 0; }
.is-done .scheme input { background: color-mix(in srgb, var(--surface) 60%, transparent); }

.last { font-size: var(--size-sm); color: var(--fg-dim); }
.last b { color: var(--fg); font-weight: var(--weight-mid); font-family: var(--font-num); }

.stepper { display: grid; grid-template-columns: 52px 1fr 52px; gap: var(--sp-2); align-items: stretch; }
.step-btn {
  height: 56px; border-radius: var(--radius-sm); background: var(--surface-2); font-size: 1.6rem; font-weight: var(--weight-bold); color: var(--fg);
  display: grid; place-items: center;
}
.step-btn:active { background: var(--line-strong); }
.num-wrap { position: relative; display: flex; align-items: center; justify-content: center; height: 56px; border-radius: var(--radius-sm); border: 1px solid var(--line-strong); background: var(--surface); }
.num-wrap input {
  width: 100%; height: 100%; border: 0; background: transparent; text-align: center;
  font-family: var(--font-num); font-size: var(--size-num); font-weight: var(--weight-bold); font-variant-numeric: tabular-nums;
  padding-right: 2.6em; padding-left: 0.6em;
}
.num-wrap input:focus { outline: none; }
.num-wrap:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.num-wrap .unit { position: absolute; right: var(--sp-3); color: var(--fg-dim); font-size: var(--size-sm); pointer-events: none; }
.is-done .num-wrap, .is-done .step-btn { background: color-mix(in srgb, var(--surface) 60%, transparent); }

.entry-actions { display: flex; gap: var(--sp-2); flex-wrap: wrap; align-items: center; }
.try { background: var(--accent-soft); color: var(--accent); }
.badge-pr { background: var(--pr); color: #2a1f00; font-weight: var(--weight-black); }

.dock {
  position: fixed; inset: auto 0 0 0; z-index: 15;
  padding: var(--sp-3) var(--gutter) calc(env(safe-area-inset-bottom) + var(--sp-3));
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
  border-top: 1px solid var(--line);
}
.dock-inner { max-width: 640px; margin: 0 auto; display: grid; grid-template-columns: 1fr 2fr; gap: var(--sp-2); }

/* ---- Sheet -------------------------------------------------------------- */

.scrim { position: fixed; inset: 0; z-index: 40; background: var(--scrim); opacity: 0; transition: opacity 180ms ease; }
.scrim.open { opacity: 1; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 41;
  max-height: calc(100dvh - env(safe-area-inset-top) - 24px);
  display: flex; flex-direction: column;
  background: var(--bg); border-radius: 18px 18px 0 0;
  transform: translateY(100%); transition: transform 220ms cubic-bezier(.2,.8,.2,1);
  max-width: 640px; margin: 0 auto;
}
.sheet.open { transform: none; }
.sheet.tall { height: calc(100dvh - env(safe-area-inset-top) - 24px); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-2); padding: var(--sp-3) var(--gutter); border-bottom: 1px solid var(--line); }
.sheet-head h2 { margin: 0; font-size: var(--size-lg); font-weight: var(--weight-bold); min-width: 0; }
.sheet-body { overflow-y: auto; padding: var(--sp-4) var(--gutter) calc(env(safe-area-inset-bottom) + var(--sp-6)); -webkit-overflow-scrolling: touch; }
.sheet-foot { padding: var(--sp-3) var(--gutter) calc(env(safe-area-inset-bottom) + var(--sp-3)); border-top: 1px solid var(--line); display: grid; gap: var(--sp-2); }
.sheet-foot.two { grid-template-columns: 1fr 1fr; }

@media (prefers-reduced-motion: reduce) {
  .sheet, .scrim, .progressbar > div, .entry { transition: none; }
}

/* ---- Alternates --------------------------------------------------------- */

.alt { width: 100%; text-align: left; padding: var(--sp-3) var(--sp-4); display: grid; grid-template-columns: 1fr auto; gap: 2px var(--sp-3); align-items: center; border-bottom: 1px solid var(--line); }
.alt:last-child { border-bottom: 0; }
.alt .row-sub b { color: var(--fg); font-family: var(--font-num); font-weight: var(--weight-mid); }

/* ---- Progress ----------------------------------------------------------- */

.stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: var(--sp-2); }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: var(--sp-3); box-shadow: var(--shadow); }
.stat .big { font-family: var(--font-num); font-size: 1.75rem; font-weight: var(--weight-bold); line-height: 1.1; }
.stat .label { font-size: var(--size-xs); color: var(--fg-dim); }

.weeks { padding: var(--sp-4); }
.weeks svg { display: block; width: 100%; height: auto; }

.pr-row { width: 100%; text-align: left; display: grid; grid-template-columns: minmax(0, 1fr) 72px auto; gap: var(--sp-3); align-items: center; padding: var(--sp-3) var(--sp-4); border-bottom: 1px solid var(--line); }
.pr-row:last-child { border-bottom: 0; }
.pr-row .best { font-family: var(--font-num); font-weight: var(--weight-bold); text-align: right; white-space: nowrap; }
.pr-row .delta { font-size: var(--size-xs); text-align: right; }
.pr-row svg { width: 72px; height: 28px; display: block; }
.delta.good { color: var(--done); }
.delta.flat { color: var(--fg-mute); }

svg text { font-family: var(--font-num); font-size: 11px; fill: var(--fg-mute); }
svg text.val { fill: var(--fg); font-weight: 600; }

.chart-card { padding: var(--sp-4) var(--sp-3) var(--sp-2); }
.legend { display: flex; gap: var(--sp-4); font-size: var(--size-xs); color: var(--fg-dim); padding: 0 var(--sp-2) var(--sp-2); }
.legend i { display: inline-block; width: 14px; height: 0; border-top: 2px solid var(--accent); vertical-align: middle; margin-right: 5px; }
.legend i.dash { border-top: 2px dashed var(--fg-mute); }

.log-row { display: grid; grid-template-columns: 76px 1fr auto; gap: var(--sp-3); padding: var(--sp-3) var(--sp-4); border-bottom: 1px solid var(--line); font-size: var(--size-sm); align-items: baseline; }
.log-row:last-child { border-bottom: 0; }
.log-row .d { color: var(--fg-dim); font-family: var(--font-num); }
.log-row .v { font-family: var(--font-num); font-weight: var(--weight-bold); white-space: nowrap; }

/* ---- History ------------------------------------------------------------ */

.hist-entry { display: grid; grid-template-columns: 1fr auto; gap: 2px var(--sp-3); padding: var(--sp-3) var(--sp-4); border-bottom: 1px solid var(--line); align-items: center; }
.hist-entry:last-child { border-bottom: 0; }
.hist-entry .v { font-family: var(--font-num); font-weight: var(--weight-bold); text-align: right; }
.hist-entry .v input { width: 5.5em; min-height: 36px; text-align: right; border-radius: 7px; border: 1px solid var(--line-strong); background: var(--surface); padding: 0 8px; font-family: var(--font-num); }

/* ---- Plans editor ------------------------------------------------------- */

.item { padding: var(--sp-3) var(--sp-4); border-bottom: 1px solid var(--line); display: grid; gap: var(--sp-2); }
.item:last-child { border-bottom: 0; }
.item-top { display: flex; justify-content: space-between; align-items: center; gap: var(--sp-2); }
.item-name { font-weight: var(--weight-mid); text-align: left; min-width: 0; }
.item-fields { display: grid; grid-template-columns: 1.4fr 0.7fr 1fr; gap: var(--sp-2); align-items: end; }
.item-fields label { display: grid; gap: 2px; font-size: var(--size-xs); color: var(--fg-dim); }
.item-fields input { min-height: 38px; border-radius: 7px; border: 1px solid var(--line-strong); background: var(--surface); padding: 0 8px; width: 100%; }
.icon-btns { display: flex; gap: 2px; }
.icon-btn { width: 36px; height: 34px; border-radius: 7px; display: grid; place-items: center; color: var(--fg-dim); background: var(--surface-2); font-size: 1rem; }
.icon-btn.danger { color: var(--danger); }

/* ---- Settings ----------------------------------------------------------- */

.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 3px; }
.seg button { min-height: 36px; border-radius: 8px; font-size: var(--size-sm); font-weight: var(--weight-mid); color: var(--fg-dim); }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--fg); box-shadow: var(--shadow); }

.switch { position: relative; width: 50px; height: 30px; border-radius: 15px; background: var(--line-strong); flex-shrink: 0; transition: background 150ms ease; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,0.25); transition: transform 150ms ease; }
.switch[aria-checked="true"] { background: var(--done); }
.switch[aria-checked="true"]::after { transform: translateX(20px); }

/* ---- Toast -------------------------------------------------------------- */

#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px); z-index: 60;
  transform: translateX(-50%); background: var(--fg); color: var(--bg);
  padding: 10px 16px; border-radius: 999px; font-size: var(--size-sm); font-weight: var(--weight-mid);
  max-width: calc(100vw - 32px); text-align: center; box-shadow: 0 6px 20px rgba(0,0,0,0.2);
}
body[data-mode="session"] #toast { bottom: calc(env(safe-area-inset-bottom) + 96px); }

/* ---- Welcome note ------------------------------------------------------- */

.welcome {
  position: fixed; inset: 0; z-index: 80;
  display: grid; place-items: center; align-content: center; gap: var(--sp-6);
  padding: calc(env(safe-area-inset-top) + var(--sp-6)) var(--sp-6) calc(env(safe-area-inset-bottom) + var(--sp-6));
  background: var(--accent); color: var(--accent-fg);
  opacity: 0; transition: opacity 350ms ease;
  cursor: pointer;
}
.welcome.open { opacity: 1; }
.welcome-card { max-width: 30ch; text-align: center; display: grid; gap: var(--sp-4); justify-items: center; transform: translateY(12px) scale(0.98); transition: transform 500ms cubic-bezier(.2,.8,.2,1); }
.welcome.open .welcome-card { transform: none; }
.welcome-heart svg { width: 44px; height: 44px; fill: var(--pr); }
.welcome.open .welcome-heart svg { animation: beat 1.4s ease-in-out 300ms 2; }
.welcome-text { margin: 0; font-size: clamp(1.6rem, 7.5vw, 2.2rem); font-weight: var(--weight-black); letter-spacing: -0.015em; line-height: 1.15; text-wrap: balance; }
.welcome-from { margin: 0; font-size: var(--size-lg); font-weight: var(--weight-mid); opacity: 0.85; }
.welcome-from::before { content: "— "; }
.welcome-hint { position: absolute; bottom: calc(env(safe-area-inset-bottom) + var(--sp-8)); left: 0; right: 0; margin: 0; text-align: center; font-size: var(--size-sm); opacity: 0.7; }

@keyframes beat {
  0%, 100% { transform: scale(1); }
  15% { transform: scale(1.18); }
  30% { transform: scale(1); }
  45% { transform: scale(1.12); }
}

@media (prefers-reduced-motion: reduce) {
  .welcome, .welcome-card { transition: none; }
  .welcome.open .welcome-heart svg { animation: none; }
}

.msg-preview { padding: var(--sp-4); display: grid; gap: var(--sp-1); }
.msg-preview .row-sub { white-space: pre-line; }
