/* ============================================
   CSS Custom Properties & Theme
   ============================================ */
:root {
  --primary: #2db87a;
  --primary-dark: #239963;
  --primary-light: #e8f8f0;
  --bg: #f5f7f6;
  --card-bg: #ffffff;
  --text: #1a1a1a;
  --text-secondary: #666;
  --border: #e0e0e0;
  --shadow: 0 2px 8px rgba(0,0,0,0.08);
  --shadow-lg: 0 4px 16px rgba(0,0,0,0.12);
  --radius: 12px;
  --radius-sm: 8px;
  --font: 'Segoe UI', system-ui, -apple-system, sans-serif;

  --amber: #f59e0b;
  --green: #22c55e;
  --red: #ef4444;
}

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

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  padding-bottom: 100px;
}

/* ============================================
   Top Bar
   ============================================ */
#top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 24px;
  background: var(--card-bg);
  box-shadow: var(--shadow);
  position: sticky;
  top: 0;
  z-index: 100;
}

.top-bar__left {
  display: flex;
  align-items: baseline;
  gap: 12px;
}

.top-bar__title {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--primary);
}

.top-bar__date {
  font-size: 0.9rem;
  color: var(--text-secondary);
}

.top-bar__right {
  display: flex;
  gap: 8px;
}

/* ============================================
   Buttons
   ============================================ */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 600;
  transition: all 0.2s;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

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

.btn--primary:hover:not(:disabled) {
  background: var(--primary-dark);
}

.btn--secondary {
  background: var(--primary-light);
  color: var(--primary-dark);
}

.btn--secondary:hover:not(:disabled) {
  background: #d0f0e0;
}

.btn--large {
  padding: 12px 28px;
  font-size: 1rem;
}

.btn--regen {
  background: var(--primary-light);
  color: var(--primary);
  padding: 8px 12px;
  font-size: 1.1rem;
  border-radius: var(--radius-sm);
}

.btn--regen:hover:not(:disabled) {
  background: var(--primary);
  color: white;
}

.btn--close {
  background: none;
  color: var(--text-secondary);
  font-size: 1.5rem;
  padding: 4px 8px;
  line-height: 1;
}

.btn--close:hover {
  color: var(--text);
}

/* ============================================
   Meal Cards
   ============================================ */
#meal-cards {
  max-width: 720px;
  margin: 24px auto;
  padding: 0 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.meal-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  transition: box-shadow 0.2s;
}

.meal-card:hover {
  box-shadow: var(--shadow-lg);
}

.meal-card.loading {
  opacity: 0.6;
  pointer-events: none;
}

.meal-card.loading .meal-card__body::after {
  content: '';
  display: block;
  width: 24px;
  height: 24px;
  margin: 12px auto;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.meal-card.error {
  border-left: 4px solid var(--red);
}

.meal-card__header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.time-badge {
  background: var(--primary-light);
  color: var(--primary-dark);
  padding: 4px 10px;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 700;
  white-space: nowrap;
}

.meal-name {
  font-size: 1.05rem;
  font-weight: 700;
}

.meal-card__body {
  min-height: 40px;
}

.meal-description {
  color: var(--text-secondary);
  font-style: italic;
  margin-bottom: 8px;
}

.meal-description.generated {
  color: var(--text);
  font-style: normal;
  font-weight: 600;
}

.meal-items {
  list-style: none;
  padding: 0;
}

.meal-items li {
  padding: 3px 0;
  font-size: 0.9rem;
  color: var(--text);
  padding-left: 16px;
  position: relative;
}

.meal-items li::before {
  content: '•';
  position: absolute;
  left: 0;
  color: var(--primary);
  font-weight: bold;
}

.meal-items .prep-time {
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-top: 6px;
  padding-left: 0;
}

.meal-items .prep-time::before {
  content: '⏱';
}

.macro-pills {
  display: flex;
  gap: 8px;
  margin-top: 12px;
  flex-wrap: wrap;
}

.pill {
  padding: 4px 10px;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 600;
}

.pill--kcal { background: #fef3c7; color: #92400e; }
.pill--protein { background: #dbeafe; color: #1e40af; }
.pill--carbs { background: #fce7f3; color: #9d174d; }
.pill--fat { background: #e0e7ff; color: #3730a3; }

.meal-card__regen {
  display: flex;
  gap: 8px;
  margin-top: 14px;
  align-items: center;
}

.regen-input {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  outline: none;
  transition: border-color 0.2s;
}

.regen-input:focus {
  border-color: var(--primary);
}

/* ============================================
   Supplement Card
   ============================================ */
#supplement-card {
  max-width: 720px;
  margin: 24px auto;
  padding: 20px;
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-left: auto;
  margin-right: auto;
  padding-left: 36px;
  padding-right: 36px;
}

.supplement-card__title {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--primary);
  margin-bottom: 16px;
}

.supplement-section {
  margin-bottom: 14px;
}

.supplement-section h3 {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 6px;
}

.supplement-list {
  list-style: none;
}

.supplement-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 0;
  font-size: 0.9rem;
  flex-wrap: wrap;
}

.supp-badge {
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 0.7rem;
  font-weight: 700;
}

.supp-badge--daily { background: #dbeafe; color: #1e40af; }
.supp-badge--weekly { background: #fef3c7; color: #92400e; }
.supp-badge--optional { background: #f3f4f6; color: #6b7280; }
.supp-badge--due { background: #fecaca; color: #991b1b; animation: pulse-bg 1.5s infinite; }
.supp-badge--ok { background: #d1fae5; color: #065f46; }

@keyframes pulse-bg {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

.supp-checkbox {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.8rem;
  color: var(--text-secondary);
  cursor: pointer;
}

.supp-checkbox input[type="checkbox"] {
  accent-color: var(--primary);
}

/* ============================================
   Macro Summary
   ============================================ */
#macro-summary {
  max-width: 720px;
  margin: 24px auto;
  padding: 20px 36px;
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.macro-summary__title {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--primary);
  margin-bottom: 16px;
}

.macro-bar {
  display: grid;
  grid-template-columns: 70px 1fr 110px;
  align-items: center;
  gap: 12px;
  margin-bottom: 10px;
}

.macro-bar label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.progress-track {
  height: 10px;
  background: #e5e7eb;
  border-radius: 5px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  border-radius: 5px;
  transition: width 0.5s ease, background-color 0.3s;
  width: 0%;
  background: var(--amber);
}

.progress-fill.green { background: var(--green); }
.progress-fill.amber { background: var(--amber); }
.progress-fill.red { background: var(--red); }

.macro-text {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-secondary);
  text-align: right;
}

/* ============================================
   Action Bar
   ============================================ */
#action-bar {
  max-width: 720px;
  margin: 24px auto;
  padding: 0 16px;
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
}

/* ============================================
   Overlay & Panels
   ============================================ */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 200;
  display: flex;
  transition: opacity 0.3s;
}

.overlay.hidden {
  display: none;
}

/* Side panel (history) */
.side-panel {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 420px;
  max-width: 100%;
  background: var(--card-bg);
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  animation: slideIn 0.3s ease;
}

@keyframes slideIn {
  from { transform: translateX(100%); }
  to { transform: translateX(0); }
}

.side-panel__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.side-panel__header h2 {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--primary);
}

.side-panel__body {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
}

/* Modal panel (settings, confirm) */
.modal-panel {
  margin: auto;
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  width: 440px;
  max-width: 92%;
  animation: fadeScale 0.2s ease;
}

.modal-panel--small {
  width: 360px;
}

@keyframes fadeScale {
  from { transform: scale(0.95); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.modal-panel__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.modal-panel__header h2 {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--primary);
}

.modal-panel__body {
  padding: 20px;
}

/* Settings specifics */
.settings-group {
  margin-bottom: 16px;
}

.settings-group legend {
  font-weight: 700;
  margin-bottom: 8px;
  color: var(--text);
}

.settings-group label {
  display: block;
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 4px;
  margin-top: 8px;
}

.radio-label {
  display: flex !important;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  padding: 4px 0;
}

.radio-label input {
  accent-color: var(--primary);
}

.settings-input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  outline: none;
  transition: border-color 0.2s;
}

.settings-input:focus {
  border-color: var(--primary);
}

/* Confirm */
.confirm-actions {
  display: flex;
  gap: 10px;
  margin-top: 16px;
  justify-content: flex-end;
}

/* ============================================
   History Entries
   ============================================ */
.history-entry {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  margin-bottom: 12px;
  overflow: hidden;
}

.history-entry__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  cursor: pointer;
  background: var(--primary-light);
  transition: background 0.2s;
}

.history-entry__header:hover {
  background: #d0f0e0;
}

.history-entry__date {
  font-weight: 700;
  font-size: 0.95rem;
}

.history-entry__macros {
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.history-entry__body {
  padding: 12px 16px;
  display: none;
}

.history-entry__body.expanded {
  display: block;
}

.history-meal {
  margin-bottom: 8px;
  font-size: 0.85rem;
}

.history-meal strong {
  color: var(--primary-dark);
}

.history-entry__actions {
  padding: 8px 16px;
  display: none;
  border-top: 1px solid var(--border);
}

.history-entry__body.expanded + .history-entry__actions {
  display: block;
}

.empty-state {
  color: var(--text-secondary);
  font-style: italic;
  text-align: center;
  padding: 40px 0;
}

/* ============================================
   Toast
   ============================================ */
#toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 300;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  background: var(--text);
  color: white;
  padding: 12px 20px;
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  box-shadow: var(--shadow-lg);
  animation: toastIn 0.3s ease;
  pointer-events: auto;
  max-width: 360px;
}

.toast--success { background: #065f46; }
.toast--error { background: #991b1b; }
.toast--warning { background: #92400e; }

@keyframes toastIn {
  from { transform: translateY(20px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.toast.removing {
  animation: toastOut 0.3s ease forwards;
}

@keyframes toastOut {
  to { transform: translateY(20px); opacity: 0; }
}

/* ============================================
   Utility
   ============================================ */
.hidden { display: none !important; }

/* ============================================
   Responsive
   ============================================ */
@media (max-width: 600px) {
  #top-bar {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
    padding: 12px 16px;
  }

  .top-bar__right {
    width: 100%;
    justify-content: flex-end;
  }

  .btn {
    padding: 6px 12px;
    font-size: 0.8rem;
  }

  #meal-cards {
    padding: 0 8px;
  }

  .meal-card {
    padding: 14px;
  }

  .side-panel {
    width: 100%;
  }

  .macro-bar {
    grid-template-columns: 60px 1fr 90px;
    gap: 8px;
  }

  #supplement-card,
  #macro-summary {
    padding-left: 16px;
    padding-right: 16px;
  }
}

/* ============================================
   Print Styles — A5 Portrait, Black & White
   ============================================ */
@media print {
  @page {
    size: A5 portrait;
    margin: 12mm 10mm;
  }

  /* Hide all interactive chrome */
  #top-bar button,
  .top-bar__right,
  .meal-card__regen,
  #macro-summary,
  #action-bar,
  #history-overlay,
  #settings-overlay,
  #confirm-overlay,
  #toast-container {
    display: none !important;
  }

  /* Force black and white */
  * {
    color: #000 !important;
    background: #fff !important;
    box-shadow: none !important;
    text-shadow: none !important;
  }

  body {
    padding: 0;
    min-height: auto;
  }

  #top-bar {
    position: static;
    border-bottom: 1px solid #ccc;
    padding: 4mm 0;
  }

  .top-bar__title {
    font-size: 13pt;
  }

  .top-bar__date {
    font-size: 10pt;
  }

  /* Compact card layout for A5 */
  #meal-cards {
    margin: 2mm 0;
    padding: 0;
    gap: 0;
  }

  .meal-card {
    break-inside: avoid;
    border: 1px solid #ccc;
    border-radius: 0;
    margin-bottom: 3mm;
    padding: 3mm;
    page-break-inside: avoid;
  }

  .meal-card__header {
    margin-bottom: 2mm;
  }

  .time-badge {
    font-size: 8pt;
    padding: 1px 6px;
    border: 1px solid #999;
  }

  .meal-name {
    font-size: 10pt;
    font-weight: bold;
  }

  .meal-description {
    font-size: 9pt;
    margin-bottom: 1mm;
  }

  .meal-items {
    font-size: 8pt;
  }

  .meal-items li {
    padding: 1px 0 1px 12px;
    font-size: 8pt;
  }

  .macro-pills {
    margin-top: 2mm;
  }

  .macro-pills span {
    font-size: 7pt;
    padding: 1px 5px;
    border: 1px solid #999;
  }

  /* Supplement section compact */
  #supplement-card {
    break-inside: avoid;
    page-break-inside: avoid;
    border: 1px solid #ccc;
    border-radius: 0;
    margin: 3mm 0;
    padding: 3mm;
  }

  .supplement-card__title {
    font-size: 10pt;
  }

  .supplement-section h3 {
    font-size: 8pt;
  }

  .supplement-list li {
    font-size: 8pt;
    padding: 2px 0;
  }

  .supp-checkbox {
    display: none !important;
  }
}
