:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e2e5eb;
  --text: #161a20;
  --text-dim: #6b7280;
  --accent: #16a34a;
  --accent-dark: #15803d;
  --accent-soft: #e8f7ee;
  --danger: #dc2626;
  --danger-soft: #fdecec;
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}

.hidden { display: none !important; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  padding: calc(12px + var(--safe-top)) 16px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-title { font-size: 18px; font-weight: 700; }

#app {
  padding: 16px 16px calc(100px + var(--safe-bottom));
  max-width: 640px;
  margin: 0 auto;
}

.view-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}
.view-header h2 { margin: 0; }
.header-actions { display: flex; gap: 8px; }

h2 { font-size: 20px; margin: 8px 0 14px; }
h3 { font-size: 15px; margin: 0 0 8px; color: var(--text); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: 0 1px 2px rgba(16,24,40,0.04);
}

.start-card h2 { margin-top: 0; }

.field {
  display: block;
  margin-bottom: 12px;
}
.field span {
  display: block;
  font-size: 13px;
  color: var(--text-dim);
  margin-bottom: 6px;
}
input[type="text"], input[type="number"], select, textarea {
  width: 100%;
  padding: 12px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  font-size: 15px;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
}
input[type="file"] {
  width: 100%;
  color: var(--text-dim);
  font-size: 13px;
}

.search-input { margin-bottom: 12px; }

.btn {
  border: none;
  border-radius: 10px;
  padding: 12px 18px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: transform .05s ease, opacity .15s ease;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-block { width: 100%; }
.btn-sm { padding: 8px 12px; font-size: 13px; }
.btn-lg { padding: 16px 18px; font-size: 17px; }
.btn-primary { background: var(--accent); color: #ffffff; }
.btn-secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn-danger { background: var(--danger-soft); color: #b91c1c; border: 1px solid rgba(220,38,38,0.25); }
.btn-ghost { background: transparent; color: var(--text); border: 1px solid var(--border); }

.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 6px 4px calc(6px + var(--safe-bottom));
  z-index: 20;
}
.tab {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-dim);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px 2px;
  font-size: 11px;
  cursor: pointer;
  border-radius: 10px;
}
.tab-icon { font-size: 20px; }
.tab.active { color: var(--accent-dark); }

/* Session (active workout) */
.session-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.session-name { font-weight: 700; font-size: 16px; }
.session-time { color: var(--text-dim); font-size: 13px; margin-top: 2px; }

.exercise-session-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
  box-shadow: 0 1px 2px rgba(16,24,40,0.04);
}
.exercise-session-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}
.exercise-thumb {
  width: 44px; height: 44px;
  border-radius: 8px;
  object-fit: cover;
  background: var(--surface-2);
  flex-shrink: 0;
}
.exercise-thumb-placeholder {
  width: 44px; height: 44px;
  border-radius: 8px;
  background: var(--surface-2);
  display: flex; align-items: center; justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
}
.exercise-session-title { font-weight: 600; }
.exercise-session-group { font-size: 12px; color: var(--text-dim); }

.exercise-session-card.is-completed {
  background: var(--surface-2);
  opacity: 0.85;
}
.exercise-session-footer {
  display: flex;
  gap: 8px;
  margin-top: 8px;
  flex-wrap: wrap;
}
.exercise-session-footer .btn { flex: 1; }

.completed-section { margin-top: 4px; margin-bottom: 12px; }
.completed-toggle {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-dim);
  text-align: left;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.completed-toggle-arrow { font-size: 12px; }
.completed-list { margin-top: 10px; }

.sets-table { width: 100%; border-collapse: collapse; margin-bottom: 10px; font-size: 14px; }
.sets-table th { text-align: left; color: var(--text-dim); font-weight: 500; font-size: 12px; padding: 4px 6px; }
.sets-table td { padding: 6px; border-top: 1px solid var(--border); }
.sets-table td.num { color: var(--text-dim); width: 24px; }

.last-set-hint { font-size: 12px; color: var(--text-dim); margin: 0 0 8px; }

/* Exercise grid */
.filter-row { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 10px; margin-bottom: 4px; }
.filter-chip {
  flex-shrink: 0;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
}
.filter-chip.active { background: var(--accent); color: #ffffff; border-color: var(--accent); }

.exercise-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}
.exercise-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  text-align: left;
  box-shadow: 0 1px 2px rgba(16,24,40,0.04);
}
.exercise-card img, .exercise-card .no-photo {
  width: 100%; height: 90px;
  object-fit: cover;
  background: var(--surface-2);
  display: flex; align-items: center; justify-content: center;
  font-size: 28px;
}
.exercise-card-body { padding: 8px 10px; }
.exercise-card-name { font-size: 13px; font-weight: 600; line-height: 1.25; }
.exercise-card-group { font-size: 11px; color: var(--text-dim); margin-top: 2px; }
.exercise-card-actions { display: flex; justify-content: flex-end; padding: 0 8px 8px; }

.photo-preview {
  width: 100%;
  max-height: 180px;
  object-fit: cover;
  border-radius: 10px;
  margin: 8px 0 14px;
}

/* History */
.history-card {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 10px;
  cursor: pointer;
  box-shadow: 0 1px 2px rgba(16,24,40,0.04);
}
.history-card input[type="checkbox"] { width: 20px; height: 20px; margin-top: 2px; flex-shrink: 0; accent-color: var(--accent); }
.history-card-title { font-weight: 700; }
.history-card-date { font-size: 13px; color: var(--text-dim); margin-top: 2px; }
.history-card-meta { font-size: 12px; color: var(--text-dim); margin-top: 6px; }
.history-empty, .exercise-empty { color: var(--text-dim); text-align: center; padding: 40px 10px; }

.hint { font-size: 13px; color: var(--text-dim); margin: 0 0 10px; }
.hint code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; font-size: 12px; }

.push-status { font-size: 13px; color: var(--text-dim); margin: 4px 0 12px; }
.push-actions { display: flex; gap: 8px; }
.push-actions .btn { flex: 1; }

/* Rest timer */
.rest-timer {
  position: fixed;
  left: 12px; right: 12px;
  bottom: calc(76px + var(--safe-bottom));
  background: var(--accent);
  color: #ffffff;
  border-radius: 16px;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 8px 24px rgba(16,24,40,0.25);
  z-index: 30;
}
.rest-timer-label { font-size: 12px; opacity: 0.85; }
.rest-timer-clock { font-size: 24px; font-weight: 800; font-variant-numeric: tabular-nums; }
.rest-timer-actions { display: flex; gap: 6px; }
.rest-timer .btn-ghost { border-color: rgba(255,255,255,0.5); color: #ffffff; }
.rest-timer .btn-secondary { background: rgba(255,255,255,0.18); color: #ffffff; border: none; }

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(16,20,26,0.5);
  display: flex;
  align-items: flex-end;
  z-index: 40;
}
.modal-content {
  background: var(--surface);
  width: 100%;
  max-height: 88vh;
  overflow-y: auto;
  border-radius: 20px 20px 0 0;
  padding: 18px 16px calc(24px + var(--safe-bottom));
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}
.btn-close {
  background: var(--surface-2);
  border: none;
  color: var(--text);
  width: 32px; height: 32px;
  border-radius: 50%;
  font-size: 18px;
  cursor: pointer;
}
.modal-actions { display: flex; gap: 8px; margin-top: 14px; }
.modal-actions .btn { flex: 1; }

.detail-exercise { margin-bottom: 16px; }
.detail-exercise-name { font-weight: 700; margin-bottom: 4px; }
.detail-exercise-group { font-size: 12px; color: var(--text-dim); margin-bottom: 8px; }

.toast {
  position: fixed;
  bottom: calc(90px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: #161a20;
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 600;
  z-index: 60;
  box-shadow: 0 4px 16px rgba(16,24,40,0.3);
  max-width: 88vw;
  text-align: center;
}

/* Редактор подхода */
.set-editor { padding-top: 22px; }
.set-editor-field {
  background: var(--surface-2);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
  text-align: center;
}
.set-editor-label {
  font-size: 13px;
  color: var(--text-dim);
  font-weight: 600;
  margin-bottom: 10px;
}
.set-editor-control {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
}
.stepper-btn {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  background: var(--surface);
  box-shadow: 0 1px 3px rgba(16,24,40,0.15);
  color: var(--accent-dark);
  font-size: 28px;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
}
.stepper-btn:active { transform: scale(0.94); background: var(--accent-soft); }
.set-editor-value {
  flex: 1;
  min-width: 0;
  max-width: 160px;
  background: none;
  border: none;
  font-size: 36px;
  font-weight: 800;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  padding: 4px 0;
  border-bottom: 2px dashed var(--border);
}
.set-editor-step {
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 10px;
}

/* Нампад */
.numpad-content { padding-top: 18px; }
.numpad-display {
  text-align: center;
  font-size: 40px;
  font-weight: 800;
  padding: 10px 0 18px;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}
.numpad-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 14px;
}
.numpad-btn {
  padding: 18px 0;
  font-size: 22px;
  font-weight: 700;
  border: none;
  border-radius: 12px;
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
}
.numpad-btn:active { background: var(--accent-soft); }
.numpad-btn-del { color: #b91c1c; }
.numpad-btn:disabled { opacity: 0.35; }
