:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #23263a;
  --border: #2d3148;
  --accent: #6c63ff;
  --accent-hover: #8078ff;
  --text: #e8eaf0;
  --text-muted: #7b7fa8;
  --success: #2ecc71;
  --error: #e74c3c;
  --warning: #f39c12;
  --radius: 10px;
}

[data-theme="light"] {
  --bg: #f4f5f9;
  --surface: #ffffff;
  --surface2: #eef0f6;
  --border: #d4d7e8;
  --accent: #5a52cc;
  --accent-hover: #6c63ff;
  --text: #1a1d27;
  --text-muted: #6b6f92;
  --success: #1e8f4e;
  --error: #c0392b;
  --warning: #c87f00;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  min-height: 100vh;
}

header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 16px 24px;
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 32px;
}

.app-logo {
  display: flex;
  align-items: center;
  gap: 12px;
  white-space: nowrap;
  flex-shrink: 0;
}

.logo-icon {
  width: 52px;
  height: 26px;
  flex-shrink: 0;
  filter: drop-shadow(0 0 8px rgba(108, 99, 255, 0.5));
}

.app-title {
  display: flex;
  flex-direction: column;
  line-height: 1.15;
}

.app-subtitle {
  font-size: 10px;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 1px;
}

.app-name {
  font-size: 19px;
  font-weight: 700;
  color: var(--accent);
}

/* ── Login overlay ──────────────────────────────────────────────────── */
.login-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-box {
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.login-logo svg {
  width: 80px;
  height: 40px;
  filter: drop-shadow(0 0 12px rgba(108, 99, 255, 0.6));
  margin-bottom: 8px;
}

.login-app-name {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 28px;
}

.login-subtitle {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 1.2px;
}

.login-title {
  font-size: 26px;
  font-weight: 700;
  color: var(--text);
}

.login-box input {
  width: 100%;
  display: block;
  margin-bottom: 12px;
  font-size: 15px;
  min-height: 44px;
}

.btn-link {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  font-size: 13px;
  padding: 10px 0;
  display: block;
  margin: 4px auto 0;
  text-decoration: underline;
  text-underline-offset: 2px;
}
.btn-link:hover { color: var(--accent-hover); }

/* ── Header user badge ──────────────────────────────────────────────── */
.header-user {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-left: auto;
  flex-shrink: 0;
}

.header-username {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
}

.btn-sm {
  padding: 5px 12px;
  font-size: 12px;
  min-height: unset;
}

.theme-toggle {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  width: 32px;
  height: 32px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: color 0.15s, border-color 0.15s;
}
.theme-toggle:hover { color: var(--text); border-color: var(--accent); }

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

.tab-btn {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-muted);
  padding: 7px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: all 0.15s;
}
.tab-btn:hover { color: var(--text); background: var(--surface2); }
.tab-btn.active { color: var(--accent); border-color: var(--accent); background: rgba(108,99,255,0.08); }

main { max-width: 960px; margin: 0 auto; padding: 24px 16px; }

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

.hidden { display: none !important; }

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}
.panel-header h2 { font-size: 22px; font-weight: 700; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 12px;
}

.card h3 { font-size: 15px; font-weight: 600; margin-bottom: 12px; }
.card h4 { font-size: 13px; font-weight: 600; color: var(--text-muted); margin-bottom: 8px; }

/* Buttons */
.btn-primary {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 8px 18px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  transition: background 0.15s;
}
.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: background 0.15s;
}
.btn-secondary:hover { background: var(--border); }

.btn-danger {
  background: transparent;
  color: var(--error);
  border: 1px solid var(--error);
  padding: 5px 12px;
  border-radius: 5px;
  cursor: pointer;
  font-size: 12px;
}
.btn-danger:hover { background: rgba(231,76,60,0.1); }

.btn-large { width: 100%; padding: 12px; font-size: 15px; margin-top: 16px; }

/* Forms */
input, select, textarea {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 13px;
  outline: none;
  transition: border-color 0.15s;
}
input:focus, select:focus { border-color: var(--accent); }
input::placeholder { color: var(--text-muted); }

.form-row {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}
.form-row input, .form-row select { flex: 1; min-width: 140px; }
.form-row label { color: var(--text-muted); font-size: 12px; white-space: nowrap; }

.filter-row {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
}
.filter-row input { flex: 1; }
.filter-row select { min-width: 160px; }

/* Exercise grid */
.exercise-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 12px;
}

.exercise-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  cursor: default;
}
.exercise-card .ex-name { font-weight: 600; font-size: 14px; margin-bottom: 4px; }
.exercise-card .ex-cat {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 12px;
  margin-bottom: 6px;
}
.exercise-card .ex-desc { font-size: 12px; color: var(--text-muted); }

/* Category colours */
.cat-chest   { background: rgba(231,76,60,0.2);  color: #e74c3c; }
.cat-back    { background: rgba(52,152,219,0.2);  color: #3498db; }
.cat-legs    { background: rgba(46,204,113,0.2);  color: #2ecc71; }
.cat-shoulders { background: rgba(243,156,18,0.2); color: #f39c12; }
.cat-arms    { background: rgba(155,89,182,0.2);  color: #9b59b6; }
.cat-core    { background: rgba(26,188,156,0.2);  color: #1abc9c; }
.cat-cardio  { background: rgba(52,73,94,0.4);    color: #95a5a6; }

/* Plans */
.plan-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px;
  margin-bottom: 12px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}
.plan-card .plan-name { font-weight: 700; font-size: 16px; margin-bottom: 8px; }
.plan-card .plan-ex-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.plan-chip {
  background: var(--surface2);
  border: 1px solid var(--border);
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 12px;
  color: var(--text-muted);
}

/* Exercise picker inside plan form */
.exercise-picker {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 8px;
  max-height: 260px;
  overflow-y: auto;
  padding: 4px;
  margin-bottom: 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
}
.picker-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  user-select: none;
  transition: background 0.1s;
}
.picker-item:hover { background: var(--surface2); }
.picker-item input[type=checkbox] { accent-color: var(--accent); width: 15px; height: 15px; flex-shrink: 0; }
.picker-item span { font-size: 13px; }
.picker-item .picker-cat { font-size: 10px; color: var(--text-muted); }

.selected-exercises { border: 1px solid var(--border); border-radius: 6px; padding: 12px; background: var(--bg); }
#selected-list { padding-left: 20px; margin-top: 8px; }
#selected-list li { font-size: 13px; color: var(--text); line-height: 1.8; }

/* Session */
.session-exercise-block {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px;
  margin-bottom: 12px;
}
.session-exercise-block h4 {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 12px;
  display: flex;
  justify-content: space-between;
}
.sets-table { width: 100%; border-collapse: collapse; }
.sets-table th {
  text-align: left;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  padding: 4px 8px;
  border-bottom: 1px solid var(--border);
}
.sets-table td { padding: 6px 8px; }
.sets-table input { width: 80px; padding: 5px 8px; font-size: 13px; }
.sets-table .set-remove { background: none; border: none; color: var(--text-muted); cursor: pointer; font-size: 16px; padding: 0 4px; }
.sets-table .set-remove:hover { color: var(--error); }
.add-set-btn { font-size: 12px; margin-top: 8px; }

/* Progress */
.stats-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}
.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  text-align: center;
}
.stat-card .stat-value { font-size: 28px; font-weight: 700; color: var(--accent); }
.stat-card .stat-label { font-size: 11px; color: var(--text-muted); margin-top: 4px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; }

.chart-container {
  position: relative;
  height: 280px;
  margin-bottom: 16px;
}

/* History */
.history-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px;
  margin-bottom: 12px;
  cursor: pointer;
  transition: border-color 0.15s;
}
.history-card:hover { border-color: var(--accent); }
.history-card .hc-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.history-card .hc-date { font-weight: 700; font-size: 15px; }
.history-card .hc-plan { font-size: 12px; color: var(--text-muted); }
.history-sets { margin-top: 10px; display: none; }
.history-sets.open { display: block; }
.history-sets table { width: 100%; border-collapse: collapse; font-size: 13px; }
.history-sets th { text-align: left; color: var(--text-muted); font-size: 11px; font-weight: 600; padding: 4px 8px; border-bottom: 1px solid var(--border); }
.history-sets td { padding: 5px 8px; }
.history-sets tr:nth-child(even) td { background: rgba(255,255,255,0.02); }

/* Misc */
.hint { font-size: 12px; color: var(--text-muted); margin: 16px 0; }
.error { color: var(--error); font-size: 12px; margin-top: 6px; }
.success { color: var(--success); font-size: 13px; margin-top: 8px; }

.empty-state { text-align: center; padding: 48px 0; color: var(--text-muted); font-size: 14px; }
.empty-state .empty-icon { font-size: 40px; margin-bottom: 12px; }

/* ── Share modal ────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.modal-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  width: 100%;
  max-width: 340px;
}

.modal-box h3 { margin-bottom: 4px; }

.share-user-btn {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 14px;
  margin-top: 8px;
  cursor: pointer;
  font-size: 14px;
  color: var(--text);
  transition: border-color 0.15s;
}
.share-user-btn:hover { border-color: var(--accent); }
.share-user-btn .send-icon { color: var(--accent); font-size: 16px; }

/* ── Section label ──────────────────────────────────────────────────── */
.section-label {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-muted);
  margin: 20px 0 10px;
}

/* ── Shared plan card ───────────────────────────────────────────────── */
.shared-plan-card {
  background: var(--surface);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  padding: 16px 20px;
  margin-bottom: 12px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}
.shared-plan-card .shared-by {
  font-size: 11px;
  color: var(--accent);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 4px;
}

/* ── Plan sets editor ───────────────────────────────────────────────── */
#selected-list {
  padding-left: 0;
  margin-top: 8px;
  list-style: none;
}

.plan-set-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  margin-bottom: 10px;
  background: var(--surface);
}

.plan-set-header {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 8px;
}

.plan-sets-table input {
  width: 80px;
  padding: 4px 8px;
  font-size: 13px;
  min-height: unset;
}

/* ── Admin portal ───────────────────────────────────────────────────── */
.admin-user-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px;
  margin-bottom: 12px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}

.admin-username {
  font-weight: 700;
  font-size: 15px;
  margin-bottom: 4px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.admin-badge {
  font-size: 10px;
  font-weight: 700;
  background: rgba(108,99,255,0.15);
  color: var(--accent);
  padding: 2px 8px;
  border-radius: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.admin-meta {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.6;
}

.admin-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.app-footer {
  text-align: center;
  padding: 24px 16px;
  font-size: 12px;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
  margin-top: 32px;
}

/* ── Desktop: hide tab icons, show full labels ─────────────────────── */
.tab-btn .ti { display: none; }

/* ── Mobile: bottom tab bar ────────────────────────────────────────── */
@media (max-width: 768px) {
  body {
    -webkit-text-size-adjust: 100%;
  }

  header {
    padding: 8px 12px;
    padding-top: calc(8px + env(safe-area-inset-top, 0px));
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: static;
    gap: 8px;
    flex-wrap: nowrap;
    overflow: hidden;
  }

  .app-logo {
    gap: 8px;
    background: transparent;
    border-bottom: none;
    padding: 0;
    min-width: 0;
  }

  .app-subtitle { font-size: 9px; }
  .app-name { font-size: 16px; }
  .logo-icon { width: 42px; height: 21px; }

  .header-username { display: none; }
  .btn-sm { padding: 5px 10px; }
  .header-user { margin-left: auto; flex-shrink: 0; gap: 6px; }

  nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 200;
    background: var(--surface);
    border-top: 1px solid var(--border);
    display: flex;
    gap: 0;
    flex-wrap: nowrap;
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }

  .tab-btn {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3px;
    padding: 10px 4px 8px;
    border-radius: 0;
    border: none;
    font-size: 10px;
    font-weight: 600;
    min-height: 56px;
  }

  .tab-btn .ti {
    display: block;
    font-size: 18px;
    line-height: 1;
  }

  .tab-btn.active {
    background: rgba(108, 99, 255, 0.1);
    border: none;
    border-top: 2px solid var(--accent);
  }

  main {
    padding: 16px 12px;
    padding-bottom: calc(72px + env(safe-area-inset-bottom, 0px));
  }

  .exercise-grid { grid-template-columns: 1fr 1fr; }

  /* Larger tap targets for inputs and buttons */
  input, select {
    font-size: 16px; /* prevents iOS zoom on focus */
    min-height: 44px;
    padding: 10px 12px;
  }

  .btn-primary, .btn-secondary { min-height: 44px; font-size: 15px; }
  .btn-large { min-height: 52px; font-size: 16px; }

  .form-row { flex-direction: column; align-items: stretch; }
  .form-row input, .form-row select { min-width: unset; width: 100%; }
  .form-row label { margin-bottom: -6px; }

  .sets-table input { width: 72px; font-size: 15px; min-height: 38px; }

  .chart-container { height: 220px; }

  .stats-row { grid-template-columns: 1fr 1fr; }
  .stat-card .stat-value { font-size: 22px; }

  .panel-header { margin-bottom: 12px; }
  .panel-header h2 { font-size: 18px; }

  .plan-card {
    flex-direction: column;
    gap: 10px;
  }
  .plan-card > div:last-child {
    display: flex;
    gap: 8px;
    width: 100%;
  }
  .plan-card > div:last-child button {
    flex: 1;
  }

  .admin-user-card {
    flex-direction: column;
    gap: 10px;
  }
  .admin-actions {
    width: 100%;
  }
  .admin-actions button {
    flex: 1;
  }
}
