:root {
  color-scheme: light dark;

  --bg: #f2f2f2;
  --card: #ffffff;
  --border: #ddd;
  --accent: #d92b2b;
  --text: #111;
  --muted: #888;
  --row-a: #ffffff;
  --row-b: #f9f9f9;
  --tab-bg: #e8e8e8;
  --tab-active-bg: #fdeaea;
  --focus-bg: linear-gradient(135deg, #fdeaea, #fff);
  --cardio-bg: #fffbe6;
  --cardio-border: #f0d060;
  --cardio-text: #666;
  --cardio-title: #997700;

  /* Tokens the shared <sync-settings> component reads. Custom properties
     inherit through the shadow boundary, so defining them here is what themes
     the panel in this tool's palette rather than its neutral fallbacks. */
  --text-2: #444;
  --text-3: #888;
  --bg-input: #f7f7f7;
  --bg-hover: #ececec;
  --border-light: #e6e6e6;
  --accent-glow: rgba(217, 43, 43, 0.12);
  --danger: #d92b2b;
  --green: #1a9e5f;
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e0e0e;
    --card: #1a1a1a;
    --border: #2a2a2a;
    --accent: #ff3c3c;
    --text: #f0f0f0;
    --muted: #888;
    --row-a: #1a1a1a;
    --row-b: #141414;
    --tab-bg: #1a1a1a;
    --tab-active-bg: #1f1010;
    --focus-bg: linear-gradient(135deg, #1f1010, #1a1a1a);
    --cardio-bg: #1a1500;
    --cardio-border: #3a3000;
    --cardio-text: #aaa;
    --cardio-title: #cc9900;

    --text-2: #c4c4c4;
    --text-3: #888;
    --bg-input: #141414;
    --bg-hover: #222;
    --border-light: #222;
    --accent-glow: rgba(255, 60, 60, 0.14);
    --danger: #ff5c5c;
    --green: #35d68a;
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; user-select: none; -webkit-user-select: none; }

[hidden] {
  display: none !important;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'DM Sans', sans-serif;
  /* Use svh, not dvh: Firefox does not shrink the dynamic viewport for the
     browser toolbar, so 100dvh here left the body taller than the visible
     area and pushed the bottom of the tab buttons off-screen. svh is the
     toolbar-visible (smallest) viewport, so the pinned tabs stay visible. */
  min-height: 100vh;
  min-height: 100svh;
  max-width: 600px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
}

.bar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

/* Mask-based icons: load a shared SVG from /icons and paint it with
   currentColor so it inherits text colour and adapts to the theme. */
.icon-mask {
  display: inline-block;
  background-color: currentColor;
  -webkit-mask-repeat: no-repeat;
  mask-repeat: no-repeat;
  -webkit-mask-position: center;
  mask-position: center;
  -webkit-mask-size: contain;
  mask-size: contain;
}

.icon-settings {
  width: 1.25rem;
  height: 1.25rem;
  -webkit-mask-image: url(/icons/settings.svg);
  mask-image: url(/icons/settings.svg);
}

.manage-link {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  /* Roomy enough to hit on a phone — the icon itself is only 1.25rem. */
  width: 44px;
  height: 44px;
  margin: -10px;
  text-decoration: none;
  color: var(--muted);
  -webkit-tap-highlight-color: transparent;
  transition: color 0.15s;
}

.manage-link:active { opacity: 0.7; }

/* Red badge on the settings cog while sync is in an error state. */
.sync-error-dot {
  position: absolute;
  top: 9px;
  right: 9px;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--danger);
  box-shadow: 0 0 0 2px var(--bg);
}

.content {
  padding: 10px 16px 16px;
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.day-panel { display: none; }
.day-panel.active {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
}

/* Absorbs the leftover height on a short workout so the trailing controls
   (elapsed timer, reset button) hug the bottom instead of floating up. When
   the list is long enough to scroll, this collapses to nothing. */
.panel-spacer {
  flex: 1 1 auto;
  min-height: 0;
}

.focus-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
  padding: 12px 16px;
  background: var(--focus-bg);
  border-left: 3px solid var(--accent);
  border-radius: 0 10px 10px 0;
}

.focus-bar .focus-text {
  font-family: 'Bebas Neue', sans-serif;
  font-size: 26px;
  letter-spacing: 2px;
  color: var(--text);
}

.focus-bar .focus-sub {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 1px;
}

.exercise-list {
  display: flex;
  flex-direction: column;
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid var(--border);
  margin-bottom: 12px;
}

.exercise-row {
  display: grid;
  grid-template-columns: 1fr 52px 80px;
  align-items: center;
  padding: 14px 14px;
  background: var(--row-a);
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: opacity 0.2s;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.exercise-row:nth-child(even) { background: var(--row-b); }
.exercise-row:last-child { border-bottom: none; }

.exercise-row.done {
  opacity: 0.4;
}

.exercise-row.done .ex-name {
  text-decoration: line-through;
}

.ex-name {
  font-size: 16px;
  font-weight: 500;
  color: var(--text);
  line-height: 1.3;
  display: flex;
  align-items: center;
  gap: 8px;
}

.ex-name::before {
  content: '';
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid var(--border);
  flex-shrink: 0;
  transition: all 0.15s;
}

.exercise-row[data-completed-sets]:not([data-completed-sets="0"]):not(.done) .ex-name::before {
  border-color: var(--accent);
}

.exercise-row.done .ex-name::before {
  background: var(--accent);
  border-color: var(--accent);
  content: '✓';
  color: white;
  font-size: 11px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}

.ex-sets {
  font-family: 'Bebas Neue', sans-serif;
  font-size: 26px;
  color: var(--muted);
  text-align: center;
  letter-spacing: 1px;
}

.exercise-row[data-completed-sets]:not([data-completed-sets="0"]):not(.done) .ex-sets {
  color: var(--accent);
}

.exercise-row.done .ex-sets {
  color: var(--accent);
}

.ex-sets-label {
  display: block;
  font-family: 'DM Sans', sans-serif;
  font-size: 10px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 1px;
  font-weight: 500;
  line-height: 1;
  margin-top: -2px;
}

.ex-reps {
  font-family: 'Bebas Neue', sans-serif;
  font-size: 20px;
  color: var(--text);
  text-align: right;
  line-height: 1.1;
  letter-spacing: 0.5px;
}

.ex-reps-label {
  display: block;
  font-family: 'DM Sans', sans-serif;
  font-size: 10px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 1px;
  font-weight: 500;
}

.cardio-block {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 16px;
  background: var(--cardio-bg);
  border: 1.5px solid var(--cardio-border);
  border-radius: 12px;
}

.cardio-icon { font-size: 26px; flex-shrink: 0; }

.cardio-title {
  font-family: 'Bebas Neue', sans-serif;
  font-size: 17px;
  letter-spacing: 1.5px;
  color: var(--cardio-title);
  line-height: 1;
  margin-bottom: 3px;
}

.cardio-desc {
  font-size: 14px;
  color: var(--cardio-text);
  line-height: 1.4;
}

.col-headers {
  display: grid;
  grid-template-columns: 1fr 52px 80px;
  padding: 0 14px 6px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--muted);
}

.col-headers span:nth-child(2) { text-align: center; }
.col-headers span:nth-child(3) { text-align: right; }

/* Bottom tabs */
.tabs {
  display: flex;
  gap: 8px;
  padding: 10px 16px 20px;
  border-top: 1px solid var(--border);
  background: var(--bg);
}

.tab {
  flex: 1;
  background: var(--tab-bg);
  border: 1.5px solid var(--border);
  color: var(--muted);
  font-family: 'Bebas Neue', sans-serif;
  font-size: 28px;
  letter-spacing: 1.5px;
  padding: 14px 8px 10px;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.15s ease;
  text-align: center;
  -webkit-tap-highlight-color: transparent;
}

.tab .day-label {
  display: block;
  font-size: 11px;
  font-family: 'DM Sans', sans-serif;
  font-weight: 500;
  letter-spacing: 1px;
  text-transform: uppercase;
  margin-top: 1px;
  color: var(--muted);
}

.tab.active {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--tab-active-bg);
}

.tab.active .day-label { color: var(--accent); }

/* ── Rep-workout elapsed timer ── */
.rep-timer {
  position: sticky;
  bottom: 12px;
  z-index: 50;
  width: max-content;
  margin: 12px auto 0;
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 8px 18px;
  background: var(--card);
  border: 1.5px solid var(--accent);
  border-radius: 999px;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.35);
}

.rep-timer-seg {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.rep-timer-seg + .rep-timer-seg {
  padding-left: 18px;
  border-left: 1px solid var(--border);
}

.rep-timer-rest .rep-timer-rest-time {
  color: var(--text);
}

.rep-timer-label {
  font-family: 'DM Sans', sans-serif;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--muted);
}

.rep-timer-time,
.rep-timer-rest-time {
  font-family: 'Bebas Neue', sans-serif;
  font-size: 22px;
  letter-spacing: 1.5px;
  line-height: 1;
  color: var(--accent);
}

.rep-timer.done {
  border-color: #22c55e;
}

.rep-timer.done .rep-timer-time,
.rep-timer.done .rep-timer-rest-time {
  color: #22c55e;
}

/* ── Reset / clear-day control ── */
.day-reset {
  align-self: center;
  margin: 14px auto 4px;
  background: transparent;
  border: 1.5px solid var(--border);
  color: var(--muted);
  border-radius: 999px;
  padding: 9px 22px;
  font-family: 'DM Sans', sans-serif;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.5px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: border-color 0.2s, color 0.2s, background 0.2s, opacity 0.15s;
}

.day-reset:active { opacity: 0.7; }

.day-reset.complete {
  border-color: #22c55e;
  color: #22c55e;
  background: rgba(34, 197, 94, 0.10);
}

/* ── Circuit Training ── */

body.circuit-border::after {
  content: '';
  position: fixed;
  inset: 0;
  border: 4px solid var(--circuit-border-color, transparent);
  box-shadow: inset 0 0 40px var(--circuit-glow-color, transparent);
  pointer-events: none;
  z-index: 9999;
  transition: border-color 0.3s, box-shadow 0.3s;
}

body.circuit-border.circuit-phase-active  { --circuit-border-color: #22c55e; --circuit-glow-color: rgba(34, 197, 94, 0.15); }
body.circuit-border.circuit-phase-rest    { --circuit-border-color: #ef4444; --circuit-glow-color: rgba(239, 68, 68, 0.15); }
body.circuit-border.circuit-phase-longRest { --circuit-border-color: #f59e0b; --circuit-glow-color: rgba(245, 158, 11, 0.12); }

.circuit-settings {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  margin-bottom: 14px;
  padding: 10px 10px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.circuit-setting {
  text-align: center;
}

.circuit-setting label {
  display: block;
  font-size: 9px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--muted);
  margin-bottom: 4px;
}

.circuit-setting input {
  width: 100%;
  text-align: center;
  font-family: 'Bebas Neue', sans-serif;
  font-size: 22px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 6px 2px;
  -moz-appearance: textfield;
}

.circuit-setting input::-webkit-inner-spin-button,
.circuit-setting input::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.circuit-round {
  text-align: center;
  font-family: 'DM Sans', sans-serif;
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--muted);
  margin-bottom: 10px;
}

.circuit-panel .exercise-list {
  margin-bottom: 0;
}

.circuit-panel .exercise-row {
  grid-template-columns: 1fr;
  cursor: default;
  padding: 12px 14px;
  transition: opacity 0.2s, background 0.2s;
}

.circuit-panel .exercise-row .ex-name::before {
  width: 8px;
  height: 8px;
  border: none;
  background: var(--border);
  border-radius: 50%;
}

.circuit-panel .exercise-row.circuit-current {
  background: rgba(34, 197, 94, 0.12);
}

.circuit-panel .exercise-row.circuit-current .ex-name::before {
  background: #22c55e;
}

.circuit-panel .exercise-row.circuit-current .ex-name {
  color: var(--text);
  font-weight: 700;
}

.circuit-panel .exercise-row.circuit-done {
  opacity: 0.35;
}

.circuit-panel .exercise-row.circuit-done .ex-name {
  text-decoration: line-through;
}

.circuit-timer {
  text-align: center;
  padding: 24px 0 8px;
}

.circuit-timer-time {
  font-family: 'Bebas Neue', sans-serif;
  font-size: 80px;
  letter-spacing: 2px;
  line-height: 1;
  color: var(--text);
  transition: color 0.3s;
}

.circuit-timer-phase {
  font-family: 'DM Sans', sans-serif;
  font-size: 14px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 2px;
  color: var(--muted);
  margin-top: 4px;
}

.circuit-controls {
  display: flex;
  gap: 10px;
  padding: 14px 0;
}

.circuit-btn {
  flex: 1;
  border: none;
  border-radius: 12px;
  padding: 16px;
  font-family: 'Bebas Neue', sans-serif;
  font-size: 24px;
  letter-spacing: 2px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: opacity 0.15s;
}

.circuit-btn:active { opacity: 0.7; }

.circuit-btn-start {
  background: var(--accent);
  color: #fff;
}

.circuit-btn-reset {
  flex: 0 0 auto;
  background: transparent;
  border: 1.5px solid var(--border);
  color: var(--muted);
  padding: 16px 20px;
}

/* ── Edit-exercise dialog ── */
.exercise-row,
.cardio-block {
  -webkit-touch-callout: none;
}

.long-press-charging {
  animation: long-press-fill var(--hold-duration, 1000ms) linear forwards;
}

@keyframes long-press-fill {
  from { box-shadow: inset 0 0 0 0 var(--accent); }
  to { box-shadow: inset 0 0 0 3px var(--accent); }
}

.long-press-ready {
  animation: long-press-ready-pulse 0.35s ease-out;
}

@keyframes long-press-ready-pulse {
  0% { box-shadow: inset 0 0 0 3px var(--accent); }
  50% { box-shadow: inset 0 0 0 6px var(--accent); }
  100% { box-shadow: inset 0 0 0 3px var(--accent); }
}

dialog {
  padding: 0;
  border: none;
  border-radius: 16px;
  background: var(--card);
  color: var(--text);
  width: min(90vw, 400px);
  position: fixed;
  top: 50%;
  left: 50%;
  margin: 0;
  transform: translate(-50%, -50%);

  &::backdrop {
    background: rgba(0, 0, 0, 0.5);
  }
}

@media (max-width: 639px) {
  dialog {
    inset: 0;
    top: 0;
    left: 0;
    margin: 0;
    transform: none;
    width: 100vw;
    height: 100vh;
    height: 100svh;
    max-width: 100vw;
    max-height: 100vh;
    max-height: 100svh;
    border-radius: 0;
  }
}

.edit-exercise-form {
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 20px;
  height: 100%;
  user-select: text;

  & h2 {
    font-family: 'Bebas Neue', sans-serif;
    font-size: 24px;
    letter-spacing: 1.5px;
  }

  & label {
    display: flex;
    flex-direction: column;
    gap: 6px;
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--muted);
  }

  & input[type="text"],
  & select {
    font-family: 'DM Sans', sans-serif;
    font-size: 16px;
    padding: 10px 12px;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--text);
    user-select: text;
  }
}

.edit-exercise-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;

  & label {
    min-width: 0;
  }

  & input {
    width: 100%;
    min-width: 0;
  }
}

@media (max-width: 360px) {
  .edit-exercise-row {
    grid-template-columns: 1fr;
  }
}

.edit-exercise-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-top: auto;
  flex-wrap: wrap;
}

.edit-exercise-actions-right {
  display: flex;
  gap: 10px;
}

.btn-delete,
.btn-secondary,
.btn-primary {
  border: none;
  border-radius: 10px;
  padding: 12px 16px;
  font-family: 'DM Sans', sans-serif;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.btn-delete:active,
.btn-secondary:active,
.btn-primary:active {
  opacity: 0.7;
}

.btn-delete {
  background: transparent;
  border: 1.5px solid var(--accent);
  color: var(--accent);
}

.btn-secondary {
  background: transparent;
  border: 1.5px solid var(--border);
  color: var(--muted);
}

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