@import url('https://fonts.googleapis.com/css2?family=Tajawal:wght@400;500;700;800;900&display=swap');

/* ============================================================
   DESIGN TOKENS & MULTI-THEME SYSTEM
   ============================================================ */

/* 1. EMERALD WELLNESS / APPLE HEALTH MODERN (Default Clean) */
[data-theme="emerald"], :root {
  --paper:          #FFFFFF;
  --paper-card:     #F8FAFC;
  --paper-edge:     #F1F5F9;
  --rule:           #E2E8F0;
  --rule-faint:     #F8FAFC;
  --ink:            #0F172A;
  --ink-soft:       #475569;
  --ink-faint:      #94A3B8;
  --tape:           #10B981; /* Emerald */
  --olive:          #059669;
  --madder:         #EF4444;
  --blue:           #3B82F6;
  --amber:          #F59E0B;
  --radius:         10px;
  --line-h:         30px;
  --gap:            14px;
  --font-display:   ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --font-body:      ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-data:      ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

/* 2. MIDNIGHT OLED DARK MODE */
[data-theme="dark"] {
  --paper:          #0F172A;
  --paper-card:     #1E293B;
  --paper-edge:     #020617;
  --rule:           #334155;
  --rule-faint:     #1E293B;
  --ink:            #F8FAFC;
  --ink-soft:       #CBD5E1;
  --ink-faint:      #64748B;
  --tape:           #38BDF8; /* Electric Cyan */
  --olive:          #34D399; /* Neon Mint */
  --madder:         #F87171;
  --blue:           #60A5FA;
  --amber:          #FBBF24;
  --radius:         10px;
}

/* 3. CLINICAL ROYAL MEDTECH BLUE */
[data-theme="clinical"] {
  --paper:          #FFFFFF;
  --paper-card:     #F8FAFC;
  --paper-edge:     #E2E8F0;
  --rule:           #CBD5E1;
  --rule-faint:     #F1F5F9;
  --ink:            #0A192F;
  --ink-soft:       #334155;
  --ink-faint:      #64748B;
  --tape:           #2563EB; /* Royal Blue */
  --olive:          #16A34A;
  --madder:         #DC2626;
  --blue:           #2563EB;
  --amber:          #D97706;
  --radius:         6px;
}

/* 4. CLASSIC KRAFT PAPER JOURNAL */
[data-theme="paper"] {
  --paper:          #FCFBF7;
  --paper-edge:     #ECEAE0;
  --rule:           #A9C4EA;
  --rule-faint:     #D8E4F5;
  --ink:            #14100C;
  --ink-soft:       #5C564D;
  --ink-faint:      #918A7E;
  --tape:           #E4EE4C;
  --olive:          #4E6B39;
  --madder:         #B3312A;
  --blue:           #2A6496;
  --amber:          #D97706;
  --radius:         3px;
}

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

html {
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
  width: 100%;
  max-width: 100%;
}

body {
  margin: 0;
  background: var(--paper-edge);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.45;
  padding-bottom: env(safe-area-inset-bottom);
  overflow-x: hidden;
  width: 100%;
  max-width: 100%;
}

/* Notebook / Modern Container Sheet */
.sheet {
  width: 100%;
  max-width: 780px;
  margin: 0 auto;
  background: var(--paper);
  min-height: 100vh;
  box-shadow: 0 0 0 1px rgba(0,0,0,.06), 0 4px 24px rgba(0,0,0,.07);
  position: relative;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  overflow-x: hidden;
  transition: max-width .2s ease, background 0.2s ease;
}

[data-theme="paper"] .sheet {
  background-image: repeating-linear-gradient(
      to bottom,
      transparent 0,
      transparent calc(var(--line-h) - 1px),
      var(--rule-faint) calc(var(--line-h) - 1px),
      var(--rule-faint) var(--line-h)
  );
  background-position: 0 0;
}

/* Wide / Full-Screen mode for Administrative & Clinical dashboards */
.sheet.sheet-wide {
  max-width: 1420px;
  width: 100%;
}

/* Red/Blue Margin Rule down the left (Paper theme only) */
[data-theme="paper"] .sheet::before {
  content: "";
  position: absolute;
  top: 0; bottom: 0; left: 34px;
  width: 1px;
  background: var(--rule);
  opacity: .5;
  pointer-events: none;
}
@media (max-width: 520px) { [data-theme="paper"] .sheet::before { left: 20px; } }

.pad { padding: 0 20px 0 24px; }
[data-theme="paper"] .pad { padding: 0 20px 0 48px; }
@media (max-width: 520px) { 
  .pad { padding: 0 14px 0 14px; }
  [data-theme="paper"] .pad { padding: 0 12px 0 26px; } 
}

/* Dark Mode Overrides */
[data-theme="dark"] body {
  background: #020617 !important;
  color: #F8FAFC !important;
}
[data-theme="dark"] .sheet {
  background: #0F172A !important;
  box-shadow: 0 4px 30px rgba(0,0,0,0.6);
  border: 1px solid #1E293B;
}
[data-theme="dark"] input, [data-theme="dark"] select, [data-theme="dark"] textarea {
  background: #1E293B !important;
  color: #F8FAFC !important;
  border-color: #334155 !important;
}
[data-theme="dark"] .card, [data-theme="dark"] .bd-section, [data-theme="dark"] .modal-card, [data-theme="dark"] .mast-btn, [data-theme="dark"] .btn-secondary {
  background: #1E293B !important;
  color: #F8FAFC !important;
  border-color: #334155 !important;
}
[data-theme="dark"] .food-row {
  border-bottom-color: #334155 !important;
}

/* Tape only visible in classic paper theme */
.tape { display: none; }
[data-theme="paper"] .tape { display: block; }

/* ============================================================
   MASTHEAD & APP HEADER
   ============================================================ */
.masthead { padding-top: 24px; position: relative; }

.tape {
  position: absolute;
  top: -6px; left: 50%;
  width: 110px; height: 26px;
  transform: translateX(-55%) rotate(-1.6deg);
  background: var(--tape);
  opacity: .75;
  mix-blend-mode: multiply;
  pointer-events: none;
}

.masthead-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.masthead h1 {
  font-family: var(--font-display);
  font-size: 24px;
  font-weight: 800;
  letter-spacing: -.02em;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 8px;
}
.masthead h1 a { color: var(--ink); text-decoration: none; }
.masthead p {
  margin: 2px 0 0;
  font-family: var(--font-data);
  font-size: 11px;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--ink-faint);
}

.user-nav {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: var(--font-data);
  font-size: 12px;
}

.mast-btn {
  border: 1.5px solid var(--ink);
  background: transparent;
  color: var(--ink);
  font-family: var(--font-data);
  font-size: 11px;
  letter-spacing: .06em;
  text-transform: uppercase;
  min-height: 32px;
  padding: 0 10px;
  cursor: pointer;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  text-decoration: none;
}
.mast-btn:hover { background: var(--tape); }
.mast-btn.active { background: var(--ink); color: var(--paper); }

/* ============================================================
   DATE NAVIGATION BAR (5:00 AM Day Definition)
   ============================================================ */
.date-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 14px;
  padding: 8px 12px;
  background: rgba(228, 238, 76, .18);
  border: 1px dashed var(--ink);
  font-family: var(--font-data);
}

.date-controls {
  display: flex;
  align-items: center;
  gap: 6px;
}

.date-nav-btn {
  border: 1px solid var(--ink);
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-display);
  font-weight: 700;
  width: 28px; height: 28px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
}
.date-nav-btn:hover { background: var(--tape); }

.date-display {
  font-size: 13px;
  font-weight: 700;
  letter-spacing: .02em;
  display: flex;
  align-items: center;
  gap: 6px;
}

.date-picker-input {
  border: 1px solid var(--ink);
  background: var(--paper);
  font-family: var(--font-data);
  font-size: 12px;
  padding: 3px 6px;
  height: 28px;
  cursor: pointer;
}

.day-badge {
  display: inline-block;
  padding: 2px 6px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .05em;
  text-transform: uppercase;
  border: 1px solid var(--ink);
}
.day-badge.open { background: var(--tape); color: var(--ink); }
.day-badge.closed { background: var(--ink); color: var(--paper); }

/* ============================================================
   DASHBOARD COMPONENT
   ============================================================ */
.dash {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--paper);
  border-bottom: 1px solid var(--ink);
  padding-top: 14px;
  padding-bottom: 12px;
  margin-top: 12px;
}

.dash-label {
  font-family: var(--font-data);
  font-size: 11px;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--ink-soft);
  margin: 0;
}

.dash-figure { display: flex; align-items: baseline; gap: 8px; margin: 2px 0 0; }

#remaining {
  font-family: var(--font-display);
  font-size: clamp(48px, 16vw, 74px);
  font-weight: 800;
  letter-spacing: -.055em;
  line-height: .95;
  font-variant-numeric: tabular-nums;
  transition: color .18s ease;
}
#remaining.is-over { color: var(--madder); }

.dash-unit {
  font-family: var(--font-data);
  font-size: 12px;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--ink-soft);
}

.marker {
  position: relative;
  height: 32px;
  margin-top: 10px;
  border-bottom: 1.5px solid var(--rule);
  overflow: hidden;
}
.marker-fill {
  position: absolute;
  inset: 4px auto 3px 0;
  width: 0%;
  background: var(--tape);
  mix-blend-mode: multiply;
  clip-path: polygon(0 0, calc(100% - 5px) 0, 100% 42%, calc(100% - 3px) 100%, 0 100%);
  transition: width .28s cubic-bezier(.22,.9,.3,1), background-color .18s ease;
}
.marker-fill.is-warn { background: #F2C13C; }
.marker-fill.is-over { background: #E58C82; }

.marker-ticks { position: absolute; inset: 0; pointer-events: none; }
.marker-ticks span {
  position: absolute;
  top: 0; bottom: 0;
  width: 1px;
  background: var(--rule);
  opacity: .7;
}
.marker-caps {
  display: flex;
  justify-content: space-between;
  font-family: var(--font-data);
  font-size: 10.5px;
  letter-spacing: .06em;
  color: var(--ink-faint);
  margin-top: 4px;
}

.day-strip { display: flex; height: 9px; margin-top: 12px; gap: 2px; }
.day-strip i { display: block; height: 100%; }
.seg-fixed { background: var(--ink); opacity: .28; }
.seg-logged { background: var(--olive); transition: flex-basis .28s ease; }
.seg-over { background: var(--madder); transition: flex-basis .28s ease; }
.seg-left { background: repeating-linear-gradient(45deg, var(--rule-faint) 0 3px, transparent 3px 6px); }

.day-legend {
  display: flex; flex-wrap: wrap; gap: 4px 14px;
  margin-top: 6px;
  font-family: var(--font-data);
  font-size: 10.5px;
  letter-spacing: .05em;
  color: var(--ink-soft);
}
.day-legend b { font-weight: 700; color: var(--ink); }

/* ============================================================
   MACRONUTRIENT BREAKDOWN
   ============================================================ */
.macro-card {
  margin-top: 14px;
  padding: 10px 12px;
  border: 1px solid var(--ink);
  background: var(--paper);
}
.macro-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  text-align: center;
}
.macro-col {
  padding: 6px 4px;
  background: rgba(0,0,0,.02);
  border-left: 3px solid var(--rule);
}
.macro-col.protein { border-left-color: var(--olive); }
.macro-col.carbs { border-left-color: var(--amber); }
.macro-col.fat { border-left-color: var(--madder); }

.macro-label {
  font-family: var(--font-data);
  font-size: 10.5px;
  text-transform: uppercase;
  color: var(--ink-soft);
  letter-spacing: .08em;
}
.macro-val {
  font-family: var(--font-display);
  font-size: 18px;
  font-weight: 800;
  margin-top: 2px;
}
.macro-sub {
  font-family: var(--font-data);
  font-size: 10.5px;
  color: var(--ink-faint);
}

.macro-bar {
  display: flex;
  height: 8px;
  margin-top: 8px;
  background: var(--rule-faint);
  gap: 1px;
}
.macro-bar-protein { background: var(--olive); }
.macro-bar-carbs   { background: var(--amber); }
.macro-bar-fat     { background: var(--madder); }

/* ============================================================
   MEAL TABS (GROUPING)
   ============================================================ */
.tabs-header {
  display: block;
  width: 100%;
  margin-top: 6px;
  border-bottom: 1.5px solid var(--ink);
  padding-bottom: 0px;
}

.tab-scroll-container {
  display: flex;
  overflow-x: auto;
  gap: 6px;
  scrollbar-width: thin;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 2px;
  width: 100%;
}
.tab-scroll-container::-webkit-scrollbar { height: 4px; }
.tab-scroll-container::-webkit-scrollbar-thumb { background: var(--rule); border-radius: 2px; }

.tab-btn {
  border: 1.5px solid var(--ink);
  border-bottom: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-display);
  font-size: 13.5px;
  font-weight: 700;
  padding: 7px 14px;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color .15s ease;
  position: relative;
  bottom: -1px;
}
.tab-btn:hover { background: var(--tape); }
.tab-btn.active {
  background: var(--ink);
  color: var(--paper);
  box-shadow: inset 0 -2px 0 var(--tape);
}

.tab-action-btn {
  border: 1.5px dashed var(--ink);
  background: var(--paper);
  font-family: var(--font-data);
  font-size: 11.5px;
  padding: 4px 10px;
  cursor: pointer;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
}

/* Filter & Search Bar */
.filter-bar {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  margin-top: 12px;
  align-items: center;
}
@media (max-width: 480px) {
  .filter-bar { grid-template-columns: 1fr; }
}

.search-box {
  width: 100%;
  min-height: 40px;
  border: 1.5px solid var(--ink);
  background: transparent;
  padding: 0 10px;
  font-family: var(--font-body);
  font-size: 14px;
  color: var(--ink);
}
.search-box::placeholder { color: var(--ink-faint); }

.sort-select {
  min-height: 40px;
  border: 1.5px solid var(--ink);
  background: var(--paper);
  padding: 0 8px;
  font-family: var(--font-data);
  font-size: 12px;
  color: var(--ink);
  cursor: pointer;
}

/* ============================================================
   FOOD ROWS & STEPPERS
   ============================================================ */
.food-list { margin-top: 10px; }

.food-row {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 6px 10px;
  padding: 8px 2px;
  border-bottom: 1px dotted var(--rule);
  transition: background-color .15s ease;
}
.food-row.is-active {
  background: rgba(16, 185, 129, 0.12) !important;
  border-left: 4px solid var(--olive);
  padding-left: 6px;
  padding-right: 6px;
  border-radius: 4px;
}
html[dir="rtl"] .food-row.is-active {
  border-left: none;
  border-right: 4px solid var(--olive);
  padding-right: 6px;
  padding-left: 6px;
}

.food-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.food-title-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.food-name {
  font-size: 15px;
  font-weight: 700;
  letter-spacing: -.01em;
}

.badge-approved {
  font-size: 12px;
  color: var(--olive);
  title: "Admin Verified";
}

.btn-star {
  background: transparent;
  border: 0;
  cursor: pointer;
  padding: 0;
  font-size: 14px;
  color: var(--ink-faint);
  line-height: 1;
}
.btn-star.starred { color: #EAB308; }

.btn-edit-food {
  background: transparent;
  border: 0;
  cursor: pointer;
  padding: 0 4px;
  font-size: 13px;
  color: var(--ink-faint);
}
.btn-edit-food:hover { color: var(--ink); }

.food-meta {
  font-family: var(--font-data);
  font-size: 11px;
  color: var(--ink-faint);
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.food-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  grid-column: 2;
  grid-row: 1 / span 2;
}

.unit-select {
  min-height: 38px;
  border: 1.5px solid var(--ink);
  background: var(--paper);
  font-family: var(--font-data);
  font-size: 12px;
  padding: 0 6px;
  color: var(--ink);
  cursor: pointer;
}

.stepper {
  display: flex;
  align-items: center;
  border: 1.5px solid var(--ink);
  background: var(--paper);
}

.stepper button {
  width: 38px; height: 38px;
  border: 0; background: transparent;
  font-family: var(--font-display);
  font-size: 18px; font-weight: 700;
  color: var(--ink);
  cursor: pointer;
  touch-action: manipulation;
}
.stepper button:hover:not(:disabled) { background: var(--tape); }
.stepper button:active:not(:disabled) { background: var(--ink); color: var(--paper); }
.stepper button:disabled { color: var(--rule); cursor: not-allowed; }

.stepper input {
  width: 54px; height: 38px;
  border: 0;
  border-left: 1.5px solid var(--ink);
  border-right: 1.5px solid var(--ink);
  background: transparent;
  font-family: var(--font-data);
  font-size: 14px; font-weight: 700;
  text-align: center;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  -moz-appearance: textfield;
}
.stepper input::-webkit-outer-spin-button,
.stepper input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

.row-total {
  font-family: var(--font-data);
  font-size: 12px;
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
  min-width: 54px;
  text-align: right;
}
.food-row.is-active .row-total { color: var(--ink); font-weight: 700; }

@media (max-width: 520px) {
  .food-row {
    padding: 7px 0;
  }
  .food-controls {
    grid-column: 1 / span 2;
    grid-row: auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 4px;
    gap: 4px;
    width: 100%;
  }
  .unit-select {
    flex: 1 1 auto;
    min-width: 80px;
    max-width: 130px;
    min-height: 32px;
    height: 32px;
    font-size: 11px;
    padding: 0 4px;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
  }
  .stepper {
    flex-shrink: 0;
  }
  .stepper button {
    width: 30px;
    height: 32px;
    font-size: 16px;
  }
  .stepper input {
    width: 40px;
    height: 32px;
    font-size: 13px;
  }
  .row-total {
    min-width: 44px;
    font-size: 11px;
    text-align: right;
    flex-shrink: 0;
  }
  html[dir="rtl"] .row-total {
    text-align: left;
  }
}

/* ============================================================
   CHARTS & BREAKDOWN
   ============================================================ */
section { padding-top: 24px; }

.sec-head {
  display: flex; align-items: baseline; justify-content: space-between; gap: 10px;
  border-bottom: 1px solid var(--ink);
  padding-bottom: 4px;
  margin-bottom: 4px;
}
.sec-head h2 {
  font-family: var(--font-display);
  font-size: 16px; font-weight: 800; letter-spacing: -.01em;
  margin: 0;
}
.sec-head .sec-meta {
  font-family: var(--font-data);
  font-size: 11px; letter-spacing: .05em; color: var(--ink-faint);
  font-variant-numeric: tabular-nums;
}

.bd-empty {
  font-size: 13.5px; color: var(--ink-faint);
  padding: 12px 0 4px; margin: 0;
}
.bd-item { padding: 8px 0; border-bottom: 1px dotted var(--rule-faint); }
.bd-top {
  display: flex; justify-content: space-between; align-items: baseline; gap: 10px;
  font-size: 13.5px;
}
.bd-top span:last-child {
  font-family: var(--font-data); font-size: 12px;
  font-variant-numeric: tabular-nums; color: var(--ink-soft); white-space: nowrap;
}
.bd-bar { height: 10px; margin-top: 4px; background: var(--rule-faint); }
.bd-bar i { display: block; height: 100%; background: var(--olive); }

.history { margin-top: 8px; }
.history svg { display: block; width: 100%; height: auto; }

/* ============================================================
   ACTIONS & DOCKED BOTTOM BAR (PERMANENTLY FIXED TO VIEWPORT)
   ============================================================ */
.actions-dock-fixed {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 999;
  background: var(--paper-card, #fff);
  border-top: 2px solid var(--ink);
  padding: 10px 16px calc(12px + env(safe-area-inset-bottom)) 16px;
  box-shadow: 0 -4px 20px rgba(0,0,0,.15);
  display: flex;
  justify-content: center;
  align-items: center;
  backdrop-filter: blur(8px);
}

.btn-dock-save {
  max-width: 740px;
  width: 100%;
  min-height: 50px !important;
  font-size: 15px !important;
  font-weight: 800 !important;
  display: flex !important;
  align-items: center !important;
  justify-content: space-between !important;
  padding: 0 18px !important;
  box-shadow: 2.5px 2.5px 0px rgba(0,0,0,0.18) !important;
  border-radius: 4px;
}

.badge-dock-cal {
  background: rgba(255,255,255,0.22);
  border: 1px solid rgba(255,255,255,0.4);
  padding: 3px 10px;
  border-radius: 4px;
  font-size: 13px;
  font-family: var(--font-data);
  font-weight: 800;
  letter-spacing: 0.02em;
}

.pad-has-dock {
  padding-bottom: calc(90px + env(safe-area-inset-bottom)) !important;
}

.actions-dock {
  position: sticky; bottom: 0; z-index: 30;
  display: flex; gap: 10px;
  padding: 12px 20px calc(12px + env(safe-area-inset-bottom)) 48px;
  margin-top: auto;
  background: var(--paper);
  border-top: 1.5px solid var(--ink);
  box-shadow: 0 -4px 12px rgba(0,0,0,.04);
}
@media (max-width: 520px) { .actions-dock { padding-left: 30px; padding-right: 14px; } }

.btn {
  flex: 1;
  min-height: 48px;
  border: 1.5px solid var(--ink);
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-display);
  font-size: 14.5px; font-weight: 700; letter-spacing: -.01em;
  cursor: pointer;
  touch-action: manipulation;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  text-decoration: none;
}
.btn:hover { background: var(--tape); }
.btn-primary { background: var(--ink); color: var(--paper); flex: 2; }
.btn-primary:hover { background: var(--olive); color: var(--paper); }
.btn-secondary { background: var(--tape); color: var(--ink); }
.btn-quiet { flex: 1; font-weight: 600; }
.btn-danger { color: var(--madder); border-color: var(--madder); }
.btn-danger:hover { background: rgba(179,49,42,.1); }

/* ============================================================
   MODALS & DIALOGS
   ============================================================ */
.modal-overlay {
  position: fixed; inset: 0; z-index: 999;
  background: rgba(20, 16, 12, 0.65);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
  opacity: 0; pointer-events: none;
  transition: opacity .2s ease;
}
.modal-overlay.open { opacity: 1; pointer-events: auto; }

.modal-sheet, .modal-card {
  background-color: #FAF8F5 !important;
  background: #FAF8F5 !important;
  max-width: 540px; width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  border: 2px solid var(--ink, #1C1917) !important;
  border-radius: 6px;
  box-shadow: 0 16px 40px rgba(0,0,0,.35), 4px 4px 0px rgba(0,0,0,0.1);
  padding: 22px 24px;
  position: relative;
  z-index: 1000;
}
.modal-title {
  font-family: var(--font-display);
  font-size: 18px; font-weight: 800;
  margin: 0 0 14px;
  border-bottom: 1.5px solid var(--ink);
  padding-bottom: 8px;
  display: flex; justify-content: space-between; align-items: center;
}
.modal-close {
  background: transparent; border: 0; font-size: 22px; cursor: pointer; font-weight: 700; color: var(--ink);
}

.field { display: block; margin-top: 12px; }
.field > span {
  display: block;
  font-family: var(--font-data); font-size: 10.5px; letter-spacing: .08em;
  text-transform: uppercase; color: var(--ink-faint); margin-bottom: 4px;
}
.field input, .field select, .field textarea {
  width: 100%; min-height: 42px;
  border: 1.5px solid var(--ink); background: transparent;
  padding: 0 10px;
  font-family: var(--font-body); font-size: 14.5px;
  color: var(--ink);
}
.field input[type="number"] { font-family: var(--font-data); }

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

.checkbox-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 6px;
}
.checkbox-pill {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border: 1.5px solid var(--ink);
  font-family: var(--font-data);
  font-size: 12px;
  cursor: pointer;
  background: var(--paper);
}
.checkbox-pill input { margin: 0; cursor: pointer; }
.checkbox-pill:has(input:checked) { background: var(--tape); font-weight: 700; }

.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 20px;
}

/* ============================================================
   TOAST & FOOTER
   ============================================================ */
#toast {
  position: fixed; left: 50%; bottom: 84px; z-index: 120;
  transform: translate(-50%, 10px);
  background: var(--ink); color: var(--paper);
  font-family: var(--font-data); font-size: 12px; letter-spacing: .05em;
  padding: 10px 18px;
  opacity: 0; pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
  max-width: calc(100vw - 32px); text-align: center;
  box-shadow: 0 4px 14px rgba(0,0,0,.15);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.is-error { background: var(--madder); }

footer {
  padding: 16px 20px 24px 48px;
  font-family: var(--font-data); font-size: 10.5px; letter-spacing: .05em;
  color: var(--ink-faint);
  border-top: 1px dotted var(--rule);
  margin-top: 20px;
}
@media (max-width: 520px) { footer { padding-left: 30px; padding-right: 14px; } }

/* Auth Forms */
.auth-sheet {
  max-width: 440px;
  margin: 40px auto;
}
.auth-card {
  border: 2px solid var(--ink);
  background: var(--paper);
  padding: 24px;
  box-shadow: 0 4px 16px rgba(0,0,0,.06);
}
.auth-card h2 {
  font-family: var(--font-display);
  font-size: 22px; font-weight: 800;
  margin: 0 0 16px;
  border-bottom: 1.5px solid var(--ink);
  padding-bottom: 8px;
}
.auth-links {
  margin-top: 16px;
  font-family: var(--font-data);
  font-size: 12px;
  text-align: center;
}
.auth-links a { color: var(--ink); font-weight: 700; }

.validation-summary-errors {
  color: var(--madder);
  font-family: var(--font-data);
  font-size: 12px;
  margin-bottom: 12px;
}
.validation-summary-errors ul { margin: 0; padding-left: 18px; }

/* Admin Table */
.admin-table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--font-data);
  font-size: 12px;
  margin-top: 14px;
}
.admin-table th, .admin-table td {
  border: 1px solid var(--ink);
  padding: 8px;
  text-align: left;
}
.admin-table th {
  background: var(--rule-faint);
  font-weight: 700;
}

/* Red Circular Delete Button with White X */
.btn-del-circle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 19px;
  height: 19px;
  min-width: 19px;
  border-radius: 50%;
  background-color: #d9383a;
  color: #ffffff !important;
  border: 1px solid #b31f24;
  font-size: 11px;
  font-weight: 800;
  line-height: 1;
  cursor: pointer;
  transition: transform 0.15s ease, background-color 0.15s ease;
  padding: 0;
  margin-left: 6px;
  vertical-align: middle;
  text-decoration: none !important;
  box-shadow: 0 1px 2px rgba(0,0,0,0.15);
}
.btn-del-circle:hover {
  transform: scale(1.18);
  background-color: #ba161c;
  color: #fff !important;
}

/* Searchable Food Combobox Picker */
.searchable-food-picker {
  position: relative;
  width: 100%;
}
.searchable-food-picker .search-input {
  width: 100%;
  padding: 8px 10px;
  font-size: 13px;
  border: 1.5px solid var(--ink);
  border-radius: 6px;
  background: var(--paper-card);
  box-sizing: border-box;
}
.searchable-food-picker .search-results {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  max-height: 220px;
  overflow-y: auto;
  background: #ffffff;
  border: 1.5px solid var(--ink);
  border-radius: 6px;
  box-shadow: 0 6px 16px rgba(0,0,0,0.2);
  z-index: 1050;
  display: none;
}
.searchable-food-picker .search-item {
  padding: 8px 10px;
  cursor: pointer;
  font-size: 12.5px;
  border-bottom: 1px solid var(--rule);
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: background 0.1s ease;
}
.searchable-food-picker .search-item:hover {
  background-color: var(--canary);
}
.searchable-food-picker .search-item .item-meta {
  font-family: var(--font-data);
  font-size: 11px;
  color: var(--ink-faint);
}

/* Template & Meal Plan Calculation Summary */
.template-calc-summary {
  margin-top: 12px;
  padding: 10px 12px;
  background: var(--paper-card);
  border: 1.5px dashed var(--rule);
  border-radius: 8px;
  font-family: var(--font-data);
  font-size: 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
}
.template-calc-summary .total-kcal {
  font-size: 13.5px;
  font-weight: 800;
  color: var(--ink);
}
.template-calc-summary .macros-badge {
  display: flex;
  gap: 8px;
  font-size: 11px;
  color: var(--ink-soft);
}

/* Badges for Clinic Prescribed vs User Custom */
.badge-clinic-prescribed {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: #e8f5e9;
  color: #2e7d32;
  border: 1px solid #a5d6a7;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 10px;
  font-weight: 700;
  font-family: var(--font-data);
}
.badge-user-custom {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: #fff8e1;
  color: #f57f17;
  border: 1px solid #ffe082;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 10px;
  font-weight: 700;
  font-family: var(--font-data);
}

/* Day Switcher Ribbon */
.day-switcher-ribbon {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  padding: 4px 0 10px 0;
  scrollbar-width: thin;
}
.day-switcher-btn {
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 700;
  font-family: var(--font-data);
  border: 1.5px solid var(--ink);
  border-radius: 6px;
  background: var(--paper-card);
  color: var(--ink);
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s ease;
}
.day-switcher-btn.active {
  background: var(--ink);
  color: #fff;
}

/* Print Optimization */
@media print {
  header.masthead, footer, .no-print, .btn, .user-nav, .tape, .admin-toolbar, .modal {
    display: none !important;
  }
  body, .sheet, .sheet-wide {
    background: #fff !important;
    color: #000 !important;
    padding: 0 !important;
    margin: 0 !important;
    width: 100% !important;
    max-width: 100% !important;
    box-shadow: none !important;
    border: none !important;
  }
  .print-only {
    display: block !important;
  }
  .printable-diet-sheet {
    display: block !important;
    padding: 10px !important;
  }
  .weekly-plan-table {
    width: 100% !important;
    border-collapse: collapse !important;
  }
  .weekly-plan-table th, .weekly-plan-table td {
    border: 1.5px solid #222 !important;
    padding: 6px !important;
    font-size: 11px !important;
  }
}

/* ============================================================
   ARABIC & RTL LOCALIZATION STYLES
   ============================================================ */
html[dir="rtl"], body.rtl {
  direction: rtl;
  text-align: right;
  font-family: 'Tajawal', 'Segoe UI Arabic', 'Segoe UI', Tahoma, sans-serif;
}

html[dir="rtl"] .user-nav {
  margin-right: auto;
  margin-left: 0;
}

html[dir="rtl"] .macro-col {
  border-left: none;
  border-right: 3px solid var(--ink);
  text-align: right;
}

html[dir="rtl"] .tab-scroll-container {
  flex-direction: row;
}

html[dir="rtl"] .stepper button:first-child {
  border-radius: 0 4px 4px 0;
}

html[dir="rtl"] .stepper button:last-child {
  border-radius: 4px 0 0 4px;
}

html[dir="rtl"] .btn-close-modal, html[dir="rtl"] .modal-close {
  left: 12px;
  right: auto;
}

html[dir="rtl"] .food-row td:last-child, html[dir="rtl"] .plan-item-row td:last-child {
  text-align: left !important;
}

html[dir="rtl"] .sec-head .sec-meta {
  margin-right: auto;
  margin-left: 0;
}

.lang-switch-btn {
  background: var(--paper-card);
  border: 1.5px solid var(--ink);
  padding: 3px 9px;
  border-radius: 4px;
  font-family: 'Tajawal', var(--font-data);
  font-size: 11.5px;
  font-weight: 800;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  color: var(--ink);
  transition: all 0.15s ease;
}
.lang-switch-btn:hover {
  background: var(--tape);
}

/* ============================================================
   RESPONSIVE MOBILE VIEWPORT & ZERO-OVERFLOW PROTECTION
   ============================================================ */
@media (max-width: 600px) {
  html, body {
    overflow-x: hidden !important;
    width: 100% !important;
    max-width: 100vw !important;
  }

  .sheet {
    width: 100% !important;
    max-width: 100% !important;
    overflow-x: hidden !important;
  }

  .masthead {
    padding-top: 14px !important;
  }

  .masthead-row, html[dir="rtl"] .masthead-row {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 8px !important;
  }

  .user-nav {
    width: 100% !important;
    flex-wrap: wrap !important;
    gap: 4px !important;
    margin: 0 !important;
    justify-content: flex-start !important;
  }

  .mast-btn, .lang-switch-btn {
    font-size: 10.5px !important;
    padding: 3px 7px !important;
    min-height: 28px !important;
  }

  .date-bar {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 8px !important;
    padding: 8px 10px !important;
  }

  .date-controls {
    display: flex !important;
    flex-wrap: wrap !important;
    align-items: center !important;
    justify-content: space-between !important;
    width: 100% !important;
    gap: 4px !important;
  }

  .date-display {
    font-size: 12px !important;
  }

  .date-picker-input {
    max-width: 130px !important;
    font-size: 11.5px !important;
    height: 26px !important;
  }

  .actions-dock {
    padding: 10px 14px calc(10px + env(safe-area-inset-bottom)) 14px !important;
  }

  .filter-bar {
    flex-direction: column !important;
    gap: 6px !important;
  }

  .search-box, .sort-select {
    width: 100% !important;
  }
}
