* { box-sizing: border-box; }

/* Атрибут hidden має ховати елемент завжди: інакше display з класу (.header-tab, label тощо)
   перекриває його, і, наприклад, кнопки адміністратора бачать усі */
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: #f3f4f6;
  color: #1f2937;
}

header {
  background: #1f2937;
  color: #fff;
  padding: 16px 20px 0;
}

header h1 { margin: 0 0 12px; font-size: 1.4rem; }

nav { display: flex; gap: 4px; }

.tab {
  background: transparent;
  color: #d1d5db;
  border: none;
  border-radius: 8px 8px 0 0;
  padding: 10px 16px;
  cursor: pointer;
  font-size: 0.95rem;
}

.tab.active { background: #f3f4f6; color: #1f2937; font-weight: 600; }

.tab-icon { display: none; }

main { max-width: 800px; margin: 0 auto; padding: 20px; }

.panel { display: none; }
.panel.active { display: block; }

.card {
  background: #fff;
  border-radius: 10px;
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.card h2 { margin: 0 0 12px; font-size: 1.1rem; }
.card h3 { margin: 0 0 8px; font-size: 1rem; }

.row {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 12px;
  align-items: flex-end;
}

label {
  display: flex;
  flex-direction: column;
  font-size: 0.85rem;
  color: #4b5563;
  gap: 4px;
}

label.grow { flex: 1; min-width: 180px; }

input, select {
  padding: 8px 10px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  font-size: 0.95rem;
  background: #fff;
}

input.num { width: 110px; }

button, .file-btn {
  padding: 9px 14px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
  font-size: 0.9rem;
  color: #1f2937;
}

button, .file-btn, .tab, label.checkbox {
  touch-action: manipulation; /* без затримки й масштабування на подвійний дотик */
  -webkit-tap-highlight-color: transparent;
}

button:hover, .file-btn:hover { background: #f9fafb; }

button.primary { background: #2563eb; color: #fff; border-color: #2563eb; }
button.primary:hover { background: #1d4ed8; }

button.danger { color: #dc2626; border-color: #fecaca; }
button.danger:hover { background: #fef2f2; }

button.small { padding: 4px 8px; font-size: 0.8rem; }

.file-btn { display: inline-block; flex-direction: row; }
.file-btn input { display: none; }

.actions { margin: 12px 0 0; }

.entries .empty, .empty { color: #9ca3af; font-style: italic; }

.entry {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid #f3f4f6;
}

.entry:last-child { border-bottom: none; }

.entry-name { font-weight: 600; }
.entry-details { color: #4b5563; font-size: 0.9rem; }

.total {
  display: inline-block;
  background: #ecfdf5;
  color: #047857;
  border-radius: 4px;
  padding: 1px 6px;
  font-size: 0.85rem;
  margin-left: 6px;
}

.workout-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.workout-meta { color: #6b7280; font-size: 0.85rem; }

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

.exercise-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid #f3f4f6;
}

.badge {
  font-size: 0.75rem;
  background: #eef2ff;
  color: #4338ca;
  border-radius: 4px;
  padding: 2px 6px;
  margin-left: 8px;
}

.badge.cardio { background: #fef3c7; color: #b45309; }

/* ---------- Статистика ---------- */

.card-header {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}

.card-header h2 { margin: 0; }

.row.compact { margin: 0; gap: 8px; }

.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.tile {
  background: #fff;
  border-radius: 10px;
  padding: 14px 16px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.tile-label { color: #52514e; font-size: 0.85rem; }
.tile-value { font-size: 1.6rem; font-weight: 600; margin-top: 4px; color: #0b0b0b; }

details { margin-top: 12px; }
summary { cursor: pointer; color: #52514e; font-size: 0.9rem; }

table { width: 100%; border-collapse: collapse; margin-top: 8px; font-size: 0.9rem; }
th, td { text-align: left; padding: 6px 8px; border-bottom: 1px solid #e1e0d9; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- Графіки ---------- */

.chart {
  --series-1: #2a78d6;
  position: relative;
}

.chart svg { display: block; overflow: visible; }

.chart-grid { stroke: #e1e0d9; stroke-width: 1; }
.chart-baseline { stroke: #c3c2b7; stroke-width: 1; }

.chart-axis-text {
  fill: #898781;
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}

.chart-value-label { fill: #0b0b0b; font-size: 12px; font-weight: 600; }

.chart-mark { fill: var(--series-1); }
.chart-mark.active { opacity: 0.8; }

.chart-line {
  fill: none;
  stroke: var(--series-1);
  stroke-width: 2;
  stroke-linejoin: round;
  stroke-linecap: round;
}

.chart-dot { fill: var(--series-1); stroke: #fff; stroke-width: 2; }
.chart-dot.active { r: 6; }

.chart-crosshair { stroke: #c3c2b7; stroke-width: 1; display: none; pointer-events: none; }

.chart-hit { fill: transparent; cursor: crosshair; }

.chart-tooltip {
  display: none;
  position: absolute;
  transform: translate(-50%, -100%);
  background: #fff;
  border: 1px solid rgba(11, 11, 11, 0.1);
  border-radius: 6px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
  padding: 6px 10px;
  font-size: 0.8rem;
  color: #0b0b0b;
  white-space: nowrap;
  pointer-events: none;
  z-index: 1;
}

.chart-tooltip .muted { color: #52514e; }

/* ---------- Редактор підходів ---------- */

.sets-editor { flex-basis: 100%; }

.set-row {
  display: grid;
  grid-template-columns: 56px 120px 120px 36px;
  gap: 8px;
  align-items: center;
  margin-bottom: 6px;
}

.set-row input { width: 100%; min-width: 0; }

.set-head { font-size: 0.85rem; color: #4b5563; margin-bottom: 4px; }

.set-num { color: #6b7280; text-align: center; }

.set-row button:disabled { opacity: 0.3; cursor: default; }

/* ---------- Анімації вправ ---------- */

.entry-form {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: flex-start;
}

.entry-form #entry-fields { flex: 1; min-width: 280px; }

.media {
  display: block;
  border-radius: 8px;
  background: #f3f4f6;
  object-fit: contain;
  cursor: zoom-in;
}

.media-preview { width: 220px; height: 165px; }
.media-thumb { width: 56px; height: 42px; flex-shrink: 0; }

.media-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  color: #9ca3af;
  font-size: 0.7rem;
  cursor: default;
}

.exercise-info { display: flex; align-items: center; gap: 12px; }
.exercise-actions { display: flex; gap: 6px; align-items: center; flex-shrink: 0; }

.file-btn.small { padding: 4px 8px; font-size: 0.8rem; }

dialog {
  border: none;
  border-radius: 12px;
  padding: 16px;
  max-width: min(720px, 92vw);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
}

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

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  margin-bottom: 12px;
}

.dialog-header h2 { margin: 0; font-size: 1.1rem; }

#media-dialog-body .media { max-width: 100%; max-height: 75vh; cursor: default; }

/* ---------- Статистика по вправах ---------- */

.exercise-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px;
}

.exercise-card {
  display: flex;
  flex-direction: column;
  gap: 6px;
  text-align: left;
  padding: 12px;
  border: 1px solid #e1e0d9;
  border-radius: 8px;
  background: #fff;
  font: inherit;
}

.exercise-card:hover { border-color: #2a78d6; background: #fff; }
.exercise-card.selected { border-color: #2a78d6; box-shadow: 0 0 0 1px #2a78d6; }

.exercise-card-title { display: flex; justify-content: space-between; align-items: center; gap: 8px; font-weight: 600; }
.exercise-card-line { color: #52514e; font-size: 0.85rem; }
.exercise-card .sparkline { height: 40px; }

.progress-summary {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: stretch;
  margin-bottom: 16px;
}

.progress-summary .tiles { flex: 1; margin: 0; min-width: 260px; }

.progress-summary .tile {
  box-shadow: none;
  border: 1px solid #e1e0d9;
  padding: 10px 12px;
}

.progress-summary .tile-value { font-size: 1.25rem; }
.tile-sub { color: #898781; font-size: 0.8rem; margin-top: 2px; }

.exercise-card .sparkline { display: block; }
.exercise-card-line.empty { font-style: italic; }

/* ---------- Повідомлення ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 16px);
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: min(480px, calc(100vw - 32px));
  padding: 12px 18px;
  border-radius: 10px;
  background: #1f2937;
  color: #fff;
  font-size: 0.95rem;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.2);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 10;
}

.toast.visible { opacity: 1; transform: translate(-50%, 0); }

.toast-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  font-size: 0.8rem;
  font-weight: 700;
  color: #fff;
}

.toast.success .toast-icon { background: #0ca30c; }
.toast.error .toast-icon { background: #d03b3b; }

/* ---------- Тренер ---------- */

label.checkbox {
  flex-direction: row;
  align-items: center;
  gap: 8px;
  font-size: 0.95rem;
  color: #1f2937;
  cursor: pointer;
}

label.checkbox.small { font-size: 0.85rem; color: #4b5563; }
label.checkbox input { width: 16px; height: 16px; margin: 0; }

.workout-actions { display: flex; align-items: center; gap: 12px; flex-shrink: 0; }

.status-note {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  margin-bottom: 16px;
  border-radius: 10px;
  background: #fff;
  border: 1px solid #e1e0d9;
  color: #0b0b0b;
}

.status-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  font-size: 0.8rem;
  font-weight: 700;
  color: #0b0b0b;
}

.status-note.warning { border-color: #fab219; }
.status-note.warning .status-icon { background: #fab219; }
.status-note.critical { border-color: #d03b3b; }
.status-note.critical .status-icon { background: #d03b3b; color: #fff; }

.ledger .muted { color: #898781; }
.ledger td.negative { color: #d03b3b; font-weight: 600; }
.ledger td { vertical-align: middle; }
.ledger td.nowrap { white-space: nowrap; }

/* ---------- Телефон ---------- */

@media (max-width: 640px) {
  /* Заголовок під «чубчиком»: при запуску з початкового екрана статус-бар прозорий */
  header { padding: calc(10px + env(safe-area-inset-top)) 16px 10px; }
  header h1 { margin: 0; font-size: 1.15rem; }

  /* Вкладки — нижня панель, як у звичайних застосунках */
  nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 5;
    gap: 2px;
    padding: 6px max(6px, env(safe-area-inset-right)) calc(6px + env(safe-area-inset-bottom)) max(6px, env(safe-area-inset-left));
    background: #1f2937;
    border-top: 1px solid rgba(255, 255, 255, 0.08);
  }

  .tab {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3px;
    padding: 6px 2px;
    border-radius: 10px;
    font-size: 0.68rem;
  }

  .tab-icon { display: block; font-size: 1.3rem; line-height: 1; }
  .tab.active { background: rgba(255, 255, 255, 0.12); color: #fff; }

  main {
    padding: 12px max(12px, env(safe-area-inset-right)) calc(88px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  }

  .card { padding: 14px; border-radius: 12px; margin-bottom: 12px; }

  /* 16px — менший шрифт змушує iOS збільшувати сторінку при фокусі на полі */
  input, select { font-size: 16px; padding: 10px 12px; min-height: 44px; }
  input[type="file"] { min-height: 0; padding: 8px; font-size: 14px; max-width: 100%; }
  label.grow { flex-basis: 100%; min-width: 0; }
  input.num { width: 104px; }

  /* Зони дотику — не менше 44px */
  button, .file-btn { min-height: 44px; font-size: 0.95rem; }
  button.small, .file-btn.small { min-height: 36px; padding: 6px 10px; font-size: 0.85rem; }
  .file-btn { display: inline-flex; align-items: center; }
  .card > button.primary { width: 100%; }
  .actions button.primary { flex: 1; }

  label.checkbox { min-height: 36px; }
  label.checkbox input { width: 20px; height: 20px; min-height: 0; }

  /* Редактор підходів вміщається у ширину екрана */
  .entry-form #entry-fields { flex-basis: 100%; min-width: 0; }
  .set-row { grid-template-columns: 28px 1fr 1fr 44px; gap: 6px; }
  .set-row button { min-height: 44px; }
  .media-preview { width: 100%; height: 200px; }

  .workout-header { flex-wrap: wrap; align-items: flex-start; gap: 8px; }
  .workout-actions { gap: 8px; }

  /* Кнопки вправи — окремим рядком під назвою */
  .exercise-list li { flex-wrap: wrap; gap: 8px; padding: 10px 0; }
  .exercise-info { flex-basis: 100%; }
  .exercise-actions { flex: 1; justify-content: flex-end; }

  /* Статистика */
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin-bottom: 12px; }
  .tile { padding: 12px; }
  .tile-value { font-size: 1.3rem; overflow-wrap: anywhere; }
  .progress-summary .tiles { flex-basis: 100%; min-width: 0; }
  .card-header > select, .card-header .row.compact { width: 100%; }
  .card-header .row.compact select { flex: 1; min-width: 0; }
  .exercise-cards { grid-template-columns: 1fr; }

  /* Журнал оплат: компактніше, щоб 5 колонок вмістилися */
  .ledger { font-size: 0.8rem; }
  .ledger th, .ledger td { padding: 6px 4px; }

  dialog { padding: 12px; }
  #media-dialog-body .media { max-height: 70vh; }

  /* Повідомлення над нижньою панеллю */
  .toast { bottom: calc(84px + env(safe-area-inset-bottom)); }
}

/* ---------- Каталог вправ ---------- */

.hint { color: #6b7280; font-size: 0.85rem; margin: 0 0 12px; }
.hint a { color: #2563eb; }

.catalog-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 0;
  border-bottom: 1px solid #f3f4f6;
}

.catalog-item:last-of-type { border-bottom: none; }
.catalog-item .media-thumb { width: 64px; height: 48px; cursor: pointer; }
.catalog-info { flex: 1; min-width: 0; }
.catalog-name { font-weight: 600; }
.catalog-meta { color: #6b7280; font-size: 0.85rem; }

.instructions { padding-left: 20px; margin: 12px 0 4px; font-size: 0.9rem; line-height: 1.45; }
.instructions li { margin-bottom: 6px; }

.catalog-add { border-top: 1px solid #e1e0d9; padding-top: 12px; margin-top: 8px; }
.catalog-add h3 { margin: 0 0 8px; font-size: 1rem; }

#media-dialog-body .catalog-meta { margin: 10px 0 0; }
#media-dialog { width: min(720px, 92vw); }
#media-dialog-body > .media { margin: 0 auto; max-height: 45vh; }

/* ---------- Вхід ---------- */

/* Поки не зрозуміло, чи є сесія, і до входу застосунок не показуємо */
body:not(.logged-in) > header,
body:not(.logged-in) > main { display: none; }

.login-screen { display: none; }

body.logged-out .login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  min-height: 100dvh;
  padding: calc(16px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom));
  background: #1f2937;
}

.login-card {
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: min(360px, 100%);
  padding: 28px 24px;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
}

.login-logo { width: 64px; height: 64px; border-radius: 14px; align-self: center; }
.login-card h1 { margin: 0 0 4px; font-size: 1.3rem; text-align: center; }
.login-card input { width: 100%; font-size: 16px; padding: 10px 12px; }
.login-card button { min-height: 44px; font-size: 1rem; }

.header-top { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 12px; }
.header-top h1 { margin: 0; }

.user-menu { display: flex; align-items: center; gap: 10px; color: #d1d5db; font-size: 0.9rem; }

/* Кнопки вкладок «Користувачі» та «Акаунт» у шапці */
.header-tab {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: transparent;
  color: #d1d5db;
  border-color: rgba(255, 255, 255, 0.25);
  padding: 6px 12px;
}

.header-tab:hover { background: rgba(255, 255, 255, 0.08); }
.header-tab.active { background: #f3f4f6; color: #1f2937; border-color: #f3f4f6; }

@media (max-width: 640px) {
  .header-top { margin-bottom: 0; }
  /* На телефоні в шапці лише іконки */
  .header-tab-label { display: none; }
  .header-tab { min-height: 40px; min-width: 40px; justify-content: center; font-size: 1.05rem; padding: 6px 8px; }
  .user-menu { gap: 6px; flex-shrink: 0; }
  /* Заголовок в один рядок поруч з трьома кнопками шапки */
  .header-top h1 { font-size: 1.05rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
}

/* ---------- Користувачі ---------- */

.user-name { font-weight: 600; }
.hint-inline { color: #6b7280; font-size: 0.8rem; margin-left: 6px; }
div.hint-inline { margin-left: 0; margin-top: 2px; }
.user-form { margin-top: 16px; border-top: 1px solid #e1e0d9; padding-top: 12px; }
.user-form h3 { margin: 0 0 8px; font-size: 1rem; }
#reset-form { display: flex; flex-direction: column; gap: 10px; min-width: min(360px, 80vw); }
#reset-form .hint { margin: 0; }

/* ---------- Акаунт і користувачі ---------- */

textarea {
  padding: 8px 10px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  font: inherit;
  font-size: 0.95rem;
  resize: vertical;
  width: 100%;
}

.account-info { margin: 0 0 12px; }
.app-version { margin: 12px 0 0; color: #6b7280; font-size: 0.85rem; }
.login-version { margin: 0; text-align: center; }
.user-info { min-width: 0; }
.user-note {
  margin-top: 4px;
  padding: 6px 8px;
  background: #f9fafb;
  border-left: 3px solid #c3c2b7;
  border-radius: 4px;
  color: #374151;
  font-size: 0.85rem;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
#note-form { display: flex; flex-direction: column; gap: 10px; min-width: min(420px, 80vw); }

@media (max-width: 640px) {
  textarea { font-size: 16px; }
  .exercise-list li .user-info { flex-basis: 100%; }
}

/* Загальні вправи (додає адміністратор для всіх) */
.badge.shared { background: #ecfdf5; color: #047857; }
#new-exercise-shared-row { margin-bottom: 12px; }
.catalog-add .checkbox { margin-bottom: 12px; }
#exercise-form { display: flex; flex-direction: column; gap: 10px; min-width: min(420px, 80vw); }
#exercise-form .hint { margin: 0; }
