:root {
  --bg: #f4efe6;
  --card: #fffcf7;
  --ink: #1c1917;
  --muted: #6b635a;
  --line: #e6ddd0;
  --accent: #3d6b4f;
  --accent-2: #2f5440;
  --danger: #b42318;
  --shadow: 0 10px 30px rgba(60, 40, 20, 0.08);
  --radius: 18px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --nav: 64px;
  --savebar: 72px;
}

* { box-sizing: border-box; }
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  touch-action: pan-x pan-y;
  height: 100%;
  height: 100dvh;
  height: -webkit-fill-available;
}
html, body {
  margin: 0;
  padding: 0;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--ink);
  font-family: ui-rounded, "SF Pro Display", "Segoe UI", system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
body {
  display: flex;
  flex-direction: column;
  width: 100%;
  min-height: 100%;
  min-height: 100dvh;
  min-height: -webkit-fill-available;
  height: 100%;
  height: 100dvh;
  height: -webkit-fill-available;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.app {
  flex: 1;
  min-height: 0;
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#main {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding: calc(12px + env(safe-area-inset-top, 0px)) 14px 0;
}

.view:not(.hidden) {
  flex: 1;
  min-height: 0;
  overflow: hidden;
}
.view.has-save:not(.hidden) {
  display: flex;
  flex-direction: column;
}
.view-body {
  flex: 1;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: none;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 12px;
}
.view:not(.has-save):not(.hidden) {
  overflow-y: auto;
  overscroll-behavior: none;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 12px;
}

.top {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}
.top h1 {
  font-size: 22px;
  margin: 0;
  letter-spacing: -0.03em;
}
.top .sub { color: var(--muted); font-size: 13px; margin-top: 2px; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
  margin-bottom: 12px;
}

.label {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 8px;
}

.bristol {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}
.bristol button {
  appearance: none;
  border: 2px solid transparent;
  border-radius: 16px;
  min-height: 64px;
  font-size: 22px;
  font-weight: 800;
  color: #1c1917;
  cursor: pointer;
}
.bristol button small {
  display: block;
  font-size: 10px;
  font-weight: 600;
  opacity: 0.8;
  margin-top: 2px;
}
.bristol button[aria-pressed="true"] {
  border-color: var(--ink);
  transform: scale(1.03);
  box-shadow: 0 0 0 3px rgba(28, 25, 23, 0.12);
}
.b1 { background: #d9d9d9; }
.b2 { background: #bfbfbf; }
.b3 { background: #c6efce; }
.b4 { background: #92d050; }
.b5 { background: #ffe699; }
.b6 { background: #f4b183; }
.b7 { background: #ff6b6b; color: white; }

.row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}
.grow { flex: 1; }

.toggle {
  width: 100%;
  min-height: 52px;
  border: 1px solid var(--line);
  background: #f7f3ec;
  border-radius: 14px;
  font-size: 16px;
  font-weight: 700;
  color: var(--ink);
}
.toggle[aria-pressed="true"] {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.chip {
  border: 1px solid var(--line);
  background: #f7f3ec;
  border-radius: 999px;
  padding: 10px 12px;
  min-height: 44px;
  font-size: 14px;
  font-weight: 650;
  color: var(--ink);
  max-width: 100%;
}
.chip[aria-pressed="true"] {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.stepper {
  display: flex;
  align-items: center;
  gap: 10px;
}
.stepper button {
  width: 48px;
  height: 48px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: #fff;
  font-size: 22px;
  font-weight: 700;
}
.stepper .val {
  min-width: 28px;
  text-align: center;
  font-size: 22px;
  font-weight: 800;
}

.field {
  width: 100%;
  min-height: 48px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: #fff;
  padding: 0 12px;
  font-size: 16px;
  color: var(--ink);
  -webkit-user-select: text;
  user-select: text;
}
textarea.field {
  min-height: 72px;
  padding: 12px;
  resize: none;
}

.grid2,
.datetime {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 8px;
  align-items: start;
}
.grid2 > *,
.datetime > * {
  min-width: 0;
}
.datetime > label {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.datetime .field {
  display: block;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  height: 48px;
  padding: 0 8px;
  overflow: hidden;
  -webkit-appearance: none;
  appearance: none;
}
input[type="date"]::-webkit-date-and-time-value,
input[type="time"]::-webkit-date-and-time-value {
  text-align: center;
  height: 48px;
  line-height: 48px;
  padding: 0;
  margin: 0;
}
input[type="date"]::-webkit-datetime-edit,
input[type="time"]::-webkit-datetime-edit {
  width: 100%;
  padding: 0;
  display: flex;
  justify-content: center;
}
input[type="date"]::-webkit-calendar-picker-indicator,
input[type="time"]::-webkit-calendar-picker-indicator {
  margin: 0 0 0 4px;
  padding: 0;
  width: 16px;
  height: 16px;
}

.save-bar {
  flex-shrink: 0;
  width: 100%;
  padding: 8px 0 10px;
  background: var(--bg);
  z-index: 5;
}
.save {
  width: 100%;
  min-height: 54px;
  border: 0;
  border-radius: 16px;
  background: var(--accent);
  color: white;
  font-size: 17px;
  font-weight: 800;
  margin-top: 4px;
}
.save:disabled { opacity: 0.45; }
.save.secondary { background: #fff; color: var(--ink); border: 1px solid var(--line); }

.nav {
  flex-shrink: 0;
  width: 100%;
  padding: 8px 14px calc(8px + env(safe-area-inset-bottom, 0px));
  background: rgba(244, 239, 230, 0.96);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
  display: flex;
  justify-content: center;
  z-index: 20;
}
.nav inner, .nav .inner {
  width: min(480px, 100%);
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
}
.nav button {
  min-height: 48px;
  border-radius: 14px;
  border: 0;
  background: transparent;
  color: var(--muted);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
.nav button svg {
  width: 26px;
  height: 26px;
  display: block;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.nav button[aria-current="page"] {
  background: var(--card);
  color: var(--accent-2);
  box-shadow: var(--shadow);
}

/* Home-screen PWA already ends above the system gesture bar, and
   safe-area-inset-bottom lifts the icons a second time. */
@media (display-mode: standalone) {
  html, body {
    height: 100dvh;
    min-height: 100dvh;
  }
  .nav {
    padding: 6px 12px 0;
  }
}
html.standalone,
html.standalone body {
  height: 100dvh;
  min-height: 100dvh;
}
html.standalone .nav {
  padding: 6px 12px 0;
}

.hidden { display: none !important; }

.kbju-table {
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
}
.kbju-table th,
.kbju-table td {
  padding: 10px 6px;
  text-align: right;
  border-bottom: 1px solid var(--line);
  font-size: 15px;
}
.kbju-table th:first-child,
.kbju-table td:first-child {
  text-align: left;
  font-weight: 750;
}
.kbju-table th {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.04em;
}
.kbju-table .week-head td {
  padding-top: 16px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
  text-align: left;
  border-bottom: 0;
}
.kbju-table .week-avg td {
  font-weight: 800;
  background: #f7f3ec;
}
.kbju-table tbody:last-child .week-avg td { border-bottom: 0; }

.day-title {
  font-size: 13px;
  font-weight: 800;
  color: var(--muted);
  margin: 14px 2px 8px;
}
.entry-day {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  margin-bottom: 8px;
  overflow: hidden;
}
.entry {
  display: flex;
  gap: 10px;
  align-items: center;
  width: 100%;
  text-align: left;
  background: none;
  border: 0;
  border-top: 1px solid var(--line);
  padding: 8px 10px;
  color: var(--ink);
  font: inherit;
}
.entry:first-child { border-top: 0; }
.entry:active { background: #f7f3ec; }
.badge {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  font-weight: 800;
  font-size: 17px;
  flex: none;
}
.entry-main { display: flex; flex-direction: column; min-width: 0; }
.entry .title { font-weight: 800; font-size: 15px; }
.entry .meta {
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.entry-day-head {
  padding: 7px 10px 6px;
  font-size: 12px;
  color: var(--muted);
  background: #f7f3ec;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.entry-day-head b { color: var(--ink); font-weight: 800; }
.entry-day-head + .entry { border-top: 0; }

.stat {
  display: flex;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
  gap: 12px;
}
.stat:last-child { border-bottom: 0; }
.stat b { font-size: 18px; }

.bars { display: grid; gap: 8px; margin-top: 8px; }
.bar-row { display: grid; grid-template-columns: 24px 1fr 28px; gap: 8px; align-items: center; }
.bar {
  height: 12px;
  background: #eee7dc;
  border-radius: 99px;
  overflow: hidden;
}
.bar span { display: block; height: 100%; border-radius: 99px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--nav) + env(safe-area-inset-bottom, 0px) + 10px);
  transform: translateX(-50%) translateY(20px);
  background: var(--ink);
  color: white;
  padding: 10px 14px;
  border-radius: 999px;
  font-weight: 700;
  opacity: 0;
  pointer-events: none;
  transition: 0.2s ease;
  z-index: 30;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.login {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: var(--bg);
  min-height: 100dvh;
  display: grid;
  place-items: center;
  text-align: center;
  padding: 24px 14px;
}
.login > div { width: min(360px, 100%); }
.login h1 { margin-bottom: 8px; }
.login form { width: 100%; }
.danger { color: var(--danger); font-size: 13px; min-height: 18px; }

.edit-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 8px; }
.del {
  background: #fff;
  color: var(--danger);
  border: 1px solid #f0c7c2;
  border-radius: 16px;
  min-height: 48px;
  font-weight: 800;
}

.empty { color: var(--muted); text-align: center; padding: 28px 8px; }
.reuse-text { font-size: 15px; font-weight: 650; line-height: 1.4; }

.kbju {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}
.kbju .cell {
  background: #f7f3ec;
  border-radius: 12px;
  padding: 8px 4px;
  text-align: center;
}
.kbju .cell b {
  display: block;
  font-size: 15px;
  font-weight: 800;
}
.kbju .cell span {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}
.kbju-inputs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  margin-bottom: 10px;
}
.kbju-inputs label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 11px;
  font-weight: 800;
  color: var(--muted);
  text-align: center;
}
.kbju-inputs .field {
  text-align: center;
  padding: 0 4px;
  min-height: 44px;
}
.weight-input {
  width: 84px;
  min-width: 84px;
  text-align: center;
  padding: 0 6px;
  font-size: 20px;
  font-weight: 800;
}
.draft-item {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: center;
  padding: 8px 0;
  border-top: 1px solid var(--line);
}
.draft-item .draft-edit {
  flex: 1;
  min-width: 0;
  border: 0;
  background: transparent;
  color: inherit;
  text-align: left;
  padding: 0;
}
.draft-item .draft-edit .title { font-weight: 800; }
.draft-item > button[data-draft] {
  border: 0;
  background: transparent;
  color: var(--danger);
  font-weight: 800;
  min-height: 36px;
}
.draft-item.editing {
  background: #f3f7f4;
  border-radius: 12px;
  padding-left: 8px;
  padding-right: 8px;
}
.draft-hint {
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
  margin-top: 10px;
}
#food-add-item { margin-top: 10px; }
.food-search {
  position: relative;
  margin: 0 0 12px;
}
.suggest {
  position: relative;
  z-index: 8;
  max-height: 240px;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: none;
  -webkit-overflow-scrolling: touch;
  margin-top: 6px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--shadow);
}
.suggest button {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 10px;
  width: 100%;
  text-align: left;
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  background: transparent;
  padding: 12px 14px;
  min-height: 52px;
  font-size: 15px;
  font-weight: 700;
  color: var(--ink);
}
.suggest button:last-child { border-bottom: 0; }
.suggest button[aria-selected="true"] { background: #f7f3ec; }
.suggest button .meta {
  flex: none;
  font-size: 12px;
  font-weight: 650;
  color: var(--muted);
  white-space: nowrap;
}
.suggest-empty {
  padding: 12px 14px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 650;
}
.food-today { padding-top: 12px; padding-bottom: 12px; }
.food-today-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.today-day { font-size: 16px; font-weight: 750; }
.today-macros { font-size: 13px; color: var(--muted); margin-top: 2px; font-variant-numeric: tabular-nums; }
.refresh {
  border: 0;
  background: transparent;
  color: var(--accent);
  font-size: 15px;
  font-weight: 750;
  padding: 4px 2px;
}
.refresh:disabled { opacity: 0.45; }
.push {
  display: block;
  margin-top: 8px;
  border: 0;
  background: transparent;
  color: var(--accent);
  font-size: 15px;
  font-weight: 750;
  padding: 4px 0;
  text-align: left;
}
.push:disabled { opacity: 0.55; }
.push-note {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

button, input, textarea, select { font-family: inherit; }
button { cursor: pointer; touch-action: manipulation; }

.kbju-table .day-row { cursor: pointer; -webkit-tap-highlight-color: transparent; }
.kbju-table .day-row:active td { background: #f7f3ec; }
.kbju-table .day-row .chev {
  display: inline-block;
  width: 14px;
  color: var(--muted);
  transition: transform 0.15s ease;
}
.kbju-table .day-row.open .chev { transform: rotate(90deg); }
.kbju-table .day-row.open td { border-bottom: 0; }
.kbju-table .day-detail td {
  padding: 0 0 10px;
  text-align: left;
  font-weight: 400;
}
.meal-sum {
  background: #f7f3ec;
  border-radius: 12px;
  padding: 8px 10px;
  margin-top: 6px;
}
.meal-sum-head {
  display: flex;
  justify-content: space-between;
  width: 100%;
  padding: 0;
  border: 0;
  background: none;
  font: inherit;
  font-size: 14px;
  color: var(--ink);
  text-align: left;
}
.meal-sum-head span { color: var(--accent); font-weight: 750; }
.meal-sum-macros { font-size: 12px; color: var(--muted); margin: 2px 0 4px; }
.meal-sum ul { list-style: none; margin: 0; padding: 0; }
.meal-sum li {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 3px 0;
  font-size: 14px;
  border-top: 1px solid var(--line);
}
.meal-sum li i { font-style: normal; color: var(--muted); font-size: 13px; }
.meal-sum li .kcal { font-variant-numeric: tabular-nums; }
.kbju-table .day-row td:first-child { white-space: nowrap; }
.kbju-table th,
.kbju-table td { padding-left: 4px; padding-right: 4px; }
.kbju-table .day-detail td { padding-left: 0; padding-right: 0; }

#food-log .meal-sum {
  background: var(--card);
  border: 1px solid var(--line);
  margin: 0 0 8px;
}
.history-link {
  display: block;
  width: 100%;
  margin: 4px 0 8px;
  padding: 10px 4px;
  border: 0;
  background: none;
  color: var(--accent);
  font-size: 14px;
  font-weight: 750;
  text-align: center;
}
.fold-line {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-height: 40px;
  padding: 6px 2px;
  margin-bottom: 8px;
  border: 0;
  border-bottom: 1px dashed var(--line);
  background: none;
  color: var(--ink);
  font-size: 15px;
  font-weight: 750;
  text-align: left;
}
.fold-edit { color: var(--accent); font-size: 16px; }
.fold-body { margin-bottom: 10px; }
.fold-body .chips { margin-top: 10px; }
.per-line {
  font-size: 13px;
  font-weight: 650;
  color: var(--muted);
  margin: 8px 0 0;
  border-bottom: 0;
  min-height: 32px;
}
#food-per { margin-top: 10px; }
.weight-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 10px;
}
.weight-row .label { margin-bottom: 0; }
.portion-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 10px;
}
.portion-text { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.portion-text b { display: block; font-size: 16px; color: var(--ink); }
.portion-row .save { width: auto; flex: none; margin: 0; padding: 0 18px; min-height: 44px; }

.bristol {
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 4px;
}
.bristol button {
  min-height: 52px;
  padding: 0;
  border-radius: 12px;
  font-size: 19px;
}
.bristol button small { font-size: 8.5px; letter-spacing: -0.02em; }
.when-line { margin: 10px 0 0; border-bottom: 0; }
#when { margin: 6px 0 0; }
.line-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 44px;
}
.line-row .label { margin-bottom: 0; }
.line-row + .line-row,
.chips.sm + .line-row { border-top: 1px solid var(--line); margin-top: 6px; padding-top: 6px; }
.stepper.sm { gap: 6px; }
.stepper.sm button { width: 38px; height: 38px; border-radius: 12px; font-size: 20px; }
.stepper.sm .val { min-width: 24px; font-size: 19px; }
.chips.sm .chip { min-height: 34px; padding: 6px 12px; font-size: 14px; }
.line-split { border-top: 1px solid var(--line); margin-top: 6px; padding-top: 8px; }
.note-card { padding: 8px; }
.note-card textarea.field { min-height: 44px; resize: none; padding-top: 11px; padding-bottom: 11px; }
