/* ============================================================
   CSS Custom Properties (Design Tokens)
   ============================================================ */
:root {
  /* Colors */
  --color-primary: #2563eb;
  --color-primary-hover: #1d4ed8;
  --color-danger: #dc2626;
  --color-danger-bg: #fee2e2;
  --color-text: #111827;
  --color-text-muted: #6b7280;
  --color-border: #e5e7eb;
  --color-bg: #f9fafb;
  --color-surface: #ffffff;
  --color-surface-muted: #f3f4f6;
  --color-nav: #111827;

  /* Spacing */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --space-7: 28px;
  --space-8: 32px;

  /* Border radius */
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-pill: 20px;
}

/* ============================================================
   Base Reset
   ============================================================ */
*, *::before, *::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Inter, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  color: var(--color-text);
  background: var(--color-bg);
  line-height: 1.5;
}

/* ============================================================
   Layout
   ============================================================ */
.container {
  max-width: 800px;
  margin: 0 auto;
  padding: 0 var(--space-5) var(--space-8);
}

/* ============================================================
   Navigation
   ============================================================ */
nav {
  background-color: var(--color-nav);
  color: white;
  padding: var(--space-4);
  margin-bottom: var(--space-8);
}

.nav-content {
  max-width: 800px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo {
  font-weight: bold;
  font-size: 1.2rem;
  color: white;
}

nav ul {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  gap: var(--space-5);
}

nav a {
  color: #fff;
  text-decoration: none;
  opacity: 0.75;
  transition: opacity 0.2s;
}

nav a:hover,
nav a.active {
  opacity: 1;
}

nav a.active {
  font-weight: 600;
}

.logout-btn {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: white;
  padding: var(--space-1) var(--space-3);
  cursor: pointer;
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  font-family: inherit;
}

.logout-btn:hover {
  background: rgba(255, 255, 255, 0.2);
}

/* ============================================================
   Typography
   ============================================================ */
h1 {
  margin: 0 0 var(--space-6);
  font-size: 1.75rem;
}

h2 {
  margin: 0 0 var(--space-4);
  font-size: 1.25rem;
}

/* ============================================================
   Buttons
   ============================================================ */
.btn {
  display: inline-flex;
  align-items: center;
  padding: var(--space-2) var(--space-4);
  border: none;
  border-radius: var(--radius-sm);
  font-size: 1rem;
  font-family: inherit;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s;
  line-height: 1.5;
}

.btn-primary {
  background: var(--color-primary);
  color: #fff;
}

.btn-primary:hover {
  background: var(--color-primary-hover);
}

.btn-secondary {
  background: var(--color-surface-muted);
  color: var(--color-text);
}

.btn-secondary:hover {
  background: var(--color-border);
}

.btn-danger {
  background: var(--color-danger-bg);
  color: var(--color-danger);
}

.btn-danger:hover {
  background: #fecaca;
}

.btn-sm {
  padding: var(--space-1) var(--space-2);
  font-size: 0.875rem;
}

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

.form-group label {
  display: block;
  margin-bottom: var(--space-1);
  font-weight: 600;
  font-size: 0.875rem;
  color: var(--color-text-muted);
}

.form-row {
  display: flex;
  gap: var(--space-4);
}

.form-row .form-group {
  flex: 1;
}

input[type="text"],
input[type="number"],
input[type="email"],
input[type="date"],
select {
  width: 100%;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 1rem;
  color: var(--color-text);
  background: var(--color-surface);
  box-sizing: border-box;
  overflow: hidden;
  text-overflow: ellipsis;
}

select {
  -webkit-appearance: none;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16'%3E%3Cpath d='M4 6l4 4 4-4' stroke='%236b7280' stroke-width='1.5' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right var(--space-3) center;
  padding-right: calc(var(--space-3) + 24px);
  cursor: pointer;
}

input:focus,
select:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

/* ============================================================
   Segmented Control
   ============================================================ */
.toggle-container {
  display: inline-flex;
  background: var(--color-surface-muted);
  border-radius: var(--radius-pill);
  padding: var(--space-1);
}

.toggle-container input[type="radio"] {
  display: none;
}

.toggle-container label {
  padding: var(--space-2) var(--space-4);
  cursor: pointer;
  border-radius: var(--radius-pill);
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--color-text-muted);
  transition: all 0.2s ease;
  margin: 0;
  display: inline-block;
}

.toggle-container input[type="radio"]:checked + label {
  background: var(--color-surface);
  color: var(--color-text);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

/* ============================================================
   Stat Cards
   ============================================================ */
.stats-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: var(--space-5);
  margin-bottom: var(--space-8);
}

.stat-card {
  background: var(--color-surface);
  padding: var(--space-4);
  border-radius: var(--radius-md);
  text-align: center;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
  border: 1px solid var(--color-border);
}

.stat-card h3 {
  margin: 0 0 var(--space-2);
  font-size: 0.75rem;
  color: var(--color-text-muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.stat-card span {
  font-size: 2rem;
  font-weight: 700;
  color: var(--color-text);
}

/* ============================================================
   Period Selector
   ============================================================ */
.period-selector {
  display: flex;
  gap: var(--space-2);
  margin-bottom: var(--space-8);
  justify-content: center;
}

.period-btn {
  padding: var(--space-2) var(--space-5);
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  cursor: pointer;
  border-radius: var(--radius-pill);
  font-size: 0.9375rem;
  font-family: inherit;
  color: var(--color-text);
  transition: background 0.15s, border-color 0.15s;
}

.period-btn:hover {
  background: var(--color-surface-muted);
}

.period-btn.active {
  background: var(--color-nav);
  color: #fff;
  border-color: var(--color-nav);
}

/* ============================================================
   Lists
   ============================================================ */
.item-list,
.logs-section ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.item-list li,
.logs-section li {
  padding: var(--space-4);
  border-bottom: 1px solid var(--color-border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.item-list li:last-child,
.logs-section li:last-child {
  border-bottom: none;
}

.item-list li > div {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ============================================================
   Panels (form sections)
   ============================================================ */
.panel {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  padding: var(--space-5);
  border-radius: var(--radius-md);
  margin-bottom: var(--space-8);
}

/* ============================================================
   Controls Bar (date picker row)
   ============================================================ */
.controls-bar {
  display: flex;
  align-items: center;
  gap: var(--space-4);
  margin-bottom: var(--space-5);
  padding: var(--space-4);
  background: var(--color-surface-muted);
  border-radius: var(--radius-md);
}

.controls-bar label {
  font-weight: 600;
  font-size: 0.875rem;
  color: var(--color-text-muted);
  white-space: nowrap;
  margin-bottom: 0;
}

.controls-bar input[type="date"] {
  width: auto;
}

/* ============================================================
   Toast Notifications
   ============================================================ */
.toast-container {
  position: fixed;
  bottom: var(--space-6);
  right: var(--space-6);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  z-index: 1000;
}

.toast {
  padding: var(--space-3) var(--space-5);
  border-radius: var(--radius-md);
  font-size: 0.9375rem;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  animation: toast-in 0.2s ease;
  max-width: 360px;
  font-family: inherit;
}

.toast-success {
  background: #dcfce7;
  color: #166534;
  border: 1px solid #bbf7d0;
}

.toast-error {
  background: var(--color-danger-bg);
  color: var(--color-danger);
  border: 1px solid #fca5a5;
}

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

/* ============================================================
   Nutrient Rows
   ============================================================ */
.nutrient-row {
  display: grid;
  grid-template-columns: 2fr 1fr 1fr auto;
  gap: var(--space-3);
  margin-bottom: var(--space-3);
  align-items: center;
}

.nutrient-row > span:first-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.nutrient-row input {
  width: 100%;
}

/* ============================================================
   Food Info / Food Actions
   ============================================================ */
.food-info {
  min-width: 0;
  overflow: hidden;
}

.food-info strong {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.food-info small {
  color: var(--color-text-muted);
}

.food-actions {
  display: flex;
  gap: var(--space-2);
  align-items: center;
  white-space: nowrap;
  flex-shrink: 0;
}

/* ============================================================
   Recipe Totals
   ============================================================ */
#recipe-totals {
  margin-top: var(--space-3);
  padding: var(--space-3);
  background: var(--color-surface-muted);
  border-radius: var(--radius-sm);
}

/* ============================================================
   Ingredient Add Row
   ============================================================ */
.ingredient-add-row {
  display: flex;
  gap: var(--space-3);
  margin-bottom: var(--space-4);
  overflow: hidden;
}

.ingredient-add-row select {
  flex: 1 1 0;
  min-width: 0;
}

.ingredient-add-row input[type="number"] {
  width: 100px;
  flex-shrink: 0;
}

/* ============================================================
   Specific overrides
   ============================================================ */
#ingredient-search {
  min-width: 0;
}

#ingredients-section .form-group select {
  max-width: 100%;
}

#recipe-servings {
  width: 120px;
}

/* ============================================================
   Logs section (dashboard)
   ============================================================ */
.logs-section {
  margin-top: var(--space-6);
}

/* ============================================================
   Login Page
   ============================================================ */
.login-page {
  max-width: 420px;
  margin: var(--space-8) auto;
  padding: 0 var(--space-4);
}

.login-page h1 {
  text-align: center;
  margin-bottom: var(--space-8);
}

.login-page .panel {
  margin-bottom: var(--space-6);
}

/* ============================================================
   Meal Badge (extracted from inline JS styles)
   ============================================================ */
.meal-badge {
  background: var(--color-surface-muted);
  padding: 2px 6px;
  border-radius: var(--radius-sm);
  font-size: 0.8em;
  margin-left: 10px;
}

/* ============================================================
   Mobile Navigation Toggle
   ============================================================ */
.nav-toggle {
  display: none;
  background: none;
  border: none;
  color: white;
  font-size: 1.5rem;
  cursor: pointer;
  padding: var(--space-1) var(--space-2);
  line-height: 1;
}

/* ============================================================
   Responsive Styles (Mobile ≤ 600px)
   ============================================================ */
@media (max-width: 600px) {

  /* --- Navigation --- */
  .nav-toggle {
    display: block;
  }

  .nav-content {
    flex-wrap: wrap;
  }

  .nav-content > ul {
    display: none !important;
    flex-direction: column;
    width: 100%;
    gap: 0;
    padding-top: var(--space-3);
  }

  nav.nav-open .nav-content > ul {
    display: flex !important;
  }

  nav ul li {
    padding: var(--space-2) 0;
  }

  nav a {
    font-size: 1rem;
    opacity: 0.9;
  }

  #auth-action {
    display: none;
    width: 100%;
    padding-top: var(--space-2);
  }

  nav.nav-open #auth-action {
    display: block;
  }

  .logout-btn {
    width: 100%;
    text-align: center;
    padding: var(--space-2);
  }

  /* --- Container & Typography --- */
  .container {
    padding: 0 var(--space-3) var(--space-6);
  }

  h1 {
    font-size: 1.5rem;
    margin-bottom: var(--space-4);
  }

  /* --- Forms: Stack on Mobile --- */
  .form-row {
    flex-direction: column !important;
    gap: 0;
  }

  .ingredient-add-row {
    flex-direction: column;
  }

  .ingredient-add-row input[type="number"] {
    width: 100%;
  }

  .nutrient-row {
    grid-template-columns: 1fr 1fr auto;
  }

  /* --- Buttons: Full Width --- */
  .panel .btn {
    width: 100%;
    justify-content: center;
  }

  form .btn {
    width: 100%;
    justify-content: center;
  }

  .container .btn-primary {
    width: 100%;
  }

  /* Keep inline button groups (edit/delete) compact */
  .food-actions .btn {
    width: auto;
  }

  /* --- List Items: Better Touch Targets --- */
  .item-list li,
  .logs-section li {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-3);
    padding: var(--space-4) var(--space-3);
  }

  .food-actions {
    width: 100%;
  }

  /* --- Controls Bar --- */
  .controls-bar {
    flex-direction: column;
    align-items: stretch;
  }

  .controls-bar input[type="date"] {
    width: 100%;
  }

  /* --- Period Selector: scroll horizontally --- */
  .period-selector {
    overflow-x: auto;
    justify-content: flex-start;
    -webkit-overflow-scrolling: touch;
  }

  .period-btn {
    flex-shrink: 0;
  }

  /* --- Toast: Full Width at Bottom --- */
  .toast-container {
    right: var(--space-3);
    left: var(--space-3);
    bottom: var(--space-4);
  }

  .toast {
    max-width: 100%;
  }

  /* --- Stats: 2-column grid --- */
  .stats-container {
    grid-template-columns: repeat(2, 1fr);
  }
}
