:root {
  color-scheme: dark;
  --background: #222326;
  --surface: #2e2f33;
  --control: #5c5e66;
  --shadow: #4a4b52;
  --text: #fff;
  --muted: #b1b2b7;
  --accent: #35bd46;
  --line: #45474c;
  --app-height: var(--tg-viewport-stable-height, 100dvh);
  --bottom-safe-area: max(env(safe-area-inset-bottom, 0px), var(--tg-content-safe-area-inset-bottom, 0px));
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

#loading-overlay {
  position: fixed;
  z-index: 10;
  inset: 0 0 auto;
  width: 100%;
  height: var(--app-height);
  max-height: 100dvh;
  display: grid;
  place-items: center;
  margin: 0;
  padding: 0;
  border: 0;
  background: rgb(0 0 0 / 38%);
  overscroll-behavior: contain;
  touch-action: none;
}

.loading-spinner {
  width: 40px;
  aspect-ratio: 1;
  border: 4px solid rgb(255 255 255 / 25%);
  border-top-color: var(--text);
  border-radius: 50%;
  animation: loading-spin 0.8s linear infinite;
}

@keyframes loading-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  .loading-spinner { animation: none; }
}

#load-error {
  position: fixed;
  z-index: 11;
  inset: 16px 12px auto;
  width: min(calc(100% - 24px), 552px);
  margin: 0 auto;
  padding: 12px 16px;
  border: 0;
  border-radius: 12px;
  color: var(--text);
  background: var(--surface);
  box-shadow: 0 4px 16px rgb(0 0 0 / 35%);
  pointer-events: none;
}

body {
  margin: 0;
  background: #000;
  color: var(--text);
  font: 1rem/1.35 "Overpass Mono", ui-monospace, monospace;
  overflow-wrap: anywhere;
}

.trainer {
  width: min(100%, 600px);
  min-height: var(--app-height);
  margin-inline: auto;
  background: var(--background);
}

.telegram-welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: var(--app-height);
  padding-block-start: clamp(20px, 12dvh, 96px);
  background: var(--accent);
  color: #202326;
  text-align: center;
}

.telegram-welcome-copy {
  display: grid;
  justify-items: center;
  gap: 24px;
  padding: 24px;
}

.telegram-welcome h1 {
  max-width: 26ch;
  margin: 0;
  font-size: clamp(1.125rem, 4.5vw, 1.5rem);
  font-weight: 500;
}

.telegram-welcome .telegram-welcome-link {
  display: grid;
  place-items: center;
  min-height: 60px;
  padding: 12px 24px;
  background: #fff;
  color: #101010;
  box-shadow: 0 8px 0 #ccc;
  text-decoration: none;
}

.telegram-welcome .telegram-welcome-link:hover { background: #f2f2f2; }
.telegram-welcome .telegram-welcome-link:focus-visible { outline-color: #202326; }

.telegram-welcome-image {
  width: min(100%, 512px, 52dvh);
  height: auto;
  margin-block-start: auto;
}

#home, #profile, #profile-page { padding-block-end: 48px; }

#preview,
#workout-active,
.workout-result {
  display: flex;
  flex-direction: column;
  min-height: var(--app-height);
}

.screen-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 24px 24px 0;
}

.screen-header > div { flex: 1; min-width: 0; }

.screen-header h1 {
  margin: 4px 0 8px;
  font-size: clamp(1.5rem, 5.4vw, 2rem);
  font-weight: 500;
  line-height: 1.2;
}

.screen-header p,
.trainer-section p {
  margin: 4px 0 0;
}

.muted { color: var(--muted); }
.status { margin: 16px 24px; font-size: 0.875rem; }
.status:empty { margin: 0; }

.raised {
  appearance: none;
  border: 0;
  border-radius: 16px;
  background: var(--control);
  color: var(--text);
  box-shadow: 0 8px 0 var(--shadow);
  font: inherit;
  cursor: pointer;
}

.raised:disabled { cursor: default; }
.raised:not(:disabled):hover { background: #575961; }

.raised[aria-pressed="true"] {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
  background: var(--surface);
  box-shadow: none;
  transform: translateY(6px);
}

:is(a, summary, input:not([type="radio"])):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

button:focus-visible {
  filter: brightness(1.22);
  text-decoration: underline;
  text-underline-offset: 0.25em;
}

button:not([aria-pressed="true"]):focus-visible { outline: none; }

.icon-button {
  display: grid;
  place-items: center;
  flex: 0 0 50px;
  width: 50px;
  height: 44px;
}

.training-switcher {
  --tab-depth: 8px;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  position: relative;
  isolation: isolate;
  margin: 24px 12px 0;
  padding-bottom: var(--tab-depth);
  border-radius: 16px;
}

.training-switcher::before {
  content: "";
  position: absolute;
  z-index: -1;
  inset: var(--tab-depth) 0 0;
  border-radius: 16px;
  background: var(--surface);
}

.training-switcher button[aria-pressed="true"] { transform: translateY(var(--tab-depth)); }

.training-switcher button {
  min-height: 40px;
  padding: 4px;
  font-size: 0.875rem;
}

.plan-grid,
.history-list {
  list-style: none;
  padding: 0;
}

.plan-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 20px 12px;
  margin: 12px 12px 0;
}

.plan-card {
  display: block;
  position: relative;
  width: 100%;
  height: 212px;
  overflow: hidden;
  padding: 12px;
  text-align: left;
}

.plan-card span {
  position: absolute;
  inset: 12px 12px auto;
  z-index: 1;
  font-size: clamp(1rem, 4vw, 1.3125rem);
  line-height: 1.25;
}

.plan-card img {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 80%;
  max-height: 160px;
  object-fit: contain;
  object-position: right bottom;
  pointer-events: none;
}

.trainer-section {
  margin: 24px 24px 0;
}

.profile-header { padding-top: 30px; }
.profile-header h1 { font-size: 2rem; }
.profile-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 20px 12px;
  margin: 22px 12px 8px;
}

@media (min-width: 450px) {
  .profile-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

.profile-card {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 8px;
  min-width: 0;
  min-height: 144px;
  padding: 12px;
  font-size: clamp(1rem, 5.25vw, 1.3125rem);
  text-align: start;
}

.profile-card .muted { font-size: 0.875em; }

.profile-links {
  display: grid;
  gap: 20px;
  margin: 40px 12px 32px;
}

.profile-links a, .profile-list button {
  display: block;
  width: 100%;
  padding: 12px;
  text-align: start;
}

.profile-links a { text-decoration: none; }
.profile-links a:focus-visible { text-decoration: underline; }
.profile-list {
  display: grid;
  gap: 12px;
  list-style: none;
  margin: 16px 12px;
  padding: 0;
}

.profile-list li { display: grid; gap: 4px; padding: 4px 12px 12px; border-bottom: 1px solid var(--line); }
.profile-list li:has(button) { padding: 0 0 8px; border: 0; }
.profile-search { display: block; margin: 16px 24px; }
.profile-search input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  color: var(--text);
  background: var(--surface);
  font: inherit;
}

.preview-header .preview-title { font-size: 1.5rem; }
.preview-count { font-size: 0.875rem; }

.preview-list {
  list-style: none;
  display: grid;
  gap: 12px;
  margin: 12px 0 0;
  padding: 0 12px;
}

.exercise_line {
  min-height: 48px;
  padding: 0 12px;
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 0.875rem;
}

.exercise_index {
  display: grid;
  place-items: center;
  flex: 0 0 48px;
  height: 36px;
  border-radius: 8px;
  background: var(--shadow);
}

.exersice_body {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.series-menu {
  width: min(calc(100% - 48px), 552px);
  max-height: calc(100% - 48px);
  padding: 0;
  border: 0;
  border-radius: 36px;
  background: var(--background);
  color: var(--text);
}

.series-menu::backdrop { background: rgb(0 0 0 / 75%); }

.series-menu h2 {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 500;
}

.series-menu p { font-size: 0.875rem; }

.change_training_buttons {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 104px), 1fr));
  gap: 16px 8px;
  padding: 24px 24px 32px;
}

.change_training {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-height: 48px;
  padding: 8px 4px;
}

.change_training[aria-current] { outline: 2px solid var(--accent); outline-offset: -2px; }

.last_training_date {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 0.75rem;
}

.trainer-section {
  padding-block-start: 16px;
  border-block-start: 1px solid var(--line);
}

.trainer-section h2 { margin: 0; font-size: 1.125rem; }

.exercise-groups,
.history-list { margin: 12px 0 0; }

.exercise-groups { padding-inline-start: 26px; }

.exercise-groups ul { list-style: none; padding: 0; }

.exercise-groups li li,
.history-list li {
  display: grid;
  gap: 2px;
  padding-block: 8px;
}

.exercise-groups > li + li,
.history-list li + li { border-block-start: 1px solid var(--line); }

.exercise-groups .muted,
.history-list .muted { font-size: 0.875rem; }

@media (min-width: 450px) {
  .plan-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

@media (min-width: 624px) {
  .trainer {
    min-height: calc(100vh - 24px);
    margin-block: 12px;
    border-radius: 24px;
  }
}

.primary-button {
  min-height: 48px;
  padding: 0 18px;
  background: var(--accent);
  color: var(--text);
  box-shadow: 0 8px 0 #2a9738;
  font-weight: 600;
}

.primary-button:not(:disabled):hover { background: #32b443; }
.primary-button:disabled { opacity: 0.65; }
.warning-button { color: #ffb08b; }

.workout-footer {
  display: grid;
  gap: 10px;
  position: sticky;
  z-index: 2;
  bottom: max(0px, calc(100dvh - var(--app-height)));
  margin-block-start: auto;
  padding: 16px 24px calc(40px + var(--bottom-safe-area));
  background: var(--background);
}

.workout-footer p { margin: 0; font-size: 0.875rem; }
.workout-footer a { color: var(--accent); }

.exercise_index.current { border: 2px solid var(--accent); background: transparent; }
.exercise_index.past { background: var(--accent); color: #142117; }
.exercise_index.future + .exersice_body { color: var(--muted); }

#workout-active {
  --workout-gap: clamp(12px, calc((var(--app-height) - 520px) * 0.12), 24px);
}

#workout-active .workout-footer {
  position: static;
  padding-block: min(16px, var(--workout-gap)) calc(clamp(24px, calc(var(--app-height) - 550px), 40px) + var(--bottom-safe-area));
}

.workout-media {
  position: relative;
  flex: 1 0 150px;
  max-height: 444px;
  margin: 12px 12px 0;
  overflow: hidden;
  border-radius: 20px;
  background: #fff;
}

#exercise-media { position: absolute; inset: 0; }
#exercise-media img,
#exercise-media video {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.media-button {
  position: absolute;
  top: 12px;
  padding: 8px 12px;
  border: 0;
  border-radius: 8px;
  color: #fff;
  background: rgb(0 0 0 / 50%);
  backdrop-filter: blur(4px);
  font: inherit;
  font-size: 0.875rem;
  cursor: pointer;
}
.media-button:hover { background: rgb(0 0 0 / 55%); }
.workout-plan { left: 12px; }
.workout-technique {
  right: 12px;
  display: grid;
  place-items: center;
  width: 40px;
  height: 32px;
  padding: 0;
}
.workout-technique img { width: 24px; height: 24px; }

.workout-mini-history {
  inset: auto auto 12px 12px;
  display: grid;
  gap: 4px;
  max-height: 48%;
  max-width: calc(100% - 24px);
  overflow: auto;
  padding: 8px 12px 8px 8px;
  text-align: start;
}
.workout-mini-history > span {
  display: flex;
  align-items: center;
  gap: 8px;
  color: rgb(255 255 255 / 65%);
}
.set-index {
  display: grid;
  place-items: center;
  width: 24px;
  height: 21px;
  flex-shrink: 0;
  border-radius: 2px;
  background: rgb(255 255 255 / 12%);
}
.current-set .set-index { border: 2px solid var(--accent); color: #fff; background: transparent; }
.past-set .set-index { background: var(--accent); color: #fff; }
.workout-mini-history .past-set { color: #fff; }

.workout-heading { padding-top: var(--workout-gap); }
.workout-heading h1 { font-size: 1.3125rem; }
.workout-heading p { font-size: 0.875rem; }

.workout-controls {
  --switch-width: 120px;
  --step-width: 50px;
  display: grid;
  grid-template-columns: var(--switch-width) minmax(0, 1fr);
  grid-template-rows: repeat(3, 50px);
  grid-template-areas: "rest weight" "weight-unit weight" "reps-unit reps";
  gap: 8px 12px;
  padding: var(--workout-gap) 24px 8px;
}

.workout-rest,
.workout-units {
  align-self: start;
  min-width: 0;
  min-height: 40px;
  border: 0;
  border-radius: 16px;
  margin: 0;
  padding: 0;
  background: var(--surface);
  font-size: 0.875rem;
}
.workout-rest {
  grid-area: rest;
  display: grid;
  place-items: center;
  background: #3f4046;
  color: var(--muted);
}
.weight-units { grid-area: weight-unit; }
.reps-units { grid-area: reps-unit; }
.workout-units { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.workout-units label { position: relative; }
.workout-units label span {
  display: grid;
  place-items: center;
  height: 40px;
  border-radius: 16px;
  background: var(--control);
  box-shadow: 0 8px 0 var(--shadow);
  cursor: pointer;
}
.workout-units input:checked + span {
  border: 2px solid var(--accent);
  background: var(--surface);
  box-shadow: none;
  transform: translateY(6px);
}
.workout-units input:focus-visible + span {
  filter: brightness(1.22);
  text-decoration: underline;
  text-underline-offset: 0.25em;
}
.workout-units input,
.workout-sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.workout-stepper {
  display: grid;
  grid-template-columns: var(--step-width) minmax(0, 1fr) var(--step-width);
  align-self: start;
  min-width: 0;
  border-radius: 16px;
  background: var(--surface);
}
.weight-stepper { grid-area: weight; height: 92px; }
.reps-stepper { grid-area: reps; height: 40px; }
.workout-stepper button {
  display: grid;
  place-items: center;
  height: 100%;
  min-height: 0;
  padding: 0;
  touch-action: none;
}
.workout-value {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-width: 0;
  padding: 0 3px;
  font-size: 0.875rem;
  white-space: nowrap;
}
.weight-stepper .workout-value { font-size: clamp(0.9375rem, 4vw, 1.125rem); }
.workout-value input {
  appearance: textfield;
  width: 6ch;
  max-width: 100%;
  min-width: 0;
  padding: 0;
  border: 0;
  border-bottom: 2px dotted #fff;
  border-radius: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  text-align: center;
}
@supports (field-sizing: content) {
  .workout-value input { field-sizing: content; width: auto; min-width: 2ch; }
}
.workout-stepper img { pointer-events: none; }
.workout-value input::-webkit-inner-spin-button,
.workout-value input::-webkit-outer-spin-button { appearance: none; margin: 0; }
#workout-submit {
  min-height: 60px;
  font-size: 1.125rem;
}
@media (max-width: 360px) {
  .workout-controls { --switch-width: 90px; --step-width: 40px; padding-inline: 16px; gap: 8px; }
  .workout-heading { padding-inline: 16px; }
  .workout-footer { padding-inline: 16px; }
}
.workout-actions {
  display: grid;
  gap: 14px;
  padding: 24px;
  white-space: pre-wrap;
}

.workout-actions button { width: 100%; min-height: 44px; padding: 8px; }
#workout-timer-value { font-size: 3rem; text-align: center; color: var(--accent); }

.workout-result-content {
  display: grid;
  flex: 1;
  align-content: center;
  justify-items: center;
  gap: 16px;
  padding: 32px 24px;
  text-align: center;
}

.workout-result h1 { font-size: 1.5rem; }
