/* ---------- Tokens ---------- */
:root {
  color-scheme: light;
  --paper: #f9f8f6;
  --paper-raised: #ffffff;
  --ink: #15181c;
  --ink-soft: #5b6168;
  --ink-faint: #8b9198;
  --line: #e2e0da;
  --line-strong: #cfccc4;

  --green: #1f5d4c;
  --green-soft: #dceae5;
  --protein: #a8503d;
  --carbs: #a6821f;
  --fat: #4f6b74;
  --danger: #be3b2e;
  --danger-soft: #f6e4e1;

  --radius: 5px;
  --gap: 20px;

  --font-ui: "Archivo", system-ui, sans-serif;
  --font-food: "Source Serif 4", Georgia, serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

.num {
  font-variant-numeric: tabular-nums lining-nums;
}

button,
input,
select {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  background: none;
  border: none;
}

a {
  color: inherit;
}

.icon {
  display: block;
  flex-shrink: 0;
}

/* ---------- Layout shell ---------- */
#app {
  max-width: 600px;
  margin: 0 auto;
  min-height: 100vh;
  padding: 0 20px 48px;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.hairline {
  border: none;
  border-top: 1px solid var(--line);
  margin: var(--gap) 0;
}

/* ---------- Login screen ---------- */
#loginScreen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 360px;
}

.login-mark {
  width: 56px;
  height: 56px;
  margin-bottom: 24px;
}

.login-card h1 {
  font-size: 24px;
  font-weight: 700;
  margin: 0 0 6px;
}

.login-card p.hint {
  color: var(--ink-soft);
  margin: 0 0 28px;
  font-size: 15px;
}

/* ---------- Header ---------- */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 24px 0 8px;
}

.date-nav {
  display: flex;
  align-items: center;
  gap: 4px;
}

.date-nav button.nav-arrow {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
}

.date-nav button.nav-arrow:active {
  background: var(--line);
}

.date-label {
  font-weight: 700;
  font-size: 18px;
  padding: 0 4px;
  position: relative;
}

.date-label input[type="date"] {
  position: absolute;
  inset: 0;
  opacity: 0;
  width: 100%;
  height: 100%;
  cursor: pointer;
}

.today-jump {
  font-size: 13px;
  color: var(--green);
  font-weight: 600;
  margin-left: 6px;
  padding: 4px 8px;
}

.header-actions {
  display: flex;
  gap: 4px;
}

.header-actions button {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
  color: var(--ink-soft);
}

.header-actions button:active {
  background: var(--line);
}

/* ---------- Stat summary ---------- */
.stat-summary {
  padding: 8px 0 var(--gap);
}

.cal-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 10px;
}

.cal-value {
  font-size: 40px;
  font-weight: 800;
  letter-spacing: -0.01em;
}

.cal-target {
  color: var(--ink-soft);
  font-size: 15px;
}

.cal-over {
  color: var(--danger);
}

.bar-track {
  height: 8px;
  border-radius: 4px;
  background: var(--green-soft);
  overflow: hidden;
  margin-bottom: 22px;
}

.bar-fill {
  height: 100%;
  background: var(--green);
  border-radius: 4px;
  transition: width 0.35s ease;
}

.bar-fill.over {
  background: var(--danger);
}

.macro-rows {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.macro-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.macro-swatch {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 2px;
  flex-shrink: 0;
}

.macro-swatch.protein {
  background: var(--protein);
}

.macro-swatch.carbs {
  background: var(--carbs);
}

.macro-swatch.fat {
  background: var(--fat);
}

.macro-name {
  display: inline-block;
  width: 60px;
  font-size: 14px;
  color: var(--ink-soft);
  flex-shrink: 0;
}

.macro-track {
  display: inline-block;
  flex: 1;
  height: 5px;
  border-radius: 3px;
  background: var(--line);
  overflow: hidden;
}

.macro-fill {
  display: inline-block;
  height: 100%;
  border-radius: 3px;
  transition: width 0.35s ease;
}

.macro-fill.protein {
  background: var(--protein);
}

.macro-fill.carbs {
  background: var(--carbs);
}

.macro-fill.fat {
  background: var(--fat);
}

.macro-value {
  display: inline-block;
  width: 76px;
  text-align: right;
  font-size: 14px;
  flex-shrink: 0;
}

/* ---------- Section headings ---------- */
.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}

.section-heading h2 {
  font-size: 15px;
  font-weight: 600;
  margin: 0;
}

.text-link {
  font-size: 14px;
  font-weight: 600;
  color: var(--green);
  display: flex;
  align-items: center;
  gap: 4px;
}

/* ---------- Meal list ---------- */
.meal-list {
  margin-bottom: 4px;
}

.meal-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 0;
  border-top: 1px solid var(--line);
}

.meal-thumb {
  width: 44px;
  height: 44px;
  border-radius: var(--radius);
  object-fit: cover;
  background: var(--line);
  flex-shrink: 0;
  border: 1px solid var(--line);
}

.meal-thumb-placeholder {
  width: 44px;
  height: 44px;
  border-radius: var(--radius);
  background: var(--paper-raised);
  border: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 17px;
  line-height: 1;
  white-space: nowrap;
  flex-shrink: 0;
}

.meal-info {
  flex: 1;
  min-width: 0;
}

.meal-name {
  font-family: var(--font-food);
  font-size: 17px;
  line-height: 1.3;
  margin-bottom: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.meal-meta {
  font-size: 13px;
  color: var(--ink-soft);
}

.meal-meta .sep {
  margin: 0 6px;
  color: var(--line-strong);
}

.meal-actions {
  display: flex;
  gap: 2px;
  flex-shrink: 0;
}

.meal-actions button {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--ink-faint);
  border-radius: var(--radius);
}

.meal-actions button:active {
  background: var(--line);
}

.empty-note {
  padding: 18px 0;
  color: var(--ink-soft);
  font-size: 15px;
  border-top: 1px solid var(--line);
}

/* ---------- Quick actions ---------- */
.quick-actions {
  margin-top: var(--gap);
}

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

.action-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px;
  background: var(--paper-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  text-align: left;
  font-size: 14px;
  font-weight: 600;
}

.action-btn:active {
  background: var(--line);
}

.action-btn[disabled] {
  opacity: 0.4;
  pointer-events: none;
}

.action-btn .icon {
  color: var(--green);
}

/* ---------- 7-day overview ---------- */
.overview {
  margin-top: 32px;
}

.chart-wrap {
  padding-top: 8px;
}

.bars {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  height: 120px;
  margin-bottom: 8px;
}

.bar-col {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  height: 100%;
  position: relative;
}

.bar-col .col-fill {
  width: 100%;
  background: var(--green);
  border-radius: 3px 3px 0 0;
}

.bar-col .col-fill.over {
  background: var(--danger);
}

.target-line {
  position: absolute;
  left: -5px;
  right: -5px;
  border-top: 1px dashed var(--ink-faint);
}

.day-labels {
  display: flex;
  gap: 10px;
}

.day-labels span {
  flex: 1;
  text-align: center;
  font-size: 12px;
  color: var(--ink-soft);
}

.avg-row {
  display: flex;
  gap: 18px;
  margin-top: 18px;
}

.avg-item {
  flex: 1;
}

.avg-item .label {
  font-size: 13px;
  color: var(--ink-soft);
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 2px;
}

.avg-item .value {
  font-size: 20px;
  font-weight: 700;
}

/* ---------- Settings ---------- */
.settings-section {
  padding: 20px 0;
  border-top: 1px solid var(--line);
}

.settings-section h3 {
  font-size: 15px;
  font-weight: 600;
  margin: 0 0 4px;
}

.settings-section p.hint {
  color: var(--ink-soft);
  font-size: 14px;
  margin: 0 0 14px;
}

.preset-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 14px 0;
  border-top: 1px solid var(--line);
}

.preset-row .preset-delete {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--ink-faint);
  border-radius: var(--radius);
  flex-shrink: 0;
}

.preset-row .preset-delete:active {
  background: var(--line);
}

.preset-row input {
  margin-bottom: 0;
}

.preset-row:first-of-type {
  border-top: none;
}

.preset-row .meal-info {
  flex: 1;
}

/* ---------- Forms ---------- */
.field {
  margin-bottom: 16px;
}

.field label {
  display: block;
  font-size: 13px;
  color: var(--ink-soft);
  margin-bottom: 6px;
}

.field input,
.field textarea,
.row2 input,
.row3 input {
  width: 100%;
  min-width: 0;
  padding: 11px 12px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  background: var(--paper-raised);
}

.field input:focus,
.field textarea:focus,
.row2 input:focus,
.row3 input:focus {
  outline: 2px solid var(--green);
  outline-offset: -1px;
}

.row2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  min-width: 0;
}

.row3 {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 12px;
  min-width: 0;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 13px 20px;
  border-radius: var(--radius);
  font-weight: 600;
  font-size: 15px;
}

.btn.primary {
  background: var(--green);
  color: #fff;
  width: 100%;
}

.btn.primary:active {
  background: #17483b;
}

.btn.secondary {
  background: transparent;
  border: 1px solid var(--line-strong);
  width: 100%;
}

.btn.danger-text {
  color: var(--danger);
  width: 100%;
}

.stepper {
  display: flex;
  align-items: center;
  gap: 12px;
}

.stepper button {
  width: 36px;
  height: 36px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
}

.stepper .stepper-value {
  min-width: 48px;
  text-align: center;
  font-weight: 700;
}

/* ---------- Modals ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: none;
}

.modal.open {
  display: block;
}

.modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(21, 24, 28, 0.45);
}

.modal-panel {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  max-width: 600px;
  margin: 0 auto;
  background: var(--paper);
  border-radius: 16px 16px 0 0;
  padding: 24px 20px 28px;
  max-height: 90vh;
  overflow-y: auto;
}

@media (min-width: 640px) {
  .modal-panel {
    top: 50%;
    bottom: auto;
    transform: translateY(-50%);
    border-radius: 12px;
  }
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
}

.modal-head h2 {
  font-size: 19px;
  font-weight: 700;
  margin: 0;
}

.modal-close {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--ink-soft);
}

.capture-area {
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius);
  padding: 32px 20px;
  text-align: center;
  margin-bottom: 16px;
}

.capture-area img {
  max-width: 100%;
  max-height: 240px;
  border-radius: var(--radius);
  margin-bottom: 12px;
}

.capture-area .icon {
  margin: 0 auto 10px;
  color: var(--ink-faint);
}

.capture-hint {
  color: var(--ink-soft);
  font-size: 14px;
  margin: 0 0 16px;
}

.spinner-row {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--ink-soft);
  font-size: 14px;
  padding: 8px 0 16px;
}

.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid var(--line-strong);
  border-top-color: var(--green);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

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

.week-day-slot {
  border-top: 1px solid var(--line);
  padding: 14px 0;
}

.week-day-slot:first-of-type {
  border-top: none;
}

.week-day-slot .day-name {
  font-size: 13px;
  color: var(--ink-soft);
  margin-bottom: 6px;
}

.week-day-slot.empty {
  color: var(--ink-faint);
  font-size: 14px;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%) translateY(20px);
  background: var(--ink);
  color: var(--paper);
  padding: 12px 20px;
  border-radius: var(--radius);
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 200;
  max-width: calc(100% - 40px);
  text-align: center;
}

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

/* ---------- Reduced motion ---------- */
@media (prefers-reduced-motion: reduce) {
  .bar-fill,
  .macro-fill,
  .toast {
    transition: none;
  }
  .spinner {
    animation: none;
  }
}

/* ---------- New meal entrance ---------- */
@keyframes rowIn {
  from {
    opacity: 0;
    transform: translateY(-6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.meal-row.new {
  animation: rowIn 0.3s ease;
}
