/* WODGen — Things 3-inspired: quiet surfaces, one blue, system type. */

:root {
  --bg: #f6f6f8;
  --surface: #ffffff;
  --text: #1c1c1e;
  --text-secondary: #86868b;
  --hairline: rgba(60, 60, 67, 0.14);
  --accent: #2f7cf6;
  --accent-tint: rgba(47, 124, 246, 0.1);
  --danger: #d64545;
  /* Sweatdiary-family gradient, anchored in Repsheet blue. */
  --grad-a: #14b8a6;
  --grad-b: var(--accent);
  --grad-c: #8b5cf6;
  --gradient: linear-gradient(95deg, var(--grad-a), var(--grad-b) 50%, var(--grad-c));
  --glow-a: rgba(20, 184, 166, 0.12);
  --glow-b: rgba(47, 124, 246, 0.1);
  --glow-c: rgba(139, 92, 246, 0.1);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17181a;
    --surface: #212226;
    --text: #f2f2f4;
    --text-secondary: #8e8e93;
    --hairline: rgba(255, 255, 255, 0.12);
    --accent: #4d94ff;
    --accent-tint: rgba(77, 148, 255, 0.14);
    --danger: #ff6b6b;
    --grad-a: #2dd4bf;
    --grad-c: #a78bfa;
    --glow-a: rgba(45, 212, 191, 0.08);
    --glow-b: rgba(77, 148, 255, 0.08);
    --glow-c: rgba(167, 139, 250, 0.08);
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; }

[hidden] { display: none !important; }

/* min-height, not height: at `height: 100%` the body box stops at the viewport
   while the config column keeps going (100dvh + safe-area padding overflow a
   phone screen), leaving the document taller than the element painting it. */
html, body { min-height: 100%; }

/* One background, on the root element — soft ambient glows, echoing the
   Sweatdiary hero wash. The root's background *is* the canvas, so it covers the
   whole screen by definition: past the last element, under the home indicator,
   into the overscroll. That matters because iOS sizes a `position: fixed` box
   to the layout viewport, which is *shorter* than the screen (see .workout), so
   any second full-screen layer with its own background ends in a hard line
   partway down. Keep this the only page background and there is nothing to
   misalign against.

   Deliberately no `background-attachment: fixed`, which iOS doesn't implement,
   and no viewport-unit `background-size`, which draws its own seam one screen
   down on a page that scrolls. Unattached, the glows are laid out over the
   whole canvas: they stretch a little on a long page, and nowhere do they
   stop. */
html {
  -webkit-text-size-adjust: 100%;
  background-color: var(--bg);
  background-image:
    radial-gradient(50rem 30rem at 15% -5%, var(--glow-a), transparent 60%),
    radial-gradient(45rem 28rem at 90% 10%, var(--glow-b), transparent 60%),
    radial-gradient(55rem 32rem at 40% 110%, var(--glow-c), transparent 60%);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

button {
  touch-action: manipulation;       /* no double-tap zoom delay on ↻ etc. */
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}

/* ---------- Config view ---------- */

.config {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh; /* stable against iOS toolbar collapse */
  max-width: 40rem;
  margin: 0 auto;
  padding: clamp(2.5rem, 9vh, 6rem) 1.5rem 0;
  /* Clear the Dynamic Island when running full-screen; the sticky generate
     bar carries the bottom safe-area itself. */
  padding-top: calc(clamp(2.5rem, 9vh, 6rem) + env(safe-area-inset-top, 0px));
  text-align: center;
}

.config-header { margin-bottom: clamp(2.5rem, 6vh, 4rem); }

/* Mirrors the workout view's eyebrow (.w-title) + headline (.w-format) pairing. */
.eyebrow {
  font-size: 0.85rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.18em;
  color: var(--text-secondary);
}

.config-header h1 {
  font-size: clamp(2.4rem, 6vw, 3.4rem);
  font-weight: 800;
  letter-spacing: -0.02em;
  margin-top: 0.3rem;
  display: inline-block; /* gradient spans the word itself, not the row */
  color: var(--accent); /* fallback if background-clip: text is unsupported */
  background: var(--gradient);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.group { margin-bottom: 2.5rem; }

.group-label {
  font-size: 0.85rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.18em;
  color: var(--text-secondary);
  margin-bottom: 1rem;
}

.hint {
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-top: 0.8rem;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.6rem;
}

.chip {
  font: inherit;
  font-size: 0.98rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  padding: 0.55rem 1.05rem;
  border-radius: 12px;
  border: 1px solid var(--hairline);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease,
    color 0.12s ease, transform 0.12s ease;
}

/* hover: hover — touch screens emulate a sticky hover on tap, which would
   leave an accent border on an untapped chip. */
@media (hover: hover) {
  .chip:hover { border-color: var(--accent); color: var(--accent); }
}

.chip:active { transform: scale(0.97); }

.chip[aria-pressed="true"],
.chip[aria-checked="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.chip:focus-visible,
.generate:focus-visible,
.customize:focus-visible,
.sheet-btn:focus-visible,
.chrome-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Always-visible CTA: sticks to the bottom of the viewport while the form
   scrolls, so the button (and its loading state) never leaves the screen. */
.generate-bar {
  /* One knob for the fade: it is both the top padding and the distance the
     gradient takes to reach --bg, so the two can't drift apart. Sized as a
     length rather than a % of the bar, which is what left the old 22% stop
     finishing *behind* the opaque Customize button — chips scrolling under met
     its top edge still half-visible, as a hard rectangular seam. */
  --fade: 3.5rem;
  position: sticky;
  bottom: 0;
  /* Full-bleed to the viewport edges. Inset, the bar's flat --bg fill sits on
     top of the body's ambient glows and its left/right edges read as a stray
     rectangle behind the button. Bleeding it out leaves only the top fade. */
  margin-inline: calc(50% - 50vw);
  padding: var(--fade) calc(50vw - 50%) calc(1rem + env(safe-area-inset-bottom, 0px));
  /* Fade completes exactly at the Customize button's top edge — below that the
     bar is solid, so scrolled content never shows through the two buttons. */
  background: linear-gradient(to bottom, transparent, var(--bg) var(--fade));
}

/* The fade is paid for out of the last group's bottom margin rather than added
   to it: the gap above Customize stays what it was, but the whole of it now
   dissolves instead of only the last few pixels. */
.config > .group:last-of-type { margin-bottom: 1rem; }

.generate {
  font: inherit;
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  width: 100%;
  max-width: 24rem;
  padding: 0.95rem;
  margin-top: 0.5rem;
  border: none;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  transition: opacity 0.12s ease, transform 0.12s ease;
}

@media (hover: hover) {
  .generate:hover:not(:disabled) { opacity: 0.9; }
}
.generate:active:not(:disabled) { transform: scale(0.99); }

.generate:disabled {
  opacity: 0.55;
  cursor: default;
}

.generate.loading {
  animation: pulse 1.4s ease-in-out infinite;
}

@keyframes pulse {
  50% { opacity: 0.4; }
}

/* Secondary action above the CTA: same footprint, none of the weight. */
.customize {
  font: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  display: block;
  width: 100%;
  max-width: 24rem;
  margin: 0 auto 0.6rem;
  padding: 0.7rem;
  border-radius: 12px;
  border: 1px solid var(--hairline);
  /* Opaque, not transparent: it sits in the generate bar's fade zone, where a
     see-through button would let the last row of chips scroll through it. */
  background: var(--bg);
  color: var(--text-secondary);
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease, color 0.12s ease,
    transform 0.12s ease;
}

/* A saved note keeps shaping every workout, so the button stays lit. The tint
   is translucent, so it's layered over an opaque base rather than replacing it. */
.customize[data-set="true"] {
  background: linear-gradient(var(--accent-tint), var(--accent-tint)), var(--bg);
  border-color: var(--accent);
  color: var(--accent);
}

@media (hover: hover) {
  .customize:hover { border-color: var(--accent); color: var(--accent); }
}

.customize:active { transform: scale(0.99); }

/* ---------- Customize sheet ---------- */

/* Left at the UA's centered position on every screen size: a bottom sheet
   would sit under the iOS keyboard, while a centered dialog gets scrolled
   into view above it. */
.sheet {
  /* The `* { margin: 0 }` reset wipes the UA's `margin: auto` on dialog, which
     is what centres a modal — put it back or the sheet pins to the top-left. */
  margin: auto;
  width: min(30rem, calc(100vw - 2rem));
  max-height: 80dvh;
  padding: 0;
  border: none;
  border-radius: 18px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.18);
}

/* Matches the generation overlay's blurred wash. */
.sheet::backdrop {
  background: color-mix(in srgb, var(--bg) 60%, transparent);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}

.sheet-inner {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  padding: 1.5rem;
  text-align: left;
}

.sheet-title {
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.sheet-inner .hint { margin-top: -0.5rem; }

/* Model choice inside the Customize sheet: a quieter, left-aligned version of
   the config page's chip groups. */
.sheet-label {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.18em;
  color: var(--text-secondary);
  margin-bottom: 0.5rem;
}

.sheet-chips { justify-content: flex-start; }

.sheet-inner .sheet-section .hint { margin-top: 0.5rem; }

.sheet-input {
  font: inherit;
  /* Exactly 16px: anything smaller makes iOS Safari zoom in on focus. */
  font-size: 1rem;
  width: 100%;
  min-height: 7rem;
  padding: 0.75rem;
  border-radius: 12px;
  border: 1px solid var(--hairline);
  background: var(--bg);
  color: var(--text);
  resize: vertical;
}

.sheet-input:focus-visible {
  outline: none;
  border-color: var(--accent);
}

.sheet-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.6rem;
}

.sheet-btn {
  font: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  padding: 0.6rem 1.2rem;
  border-radius: 12px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: opacity 0.12s ease, border-color 0.12s ease, color 0.12s ease;
}

.sheet-btn.ghost {
  background: transparent;
  border-color: var(--hairline);
  color: var(--text-secondary);
}

.sheet-btn.primary {
  background: var(--accent);
  color: #fff;
}

@media (hover: hover) {
  .sheet-btn.ghost:hover { border-color: var(--danger); color: var(--danger); }
  .sheet-btn.primary:hover { opacity: 0.9; }
}

/* Minimal legal footer: quiet, out of the visual hierarchy. Sits at the very
   bottom of the viewport-filling config column, not under the button. Config
   view only: the workout poster carries nothing but the workout. */
.footer {
  margin-top: auto;
  /* The generate bar's fill stops at its own bottom edge; the footer picks it
     up and carries it to the end of the page, safe area included, so the two
     read as one surface instead of a band with a seam across it. Full-bleed
     for the same reason the bar is. */
  margin-inline: calc(50% - 50vw);
  padding: 0.75rem calc(50vw - 50%) calc(0.75rem + env(safe-area-inset-bottom, 0px));
  background: var(--bg);
  font-size: 0.75rem;
}

.footer a {
  color: var(--text-secondary);
  text-decoration: none;
}

@media (hover: hover) {
  .footer a:hover { text-decoration: underline; }
}

/* ---------- Generation overlay ---------- */

.overlay {
  position: fixed;
  inset: 0;
  /* Same short layout viewport as .workout, but this one has to obscure what's
     behind it, so it can't go transparent — reach past the bottom edge instead
     and pad the content box back to centre. */
  bottom: -20vh;
  padding-bottom: 20vh;
  z-index: 10;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.1rem;
  background: color-mix(in srgb, var(--bg) 75%, transparent);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
}

.spinner {
  width: 2.4rem;
  height: 2.4rem;
  border-radius: 50%;
  border: 3px solid var(--accent-tint);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.overlay-text {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text-secondary);
  opacity: 1;
  transition: opacity 0.25s ease;
}

.overlay-text.swap { opacity: 0; }

.overlay-dismiss {
  display: none;
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.overlay.error .spinner { display: none; }

.overlay.error .overlay-text {
  color: var(--danger);
  font-weight: 500;
  max-width: 26rem;
  padding: 0 1.5rem;
  text-align: center;
}

.overlay.error .overlay-dismiss { display: block; }

/* ---------- Workout view ---------- */

.workout {
  position: fixed;
  inset: 0;
  /* No background of its own — the canvas shows through. iOS sizes a fixed box
     to the layout viewport, which stops short of the bottom of the screen (844
     of 912 CSS px in the installed app; above the toolbar in Safari). A second
     opaque copy of the glows therefore ended in a hard line, with the canvas's
     plain --bg visible below it. The canvas is the one surface that always
     covers the whole screen, and the config view is hidden while this is open,
     so there is nothing behind here to hide. */
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow-y: auto;
  animation: appear 0.25s ease-out;
}

@keyframes appear {
  from { opacity: 0; transform: scale(0.985); }
  to   { opacity: 1; transform: scale(1); }
}

.workout-chrome {
  position: fixed;
  /* Below the safe area, not at it — clears iOS's status-bar glass effect. */
  top: calc(env(safe-area-inset-top, 0px) + 1rem);
  left: max(1rem, env(safe-area-inset-left, 0px));
  right: max(1rem, env(safe-area-inset-right, 0px));
  /* Hug the content column on wide screens instead of the viewport corners —
     keeps mouse travel short on desktop. Matches .workout-inner's width. */
  max-width: 72rem;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
  z-index: 2;
  pointer-events: none; /* the bar spans the width; only its buttons are targets */
}

.workout-chrome > * { pointer-events: auto; }

.chrome-group { display: flex; gap: 0.5rem; }

.chrome-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.75rem;
  height: 2.75rem;
  border-radius: 50%;
  border: 1px solid var(--hairline);
  background: var(--surface);
  color: var(--text-secondary);
  cursor: pointer;
  transition: opacity 0.15s ease, color 0.15s ease;
}

.chrome-btn svg { width: 1.15rem; height: 1.15rem; }
/* display: inline-flex above would otherwise beat the hidden attribute. */
.chrome-btn[hidden] { display: none; }

/* Copy button: swap to an accent checkmark for a moment after copying. */
.chrome-btn .icon-check { display: none; }
.chrome-btn.copied { border-color: var(--accent); color: var(--accent); }
.chrome-btn.copied .icon-copy { display: none; }
.chrome-btn.copied .icon-check { display: block; }

/* Wake-lock toggle: a labeled pill, tinted while the screen is kept awake. */
.wake-pill {
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  white-space: nowrap;
  padding: 0.55rem 1rem;
  border-radius: 999px;
  border: 1px solid var(--hairline);
  background: var(--surface);
  color: var(--text-secondary);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease,
    opacity 0.15s ease;
}

.wake-pill[aria-pressed="true"] {
  background: var(--accent-tint);
  border-color: var(--accent);
  color: var(--accent);
}

/* Desktop only: chrome fades until hovered. Touch screens have no hover, so
   controls stay fully visible there. */
@media (hover: hover) {
  .chrome-btn, .wake-pill { opacity: 0.7; }
  .chrome-btn:hover { opacity: 1; color: var(--text); }
  .chrome-btn.copied { opacity: 1; color: var(--accent); }
  .wake-pill:hover, .wake-pill[aria-pressed="true"] { opacity: 1; }
}

.workout-inner {
  width: 100%;
  max-width: 72rem;
  padding: 4rem clamp(1.5rem, 5vw, 4rem);
  padding-top: calc(4rem + env(safe-area-inset-top, 0px));
  padding-bottom: calc(4rem + env(safe-area-inset-bottom, 0px));
  text-align: center;
  margin: auto;
}

.w-format {
  font-size: clamp(1.8rem, 5.2vmin, 4rem);
  font-weight: 800;
  letter-spacing: -0.02em;
}

.w-scheme {
  font-size: clamp(1.2rem, 3.2vmin, 2.4rem);
  font-weight: 600;
  color: var(--accent);
  margin-top: 0.3rem;
}

.w-scheme:empty { display: none; }

.w-movements {
  list-style: none;
  margin: clamp(1.5rem, 4vmin, 3rem) auto 0;
  display: table;
  border-collapse: collapse;
}

.w-movements li {
  display: table-row;
}

.w-movements .reps,
.w-movements .move {
  display: table-cell;
  padding: clamp(0.3rem, 1.2vmin, 0.8rem) 0;
  vertical-align: baseline;
  /* --fit is set by JS: shrink-to-fit beats line breaks, on any screen. */
  font-size: calc(clamp(1.5rem, var(--move-size, 5.5vmin), 5rem) * var(--fit, 1));
  font-weight: 750;
  letter-spacing: -0.015em;
  line-height: 1.15;
  white-space: nowrap;
}

.w-movements .reps {
  color: var(--accent);
  text-align: right;
  padding-right: clamp(0.8rem, 2vmin, 1.5rem);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.w-movements .move { text-align: left; }

.w-movements .load {
  font-size: 0.55em;
  font-weight: 500;
  color: var(--text-secondary);
  white-space: nowrap;
  margin-left: 0.5em;
}

.sheet-notes {
  font-size: 1.05rem;
  line-height: 1.5;
}

/* Touch devices: Apple's 44pt minimum target size. */
@media (pointer: coarse) {
  .chip { padding: 0.65rem 1.1rem; }
  .wake-pill { padding: 0.7rem 1.1rem; }
}

/* Home-Screen app: drop the chrome well below the Liquid Glass zone that iOS
   draws under the status bar, and lift it off the background. */
@media (display-mode: standalone) {
  .workout-chrome { top: calc(env(safe-area-inset-top, 0px) + 2rem); }
  .chrome-btn, .wake-pill { box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08); }
}

/* Narrow screens (phones): tighter padding and width-driven type so the
   longest movement row never overflows the viewport. */
@media (max-width: 600px) {
  .workout-inner { padding-left: 1rem; padding-right: 1rem; }
  .w-movements .reps,
  .w-movements .move {
    font-size: calc(clamp(1.2rem, 5vw, 2.2rem) * var(--fit, 1));
  }
  .w-movements .reps { padding-right: 0.8rem; }
}

/* Phones: four round buttons plus the lockscreen pill only just fit — trim the
   pill and the gaps (never the 44pt buttons) so the bar keeps its 1rem margin. */
@media (max-width: 430px) {
  .workout-chrome, .chrome-group { gap: 0.35rem; }
  .wake-pill { font-size: 0.8rem; padding-left: 0.85rem; padding-right: 0.85rem; }
}

/* The pill's short label: only where the full one would push the bar off-screen. */
.wake-short { display: none; }
@media (max-width: 380px) {
  .wake-long { display: none; }
  .wake-short { display: inline; }
}
@media (max-width: 350px) {
  .workout-chrome, .chrome-group { gap: 0.25rem; }
  .wake-pill { padding-left: 0.7rem; padding-right: 0.7rem; }
}

/* Phones: tighter vertical rhythm so the whole form fits one viewport with
   the sticky Generate bar. */
@media (max-width: 640px) {
  .config {
    padding-top: calc(clamp(1.5rem, 4vh, 3rem) + env(safe-area-inset-top, 0px));
  }
  .config-header { margin-bottom: 1.25rem; }
  .config-header h1 { font-size: 2rem; }
  .group { margin-bottom: 1.4rem; }
  .group-label { margin-bottom: 0.6rem; }
  .chips { gap: 0.5rem; }
  .chip { font-size: 0.9rem; padding: 0.6rem 0.95rem; }
  .hint { font-size: 0.75rem; margin-top: 0.5rem; }
  /* Shorter fade, matching the tighter phone rhythm — same trade as above. */
  .generate-bar { --fade: 2.6rem; }
  .config > .group:last-of-type { margin-bottom: 0.6rem; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
