/* === Reset & Variables === */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg-primary: #0f0f1a;
  --bg-secondary: #1a1a2e;
  --bg-card: #16213e;
  --bg-input: #1a1a3e;
  --text-primary: #eaeaea;
  --text-secondary: #8b8b9e;
  --text-muted: #5a5a6e;
  --accent: #e94560;
  --accent-hover: #ff6b81;
  --success: #2ecc71;
  --danger: #e74c3c;
  --warning: #f39c12;
  --border: #2a2a4a;
  --radius: 12px;
  --radius-sm: 8px;
  --nav-height: 52px;
  --header-height: 56px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* === Screens === */
.screen {
  display: none;
  height: 100%;
}
.screen.active {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding-top: var(--safe-top);
}

.hidden {
  display: none !important;
}

/* === Auth === */
.auth-container {
  width: 100%;
  max-width: 360px;
  padding: 24px;
}

.auth-logo {
  text-align: center;
  margin-bottom: 40px;
}
.auth-logo h1 {
  font-size: 28px;
  margin-top: 16px;
  color: var(--text-primary);
}
.auth-logo p {
  color: var(--text-secondary);
  margin-top: 4px;
}

.form-group {
  margin-bottom: 12px;
}
.form-group label {
  display: block;
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

input[type="text"],
input[type="email"],
input[type="password"],
input[type="url"],
input[type="number"] {
  width: 100%;
  padding: 14px 16px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 16px;
  outline: none;
  transition: border-color 0.2s;
}
input:focus {
  border-color: var(--accent);
}
input::placeholder {
  color: var(--text-muted);
}

.input-large {
  font-size: 20px !important;
  font-weight: 600;
  padding: 16px !important;
  background: transparent !important;
  border: none !important;
  border-bottom: 2px solid var(--border) !important;
  border-radius: 0 !important;
}
.input-large:focus {
  border-bottom-color: var(--accent) !important;
}

/* === Buttons === */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 20px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  -webkit-tap-highlight-color: transparent;
}
.btn-primary {
  background: var(--accent);
  color: white;
  border: 1px solid var(--accent);
}
.btn-primary:hover, .btn-primary:active {
  background: var(--accent-hover);
}
.btn-secondary {
  background: var(--bg-card);
  color: var(--text-primary);
  border: 1px solid var(--border);
}
.btn-danger {
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--danger);
}
.btn-danger:hover {
  background: var(--danger);
  color: white;
}
.btn-full {
  width: 100%;
}
.btn-sm {
  padding: 10px 16px;
  font-size: 13px;
}
.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  pointer-events: none;
}
.btn-icon {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  transition: background 0.2s;
}
.btn-icon:hover {
  background: var(--bg-card);
}
.btn + .btn {
  margin-top: 8px;
}

.error-msg {
  margin-top: 16px;
  padding: 12px;
  background: rgba(231, 76, 60, 0.15);
  border: 1px solid var(--danger);
  border-radius: var(--radius-sm);
  color: var(--danger);
  font-size: 14px;
  text-align: center;
}

/* === App Layout === */
#app {
  display: flex;
  flex-direction: column;
  height: 100%;
}

#app-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  min-height: calc(var(--header-height) + var(--safe-top));
  padding: 0 16px 12px;
  padding-top: var(--safe-top);
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
#app-header h2 {
  font-size: 18px;
  font-weight: 700;
}

/* === Tabs === */
.tab-content {
  display: none;
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  padding-bottom: calc(var(--nav-height) + 16px);
  -webkit-overflow-scrolling: touch;
}
.tab-content.active {
  display: block;
}

/* === Bottom Nav === */
#bottom-nav {
  display: flex;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--nav-height);
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  z-index: 100;
}
#bottom-nav::after {
  content: '';
  position: absolute;
  bottom: calc(-1 * var(--nav-height));
  left: 0;
  right: 0;
  height: var(--nav-height);
  background: var(--bg-secondary);
}

/* === Active Workout Bar === */
.active-workout-bar {
  position: fixed;
  bottom: var(--nav-height);
  left: 0;
  right: 0;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  z-index: 99;
}
.active-workout-bar-info {
  display: flex;
  align-items: center;
  gap: 12px;
  color: white;
  font-weight: 600;
  font-size: 14px;
}
.active-bar-timer {
  font-variant-numeric: tabular-nums;
  opacity: 0.85;
}
.active-workout-bar .btn {
  background: white;
  color: var(--accent);
  border-color: white;
  font-size: 12px;
  padding: 6px 14px;
}

.nav-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 10px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: color 0.2s;
}
.nav-btn.active {
  color: var(--accent);
}

/* === Empty State === */
.empty-state {
  text-align: center;
  padding: 48px 16px;
}
.empty-state svg {
  margin-bottom: 16px;
  opacity: 0.6;
}
.empty-state h3 {
  font-size: 18px;
  margin-bottom: 8px;
}
.empty-state p {
  color: var(--text-secondary);
  font-size: 14px;
}

/* === Cards === */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}
/* === Routine Preview === */
.routine-preview-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 20px;
}
.routine-preview-header h3 {
  font-size: 20px;
  font-weight: 700;
}
.preview-section-header {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--accent);
  padding: 12px 0 6px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
}
.preview-exercise {
  padding: 10px 0;
  border-bottom: 1px solid rgba(255,255,255,0.05);
}
.preview-exercise:last-child {
  border-bottom: none;
}
.preview-exercise-name {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
}
.preview-exercise-detail {
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 2px;
  display: flex;
  align-items: center;
  gap: 12px;
}
.preview-rest {
  color: var(--text-muted);
  font-size: 12px;
}
.preview-exercise-notes {
  font-size: 12px;
  color: var(--text-muted);
  font-style: italic;
  margin-top: 2px;
}
#btn-start-from-preview {
  margin-top: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}
.card-title {
  font-size: 16px;
  font-weight: 600;
}
.card-subtitle {
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 2px;
}

/* === Collapsable sections === */
.section-header-toggle {
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.collapse-icon {
  transition: transform 0.2s;
  color: var(--text-muted);
}
.collapse-icon.rotated {
  transform: rotate(180deg);
}
.routine-toggle {
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.routine-body {
  overflow: hidden;
}

/* === Routine Card === */
.routine-card {
  transition: border-color 0.2s;
}
.routine-card:hover, .routine-card:active {
  border-color: var(--accent);
}
.routine-card .exercise-preview {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.6;
}
.routine-actions {
  display: flex;
  gap: 8px;
  margin-top: 12px;
}
.routine-actions .btn {
  flex: 1;
}

/* === Quick Start List === */
#routine-quick-list {
  margin-bottom: 16px;
}
.quick-routine-card {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 8px;
  cursor: pointer;
  transition: border-color 0.2s;
  width: 100%;
  text-align: left;
  color: var(--text-primary);
}
.quick-routine-card:hover, .quick-routine-card:active {
  border-color: var(--accent);
}
.quick-routine-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: rgba(233, 69, 96, 0.15);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.quick-routine-info {
  flex: 1;
  min-width: 0;
}
.quick-routine-info strong {
  display: block;
  font-size: 15px;
}
.quick-routine-info span {
  display: block;
  font-size: 13px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* === Active Workout === */
.workout-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 16px;
  gap: 12px;
}
.workout-info h3 {
  font-size: 18px;
}
.workout-section-header {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-muted);
  padding: 12px 0 6px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 8px;
}
.exercise-notes {
  font-size: 13px;
  color: var(--text-secondary);
  font-style: italic;
  padding: 4px 0 8px;
}
.workout-timer {
  font-size: 14px;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}
.workout-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

/* === Exercise Block === */
.exercise-block {
  margin-bottom: 16px;
}
.exercise-block-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
}
.exercise-block-name {
  font-size: 16px;
  font-weight: 600;
  color: var(--accent);
}
.exercise-rest-label {
  display: block;
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}
.btn-remove-exercise {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 18px;
  padding: 4px 8px;
}

/* === Sets Table === */
.sets-table {
  width: 100%;
  border-collapse: collapse;
}
.sets-table th {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 500;
  padding: 6px 4px;
  text-align: center;
  text-transform: uppercase;
}
.sets-table th:first-child {
  width: 40px;
  text-align: center;
}
.sets-table td {
  padding: 4px;
  text-align: center;
}
.set-number {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
  min-width: 24px;
  text-align: center;
}
.swipe-wrapper td {
  padding: 0 !important;
  position: relative;
  overflow: hidden;
}
.swipe-delete-bg {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 120px;
  background: var(--danger);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 13px;
  font-weight: 600;
  border-radius: 6px;
}
.swipe-content {
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--bg-primary);
  position: relative;
  z-index: 1;
  padding: 4px;
  will-change: transform;
}
.swipe-content .set-input {
  flex: 1;
}
.set-input {
  width: 100%;
  padding: 10px 4px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-primary);
  font-size: 15px;
  text-align: center;
  outline: none;
  -moz-appearance: textfield;
}
.set-input::-webkit-inner-spin-button,
.set-input::-webkit-outer-spin-button {
  -webkit-appearance: none;
}
.set-input:focus {
  border-color: var(--accent);
}
.set-input.previous {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 13px;
}

.btn-check-set {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  margin: 0 auto;
}
.btn-check-set.checked {
  background: var(--success);
  border-color: var(--success);
  color: white;
}
.btn-check-set svg {
  width: 18px;
  height: 18px;
}

.btn-add-set {
  display: block;
  width: 100%;
  padding: 8px;
  margin-top: 8px;
  background: transparent;
  border: 1px dashed var(--border);
  border-radius: 6px;
  color: var(--text-secondary);
  font-size: 13px;
  cursor: pointer;
  transition: border-color 0.2s;
}
.btn-add-set:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* === History Filter === */
.history-filter {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}
.filter-btn {
  flex: 1;
  padding: 8px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  text-align: center;
  -webkit-tap-highlight-color: transparent;
}
.filter-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

/* === History === */
.history-card {
  margin-bottom: 12px;
}
.history-date {
  font-size: 13px;
  color: var(--text-muted);
}
.history-exercises {
  margin-top: 10px;
}
.history-exercise-item {
  padding: 8px 0;
  border-bottom: 1px solid rgba(255,255,255,0.05);
}
.history-exercise-item:last-child {
  border-bottom: none;
}
.history-exercise-name {
  color: var(--accent);
  font-weight: 600;
  font-size: 14px;
  margin-bottom: 6px;
}
.history-sets-list {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}
.history-set {
  display: inline-flex;
  align-items: center;
  gap: 0;
  background: var(--bg-input);
  border-radius: 6px;
  font-size: 13px;
  overflow: hidden;
}
.history-set-num {
  color: var(--text-muted);
  font-weight: 700;
  padding: 5px 6px;
  background: rgba(255,255,255,0.03);
  font-size: 11px;
}
.history-set-weight {
  color: var(--accent);
  font-weight: 700;
  padding: 5px 6px;
}
.history-set-reps {
  color: var(--text-primary);
  padding: 5px 6px;
  font-weight: 500;
}
.history-duration {
  font-size: 13px;
  color: var(--accent);
  margin-top: 8px;
}

/* === Profile === */
.profile-section {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}
.profile-section h3 {
  font-size: 16px;
  margin-bottom: 8px;
}
.profile-section h4 {
  font-size: 14px;
  margin-bottom: 6px;
  margin-top: 12px;
}
.text-muted {
  color: var(--text-secondary);
  font-size: 13px;
}

.api-key-container {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
  flex-wrap: wrap;
}
.api-key-container code {
  flex: 1;
  padding: 8px 12px;
  background: var(--bg-primary);
  border-radius: 6px;
  font-size: 12px;
  word-break: break-all;
  min-width: 0;
}

.api-docs {
  margin-top: 16px;
}
code.block {
  display: block;
  padding: 8px 12px;
  background: var(--bg-primary);
  border-radius: 6px;
  font-size: 12px;
  margin: 6px 0;
}

.timer-setting {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 8px;
}
.timer-setting label {
  flex: 1;
  font-size: 14px;
}
.timer-setting input {
  width: 80px;
  text-align: center;
}

/* === Rest Timer Overlay === */
.rest-timer-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.9);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
  overscroll-behavior: contain;
}
.rest-timer-content {
  text-align: center;
}
.rest-timer-label {
  font-size: 16px;
  color: var(--text-secondary);
  margin-bottom: 16px;
}
.rest-timer-time {
  font-size: 72px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
  margin-bottom: 24px;
}
.rest-timer-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  align-items: center;
}
.btn-timer {
  background: var(--bg-input);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
}
.btn-timer-skip {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
  font-weight: 600;
}

/* === Overlays & Modals === */

.modal {
  position: fixed;
  inset: 0;
  background: var(--bg-primary);
  z-index: 300;
  display: flex;
  flex-direction: column;
}
#exercise-picker {
  z-index: 400;
}
.modal-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-full {
  height: 100%;
}
.modal-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  padding: 12px 16px;
  padding-top: calc(var(--safe-top) + 12px);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  background: var(--bg-primary);
}
.modal-header h3 {
  font-size: 18px;
}
.modal-footer {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.modal-footer input {
  flex: 1;
}

.btn-close, .btn-back {
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 28px;
  cursor: pointer;
  padding: 4px;
  line-height: 1;
}

.search-input {
  margin: 12px 16px;
  width: calc(100% - 32px) !important;
}

/* === Import Progress === */
.import-progress-bar-container {
  width: 100%;
  height: 8px;
  background: var(--bg-primary);
  border-radius: 4px;
  overflow: hidden;
}
.import-progress-bar {
  height: 100%;
  width: 0%;
  background: var(--accent);
  border-radius: 4px;
  transition: width 0.3s ease;
}

/* === Editor Sections === */
.editor-section {
  margin-bottom: 20px;
}
.editor-section-header {
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--accent);
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 8px;
}
.editor-notes {
  width: 100%;
  margin-top: 8px;
  padding: 8px 12px !important;
  font-size: 13px !important;
  font-style: italic;
  background: transparent !important;
  border: 1px solid var(--border) !important;
  border-radius: 6px !important;
}

/* === Exercise List === */
.exercise-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 16px;
}
.exercise-list-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 0;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.exercise-list-item:active {
  background: var(--bg-card);
}
.exercise-muscle-tag {
  font-size: 11px;
  padding: 2px 8px;
  background: rgba(233, 69, 96, 0.15);
  color: var(--accent);
  border-radius: 4px;
}

/* === Routine Editor === */
.routine-editor-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  padding-bottom: calc(var(--safe-bottom) + 32px);
}
.routine-exercise-item {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 12px;
}
.routine-exercise-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
.routine-exercise-header strong {
  color: var(--accent);
}
.routine-sets-config {
  display: flex;
  gap: 8px;
  align-items: center;
  font-size: 14px;
  color: var(--text-secondary);
}
.routine-sets-config input,
.routine-sets-config select {
  width: 60px;
  padding: 8px;
  text-align: center;
  font-size: 14px;
}
.routine-sets-config select {
  width: auto;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-primary);
  cursor: pointer;
}
.routine-sets-config span {
  color: var(--text-muted);
}
.routine-rest-config {
  display: flex;
  gap: 8px;
  align-items: center;
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid rgba(255,255,255,0.05);
}
.routine-rest-config input {
  width: 60px;
  padding: 6px;
  text-align: center;
  font-size: 13px;
}
.routine-rest-config svg {
  flex-shrink: 0;
}

/* === Animations === */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}
.card, .exercise-block {
  animation: fadeIn 0.2s ease;
}

/* === Scrollbar === */
::-webkit-scrollbar {
  width: 4px;
}
::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}

/* === Toasts === */
.toast {
  position: fixed;
  top: calc(var(--safe-top) + 16px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-card);
  color: var(--text-primary);
  padding: 12px 24px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  font-size: 14px;
  z-index: 500;
  animation: fadeIn 0.3s ease;
  pointer-events: none;
}
.toast.success {
  border-color: var(--success);
}
.toast.error {
  border-color: var(--danger);
}
