/* ============ Apex Recomp & Health Tracker — COMPLETE FINAL styles ============ */
:root {
  --bg: #0d1117;
  --surface: #161b22;
  --surface-2: #1c2129;
  --border: #30363d;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #4d6bfe;
  --accent-hover: #3b5bdb;
  --green: #2ea043;
  --orange: #d29922;
  --purple: #a371f7;
  --red: #f85149;
  --radius: 12px;
  --shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 0.8rem; }

/* ============ Buttons ============ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  border: none;
  border-radius: 8px;
  padding: 0.65rem 1.25rem;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
  white-space: nowrap;
}
.btn:disabled { opacity: 0.6; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-outline { background: transparent; color: var(--accent); border: 1px solid var(--accent); }
.btn-outline:hover { background: rgba(77, 107, 254, 0.1); }
.btn-ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); }
.btn-ghost:hover { color: var(--text); border-color: var(--muted); }
.btn-block { width: 100%; }
.icon-btn {
  background: transparent; border: none; color: var(--muted);
  cursor: pointer; font-size: 0.9rem; padding: 0.25rem 0.5rem; border-radius: 6px;
  flex-shrink: 0;
}
.icon-btn:hover { color: var(--red); background: rgba(248, 81, 73, 0.1); }

/* ============ Auth ============ */
#auth-view {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; padding: 1.5rem;
}
.auth-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 2.25rem; width: 100%; max-width: 400px;
}
.app-logo { font-size: 1.8rem; letter-spacing: -0.5px; text-align: center; }
.app-logo span { color: var(--accent); }
.tagline { text-align: center; color: var(--muted); margin-bottom: 1.5rem; font-size: 0.9rem; }

.auth-tabs { display: flex; gap: 0.5rem; margin-bottom: 1.5rem; background: var(--surface-2); padding: 0.3rem; border-radius: 8px; }
.auth-tab {
  flex: 1; padding: 0.5rem; border: none; border-radius: 6px; cursor: pointer;
  background: transparent; color: var(--muted); font-weight: 600; font-size: 0.9rem;
}
.auth-tab.active { background: var(--accent); color: #fff; }

.field-label { display: block; margin: 0.9rem 0 0.35rem; font-size: 0.85rem; color: var(--muted); }
input, textarea, select {
  width: 100%; padding: 0.65rem 0.85rem; border-radius: 8px;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  font-size: 0.95rem; transition: border-color 0.15s;
}
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--accent); }
.form-error { color: var(--red); margin: 0.75rem 0; font-size: 0.85rem; }

/* ============ Top bar + nav ============ */
.topbar {
  display: flex; justify-content: space-between; align-items: center;
  gap: 0.75rem;
  padding: 0.85rem 1.25rem; border-bottom: 1px solid var(--border);
  background: var(--surface); position: sticky; top: 0; z-index: 50;
}
.topbar-brand { font-weight: 700; font-size: 1.1rem; white-space: nowrap; }
.topbar-brand span { color: var(--accent); }

.nav-tabs { display: flex; gap: 0.3rem; background: var(--surface-2); padding: 0.3rem; border-radius: 8px; }
.nav-tab {
  padding: 0.45rem 1rem; border: none; border-radius: 6px; cursor: pointer;
  background: transparent; color: var(--muted); font-weight: 600; font-size: 0.9rem;
  white-space: nowrap;
}
.nav-tab.active { background: var(--accent); color: #fff; }

/* ============ Main layout ============ */
.dashboard, .gym-page, .history-page, .admin-page {
  display: grid;
  gap: 1.25rem;
  padding: 1.25rem 1.5rem 3rem;
  grid-template-columns: 1fr;
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
}

.macro-card, .meal-card, .settings-card, .trends-card,
.hist-card, .admin-stats-card, .admin-users-card,
.huawei-tiles, .huawei-note { grid-column: 1 / -1; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.5rem;
  min-width: 0;
}
.card-title { font-size: 1.05rem; margin-bottom: 0.25rem; }
.card-sub { color: var(--muted); font-size: 0.85rem; margin-bottom: 1rem; }
.card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.75rem; flex-wrap: wrap; gap: 0.5rem; }

/* ============ Macro bars ============ */
.macro-bars { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1.5rem; }
.macro-item { min-width: 0; }
.macro-head { display: flex; justify-content: space-between; font-size: 0.9rem; margin-bottom: 0.3rem; gap: 0.5rem; }
.macro-label { font-weight: 600; }
.macro-nums b { font-size: 1rem; }
.progress-track { height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.progress-fill { height: 100%; border-radius: 999px; transition: width 0.4s ease; }
.macro-remaining { font-size: 0.78rem; color: var(--muted); margin-top: 0.25rem; text-align: right; }

/* ============ Status badge ============ */
.status-badge {
  font-size: 0.7rem; font-weight: 700; padding: 0.25rem 0.7rem; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border);
  display: inline-block;
}
.status-badge.ok { color: var(--green); border-color: var(--green); }
.status-badge.err { color: var(--red); border-color: var(--red); }

/* ============ Meals ============ */
.meal-group {
  border: 1px solid var(--border); border-radius: 10px; overflow: hidden;
  margin-bottom: 1rem; background: var(--surface-2);
}
.meal-group-header {
  display: flex; align-items: center; gap: 0.75rem;
  padding: 0.7rem 1rem; background: var(--surface); border-bottom: 1px solid var(--border);
}
.meal-type-badge { font-weight: 700; font-size: 0.9rem; white-space: nowrap; }
.meal-group-totals { margin-left: auto; font-size: 0.8rem; color: var(--muted); white-space: nowrap; }
.meal-items { border-bottom: 1px solid var(--border); }
.meal-item-row {
  display: grid; grid-template-columns: 1fr auto auto; align-items: center;
  gap: 0.75rem; padding: 0.5rem 1rem; border-bottom: 1px solid var(--surface-2); font-size: 0.85rem;
}
.meal-item-row:last-child { border-bottom: none; }
.meal-item-name { line-height: 1.3; }
.meal-item-macros { color: var(--muted); white-space: nowrap; font-size: 0.8rem; }
.meal-item-empty { padding: 0.75rem 1rem; font-size: 0.85rem; }
.add-food-btn { margin: 0.5rem; }
.meal-total { margin-top: 0.75rem; font-size: 0.85rem; color: var(--muted); }

/* ============ Gym ============ */
.metrics-card, .workouts-card { grid-column: 1 / -1; }
.new-workout-row { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.new-workout-row input { width: auto; flex: 1; min-width: 140px; }
.new-workout-row input[type="time"] { width: auto; flex: 0 0 auto; min-width: 110px; }

.workout-card {
  border: 1px solid var(--border); border-radius: 10px; margin-bottom: 1rem;
  background: var(--surface-2); overflow: hidden;
}
.workout-header {
  display: flex; align-items: center; gap: 0.75rem;
  padding: 0.7rem 1rem; background: var(--surface); border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.workout-header-info { display: flex; flex-direction: column; gap: 0.1rem; }
.workout-date { font-weight: 700; }
.workout-time { font-size: 0.75rem; }
.workout-volume { margin-left: auto; font-size: 0.8rem; color: var(--muted); white-space: nowrap; }
.complete-workout-btn { color: var(--green); border-color: var(--green); }
.complete-workout-btn:hover { background: rgba(46, 160, 67, 0.1); color: var(--green); border-color: var(--green); }
.workout-exercises { padding: 0 0.75rem; }

.exercise-block {
  border: 1px solid var(--surface-2); border-radius: 8px; margin: 0.75rem 0;
  background: var(--surface); overflow: hidden;
}
.exercise-header { display: flex; justify-content: space-between; align-items: center; padding: 0.55rem 0.75rem; }
.exercise-name { font-weight: 600; font-size: 0.9rem; }

.set-table { border-top: 1px solid var(--surface-2); }
.set-row {
  display: grid; grid-template-columns: 0.6fr 1fr 0.7fr 1fr auto;
  align-items: center; gap: 0.4rem; padding: 0.35rem 0.75rem;
  font-size: 0.82rem; border-bottom: 1px solid var(--surface-2);
}
.set-row:last-child { border-bottom: none; }
.set-row-head { color: var(--muted); font-weight: 600; font-size: 0.75rem; }
.set-empty { padding: 0.5rem 0.75rem; font-size: 0.8rem; }

.add-set-row { display: flex; gap: 0.4rem; padding: 0.5rem 0.75rem; border-top: 1px solid var(--surface-2); flex-wrap: wrap; }
.add-set-row input { flex: 1; min-width: 60px; }

.add-exercise-row { display: flex; gap: 0.5rem; padding: 0.75rem; flex-wrap: wrap; }
.add-exercise-row input { flex: 1; min-width: 140px; }

.trend-select { max-width: 280px; }
.trend-title { font-size: 1rem; margin-bottom: 0.75rem; }
.trend-chart-wrap { margin-bottom: 1rem; }
.trend-chart { width: 100%; height: 160px; display: block; }
.trend-chart-title { margin-bottom: 0.4rem; }
.trend-chart-labels { display: flex; justify-content: space-between; font-size: 0.7rem; color: var(--muted); }

/* ============ Vitals ============ */
.vitals-form { display: flex; flex-direction: column; gap: 0.9rem; margin-bottom: 1.25rem; }
.vitals-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.5rem; }
.vitals-grid label { font-size: 0.75rem; color: var(--muted); }
.vitals-grid input { margin-top: 0.25rem; }

.table-wrap { overflow-x: auto; margin-top: 0.5rem; }
.data-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.data-table th { text-align: left; color: var(--muted); font-weight: 600; padding: 0.5rem 0.4rem; border-bottom: 1px solid var(--border); white-space: nowrap; }
.data-table td { padding: 0.5rem 0.4rem; border-bottom: 1px solid var(--surface-2); white-space: nowrap; }
.notes-cell { max-width: 200px; white-space: normal !important; line-height: 1.3; color: var(--muted); }

/* ============ History tables ============ */
.hist-trend { margin-top: 1.5rem; }
.history-table th { font-size: 0.75rem; }
.history-table td { font-size: 0.82rem; }

.meal-type-cell { font-weight: 600; vertical-align: top; background: var(--surface-2); }
.exercise-cell { font-weight: 600; vertical-align: top; max-width: 180px; white-space: normal !important; }

.meal-subtotal-row td { background: var(--surface-2); font-size: 0.78rem; }
.meal-grand-total-row td { background: var(--surface); font-size: 0.85rem; border-top: 2px solid var(--border); }
.exercise-subtotal-row td { background: var(--surface-2); font-size: 0.78rem; }

.hist-workout-block {
  border: 1px solid var(--border); border-radius: 8px; margin-bottom: 1rem;
  overflow: hidden; background: var(--surface-2);
}
.hist-workout-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 0.6rem 0.85rem; background: var(--surface);
  border-bottom: 1px solid var(--border); font-weight: 600; font-size: 0.9rem;
}
.hist-workout-footer {
  padding: 0.5rem 0.85rem; font-size: 0.8rem;
  color: var(--muted); border-top: 1px solid var(--border);
}

/* ============ Modals ============ */
.modal { position: fixed; inset: 0; z-index: 100; display: flex; align-items: center; justify-content: center; padding: 1.25rem; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.6); }
.modal-card {
  position: relative; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.5rem;
  width: 100%; max-width: 520px; max-height: 90vh; overflow-y: auto;
}
.modal-small { max-width: 380px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem; }
.modal-head h3 { font-size: 1.05rem; }

.meal-type-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.6rem; }
.meal-type-btn {
  padding: 1rem 0.75rem; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); font-size: 0.95rem;
  font-weight: 600; cursor: pointer; transition: border-color 0.15s, background 0.15s;
}
.meal-type-btn:hover { border-color: var(--accent); background: rgba(77, 107, 254, 0.1); }

/* ============ Profile modal ============ */
.profile-form { display: flex; flex-direction: column; gap: 0.9rem; }
.profile-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.6rem; }
.profile-grid .field-label { margin: 0; }
.profile-grid input { margin-top: 0.25rem; }

/* ============ Food form ============ */
.food-form { display: flex; flex-direction: column; gap: 0.9rem; }
.serving-row {
  display: flex; align-items: flex-end; gap: 0.6rem; padding: 0.6rem 0.8rem;
  border-radius: 8px; background: var(--surface-2); border: 1px solid var(--border); flex-wrap: wrap;
}
.serving-row .field-label { margin: 0; flex: 1; min-width: 120px; }
.serving-unit { color: var(--muted); font-size: 0.85rem; padding-bottom: 0.65rem; }
.serving-note { color: var(--accent); font-size: 0.8rem; padding-bottom: 0.65rem; white-space: nowrap; }

.food-results { display: flex; flex-direction: column; gap: 0.4rem; max-height: 240px; overflow-y: auto; }
.food-empty {
  padding: 0.75rem; border-radius: 8px; font-size: 0.85rem;
  background: var(--surface-2); border: 1px dashed var(--border); color: var(--muted); text-align: center;
}
.food-result {
  display: flex; flex-direction: column; gap: 0.3rem; text-align: left;
  padding: 0.65rem 0.85rem; border-radius: 8px; cursor: pointer;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  transition: border-color 0.15s;
}
.food-result:hover { border-color: var(--accent); }
.food-result-name { display: flex; flex-direction: column; gap: 0.1rem; }
.food-result-name b { font-size: 0.9rem; line-height: 1.3; }
.food-result-name small { color: var(--muted); }
.food-source { font-size: 0.7rem; font-weight: 600; }
.food-source.complete { color: var(--green); }
.food-source.partial { color: var(--orange); }
.food-macros { font-size: 0.8rem; color: var(--muted); display: flex; flex-wrap: wrap; gap: 0.3rem; align-items: center; }
.food-macros small { opacity: 0.7; }

.meal-macro-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 0.5rem; }
.meal-macro-grid label { font-size: 0.75rem; color: var(--muted); }
.meal-macro-grid input { margin-top: 0.25rem; }

/* ============ Huawei settings ============ */
.settings-row { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }
.settings-row .settings-label { margin-left: auto; color: var(--muted); font-size: 0.85rem; white-space: nowrap; }

.switch { position: relative; display: inline-block; width: 48px; height: 26px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider {
  position: absolute; cursor: pointer; inset: 0;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; transition: 0.25s;
}
.slider:before {
  content: ""; position: absolute; height: 18px; width: 18px; left: 3px; top: 3px;
  background: var(--muted); border-radius: 50%; transition: 0.25s;
}
.switch input:checked + .slider { background: var(--accent); border-color: var(--accent); }
.switch input:checked + .slider:before { transform: translateX(22px); background: #fff; }

.mini-switch { position: relative; display: inline-block; width: 38px; height: 20px; }
.mini-switch input { opacity: 0; width: 0; height: 0; }
.mini-switch span {
  position: absolute; cursor: pointer; inset: 0;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 999px; transition: 0.2s;
}
.mini-switch span:before {
  content: ""; position: absolute; height: 14px; width: 14px;
  left: 2px; top: 2px; background: var(--muted);
  border-radius: 50%; transition: 0.2s;
}
.mini-switch input:checked + span { background: var(--accent); border-color: var(--accent); }
.mini-switch input:checked + span:before { transform: translateX(18px); background: #fff; }

/* ============ Huawei tiles ============ */
.huawei-tiles { display: grid; grid-template-columns: repeat(5, 1fr); gap: 1rem; }
.tile {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 1.25rem; min-width: 0;
}
.tile-title { font-size: 0.9rem; font-weight: 600; margin-bottom: 0.5rem; }
.tile-value { font-size: 1.7rem; font-weight: 700; line-height: 1.1; overflow-wrap: break-word; }
.tile-unit { font-size: 1rem; color: var(--muted); margin-left: 0.2rem; }
.tile-sub { font-size: 0.78rem; color: var(--muted); margin-top: 0.2rem; }

.mini-stage { margin-top: 0.45rem; }
.mini-stage-head { display: flex; justify-content: space-between; font-size: 0.75rem; margin-bottom: 0.2rem; }
.mini-stage-head b { font-weight: 600; }

.huawei-note { font-size: 0.8rem; color: var(--muted); margin-top: -0.25rem; }

/* ============ Admin stats (improved) ============ */
.admin-stats-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 0.9rem;
  margin-bottom: 1.5rem;
}

.admin-stat {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.25rem 1rem;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.admin-stat-value {
  font-size: 2rem;
  font-weight: 700;
  line-height: 1;
  color: var(--text);
}

.admin-stat-label {
  font-size: 0.75rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.admin-stats-extra {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  font-size: 0.9rem;
}

.admin-stats-extra p {
  margin: 0;
  color: var(--muted);
}

.admin-stats-extra b {
  color: var(--text);
}

/* ============ Responsive ============ */
@media (min-width: 900px) {
  .metrics-card, .workouts-card { grid-column: span 6; }
}

@media (max-width: 1100px) {
  .huawei-tiles { grid-template-columns: repeat(3, 1fr); }
  .admin-stats-grid { grid-template-columns: repeat(3, 1fr); }
}

@media (max-width: 900px) {
  .macro-bars { grid-template-columns: repeat(2, 1fr); }
  .huawei-tiles { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 640px) {
  .dashboard, .gym-page, .history-page, .admin-page { padding: 1rem; gap: 1rem; }
  .topbar { padding: 0.75rem 0.9rem; flex-wrap: wrap; gap: 0.5rem; }
  .nav-tab { padding: 0.4rem 0.7rem; font-size: 0.85rem; }
  .auth-card { padding: 1.5rem; }
  .macro-bars { grid-template-columns: 1fr; }
  .meal-macro-grid, .vitals-grid { grid-template-columns: repeat(2, 1fr); }
  .serving-row { flex-wrap: wrap; }
  .serving-unit, .serving-note { padding-bottom: 0; }
  .meal-item-row { grid-template-columns: 1fr auto; }
  .meal-item-macros { grid-column: 1 / -1; font-size: 0.75rem; }
  .meal-type-grid { grid-template-columns: 1fr; }
  .new-workout-row input { flex: 1 1 100%; }
  .set-row { grid-template-columns: 0.5fr 0.8fr 0.6fr 0.8fr auto; font-size: 0.75rem; }
  .profile-grid { grid-template-columns: 1fr; }
  .admin-stats-grid { grid-template-columns: repeat(2, 1fr); }
  .admin-stat-value { font-size: 1.5rem; }

  .huawei-tiles {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
  }
  .huawei-tiles .tile { width: 100%; }

  .settings-row {
    flex-direction: column;
    align-items: stretch;
    gap: 0.75rem;
  }
  .settings-row .settings-label { margin-left: 0; width: 100%; }
  .settings-row .switch,
  .settings-row .btn { align-self: flex-start; }

  .hist-workout-head {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.25rem;
  }
}

/* ============ Overview stats ============ */
.overview-stats-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 0.9rem;
}

.overview-stat {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.25rem 1rem;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.overview-stat-value {
  font-size: 1.6rem;
  font-weight: 700;
  line-height: 1;
  color: var(--text);
}

.overview-stat-label {
  font-size: 0.75rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.overview-chart-wrap {
  margin-top: 0.5rem;
}

/* ============ Nutrition page ============ */
.nutrition-page {
  display: grid;
  gap: 1.25rem;
  padding: 1.25rem 1.5rem 3rem;
  grid-template-columns: 1fr;
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
}

.meal-card {
  grid-column: 1 / -1;
}

/* ============ Watch page ============ */
.watch-page {
  display: grid;
  gap: 1.25rem;
  padding: 1.25rem 1.5rem 3rem;
  grid-template-columns: 1fr;
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
}

.settings-card,
.huawei-tiles,
.huawei-note {
  grid-column: 1 / -1;
}

/* ============ Responsive ============ */
@media (max-width: 1100px) {
  .overview-stats-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 640px) {
  .overview-stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  .overview-stat-value {
    font-size: 1.3rem;
  }
}