:root {
  --bg: #14161a;
  --panel: #1e2128;
  --panel-hi: #262a33;
  --ghost: #2a2e37;
  --text: #f2f3f5;
  --muted: #9aa0ab;
  --accent: #3fa9a0;
  --accent-2: #57c2b8;
  --accent-soft: rgba(63, 169, 160, 0.18);
  --rest: #d98c3f;
  --rest-soft: rgba(217, 140, 63, 0.18);
  --good: #34d17c;
  --radius-lg: 20px;
  --radius-md: 12px;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: radial-gradient(circle at 50% -10%, #1a1d24 0%, var(--bg) 55%);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  height: 100%;
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
}

.app {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: max(16px, env(safe-area-inset-top)) 1.1rem max(16px, env(safe-area-inset-bottom));
  max-width: 480px;
  margin: 0 auto;
}

.hidden { display: none !important; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.55rem 0 0.9rem;
}
.topbar h1 { font-size: 1.3rem; margin: 0; letter-spacing: -0.01em; }
.topbar h1 .dot {
  color: var(--accent);
  font-size: 0.85rem;
  font-weight: 600;
  margin-left: 0.2rem;
  vertical-align: middle;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.screen { flex: 1; display: flex; flex-direction: column; min-height: 0; }

.hint { color: var(--muted); margin: 0 0 0.85rem; font-size: 0.92rem; }

/* ---------- library ---------- */
.grid { display: flex; flex-direction: column; gap: 0.75rem; overflow-y: auto; padding-bottom: 1.25rem; }

.workout-card {
  background: var(--panel);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow);
  border: 1px solid rgba(255, 255, 255, 0.03);
  display: flex;
  gap: 0.9rem;
  align-items: center;
  padding: 0.6rem;
  transition: transform 0.08s ease, background 0.15s ease;
}
.workout-card:active { transform: scale(0.98); background: var(--panel-hi); }
.workout-card-thumb {
  width: 72px;
  height: 96px;
  border-radius: var(--radius-md);
  object-fit: cover;
  background: var(--ghost);
  flex-shrink: 0;
}
.workout-card-body { min-width: 0; }
.workout-card-title { font-weight: 700; font-size: 1.02rem; margin: 0 0 0.25rem; }
.workout-card-meta { color: var(--muted); font-size: 0.85rem; margin: 0 0 0.2rem; }
.workout-card-source { color: var(--muted); font-size: 0.75rem; opacity: 0.8; }

/* ---------- headers shared by detail/active screens ---------- */
.run-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0 0.75rem;
  flex-shrink: 0;
  gap: 0.5rem;
}
.icon-btn {
  background: var(--panel);
  border: none;
  color: var(--text);
  font-size: 1.3rem;
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.icon-btn:active { background: var(--ghost); }
.icon-btn-lg { width: 3.25rem; height: 3.25rem; font-size: 1.6rem; }
.icon-btn-spacer { width: 2.5rem; height: 2.5rem; flex-shrink: 0; }
.run-title { font-weight: 700; color: var(--text); font-size: 0.98rem; text-align: center; flex: 1; }

/* ---------- detail screen ---------- */
.detail-scroll { flex: 1; overflow-y: auto; min-height: 0; }
.detail-hero { width: 100%; aspect-ratio: 16/9; border-radius: var(--radius-lg); overflow: hidden; background: var(--ghost); margin-bottom: 0.9rem; }
.detail-hero-img { width: 100%; height: 100%; object-fit: cover; object-position: 50% 20%; }
.stat-row {
  display: flex;
  gap: 0.6rem;
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 600;
  margin-bottom: 1rem;
}
.stat-row span { background: var(--panel); padding: 0.35rem 0.7rem; border-radius: 999px; }
.exercise-list { display: flex; flex-direction: column; gap: 0.5rem; margin-bottom: 1rem; }
.exercise-row {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  background: var(--panel);
  border-radius: var(--radius-md);
  padding: 0.5rem 0.7rem;
}
.exercise-thumb { width: 40px; height: 52px; border-radius: 8px; object-fit: cover; background: var(--ghost); flex-shrink: 0; }
.exercise-row-name { font-size: 0.92rem; font-weight: 600; }
.detail-source { color: var(--muted); font-size: 0.8rem; padding-bottom: 0.5rem; }

/* ---------- active screen meta ---------- */
.active-meta { display: flex; align-items: center; justify-content: center; gap: 0.7rem; padding: 0.2rem 0 0.6rem; flex-shrink: 0; }
.round-dots { display: flex; gap: 0.35rem; }
.round-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ghost); transition: background 0.2s ease, transform 0.2s ease; }
.round-dot.is-done { background: var(--good); }
.round-dot.is-current { background: var(--accent); transform: scale(1.3); }
.exercise-counter { color: var(--muted); font-size: 0.85rem; font-weight: 600; }

/* ---------- stage ---------- */
.stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 0.5rem;
  min-height: 40vh;
  position: relative;
  z-index: 0;
}

.video-frame {
  position: relative;
  width: min(62vw, 260px);
  aspect-ratio: 9/16;
  border-radius: var(--radius-lg);
  overflow: hidden;
  background: var(--ghost);
  box-shadow: var(--shadow);
  margin-bottom: 0.3rem;
}
.video-poster, .exercise-video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.video-poster { transition: opacity 0.2s ease; }
.video-poster.is-loading { animation: pulse 1.1s ease-in-out infinite; }
.video-poster.is-hidden { opacity: 0; pointer-events: none; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.55; } }
.mute-btn {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  width: 2.1rem;
  height: 2.1rem;
  border-radius: 50%;
  border: none;
  background: rgba(20, 22, 26, 0.6);
  color: var(--text);
  font-size: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(4px);
}

.up-next-frame {
  width: min(38vw, 140px);
  aspect-ratio: 9/16;
  border-radius: var(--radius-md);
  overflow: hidden;
  box-shadow: var(--shadow);
  background: var(--ghost);
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.3s ease, transform 0.3s ease;
}
.up-next-frame.is-visible { opacity: 1; transform: translateY(0); }
.up-next-video { width: 100%; height: 100%; object-fit: cover; display: block; }

.segment-label {
  font-size: 1.25rem;
  font-weight: 800;
  letter-spacing: 0.06em;
  color: var(--accent);
  transition: color 0.25s ease;
  text-transform: uppercase;
  max-width: 90%;
}
.segment-label.is-rest { color: var(--rest); }

.ring-wrap {
  position: relative;
  width: min(68vw, 260px);
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: width 0.2s ease;
}
.stage.phase-work .ring-wrap { width: min(40vw, 150px); }
.ring { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring-track { fill: none; stroke: var(--ghost); stroke-width: 11; }
.ring-fill {
  fill: none;
  stroke: var(--accent);
  stroke-width: 11;
  stroke-linecap: round;
  stroke-dasharray: 628.318;
  stroke-dashoffset: 0;
  transition: stroke-dashoffset 0.25s linear, stroke 0.25s ease;
}
.ring-fill.is-rest { stroke: var(--rest); }

.countdown {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(2.4rem, 12vw, 3.2rem);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}
.stage.phase-work .countdown { font-size: clamp(1.4rem, 7vw, 1.9rem); }

.next-label { color: var(--muted); font-size: 0.92rem; min-height: 1.2em; opacity: 0; transition: opacity 0.3s ease; }
.next-label.is-visible { opacity: 1; }

.done-badge {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: var(--accent-soft);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  margin-bottom: 0.4rem;
}
.done-msg { color: var(--muted); font-size: 1.02rem; margin-top: 0.3rem; }

.progress-wrap { padding: 0 0.25rem 1rem; flex-shrink: 0; }
.progress-bar { height: 7px; background: var(--ghost); border-radius: 999px; overflow: hidden; }
.progress-fill { height: 100%; width: 0%; background: linear-gradient(90deg, var(--accent), var(--accent-2)); transition: width 0.3s linear; }
.progress-time { text-align: center; color: var(--muted); font-size: 0.83rem; margin-top: 0.45rem; font-variant-numeric: tabular-nums; }

.controls { display: flex; gap: 0.6rem; padding-bottom: 1.25rem; flex-shrink: 0; }
.btn {
  flex: 1;
  border: none;
  border-radius: 14px;
  padding: 1rem;
  font-size: 1.02rem;
  font-weight: 700;
  min-height: 44px;
  transition: transform 0.08s ease, filter 0.15s ease;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: linear-gradient(135deg, var(--accent-2), var(--accent)); color: #0b1615; box-shadow: 0 6px 18px rgba(63, 169, 160, 0.28); }
.btn-primary:active { filter: brightness(0.94); }
.btn-ghost { background: var(--panel-hi); color: var(--text); flex: 0.7; }
.btn-ghost:active { filter: brightness(1.15); }
.btn-block { flex: 1; }

/* ---------- screen transitions ---------- */
.screen { transition: opacity 180ms ease; }
.screen.hidden { display: none; opacity: 0; }

/* ---------- lock overlay (reused from couch-to-5k) ---------- */
.lock-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  padding: 1.5rem;
  text-align: center;
  touch-action: none;
}
.lock-top-spacer { flex: 1; }
.lock-bottom-spacer { flex: 2; }
.lock-stage { flex: 0 0 auto; width: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0.5rem; }
.lock-segment { font-size: 1.25rem; font-weight: 800; letter-spacing: 0.06em; color: var(--accent); text-transform: uppercase; }
.lock-segment.is-rest { color: var(--rest); }
.lock-progress-wrap { width: 100%; max-width: 300px; padding: 0 0.25rem; margin-bottom: 0.5rem; flex-shrink: 0; }
.lock-hint { color: var(--muted); font-size: 0.9rem; max-width: 240px; margin: 0.5rem 0 1.75rem; }

.unlock-btn {
  position: relative;
  width: 120px;
  height: 120px;
  border-radius: 50%;
  border: none;
  background: var(--panel);
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-user-select: none;
  user-select: none;
}
.unlock-icon { font-size: 2rem; }
.unlock-ring { position: absolute; inset: 0; width: 100%; height: 100%; transform: rotate(-90deg); }
.unlock-track { fill: none; stroke: var(--ghost); stroke-width: 6; }
.unlock-fill {
  fill: none;
  stroke: var(--accent);
  stroke-width: 6;
  stroke-linecap: round;
  stroke-dasharray: 326.7256;
  stroke-dashoffset: 326.7256;
}
.unlock-fill.filling { transition: stroke-dashoffset 1.1s linear; stroke-dashoffset: 0; }
