:root {
  --bg: #0d1117;
  --panel: #12181f;
  --card: #161e27;
  --line: #232c38;
  --text: #e7edf3;
  --muted: #93a3b3;
  --teal: #35d0ba;
  --teal-dim: #1e8f80;
  --warn: #ffb347;
  --blue: #59a6ff;
  --radius: 14px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
html { overflow-x: hidden; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", Roboto, system-ui, -apple-system, sans-serif;
  font-size: 15px;
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: hidden;
}

button { font: inherit; }

/* ---------- topbar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 0.75rem;
  padding: 0.55rem max(1rem, env(safe-area-inset-right)) 0.55rem max(1rem, env(safe-area-inset-left));
  background: linear-gradient(180deg, #141b24, #101620);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 0.55rem; min-width: 0; }
.brand h1 { font-size: 1.1rem; margin: 0; font-weight: 600; letter-spacing: 0.02em; }
.brand-sub { color: var(--muted); font-weight: 400; font-size: 0.78rem; }
.brand-dot {
  width: 14px; height: 14px; border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #6ff2e2, var(--teal));
  box-shadow: 0 0 12px var(--teal);
}
.timer-box {
  display: flex; align-items: baseline; gap: 0.4rem;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0.3rem 0.75rem;
  font-variant-numeric: tabular-nums;
}
.timer-label { color: var(--muted); font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.08em; }
#session-timer { font-size: 1rem; font-weight: 600; color: var(--teal); }

.ctrl-btn:focus-visible,
.ex-btn:focus-visible {
  outline: 2px solid var(--teal);
  outline-offset: 2px;
}
/* ---------- layout ---------- */
.layout {
  display: grid;
  grid-template-columns: 360px 1fr;
  gap: 1rem;
  padding: 1rem max(1rem, env(safe-area-inset-right)) max(1rem, env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-left));
  max-width: 1500px; margin: 0 auto;
  align-items: start;
}
.panel { display: flex; flex-direction: column; gap: 0.9rem; max-height: calc(100vh - 5.2rem); overflow-y: auto; min-width: 0; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
}
.card h3 { margin: 0 0 0.6rem; font-size: 0.92rem; font-weight: 600; }

.ex-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 0.5rem; }
.ex-head h2 { margin: 0; font-size: 1.05rem; }
#phase-pill {
  font-size: 0.65rem; letter-spacing: 0.1em;
  padding: 0.22rem 0.5rem; border-radius: 6px;
  background: #1e2632; color: var(--muted);
  white-space: nowrap; flex-shrink: 0;
}
#phase-pill[data-p="slow"] { background: #153c34; color: var(--teal); }
#phase-pill[data-p="fast"] { background: #2c2e14; color: var(--warn); }
#phase-pill[data-p="paused"] { background: #3a2b1e; color: var(--warn); }
#phase-pill[data-p="completed"] { background: #14334a; color: var(--blue); }

.cue { color: var(--muted); font-size: 0.82rem; line-height: 1.45; margin: 0.45rem 0 0.6rem; }

.stat-row { display: flex; align-items: center; gap: 0.7rem; flex-wrap: wrap; }
.stat { display: flex; flex-direction: column; }
.stat-label { font-size: 0.62rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; }
.stat-val { font-size: 0.98rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.done-chip { margin-left: auto; background: #143c31; color: var(--teal); border: 1px solid var(--teal-dim); border-radius: 8px; padding: 0.2rem 0.5rem; font-size: 0.7rem; }

/* ---------- controls ---------- */
.ctrl-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.45rem; }
.ctrl-btn {
  background: #1b2430; color: var(--text);
  border: 1px solid var(--line); border-radius: 10px;
  min-height: 42px;
  padding: 0.45rem 0.4rem; font-size: 0.82rem; cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.ctrl-btn:hover { background: #22303f; border-color: var(--teal); }
.ctrl-btn.warn:hover { border-color: var(--warn); }
.loop-btn { grid-column: 1 / -1; font-weight: 600; border-color: var(--teal-dim); }
.loop-btn:hover { background: #153c34; border-color: var(--teal); }
.loop-btn.active { background: #153c34; color: var(--teal); border-color: var(--teal); }
/* ---------- library ---------- */
.tag { font-size: 0.66rem; padding: 0.18rem 0.5rem; border-radius: 999px; letter-spacing: 0.06em; }
.lower-tag { background: #123a39; color: var(--teal); }
.upper-tag { background: #1a2f4b; color: var(--blue); }
.ex-list { display: flex; flex-direction: column; gap: 0.35rem; }
.ex-btn {
  display: flex; flex-direction: column; align-items: flex-start; text-align: left;
  background: #1b2430; color: var(--text);
  border: 1px solid transparent; border-radius: 10px;
  min-height: 44px;
  padding: 0.45rem 0.6rem; cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.ex-btn:hover { background: #21303d; }
.ex-btn.active { border-color: var(--teal); background: #15302c; }
.ex-name { font-size: 0.82rem; font-weight: 600; }
.ex-dose { font-size: 0.7rem; color: var(--muted); }

/* ---------- stage ---------- */
.stage-col { display: flex; flex-direction: column; gap: 0.5rem; min-width: 0; }
.stage {
  position: relative;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  height: calc(100vh - 8.4rem);
  min-height: 380px;
  background:
    radial-gradient(circle at 50% 42%, rgb(78 98 94 / 24%), transparent 36%),
    linear-gradient(145deg, #171c1b 0%, #0d1111 72%);
  box-shadow: inset 0 0 80px rgb(0 0 0 / 30%);
}
.stage::after {
  position: absolute;
  inset: 0;
  z-index: 1;
  pointer-events: none;
  content: "";
  background: linear-gradient(90deg, rgb(255 255 255 / 2%) 1px, transparent 1px);
  background-size: 25% 100%;
  mask-image: linear-gradient(to bottom, transparent, #000 18%, #000 82%, transparent);
}
.stage canvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
  touch-action: pan-y;
}
.stage.is-dragging canvas { cursor: grabbing; }
.stage-hud {
  position: absolute;
  z-index: 3;
  top: 1.1rem;
  left: 1.2rem;
  display: flex;
  flex-direction: column;
  gap: 0.22rem;
  pointer-events: none;
  font-family: ui-monospace, "SFMono-Regular", Consolas, monospace;
}
.asset-kicker {
  color: #e8efec;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.13em;
}
.asset-spec {
  color: #7d918c;
  font-size: 0.56rem;
  letter-spacing: 0.1em;
}
.drag-hint {
  position: absolute;
  right: 1.2rem;
  bottom: 1rem;
  z-index: 3;
  color: #74847f;
  pointer-events: none;
  font-family: ui-monospace, "SFMono-Regular", Consolas, monospace;
  font-size: 0.56rem;
  letter-spacing: 0.12em;
}
.drag-hint::before {
  display: inline-block;
  width: 1.7rem;
  height: 1px;
  margin-right: 0.5rem;
  vertical-align: middle;
  content: "";
  background: var(--teal-dim);
}
.countdown-overlay[hidden] {
  display: none;
}
.countdown-overlay {
  position: absolute;
  inset: 0;
  z-index: 6;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.3rem;
  background: rgb(13 17 23 / 78%);
  backdrop-filter: blur(4px);
}
.countdown-label {
  color: var(--muted);
  font-size: 0.85rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}
.countdown-num {
  font-size: 5rem;
  font-weight: 700;
  color: var(--teal);
  font-variant-numeric: tabular-nums;
  text-shadow: 0 0 34px var(--teal-dim);
  animation: countdown-pulse 1s ease-in-out infinite;
}
@keyframes countdown-pulse {
  0% { transform: scale(0.85); opacity: 0.65; }
  30% { transform: scale(1.05); opacity: 1; }
  100% { transform: scale(1); opacity: 0.9; }
}
.stage-strip {
  display: flex; justify-content: space-between; align-items: center;
  padding: 0 0.3rem; color: var(--muted); font-size: 0.72rem;
}
.legend { display: flex; gap: 0.7rem; }
.legend-item { display: flex; align-items: center; gap: 0.3rem; }
.legend-item::before { content: ""; width: 10px; height: 10px; border-radius: 3px; }
.legend-slow::before { background: var(--teal); }
.legend-fast::before { background: var(--warn); }

/* ---------- responsive ---------- */
@media (max-width: 900px) {
  .layout {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-areas:
      "exercise exercise"
      "stage stage"
      "controls controls"
      "lower upper"
      "strip strip";
  }
  .panel,
  .stage-col { display: contents; }
  .exercise-card { grid-area: exercise; }
  .controls { grid-area: controls; }
  .library.lower { grid-area: lower; }
  .library.upper { grid-area: upper; }
  .stage { grid-area: stage; height: clamp(320px, 52svh, 560px); }
  .stage-strip { grid-area: strip; }
}

@media (max-width: 640px) {
  .brand-sub { display: none; }
  .layout {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas:
      "exercise"
      "stage"
      "controls"
      "lower"
      "upper"
      "strip";
    gap: 0.75rem;
    padding-top: 0.75rem;
  }
  .card { padding: 0.8rem; }
  .stage { height: clamp(300px, 52svh, 470px); }
  .stage-strip { gap: 0.75rem; }
  .stage-strip > span:first-child { display: none; }
  .legend { width: 100%; justify-content: center; }
  .ex-list { gap: 0.45rem; }
  .ex-btn { padding: 0.6rem 0.7rem; }
}

@media (max-width: 480px) {
  .topbar {
    padding-top: max(0.5rem, env(safe-area-inset-top));
    gap: 0.45rem;
  }
  .brand h1 { font-size: 1rem; }
  .brand-dot { width: 12px; height: 12px; }
  .timer-box { padding: 0.3rem 0.55rem; }
  .timer-label { display: none; }
  .ex-head h2 { font-size: 1rem; }
  .stat-row { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.5rem; }
  .done-chip { grid-column: 1 / -1; justify-self: start; margin-left: 0; }
  .ctrl-grid { gap: 0.55rem; }
  .ctrl-btn { min-height: 46px; }
}

@media (max-width: 360px) {
  .layout { padding-right: 0.6rem; padding-left: 0.6rem; }
  .topbar { padding-right: 0.6rem; padding-left: 0.6rem; }
}

@media (max-width: 900px) and (orientation: landscape) and (max-height: 520px) {
  .stage { height: 76svh; min-height: 260px; }
}
