:root {
  --bg: #eef2f6;
  --surface: #ffffff;
  --ink: #1f2937;
  --muted: #6b7280;
  --line: #e2e8f0;
  --teal: #0f766e;
  --teal-dark: #0b5d57;
  --danger: #dc2626;
  --amber: #d97706;
  --green: #059669;
  --blue: #2563eb;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.08);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "PingFang SC", "Microsoft YaHei", "Segoe UI", system-ui, sans-serif;
  line-height: 1.5;
}

.app {
  min-height: 100vh;
  min-height: 100dvh;
}

button {
  font: inherit;
  letter-spacing: 0;
}

button:focus-visible,
input:focus-visible {
  outline: 2px solid var(--teal);
  outline-offset: 1px;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: calc(12px + var(--safe-top)) 24px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 8px;
  background: var(--teal);
  font-size: 20px;
  flex: none;
}

.brand-text {
  font-size: 18px;
  font-weight: 700;
  white-space: nowrap;
}

.view-tabs {
  display: flex;
  gap: 4px;
  margin-left: auto;
  padding: 4px;
  background: #e8edf3;
  border-radius: 8px;
}

.view-tab {
  padding: 8px 16px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  white-space: nowrap;
}

.view-tab.is-active {
  background: var(--surface);
  color: var(--ink);
  font-weight: 600;
  box-shadow: var(--shadow);
}

main {
  width: min(1200px, 100%);
  margin: 0 auto;
  padding: 20px 24px calc(80px + var(--safe-bottom));
}

.view {
  display: none;
}

.view.is-active {
  display: block;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
  padding: 14px 16px;
  margin-bottom: 18px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.date-nav,
.toolbar-controls,
.people-control {
  display: flex;
  align-items: center;
  gap: 8px;
}

.date-box {
  display: flex;
  flex-direction: column;
  line-height: 1.25;
  min-width: 104px;
}

.date-box strong {
  font-size: 15px;
}

.date-box span {
  font-size: 12px;
  color: var(--muted);
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
  font-size: 16px;
  flex: none;
}

.icon-btn:hover {
  border-color: var(--teal);
  color: var(--teal);
}

.btn-ghost,
.btn-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
  font-size: 13px;
  white-space: nowrap;
}

.btn-ghost:hover {
  border-color: var(--teal);
  color: var(--teal);
}

.btn-primary {
  border-color: var(--teal);
  background: var(--teal);
  color: #ffffff;
  font-weight: 600;
}

.btn-primary:hover {
  background: var(--teal-dark);
}

.control-label {
  font-size: 13px;
  color: var(--muted);
}

.people-control {
  padding: 4px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.people-value {
  min-width: 24px;
  text-align: center;
  font-weight: 700;
  font-size: 15px;
}

.badge {
  min-width: 20px;
  padding: 1px 7px;
  border-radius: 999px;
  background: #fee2e2;
  color: #b91c1c;
  font-size: 12px;
  text-align: center;
}

.offline-status {
  padding: 3px 8px;
  border-radius: 999px;
  background: #fef2f2;
  color: #b91c1c;
  font-size: 11px;
  white-space: nowrap;
}

.offline-status.is-ready {
  background: #ecfdf5;
  color: #047857;
}

.toggle-label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--ink);
  cursor: pointer;
  white-space: nowrap;
}

.toggle-label input {
  width: 16px;
  height: 16px;
  margin: 0;
  accent-color: var(--teal);
  cursor: pointer;
}

.meal-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 16px;
  align-items: start;
}

.meal-panel {
  display: flex;
  flex-direction: column;
  min-height: 330px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}

.meal-breakfast {
  --meal: var(--amber);
  --meal-soft: #fef3c7;
}

.meal-lunch {
  --meal: var(--green);
  --meal-soft: #d1fae5;
}

.meal-dinner {
  --meal: var(--blue);
  --meal-soft: #dbeafe;
}

.meal-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

.meal-emoji {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 8px;
  background: var(--meal-soft);
  font-size: 20px;
  flex: none;
}

.meal-title {
  font-size: 16px;
  font-weight: 700;
}

.meal-sub {
  font-size: 12px;
  color: var(--muted);
}

.count-stepper {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-left: auto;
}

.count-btn {
  display: grid;
  place-items: center;
  width: 26px;
  height: 26px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--meal);
  cursor: pointer;
  font-size: 15px;
}

.count-btn:hover {
  border-color: var(--meal);
}

.count-value {
  min-width: 18px;
  text-align: center;
  font-weight: 700;
  font-size: 14px;
}

.meal-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
}

.member-tabs {
  display: flex;
  gap: 6px;
  padding: 10px 14px 0;
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
}

.member-tab {
  flex: none;
  padding: 6px 12px;
  border: 1px solid var(--line);
  border-bottom: none;
  border-radius: 8px 8px 0 0;
  background: #f8fafc;
  color: var(--muted);
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
}

.member-tab.is-active {
  background: var(--surface);
  color: var(--ink);
  font-weight: 600;
  border-color: #cbd5e1;
}

.meal-nutrients {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-right: auto;
  flex-wrap: wrap;
}

.nutrient-chip {
  padding: 3px 8px;
  border: 1px solid #e2e8f0;
  border-radius: 999px;
  background: #f8fafc;
  color: #9ca3af;
  font-size: 11px;
  white-space: nowrap;
}

.nutrient-chip.is-on {
  border-color: #a7f3d0;
  background: #ecfdf5;
  color: #047857;
  font-weight: 600;
}

.btn-ghost.small {
  padding: 6px 10px;
  font-size: 12px;
}

.meal-list {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
}

.dish-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  background: #f8fafc;
  border: 1px solid #e8edf3;
  border-radius: 8px;
}

.dish-row.status-ate {
  background: #ecfdf5;
  border-color: #a7f3d0;
}

.dish-row.status-skipped {
  opacity: 0.72;
}

.dish-row.has-avoid {
  border-color: #fecaca;
}

.dish-emoji {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  font-size: 17px;
  flex: none;
}

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

.dish-name {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
}

.dish-meta {
  margin-top: 2px;
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.avoid-tag {
  flex: none;
  padding: 0 6px;
  border-radius: 999px;
  background: #fef2f2;
  color: var(--danger);
  font-size: 10px;
  font-weight: 600;
}

.pantry-tag {
  background: #ecfdf5;
  color: #047857;
}

.fixed-tag {
  background: #eff6ff;
  color: #1d4ed8;
}

.dish-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: none;
}

.status-btn {
  padding: 5px 9px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--muted);
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
}

.status-btn.status-ate {
  border-color: #a7f3d0;
  background: #ecfdf5;
  color: #047857;
  font-weight: 600;
}

.status-btn.status-skipped {
  border-color: #e5e7eb;
  background: #f3f4f6;
  color: #9ca3af;
}

.row-btn {
  width: 28px;
  height: 28px;
  font-size: 13px;
}

.row-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.meal-empty {
  padding: 28px 16px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
}

.meal-photos {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  padding: 0 14px 12px;
}

.meal-photo {
  position: relative;
  width: 72px;
  height: 72px;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  flex: none;
}

.meal-photo img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.photo-remove {
  position: absolute;
  top: 4px;
  right: 4px;
  display: grid;
  place-items: center;
  width: 20px;
  height: 20px;
  padding: 0;
  border: none;
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.68);
  color: #ffffff;
  font-size: 11px;
  cursor: pointer;
}

.add-btn {
  margin: 0 14px 14px;
  padding: 9px;
  border: 1px dashed #94a3b8;
  border-radius: 8px;
  background: transparent;
  color: var(--teal);
  font-size: 13px;
  cursor: pointer;
}

.add-btn:hover {
  border-style: solid;
  background: #f0fdfa;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.history-day {
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.history-head {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.history-date {
  font-size: 15px;
  font-weight: 700;
}

.history-weekday {
  font-size: 12px;
  color: var(--muted);
}

.history-summary {
  margin-left: auto;
  font-size: 13px;
  color: var(--muted);
}

.history-edit {
  padding: 6px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--teal);
  font-size: 13px;
  cursor: pointer;
}

.history-edit:hover {
  border-color: var(--teal);
}

.history-meals {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.history-meal-title {
  margin: 0 0 6px;
  font-size: 13px;
  font-weight: 600;
}

.history-meal-title.breakfast {
  color: var(--amber);
}

.history-meal-title.lunch {
  color: var(--green);
}

.history-meal-title.dinner {
  color: var(--blue);
}

.history-dishes {
  display: flex;
  flex-direction: column;
  gap: 3px;
  margin: 0;
  padding: 0;
  list-style: none;
  font-size: 13px;
}

.history-dishes .ate {
  color: #047857;
}

.history-dishes .skipped {
  color: #9ca3af;
  text-decoration: line-through;
}

.history-dishes .planned {
  color: #374151;
}

.member-line {
  margin-top: 4px;
  font-size: 12px;
  color: #475569;
}

.member-line strong {
  color: var(--teal);
}

.history-empty {
  padding: 48px 20px;
  background: var(--surface);
  border: 1px dashed var(--line);
  border-radius: 8px;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px;
}

.stat-card {
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.stat-label {
  font-size: 12px;
  color: var(--muted);
}

.stat-value {
  margin-top: 6px;
  font-size: 26px;
  font-weight: 700;
}

.stat-sub {
  margin-top: 4px;
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.charts-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 12px;
  margin-top: 12px;
}

.chart-card {
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.chart-title {
  margin: 0 0 14px;
  font-size: 15px;
  font-weight: 700;
}

.bar-row {
  margin: 12px 0;
}

.bar-label {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 5px;
  font-size: 13px;
}

.bar-label strong {
  font-weight: 600;
}

.bar-label span {
  color: var(--muted);
}

.bar-track {
  height: 8px;
  border-radius: 999px;
  background: #e8edf3;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  border-radius: 999px;
}

.table-scroll {
  overflow-x: auto;
}

.daily-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.daily-table th,
.daily-table td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

.daily-table th {
  color: var(--muted);
  font-weight: 500;
  white-space: nowrap;
}

.daily-table td {
  color: #374151;
}

.daily-table .date-cell {
  white-space: nowrap;
  font-weight: 600;
}

.daily-table .empty-cell {
  color: #9ca3af;
}

.photo-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 12px;
}

.photo-item {
  margin: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface);
}

.photo-item img {
  display: block;
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
}

.photo-caption {
  padding: 6px 8px;
  font-size: 12px;
  color: var(--muted);
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(15, 23, 42, 0.45);
}

.modal-backdrop[hidden] {
  display: none;
}

.modal {
  display: flex;
  flex-direction: column;
  width: min(560px, 100%);
  max-height: 84vh;
  overflow: auto;
  padding: 20px;
  background: var(--surface);
  border-radius: 8px;
}

.modal-head {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.modal-head h2 {
  margin: 0;
  font-size: 17px;
}

.chip-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 16px;
}

.chip {
  padding: 7px 13px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #f8fafc;
  color: #374151;
  font-size: 13px;
  cursor: pointer;
}

.chip:hover {
  border-color: var(--teal);
}

.chip.is-on {
  border-color: var(--teal);
  background: var(--teal);
  color: #ffffff;
  font-weight: 600;
}

.chip-remove {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  margin-left: 6px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.28);
  color: inherit;
  font-size: 11px;
  cursor: pointer;
}

.input-row {
  display: flex;
  gap: 8px;
  flex: none;
  margin-top: 14px;
}

.member-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1 1 auto;
  min-height: 60px;
  overflow-y: auto;
  margin-top: 16px;
}

.member-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fafc;
}

.member-avatar {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 8px;
  background: #dbeafe;
  color: #1d4ed8;
  font-size: 15px;
  font-weight: 700;
  flex: none;
}

.member-name {
  flex: 1;
  min-width: 0;
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.member-empty {
  padding: 18px 12px;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
}

.ratio-block {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 16px;
}

.segmented {
  display: flex;
  gap: 4px;
  padding: 4px;
  background: #e8edf3;
  border-radius: 8px;
}

.seg-btn {
  padding: 7px 16px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 13px;
}

.seg-btn.is-on {
  background: var(--surface);
  color: var(--ink);
  font-weight: 600;
  box-shadow: var(--shadow);
}

input[type="text"],
input[type="search"] {
  flex: 1;
  min-width: 0;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  font-size: 14px;
}

input::placeholder {
  color: #9ca3af;
}

.modal-foot {
  display: flex;
  justify-content: flex-end;
  flex: none;
  margin-top: 18px;
}

.pick-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 280px;
  overflow: auto;
  margin-top: 12px;
}

.pick-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  cursor: pointer;
}

.pick-item:hover {
  border-color: var(--teal);
  background: #f0fdfa;
}

.pick-item .dish-emoji {
  width: 32px;
  height: 32px;
  font-size: 16px;
}

.pick-item .dish-meta {
  font-size: 11px;
}

.pick-empty {
  padding: 18px 12px;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
}

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  z-index: 100;
  transform: translateX(-50%);
  padding: 10px 18px;
  border-radius: 8px;
  background: #1f2937;
  color: #ffffff;
  font-size: 13px;
  box-shadow: 0 4px 14px rgba(15, 23, 42, 0.25);
}

.toast[hidden] {
  display: none;
}

@media (max-width: 760px) {
  .topbar {
    flex-wrap: wrap;
    padding: calc(10px + var(--safe-top)) 12px 10px;
    gap: 10px;
  }

  .view-tabs {
    width: 100%;
    justify-content: stretch;
  }

  .view-tab {
    flex: 1;
    padding: 9px 4px;
  }

  main {
    padding: 14px 12px calc(72px + var(--safe-bottom));
  }

  .toolbar {
    align-items: stretch;
    padding: 12px;
    gap: 10px;
  }

  .date-nav,
  .toolbar-controls {
    width: 100%;
    justify-content: space-between;
  }

  .toolbar-controls {
    flex-wrap: wrap;
  }

  .people-control {
    flex: 1;
    justify-content: center;
  }

  .toolbar-controls .btn-ghost,
  .toolbar-controls .btn-primary {
    flex: 1;
    padding: 10px 8px;
  }

  .toggle-label {
    width: 100%;
    justify-content: center;
    padding: 6px 0;
  }

  .meal-grid {
    gap: 12px;
  }

  .meal-actions {
    padding: 8px 10px;
  }

  .meal-nutrients {
    width: 100%;
    margin-right: 0;
  }

  .dish-row {
    gap: 8px;
    padding: 8px;
  }

  .dish-emoji {
    width: 32px;
    height: 32px;
  }

  .status-btn {
    padding: 5px 7px;
  }

  .meal-photo {
    width: 64px;
    height: 64px;
  }

  .charts-row {
    grid-template-columns: 1fr;
  }

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

  .history-meals {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .history-summary {
    width: 100%;
    margin-left: 0;
  }

  .modal-backdrop {
    align-items: flex-end;
    padding: 0;
  }

  .modal {
    width: 100%;
    max-height: 88vh;
    border-radius: 8px 8px 0 0;
    padding: 18px 16px calc(20px + var(--safe-bottom));
  }

  .member-list {
    max-height: 42vh;
  }

  input[type="text"],
  input[type="search"] {
    font-size: 16px;
  }
}
