:root {
  --bg: #f3f5fa;
  --surface: #ffffff;
  --surface-alt: #f6f7fb;
  --border: #e5e8f0;
  --text: #14172a;
  --text-muted: #656d80;
  --primary: #6366f1;
  --primary-hover: #4f46e5;
  --primary-contrast: #ffffff;
  --danger: #dc2626;
  --shadow: 0 1px 2px rgba(20, 23, 42, 0.04), 0 6px 20px rgba(20, 23, 42, 0.06);
  --radius: 14px;
  --radius-sm: 8px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1120;
    --surface: #171a2e;
    --surface-alt: #1e2138;
    --border: #2a2e4a;
    --text: #eef0fa;
    --text-muted: #9aa4c0;
    --primary: #818cf8;
    --primary-hover: #a5b0fb;
    --primary-contrast: #14172a;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  line-height: 1.5;
}

h1, h2 { letter-spacing: -0.01em; }
h1 { font-size: 1.6rem; margin: 0 0 1.25rem; }
h2 { font-size: 1.05rem; margin: 0; }

a { color: var(--primary); }

/* Nav */
.nav {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.nav-inner {
  max-width: 780px;
  margin: 0 auto;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 0.6rem 1rem;
  padding: 0.9rem 1.5rem;
}
.nav-brand { font-weight: 700; text-decoration: none; color: var(--text); font-size: 1.05rem; }
.nav-links { display: flex; align-items: center; flex-wrap: wrap; gap: 0.5rem 1.15rem; }
.nav-links a {
  color: var(--text-muted);
  text-decoration: none;
  font-size: 0.92rem;
  font-weight: 500;
  padding: 0.35rem 0;
  border-bottom: 2px solid transparent;
  transition: color 0.15s ease, border-color 0.15s ease;
}
.nav-links a:hover { color: var(--text); }
.nav-links a.active { color: var(--primary); border-color: var(--primary); }
.nav-logout { margin: 0; }
.nav-logout button {
  background: none; border: none; color: var(--text-muted); cursor: pointer;
  font-size: 0.92rem; font-weight: 500; padding: 0; font-family: inherit;
}
.nav-logout button:hover { color: var(--danger); }

/* Layout */
.page {
  max-width: 780px;
  margin: 0 auto;
  padding: 2rem 1.5rem 4rem;
}

/* Auth pages */
.auth-shell {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}
.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 2rem 2rem 1.75rem;
}
.auth-card-wide { max-width: 460px; }
.auth-brand { font-weight: 700; color: var(--text-muted); font-size: 0.9rem; margin: 0 0 1.25rem; }
.auth-card h1 { margin-bottom: 0.35rem; }
.auth-subtitle { color: var(--text-muted); font-size: 0.92rem; margin: 0 0 1.5rem; }
.auth-footer { text-align: center; color: var(--text-muted); font-size: 0.9rem; margin: 1.5rem 0 0; }
.auth-footer a { font-weight: 600; text-decoration: none; }

.code-input {
  letter-spacing: 0.4em;
  text-align: center;
  font-size: 1.3rem;
  font-weight: 600;
}

/* Forms */
.form { display: flex; flex-direction: column; gap: 1rem; }
.form label { display: flex; flex-direction: column; gap: 0.4rem; font-size: 0.88rem; color: var(--text-muted); font-weight: 500; }
.label-optional { font-weight: 400; text-transform: none; letter-spacing: 0; opacity: 0.75; }
.form input, .form select {
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.95rem;
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.form input:focus, .form select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 20%, transparent);
}
.form-grid { max-width: 340px; }

.form .question-group { border: none; padding: 0; margin: 0 0 1.25rem; max-width: 420px; }
.form .question-group legend {
  padding: 0;
  margin: 0 0 0.6rem;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
}
.form .radio-option {
  flex-direction: row;
  align-items: center;
  gap: 0.6rem;
  padding: 0.65rem 0.85rem;
  margin-bottom: 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  color: var(--text);
  font-weight: 500;
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease;
}
.form .radio-option:last-child { margin-bottom: 0; }
.form .radio-option:hover { background: var(--surface-alt); }
.form .radio-option input[type="radio"] { accent-color: var(--primary); width: 16px; height: 16px; flex: 0 0 auto; }
.form .radio-option:has(input:checked) {
  border-color: var(--primary);
  background: color-mix(in srgb, var(--primary) 8%, transparent);
}

.btn-primary {
  padding: 0.65rem 1.1rem;
  background: var(--primary);
  color: var(--primary-contrast);
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  width: fit-content;
  transition: background 0.15s ease, transform 0.1s ease;
  font-family: inherit;
}
.btn-primary:hover { background: var(--primary-hover); }
.btn-primary:active { transform: scale(0.97); }
.btn-primary:disabled { opacity: 0.6; cursor: default; transform: none; }
.btn-block { width: 100%; }

.btn-secondary {
  display: inline-block;
  padding: 0.6rem 1rem;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  width: fit-content;
  transition: background 0.15s ease, transform 0.1s ease;
}
.btn-secondary:hover { background: var(--surface-alt); }
.btn-secondary:active { transform: scale(0.97); }
.btn-secondary:disabled { opacity: 0.6; cursor: default; transform: none; }
.photo-upload-label { margin-top: 0; }

.add-methods {
  margin-top: 1.1rem;
  padding-top: 1.1rem;
  border-top: 1px solid var(--border);
}
.add-methods .form.inline {
  margin-top: 0;
  padding-top: 0;
  border-top: none;
}
.add-method-label {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin: 0 0 0.5rem;
}
.add-methods-divider {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin: 1.25rem 0;
  color: var(--text-muted);
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.add-methods-divider::before,
.add-methods-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

.form.inline {
  flex-direction: row;
  flex-wrap: wrap;
  align-items: center;
  margin-top: 1.1rem;
  padding-top: 1.1rem;
  border-top: 1px solid var(--border);
}
.form.inline input, .form.inline select { flex: 1 1 130px; }
.form.inline .btn-primary { flex: 0 0 auto; }

.calories-input-group {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex: 1 1 220px;
}
.calories-input-group input { flex: 1 1 auto; min-width: 0; }
.calories-input-group .btn-secondary {
  flex: 0 0 auto;
  padding: 0.6rem 0.75rem;
  font-size: 0.82rem;
  white-space: nowrap;
}

.dob-fields { display: flex; gap: 0.4rem; }
.dob-fields select { flex: 1 1 45%; }
.dob-fields input[name="dobDay"] { flex: 1 1 25%; min-width: 0; }
.dob-fields input[name="dobYear"] { flex: 1 1 30%; min-width: 0; }

.form-hint {
  font-size: 0.78rem;
  color: var(--text-muted);
  margin: 0.6rem 0 0;
}

.error {
  color: var(--danger);
  background: color-mix(in srgb, var(--danger) 10%, transparent);
  border-radius: var(--radius-sm);
  padding: 0.6rem 0.8rem;
  font-size: 0.88rem;
  margin: 0 0 1rem;
}

.day-picker {
  display: flex;
  gap: 0.5rem;
  overflow-x: auto;
  margin-bottom: 1.5rem;
  padding-bottom: 0.2rem;
}
.day-pill {
  position: relative;
  flex: 0 0 auto;
  text-decoration: none;
  color: var(--text-muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.85rem;
  font-size: 0.85rem;
  font-weight: 600;
  white-space: nowrap;
  transition: background 0.12s ease, border-color 0.12s ease, transform 0.12s ease;
}
.day-pill:hover { background: var(--surface-alt); transform: translateY(-1px); }
.day-pill.selected {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--primary-contrast);
}
.day-pill.selected:hover { transform: none; }
.day-pill-dot {
  display: inline-block;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: currentColor;
  margin-left: 0.35rem;
  vertical-align: middle;
  opacity: 0.7;
}

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.4rem 1.5rem;
  margin-bottom: 1.25rem;
}

.store-ad img {
  display: block;
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 0.9rem;
}
.card-header-metric { color: var(--text-muted); font-size: 0.88rem; font-weight: 500; }
.card > h2 { margin-bottom: 0.9rem; }

/* Entry lists */
.entry-list { list-style: none; padding: 0; margin: 0 0 0.25rem; }
.entry-list li {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.65rem 0.5rem;
  margin: 0 -0.5rem;
  border-radius: var(--radius-sm);
  border-bottom: 1px solid var(--border);
  font-size: 0.92rem;
  transition: background 0.12s ease;
}
.entry-list li:not(.empty):hover { background: var(--surface-alt); }
.entry-list li:last-child { border-bottom: none; }
.entry-list li.empty {
  color: var(--text-muted);
  padding: 1rem 0.75rem;
  margin: 0;
  justify-content: center;
  text-align: center;
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.86rem;
}
.entry-value { font-weight: 600; }
.entry-note { color: var(--text-muted); font-size: 0.86rem; }
.inline-form { margin-left: auto; }
.link-button {
  background: none; border: none; color: var(--text-muted); cursor: pointer;
  font-size: 0.82rem; padding: 0; font-family: inherit;
}
.link-button:hover { color: var(--danger); }

.badge {
  display: inline-block;
  padding: 0.25rem 0.65rem;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: capitalize;
  min-width: 78px;
  text-align: center;
  background: var(--surface-alt);
  color: var(--text-muted);
}
.badge[data-meal="breakfast"] { background: color-mix(in srgb, #f59e0b 16%, transparent); color: #b45309; }
.badge[data-meal="lunch"] { background: color-mix(in srgb, #6366f1 16%, transparent); color: #4338ca; }
.badge[data-meal="dinner"] { background: color-mix(in srgb, #ec4899 16%, transparent); color: #be185d; }
.badge[data-meal="snack"] { background: color-mix(in srgb, #14b8a6 16%, transparent); color: #0f766e; }
.badge[data-meal="exercise"] { background: color-mix(in srgb, #16a34a 16%, transparent); color: #15803d; }
@media (prefers-color-scheme: dark) {
  .badge[data-meal="breakfast"] { color: #fbbf24; }
  .badge[data-meal="lunch"] { color: #a5b4fc; }
  .badge[data-meal="dinner"] { color: #f9a8d4; }
  .badge[data-meal="snack"] { color: #5eead4; }
  .badge[data-meal="exercise"] { color: #86efac; }
}

/* Stats */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 1rem; margin-bottom: 1.25rem; }
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.1rem;
  border-top: 3px solid var(--primary);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.stat:hover {
  transform: translateY(-2px);
  box-shadow: 0 2px 4px rgba(20, 23, 42, 0.06), 0 10px 28px rgba(20, 23, 42, 0.09);
}
@media (prefers-color-scheme: dark) {
  .stat:hover { box-shadow: 0 2px 4px rgba(0, 0, 0, 0.35), 0 12px 32px rgba(0, 0, 0, 0.4); }
}
.stat[data-accent="blue"] { border-top-color: #6366f1; }
.stat[data-accent="purple"] { border-top-color: #a855f7; }
.stat[data-accent="green"] { border-top-color: #16a34a; }
.stat[data-accent="orange"] { border-top-color: #ea580c; }
.stat-value { display: block; font-size: 1.7rem; font-weight: 700; letter-spacing: -0.02em; }
.stat-label { display: block; font-size: 0.8rem; color: var(--text-muted); margin-top: 0.3rem; }

.chart-wrap { height: 220px; }

.bmr-value {
  margin: 0.2rem 0 0.4rem;
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--primary);
}
.bmr-unit { font-size: 0.95rem; font-weight: 500; color: var(--text-muted); }
.bmr-caption { margin: 0 0 0.6rem; font-size: 0.85rem; color: var(--text-muted); }
.bmr-caption a { font-weight: 600; }
.bmr-caption:last-child { margin-bottom: 0; }

.chart-legend {
  display: flex;
  gap: 1.25rem;
  margin-bottom: 0.75rem;
}
.chart-legend-item {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.82rem;
  color: var(--text-muted);
  font-weight: 500;
}
.chart-legend-swatch {
  width: 10px;
  height: 10px;
  border-radius: 3px;
  display: inline-block;
}
.chart-legend-swatch[data-swatch="surplus"] { background: #e34948; }
.chart-legend-swatch[data-swatch="deficit"] { background: #2a78d6; }
@media (prefers-color-scheme: dark) {
  .chart-legend-swatch[data-swatch="surplus"] { background: #e66767; }
  .chart-legend-swatch[data-swatch="deficit"] { background: #3987e5; }
}

.reward-total {
  text-align: center;
  padding: 2rem 1.5rem;
  background: linear-gradient(135deg, var(--primary), #a855f7);
}
.reward-points {
  display: block;
  font-size: 3rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  color: #fff;
}
.reward-points-label {
  display: block;
  font-size: 0.9rem;
  font-weight: 600;
  color: rgba(255, 255, 255, 0.85);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-top: 0.2rem;
}

.subtitle { color: var(--text-muted); margin-top: -0.9rem; margin-bottom: 1.5rem; font-size: 0.92rem; }

.week-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.week-nav-btn {
  text-decoration: none;
  font-weight: 600;
  font-size: 0.88rem;
  color: var(--primary);
  padding: 0.4rem 0.7rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  white-space: nowrap;
  transition: background 0.12s ease;
}
.week-nav-btn:hover { background: var(--surface-alt); }
.week-nav-btn.disabled {
  color: var(--text-muted);
  pointer-events: none;
  opacity: 0.5;
}
.week-nav-label {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.2rem;
  font-weight: 600;
  font-size: 0.95rem;
  text-align: center;
}
.week-nav-current {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--primary);
  background: color-mix(in srgb, var(--primary) 14%, transparent);
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
}

.week-jump {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.6rem;
  margin-bottom: 1.5rem;
}
.week-jump label {
  font-size: 0.85rem;
  color: var(--text-muted);
  font-weight: 500;
}
.week-jump input[type="date"] {
  padding: 0.45rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
}
.week-jump .btn-primary {
  padding: 0.45rem 0.9rem;
  font-size: 0.88rem;
}

@media (max-width: 560px) {
  .nav-inner { flex-direction: column; gap: 0.6rem; align-items: flex-start; }
  .nav-links { gap: 1rem; flex-wrap: wrap; }
  .week-nav { flex-wrap: wrap; }
  .week-nav-btn { flex: 1 1 auto; text-align: center; }
  .week-jump { flex-wrap: wrap; }
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 17, 32, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  z-index: 100;
}
/* `[hidden]` and the bare class above have equal specificity, and this
   stylesheet loads after the browser's default one — without this rule,
   `display: flex` wins the tie and the modal shows on every page load. */
.modal-overlay[hidden] { display: none; }
.modal {
  position: relative;
  width: 100%;
  max-width: 420px;
  max-height: 90vh;
  overflow-y: auto;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.5rem;
}
.modal h2 { margin: 0 0 1rem; font-size: 1.15rem; }
.modal-close {
  position: absolute;
  top: 0.75rem;
  right: 0.85rem;
  background: none;
  border: none;
  font-size: 1.4rem;
  line-height: 1;
  color: var(--text-muted);
  cursor: pointer;
}
.modal-close:hover { color: var(--text); }
.modal-photo-preview {
  width: 100%;
  max-height: 220px;
  object-fit: cover;
  border-radius: var(--radius-sm);
  margin-bottom: 1rem;
  background: var(--surface-alt);
}
.modal-status {
  font-size: 0.9rem;
  color: var(--text-muted);
  margin: 0 0 1rem;
}
.modal-error {
  color: var(--danger);
  background: color-mix(in srgb, var(--danger) 10%, transparent);
  border-radius: var(--radius-sm);
  padding: 0.6rem 0.8rem;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 1rem;
  margin-top: 1.25rem;
}

.chat-card { display: flex; flex-direction: column; }
.chat-log {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  max-height: 420px;
  overflow-y: auto;
  padding-right: 0.25rem;
  margin-bottom: 1rem;
}
.chat-message {
  max-width: 80%;
  padding: 0.65rem 0.9rem;
  border-radius: var(--radius-sm);
  font-size: 0.92rem;
  line-height: 1.5;
}
.chat-message p { margin: 0; white-space: pre-wrap; }
.chat-message-user {
  align-self: flex-end;
  background: var(--primary);
  color: var(--primary-contrast);
}
.chat-message-assistant {
  align-self: flex-start;
  background: var(--surface-alt);
  border: 1px solid var(--border);
}
.chat-input-row { display: flex; gap: 0.6rem; }
.chat-input-row input {
  flex: 1;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  font-size: 0.95rem;
}
.chat-input-row input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 20%, transparent);
}

.coach-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 1.25rem;
}
.coach-header h1 { margin-bottom: 0; }
.lang-toggle { display: flex; gap: 0.4rem; }
.lang-toggle a {
  text-decoration: none;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-muted);
  padding: 0.35rem 0.7rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}
.lang-toggle a:hover { background: var(--surface-alt); }
.lang-toggle a.active {
  background: var(--primary);
  color: var(--primary-contrast);
  border-color: var(--primary);
}
