:root {
  --bg: #0d0d0d;
  --surface: #181818;
  --surface2: #222222;
  --border: #2a2a2a;
  --accent: #4ade80;
  --accent-dark: #16a34a;
  --accent-dim: rgba(74, 222, 128, 0.12);
  --text: #f0f0f0;
  --text-muted: #777;
  --danger: #ef4444;
  --danger-dim: rgba(239, 68, 68, 0.12);
  --warning: #f59e0b;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 2px 12px rgba(0, 0, 0, 0.5);
  --transition: 0.15s ease;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
* { -webkit-tap-highlight-color: transparent; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

img { max-width: 100%; height: auto; display: block; }

/* ---- Layout ---- */
.container { max-width: 720px; margin: 0 auto; padding: 0 16px; }

/* ---- Header / Nav ---- */
.header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
  padding-top: env(safe-area-inset-top, 0px);
}
.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 56px;
  max-width: 720px;
  margin: 0 auto;
  padding: 0 16px;
}
.header-logo {
  font-weight: 700;
  font-size: 18px;
  color: var(--accent);
  text-decoration: none;
  letter-spacing: -0.3px;
}
.header-logo:hover { text-decoration: none; }
.header-logo span { color: var(--text-muted); font-weight: 400; font-size: 14px; margin-left: 6px; }
.header-nav { display: flex; align-items: center; gap: 8px; }
.header-nav a, .header-nav button {
  font-size: 14px;
  color: var(--text-muted);
  background: none;
  border: none;
  cursor: pointer;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  transition: color var(--transition), background var(--transition);
}
.header-nav a:hover, .header-nav button:hover {
  color: var(--text);
  background: var(--surface2);
  text-decoration: none;
}
.header-nav a.active { color: var(--accent); }

.workout-badge {
  background: var(--accent-dim);
  color: var(--accent);
  border: 1px solid rgba(74, 222, 128, 0.25);
  border-radius: 100px;
  padding: 3px 10px;
  font-size: 12px;
  font-weight: 700;
}
.workout-badge:hover { background: rgba(74, 222, 128, 0.2); text-decoration: none; }

/* ---- Page ---- */
.page { padding: 24px 0 calc(80px + env(safe-area-inset-bottom, 0px)); }
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
  gap: 12px;
}
.page-title { font-size: 22px; font-weight: 700; letter-spacing: -0.3px; }
.page-subtitle { color: var(--text-muted); font-size: 14px; margin-top: 2px; }
.back-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--text-muted);
  font-size: 14px;
  margin-bottom: 16px;
}
.back-link:hover { color: var(--text); text-decoration: none; }

/* ---- Buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 18px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: all var(--transition);
  white-space: nowrap;
  text-decoration: none;
  touch-action: manipulation;
  user-select: none;
}
.btn:hover { text-decoration: none; }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

.btn-primary { background: var(--accent); color: #000; }
.btn-primary:hover { background: #6ee7a0; }

.btn-secondary { background: var(--surface2); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: #2e2e2e; border-color: #3a3a3a; }

.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-ghost:hover { background: var(--surface2); color: var(--text); }

.btn-danger { background: var(--danger-dim); color: var(--danger); border: 1px solid rgba(239, 68, 68, 0.2); }
.btn-danger:hover { background: rgba(239, 68, 68, 0.2); }

.btn-sm { padding: 6px 12px; font-size: 13px; }
.btn-lg { padding: 13px 28px; font-size: 16px; }
.btn-full { width: 100%; }
.btn-icon { padding: 8px; width: 36px; height: 36px; }

/* ---- Cards ---- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  transition: border-color var(--transition);
}
.card:hover { border-color: #3a3a3a; }
.card-link { cursor: pointer; }
.card-link:hover { border-color: var(--accent); }

.card-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.card-title { font-size: 16px; font-weight: 600; }
.card-subtitle { font-size: 13px; color: var(--text-muted); margin-top: 2px; }
.card-actions { display: flex; gap: 6px; flex-shrink: 0; }

.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 14px; }
.card-list { display: flex; flex-direction: column; gap: 10px; }

/* ---- Forms ---- */
.form-group { margin-bottom: 18px; }
.form-label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.form-input, .form-textarea, .form-select {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 16px;
  font-family: inherit;
  padding: 10px 14px;
  transition: border-color var(--transition);
  outline: none;
}
.form-input:focus, .form-textarea:focus, .form-select:focus {
  border-color: var(--accent);
}
.form-textarea { resize: vertical; min-height: 100px; }
.form-select { cursor: pointer; }
.form-select option { background: var(--surface2); }
.form-hint { font-size: 12px; color: var(--text-muted); margin-top: 4px; }
.form-error { font-size: 12px; color: var(--danger); margin-top: 4px; }

.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-row-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }

/* ---- Image Upload ---- */
.img-upload-area {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 32px 20px;
  text-align: center;
  cursor: pointer;
  transition: border-color var(--transition), background var(--transition);
  position: relative;
}
.img-upload-area:hover { border-color: var(--accent); background: var(--accent-dim); }
.img-upload-area input[type="file"] {
  position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; height: 100%;
}
.img-upload-icon { font-size: 40px; margin-bottom: 10px; }
.img-upload-text { color: var(--text-muted); font-size: 14px; }
.img-upload-text strong { color: var(--accent); }

.img-preview {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface2);
}
.img-preview img {
  width: 100%;
  max-height: 300px;
  object-fit: contain;
  background: #111;
}
.img-preview-remove {
  position: absolute;
  top: 8px; right: 8px;
  background: rgba(0,0,0,0.7);
  color: var(--danger);
  border: none;
  border-radius: 50%;
  width: 32px; height: 32px;
  cursor: pointer;
  font-size: 16px;
  display: flex; align-items: center; justify-content: center;
}

/* ---- Exercise row in routine ---- */
.exercise-row {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  transition: border-color var(--transition);
}
.exercise-row:hover { border-color: #3a3a3a; }
.exercise-row-thumb {
  width: 44px; height: 44px;
  border-radius: var(--radius-sm);
  object-fit: cover;
  background: var(--border);
  flex-shrink: 0;
}
.exercise-row-info { flex: 1; min-width: 0; }
.exercise-row-name { font-weight: 600; font-size: 14px; }
.exercise-row-meta { font-size: 13px; color: var(--text-muted); margin-top: 2px; }
.exercise-row-actions { display: flex; gap: 6px; }

/* ---- Set tracker (workout mode) ---- */
.set-tracker { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }
.set-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  transition: all var(--transition);
}
.set-row.done { border-color: var(--accent); background: var(--accent-dim); }
.set-row.done .set-label { color: var(--accent); }
.set-label { font-size: 13px; font-weight: 700; color: var(--text-muted); width: 28px; flex-shrink: 0; }
.set-input-group { display: flex; align-items: center; gap: 6px; flex: 1; }
.set-input {
  width: 70px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  font-size: 16px;
  font-weight: 600;
  text-align: center;
  padding: 5px 8px;
  outline: none;
  transition: border-color var(--transition);
}
.set-input:focus { border-color: var(--accent); }
.set-input.done-input { border-color: var(--accent); }
.set-sep { color: var(--text-muted); font-size: 13px; }
.set-check-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  cursor: pointer;
  padding: 6px 12px;
  font-size: 18px;
  transition: all var(--transition);
  margin-left: auto;
}
.set-check-btn:hover { border-color: var(--accent); color: var(--accent); }
.set-row.done .set-check-btn { background: var(--accent); border-color: var(--accent); color: #000; }

/* ---- Exercise card (library) ---- */
.ex-card { overflow: hidden; }
.ex-card-img {
  width: 100%;
  height: 160px;
  object-fit: cover;
  background: var(--surface2);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  font-size: 13px;
  border-radius: var(--radius) var(--radius) 0 0;
  margin: -20px -20px 16px;
  width: calc(100% + 40px);
}
.ex-card-img img { width: 100%; height: 100%; object-fit: cover; }

/* ---- Badge / Tag ---- */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 100px;
  font-size: 12px;
  font-weight: 600;
  background: var(--surface2);
  color: var(--text-muted);
  border: 1px solid var(--border);
}
.badge-accent { background: var(--accent-dim); color: var(--accent); border-color: rgba(74, 222, 128, 0.25); }

/* ---- Alert ---- */
.alert {
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  border: 1px solid transparent;
  margin-bottom: 16px;
}
.alert-error { background: var(--danger-dim); color: var(--danger); border-color: rgba(239, 68, 68, 0.2); }
.alert-success { background: var(--accent-dim); color: var(--accent); border-color: rgba(74, 222, 128, 0.2); }

/* ---- Loading ---- */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 48px;
  color: var(--text-muted);
  font-size: 14px;
  flex-direction: column;
  gap: 12px;
}
.spinner {
  width: 28px; height: 28px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- Empty state ---- */
.empty-state {
  text-align: center;
  padding: 56px 24px;
  color: var(--text-muted);
}
.empty-state-icon { font-size: 48px; margin-bottom: 16px; }
.empty-state-title { font-size: 18px; font-weight: 600; color: var(--text); margin-bottom: 6px; }
.empty-state-desc { font-size: 14px; margin-bottom: 20px; }

/* ---- Divider ---- */
.divider { height: 1px; background: var(--border); margin: 20px 0; }

/* ---- Modal / Overlay ---- */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000;
  padding: 16px;
}
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  width: 100%;
  max-width: 480px;
  max-height: 90dvh;
  overflow-y: auto;
}
.modal-title { font-size: 18px; font-weight: 700; margin-bottom: 16px; }

/* ---- Drag handle ---- */
.drag-handle {
  color: var(--text-muted);
  cursor: grab;
  font-size: 18px;
  padding: 0 4px;
  user-select: none;
}
.drag-handle:active { cursor: grabbing; }

/* ---- Workout exercise card ---- */
.workout-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 16px;
  transition: border-color var(--transition);
}
.workout-card.completed { border-color: var(--accent); }
.workout-card-header {
  padding: 16px 20px;
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  user-select: none;
}
.workout-card-header:hover { background: rgba(255,255,255,0.02); }
.workout-card-num {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: var(--surface2);
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700;
  color: var(--text-muted);
  flex-shrink: 0;
}
.workout-card.completed .workout-card-num { background: var(--accent); color: #000; }
.workout-card-info { flex: 1; }
.workout-card-name { font-size: 16px; font-weight: 700; }
.workout-card-meta { font-size: 13px; color: var(--text-muted); margin-top: 2px; }
.workout-card-toggle { color: var(--text-muted); font-size: 18px; transition: transform 0.2s; }
.workout-card-body { padding: 0 20px 20px; }
.workout-card-thumb {
  width: 100%;
  max-height: 200px;
  object-fit: contain;
  border-radius: var(--radius-sm);
  background: var(--surface2);
  margin-bottom: 14px;
}
.workout-card-tips {
  font-size: 13px;
  color: var(--text-muted);
  background: var(--surface2);
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  margin-bottom: 14px;
  white-space: pre-wrap;
  line-height: 1.6;
}
.workout-progress {
  font-size: 13px;
  color: var(--text-muted);
  text-align: center;
  margin-top: 24px;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.workout-progress-bar {
  height: 6px;
  background: var(--surface2);
  border-radius: 3px;
  margin: 10px 0 6px;
  overflow: hidden;
}
.workout-progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 3px;
  transition: width 0.4s ease;
}

/* ---- Login ---- */
.login-wrap {
  min-height: 100svh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(24px + env(safe-area-inset-top, 0px)) 24px calc(24px + env(safe-area-inset-bottom, 0px));
}
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 36px 32px;
}
.login-logo {
  text-align: center;
  margin-bottom: 28px;
}
.login-logo-text {
  font-size: 28px;
  font-weight: 800;
  color: var(--accent);
  letter-spacing: -0.5px;
}
.login-logo-sub { font-size: 13px; color: var(--text-muted); margin-top: 4px; }

/* ---- Floating action ---- */
.fab {
  position: fixed;
  bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  right: calc(24px + env(safe-area-inset-right, 0px));
  background: var(--accent);
  color: #000;
  border: none;
  border-radius: 50%;
  width: 56px; height: 56px;
  font-size: 28px;
  cursor: pointer;
  box-shadow: 0 4px 20px rgba(74, 222, 128, 0.35);
  display: flex; align-items: center; justify-content: center;
  transition: transform var(--transition), box-shadow var(--transition);
  z-index: 50;
  text-decoration: none;
  touch-action: manipulation;
}
.fab:hover { transform: scale(1.08); box-shadow: 0 6px 24px rgba(74, 222, 128, 0.45); text-decoration: none; color: #000; }
.fab:active { transform: scale(0.95); }

/* ---- Search ---- */
.search-bar {
  position: relative;
  margin-bottom: 20px;
}
.search-bar input {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 16px;
  font-family: inherit;
  padding: 10px 14px 10px 40px;
  outline: none;
  transition: border-color var(--transition);
}
.search-bar input:focus { border-color: var(--accent); }
.search-bar-icon {
  position: absolute;
  left: 14px; top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  font-size: 16px;
  pointer-events: none;
}

/* ---- Sortable ghost ---- */
.sortable-ghost { opacity: 0.4; }

/* ---- Scrollbar ---- */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #3a3a3a; }

/* ---- Responsive ---- */
@media (max-width: 600px) {
  .form-row { grid-template-columns: 1fr; }
  .form-row-3 { grid-template-columns: 1fr 1fr; }
  .card-grid { grid-template-columns: 1fr; }
  .page-header { flex-direction: column; align-items: flex-start; }
  .login-card { padding: 28px 20px; }
}
