:root {
  --bg: #F1F2EA;
  --surface: #FFFFFF;
  --surface-sunken: #EBEDE2;
  --ink: #23261E;
  --ink-soft: #565C4C;
  --border: #DBDECC;
  --primary: #3F5D3A;
  --primary-dark: #2C4228;
  --primary-tint: #E4EBDD;
  --accent: #B5563A;
  --accent-tint: #F3E2DA;
  --danger: #A83A3A;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(35,38,30,0.06), 0 6px 20px rgba(35,38,30,0.05);
  --font-display: "Fraunces", Georgia, "Times New Roman", serif;
  --font-body: "Public Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1B1D17;
    --surface: #24271F;
    --surface-sunken: #2C2F25;
    --ink: #EDEFE3;
    --ink-soft: #ABB09B;
    --border: #3A3E30;
    --primary: #8FB380;
    --primary-dark: #A9CB9B;
    --primary-tint: #2E3A28;
    --accent: #E0947B;
    --accent-tint: #3B2C25;
    --danger: #E08080;
  }
}
:root[data-theme="dark"] {
  --bg: #1B1D17;
  --surface: #24271F;
  --surface-sunken: #2C2F25;
  --ink: #EDEFE3;
  --ink-soft: #ABB09B;
  --border: #3A3E30;
  --primary: #8FB380;
  --primary-dark: #A9CB9B;
  --primary-tint: #2E3A28;
  --accent: #E0947B;
  --accent-tint: #3B2C25;
  --danger: #E08080;
}

* { box-sizing: border-box; }
/* Several elements below (.login-screen, .app, .modal-backdrop) set
   their own `display` value for when they're visible. Without this
   rule, that value would win over the browser's built-in "hidden
   means display:none" behavior — the element would stay visible
   (and, if it's positioned fixed/full-screen, keep intercepting
   clicks) even while marked hidden. !important guarantees `hidden`
   always wins, regardless of what any class sets. */
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.5;
}
h1, h2, h3 { font-family: var(--font-display); font-weight: 600; margin: 0; color: var(--ink); }
button { font-family: inherit; }
input, select, textarea {
  font-family: inherit;
  font-size: 14px;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--surface);
  color: var(--ink);
  width: 100%;
}
input:focus, select:focus, textarea:focus, button:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}
label { display: block; font-size: 13px; color: var(--ink-soft); margin-bottom: 12px; }
label > input, label > select, label > textarea { margin-top: 5px; }

/* ---------- Login ---------- */
.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--primary-dark);
  padding: 24px;
}
.login-card {
  background: var(--surface);
  border-radius: 14px;
  padding: 40px 36px;
  width: 100%;
  max-width: 380px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
}
.login-title { font-size: 28px; margin-bottom: 6px; }
.login-sub { color: var(--ink-soft); font-size: 14px; margin: 0 0 26px; line-height: 1.5; }
.error-text { color: var(--danger); font-size: 13px; margin: -6px 0 14px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: 7px;
  padding: 9px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background-color .12s ease, border-color .12s ease;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary { background: var(--surface); border-color: var(--border); color: var(--ink); }
.btn-secondary:hover { background: var(--surface-sunken); }
.btn-danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: #fff; }
.btn-block { width: 100%; }
.link-btn {
  background: none; border: none; color: var(--ink-soft);
  font-size: 13px; cursor: pointer; text-decoration: underline;
  padding: 0;
}
.link-btn:hover { color: var(--ink); }

/* ---------- App shell ---------- */
/* height (not min-height) + overflow: hidden pins the header and
   sidebar in place — the whole app fills exactly one viewport and
   never itself grows taller, so it's .main below that scrolls a
   page's content, not the document. */
.app { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }
.app-header {
  background: var(--surface); border-bottom: 1px solid var(--border);
  padding: 14px 28px; flex-shrink: 0;
}
.app-logo { height: 42px; width: auto; display: block; }
.app-body { display: flex; flex: 1; min-height: 0; }
.sidebar {
  width: 220px;
  flex-shrink: 0;
  background: var(--primary-dark);
  color: #fff;
  display: flex;
  flex-direction: column;
  padding: 22px 14px;
  overflow-y: auto;
}
.nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav-item {
  text-align: left;
  background: none;
  border: none;
  color: rgba(255,255,255,0.75);
  padding: 10px 12px;
  border-radius: 7px;
  font-size: 14px;
  cursor: pointer;
}
.nav-item:hover { background: rgba(255,255,255,0.08); color: #fff; }
.nav-item.active { background: rgba(255,255,255,0.14); color: #fff; font-weight: 600; }
.sidebar-footer {
  border-top: 1px solid rgba(255,255,255,0.15);
  padding-top: 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
}
.sidebar-footer #whoami { color: rgba(255,255,255,0.65); }
.sidebar-footer .link-btn { color: rgba(255,255,255,0.75); }
.sidebar-footer .link-btn:hover { color: #fff; }

.main { flex: 1; padding: 32px 40px; max-width: 1120px; overflow-y: auto; }
.view-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 22px;
  gap: 16px;
}
.view-header h2 { font-size: 24px; }
.view-toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 18px; flex-wrap: wrap; }
.view-toolbar input, .view-toolbar select { width: auto; min-width: 180px; }

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

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; margin-bottom: 28px; }
.stat-card { padding: 18px 20px; }
.stat-label { font-size: 12.5px; color: var(--ink-soft); margin-bottom: 6px; }
.stat-value { font-family: var(--font-display); font-size: 30px; font-weight: 600; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 11px 14px; font-size: 14px; border-bottom: 1px solid var(--border); }
th { font-size: 12px; text-transform: none; color: var(--ink-soft); font-weight: 600; }
tbody tr:hover { background: var(--surface-sunken); }
tbody tr { cursor: pointer; }
.row-actions { display: flex; gap: 8px; }
.row-actions button { font-size: 12.5px; padding: 5px 10px; }

.empty-state {
  text-align: center;
  padding: 48px 20px;
  color: var(--ink-soft);
}
.empty-state p { margin: 0 0 16px; }

.badge {
  display: inline-block;
  font-size: 12px;
  font-weight: 600;
  padding: 3px 9px;
  border-radius: 100px;
  background: var(--primary-tint);
  color: var(--primary-dark);
}
.badge.accent { background: var(--accent-tint); color: var(--accent); }
.badge.muted { background: var(--surface-sunken); color: var(--ink-soft); }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(20,22,16,0.45);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 40px 20px;
  overflow-y: auto;
  z-index: 50;
}
.modal {
  background: var(--surface);
  border-radius: 12px;
  width: 100%;
  max-width: 560px;
  padding: 28px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.modal h3 { font-size: 20px; margin-bottom: 20px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 22px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }

/* ---------- Client detail page (Profile / BMI / Plans tabs) ---------- */
.client-tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 18px; }
.client-tab {
  padding: 7px 16px; border-radius: 100px; border: 1px solid var(--border);
  background: var(--surface); font-size: 13.5px; font-weight: 600; cursor: pointer;
}
.client-tab.active { background: var(--primary); border-color: var(--primary); color: #fff; }

/* ---------- BMI tracking (the client's BMI tab) ---------- */
.bmi-live { font-size: 14px; font-weight: 600; color: var(--primary-dark); min-height: 20px; margin-bottom: 10px; }
.field-grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0 14px; }
.bmi-date { font-size: 13.5px; color: var(--ink-soft); margin: 4px 0 14px; }
.bmi-history { margin-top: 22px; display: grid; gap: 12px; }
.bmi-entry { border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; }
.bmi-entry-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.bmi-entry .btn { font-size: 12px; padding: 4px 8px; }
.bmi-entry-main { font-size: 13.5px; }
.bmi-change { color: var(--ink-soft); font-size: 12.5px; margin-left: 4px; }
.bmi-readings { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px 14px; margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--border); }
.bmi-readings div { display: flex; flex-direction: column; font-size: 12px; color: var(--ink-soft); }
.bmi-readings strong { font-size: 13.5px; color: var(--ink); }
.bmi-note { color: var(--ink-soft); font-size: 12.5px; font-style: italic; margin-top: 8px; }
.muted-text { color: var(--ink-soft); font-size: 13.5px; }

/* ---------- Meal plan builder ---------- */
.day-tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 16px; }
.day-tab {
  padding: 6px 13px; border-radius: 100px; border: 1px solid var(--border);
  background: var(--surface); font-size: 13px; cursor: pointer;
}
.day-tab.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.meal-block { border: 1px solid var(--border); border-radius: 8px; padding: 14px; margin-bottom: 12px; background: var(--surface-sunken); }
.meal-block-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.food-item-row {
  display: flex; flex-wrap: wrap; gap: 6px 12px; align-items: center;
  padding: 8px 0; font-size: 13.5px; border-bottom: 1px dashed var(--border);
}
.food-item-row:last-child { border-bottom: none; }
.food-item-row .food-name { flex: 1 1 130px; font-weight: 600; }
.item-amount-field { display: flex; align-items: center; gap: 5px; }
.item-amount-field input { width: 60px; }
.item-unit { color: var(--ink-soft); font-size: 12.5px; }
.food-item-macros { display: flex; flex-wrap: wrap; gap: 4px 12px; flex: 1 1 280px; }
.macro-chip { color: var(--ink-soft); font-size: 12px; white-space: nowrap; }
.macro-chip strong { color: var(--ink); font-weight: 600; }
.add-food-row {
  display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: center;
  margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--border);
}
.add-food-row select { flex: 1 1 200px; }
.food-picker { position: relative; flex: 1 1 200px; }
.food-picker .add-food-search { width: 100%; }
.food-picker-list {
  position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 30;
  max-height: 260px; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; box-shadow: var(--shadow);
}
.food-picker-option { display: flex; justify-content: space-between; gap: 12px; padding: 8px 12px; font-size: 13.5px; cursor: pointer; }
.food-picker-option span { color: var(--ink-soft); font-size: 12px; white-space: nowrap; }
.food-picker-option.active, .food-picker-option:hover { background: var(--primary-tint); }
.food-picker-empty, .food-picker-more { padding: 10px 12px; font-size: 12.5px; color: var(--ink-soft); }
.add-food-row .add-food-macros { flex: 1 1 260px; align-items: center; }
.preview-tag {
  font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em;
  color: var(--accent); background: var(--accent-tint); padding: 2px 7px; border-radius: 100px;
}

.day-totals {
  margin-top: 16px; padding: 16px 20px; border-radius: 10px;
  background: var(--primary-tint); border: 1px solid var(--primary);
}
.day-totals-title { font-weight: 700; font-size: 13px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--primary-dark); margin-bottom: 12px; }
.day-totals-chips { display: flex; flex-wrap: wrap; gap: 14px 28px; }
.day-total-chip { display: flex; flex-direction: column; gap: 2px; }
.day-total-chip span { font-size: 11.5px; color: var(--ink-soft); text-transform: uppercase; letter-spacing: 0.03em; }
.day-allowance { margin-top: 14px; padding-top: 14px; border-top: 1px dashed var(--primary); }
.day-allowance-note { font-size: 12.5px; color: var(--ink-soft); margin: 8px 0 0; }
.day-total-chip.over strong { color: var(--danger); }
.day-total-chip strong { font-family: var(--font-display); font-size: 19px; font-weight: 600; color: var(--ink); }

.toast {
  position: fixed; top: 20px; right: 20px;
  background: var(--ink); color: var(--surface);
  padding: 11px 18px; border-radius: 8px; font-size: 13.5px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.2);
  z-index: 100;
}
.toast.error { background: var(--danger); }

@media (max-width: 780px) {
  .app-body { flex-direction: column; }
  .app-header { padding: 10px 16px; }
  .app-logo { height: 32px; }
  .sidebar { width: 100%; flex-direction: row; align-items: center; padding: 12px 16px; overflow-x: auto; }
  .nav { flex-direction: row; }
  .sidebar-footer { flex-direction: row; margin-left: auto; border: none; padding: 0; }
  .main { padding: 20px; }
  .field-row { grid-template-columns: 1fr; }
  .field-grid-3, .bmi-readings { grid-template-columns: 1fr 1fr; }
}

/* ---------- Printable reports (BMI "Open" modal, meal plan print) ---------- */
.report-topbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 22px; }
.report-logo { height: 68px; width: auto; display: block; }
.bmi-report-stats {
  display: flex; flex-wrap: wrap; gap: 16px 32px;
  margin-bottom: 20px; padding-bottom: 16px; border-bottom: 1px solid var(--border);
}
.bmi-report-stats div { display: flex; flex-direction: column; gap: 2px; }
.bmi-report-stats span { font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.03em; color: var(--ink-soft); }
.bmi-report-stats strong { font-family: var(--font-display); font-size: 20px; font-weight: 600; }
.report-table { width: 100%; border-collapse: collapse; }
.report-table th, .report-table td { text-align: left; padding: 9px 4px; font-size: 13.5px; border-bottom: 1px solid var(--border); }
.report-table th { font-weight: 600; color: var(--ink-soft); }
.report-table td { font-weight: 600; }
.bmi-report-table.report-table th { width: 55%; }
.mealplan-report-table.report-table td { font-weight: 500; }
.mealplan-report-table.report-table td:first-child { font-weight: 600; vertical-align: top; }
.mealplan-report-day { font-size: 16px; margin: 22px 0 8px; }
.mealplan-report-day:first-of-type { margin-top: 0; }

/* Printing a report: hide everything except the report itself (no
   sidebar, no Close/Print bar, no dark modal backdrop). Any printable
   modal wraps its content in .print-area. */
@media print {
  body * { visibility: hidden; }
  .print-area, .print-area * { visibility: visible; }
  .print-area { position: absolute; top: 0; left: 0; width: 100%; padding: 24px; }
  .no-print { display: none !important; }
}
