/* ---- theme ---------------------------------------------------------------- */
:root {
  --bg: #f2f2f7;
  --card: #ffffff;
  --text: #111114;
  --muted: #6c6c75;
  --line: #e3e3e9;
  --track: #e6e6ec;
  --accent: #1f9d63;
  --accent-text: #ffffff;
  --over: #e5484d;
  --chip: #e9f6ef;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
  --radius: 16px;
  --font: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #000000;
    --card: #1c1c1e;
    --text: #f5f5f7;
    --muted: #9a9aa3;
    --line: #2c2c2e;
    --track: #2c2c2e;
    --accent: #30c97b;
    --accent-text: #04140b;
    --over: #ff6369;
    --chip: #14281e;
    --shadow: 0 8px 24px rgba(0, 0, 0, 0.6);
    color-scheme: dark;
  }
}

:root[data-theme='dark'] {
  --bg: #000000;
  --card: #1c1c1e;
  --text: #f5f5f7;
  --muted: #9a9aa3;
  --line: #2c2c2e;
  --track: #2c2c2e;
  --accent: #30c97b;
  --accent-text: #04140b;
  --over: #ff6369;
  --chip: #14281e;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.6);
  color-scheme: dark;
}

/* ---- base ----------------------------------------------------------------- */
* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.35 var(--font);
  overscroll-behavior-y: none;
}

button,
input,
select {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  touch-action: manipulation;
}

a {
  color: inherit;
  text-decoration: none;
}

h1,
h2 {
  margin: 0;
}

.muted {
  color: var(--muted);
}

#screen {
  max-width: 520px;
  margin: 0 auto;
  padding: env(safe-area-inset-top) 16px calc(112px + env(safe-area-inset-bottom));
  min-height: 100dvh;
}

.fatal {
  margin: 48px 16px;
  padding: 16px;
  border-radius: var(--radius);
  background: var(--card);
}

/* ---- buttons -------------------------------------------------------------- */
.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--text);
}

.icon-btn:active {
  background: var(--track);
}

.icon-btn.danger,
.link-btn.danger {
  color: var(--over);
}

.link-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  min-height: 44px;
  padding: 0 8px;
  border: 0;
  background: none;
  color: var(--accent);
  font-weight: 600;
}

.link-btn:disabled {
  opacity: 0.4;
}

.btn {
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--card);
  font-weight: 600;
}

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

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

/* ---- Today ---------------------------------------------------------------- */
.top-bar {
  display: flex;
  align-items: center;
  gap: 2px;
  padding-top: 8px;
}

.date-btn {
  min-height: 44px;
  padding: 0 8px;
  border: 0;
  background: none;
  font-size: 1.1rem;
  font-weight: 700;
}

.date-btn {
  white-space: nowrap;
}

.today-pill {
  margin-top: 8px;
  padding: 6px 16px;
  border: 0;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 0.85rem;
  font-weight: 700;
}

.spacer {
  flex: 1;
}

.top {
  touch-action: pan-y;
}

.hero {
  padding: 20px 0 12px;
  text-align: center;
}

.hero-num {
  font-size: 4.5rem;
  font-weight: 800;
  line-height: 1;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
}

.hero-label {
  margin-top: 4px;
  color: var(--muted);
  font-weight: 600;
}

.hero.over .hero-num,
.hero.over .hero-label {
  color: var(--over);
}

.bars {
  display: grid;
  gap: 14px;
  padding: 8px 0 16px;
}

.bar-text {
  display: flex;
  justify-content: space-between;
  margin-bottom: 6px;
  font-weight: 600;
}

.bar-nums {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.bar-track {
  height: 10px;
  border-radius: 999px;
  background: var(--track);
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  border-radius: inherit;
  background: var(--accent);
  transition: width 0.25s;
}

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

.weight-row {
  display: flex;
  justify-content: space-between;
  width: 100%;
  min-height: 48px;
  padding: 0 16px;
  align-items: center;
  border: 0;
  border-radius: var(--radius);
  background: var(--card);
  font-weight: 600;
}

.weight-val {
  font-variant-numeric: tabular-nums;
}

.banners {
  display: grid;
  gap: 8px;
  margin-top: 12px;
}

.banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--chip);
  font-size: 0.92rem;
  font-weight: 600;
}

.banner-close {
  flex: none;
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 50%;
  background: transparent;
}

.list-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 20px;
}

.list-head h2 {
  font-size: 1.05rem;
}

.entry-list {
  overflow: hidden;
  border-radius: var(--radius);
  background: var(--card);
}

.row-wrap {
  position: relative;
  overflow: hidden;
  border-bottom: 1px solid var(--line);
}

.row-wrap:last-child {
  border-bottom: 0;
}

.row-bg {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 24px;
  background: var(--over);
  color: #fff;
}

.row {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 56px;
  padding: 8px 16px;
  background: var(--card);
  touch-action: pan-y;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.row.selected {
  background: var(--chip);
}

.check {
  display: inline-grid;
  place-items: center;
  flex: none;
  width: 22px;
  height: 22px;
  border: 2px solid var(--muted);
  border-radius: 50%;
}

.check.on {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--accent-text);
}

.row-main {
  flex: 1;
  min-width: 0;
}

.row-name {
  overflow: hidden;
  font-weight: 600;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.row-sub,
.row-protein {
  color: var(--muted);
  font-size: 0.82rem;
}

.row-nums {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.row-kcal {
  font-weight: 700;
}

.empty-note {
  margin: 0;
  padding: 24px 16px;
  color: var(--muted);
  text-align: center;
}

.fab {
  position: fixed;
  right: max(20px, calc(50% - 240px));
  bottom: calc(20px + env(safe-area-inset-bottom));
  display: grid;
  place-items: center;
  width: 64px;
  height: 64px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
  box-shadow: var(--shadow);
}

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

.select-bar {
  position: fixed;
  right: 0;
  bottom: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 520px;
  margin: 0 auto;
  padding: 8px 16px calc(8px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--card);
}

.select-bar > span {
  flex: 1;
  font-weight: 600;
}

/* ---- sheets and dialogs --------------------------------------------------- */
.backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  background: transparent;
  transition: background 0.18s;
}

.backdrop.open {
  background: rgba(0, 0, 0, 0.4);
}

.sheet-strip {
  flex: 1;
  min-height: 24px;
}

.sheet {
  width: 100%;
  max-width: 520px;
  margin: 0 auto;
  padding: 0 16px calc(12px + env(safe-area-inset-bottom));
  border-radius: 22px 22px 0 0;
  background: var(--bg);
  transform: translateY(100%);
  transition: transform 0.2s ease-out;
}

.backdrop.open .sheet {
  transform: none;
}

.sheet-head {
  padding: 8px 0 4px;
  touch-action: none;
}

.grabber {
  width: 40px;
  height: 5px;
  margin: 0 auto 6px;
  border-radius: 3px;
  background: var(--line);
}

.sheet-title {
  font-size: 1.05rem;
  text-align: center;
}

.sheet-message {
  margin: 4px 0 12px;
  color: var(--muted);
  text-align: center;
}

.action-sheet {
  padding-top: 16px;
}

.action-list {
  overflow: hidden;
  border-radius: 14px;
  background: var(--card);
}

.action {
  display: block;
  width: 100%;
  min-height: 52px;
  padding: 0 16px;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: var(--card);
  font-size: 1.05rem;
}

.action-list .action:last-child {
  border-bottom: 0;
}

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

.action.cancel,
.action.primary {
  margin-top: 8px;
  border: 0;
  border-radius: 14px;
  font-weight: 600;
}

.action.primary {
  background: var(--accent);
  color: var(--accent-text);
}

.text-input {
  display: block;
  width: 100%;
  min-height: 48px;
  margin: 12px 0 4px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--card);
}

/* add sheet */
.chips {
  display: flex;
  gap: 8px;
  min-height: 44px;
  padding: 4px 0 10px;
  overflow-x: auto;
  scrollbar-width: none;
}

.chips::-webkit-scrollbar {
  display: none;
}

.chip {
  display: inline-flex;
  flex: none;
  align-items: center;
  gap: 8px;
  height: 40px;
  padding: 0 14px;
  border: 0;
  border-radius: 999px;
  background: var(--chip);
  font-weight: 600;
  white-space: nowrap;
}

.chip:active {
  filter: brightness(0.92);
}

.chip-kcal {
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.chips-empty {
  align-self: center;
  color: var(--muted);
  font-size: 0.9rem;
}

.fields {
  display: grid;
  gap: 8px;
}

.fields-row {
  display: grid;
  grid-template-columns: 120px 1fr;
  gap: 8px;
}

.field {
  display: flex;
  flex-direction: column;
  justify-content: center;
  width: 100%;
  min-height: 56px;
  padding: 6px 14px;
  border: 2px solid transparent;
  border-radius: 14px;
  background: var(--card);
  text-align: left;
}

.field.active {
  border-color: var(--accent);
}

.field-label {
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 600;
}

.field-value {
  font-size: 1.3rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.field-kcal {
  min-height: 76px;
}

.field-kcal .field-value {
  font-size: 2.6rem;
  line-height: 1.1;
}

.field-value.empty {
  color: var(--line);
}

.field-name {
  min-height: 56px;
  font-size: 1rem;
}

.after-line {
  padding: 8px 2px;
  color: var(--muted);
  font-weight: 600;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.after-line.over {
  color: var(--over);
}

.sheet-actions {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}

.star-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 52px;
  padding: 0 16px;
  border: 0;
  border-radius: 14px;
  background: var(--card);
  font-weight: 600;
}

.star-btn.on {
  color: var(--accent);
}

.add-btn {
  flex: 1;
  min-height: 52px;
  border: 0;
  border-radius: 14px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 1.1rem;
  font-weight: 700;
}

.add-btn:disabled {
  opacity: 0.35;
}

.add-btn.armed {
  background: var(--over);
  color: #fff;
}

.pad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.pad.no-dot .key-dot {
  visibility: hidden;
}

.key {
  min-height: 54px;
  border: 0;
  border-radius: 12px;
  background: var(--card);
  font-size: 1.5rem;
  font-weight: 600;
  user-select: none;
  -webkit-user-select: none;
}

.key:active {
  background: var(--track);
}

.sheet.typing .pad,
.sheet.typing .after-line {
  display: none;
}

/* ---- toast, update banner ------------------------------------------------- */
#toast-root {
  position: fixed;
  right: 12px;
  bottom: calc(96px + env(safe-area-inset-bottom));
  left: 12px;
  z-index: 40;
  display: flex;
  justify-content: center;
  pointer-events: none;
}

.toast {
  display: flex;
  align-items: center;
  gap: 16px;
  max-width: 480px;
  padding: 10px 10px 10px 18px;
  border-radius: 999px;
  background: #1c1c1e;
  color: #fff;
  box-shadow: var(--shadow);
  font-weight: 600;
  pointer-events: auto;
}

.toast-action {
  min-height: 36px;
  padding: 0 14px;
  border: 0;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.16);
  color: #7ee2ad;
  font-weight: 700;
}

#update-root {
  position: fixed;
  top: env(safe-area-inset-top);
  right: 12px;
  left: 12px;
  z-index: 50;
}

.update-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 480px;
  margin: 8px auto;
  padding: 8px 8px 8px 16px;
  border-radius: 14px;
  background: var(--accent);
  color: var(--accent-text);
  box-shadow: var(--shadow);
  font-weight: 700;
}

.update-banner button {
  min-height: 36px;
  padding: 0 16px;
  border: 0;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.25);
  color: inherit;
  font-weight: 700;
}

/* ---- trends and settings pages -------------------------------------------- */
.page-bar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding-top: 8px;
}

.page-bar h1 {
  font-size: 1.4rem;
}

.range-chips {
  display: flex;
  gap: 8px;
  padding: 12px 0;
}

.range-chip {
  flex: 1;
  min-height: 40px;
  border: 0;
  border-radius: 999px;
  background: var(--card);
  font-weight: 600;
}

.range-chip.on {
  background: var(--accent);
  color: var(--accent-text);
}

.stats {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.stat {
  padding: 12px 14px;
  border-radius: var(--radius);
  background: var(--card);
}

.stat-value {
  font-size: 1.5rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.stat-label,
.stat-sub {
  color: var(--muted);
  font-size: 0.82rem;
}

.card {
  margin-top: 12px;
  padding: 14px 16px;
  border-radius: var(--radius);
  background: var(--card);
}

.card h2 {
  margin-bottom: 8px;
  font-size: 1rem;
}

.chart {
  display: block;
  width: 100%;
  height: auto;
}

.chart .grid {
  stroke: var(--line);
  stroke-width: 1;
}

.chart .axis-text {
  fill: var(--muted);
  font-size: 10px;
}

.chart .bar {
  fill: var(--accent);
}

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

.chart .target-line {
  stroke: var(--muted);
  stroke-width: 1.2;
  stroke-dasharray: 5 4;
}

.chart .trend-line {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2.5;
  stroke-linejoin: round;
}

.chart .dot {
  fill: var(--muted);
  opacity: 0.7;
}

.chart-hint,
.hint {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 0.82rem;
}

.settings-section h2 {
  color: var(--muted);
  font-size: 0.8rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 48px;
  border-top: 1px solid var(--line);
}

.settings-section h2 + .setting-row {
  border-top: 0;
}

.setting-row > .muted {
  flex: none;
  text-align: right;
}

.setting-label {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
}

.setting-label small {
  color: var(--muted);
  font-size: 0.78rem;
}

.num-input,
.select {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
}

.num-input {
  width: 100px;
  padding: 0 10px;
  text-align: right;
}

.select {
  max-width: 55%;
  padding: 0 8px;
}

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding-top: 8px;
}

.about {
  margin: 24px 0 0;
  color: var(--muted);
  font-size: 0.82rem;
  text-align: center;
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
  }
}

/* ---- meal groups ---------------------------------------------------------- */
.entry-list.grouped {
  display: grid;
  gap: 10px;
  overflow: visible;
  background: none;
}

.meal-group {
  overflow: hidden;
  border-radius: var(--radius);
  background: var(--card);
  outline: 2px solid transparent;
  transition: outline-color 0.12s;
}

.meal-group.empty {
  display: none;
}

.entry-list.dragging .meal-group.empty {
  display: block;
}

.meal-group.drop-target {
  outline-color: var(--accent);
}

.meal-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 10px 16px 6px;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.meal-kcal {
  font-variant-numeric: tabular-nums;
}

.meal-group.empty .meal-head {
  padding: 18px 16px;
}

.grip {
  display: grid;
  flex: none;
  place-items: center;
  width: 28px;
  height: 44px;
  margin-left: -10px;
  color: var(--muted);
  opacity: 0.7;
  cursor: grab;
  touch-action: none;
}

.drag-source {
  opacity: 0.3;
}

.drag-ghost {
  position: fixed;
  z-index: 60;
  border-radius: 12px;
  box-shadow: var(--shadow);
  pointer-events: none;
}

/* the red delete background only shows while a row is being swiped */
.row-bg {
  opacity: 0;
}

.row-wrap.swiping .row-bg {
  opacity: 1;
}

/* ---- compact rows (no timestamps) ------------------------------------------ */
.row {
  min-height: 46px;
  padding: 4px 16px;
}

.row-name {
  flex: 1;
  min-width: 0;
}

.row-nums {
  display: flex;
  flex: none;
  align-items: baseline;
  gap: 10px;
}

.row-protein {
  font-size: 0.82rem;
}

.meal-head {
  padding: 8px 16px 0;
}

.list-actions {
  display: flex;
  align-items: center;
}
