:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #15181d;
  --muted: #6b7280;
  --border: #e3e5e9;
  --accent: #16a34a;
  --accent-text: #ffffff;
  --chip-bg: #eef0f3;

  --leadin: #d97706;
  --active: #16a34a;
  --rest: #2563eb;
  --setrest: #7c3aed;
  --done: #0f766e;

  --radius: 14px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418;
    --surface: #1b1f25;
    --text: #eef0f3;
    --muted: #9aa1ab;
    --border: #2b3038;
    --chip-bg: #262b33;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  min-height: 100dvh;
}

[hidden] { display: none !important; }

.muted { color: var(--muted); }
.small { font-size: 0.8rem; }

/* ---------- Setup ---------- */

.setup {
  max-width: 760px;
  margin: 0 auto;
  padding: 24px 16px 120px;
}

.app-header h1 { margin: 0 0 4px; font-size: 1.75rem; }
.app-header p { margin: 0 0 20px; }

.circuits { display: grid; gap: 16px; }

.presets { display: grid; gap: 10px; margin-bottom: 16px; }
.preset-row { display: flex; gap: 8px; margin: 0; }
.preset-row select,
.preset-row input {
  flex: 1;
  min-width: 0;
  height: 44px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}
.preset-row select:focus,
.preset-row input:focus { outline: none; border-color: var(--accent); }
.presets .hint:empty { display: none; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}

.card-head {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
}
.card-head h2 { margin: 0; font-size: 1.15rem; }
.circuit-duration { margin-left: auto; font-variant-numeric: tabular-nums; }

.fields {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 14px 16px;
}

.field label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 6px;
}

.stepper {
  display: flex;
  align-items: stretch;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  background: var(--bg);
}

.stepper button {
  flex: 0 0 48px;
  height: 48px;
  border: 0;
  background: var(--chip-bg);
  color: var(--text);
  font-size: 1.4rem;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}
.stepper button:active { filter: brightness(0.9); }
.stepper button:disabled { opacity: 0.35; cursor: default; }

.stepper .value {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-width: 0;
}

.stepper input {
  width: 100%;
  min-width: 0;
  border: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 1.15rem;
  font-weight: 600;
  text-align: center;
  font-variant-numeric: tabular-nums;
  -moz-appearance: textfield;
  appearance: textfield;
}
.stepper input::-webkit-outer-spin-button,
.stepper input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.stepper input:focus { outline: none; }
.stepper:focus-within { border-color: var(--accent); }

.stepper .unit {
  color: var(--muted);
  font-size: 0.85rem;
  padding-right: 10px;
}

.hint {
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 4px;
  min-height: 1em;
}

.splits { margin-top: 18px; }
.splits-label {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 10px;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 8px;
}
.splits-label .small { font-weight: 400; }

.split-chips { display: flex; flex-wrap: wrap; gap: 8px; }

.chip {
  min-width: 44px;
  height: 44px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--chip-bg);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}
.chip[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

/* ---------- Buttons ---------- */

.btn {
  font: inherit;
  font-weight: 600;
  border-radius: 12px;
  border: 1px solid transparent;
  padding: 10px 16px;
  cursor: pointer;
  touch-action: manipulation;
}
.btn-lg { padding: 14px 24px; font-size: 1.1rem; }
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-primary:active { filter: brightness(0.92); }
.btn-secondary { background: var(--chip-bg); color: var(--text); border-color: var(--border); }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn-ghost {
  background: transparent;
  color: var(--text);
  border: 1px dashed var(--border);
}
.btn-icon {
  background: transparent;
  color: var(--muted);
  border: 0;
  width: 36px;
  height: 36px;
  padding: 0;
  font-size: 1rem;
}
.btn-icon:hover { color: var(--text); }
.btn-light { background: rgba(255, 255, 255, 0.95); color: #111; }
.btn-outline {
  background: transparent;
  color: #fff;
  border: 2px solid rgba(255, 255, 255, 0.7);
}

.add-circuit { width: 100%; margin-top: 16px; padding: 14px; }

.start-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px max(16px, calc((100vw - 760px) / 2 + 16px)) calc(12px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.total { display: flex; flex-direction: column; line-height: 1.2; }
.total strong { font-size: 1.3rem; font-variant-numeric: tabular-nums; }

/* ---------- Timer ---------- */

.timer {
  --phase: var(--leadin);
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: var(--phase);
  color: #fff;
  padding: calc(16px + env(safe-area-inset-top)) 16px calc(20px + env(safe-area-inset-bottom));
  transition: background-color 0.3s ease;
}
.timer[data-phase="leadin"] { --phase: var(--leadin); }
.timer[data-phase="active"] { --phase: var(--active); }
.timer[data-phase="rest"] { --phase: var(--rest); }
.timer[data-phase="setrest"] { --phase: var(--setrest); }
.timer[data-phase="done"] { --phase: var(--done); }

.timer-top {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-weight: 600;
  opacity: 0.9;
  font-variant-numeric: tabular-nums;
}

.timer-center {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 8px;
}

.phase {
  font-size: clamp(1.8rem, 7vw, 3.2rem);
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.round { font-size: clamp(1rem, 4vw, 1.4rem); font-weight: 600; opacity: 0.9; min-height: 1.4em; }

.time {
  font-size: clamp(6rem, 30vw, 16rem);
  font-weight: 800;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}

.split-note {
  font-size: clamp(1rem, 4.5vw, 1.5rem);
  font-weight: 700;
  background: rgba(0, 0, 0, 0.18);
  padding: 6px 14px;
  border-radius: 999px;
}

.progress {
  width: min(560px, 90%);
  height: 10px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.2);
  overflow: hidden;
  margin-top: 12px;
}
.progress-fill {
  height: 100%;
  width: 0;
  background: rgba(255, 255, 255, 0.9);
}

.next { margin-top: 10px; font-weight: 600; opacity: 0.85; min-height: 1.4em; }

.timer.paused .time { opacity: 0.5; }

/* Workout map: one block per interval, width proportional to duration. */
.flow {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  width: min(760px, 100%);
  height: 28px;
  margin: 0 auto 20px;
}
.flow-seg {
  position: relative;
  flex: 1 1 0;
  min-width: 2px;
  height: 45%;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.22);
  overflow: hidden;
}
.flow-seg.active { height: 100%; }
.flow-seg.circuit-start:not(:first-child) { margin-left: 8px; }
.flow-seg .fill {
  position: absolute;
  inset: 0 auto 0 0;
  width: 0;
  background: rgba(255, 255, 255, 0.9);
}
.flow-seg.current { box-shadow: 0 0 0 2px #fff; }
.flow-seg .split-mark {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 2px;
  margin-left: -1px;
  background: rgba(0, 0, 0, 0.35);
}

.timer-controls {
  display: flex;
  gap: 12px;
  justify-content: center;
}
.timer-controls .btn { flex: 1; max-width: 220px; }
