:root {
  --bg: #f7f6f3;
  --surface: #ffffff;
  --text: #1a1a1a;
  --text-muted: #6b6b6b;
  --border: #e8e6e1;
  --accent: #2d5a3d;
  --accent-soft: #edf3ec;
  --accent-text: #2d5a3d;
  --protein: #9f2f2d;
  --protein-bg: #fdebec;
  --carbs: #1f6c9f;
  --carbs-bg: #e1f3fe;
  --fat: #956400;
  --fat-bg: #fbf3db;
  --kcal: #4a4a4a;
  --kcal-bg: #f0f0ee;
  --danger: #9f2f2d;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
  --nav-height: 64px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --font: 'DM Sans', system-ui, sans-serif;
  --font-display: 'Instrument Serif', Georgia, serif;
}

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

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
}

#app {
  min-height: 100dvh;
  padding-bottom: calc(var(--nav-height) + var(--safe-bottom));
}

.loading-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  gap: 1rem;
  color: var(--text-muted);
}

.loading-mark {
  width: 56px;
  height: 56px;
  border-radius: var(--radius);
  background: var(--text);
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 1.1rem;
  letter-spacing: 0.05em;
}

.hidden { display: none !important; }

/* Auth */
.auth-screen {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 2rem 1.5rem;
  max-width: 420px;
  margin: 0 auto;
}

.auth-brand {
  margin-bottom: 2.5rem;
}

.auth-brand h1 {
  font-family: var(--font-display);
  font-size: 2.5rem;
  font-weight: 400;
  margin: 0 0 0.5rem;
  letter-spacing: -0.02em;
  line-height: 1.1;
}

.auth-brand p {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.95rem;
}

.auth-tabs {
  display: flex;
  gap: 0;
  margin-bottom: 1.5rem;
  border-bottom: 1px solid var(--border);
}

.auth-tab {
  flex: 1;
  padding: 0.75rem;
  background: none;
  border: none;
  font-family: var(--font);
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  transition: color 0.2s, border-color 0.2s;
}

.auth-tab.active {
  color: var(--text);
  border-bottom-color: var(--text);
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* Layout */
.page-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: rgba(247, 246, 243, 0.92);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  padding: 1rem 1.25rem 0.75rem;
  border-bottom: 1px solid var(--border);
}

.page-header h2 {
  font-family: var(--font-display);
  font-size: 1.75rem;
  font-weight: 400;
  margin: 0;
  letter-spacing: -0.02em;
}

.page-header .subtitle {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-top: 0.15rem;
}

.page-content {
  padding: 1rem 1.25rem 2rem;
  max-width: 640px;
  margin: 0 auto;
}

/* Forms */
.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.field label {
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

input, select, textarea {
  font-family: var(--font);
  font-size: 1rem;
  padding: 0.75rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  width: 100%;
  transition: border-color 0.2s;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--text);
}

input::placeholder { color: #aaa; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  font-family: var(--font);
  font-size: 0.95rem;
  font-weight: 500;
  padding: 0.8rem 1.25rem;
  border-radius: var(--radius-sm);
  border: none;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  text-decoration: none;
}

.btn:active { transform: scale(0.98); }

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

.btn-primary:hover { background: #333; }

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

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

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  padding: 0.5rem;
}

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

.btn-block { width: 100%; }

.btn-sm {
  padding: 0.45rem 0.75rem;
  font-size: 0.85rem;
}

.btn-icon {
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: var(--radius-sm);
}

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  margin-bottom: 1rem;
}

.card-title {
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin: 0 0 1rem;
}

/* Day controls */
.day-controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.segment-control {
  display: flex;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}

.segment-btn {
  flex: 1;
  padding: 0.6rem;
  border: none;
  background: transparent;
  font-family: var(--font);
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}

.segment-btn.active {
  background: var(--text);
  color: #fff;
}

.date-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
}

.date-nav span {
  font-weight: 500;
  font-size: 0.95rem;
}

/* Macro bars */
.macro-summary {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.macro-item {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.macro-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}

.macro-label {
  font-size: 0.85rem;
  font-weight: 600;
}

.macro-values {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.macro-remaining {
  font-size: 0.8rem;
  font-weight: 600;
}

.macro-remaining.positive { color: var(--accent-text); }
.macro-remaining.done { color: var(--text-muted); }

.macro-bar {
  height: 8px;
  background: var(--bg);
  border-radius: 4px;
  overflow: hidden;
}

.macro-bar-fill {
  height: 100%;
  border-radius: 4px;
  transition: width 0.4s cubic-bezier(0.16, 1, 0.3, 1);
  max-width: 100%;
}

.macro-bar-fill.protein { background: var(--protein); }
.macro-bar-fill.carbs { background: var(--carbs); }
.macro-bar-fill.fat { background: var(--fat); }
.macro-bar-fill.kcal { background: var(--kcal); }

.kcal-card {
  background: var(--kcal-bg);
  border-color: var(--border);
}

/* Meal list */
.meal-list {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.meal-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  cursor: pointer;
  transition: box-shadow 0.2s;
}

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

.meal-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
}

.meal-name {
  font-weight: 600;
  font-size: 1rem;
}

.meal-macros {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.meal-items {
  font-size: 0.85rem;
  color: var(--text-muted);
  line-height: 1.4;
}

.empty-state {
  text-align: center;
  padding: 2.5rem 1rem;
  color: var(--text-muted);
}

.empty-state p {
  margin: 0 0 1rem;
}

/* Bottom nav */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: calc(var(--nav-height) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-around;
  align-items: center;
  z-index: 100;
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.2rem;
  padding: 0.5rem 0.75rem;
  background: none;
  border: none;
  font-family: var(--font);
  font-size: 0.65rem;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  transition: color 0.2s;
}

.nav-item.active { color: var(--text); }

.nav-icon {
  width: 22px;
  height: 22px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Search */
.search-box {
  position: relative;
  margin-bottom: 1rem;
}

#meal-form > .field {
  margin-bottom: 1.25rem;
}

.search-field {
  margin-bottom: 1.25rem;
}

.search-field .search-box {
  margin-bottom: 0;
}

#meal-form .search-field {
  margin-top: 0.25rem;
}

.search-results-label {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin: 0.75rem 0 0.35rem;
}

.search-results-label:first-child {
  margin-top: 0;
}

.search-more-btn {
  margin-top: 0.75rem;
}

.search-box input {
  padding-left: 2.5rem;
}

.search-icon {
  position: absolute;
  left: 0.85rem;
  top: 50%;
  transform: translateY(-50%);
  width: 18px;
  height: 18px;
  color: var(--text-muted);
  pointer-events: none;
}

.search-results {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-height: 280px;
  overflow-y: auto;
  margin-bottom: 1rem;
}

.search-result {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: border-color 0.2s;
}

.search-result:hover { border-color: var(--text); }

.search-result-info h4 {
  margin: 0 0 0.15rem;
  font-size: 0.9rem;
  font-weight: 500;
}

.search-result-info span {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.badge {
  font-size: 0.65rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 0.2rem 0.5rem;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent-text);
}

.badge.off { background: var(--carbs-bg); color: var(--carbs); }
.badge.usda { background: var(--fat-bg); color: var(--fat); }
.badge.local { background: var(--kcal-bg); color: var(--kcal); }

/* Meal form rows */
.righe-list {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin: 1rem 0;
}

.riga-item {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 0.5rem;
  align-items: end;
  padding: 0.75rem;
  background: var(--bg);
  border-radius: var(--radius-sm);
}

.riga-item .field { margin: 0; }

.riga-name {
  font-size: 0.9rem;
  font-weight: 500;
  padding: 0.75rem 0 0;
}

.riga-macros {
  display: block;
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
}

.manual-food-details {
  margin: 1rem 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
}

.manual-food-details summary {
  padding: 0.85rem 1rem;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  list-style: none;
}

.manual-food-details summary::-webkit-details-marker { display: none; }

.manual-food-details summary::before {
  content: '+ ';
  color: var(--text-muted);
}

.manual-food-details[open] summary::before {
  content: '− ';
}

.manual-food-form {
  padding: 0 1rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.plan-preview-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.plan-preview-item {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  padding: 0.75rem;
  background: var(--bg);
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
}

.plan-preview-item span {
  color: var(--text-muted);
}

/* Food list */
.food-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.food-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.9rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.food-item-info h4 {
  margin: 0 0 0.15rem;
  font-size: 0.95rem;
  font-weight: 500;
}

.food-item-info span {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.food-actions {
  display: flex;
  gap: 0.25rem;
}

/* Plans grid */
.plans-grid {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.plan-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
}

.plan-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.plan-card-header h3 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

.plan-fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}

.plan-kcal-info {
  grid-column: 1 / -1;
  font-size: 0.8rem;
  color: var(--text-muted);
  padding: 0.5rem 0;
}

/* History */
.history-month-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.25rem;
}

.history-month-nav h3 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

.history-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  margin-bottom: 0.75rem;
  cursor: pointer;
  transition: box-shadow 0.2s;
}

.history-item-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 0.5rem;
}

.history-date {
  font-weight: 600;
}

.history-tags {
  display: flex;
  gap: 0.35rem;
}

.history-macros {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.5rem;
  font-size: 0.75rem;
}

.history-macro {
  text-align: center;
  padding: 0.4rem;
  border-radius: var(--radius-sm);
  background: var(--bg);
}

.history-macro strong {
  display: block;
  font-size: 0.85rem;
}

/* Settings */
.settings-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.settings-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.25rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 0.95rem;
}

.settings-item span {
  color: var(--text-muted);
  font-size: 0.85rem;
}

/* Toast */
.toast {
  position: fixed;
  bottom: calc(var(--nav-height) + var(--safe-bottom) + 1rem);
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: var(--text);
  color: #fff;
  padding: 0.75rem 1.25rem;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  z-index: 200;
  opacity: 0;
  transition: transform 0.3s, opacity 0.3s;
  max-width: calc(100% - 2rem);
  text-align: center;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

.toast.error { background: var(--danger); }

/* FAB */
.fab {
  position: fixed;
  bottom: calc(var(--nav-height) + var(--safe-bottom) + 1rem);
  right: 1.25rem;
  width: 56px;
  height: 56px;
  border-radius: var(--radius);
  background: var(--text);
  color: #fff;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  z-index: 50;
  display: grid;
  place-items: center;
  transition: transform 0.2s;
}

.fab:active { transform: scale(0.95); }

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 150;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  animation: fadeIn 0.2s;
}

.modal {
  background: var(--surface);
  border-radius: var(--radius) var(--radius) 0 0;
  width: 100%;
  max-width: 640px;
  max-height: 85dvh;
  overflow-y: auto;
  padding: 1.5rem 1.25rem calc(1.5rem + var(--safe-bottom));
  animation: slideUp 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

.modal h3 {
  margin: 0 0 1.25rem;
  font-size: 1.1rem;
}

.modal-actions {
  display: flex;
  gap: 0.75rem;
  margin-top: 1.25rem;
}

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }

/* Import */
.import-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 2rem 1rem;
  text-align: center;
  color: var(--text-muted);
  cursor: pointer;
  transition: border-color 0.2s;
  margin-bottom: 1rem;
}

.import-zone:hover { border-color: var(--text); }

.import-zone input { display: none; }

/* Desktop */
@media (min-width: 768px) {
  .page-content { padding: 1.5rem 2rem 3rem; }
  .bottom-nav {
    max-width: 640px;
    left: 50%;
    transform: translateX(-50%);
    border-radius: var(--radius) var(--radius) 0 0;
  }
  .fab { right: calc(50% - 320px + 1.25rem); }
  .toast { bottom: calc(var(--nav-height) + var(--safe-bottom) + 1.5rem); }
}

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