/* EasyMeals — styles. Design tokens (colors, type, spacing) are defined once in the :root blocks below. */

/* ---------- 0. Font tự host (tiêu đề) ---------- */
/* Fraunces 600, subset Latin + Latin Extended + Tiếng Việt, tự host.
   Tổng ~41 KB trên đĩa; trình duyệt chỉ tải tập unicode-range cần thiết (~24 KB cho trang VN).
   Gốc: Google Fonts (Fraunces v38), tự host để chạy offline trong PWA. */
@font-face {
  font-family: "Fraunces";
  font-style: normal;
  font-weight: 600;
  font-display: swap;
  src: url("/static/fonts/fraunces/fraunces-latin.woff2") format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
@font-face {
  font-family: "Fraunces";
  font-style: normal;
  font-weight: 600;
  font-display: swap;
  src: url("/static/fonts/fraunces/fraunces-latin-ext.woff2") format("woff2");
  unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20C4, U+2113, U+2C60-2C7F, U+A720-A7FF;
}
@font-face {
  font-family: "Fraunces";
  font-style: normal;
  font-weight: 600;
  font-display: swap;
  src: url("/static/fonts/fraunces/fraunces-vietnamese.woff2") format("woff2");
  unicode-range: U+0102-0103, U+0110-0111, U+0128-0129, U+0168-0169, U+01A0-01A1, U+01AF-01B0, U+0300-0301, U+0303-0304, U+0308-0309, U+0323, U+0329, U+1EA0-1EF9, U+20AB;
}

/* ---------- 1. Tokens ---------- */
:root {
  /* Color (light) */
  --bg: #f6f1e7;
  --surface: #fffbf3;
  --surface-sunken: #ede6d6;
  --ink: #2b2a22;
  --ink-muted: #6b6554;
  --line: #ddd3bf;
  --primary: #3f6b3a;
  --primary-strong: #2f5530;
  --on-primary: #fffbf3;
  --primary-soft: #dde8d3;
  --accent: #b5532f;
  --accent-soft: #f3ddd0;
  --honey: #c9962b;
  --danger: #9b2c2c;
  --danger-soft: #f6dcdc;
  --focus: #1f4fa3;
  color-scheme: light;

  /* Type */
  --font-body: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-display: "Fraunces", Georgia, "Times New Roman", serif;
  --text-xs: 0.8125rem;
  --text-sm: 0.9375rem;
  --text-base: 1rem;
  --text-lg: 1.25rem;
  --text-xl: 1.625rem;
  --text-2xl: 2.125rem;

  /* Space, shape */
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.5rem;
  --space-6: 2rem;
  --space-8: 3rem;
  --radius-sm: 6px;
  --radius: 10px;
  --radius-lg: 16px;
  --shadow-pop: 0 8px 24px rgb(43 42 34 / 0.16);

  /* Motion */
  --ease: cubic-bezier(0.2, 0, 0, 1);
  --dur: 160ms;

  /* Layout */
  --sidebar-w: 248px;
  --tabbar-h: 72px;

  /* Legacy aliases so existing page markup keeps working until each page is ported.
     Remove as pages move to the tokens above. */
  --primary-hover: var(--primary-strong);
  --primary-light: var(--primary-soft);
  --primary-text: var(--primary-strong);
  --accent-light: var(--accent-soft);
  --danger-light: var(--danger-soft);
  --text: var(--ink);
  --text-muted: var(--ink-muted);
  --border: var(--line);
  --shadow-sm: none;
  --shadow: none;
  --shadow-lg: var(--shadow-pop);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1a1d16;
    --surface: #232720;
    --surface-sunken: #171a13;
    --ink: #ece6d6;
    --ink-muted: #a7a28f;
    --line: #383d31;
    --primary: #8fbf82;
    --primary-strong: #a9d49c;
    --on-primary: #142013;
    --primary-soft: #2e3a2a;
    --accent: #e08a66;
    --accent-soft: #42291f;
    --honey: #d9ae4e;
    --danger: #e07070;
    --danger-soft: #3d2222;
    --focus: #8ab4ff;
    --shadow-pop: 0 8px 24px rgb(0 0 0 / 0.5);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #1a1d16;
  --surface: #232720;
  --surface-sunken: #171a13;
  --ink: #ece6d6;
  --ink-muted: #a7a28f;
  --line: #383d31;
  --primary: #8fbf82;
  --primary-strong: #a9d49c;
  --on-primary: #142013;
  --primary-soft: #2e3a2a;
  --accent: #e08a66;
  --accent-soft: #42291f;
  --honey: #d9ae4e;
  --danger: #e07070;
  --danger-soft: #3d2222;
  --focus: #8ab4ff;
  --shadow-pop: 0 8px 24px rgb(0 0 0 / 0.5);
  color-scheme: dark;
}

/* ---------- 2. Base ---------- */
*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: var(--text-base);
  line-height: 1.55;
  -webkit-text-size-adjust: 100%;
}

h1,
h2,
h3 {
  font-family: var(--font-display);
  font-weight: 600;
  line-height: 1.2;
  margin: 0;
  color: var(--ink);
}

h1 { font-size: var(--text-xl); }
h2 { font-size: var(--text-lg); margin-bottom: var(--space-2); }
h3 { font-size: 1.0625rem; }

p { margin: 0; }
ul, ol { margin: 0; }

a {
  color: var(--primary-strong);
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}

button,
input,
select,
textarea {
  font: inherit;
}

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.skip-link {
  position: absolute;
  left: var(--space-3);
  top: -100px;
  z-index: 300;
  padding: var(--space-2) var(--space-4);
  background: var(--primary);
  color: var(--on-primary);
  border-radius: var(--radius-sm);
}
.skip-link:focus { top: var(--space-3); }

.ico {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
}

/* ---------- 3. App shell: sidebar (desktop) / tab bar (mobile) ---------- */
.app-shell {
  min-height: 100vh;
}

.sidebar {
  display: none;
}

.brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 0 var(--space-3);
  color: var(--primary-strong);
  text-decoration: none;
}

.brand-name {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.375rem;
  color: var(--ink);
}

.nav-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.nav-link {
  position: relative;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-height: 44px;
  padding: 0 var(--space-3);
  border-radius: 8px;
  color: var(--ink);
  font-weight: 500;
  text-decoration: none;
  transition: background var(--dur) var(--ease);
}

.nav-link:hover {
  background: var(--surface-sunken);
}

.nav-link[aria-current="page"] {
  background: var(--primary-soft);
  color: var(--primary-strong);
  font-weight: 600;
  box-shadow: inset 3px 0 0 var(--primary);
}

.theme-toggle {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-height: 44px;
  width: 100%;
  padding: 0 var(--space-3);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
}
.theme-toggle:hover { background: var(--surface-sunken); }
.theme-toggle .icon-moon { display: none; }
:root[data-theme="dark"] .theme-toggle .icon-sun { display: none; }
:root[data-theme="dark"] .theme-toggle .icon-moon { display: inline; }

/* Mobile first: tab bar at the bottom */
.content {
  padding: var(--space-5) var(--space-4) calc(var(--tabbar-h) + var(--space-6) + env(safe-area-inset-bottom, 0px));
  max-width: 1100px;
  margin: 0 auto;
}

.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 100;
  display: flex;
  padding: var(--space-2) var(--space-2) calc(var(--space-2) + env(safe-area-inset-bottom, 0px));
  background: var(--surface);
  border-top: 1px solid var(--line);
}

.tab {
  flex: 1 1 0;
  min-width: 0;
  min-height: 56px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border-radius: 12px;
  color: var(--ink-muted);
  font-size: 0.75rem;
  font-weight: 500;
  text-decoration: none;
}

.tab-pill {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 56px;
  height: 30px;
  border-radius: 15px;
  transition: background var(--dur) var(--ease);
}

.tab[aria-current="page"] {
  color: var(--primary-strong);
  font-weight: 600;
}
.tab[aria-current="page"] .tab-pill {
  background: var(--primary-soft);
}

/* Nút đổi chủ đề trên tabbar mobile (chỉ hiện <720px; desktop dùng nút trong sidebar). */
.theme-tab {
  position: relative;
  padding: 0;
  color: var(--ink-muted);
  font-size: 0.75rem;
  font-weight: 500;
  text-decoration: none;
}
.theme-tab .ico { width: 22px; height: 22px; }
.theme-tab .icon-moon { display: none; }
[data-theme="dark"] .theme-tab .icon-sun { display: none; }
[data-theme="dark"] .theme-tab .icon-moon { display: inline; }

@media (min-width: 720px) {
  .theme-tab { display: none; }

  .app-shell {
    display: grid;
    grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  }

  .sidebar {
    position: sticky;
    top: 0;
    height: 100vh;
    display: flex;
    flex-direction: column;
    gap: var(--space-5);
    padding: 28px var(--space-4);
    background: var(--surface);
    border-right: 1px solid var(--line);
  }
  .sidebar .theme-toggle { margin-top: auto; }

  .tabbar { display: none; }

  .content {
    padding: var(--space-6) var(--space-6) var(--space-8);
  }
}

/* ---------- 4. Page chrome used by existing pages ---------- */
.section-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  flex-wrap: wrap;
  gap: var(--space-3);
  margin-bottom: var(--space-4);
}

p.subtitle {
  color: var(--ink-muted);
  font-size: var(--text-sm);
  margin-top: var(--space-1);
}

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--space-5);
  margin-bottom: var(--space-5);
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: var(--space-4);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: var(--space-4);
  margin-bottom: var(--space-5);
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--space-4);
  display: flex;
  flex-direction: column;
}

.stat-label {
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--ink-muted);
}

.stat-value {
  font-family: var(--font-display);
  font-size: var(--text-xl);
  font-weight: 600;
  color: var(--primary-strong);
  margin-top: var(--space-1);
  font-variant-numeric: tabular-nums;
}

/* ---------- 5. Forms ---------- */
.form-group { margin-bottom: var(--space-3); }

label {
  display: block;
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--ink-muted);
  margin-bottom: var(--space-1);
}

input,
select,
textarea {
  width: 100%;
  min-height: 44px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  background: var(--surface);
  transition: border-color var(--dur) var(--ease);
}
textarea { padding: var(--space-3) 14px; }
input:hover, select:hover, textarea:hover { border-color: var(--ink-muted); }
input::placeholder, textarea::placeholder { color: var(--ink-muted); }
input[type="number"] { text-align: right; font-variant-numeric: tabular-nums; }
/* G3: checkbox/radio không bị style như ô nhập chữ; giữ hình vuông dễ nhận biết. */
input[type="checkbox"],
input[type="radio"] {
  width: 20px;
  height: 20px;
  min-height: 20px;
  padding: 0;
  flex: none;
  accent-color: var(--primary);
}
.shopping-check {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  flex: none;
  cursor: pointer;
}
.shopping-item-row { flex-wrap: wrap; }
.shopping-item-label { flex: 1 1 220px; min-width: 0; }
@media (max-width: 520px) {
  .shopping-item-label { flex-basis: 100%; }
}

.form-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: var(--space-3);
}

/* ---------- 6. Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: 44px;
  padding: 0 18px;
  font-size: var(--text-sm);
  font-weight: 600;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink);
  cursor: pointer;
  text-decoration: none;
  transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease);
}
.btn:hover { background: var(--surface-sunken); }
.btn:disabled,
.btn[aria-disabled="true"] { opacity: 0.55; cursor: not-allowed; }

.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--on-primary);
}
.btn-primary:hover { background: var(--primary-strong); border-color: var(--primary-strong); }
:root[data-theme="dark"] .btn-primary:hover { color: var(--on-primary); }

.btn-outline { background: transparent; }

.btn-danger {
  background: var(--danger-soft);
  border-color: var(--danger);
  color: var(--ink);
}
.btn-danger:hover { background: var(--danger-soft); border-color: var(--ink); }

.btn-sm { min-height: 44px; padding: 0 var(--space-3); font-size: var(--text-xs); }
/* G3: nút hành động nhỏ trong bảng vẫn phải đủ vùng chạm 44×44 px. */
.btn-xs { min-width: 44px; min-height: 44px; padding: 0 10px; font-size: var(--text-xs); }

/* ---------- 7. Chips, badges, filters ---------- */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 10px;
  font-size: var(--text-xs);
  font-weight: 600;
  border-radius: var(--radius-sm);
  background: var(--surface-sunken);
  color: var(--ink);
}
.badge-man { background: var(--accent-soft); color: var(--ink); }
.badge-rau { background: var(--primary-soft); color: var(--primary-strong); }
.badge-canh { background: var(--surface-sunken); color: var(--ink); box-shadow: inset 0 0 0 1px var(--honey); }
.badge-new { background: var(--primary-soft); color: var(--primary-strong); }
.badge-leftover { background: transparent; color: var(--ink); box-shadow: inset 0 0 0 1px var(--honey); }
.badge-eaten { background: var(--primary-soft); color: var(--primary-strong); }

.filter-btn,
.meta-chip {
  min-height: 44px;
  padding: 0 16px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink);
  font-weight: 500;
  cursor: pointer;
}
.filter-btn.active,
.meta-chip.active {
  background: var(--primary-soft);
  border-color: var(--primary);
  color: var(--primary-strong);
  font-weight: 600;
}

/* ---------- 8. Autocomplete ---------- */
.autocomplete-wrapper { position: relative; }

.autocomplete-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  margin-top: var(--space-1);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  max-height: 220px;
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
  z-index: 50;
  box-shadow: var(--shadow-pop);
  display: none;
}

.autocomplete-item {
  min-height: 48px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  padding: var(--space-3) var(--space-4);
  cursor: pointer;
  border-bottom: 1px solid var(--line);
  font-size: var(--text-sm);
}
.autocomplete-item:last-child { border-bottom: none; }
.autocomplete-item:hover,
.autocomplete-item.selected {
  background: var(--primary-soft);
  color: var(--primary-strong);
}

/* ---------- 9. Meal plan (existing markup) ---------- */
.day-card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  margin-bottom: var(--space-5);
  overflow: hidden;
}

.day-header {
  background: var(--surface-sunken);
  padding: var(--space-3) var(--space-5);
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.day-title {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.125rem;
}

.meal-block {
  padding: var(--space-4) var(--space-5);
  border-bottom: 1px solid var(--line);
}
.meal-block:last-child { border-bottom: none; }

.meal-title-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: var(--space-3);
}

.dish-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: var(--space-3);
}

.dish-item {
  border-radius: 8px;
  padding: var(--space-3);
  background: var(--surface-sunken);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: var(--space-2);
}

.dish-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: var(--space-2);
}

.dish-name {
  font-weight: 600;
  font-size: var(--text-sm);
}

/* ---------- 10. Toast and modal ---------- */
#toast-container {
  position: fixed;
  right: var(--space-4);
  bottom: calc(var(--tabbar-h) + var(--space-4) + env(safe-area-inset-bottom, 0px));
  z-index: 250;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}
@media (min-width: 720px) {
  #toast-container { bottom: var(--space-5); right: var(--space-5); }
}

.toast {
  background: var(--ink);
  color: var(--bg);
  padding: var(--space-3) var(--space-4);
  border-radius: 8px;
  font-size: var(--text-sm);
  box-shadow: var(--shadow-pop);
  transition: opacity var(--dur) var(--ease);
  animation: toast-in var(--dur) var(--ease);
}
.toast-error { background: var(--danger); color: var(--surface); }
:root[data-theme="dark"] .toast-error { color: var(--bg); }
.toast-success { background: var(--primary); color: var(--on-primary); }

@keyframes toast-in {
  from { transform: translateY(12px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgb(26 29 22 / 0.55);
  z-index: 200;
  justify-content: center;
  align-items: center;
  padding: var(--space-4);
}

.modal {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  max-width: 600px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: var(--shadow-pop);
  padding: var(--space-5);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: var(--space-3);
  margin-bottom: var(--space-4);
}

/* ---------- 10.5. Trạng thái trống / tải / lỗi + thanh tồn kho ---------- */
.empty-state,
.error-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: var(--space-8) var(--space-4);
}
.empty-state svg,
.error-state svg {
  width: 48px;
  height: 48px;
  margin-bottom: var(--space-3);
  color: var(--ink-muted);
}
.error-state svg { color: var(--accent); }
.empty-state p,
.error-state p {
  color: var(--ink-muted);
  font-size: var(--text-sm);
  max-width: 320px;
  margin: 0 auto var(--space-4) auto;
}
.empty-state .btn,
.error-state .btn { min-width: 140px; }

.skeleton {
  background: var(--surface-sunken);
  border-radius: var(--radius-sm);
  position: relative;
  overflow: hidden;
}
.skeleton-text {
  height: var(--text-sm);
  margin-bottom: var(--space-2);
  width: 100%;
}
.skeleton-title {
  height: var(--text-lg);
  margin-bottom: var(--space-3);
  width: 60%;
}

.stock-bar {
  width: 100%;
  height: 10px;
  background: var(--surface-sunken);
  border-radius: 999px;
  overflow: hidden;
  margin-top: var(--space-1);
}
.stock-bar-fill {
  height: 100%;
  background: var(--primary);
  border-radius: 999px;
  transition: width var(--dur) var(--ease);
}
.stock-bar-fill.low { background: var(--accent); }

.dish-img {
  width: 48px;
  height: 48px;
  color: var(--ink-muted);
}

/* ---------- 11. Responsive tweaks and motion ---------- */
@media (max-width: 719px) {
  .form-row { grid-template-columns: 1fr; }
  .dish-list { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
