/* Workout Tracer — un solo foglio.
   Token su :root (tema scuro via prefers-color-scheme), tipografia di sistema,
   layout mobile-first: topbar 56px, main che scorre, tabbar 64px, gutter 16px,
   contenuto largo al massimo 640px. */

:root {
  color-scheme: light;
  --bg: #f3f5f8;
  --surface: #ffffff;
  --surface-2: #e9edf2;
  --ink: #15202b;
  --muted: #5d6b7a;
  --line: #d9dee5;
  --accent: #e4572e;
  --accent-ink: #111112;
  --accent-soft: #fce9e4;
  --accent-text: #c53905;
  --ok: #1f8a4c;
  --ok-soft: #e1f3e8;
  --warn: #b8730a;
  --danger: #c22d6d;
  --danger-ink: #ffffff;
  --danger-soft: #fbe6e3;
  --radius: 12px;
  --gap: 12px;
  --tap: 44px;
  --topbar-h: 56px;
  --tabbar-h: 64px;
  --bar-row: 52px;
  --shadow: 0 1px 2px rgba(21, 32, 43, .06), 0 8px 24px rgba(21, 32, 43, .08);
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #0f141a;
    --surface: #182028;
    --surface-2: #232d38;
    --ink: #eef2f6;
    --muted: #9aa8b6;
    --line: #2b3641;
    --accent: #e4572e;
    --accent-ink: #111112;
    --accent-soft: #382929;
    --accent-text: #f5663e;
    --ok: #46c27a;
    --ok-soft: #173224;
    --warn: #e2a63a;
    --danger: #ea6c99;
    --danger-ink: #111112;
    --danger-soft: #3d1f1b;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .4);
  }
}

/* ---------- base ---------- */

*, *::before, *::after { box-sizing: border-box; }

html { height: 100%; -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  font-size: 16px;
  line-height: 1.4;
  font-variant-numeric: tabular-nums;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, p { margin: 0; }
h1 { font-size: 22px; line-height: 1.25; font-weight: 700; }
h2 { font-size: 17px; line-height: 1.3; font-weight: 700; }
h3 { font-size: 15px; line-height: 1.3; font-weight: 600; }
a { color: var(--accent-text); }
button, input, select, textarea { font: inherit; color: inherit; }
button { -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
svg { display: block; }
[hidden] { display: none !important; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
:focus:not(:focus-visible) { outline: none; }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.stack { display: flex; flex-direction: column; gap: var(--gap); }
.spread { display: flex; align-items: center; justify-content: space-between; gap: 8px; }

/* ---------- shell ---------- */

.topbar {
  flex: 0 0 auto;
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: calc(var(--topbar-h) + env(safe-area-inset-top, 0px));
  padding: env(safe-area-inset-top, 0px) max(16px, calc((100% - 640px) / 2)) 0;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.topbar__title {
  flex: 1;
  min-width: 0;
  font-size: 18px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topbar__right { display: flex; align-items: center; gap: 4px; }

main {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 16px max(16px, calc((100% - 640px) / 2)) 24px;
}
body.app--workout main { padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px) + 16px); }

.tabbar {
  flex: 0 0 auto;
  display: flex;
  min-height: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px));
  padding: 0 max(0px, calc((100% - 640px) / 2)) env(safe-area-inset-bottom, 0px);
  background: var(--surface);
  border-top: 1px solid var(--line);
}
.tabbar__item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: var(--tap);
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  text-decoration: none;
  -webkit-tap-highlight-color: transparent;
}
.tabbar__item svg { width: 24px; height: 24px; }
.tabbar__item--active { color: var(--accent-text); }
body.app--workout .tabbar { display: none; }

.workout-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px));
  padding: 8px max(16px, calc((100% - 640px) / 2)) calc(8px + env(safe-area-inset-bottom, 0px));
  background: var(--surface);
  border-top: 1px solid var(--line);
}
/* Con il chip di recupero la barra ha due righe (--bar-row = una riga + il gap). */
.workout-bar--stacked { flex-direction: column; align-items: stretch; }
body.app--workout main:has(.workout-bar--stacked) { padding-bottom: calc(var(--tabbar-h) + var(--bar-row) + env(safe-area-inset-bottom, 0px) + 16px); }
.workout-bar__row { display: flex; gap: 8px; width: 100%; }
.workout-bar .workout-bar__grow, .workout-bar .workout-rest { flex: 1 1 0; justify-content: center; }
.workout__current:not(:last-child) { border-bottom: 1px solid var(--line); }
.workout__body { padding: 12px 16px 16px; }
.workout__facts { gap: 4px; }
.workout__done { color: var(--ok); font-weight: 700; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--tap);
  height: var(--tap);
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
}
.icon-btn:active { background: var(--surface-2); }

/* ---------- superfici ---------- */

.card {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.card__title {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--muted);
}

.list {
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.row {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 56px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
}
.row:last-child { border-bottom: 0; }
.row--tap { cursor: pointer; }
.row--tap:active { background: var(--surface-2); }
.row--current { background: var(--accent-soft); }
.row--disabled { opacity: .55; }
.row__main { flex: 1; min-width: 0; }
.row__title { font-weight: 500; overflow-wrap: anywhere; }
.row__sub { font-size: 13px; color: var(--muted); }
/* 0021 P4: l'obiettivo della scheda si legge come un dato, la nota resta secondaria */
.workout__target, .routine__target { color: var(--ink); overflow-wrap: anywhere; }
.workout__target-note, .routine__note { overflow-wrap: anywhere; }
.routine__description { margin: 0; white-space: pre-line; overflow-wrap: anywhere; }
.nowrap { white-space: nowrap; }
.row__aside { flex: 0 0 auto; text-align: right; }
button.row {
  width: 100%;
  text-align: left;
  background: var(--surface);
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
}
button.row:last-child { border-bottom: 0; }
button.row:disabled { cursor: default; }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: var(--gap); }
.tile {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.tile__label { font-size: 13px; color: var(--muted); }
.kpi { font-size: 28px; font-weight: 700; line-height: 1.1; letter-spacing: -.01em; }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  line-height: 1.4;
  white-space: nowrap;
}
.badge--pr { background: var(--ok-soft); color: var(--ok); }

.empty {
  padding: 32px 16px;
  text-align: center;
  color: var(--muted);
}

/* ---------- bottoni ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: var(--tap);
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  user-select: none;
}
.btn:active { background: var(--surface-2); transform: scale(.98); }
.btn:disabled { opacity: .5; cursor: default; transform: none; }
.btn--primary {
  min-height: 56px;
  font-size: 17px;
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}
.btn--primary:active { background: var(--accent); filter: brightness(1.08); }
.btn--danger { background: var(--danger); border-color: var(--danger); color: var(--danger-ink); }
.btn--danger:active { background: var(--danger); filter: brightness(.92); }
.btn--ghost { background: transparent; border-color: transparent; color: var(--accent-text); }
.btn--ghost:active { background: var(--accent-soft); }
.btn--block { display: flex; width: 100%; }
/* "← Storico", "← Progressi", "← Home": link di ritorno allineati a sinistra dentro .stack */
.history__back, .progress__back, .workout__back { align-self: flex-start; padding-left: 0; }

.chips {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
  padding: 2px 0;
  -webkit-overflow-scrolling: touch;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  min-height: var(--tap);
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  user-select: none;
}
.chip--on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.chip__n { margin-left: 6px; font-size: 12px; opacity: .75; }

/* ---------- campi ---------- */

.field { display: flex; flex-direction: column; gap: 6px; }
.field > label { font-size: 13px; font-weight: 600; color: var(--muted); }
.field__head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.field__head label { font-size: 13px; font-weight: 600; color: var(--muted); }
.field > input, .field > select, .field > textarea, .picker__search {
  width: 100%;
  min-height: var(--tap);
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
}
.field > textarea { padding: 10px 12px; resize: vertical; }
.field__error { font-size: 13px; color: var(--danger); }
/* Sotto un campo: il totale della slitta o un riferimento (0020 P6, P16); l'avviso di
   plausibilità, che non è un errore e non blocca l'invio (Esito 5). */
.field__note { font-size: 13px; color: var(--muted); }
.field__warn { font-size: 13px; color: var(--warn); }
.field--invalid input { border-color: var(--danger); }
input::placeholder { color: var(--muted); opacity: .7; }

.stepper {
  display: grid;
  grid-template-columns: 56px minmax(0, 1fr) 56px;
  gap: 8px;
  align-items: center;
}
.stepper__btn {
  width: 56px;
  height: 56px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--ink);
  font-size: 28px;
  line-height: 1;
  cursor: pointer;
  user-select: none;
}
.stepper__btn:active { background: var(--line); }
.stepper__value {
  width: 100%;
  min-width: 0;
  height: 56px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  font-size: 28px;
  font-weight: 600;
  text-align: center;
}

.set-form { display: flex; flex-direction: column; gap: 12px; }
.set-form__primary { display: grid; gap: 12px; }
@media (min-width: 480px) {
  .set-form__primary { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
}
.more > summary {
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: var(--tap);
  color: var(--accent-text);
  font-weight: 600;
  cursor: pointer;
  user-select: none;
}
.more > summary::-webkit-details-marker { display: none; }
.more > summary::after {
  content: '';
  width: 8px;
  height: 8px;
  border-right: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: rotate(45deg) translateY(-2px);
  transition: transform .15s;
}
.more[open] > summary::after { transform: rotate(-135deg) translateY(2px); }
.more__body { display: flex; flex-direction: column; gap: 12px; padding-top: 4px; }
.more__row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 2fr); gap: 12px; }

/* ---------- impostazioni ---------- */

/* Masse degli attrezzi: nome ed etichetta, poi campo stretto e Salva; sotto i ~360 px il
   campo va a capo invece di schiacciare il nome. */
.settings__mass { flex-wrap: wrap; }
.settings__mass .row__main { min-width: 10em; }
.settings__mass-edit { display: flex; align-items: center; gap: 8px; margin-left: auto; }
.settings__mass-edit input {
  width: 7em;
  min-height: var(--tap);
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  text-align: right;
}

/* ---------- picker ---------- */

.picker { display: flex; flex-direction: column; gap: 12px; }
.difficulty { font-size: 10px; letter-spacing: 1px; color: var(--accent-text); white-space: nowrap; }
.picker__check { font-size: 18px; font-weight: 700; color: var(--ok); }

/* ---------- sheet ---------- */

.sheet__backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: var(--accent-ink);
  opacity: .45;
  animation: velo-in .15s ease;
}
.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 41;
  display: flex;
  flex-direction: column;
  max-width: 640px;
  max-height: 90vh;
  max-height: 90dvh;
  margin: 0 auto;
  padding: 10px 16px calc(16px + env(safe-area-inset-bottom, 0px));
  background: var(--surface);
  border-radius: var(--radius) var(--radius) 0 0;
  box-shadow: 0 -8px 32px rgba(0, 0, 0, .25);
  animation: slide-up .2s ease;
  outline: none;
}
.sheet__handle { width: 36px; height: 4px; margin: 0 auto 10px; border-radius: 2px; background: var(--line); }
.sheet__title { margin-bottom: 12px; font-size: 18px; font-weight: 700; }
.sheet__body { flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
.sheet__text { font-size: 16px; }
.sheet__actions { display: flex; flex-direction: column; gap: 8px; padding-top: 12px; }

/* ---------- toast ---------- */

#toast-host {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px) + 12px);
  z-index: 50;
  display: flex;
  justify-content: center;
  padding: 0 16px;
  pointer-events: none;
}
/* In allenamento la barra in basso può avere due righe: il toast sta sopra entrambe. */
body.app--workout #toast-host { bottom: calc(var(--tabbar-h) + var(--bar-row) + env(safe-area-inset-bottom, 0px) + 12px); }
.toast {
  max-width: min(100%, 480px);
  padding: 10px 16px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--bg);
  font-size: 14px;
  font-weight: 500;
  box-shadow: var(--shadow);
  animation: toast-in .2s ease;
  pointer-events: auto;
}
.toast--success { background: var(--ok); color: #ffffff; }
.toast--error { background: var(--danger); color: var(--danger-ink); }

/* ---------- grafici ---------- */

.chart { display: block; width: 100%; overflow: visible; touch-action: manipulation; font-variant-numeric: tabular-nums; }
.chart__grid { stroke: var(--line); stroke-width: 1; }
.chart__line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart__area { fill: var(--accent); opacity: .1; }
.chart__pt { fill: var(--accent); stroke: var(--surface); stroke-width: 2; }
.chart__bar { fill: var(--accent); transition: opacity .15s; }
.chart__bar--muted { opacity: .55; }
.chart__bar--hover { opacity: 1; filter: brightness(1.12); }
.chart__hit { fill: transparent; pointer-events: all; cursor: pointer; }
.chart__label { fill: var(--muted); font-size: 11px; }
.chart__value { fill: var(--ink); font-size: 12px; font-weight: 600; }
.chart__tip { pointer-events: none; }
.chart__tip rect { fill: var(--ink); }
.chart__tip text { fill: var(--bg); font-size: 12px; font-weight: 600; }

/* ---------- movimento ---------- */

@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes velo-in { from { opacity: 0; } to { opacity: .45; } }
@keyframes slide-up { from { transform: translateY(24px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
    scroll-behavior: auto !important;
  }
}
