:root {
  color-scheme: light dark;
  --bg: #f7f7f4;
  --surface: #ffffff;
  --surface-2: #f0f1ec;
  --border: #e2e2dc;
  --text: #1f2320;
  --muted: #6b6f6a;
  --accent: #2f6fa8;
  --accent-hover: #24567f;
  --accent-soft: #e5eef6;
  --danger: #b3413c;
  --danger-soft: #fbeae9;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(20, 20, 15, 0.05), 0 4px 14px rgba(20, 20, 15, 0.06);
  --nav-height: 3.75rem;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171a;
    --surface: #1d2023;
    --surface-2: #24272b;
    --border: #34383c;
    --text: #eceeec;
    --muted: #989c9a;
    --accent: #5b9bd8;
    --accent-hover: #7fb0e0;
    --accent-soft: rgba(91, 155, 216, 0.16);
    --danger: #e2726c;
    --danger-soft: rgba(226, 114, 108, 0.14);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 14px rgba(0, 0, 0, 0.35);
  }
}

* {
  box-sizing: border-box;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  margin: 0;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
}

h1 {
  font-size: 1.4rem;
  line-height: 1.3;
  margin: 0 0 0.25rem;
}

h2 {
  font-size: 1.05rem;
  margin: 1.5rem 0 0.6rem;
}

a {
  color: var(--accent);
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- Navigation ---------- */

.topnav {
  display: flex;
  gap: 1.5rem;
  padding: 0.9rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 40;
}

.topnav a {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  text-decoration: none;
  font-weight: 600;
  color: var(--muted);
  transition: color 0.15s ease;
}

.topnav a:hover,
.topnav a.active {
  color: var(--accent);
}

.logout-form {
  margin-left: auto;
}

.nav-logout {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  background: none;
  border: none;
  padding: 0;
  font: inherit;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  transition: color 0.15s ease;
}

.nav-logout:hover {
  color: var(--danger);
}

.nav-icon {
  font-size: 1.15rem;
  line-height: 1;
}

@media (max-width: 640px) {
  .topnav {
    position: fixed;
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    justify-content: space-around;
    gap: 0;
    padding: 0.4rem 0.25rem calc(0.4rem + env(safe-area-inset-bottom));
    border-top: 1px solid var(--border);
    border-bottom: none;
    box-shadow: 0 -2px 10px rgba(20, 20, 15, 0.06);
  }

  .topnav a,
  .nav-logout {
    flex-direction: column;
    gap: 0.15rem;
    font-size: 0.68rem;
    flex: 1;
    text-align: center;
  }

  .logout-form {
    margin-left: 0;
    flex: 1;
    display: flex;
  }

  .nav-icon {
    font-size: 1.3rem;
  }

  body {
    padding-bottom: calc(var(--nav-height) + env(safe-area-inset-bottom));
  }
}

/* ---------- Layout ---------- */

.container {
  max-width: 900px;
  margin: 0 auto;
  padding: 1.5rem;
}

.login-container {
  max-width: 320px;
  margin-top: 4rem;
}

@media (max-width: 640px) {
  .container {
    padding: 1rem;
  }
}

.page-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

/* ---------- Buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 2.5rem;
  padding: 0.5rem 1rem;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: white;
  text-decoration: none;
  border: none;
  cursor: pointer;
  font-size: 0.95rem;
  font-weight: 600;
  transition: background 0.15s ease;
}

.btn:hover {
  background: var(--accent-hover);
}

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-secondary:hover {
  background: var(--surface-2);
}

.btn-danger {
  background: var(--danger);
}

.btn-danger:hover {
  background: var(--danger);
  filter: brightness(0.9);
}

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.5rem;
  height: 2.5rem;
  padding: 0;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: white;
  border: none;
  text-decoration: none;
  font-size: 1.1rem;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s ease;
}

.btn-icon:hover {
  background: var(--accent-hover);
}

/* ---------- Forms ---------- */

form label {
  display: block;
  margin-bottom: 1rem;
  font-weight: 600;
}

.checkbox-label {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-weight: 400;
}

.checkbox-label input {
  width: auto;
  min-height: auto;
  margin: 0;
}

input[type="text"],
input[type="number"],
input[type="date"],
textarea,
select {
  display: block;
  width: 100%;
  box-sizing: border-box;
  margin-top: 0.3rem;
  padding: 0.55rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font: inherit;
  background: var(--surface);
  color: var(--text);
  min-height: 2.5rem;
}

textarea {
  min-height: auto;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--accent);
}

.error {
  color: var(--danger);
  font-weight: 600;
}

.hint {
  color: var(--muted);
  font-size: 0.85rem;
}

.empty {
  color: var(--muted);
}

/* ---------- Tags / badges ---------- */

.tag {
  display: inline-block;
  font-size: 0.75rem;
  color: var(--muted);
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 999px;
  padding: 0.15rem 0.6rem;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  text-decoration: none;
  font: inherit;
  font-size: 1rem;
  cursor: pointer;
  flex-shrink: 0;
}

.icon-btn:hover {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.icon-btn-danger:hover {
  background: var(--danger-soft);
  border-color: var(--danger);
  color: var(--danger);
}

.row-actions {
  display: flex;
  gap: 0.5rem;
  flex-shrink: 0;
}

.inline {
  display: inline;
}

.title-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  min-width: 0;
}

.title-row h1 {
  margin: 0;
  min-width: 0;
}

table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 0.75rem;
}

.table-scroll {
  overflow-x: auto;
  margin: 0 -0.05rem 0.75rem;
}

th, td {
  padding: 0.4rem 0.3rem;
  text-align: left;
}

th {
  font-size: 0.8rem;
  color: var(--muted);
  font-weight: 600;
  border-bottom: 1px solid var(--border);
}

td {
  border-bottom: 1px solid var(--border);
}

/* ---------- Searchable select ---------- */

.searchable-select {
  position: relative;
}

.searchable-select-native {
  display: none;
}

.searchable-select-input {
  margin-top: 0;
}

.searchable-select-dropdown {
  position: fixed;
  z-index: 100;
  max-height: 260px;
  overflow-y: auto;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
}

.searchable-select-group {
  padding: 0.3rem 0.6rem 0.15rem;
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
}

.searchable-select-item {
  padding: 0.55rem 0.6rem;
  cursor: pointer;
}

.searchable-select-item:hover {
  background: var(--accent-soft);
}

.searchable-select-empty {
  padding: 0.4rem 0.6rem;
  color: var(--muted);
  font-size: 0.9rem;
}

/* ---------- Exercise library ---------- */

.muscle-group-block {
  margin-bottom: 1.75rem;
}

.muscle-group-block h2 {
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.3rem;
}

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

.exercise-list li {
  padding: 0.55rem 0.1rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.exercise-name {
  font-weight: 600;
}

.exercise-meta {
  font-size: 0.8rem;
  color: var(--muted);
}

/* ---------- Journal (month calendar) ---------- */

.tag-done {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--accent-soft);
}

.tag-future {
  color: var(--muted);
  border-style: dashed;
}

.cal-nav {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.cal-month-label {
  font-weight: 700;
  font-size: 1.05rem;
  min-width: 9rem;
}

.cal-today-link {
  margin-left: auto;
}

.cal-legend {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.cal-grid {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface);
}

.cal-week-row {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  border-top: 1px solid var(--border);
}

.cal-week-row:first-child {
  border-top: none;
}

.cal-week-row.is-current-week {
  background: var(--accent-soft);
}

.cal-weekday-header {
  background: var(--surface-2);
}

.cal-weekday {
  padding: 0.4rem 0.3rem;
  text-align: center;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--muted);
}

.cal-day {
  min-height: 4.5rem;
  padding: 0.3rem;
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.cal-day:first-child {
  border-left: none;
}

.cal-day.is-outside {
  opacity: 0.4;
}

.cal-day-num {
  font-size: 0.8rem;
  color: var(--muted);
  font-weight: 600;
}

.cal-day.is-today .cal-day-num {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.4rem;
  height: 1.4rem;
  padding: 0 0.3rem;
  border-radius: 999px;
  background: var(--accent);
  color: white;
}

.cal-entries {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.cal-entry {
  display: flex;
  align-items: center;
  gap: 0.2rem;
  padding: 0.1rem 0.25rem;
  border-radius: var(--radius-sm);
  font-size: 0.65rem;
  text-decoration: none;
  color: inherit;
  border: 1px solid transparent;
  overflow: hidden;
}

.cal-entry-icon {
  flex-shrink: 0;
}

.cal-entry-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.cal-entry-done {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.cal-entry-planned {
  background: var(--surface-2);
  border-color: var(--border);
  border-style: dashed;
}

@media (max-width: 640px) {
  .cal-day {
    min-height: 3.5rem;
    padding: 0.2rem;
  }

  .cal-entry-label {
    display: none;
  }

  .cal-entry {
    justify-content: center;
    padding: 0.1rem;
  }

  .cal-month-label {
    min-width: 0;
    flex: 1;
    text-align: center;
  }
}

.exercise-group {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 0.9rem 1rem;
  margin-bottom: 0.9rem;
}

.exercise-group-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.exercise-group-head h3 {
  margin: 0;
  font-size: 1rem;
  min-width: 0;
}

.exercise-group-title {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}

.exercise-progress-tag {
  flex-shrink: 0;
}

.workout-progress {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-bottom: 1.25rem;
}

.workout-progress-bar {
  flex: 1;
  height: 0.5rem;
  border-radius: 999px;
  background: var(--surface-2);
  overflow: hidden;
}

.workout-progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 999px;
  transition: width 0.2s ease;
}

.workout-progress-label {
  flex-shrink: 0;
  font-size: 0.85rem;
  color: var(--muted);
  white-space: nowrap;
}

.exercise-collapse-toggle {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  background: none;
  border: none;
  padding: 0;
  margin: 0;
  font: inherit;
  font-weight: 600;
  color: inherit;
  cursor: pointer;
  min-width: 0;
  text-align: left;
}

.collapse-chevron {
  display: inline-block;
  flex-shrink: 0;
  font-size: 0.75em;
  color: var(--muted);
  transition: transform 0.15s ease;
}

.exercise-group.is-collapsed .collapse-chevron {
  transform: rotate(-90deg);
}

.exercise-group.is-collapsed .exercise-group-body {
  display: none;
}

.reorder-controls {
  display: flex;
  gap: 0.15rem;
  flex-shrink: 0;
}

.recommend-inline {
  font-size: 0.8rem;
  color: var(--accent);
  margin: 0 0 0.5rem;
}

.template-plan .recommend-inline {
  display: block;
  margin: 0.1rem 0 0.4rem;
}

.exercise-history {
  font-size: 0.78rem;
  color: var(--muted);
  margin: 0 0 0.5rem;
}

.template-plan .exercise-history {
  display: block;
  margin: 0.1rem 0 0.4rem;
}

.icon-btn-xs {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.75rem;
  height: 1.75rem;
  border-radius: var(--radius-sm);
  padding: 0;
  font-size: 0.8rem;
  line-height: 1;
  cursor: pointer;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

.icon-btn-xs:hover {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.icon-btn-xs:disabled {
  opacity: 0.3;
  cursor: default;
  pointer-events: none;
}

.set-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.3rem 0;
  border-top: 1px solid var(--border);
}

.set-row:first-of-type {
  border-top: none;
}

.set-number {
  flex: 0 0 1.75rem;
  color: var(--muted);
  font-size: 0.85rem;
}

.set-values {
  flex: 1;
}

.set-reps-input {
  display: inline-block;
  width: 3.2rem;
  min-height: auto;
  margin: 0;
  padding: 0.2rem 0.3rem;
  text-align: center;
}

.reps-presets {
  display: inline-flex;
  gap: 0.3rem;
  margin-left: 0.5rem;
}

.btn-preset {
  min-height: auto;
  padding: 0.25rem 0.55rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
}

.btn-preset:hover {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.set-check {
  width: 1.2rem;
  height: 1.2rem;
  min-height: auto;
  margin: 0;
  flex-shrink: 0;
  cursor: pointer;
}

.set-row.is-completed .set-values {
  color: var(--muted);
  text-decoration: line-through;
}

.rest-timer {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  margin-bottom: 1.25rem;
  flex-wrap: wrap;
  position: sticky;
  top: 0.5rem;
  z-index: 30;
}

.rest-timer-display {
  font-size: 1.4rem;
  font-weight: 700;
  min-width: 3.5rem;
  font-variant-numeric: tabular-nums;
}

.rest-timer.is-running {
  border-color: var(--accent);
}

.rest-timer.is-running .rest-timer-display {
  color: var(--accent);
}

.rest-timer.is-done {
  border-color: var(--danger);
}

.rest-timer.is-done .rest-timer-display {
  color: var(--danger);
}

.rest-timer-controls {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.copy-day-form {
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
  flex-wrap: wrap;
  margin-bottom: 1.25rem;
  padding: 0.75rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.copy-day-form label {
  margin-bottom: 0;
}

.add-set-form {
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
  flex-wrap: wrap;
  margin-bottom: 1.5rem;
  padding: 0.9rem;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}

.add-set-form label {
  margin-bottom: 0;
  flex: 1 1 140px;
  min-width: 0;
}

.add-set-form label.exercise-field {
  flex: 2 1 220px;
}

.template-plan {
  background: var(--accent-soft);
  border-radius: var(--radius);
  padding: 0.9rem 1.1rem;
  margin-bottom: 1.5rem;
}

.template-plan ul {
  margin: 0.4rem 0 0;
  padding-left: 1.1rem;
}

/* ---------- Templates ---------- */

.template-list {
  list-style: none;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.template-list li {
  padding: 0.7rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.template-list a {
  color: inherit;
  text-decoration: none;
  font-weight: 600;
}

.template-exercise-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.5rem 0;
  border-top: 1px solid var(--border);
}

.template-exercise-row:first-of-type {
  border-top: none;
}

.add-template-exercise-form {
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
  flex-wrap: wrap;
  margin: 1rem 0;
  padding: 0.9rem;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}

.add-template-exercise-form label {
  margin-bottom: 0;
  flex: 1 1 140px;
  min-width: 0;
}

.add-template-exercise-form label.exercise-field {
  flex: 2 1 220px;
}

/* ---------- Group class types ---------- */

.class-type-list {
  list-style: none;
  padding: 0;
  margin-bottom: 1rem;
}

.class-type-list li {
  padding: 0.5rem 0.1rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.add-class-type-form {
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
  flex-wrap: wrap;
}

.add-class-type-form label {
  margin-bottom: 0;
  flex: 1 1 220px;
}

/* ---------- Progress ---------- */

.progress-picker {
  margin-bottom: 1.5rem;
  max-width: 360px;
}

.summary-cards {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin-bottom: 1.25rem;
}

.summary-card {
  flex: 1 1 140px;
  padding: 0.8rem 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.summary-card .value {
  font-size: 1.4rem;
  font-weight: 700;
}

.summary-card .label {
  font-size: 0.8rem;
  color: var(--muted);
}

.progress-chart {
  width: 100%;
  height: auto;
  margin-bottom: 1.5rem;
}

.recommend-list {
  list-style: none;
  padding: 0;
  margin-bottom: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.recommend-item {
  padding: 0.6rem 0.8rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.5rem;
}

.recommend-exercise {
  font-weight: 700;
  color: inherit;
  text-decoration: none;
  flex-shrink: 0;
}

.recommend-message {
  color: var(--muted);
  font-size: 0.9rem;
}

.recommend-card {
  padding: 0.8rem 1rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface-2);
  margin-bottom: 1.25rem;
}

.recommend-plateau,
.recommend-card.recommend-plateau {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.recommend-ready,
.recommend-card.recommend-ready {
  background: var(--accent-soft);
}

.recommend-incomplete,
.recommend-card.recommend-incomplete {
  border-style: dashed;
}
