:root {
  --bg: #0b0f14;
  --surface: #151b23;
  --surface-2: #1d252f;
  --line: #2a3441;
  --text: #eef2f6;
  --muted: #9aa7b6;
  --accent: #ff6b2c;
  --accent-ink: #1a0a02;
  --danger: #ff5d5d;
  --radius: 16px;
  --phase: #0b0f14;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --mono: ui-monospace, "JetBrains Mono", "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  overscroll-behavior: none;
  transition: background-color .35s ease;
}

button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid #7cc4ff; outline-offset: 2px; }

.view {
  min-height: 100dvh;
  padding: calc(var(--safe-t) + 12px) 16px calc(var(--safe-b) + 24px);
  max-width: 720px;
  margin: 0 auto;
}

/* ---------- barre e pulsanti ---------- */
.bar { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 0 16px; }
.bar h1 { margin: 0; font-size: 1.6rem; letter-spacing: -.01em; }
.bar-actions { display: flex; align-items: center; gap: 8px; }

.btn {
  min-height: 48px; padding: 0 18px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface-2); cursor: pointer; font-weight: 600;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); border-color: #5a2a2a; background: transparent; }

.icon-btn {
  width: 48px; height: 48px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface-2); display: grid; place-items: center; cursor: pointer; color: var(--text);
}
.icon-btn svg { width: 24px; height: 24px; }

/* ---------- elenco ---------- */
.list { display: grid; gap: 12px; }
.card {
  display: grid; grid-template-columns: 1fr auto; align-items: stretch;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden;
}
.card-main {
  text-align: left; background: none; border: 0; padding: 16px; cursor: pointer;
  display: grid; gap: 4px; min-height: 72px;
}
.card-main strong { font-size: 1.1rem; }
.meta { color: var(--muted); font-size: .9rem; }
.card-side { display: grid; justify-items: end; align-content: space-between; padding: 12px 12px 12px 0; gap: 8px; }
.dur { font-family: var(--mono); font-variant-numeric: tabular-nums; color: var(--accent); font-weight: 700; }
.card-side .btn { min-height: 40px; padding: 0 12px; font-size: .9rem; }
.empty { color: var(--muted); text-align: center; padding: 32px 0; }
.hint { color: var(--muted); font-size: .9rem; margin: 24px 4px 0; }
.hint.small { font-size: .8rem; margin: 8px 0 0; }

/* ---------- editor ---------- */
.form { display: grid; gap: 16px; }
.grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.field { display: grid; gap: 6px; }
.field.wide { grid-column: 1 / -1; }
.field > span { color: var(--muted); font-size: .85rem; }
.field small { color: var(--muted); font-size: .8rem; }
.field input, .field textarea, .row select {
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  padding: 12px 14px; min-height: 48px; width: 100%;
}
.field textarea { resize: vertical; }
.total { margin: 0; font-family: var(--mono); color: var(--accent); font-weight: 700; }
.form-actions { display: flex; flex-wrap: wrap; gap: 10px; }

/* ---------- schermata allenamento ---------- */
/* Tutta la schermata resta nello schermo: altezza fissa = viewport, e dimensioni che si adattano all'altezza (dvh). */
#run {
  max-width: none; padding: 0; margin: 0;
  display: grid; grid-template-rows: auto minmax(0, 1fr) auto;
  height: 100vh; height: 100dvh; min-height: 0; overflow: hidden;
}
html:has(body.running-view) { overflow: hidden; }
body.running-view { overflow: hidden; height: 100vh; height: 100dvh; }
body[data-phase="prep"]    { --phase: #a16207; }
body[data-phase="warmup"]  { --phase: #6d28d9; }
body[data-phase="cool"]    { --phase: #6d28d9; }
body[data-phase="work"]    { --phase: #c2410c; }
body[data-phase="rest"]    { --phase: #0f766e; }
body[data-phase="setrest"] { --phase: #1d4ed8; }
body[data-phase="done"]    { --phase: #15803d; }
body[data-phase="ready"]   { --phase: #1d252f; }
body.running-view { background: var(--phase); }

.run-top {
  display: flex; align-items: center; gap: 12px;
  padding: calc(var(--safe-t) + 10px) 16px 8px;
}
.run-top .icon-btn { background: rgba(0,0,0,.25); border-color: rgba(255,255,255,.18); }
.run-title { flex: 1; min-width: 0; font-weight: 700; opacity: .9; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.run-body {
  display: grid; align-content: center; justify-items: center; gap: clamp(2px, 1.2dvh, 10px);
  padding: 0 20px; text-align: center; min-height: 0; overflow: hidden;
}
.phase { font-weight: 800; letter-spacing: .18em; font-size: clamp(.95rem, 3dvh, 1.3rem); opacity: .95; }
.clock {
  font-family: var(--mono); font-variant-numeric: tabular-nums; font-weight: 800;
  font-size: clamp(3.2rem, min(26vw, 19dvh), 10rem); line-height: 1; letter-spacing: -.04em;
}
.exercise { font-size: clamp(1.2rem, min(6vw, 4.2dvh), 2.2rem); font-weight: 800; min-height: 1.2em; line-height: 1.15; }
.next { font-size: clamp(.9rem, 2.4dvh, 1.1rem); opacity: .85; min-height: 1.3em; }
.bar-progress { width: min(520px, 100%); height: clamp(6px, 1.2dvh, 10px); border-radius: 99px; background: rgba(0,0,0,.28); overflow: hidden; margin-top: clamp(2px, 1.2dvh, 12px); }
.bar-progress span { display: block; height: 100%; width: 0%; background: #fff; border-radius: 99px; }
.round { font-weight: 700; font-size: clamp(.9rem, 2.4dvh, 1.05rem); min-height: 1.3em; }
.totals { font-family: var(--mono); font-size: clamp(.75rem, 2dvh, .9rem); opacity: .8; font-variant-numeric: tabular-nums; }

.run-controls {
  display: flex; align-items: center; justify-content: center; gap: clamp(16px, 5vw, 28px);
  padding: clamp(6px, 1.5dvh, 12px) 16px calc(var(--safe-b) + clamp(10px, 3dvh, 28px));
}
.ctl {
  --d: clamp(52px, 9dvh, 72px);
  width: var(--d); height: var(--d); border-radius: 50%; border: 2px solid rgba(255,255,255,.4);
  background: rgba(0,0,0,.25); color: #fff; display: grid; place-items: center; cursor: pointer;
}
.ctl svg { width: 42%; height: 42%; }
.ctl.big { --d: clamp(72px, 13dvh, 104px); background: #fff; color: #111; border-color: #fff; }
.ctl.big svg { width: 44%; height: 44%; }
.ctl:active { transform: scale(.95); }

/* ---------- dialoghi ---------- */
dialog {
  border: 1px solid var(--line); border-radius: 20px; background: var(--surface); color: var(--text);
  padding: 0; width: min(520px, calc(100vw - 24px)); max-height: calc(100vh - 24px); max-height: calc(100dvh - 24px);
  overflow: auto; overscroll-behavior: contain;
}
dialog::backdrop { background: rgba(0,0,0,.65); }
.sheet { display: grid; gap: 14px; padding: 20px; }
.sheet h2 { margin: 0; font-size: 1.25rem; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.row.check { justify-content: flex-start; min-height: 44px; }
.row.check input { width: 22px; height: 22px; accent-color: var(--accent); }
.row input[type="range"] { flex: 1; max-width: 220px; accent-color: var(--accent); }
.row select { width: auto; min-width: 170px; }
.row.buttons { flex-wrap: wrap; justify-content: flex-start; }
.row.buttons.end { justify-content: flex-end; }
.modes { border: 1px solid var(--line); border-radius: 14px; padding: 8px 12px 12px; display: grid; gap: 8px; margin: 0; }
.modes legend { padding: 0 6px; color: var(--muted); font-size: .85rem; }
.mode { display: flex; gap: 12px; align-items: flex-start; padding: 8px 0; cursor: pointer; }
.mode input { width: 22px; height: 22px; margin-top: 2px; accent-color: var(--accent); flex: none; }
.mode small { display: block; color: var(--muted); font-size: .82rem; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-b) + 24px); transform: translate(-50%, 20px);
  background: #fff; color: #111; padding: 12px 18px; border-radius: 12px; font-weight: 600;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; max-width: calc(100vw - 32px); z-index: 50;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (min-width: 560px) {
  .grid { grid-template-columns: repeat(4, 1fr); }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
