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

:root {
  --bg: #0f0f1a;
  --surface: #1a1a2e;
  --surface2: #22223a;
  --border: #2a2a42;
  --accent: #6c63ff;
  --accent-dim: rgba(108,99,255,.18);
  --green: #4ade80;
  --green-dim: rgba(74,222,128,.15);
  --red: #f87171;
  --red-dim: rgba(248,113,113,.15);
  --text: #e2e8f0;
  --muted: #64748b;
  --muted2: #94a3b8;
  --radius: 14px;
  --radius-sm: 8px;
  --header-h: 56px;
  --nav-h: 66px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html { height: 100%; background: var(--bg); }

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

#app { height: 100%; position: relative; }

/* ── Screen system ────────────────────────────────────────── */
.screen {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  overflow: hidden;
}
.screen.hidden { display: none; }

/* ── Center content (auth/setup screens) ─────────────────── */
.center-content {
  flex: 1;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 32px 24px;
  gap: 12px;
  text-align: center;
}

.app-logo { font-size: 64px; line-height: 1; }
.center-content h1 { font-size: 2rem; font-weight: 700; letter-spacing: -.5px; }
.subtitle { color: var(--muted2); font-size: .95rem; }

/* ── Cards ────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

.setup-card {
  width: 100%; max-width: 420px;
  display: flex; flex-direction: column; gap: 12px;
  text-align: left;
  margin-top: 8px;
}
.setup-card h2 { font-size: 1.1rem; }
.setup-card p { color: var(--muted2); font-size: .875rem; line-height: 1.5; }
.setup-card label { font-size: .8rem; font-weight: 600; color: var(--muted2); text-transform: uppercase; letter-spacing: .06em; }
.setup-help-text { font-size: .78rem; color: var(--muted); text-align: center; }

.auth-note { font-size: .82rem; color: var(--muted); margin-top: 4px; }

/* ── Inputs ───────────────────────────────────────────────── */
input[type="text"], input[type="number"], select, textarea {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: .95rem;
  padding: 10px 12px;
  width: 100%;
  outline: none;
  transition: border-color .15s;
  -webkit-appearance: none;
  appearance: none;
}
input[type="text"]:focus, input[type="number"]:focus, select:focus {
  border-color: var(--accent);
}
input[type="number"] { text-align: center; }
select { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 10px center; padding-right: 28px; }

/* ── Buttons ──────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: none; border-radius: var(--radius-sm);
  font-size: .9rem; font-weight: 600;
  padding: 10px 18px; cursor: pointer;
  transition: opacity .15s, transform .1s;
  white-space: nowrap;
}
.btn:active { transform: scale(.97); }

.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { opacity: .9; }
.btn-success { background: var(--green); color: #0a1a0f; }
.btn-outline { background: transparent; color: var(--accent); border: 1.5px solid var(--accent); }
.btn-ghost { background: transparent; color: var(--muted2); border: 1.5px solid var(--border); }
.btn-danger { background: var(--red-dim); color: var(--red); border: 1px solid var(--red); }
.btn-block { width: 100%; }
.btn-lg { padding: 14px 28px; font-size: 1rem; border-radius: var(--radius); }
.btn-sm { padding: 6px 12px; font-size: .8rem; }
.btn-icon {
  background: transparent; border: none; color: var(--muted2);
  cursor: pointer; padding: 6px; border-radius: 8px;
  display: flex; align-items: center;
  transition: color .15s;
}
.btn-icon:hover { color: var(--text); }

/* ── Header ───────────────────────────────────────────────── */
.app-header {
  height: var(--header-h);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  position: relative; z-index: 10;
  flex-shrink: 0;
  padding-top: env(safe-area-inset-top, 0);
}
.app-header-title { font-size: 1.1rem; font-weight: 700; letter-spacing: -.3px; }
.header-right { display: flex; align-items: center; gap: 8px; }

.sync-indicator {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--muted);
  transition: background .3s;
  display: block;
}
.sync-indicator.saving { background: #facc15; }
.sync-indicator.saved { background: var(--green); }
.sync-indicator.error { background: var(--red); }

/* ── View container ───────────────────────────────────────── */
.view-container {
  flex: 1;
  overflow: hidden;
  position: relative;
}
.view {
  position: absolute; inset: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: calc(var(--nav-h) + var(--safe-bottom) + 16px);
}
.view.hidden { display: none; }
.view.active { display: block; }

.view-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 20px 16px 12px;
  position: sticky; top: 0;
  background: var(--bg);
  z-index: 5;
}
.view-header h2 { font-size: 1.3rem; font-weight: 700; }

/* ── Bottom nav ───────────────────────────────────────────── */
.bottom-nav {
  position: absolute;
  bottom: 0; left: 0; right: 0;
  height: calc(var(--nav-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  z-index: 20;
}
.nav-item {
  flex: 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px;
  background: transparent; border: none;
  color: var(--muted); font-size: .68rem; font-weight: 500;
  cursor: pointer; padding: 8px 4px;
  transition: color .15s;
}
.nav-item span { line-height: 1; }
.nav-item.active { color: var(--accent); }
.nav-item svg { transition: transform .15s; }
.nav-item.active svg { transform: scale(1.1); }

/* ── TODAY view ───────────────────────────────────────────── */
.today-date { color: var(--muted2); font-size: .85rem; }

.day-cards { display: flex; flex-direction: column; gap: 10px; padding: 0 16px 16px; }

.day-card {
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  cursor: pointer;
  transition: border-color .15s, background .15s;
  display: flex; align-items: center; justify-content: space-between;
}
.day-card:hover, .day-card:active { border-color: var(--accent); background: var(--accent-dim); }
.day-card-info h3 { font-size: 1rem; font-weight: 600; }
.day-card-info p { font-size: .8rem; color: var(--muted2); margin-top: 3px; }
.day-card-chevron { color: var(--muted); }
.day-card-last { font-size: .75rem; color: var(--muted); margin-top: 4px; }

.session-header {
  display: flex; align-items: flex-start; justify-content: space-between;
  padding: 16px 16px 8px;
  position: sticky; top: 0; z-index: 5;
  background: var(--bg);
}
.session-header h2 { font-size: 1.1rem; font-weight: 700; }
.session-timer { font-size: .85rem; color: var(--accent); font-variant-numeric: tabular-nums; font-weight: 500; }

.session-exercises { display: flex; flex-direction: column; gap: 12px; padding: 4px 16px 8px; }

.exercise-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.exercise-card-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px;
  cursor: pointer;
}
.exercise-card-header h4 { font-size: .95rem; font-weight: 600; }
.prescription-badge {
  display: inline-block;
  font-size: .7rem; font-weight: 600;
  background: var(--accent-dim); color: var(--accent);
  border: 1px solid rgba(108,99,255,.35);
  border-radius: 4px; padding: 1px 5px;
  vertical-align: middle; margin-left: 6px;
  letter-spacing: .02em;
}
.exercise-prev { font-size: .75rem; color: var(--muted); margin-top: 2px; }
.exercise-card-body { padding: 0 14px 12px; }

.sets-table { width: 100%; border-collapse: collapse; }
.sets-table th {
  font-size: .72rem; color: var(--muted); font-weight: 600;
  text-transform: uppercase; letter-spacing: .06em;
  text-align: center; padding: 6px 4px;
}
.sets-table th:first-child { text-align: left; width: 32px; }
.sets-table td { padding: 4px 2px; text-align: center; }
.sets-table td:first-child { text-align: left; color: var(--muted); font-size: .85rem; font-weight: 600; }

.set-input-wrap { display: flex; align-items: center; gap: 4px; }
.set-input-wrap input[type="number"] {
  width: 64px; padding: 7px 4px; font-size: .9rem;
}
.stepper { display: flex; }
.stepper button {
  background: var(--surface2); border: 1px solid var(--border);
  color: var(--muted2); width: 24px; height: 32px;
  cursor: pointer; font-size: 1rem; line-height: 1;
  transition: background .1s, color .1s;
}
.stepper button:first-child { border-radius: 4px 0 0 4px; border-right: none; }
.stepper button:last-child { border-radius: 0 4px 4px 0; border-left: none; }
.stepper button:hover { background: var(--border); color: var(--text); }

.set-delete-btn {
  background: transparent; border: none; color: var(--muted);
  cursor: pointer; font-size: 1rem; padding: 4px; line-height: 1;
  transition: color .15s;
}
.set-delete-btn:hover { color: var(--red); }

.add-set-row { margin-top: 10px; }

/* ── HISTORY view ─────────────────────────────────────────── */
.history-list { display: flex; flex-direction: column; gap: 10px; padding: 0 16px; }

.history-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  cursor: pointer;
  transition: border-color .15s;
}
.history-item:hover { border-color: var(--accent); }
.history-item-header { display: flex; justify-content: space-between; align-items: flex-start; }
.history-item-header h3 { font-size: .95rem; font-weight: 600; }
.history-item-date { font-size: .8rem; color: var(--muted2); }
.history-item-stats { display: flex; gap: 16px; margin-top: 8px; }
.history-stat { font-size: .78rem; color: var(--muted); }
.history-stat strong { color: var(--muted2); }

.empty-state {
  text-align: center; color: var(--muted); font-size: .9rem;
  padding: 48px 24px;
}

/* ── BESTS / PROGRESS view ────────────────────────────────── */
.bests-selected-card {
  margin: 0 16px 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}
.bests-selected-card h3 { font-size: 1rem; font-weight: 600; margin-bottom: 10px; }
.bests-stats-row { display: flex; gap: 12px; }
.bests-stat-box {
  flex: 1; background: var(--surface2); border-radius: var(--radius-sm);
  padding: 10px; text-align: center;
}
.bests-stat-box .label { font-size: .7rem; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.bests-stat-box .value { font-size: 1.3rem; font-weight: 700; color: var(--accent); margin-top: 2px; }
.bests-stat-box .unit { font-size: .75rem; color: var(--muted2); }

.chart-section { padding: 0 16px 8px; }
.section-title { font-size: .95rem; font-weight: 600; color: var(--muted2); padding: 0 16px; margin-bottom: 4px; }

.chart-container {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 8px 4px;
  min-height: 160px;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
}
.chart-container svg { display: block; width: 100%; }

.bests-grid {
  display: grid; grid-template-columns: 1fr 1fr;
  gap: 8px; padding: 0 16px 16px;
}
.best-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
}
.best-card .exercise-name { font-size: .78rem; color: var(--muted2); margin-bottom: 4px; line-height: 1.3; }
.best-card .best-weight { font-size: 1.2rem; font-weight: 700; color: var(--text); }
.best-card .best-reps { font-size: .78rem; color: var(--muted); }
.best-card .best-1rm { font-size: .72rem; color: var(--accent); margin-top: 2px; }

/* ── PLAN view ────────────────────────────────────────────── */
.plan-days { display: flex; flex-direction: column; gap: 12px; padding: 0 16px 16px; }

.plan-day-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.plan-day-header {
  display: flex; align-items: center; gap: 8px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}
.plan-day-name-input {
  flex: 1; background: transparent; border: none !important;
  font-size: .95rem; font-weight: 600; color: var(--text);
  padding: 0; border-radius: 0 !important;
}
.plan-day-name-input:focus { outline: 2px solid var(--accent); border-radius: 4px !important; padding: 2px 4px; }
.plan-day-delete-btn {
  background: transparent; border: none; color: var(--muted);
  cursor: pointer; font-size: .85rem; padding: 4px 6px;
  border-radius: 6px; transition: background .15s, color .15s;
  flex-shrink: 0;
}
.plan-day-delete-btn:hover { background: var(--red-dim); color: var(--red); }

.plan-exercises { list-style: none; }
.plan-exercise-item {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--border);
}
.plan-exercise-item:last-child { border-bottom: none; }
.plan-exercise-fields { flex: 1; display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.plan-exercise-name-input {
  font-size: .88rem; font-weight: 500; padding: 4px 6px !important;
}
.plan-exercise-pres-input {
  font-size: .75rem; color: var(--accent) !important; padding: 3px 6px !important;
  width: 100px !important;
}
.plan-exercise-move {
  display: flex; flex-direction: column; gap: 1px;
}
.plan-exercise-move button {
  background: transparent; border: none; color: var(--muted);
  cursor: pointer; font-size: .75rem; padding: 2px 4px;
  line-height: 1; transition: color .1s;
}
.plan-exercise-move button:hover { color: var(--text); }
.plan-exercise-delete {
  background: transparent; border: none; color: var(--muted);
  cursor: pointer; font-size: 1rem; padding: 2px 6px; line-height: 1;
  transition: color .15s;
}
.plan-exercise-delete:hover { color: var(--red); }

.plan-add-exercise {
  display: flex; gap: 6px; flex-wrap: wrap;
  padding: 10px 14px;
  border-top: 1px solid var(--border);
}
.plan-add-exercise .add-exercise-input { flex: 2; min-width: 120px; }
.plan-add-exercise .add-exercise-pres-input { flex: 1; min-width: 80px; }
.plan-add-exercise button { flex-shrink: 0; padding: 8px 12px; }

/* ── User menu ────────────────────────────────────────────── */
.overlay {
  position: fixed; inset: 0; z-index: 30;
  background: transparent;
}
.overlay.hidden { display: none; }

.user-menu {
  position: fixed; top: calc(var(--header-h) + env(safe-area-inset-top, 0) + 4px);
  right: 12px; z-index: 40;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  min-width: 200px;
  box-shadow: 0 8px 32px rgba(0,0,0,.5);
  overflow: hidden;
}
.user-menu.hidden { display: none; }
.user-email-display { padding: 12px 16px; font-size: .82rem; color: var(--muted2); border-bottom: 1px solid var(--border); }
.user-menu-item {
  display: block; width: 100%; text-align: left;
  background: transparent; border: none;
  color: var(--text); font-size: .9rem;
  padding: 12px 16px; cursor: pointer;
  transition: background .12s;
}
.user-menu-item:hover { background: var(--surface); }

/* ── Modal ────────────────────────────────────────────────── */
.modal { position: fixed; inset: 0; z-index: 50; display: flex; align-items: flex-end; }
.modal.hidden { display: none; }
.modal-backdrop {
  position: absolute; inset: 0;
  background: rgba(0,0,0,.7);
  backdrop-filter: blur(2px);
}
.modal-sheet {
  position: relative; z-index: 1;
  background: var(--surface);
  border-radius: var(--radius) var(--radius) 0 0;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: calc(24px + var(--safe-bottom));
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px 12px;
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--surface);
}
.modal-header h3 { font-size: 1rem; font-weight: 600; }
.modal-close {
  background: transparent; border: none; color: var(--muted);
  font-size: 1.1rem; cursor: pointer; padding: 4px 8px;
}
.modal-body { padding: 16px 20px; }

.modal-session-day { font-weight: 600; font-size: .95rem; color: var(--muted2); margin-bottom: 16px; }
.modal-exercise { margin-bottom: 20px; }
.modal-exercise h4 { font-size: .9rem; font-weight: 600; margin-bottom: 8px; }
.modal-sets { display: flex; flex-direction: column; gap: 4px; }
.modal-set {
  display: flex; gap: 8px; font-size: .85rem; color: var(--muted2);
  padding: 6px 10px;
  background: var(--surface2);
  border-radius: 6px;
}
.modal-set .set-num { color: var(--muted); width: 40px; }
.modal-set .set-weight { font-weight: 600; color: var(--text); }

/* ── Toast ────────────────────────────────────────────────── */
.toast {
  position: fixed; bottom: calc(var(--nav-h) + var(--safe-bottom) + 12px);
  left: 50%; transform: translateX(-50%);
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text); font-size: .88rem;
  padding: 10px 18px; border-radius: 99px;
  white-space: nowrap;
  z-index: 60;
  box-shadow: 0 4px 16px rgba(0,0,0,.4);
  transition: opacity .25s;
}
.toast.hidden { display: none; }

/* ── Misc helpers ─────────────────────────────────────────── */
.hidden { display: none !important; }
.mt-8 { margin-top: 8px; }

/* ── Backup section ───────────────────────────────────────── */
.backup-section { padding: 0 16px 32px; }
.backup-row { display: flex; gap: 10px; margin-top: 8px; }
.backup-row .btn { flex: 1; }

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