:root {
  color-scheme: light;
  --bg: #f7f8fa;
  --surface: #ffffff;
  --surface-soft: #edf1f5;
  --text: #151a1f;
  --muted: #66717d;
  --line: #d8e0e8;
  --primary: #0f766e;
  --accent: #2563eb;
  --accent-soft: #e7f3ff;
  --orange: #b85c00;
  --orange-soft: #fff0da;
  --red: #b42318;
  --red-soft: #ffe8e5;
  --green-soft: #dff5ee;
  --shadow: 0 10px 30px rgba(21, 30, 38, 0.12);
  --radius: 8px;
  --bottom-space: 132px;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  scroll-behavior: smooth;
  overflow-x: hidden;
}

body {
  min-height: 100%;
  margin: 0;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 16px;
  letter-spacing: 0;
}

button,
input {
  font: inherit;
}

button {
  -webkit-tap-highlight-color: transparent;
}

.app-shell {
  width: 100%;
  max-width: 760px;
  min-width: 0;
  margin: 0 auto;
  padding: 14px 14px calc(var(--bottom-space) + env(safe-area-inset-bottom));
}

.top-shell {
  position: sticky;
  top: 0;
  z-index: 5;
  margin: -14px -14px 18px;
  padding: calc(12px + env(safe-area-inset-top)) 14px 12px;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid color-mix(in srgb, var(--line) 76%, transparent);
}

.title-row,
.meal-switcher,
.total-row,
.log-header,
.dialog-header,
.section-title-row,
.food-card-head,
.total-head {
  display: flex;
  align-items: center;
}

.title-row,
.dialog-header,
.section-title-row {
  justify-content: space-between;
  gap: 12px;
}

.eyebrow {
  margin: 0 0 2px;
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

h1,
h2,
h3 {
  margin: 0;
  letter-spacing: 0;
  line-height: 1.12;
}

h1 {
  font-size: 1.9rem;
}

h2 {
  font-size: 1.5rem;
}

h3 {
  font-size: 1rem;
}

.icon-button,
.text-button,
.primary-button,
.danger-button,
.file-button,
.card-remove,
.small-action {
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: transform 140ms ease, border-color 140ms ease, background-color 140ms ease, color 140ms ease;
}

.icon-button:active,
.text-button:active,
.primary-button:active,
.danger-button:active,
.file-button:active,
.food-card:active,
.small-action:active {
  transform: scale(0.98);
}

.icon-button {
  display: inline-grid;
  width: 46px;
  min-width: 46px;
  place-items: center;
  color: var(--primary);
  font-size: 1.35rem;
  font-weight: 800;
}

.text-button,
.primary-button,
.danger-button,
.file-button,
.small-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 0 14px;
  font-weight: 800;
  text-decoration: none;
}

.primary-button {
  border-color: var(--primary);
  background: var(--primary);
  color: #ffffff;
}

.danger-button {
  border-color: color-mix(in srgb, var(--red) 35%, var(--line));
  color: var(--red);
}

.meal-switcher {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
  margin-top: 14px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-soft);
  width: 100%;
  min-width: 0;
}

.meal-switcher > * {
  min-width: 0;
}

.meal-option {
  position: relative;
  display: flex;
  min-height: 52px;
  min-width: 0;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: calc(var(--radius) - 2px);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-weight: 900;
  line-height: 1.1;
  overflow: hidden;
  padding: 8px 6px 6px;
  text-align: center;
  transition: transform 140ms ease, background-color 140ms ease, color 140ms ease, box-shadow 140ms ease;
}

.meal-option:active {
  transform: scale(0.98);
}

.meal-option.is-active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 2px 10px rgba(21, 30, 38, 0.1);
}

.meal-option.is-active::before {
  content: "";
  position: absolute;
  top: 6px;
  right: 10px;
  left: 10px;
  height: 3px;
  border-radius: 999px;
  background: var(--meal-color, var(--primary));
}

.meal-option[data-meal="breakfast"] {
  --meal-color: #d97706;
}

.meal-option[data-meal="snack"] {
  --meal-color: #db2777;
}

.meal-option[data-meal="lunch"] {
  --meal-color: #0f766e;
}

.meal-option[data-meal="dinner"] {
  --meal-color: #2563eb;
}

.log-container {
  display: grid;
  gap: 26px;
}

.log-section {
  min-width: 0;
}

.log-header {
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 10px;
}

.log-header > div {
  min-width: 0;
}

.log-header h2 {
  min-width: 0;
  font-size: 1.26rem;
}

.log-totals {
  flex: 0 0 auto;
  max-width: 58%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 800;
  text-align: right;
}

.food-grid {
  display: grid;
  gap: 9px;
  min-width: 0;
}

.empty-note {
  margin: 0;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--muted);
  font-weight: 800;
}

.food-card {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  min-height: 74px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: pointer;
  min-width: 0;
  width: 100%;
  outline: none;
  transition: transform 140ms ease, border-color 140ms ease, background-color 140ms ease, box-shadow 140ms ease;
}

.food-card:focus-visible {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 24%, transparent);
}

.food-card.is-selected {
  border-color: color-mix(in srgb, var(--accent) 70%, var(--line));
  background: var(--accent-soft);
}

.food-main {
  min-width: 0;
}

.food-name {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  overflow-wrap: anywhere;
  font-size: 1rem;
  font-weight: 900;
}

.today-badge {
  display: inline-flex;
  min-height: 24px;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  background: var(--surface-soft);
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 900;
  padding: 0 8px;
  white-space: nowrap;
}

.food-portion {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 700;
}

.food-macros {
  display: grid;
  grid-template-columns: 1fr 1fr;
  min-width: 132px;
  gap: 8px;
  color: var(--muted);
  font-weight: 800;
  text-align: right;
}

.macro-pill,
.selection-macro {
  display: grid;
  gap: 2px;
}

.macro-pill strong,
.selection-macro strong {
  color: var(--primary);
  font-size: 1rem;
  line-height: 1;
}

.macro-pill small,
.selection-macro small {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 900;
  line-height: 1;
}

.selected-summary {
  display: grid;
  grid-column: 1 / -1;
  grid-template-columns: auto minmax(0, 1fr) minmax(0, 1fr) 44px;
  align-items: center;
  gap: 8px;
  min-height: 52px;
  padding: 8px;
  border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--line));
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--surface) 76%, var(--accent-soft));
}

.selection-count {
  display: inline-flex;
  min-width: 42px;
  min-height: 34px;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  background: var(--primary);
  color: #ffffff;
  font-size: 0.92rem;
  font-weight: 900;
}

.card-remove {
  display: inline-flex;
  width: 44px;
  min-width: 44px;
  min-height: 44px;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
  border-color: color-mix(in srgb, var(--red) 30%, var(--line));
  background: var(--surface);
  color: var(--red);
  font-size: 1.35rem;
  font-weight: 900;
}

.bottom-totals {
  position: fixed;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 10;
  padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--surface) 96%, transparent);
  border-top: 1px solid var(--line);
  box-shadow: 0 -10px 28px rgba(21, 42, 28, 0.1);
  backdrop-filter: blur(14px);
}

.total-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
  gap: 10px;
}

.total-metric {
  min-width: 0;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.total-head {
  justify-content: space-between;
  gap: 8px;
  font-size: 0.86rem;
  font-weight: 900;
}

.total-head strong {
  font-size: 1.18rem;
}

.progress-track {
  height: 5px;
  margin-top: 8px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--surface-soft);
}

.progress-fill {
  display: block;
  width: 0%;
  height: 100%;
  border-radius: inherit;
  background: var(--muted);
  transition: width 180ms ease, background-color 180ms ease;
}

.target-label {
  display: block;
  margin-top: 5px;
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 800;
}

.total-metric.status-green .progress-fill {
  background: var(--accent);
}

.total-metric.status-green .total-head strong {
  color: var(--accent);
}

.total-metric.status-orange .progress-fill {
  background: var(--orange);
}

.total-metric.status-orange .total-head strong {
  color: var(--orange);
}

.total-metric.status-red .progress-fill {
  background: var(--red);
}

.total-metric.status-red .total-head strong {
  color: var(--red);
}

.undo-button {
  position: fixed;
  right: 16px;
  bottom: calc(122px + env(safe-area-inset-bottom));
  z-index: 12;
  min-height: 44px;
  padding: 0 18px;
  border: 0;
  border-radius: 999px;
  background: var(--text);
  color: var(--surface);
  font-weight: 900;
  box-shadow: var(--shadow);
  cursor: pointer;
}

.settings-dialog {
  width: calc(100% - 20px);
  max-width: 700px;
  max-height: calc(100dvh - 20px);
  padding: 0;
  border: 0;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.settings-dialog::backdrop {
  background: rgba(14, 23, 18, 0.46);
}

.dialog-inner {
  max-height: calc(100dvh - 20px);
  overflow: auto;
  padding: 16px;
}

.dialog-header {
  position: sticky;
  top: -16px;
  z-index: 1;
  margin: -16px -16px 14px;
  padding: 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.settings-section {
  padding: 18px 0;
  border-bottom: 1px solid var(--line);
}

.settings-section:last-child {
  border-bottom: 0;
}

.field-grid,
.button-grid {
  display: grid;
  gap: 10px;
  margin-top: 12px;
}

.field-grid label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
}

.field-grid input {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px;
  background: var(--surface);
  color: var(--text);
}

.field-grid input:focus {
  border-color: var(--primary);
  outline: 3px solid color-mix(in srgb, var(--accent) 20%, transparent);
}

.target-form .primary-button {
  width: 100%;
  margin-top: 12px;
}

.button-grid {
  grid-template-columns: 1fr;
}

.small-action {
  min-width: 44px;
  padding: 0 10px;
  color: var(--primary);
}

.small-action.danger {
  color: var(--red);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

@media (min-width: 560px) {
  .field-grid,
  .button-grid {
    grid-template-columns: 1fr 1fr;
  }
}

@media (max-width: 559px) {
  :root {
    --bottom-space: 108px;
  }

  .app-shell {
    padding: 10px 10px calc(var(--bottom-space) + env(safe-area-inset-bottom));
  }

  .top-shell {
    margin: -10px -10px 10px;
    padding: calc(10px + env(safe-area-inset-top)) 10px 10px;
  }

  .top-shell,
  .bottom-totals {
    width: 100vw;
    max-width: 100vw;
  }

  .title-row,
  .meal-switcher,
  .log-container,
  .total-row {
    width: calc(100vw - 20px);
    max-width: calc(100vw - 20px);
  }

  h1 {
    font-size: 1.35rem;
  }

  .title-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 46px;
  }

  .title-row > div {
    grid-column: 1;
  }

  #settingsButton {
    grid-column: 2;
    grid-row: 1;
  }

  .meal-switcher {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 4px;
    padding: 3px;
    margin-top: 8px;
  }

  .meal-option {
    min-height: 40px;
    padding: 6px 4px;
    font-size: 0.85rem;
  }

  .meal-option.is-active::before {
    top: 4px;
    right: 6px;
    left: 6px;
    height: 2px;
  }

  .log-container {
    gap: 14px;
  }

  .log-header {
    display: grid;
    grid-template-columns: 1fr;
    gap: 4px;
    margin-bottom: 6px;
  }

  .log-header h2 {
    font-size: 1.1rem;
  }

  .log-totals {
    max-width: 100%;
    text-align: left;
  }

  .food-grid {
    gap: 7px;
  }

  .food-card {
    grid-template-columns: 1fr;
    gap: 8px;
    padding: 10px;
    min-height: 0;
  }

  .food-card.is-selected {
    padding: 10px;
  }

  .food-macros {
    grid-template-columns: 1fr 1fr;
    max-width: none;
    min-width: 0;
    text-align: left;
  }

  .selected-summary {
    grid-template-columns: auto 1fr 1fr 44px;
  }

  .bottom-totals {
    padding: 7px 10px calc(7px + env(safe-area-inset-bottom));
  }

  .total-row {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 8px;
  }

  .total-metric {
    padding: 7px 8px;
  }

  .total-head {
    font-size: 0.78rem;
  }

  .total-head strong {
    font-size: 1.05rem;
  }

  .progress-track {
    height: 4px;
    margin-top: 5px;
  }

  .target-label {
    margin-top: 3px;
    font-size: 0.7rem;
  }

  .undo-button {
    bottom: calc(98px + env(safe-area-inset-bottom));
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #101418;
    --surface: #171c22;
    --surface-soft: #222932;
    --text: #eef3f7;
    --muted: #a8b2bd;
    --line: #303946;
    --primary: #2dd4bf;
    --accent: #60a5fa;
    --accent-soft: #16324d;
    --orange: #f59e0b;
    --orange-soft: #3b2712;
    --red: #fb7185;
    --red-soft: #3d1d26;
    --green-soft: #123d36;
    --shadow: 0 14px 36px rgba(0, 0, 0, 0.35);
  }

  .top-shell,
  .bottom-totals {
    background: color-mix(in srgb, var(--bg) 88%, transparent);
  }
}
