/* ---------- Design tokens ---------- */
:root {
  --bg: #000000;
  --bg-2: #000000;
  --surface: rgba(255, 255, 255, 0.055);
  --surface-2: rgba(255, 255, 255, 0.09);
  --border: rgba(255, 255, 255, 0.10);
  --text: #f4f4fb;
  --text-dim: #a6a6c2;
  --text-faint: #8a8ab0; /* raised for WCAG AA contrast on the dark surfaces */

  /* phase accents (overridden per phase on <body>) */
  --accent: #8b5cf6;
  --accent-2: #c084fc;
  --glow: rgba(139, 92, 246, 0.35);

  --ring-size: min(78vw, 46vh, 420px);
  --ease: cubic-bezier(.22, 1, .36, 1);

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

/* Phase palettes */
body[data-phase="idle"]     { --accent:#8b5cf6; --accent-2:#c084fc; --glow:rgba(139,92,246,.30); }
body[data-phase="prepare"]  { --accent:#fbbf24; --accent-2:#fcd34d; --glow:rgba(251,191,36,.34); }
body[data-phase="work"]     { --accent:#ff4d6d; --accent-2:#ff9a44; --glow:rgba(255,77,109,.40); }
body[data-phase="rest"]     { --accent:#22d3ee; --accent-2:#38bdf8; --glow:rgba(34,211,238,.34); }
body[data-phase="rest-set"] { --accent:#34d399; --accent-2:#22d3ee; --glow:rgba(52,211,153,.32); }
body[data-phase="done"]     { --accent:#34d399; --accent-2:#a3e635; --glow:rgba(52,211,153,.40); }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }

/* Stop the browser's double-tap-to-zoom on controls (rapid stepper taps) while
   keeping pinch-zoom available. Drag handles set their own touch-action below. */
button, input { touch-action: manipulation; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
  user-select: none;
  -webkit-user-select: none;
}

/* ---------- Top bar ---------- */
.topbar {
  position: relative; z-index: 3;
  display: flex; align-items: center; justify-content: flex-end;
  padding: 14px 18px 4px;
}
.topbar-actions { display: flex; gap: 8px; }

.icon-btn {
  display: grid; place-items: center;
  width: 44px; height: 44px; border-radius: 12px;
  background: var(--surface); border: 1px solid var(--border);
  color: var(--text); cursor: pointer;
  transition: background .2s, transform .1s;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn:active { transform: scale(.94); }
.icon-btn svg { width: 22px; height: 22px; fill: currentColor; }

.ic-sound-off { display: none; }
body.muted .ic-sound-on { display: none; }
body.muted .ic-sound-off { display: block; }

.ic-bell-on { display: none; }
body.alerts-on .ic-bell-on { display: block; }
body.alerts-on .ic-bell-off { display: none; }
body.alerts-on #alertsBtn { color: var(--accent-2); border-color: color-mix(in srgb, var(--accent) 45%, var(--border)); }

/* ---------- Overall workout progress ---------- */
.workout-progress {
  position: relative; z-index: 3;
  height: 4px; margin: 8px 18px 0;
  border-radius: 99px; background: var(--surface);
  overflow: hidden;
}
.workout-progress span {
  display: block; height: 100%; width: 0%;
  border-radius: 99px;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  box-shadow: 0 0 12px var(--glow);
  transition: width .25s linear, background .6s var(--ease);
}

/* ---------- Stage ---------- */
.stage {
  position: relative; z-index: 2;
  flex: 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: clamp(14px, 3.2vh, 30px);
  padding: 10px 18px 22px;
  min-height: 0;
}

.meta { text-align: center; }
.phase-label {
  font-size: clamp(15px, 4.4vw, 20px);
  font-weight: 800; letter-spacing: .32em; text-transform: uppercase;
  color: var(--accent-2);
  transition: color .5s var(--ease);
}
.counters {
  margin-top: 8px;
  display: inline-flex; align-items: baseline; gap: 12px;
  color: var(--text-dim); font-size: 13px;
}
.counter { display: inline-flex; align-items: baseline; gap: 6px; }
.counter-val { font-size: 17px; font-weight: 700; color: var(--text); font-variant-numeric: tabular-nums; }
.counter-lab { font-size: 11px; letter-spacing: .12em; text-transform: uppercase; color: var(--text-faint); }
.counter-sep { color: var(--text-faint); }
body.single-set #setCounter, body.single-set #setSep { display: none; }

/* ---------- Timer ring ---------- */
.timer-wrap {
  position: relative;
  width: var(--ring-size); height: var(--ring-size);
  display: grid; place-items: center;
}
.ring { width: 100%; height: 100%; transform: rotate(-90deg); overflow: visible; }
.ring-track {
  fill: none; stroke: var(--surface); stroke-width: 12;
}
.ring-progress {
  fill: none; stroke: url(#ringGrad);
  stroke-width: 12; stroke-linecap: round;
  stroke-dasharray: 691.15; /* 2πr, r=110 */
  stroke-dashoffset: 0;
  filter: drop-shadow(0 0 10px var(--glow));
  transition: stroke .5s var(--ease), stroke-dashoffset .18s linear;
}

.readout {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 4px;
}
.time {
  font-size: clamp(56px, 17vw, 118px);
  font-weight: 800; line-height: .95;
  font-variant-numeric: tabular-nums;
  letter-spacing: -.02em;
  color: var(--text);
  transition: color .3s var(--ease), transform .15s var(--ease);
}
.next {
  font-size: 13px; letter-spacing: .04em;
  color: var(--text-dim);
  max-width: 80%; text-align: center;
}
body[data-phase="work"] .time { text-shadow: 0 0 34px var(--glow); }

/* Count-in emphasis (last 3 seconds) */
body.counting .time { color: var(--accent-2); animation: pulse .5s var(--ease); }
@keyframes pulse { 0%{ transform: scale(1);} 40%{ transform: scale(1.09);} 100%{ transform: scale(1);} }

/* ---------- Controls ---------- */
.controls { display: flex; align-items: center; gap: clamp(18px, 6vw, 34px); }
.ctrl {
  display: grid; place-items: center;
  border: none; cursor: pointer; color: var(--text);
  transition: transform .12s var(--ease), background .3s var(--ease), box-shadow .3s var(--ease);
}
.ctrl:active { transform: scale(.92); }
.ctrl-sec {
  width: 58px; height: 58px; border-radius: 50%;
  background: var(--surface); border: 1px solid var(--border);
}
.ctrl-sec:hover { background: var(--surface-2); }
.ctrl-sec svg { width: 26px; height: 26px; }
.ctrl-primary {
  width: 92px; height: 92px; border-radius: 50%;
  background: linear-gradient(145deg, var(--accent), var(--accent-2));
  color: #0a0a12;
  box-shadow: 0 12px 40px -8px var(--glow), inset 0 1px 1px rgba(255,255,255,.35);
}
.ctrl-primary svg { width: 40px; height: 40px; fill: currentColor; }
.ctrl-primary .ic-pause { display: none; }
body.running .ctrl-primary .ic-play { display: none; }
body.running .ctrl-primary .ic-pause { display: block; }

.reset-btn {
  background: none; border: none; color: var(--text-faint);
  font-size: 13px; font-weight: 600; letter-spacing: .12em; text-transform: uppercase;
  padding: 12px 20px; min-height: 44px; cursor: pointer; border-radius: 10px;
  transition: color .2s, background .2s;
}
.reset-btn:hover { color: var(--text-dim); background: var(--surface); }

/* ---------- Settings sheet ---------- */
.sheet-scrim {
  position: fixed; inset: 0; z-index: 8;
  background: rgba(4, 4, 10, .6);
  backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px);
  animation: fade .25s var(--ease);
}
@keyframes fade { from { opacity: 0; } }

.sheet {
  position: fixed; z-index: 9; left: 0; right: 0; bottom: 0;
  max-height: 88vh; overflow-y: auto;
  background: linear-gradient(180deg, #16142a, #100e1e);
  border-top: 1px solid var(--border);
  border-radius: 26px 26px 0 0;
  padding: 10px calc(20px + var(--safe-right)) calc(28px + var(--safe-bottom)) calc(20px + var(--safe-left));
  box-shadow: 0 -20px 60px rgba(0,0,0,.5);
  animation: slideUp .34s var(--ease);
}
@keyframes slideUp { from { transform: translateY(100%); } }
@media (min-width: 720px) {
  .sheet {
    left: 50%; right: auto; bottom: 50%;
    transform: translate(-50%, 50%);
    width: 460px; max-height: 82vh;
    border-radius: 24px; border: 1px solid var(--border);
    animation: popIn .3s var(--ease);
  }
  @keyframes popIn { from { opacity: 0; transform: translate(-50%, calc(50% + 16px)); } }
  .sheet-handle { display: none; }
}

.sheet-handle {
  width: 40px; height: 4px; border-radius: 99px;
  background: var(--surface-2); margin: 6px auto 4px;
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin: 6px 0 14px; }
.sheet-head h2 { margin: 0; font-size: 22px; font-weight: 800; }

.group-title {
  margin: 8px 0; font-size: 11px; font-weight: 700;
  letter-spacing: .16em; text-transform: uppercase; color: var(--text-faint);
}

.preset-row { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 18px; }
.preset {
  flex: 1 1 auto;
  padding: 10px 12px; border-radius: 13px;
  background: var(--surface); border: 1px solid var(--border);
  color: var(--text); cursor: pointer; text-align: left;
  transition: background .2s, border-color .2s, transform .1s;
}
.preset:active { transform: scale(.97); }
.preset.active { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 16%, transparent); }
.preset b { display: block; font-size: 14px; font-weight: 700; }
.preset small { display: block; color: var(--text-dim); font-size: 11px; margin-top: 2px; }

.field {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 4px; border-bottom: 1px solid var(--border); gap: 12px;
}
.field:last-child { border-bottom: none; }
.field-info { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.field-name { font-size: 15px; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.field-dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.field-hint { font-size: 12px; color: var(--text-faint); }

.stepper { display: flex; align-items: center; gap: 8px; flex: none; }
.step-btn {
  width: 44px; height: 44px; border-radius: 11px;
  background: var(--surface); border: 1px solid var(--border);
  color: var(--text); font-size: 22px; line-height: 1; cursor: pointer;
  display: grid; place-items: center;
  transition: background .15s, transform .08s;
}
.step-btn:hover { background: var(--surface-2); }
.step-btn:active { transform: scale(.9); }
.step-btn:disabled { opacity: .35; cursor: not-allowed; }
.step-val {
  min-width: 66px; text-align: center;
  font-size: 19px; font-weight: 700; font-variant-numeric: tabular-nums;
  background: none; border: none; color: var(--text);
  padding: 6px 2px;
}
.step-val:focus { outline: 2px solid var(--accent); border-radius: 8px; }

.summary {
  margin-top: 18px; padding: 14px 16px;
  border-radius: 14px; background: var(--surface);
  border: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center;
  font-size: 14px;
}
.summary b { font-size: 18px; font-variant-numeric: tabular-nums; }
.summary .sum-lab { color: var(--text-dim); }

/* ---------- Simple / Blocks mode toggle ---------- */
.mode-toggle {
  display: flex; gap: 4px; padding: 4px; margin: 0 0 18px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
}
.mode-btn {
  flex: 1; padding: 11px; border: none; background: none;
  color: var(--text-dim); font-size: 14px; font-weight: 700; letter-spacing: .01em;
  border-radius: 10px; cursor: pointer; min-height: 44px;
  transition: background .2s, color .2s;
}
.mode-btn.is-active {
  background: linear-gradient(145deg, var(--accent), var(--accent-2));
  color: #0a0a12;
  box-shadow: 0 6px 18px -6px var(--glow);
}

/* ---------- Presets panel ---------- */
.panel-hint {
  margin: 0 0 14px; padding: 10px 12px; border-radius: 10px;
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent);
  color: var(--text-dim); font-size: 12px; line-height: 1.45;
}
.save-preset {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; min-height: 48px; margin-bottom: 18px;
  border-radius: 13px; border: 1px dashed color-mix(in srgb, var(--accent) 55%, var(--border));
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  color: var(--text); font-weight: 700; font-size: 14px; cursor: pointer;
  transition: background .2s, transform .1s;
}
.save-preset span { font-size: 19px; color: var(--accent-2); }
.save-preset:hover { background: color-mix(in srgb, var(--accent) 20%, transparent); }
.save-preset:active { transform: scale(.98); }
.preset-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.preset { width: 100%; }
.preset-empty { color: var(--text-faint); font-size: 13px; padding: 6px 2px; }
.preset-custom { display: flex; align-items: stretch; padding: 0; overflow: hidden; }
.preset-load {
  flex: 1; min-width: 0; text-align: left; background: none; border: none;
  color: var(--text); padding: 12px 14px; cursor: pointer;
}
.preset-load b { display: block; font-size: 14px; font-weight: 700; }
.preset-load small { display: block; color: var(--text-dim); font-size: 12px; margin-top: 2px; font-variant-numeric: tabular-nums; }
.preset-del {
  flex: none; width: 48px; background: none; border: none;
  border-left: 1px solid var(--border); color: var(--text-faint);
  font-size: 15px; cursor: pointer; transition: color .15s, background .15s;
}
.preset-del:hover { color: #ff6b81; background: rgba(255, 107, 129, .12); }

/* ---------- Block palette ---------- */
.palette { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 18px; }
.palette-block {
  --blk: #888;
  flex: 1 1 auto; min-width: 82px;
  display: flex; align-items: center; justify-content: center; gap: 7px;
  padding: 12px 10px; min-height: 48px; border-radius: 13px;
  border: 1px solid color-mix(in srgb, var(--blk) 50%, transparent);
  background: color-mix(in srgb, var(--blk) 16%, transparent);
  color: var(--text); font-weight: 700; font-size: 14px;
  cursor: grab; touch-action: pan-y; user-select: none;
  transition: transform .1s var(--ease), background .2s;
}
.palette-block:hover { background: color-mix(in srgb, var(--blk) 26%, transparent); }
.palette-block:active { transform: scale(.95); cursor: grabbing; }
.pb-icon { color: var(--blk); font-size: 16px; }

.blocks-subhead { display: flex; align-items: center; justify-content: space-between; }
.text-btn {
  background: none; border: none; color: var(--text-faint);
  font-size: 12px; font-weight: 700; letter-spacing: .1em; text-transform: uppercase;
  cursor: pointer; padding: 8px 10px; border-radius: 8px; min-height: 40px;
  transition: color .2s, background .2s;
}
.text-btn:hover { color: var(--text-dim); background: var(--surface); }

/* ---------- Script (block canvas) ---------- */
.script {
  display: flex; flex-direction: column; gap: 8px; min-height: 64px;
  padding: 8px; border-radius: 14px;
  background: rgba(0, 0, 0, .18); border: 1px dashed var(--border);
}
.script-empty { color: var(--text-faint); font-size: 13px; text-align: center; padding: 18px 8px; }

.blk { --blk: #888; border-radius: 12px; }
.blk-head {
  display: flex; align-items: center; flex-wrap: wrap; gap: 8px;
  padding: 8px 10px; border-radius: 12px;
  background: color-mix(in srgb, var(--blk) 22%, #15131f);
  border: 1px solid color-mix(in srgb, var(--blk) 42%, transparent);
  border-left: 4px solid var(--blk);
}
.grip {
  background: none; border: none;
  cursor: grab; color: var(--text-dim); font-size: 17px; line-height: 1;
  padding: 6px 4px; touch-action: none; user-select: none;
}
.grip:active { cursor: grabbing; }
.grip:focus-visible { outline: 2px solid var(--accent-2); outline-offset: 2px; border-radius: 6px; }
.blk-name { font-weight: 700; font-size: 14px; min-width: 0; }
.blk-unit { color: var(--text-dim); font-size: 14px; font-weight: 700; }
.blk-controls { display: flex; align-items: center; gap: 6px; margin-left: auto; }
.blk-step { display: flex; align-items: center; gap: 5px; }
.mini {
  width: 44px; height: 44px; border-radius: 10px;
  border: 1px solid var(--border); background: rgba(255, 255, 255, .08);
  color: var(--text); font-size: 20px; line-height: 1; cursor: pointer;
  display: grid; place-items: center; transition: transform .08s, background .15s;
}
.mini:hover { background: rgba(255, 255, 255, .14); }
.mini:active { transform: scale(.9); }
.blk-val {
  width: 52px; text-align: center;
  background: rgba(0, 0, 0, .28); border: 1px solid var(--border); border-radius: 8px;
  color: var(--text); font-size: 14px; font-weight: 700; padding: 10px 2px;
  font-variant-numeric: tabular-nums;
}
.blk-val:focus { outline: 2px solid var(--blk); outline-offset: 1px; }
.blk-del {
  background: none; border: none; color: var(--text-faint);
  width: 44px; height: 44px; border-radius: 10px; font-size: 15px; cursor: pointer;
  display: grid; place-items: center; transition: color .15s, background .15s;
}
.blk-del:hover { color: #ff6b81; background: rgba(255, 107, 129, .12); }

/* Loop = C-shaped bracket */
.blk-loop > .blk-head { border-radius: 12px 12px 0 0; }
.loop-wrap { display: flex; }
.loop-arm { width: 12px; flex: none; background: var(--blk); }
.loop-body {
  flex: 1; display: flex; flex-direction: column; gap: 8px;
  padding: 8px; min-height: 24px;
  background: color-mix(in srgb, var(--blk) 9%, #100e1c);
  border-right: 1px solid color-mix(in srgb, var(--blk) 30%, transparent);
}
.loop-foot { height: 14px; border-radius: 0 0 12px 12px; background: var(--blk); }
.loop-empty {
  color: var(--text-faint); font-size: 12px; text-align: center; padding: 6px;
  border: 1px dashed color-mix(in srgb, var(--blk) 40%, transparent); border-radius: 8px;
}

/* Drag ghost + drop indicator */
.blk-ghost {
  --blk: #888; position: fixed; z-index: 9999; left: 0; top: 0;
  transform: translate(-50%, -50%) rotate(-2deg);
  padding: 11px 16px; border-radius: 12px;
  background: color-mix(in srgb, var(--blk) 32%, #15131f);
  border: 1px solid var(--blk); color: var(--text); font-weight: 700; font-size: 14px;
  pointer-events: none; box-shadow: 0 14px 34px rgba(0, 0, 0, .55);
}
.blk.dragging { opacity: .35; }
.drop-line {
  height: 4px; border-radius: 99px; margin: 1px 2px;
  background: var(--accent-2); box-shadow: 0 0 10px var(--glow);
}
body.is-dragging { cursor: grabbing; }
body.is-dragging, body.is-dragging * { user-select: none; }

/* ---------- Count-in flash ---------- */
.flash {
  position: fixed; inset: 0; z-index: 5; pointer-events: none;
  background: var(--glow); opacity: 0;
}
.flash.go { animation: flashGo .5s var(--ease); }
@keyframes flashGo { 0%{ opacity: .0;} 25%{ opacity: .5;} 100%{ opacity: 0;} }

/* Done celebration */
body[data-phase="done"] .phase-label { animation: pulse 1s var(--ease) infinite; }

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; }
  .ring-progress { transition: stroke-dashoffset .1s linear; }
}

/* Landscape phones: tighten */
@media (max-height: 480px) and (orientation: landscape) {
  :root { --ring-size: min(42vh, 60vw); }
  .stage { flex-direction: row; flex-wrap: wrap; gap: 18px; }
  /* time must track the height-based ring here, not viewport width, or it overflows */
  .time { font-size: clamp(34px, 13vh, 64px); }
  .next { font-size: 11px; }
}
