* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

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

.matplan-hidden-select {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

body {
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text-primary);
  min-height: 100dvh;
}

.page-matplan .app-container {
  width: 100%;
  max-width: 32rem;
  margin: 0 auto;
  padding: 1.75rem var(--page-pad-x) max(3rem, calc(1rem + env(safe-area-inset-bottom))) var(--page-pad-x-right);
}

/* ==================== Summary card ==================== */

.matplan-summary {
  padding: 1rem 1rem 1.05rem;
  margin-bottom: 0.75rem;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border);
}

.matplan-donut-row {
  display: flex;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 1rem;
}

.matplan-hero-side {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.5rem;
  min-width: 0;
}

.matplan-tile-grid {
  margin-bottom: 0.85rem;
}

.matplan-tile .ds-metric-card__value {
  color: var(--text-primary);
}

.matplan-tile--p {
  background: color-mix(in srgb, #3b82f6 9%, var(--surface-2));
  border-color: color-mix(in srgb, #3b82f6 22%, var(--border));
}
.matplan-tile--kh {
  background: color-mix(in srgb, #10b981 9%, var(--surface-2));
  border-color: color-mix(in srgb, #10b981 22%, var(--border));
}
.matplan-tile--f {
  background: color-mix(in srgb, #ef4444 8%, var(--surface-2));
  border-color: color-mix(in srgb, #ef4444 22%, var(--border));
}
.matplan-tile--kcal {
  background: var(--surface-2);
}

.matplan-balance-row {
  margin-top: 0.15rem;
  margin-bottom: 0.65rem;
  padding-top: 0.35rem;
  border-top: 1px solid var(--divider);
}

.matplan-balance-row .donut {
  width: 5.75rem;
  height: 5.75rem;
}
.matplan-balance-row .donut-center {
  width: 4rem;
  height: 4rem;
}
.matplan-balance-row .donut-val {
  font-size: 1.2rem;
}

.matplan-balance-label {
  font-size: 0.6875rem;
  line-height: 1.3;
  color: var(--text-secondary);
  margin: 0 0 0.35rem;
  max-width: 10rem;
  text-align: right;
  align-self: flex-end;
}

.matplan-bars-legend {
  font-size: 0.6rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-secondary);
  margin: 0 0 0.45rem 0.05rem;
}

/* Values repeated in tile row; keep macro row for bar + % only (still in DOM for a11y) */
.matplan-summary .macro-bar .mb-val {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.matplan-summary .macro-bar {
  position: relative;
}

.summary-hd {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1.15rem;
}

.summary-hd.matplan-donut-row {
  align-items: flex-start;
  margin-bottom: 1rem;
}

.donut {
  position: relative;
  width: 7.5rem;
  height: 7.5rem;
  border-radius: 50%;
  background: var(--surface-2);
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

.donut-center {
  width: 5.4rem;
  height: 5.4rem;
  border-radius: 50%;
  background: var(--surface);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.05rem;
}

.donut-val {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

.donut-goal {
  font-size: 0.625rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  text-align: center;
  line-height: 1.25;
}

.donut-goal .kcal-of {
  display: block;
  text-transform: none;
  letter-spacing: 0;
  font-size: 0.625rem;
  font-weight: 500;
  margin-top: 0.1rem;
  color: var(--text-secondary);
}

.goals-edit-btn {
  margin-left: auto;
  font-family: var(--font);
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--accent);
  background: var(--accent-soft);
  border: 0;
  border-radius: 99px;
  padding: 0.4rem 0.8rem;
  min-height: 2rem;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.12s ease, color 0.12s ease;
}

.goals-edit-btn:hover,
.goals-edit-btn[aria-expanded="true"] {
  background: var(--accent);
  color: #fff;
}

.matplan-hero-side .goals-edit-btn {
  margin-left: 0;
  min-height: 2.75rem;
}

/* ---- Macro progress bars ---- */

.macro-bars {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.macro-bar {
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  grid-template-rows: auto auto;
  column-gap: 0.45rem;
  row-gap: 0.3rem;
  align-items: center;
}

.mb-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  grid-column: 1;
  grid-row: 1;
}

.macro-bar[data-macro="p"] .mb-dot { background: #3b82f6; }
.macro-bar[data-macro="kh"] .mb-dot { background: #10b981; }
.macro-bar[data-macro="f"] .mb-dot { background: #ef4444; }

.mb-lbl {
  grid-column: 2;
  grid-row: 1;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--text-primary);
}

.mb-val {
  grid-column: 3;
  grid-row: 1;
  font-size: 0.8125rem;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.mb-val strong {
  color: var(--text-primary);
  font-weight: 600;
}

.mb-val .mb-goal:not(:empty)::before {
  content: " / ";
  color: var(--text-secondary);
  font-weight: 400;
}

.mb-val .mb-unit {
  font-style: normal;
  color: var(--text-secondary);
  margin-left: 0.15rem;
}

.mb-pct {
  grid-column: 4;
  grid-row: 1;
  min-width: 2.6rem;
  text-align: right;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.mb-pct:empty { display: none; }

.mb-track {
  grid-column: 1 / -1;
  grid-row: 2;
  height: 6px;
  background: var(--surface-2);
  border-radius: 99px;
  overflow: hidden;
  position: relative;
}

.mb-fill {
  display: block;
  height: 100%;
  width: 0%;
  border-radius: 99px;
  transition: width 280ms ease;
}

.macro-bar[data-macro="p"] .mb-fill { background: #3b82f6; }
.macro-bar[data-macro="kh"] .mb-fill { background: #10b981; }
.macro-bar[data-macro="f"] .mb-fill { background: #ef4444; }

.macro-bar.is-over .mb-fill { background: var(--run-warm); }
.macro-bar.is-over .mb-pct { color: var(--run-warm); }

/* ---- Color sums (optional) ---- */

.color-sums {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.9rem;
  padding-top: 0.8rem;
  border-top: 1px solid var(--divider);
}

.color-sum {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  font-size: 0.6875rem;
  font-weight: 500;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}

.cs-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
}

/* ==================== Goals panel (collapsible) ==================== */

.goals-panel {
  margin-bottom: 0.75rem;
  padding: 1rem 1rem 1.05rem;
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-xs);
  animation: slideDown 180ms ease;
}

.goals-panel .ds-section-card__label {
  margin-bottom: 0.5rem;
}

.matplan-clear {
  margin-top: 1rem;
  display: flex;
  justify-content: flex-end;
}

.add-bar-card {
  margin-top: 0.5rem;
  margin-bottom: 0.25rem;
  padding: 1rem 1rem 1.05rem;
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}

.add-bar-card .ds-section-card__label {
  margin-bottom: 0.55rem;
}

.goals-panel[hidden] { display: none; }

@keyframes slideDown {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

.preset-label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.6875rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-secondary);
  margin-bottom: 0.6rem;
}

.preset-label select {
  width: 100%;
  padding: 0.55rem 0.6rem;
  min-height: 2.5rem;
  font-family: var(--font);
  font-size: 0.875rem;
  font-weight: 500;
  letter-spacing: 0;
  text-transform: none;
  border: 0;
  border-radius: var(--radius-xs);
  background: var(--surface-2);
  color: var(--text-primary);
}

.preset-label select:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 1px;
}

.targets-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.4rem;
}

.targets-row label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.625rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-secondary);
}

.targets-row input {
  width: 100%;
  padding: 0.5rem 0.55rem;
  min-height: 2.4rem;
  font-family: var(--font);
  font-size: 0.9375rem;
  font-weight: 600;
  border: 0;
  border-radius: var(--radius-xs);
  background: var(--surface-2);
  color: var(--text-primary);
  text-align: center;
  font-variant-numeric: tabular-nums;
  text-transform: none;
  letter-spacing: 0;
}

.targets-row input:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 1px;
}

/* ==================== Items list header ==================== */

.items-hd {
  display: flex;
  align-items: baseline;
  gap: 0.55rem;
  padding: 0.25rem 0.15rem 0.6rem;
}

.items-hd-title {
  font-size: 0.6875rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-secondary);
  margin: 0;
}

.items-hd-count {
  font-size: 0.625rem;
  font-weight: 600;
  color: var(--text-secondary);
  background: var(--surface);
  padding: 0.1rem 0.45rem;
  border-radius: 99px;
  min-width: 1.25rem;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.items-empty {
  padding: 1.25rem 1rem;
  text-align: center;
  font-size: 0.8125rem;
  color: var(--text-secondary);
  background: var(--surface);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-xs);
}

/* ==================== Food cards ==================== */

#items-root {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.food-card {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  column-gap: 0.6rem;
  row-gap: 0.5rem;
  padding: 0.85rem 0.95rem;
  background: var(--surface);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-xs);
  border: 1px solid var(--border);
}

.card-dot {
  grid-column: 1;
  grid-row: 1 / span 2;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 0;
  padding: 0;
  cursor: pointer;
  align-self: center;
  -webkit-tap-highlight-color: transparent;
  transition: transform 0.1s ease;
}

.card-dot:active { transform: scale(1.35); }

.card-top {
  grid-column: 2;
  grid-row: 1;
  display: flex;
  align-items: baseline;
  gap: 0.45rem;
  min-width: 0;
}

.card-name {
  flex: 1;
  font-size: 0.9375rem;
  font-weight: 600;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
  cursor: pointer;
  letter-spacing: -0.005em;
  -webkit-tap-highlight-color: transparent;
}

.card-food-select {
  flex: 1;
  min-width: 0;
  font-family: var(--font);
  font-size: 0.9375rem;
  font-weight: 600;
  color: var(--text-primary);
  border: 0;
  border-radius: var(--radius-xs);
  background: var(--accent-soft);
  padding: 0.25rem 0.4rem;
  box-shadow: inset 0 0 0 2px var(--accent-dim);
}

.card-kcal {
  font-size: 1rem;
  font-weight: 700;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  letter-spacing: -0.02em;
}

.card-kcal small {
  font-weight: 500;
  font-size: 0.625rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-secondary);
  margin-left: 0.15rem;
}

.card-remove {
  grid-column: 3;
  grid-row: 1 / span 2;
  width: 1.85rem;
  height: 1.85rem;
  padding: 0;
  font-size: 1.125rem;
  line-height: 1;
  color: var(--text-secondary);
  opacity: 0.35;
  background: transparent;
  border: 0;
  border-radius: 50%;
  cursor: pointer;
  align-self: center;
  -webkit-tap-highlight-color: transparent;
  transition: color 0.12s ease, background 0.12s ease, opacity 0.12s ease;
}

.card-remove:hover {
  opacity: 1;
  color: var(--danger);
  background: var(--run-warm-bg);
}

.card-remove:active {
  color: var(--danger);
  opacity: 1;
}

.card-bottom {
  grid-column: 2;
  grid-row: 2;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.45rem 0.7rem;
}

.card-dose-fields {
  display: inline-flex;
  align-items: stretch;
  border-radius: var(--radius-xs);
  background: var(--surface-2);
  overflow: hidden;
  min-height: 2.2rem;
  flex: 0 0 auto;
}

.card-amt {
  width: 4rem;
  min-width: 0;
  padding: 0.35rem 0.5rem;
  font-family: var(--font);
  font-size: 0.9375rem;
  font-weight: 600;
  text-align: right;
  border: 0;
  background: transparent;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
  touch-action: manipulation;
}

.card-amt:focus {
  outline: none;
  background: var(--surface);
  box-shadow: inset 0 0 0 2px var(--accent);
}

.card-unit {
  flex: 0 0 auto;
  min-width: 2.75rem;
  padding: 0.35rem 0.45rem;
  font-family: var(--font);
  font-size: 0.8125rem;
  font-weight: 500;
  border: 0;
  background: rgba(0, 0, 0, 0.02);
  color: var(--text-primary);
  appearance: none;
  -webkit-appearance: none;
  cursor: pointer;
  touch-action: manipulation;
}

.card-unit:focus {
  outline: none;
  box-shadow: inset 0 0 0 2px var(--accent);
  z-index: 1;
}

.card-macros {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  margin-left: auto;
  white-space: nowrap;
}

/* ==================== Add bar ==================== */

.add-bar {
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  grid-template-rows: auto auto;
  column-gap: 0.5rem;
  row-gap: 0.5rem;
  align-items: center;
  margin-top: 0.85rem;
  padding: 0.85rem 0.95rem;
  background: var(--surface);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-xs);
}

.add-dot {
  grid-column: 1;
  grid-row: 1;
  width: 1.15rem;
  height: 1.15rem;
  border-radius: 50%;
  border: 2px solid rgba(0, 0, 0, 0.08);
  padding: 0;
  cursor: pointer;
  align-self: center;
  -webkit-tap-highlight-color: transparent;
  transition: transform 0.1s ease;
}

.add-dot:active { transform: scale(1.25); }

.add-food-select {
  grid-column: 2 / 5;
  grid-row: 1;
  font-family: var(--font);
  font-size: 0.9375rem;
  font-weight: 500;
  color: var(--text-primary);
  border: 0;
  border-radius: var(--radius-xs);
  background: var(--surface-2);
  padding: 0.55rem 0.65rem;
  min-height: 2.6rem;
  width: 100%;
  touch-action: manipulation;
}

.add-food-select:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 1px;
}

#add-amt {
  grid-column: 1 / 3;
  grid-row: 2;
  padding: 0.55rem 0.7rem;
  font-family: var(--font);
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-primary);
  border: 0;
  border-radius: var(--radius-xs);
  background: var(--surface-2);
  min-height: 2.6rem;
  text-align: left;
  font-variant-numeric: tabular-nums;
  touch-action: manipulation;
}

#add-amt:focus {
  outline: none;
  background: var(--surface);
  box-shadow: inset 0 0 0 2px var(--accent);
}

#add-unit {
  grid-column: 3;
  grid-row: 2;
  font-family: var(--font);
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text-primary);
  border: 0;
  border-radius: var(--radius-xs);
  background: var(--surface-2);
  padding: 0.5rem 0.6rem;
  min-height: 2.6rem;
  min-width: 3.5rem;
  appearance: none;
  -webkit-appearance: none;
  cursor: pointer;
  touch-action: manipulation;
}

.add-btn,
#add-btn {
  grid-column: 4;
  grid-row: 2;
  align-self: center;
  justify-self: end;
  flex-shrink: 0;
  min-width: 2.6rem;
  min-height: 2.6rem;
  width: 2.6rem;
  height: 2.6rem;
  padding: 0;
  font-family: var(--font);
  font-size: 1.35rem;
  font-weight: 600;
  line-height: 1;
  color: #fff;
  background: var(--accent);
  border: 0;
  border-radius: 50%;
  cursor: pointer;
  display: grid;
  place-items: center;
  box-shadow: 0 4px 12px rgba(47, 107, 255, 0.32);
  -webkit-tap-highlight-color: transparent;
  transition: transform 0.1s ease, background 0.12s ease;
}

.add-btn:active,
#add-btn:active {
  transform: scale(0.94);
  background: var(--accent-strong);
}

/* ==================== Bottom ==================== */

.btn-clear {
  display: block;
  margin: 0.9rem 0 0 auto;
  padding: 0.5rem 0.9rem;
  min-height: 2rem;
  font-family: var(--font);
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-secondary);
  background: var(--surface);
  border: 0;
  border-radius: 99px;
  cursor: pointer;
  box-shadow: var(--shadow-xs);
  -webkit-tap-highlight-color: transparent;
  transition: color 0.12s ease, background 0.12s ease;
}

.btn-clear:hover,
.btn-clear:active {
  color: var(--danger);
  background: var(--run-warm-bg);
}

/* ==================== Toast ==================== */

.toast-container {
  position: fixed;
  bottom: max(1.25rem, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  align-items: center;
  pointer-events: none;
  max-width: min(90vw, 24rem);
}

.toast {
  pointer-events: auto;
  padding: 0.55rem 0.9rem;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--toast-text);
  background: var(--toast-bg);
  border-radius: 99px;
  box-shadow: var(--shadow-md);
}

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

/* ==================== Desktop ==================== */

@media (min-width: 640px) {
  .page-matplan .app-container {
    max-width: 40rem;
    margin: 0 auto;
    padding-top: 2.5rem;
  }

  .donut {
    width: 8.25rem;
    height: 8.25rem;
  }

  .donut-center {
    width: 6rem;
    height: 6rem;
  }

  .donut-val { font-size: 1.75rem; }

  .add-bar {
    grid-template-columns: auto 1.25fr auto auto auto;
    grid-template-rows: auto;
  }

  .add-dot {
    grid-column: 1;
    grid-row: 1;
  }

  .add-food-select {
    grid-column: 2;
    grid-row: 1;
    min-height: 2.6rem;
  }

  #add-amt {
    grid-column: 3;
    grid-row: 1;
    width: 5.5rem;
  }

  #add-unit {
    grid-column: 4;
    grid-row: 1;
  }

  .add-btn,
  #add-btn {
    grid-column: 5;
    grid-row: 1;
  }

  .card-remove {
    opacity: 0;
    transition: opacity 0.12s ease, color 0.12s ease, background 0.12s ease;
  }

  .food-card:hover .card-remove {
    opacity: 0.35;
  }
}
