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

:root {
  --bg:        #f3f0e8;
  --bg2:       #eae6db;
  --bg3:       #e1dcd0;
  --bg4:       #d7d1c4;
  --border:    rgba(0,0,0,0.08);
  --text:      #1a1a12;
  --muted:     #5e5a48;
  --dim:       #9a9480;
  --sage:      #3a7a28;
  --sage-l:    #7eb86a;
  --amber:     #a07020;
  --green:     #2a8a46;
  --red:       #c04040;
  --grad:      linear-gradient(135deg, #3d6b2c, #2a5a1e);
  --grad2:     linear-gradient(135deg, #7eb86a, #c8973a);
  --radius:    14px;
  --radius-sm: 9px;
  --shadow:    0 2px 16px rgba(0,0,0,0.08);
  --nav-h:     60px;
}

[data-theme="dark"] {
  --bg:     #0b0d09;
  --bg2:    #111410;
  --bg3:    #171b14;
  --bg4:    #1d231a;
  --border: rgba(255,255,255,0.07);
  --text:   #e8e3d4;
  --muted:  #7d7864;
  --dim:    #4a4638;
  --sage:   #7eb86a;
  --sage-l: #a0d090;
  --amber:  #c8973a;
  --green:  #5dba78;
  --red:    #e07070;
  --shadow: 0 2px 16px rgba(0,0,0,0.4);
}

html { scroll-behavior: smooth; }

body {
  font-family: 'Inter', -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  transition: background 0.2s, color 0.2s;
}

/* ── Nav ── */
.nav {
  position: sticky;
  top: 0; z-index: 100;
  height: var(--nav-h);
  display: flex;
  align-items: center;
  padding: 0 20px;
  gap: 16px;
  background: rgba(243,240,232,0.9);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border);
}

[data-theme="dark"] .nav { background: rgba(11,13,9,0.9); }

.nav-brand { display: flex; align-items: center; gap: 8px; text-decoration: none; flex-shrink: 0; }

.nav-mark {
  width: 28px; height: 28px;
  background: var(--grad);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 0 12px rgba(74,138,56,0.3);
}

.nav-mark svg { color: white; }
.nav-title { font-size: 16px; font-weight: 700; letter-spacing: 0.05em; color: var(--text); }

.nav-tabs { display: flex; gap: 2px; flex: 1; overflow-x: auto; scrollbar-width: none; }
.nav-tabs::-webkit-scrollbar { display: none; }

.nav-tab {
  background: none; border: none;
  color: var(--muted);
  font-family: inherit; font-size: 13px; font-weight: 500;
  padding: 6px 12px; border-radius: 8px;
  cursor: pointer; transition: all 0.15s;
  white-space: nowrap;
}

.nav-tab:hover { color: var(--text); background: rgba(0,0,0,0.04); }
[data-theme="dark"] .nav-tab:hover { background: rgba(255,255,255,0.05); }
.nav-tab.active { color: var(--sage); background: rgba(74,138,56,0.1); }

.nav-right { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }

.theme-btn, .nav-logout {
  width: 30px; height: 30px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: all 0.15s;
  font-size: 11px; font-family: inherit; font-weight: 500;
  text-decoration: none;
}

.theme-btn:hover, .nav-logout:hover { color: var(--text); border-color: rgba(74,138,56,0.3); }
.theme-btn svg, .nav-logout svg { width: 14px; height: 14px; }

/* ── Mobile menu ── */
.hamburger { display: none; flex-direction: column; gap: 4px; background: none; border: none; cursor: pointer; padding: 4px; }
.hamburger span { width: 20px; height: 2px; background: var(--muted); border-radius: 2px; display: block; transition: all 0.2s; }

.mobile-menu {
  display: none;
  flex-direction: column;
  position: fixed;
  top: var(--nav-h); left: 0; right: 0;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding: 8px 0;
  z-index: 99;
}

.mobile-menu.open { display: flex; }

.mobile-tab {
  background: none; border: none;
  color: var(--muted);
  font-family: inherit; font-size: 14px; font-weight: 500;
  padding: 12px 20px; text-align: left;
  cursor: pointer; transition: color 0.15s;
}

.mobile-tab.active { color: var(--sage); }

/* ── Main ── */
.main { max-width: 900px; margin: 0 auto; padding: 24px 20px 60px; }

.page { display: none; }
.page.active { display: block; }

.page-header { margin-bottom: 24px; }
.page-title { font-size: 22px; font-weight: 800; letter-spacing: -0.02em; }
.page-sub { font-size: 13px; color: var(--muted); margin-top: 2px; }

/* ── Trial Banner ── */
.trial-banner {
  background: rgba(74,138,56,0.08);
  border: 1px solid rgba(74,138,56,0.2);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  font-size: 13px;
  color: var(--sage);
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.trial-banner.expired {
  background: rgba(192,64,64,0.08);
  border-color: rgba(192,64,64,0.2);
  color: var(--red);
}

.trial-banner a {
  color: inherit; font-weight: 600; text-decoration: underline; white-space: nowrap;
}

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

.card-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 16px; gap: 12px;
}

.card-title { font-size: 15px; font-weight: 700; letter-spacing: -0.01em; }

/* ── Stat Grid ── */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  margin-bottom: 16px;
}

.stat-card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}

.stat-label { font-size: 11px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; margin-bottom: 6px; }
.stat-value { font-size: 22px; font-weight: 800; letter-spacing: -0.02em; margin-bottom: 3px; }
.stat-target { font-size: 11px; color: var(--dim); margin-bottom: 8px; }
.stat-bar { height: 4px; background: var(--bg3); border-radius: 2px; overflow: hidden; }
.stat-bar-fill { height: 100%; border-radius: 2px; background: var(--grad); transition: width 0.5s ease; }

/* ── Charts ── */
.chart-wrap { position: relative; height: 180px; }

/* ── Coach Card ── */
.coach-card { }
.advice-text { font-size: 14px; line-height: 1.75; color: var(--text); white-space: pre-line; }
.advice-cached { font-size: 11px; color: var(--dim); margin-top: 10px; }

/* ── Forms ── */
.form { display: flex; flex-direction: column; gap: 10px; }

.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.form-row.three { grid-template-columns: 1fr 1fr 1fr; }

.form-group { display: flex; flex-direction: column; gap: 4px; }
.form-label { font-size: 11px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }

.form-input, .form-select, .form-textarea {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 12px;
  font-family: inherit;
  font-size: 14px;
  color: var(--text);
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
  width: 100%;
}

.form-input::placeholder, .form-textarea::placeholder { color: var(--dim); }
.form-input:focus, .form-select:focus, .form-textarea:focus {
  border-color: rgba(74,138,56,0.45);
  box-shadow: 0 0 0 3px rgba(74,138,56,0.07);
}

.form-textarea { resize: vertical; min-height: 70px; }

/* ── Buttons ── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 9px 18px; border-radius: var(--radius-sm);
  font-family: inherit; font-size: 13px; font-weight: 600;
  cursor: pointer; transition: all 0.15s;
  text-decoration: none; border: none; letter-spacing: 0.01em;
}

.btn-primary { background: var(--grad); color: #fff; box-shadow: 0 3px 12px rgba(74,138,56,0.3); }
.btn-primary:hover { transform: translateY(-1px); box-shadow: 0 5px 18px rgba(74,138,56,0.4); }
.btn-primary:active { transform: translateY(0); }
.btn-primary:disabled { opacity: 0.6; cursor: not-allowed; transform: none; }

.btn-ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); }
.btn-ghost:hover { color: var(--text); border-color: rgba(74,138,56,0.3); }

.btn-sm { padding: 5px 10px; font-size: 12px; border-radius: 7px; }
.btn-full { width: 100%; }

/* ── Tables ── */
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th {
  padding: 8px 10px; text-align: left;
  font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.07em;
  color: var(--muted); border-bottom: 1px solid var(--border);
}
.data-table td { padding: 10px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.data-table tr:last-child td { border-bottom: none; }
.data-table tr:hover td { background: rgba(0,0,0,0.02); }
[data-theme="dark"] .data-table tr:hover td { background: rgba(255,255,255,0.02); }

/* ── Meal items ── */
.meal-item {
  display: flex; align-items: flex-start; gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.meal-item:last-child { border-bottom: none; }

.meal-photo { width: 56px; height: 56px; border-radius: 8px; object-fit: cover; flex-shrink: 0; }
.meal-photo-placeholder {
  width: 56px; height: 56px; border-radius: 8px;
  background: var(--bg3); border: 1px solid var(--border);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; color: var(--dim); font-size: 11px; text-align: center;
}

.meal-info { flex: 1; min-width: 0; }
.meal-name { font-size: 14px; font-weight: 600; }
.meal-macros { font-size: 12px; color: var(--muted); margin-top: 2px; }
.meal-notes { font-size: 12px; color: var(--dim); margin-top: 2px; font-style: italic; }
.meal-time { font-size: 11px; color: var(--dim); margin-top: 3px; }

/* ── Workout items ── */
.workout-date-group { margin-bottom: 16px; }
.workout-date-label {
  font-size: 11px; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.08em; color: var(--muted);
  padding-bottom: 8px; border-bottom: 1px solid var(--border);
  margin-bottom: 8px;
}
.workout-item {
  display: flex; align-items: flex-start; gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}
.workout-item:last-child { border-bottom: none; }
.workout-ex { font-size: 14px; font-weight: 600; flex: 1; }
.workout-detail { font-size: 12px; color: var(--muted); }
.workout-notes { font-size: 12px; color: var(--dim); font-style: italic; }

/* ── Delete button ── */
.item-delete {
  background: none; border: none;
  color: var(--dim); cursor: pointer; font-size: 14px;
  padding: 2px 6px; border-radius: 5px;
  transition: color 0.15s, background 0.15s;
  flex-shrink: 0;
}
.item-delete:hover { color: var(--red); background: rgba(192,64,64,0.08); }

/* ── Photo scan ── */
.scan-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius-sm);
  padding: 20px;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s;
  font-size: 13px; color: var(--dim);
}
.scan-zone:hover { border-color: rgba(74,138,56,0.4); color: var(--muted); }
.scan-zone svg { margin-bottom: 6px; color: var(--dim); }

.scan-preview { display: none; }
.scan-preview img { width: 100%; max-height: 200px; object-fit: cover; border-radius: var(--radius-sm); }
.scan-analyzing { display: none; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); margin-top: 8px; }

/* ── Chat ── */
.chat-wrap { display: flex; flex-direction: column; height: calc(100vh - var(--nav-h) - 120px); min-height: 400px; }
.chat-messages { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; padding: 16px 0; }

.chat-msg { display: flex; gap: 8px; align-items: flex-end; max-width: 85%; }
.chat-msg.user { flex-direction: row-reverse; align-self: flex-end; }

.chat-av {
  width: 26px; height: 26px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 10px; font-weight: 700; color: white; flex-shrink: 0;
}
.chat-msg.ai .chat-av { background: var(--grad); }
.chat-msg.user .chat-av { background: var(--bg3); border: 1px solid var(--border); color: var(--muted); }

.chat-bub { padding: 9px 13px; border-radius: 14px; font-size: 13px; line-height: 1.6; }
.chat-bub strong { font-weight: 600; }
.chat-bub em { font-style: italic; opacity: 0.9; }
.advice-text strong { font-weight: 600; color: var(--text); }
.advice-text ul { margin: 6px 0 6px 18px; display: flex; flex-direction: column; gap: 3px; }
.chat-msg.ai .chat-bub {
  background: var(--bg2); border: 1px solid var(--border);
  border-bottom-left-radius: 4px; color: var(--text);
}
.chat-msg.user .chat-bub { background: var(--grad); border-bottom-right-radius: 4px; color: #fff; }

.chat-input-row { display: flex; gap: 8px; padding-top: 12px; border-top: 1px solid var(--border); }
.chat-input {
  flex: 1;
  background: var(--bg2); border: 1px solid var(--border); border-radius: 20px;
  padding: 9px 16px; font-family: inherit; font-size: 13px; color: var(--text);
  outline: none; resize: none; height: 38px; transition: border-color 0.15s;
}
.chat-input:focus { border-color: rgba(74,138,56,0.4); }
.chat-send { width: 38px; height: 38px; border-radius: 50%; border: none; background: var(--grad); color: white; cursor: pointer; display: flex; align-items: center; justify-content: center; flex-shrink: 0; transition: opacity 0.15s; }
.chat-send:hover { opacity: 0.85; }
.chat-send svg { width: 16px; height: 16px; }

/* ── Modal ── */
.modal-overlay {
  display: none; position: fixed; inset: 0; z-index: 200;
  background: rgba(0,0,0,0.5); backdrop-filter: blur(4px);
  align-items: center; justify-content: center; padding: 20px;
}
.modal-overlay.open { display: flex; }
.modal {
  background: var(--bg); border: 1px solid var(--border);
  border-radius: 18px; padding: 24px; max-width: 560px; width: 100%;
  max-height: 80vh; overflow-y: auto; position: relative;
}
.modal-close { position: absolute; top: 16px; right: 16px; }
.modal-body { font-size: 13px; line-height: 1.75; color: var(--text); white-space: pre-line; }

/* ── Spinner ── */
.spinner {
  width: 16px; height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--sage);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.spinner-center { display: flex; justify-content: center; align-items: center; padding: 32px; gap: 10px; color: var(--muted); }

/* ── Empty state ── */
.empty { text-align: center; padding: 32px; color: var(--dim); font-size: 13px; }

/* ── Date input ── */
input[type="date"] { color-scheme: light; }
[data-theme="dark"] input[type="date"] { color-scheme: dark; }
.form-input[type="date"] { max-width: 160px; width: auto; min-width: 140px; }

/* ── Auth pages ── */
.auth-wrap {
  min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px;
  background: var(--bg);
}
.auth-card {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: 20px; padding: 36px; width: 100%; max-width: 400px;
}
.auth-logo {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  margin-bottom: 28px; text-decoration: none;
}
.auth-mark {
  width: 36px; height: 36px; background: var(--grad); border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 0 16px rgba(74,138,56,0.3);
}
.auth-mark svg { color: white; }
.auth-name { font-size: 20px; font-weight: 700; letter-spacing: 0.05em; color: var(--text); }
.auth-title { font-size: 22px; font-weight: 800; text-align: center; margin-bottom: 6px; }
.auth-sub { font-size: 13px; color: var(--muted); text-align: center; margin-bottom: 24px; }
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.auth-footer { text-align: center; margin-top: 18px; font-size: 13px; color: var(--muted); }
.auth-footer a { color: var(--sage); font-weight: 500; text-decoration: none; }
.auth-footer a:hover { text-decoration: underline; }
.auth-error { color: var(--red); font-size: 12px; text-align: center; padding: 8px 12px; background: rgba(192,64,64,0.08); border-radius: 7px; display: none; }
.auth-error.show { display: block; }

/* ── Onboarding ── */
.onboard-wrap { min-height: 100vh; display: flex; align-items: flex-start; justify-content: center; padding: 40px 24px; background: var(--bg); }
.onboard-card { background: var(--bg2); border: 1px solid var(--border); border-radius: 20px; padding: 36px; width: 100%; max-width: 560px; }
.onboard-step { font-size: 11px; font-weight: 600; color: var(--muted); letter-spacing: 0.1em; text-transform: uppercase; margin-bottom: 6px; }
.onboard-title { font-size: 24px; font-weight: 800; letter-spacing: -0.02em; margin-bottom: 6px; }
.onboard-sub { font-size: 14px; color: var(--muted); margin-bottom: 28px; line-height: 1.6; }

/* ── Upgrade ── */
.upgrade-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px; }
.upgrade-card { background: var(--bg2); border: 1px solid rgba(74,138,56,0.3); border-radius: 20px; padding: 40px; max-width: 400px; width: 100%; text-align: center; }
.upgrade-price { font-size: 48px; font-weight: 900; letter-spacing: -0.04em; background: var(--grad2); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text; margin: 8px 0; }

/* ── Responsive ── */
@media (max-width: 640px) {
  .nav-tabs { display: none; }
  .hamburger { display: flex; }
  .stat-grid { grid-template-columns: 1fr 1fr; }
  .form-row { grid-template-columns: 1fr; }
  .form-row.three { grid-template-columns: 1fr 1fr; }

  /* Prevent iOS auto-zoom on inputs */
  .form-input, .form-select, .form-textarea, .chat-input {
    font-size: 16px;
  }

  /* Chat — use svh for iOS Safari toolbar awareness */
  .chat-wrap {
    height: calc(100svh - var(--nav-h) - 140px);
    min-height: 320px;
  }

  /* Tables — horizontal scroll */
  .card .data-table { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; }

  /* Auth / onboard cards */
  .auth-card, .onboard-card { padding: 24px; }
  .upgrade-card { padding: 28px; }

  /* Modal — full width */
  .modal { border-radius: 16px 16px 0 0; margin-top: auto; }
  .modal-overlay { align-items: flex-end; padding: 0; }

  /* Main padding */
  .main { padding: 16px 14px 80px; }

  /* Nav right — tighter */
  .nav-right { gap: 6px; }
}
