:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --text: #1d1d1b;
  --muted: #66645e;
  --border: #dedbd3;
  --accent: #2f6b4f;
  --accent-text: #ffffff;
  --ok: #2f6b4f;
  --ok-bg: #e5f1ea;
  --warn: #8a5a00;
  --warn-bg: #fdf0d5;
  --bad: #a3261b;
  --bad-bg: #fbe3e0;
  --frost: #1f5f8b;
  --frost-bg: #e2eff8;
  --radius: 10px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161614;
    --surface: #211f1d;
    --text: #efede8;
    --muted: #a8a59d;
    --border: #3a3834;
    --accent: #6cc59a;
    --accent-text: #0e1f16;
    --ok: #6cc59a;
    --ok-bg: #1c3228;
    --warn: #f0bc5e;
    --warn-bg: #3a2c12;
    --bad: #f28b80;
    --bad-bg: #3d1c19;
    --frost: #8cc8f0;
    --frost-bg: #16293a;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

main { max-width: 860px; margin: 0 auto; padding: 16px; }

h1, h2, h3 { line-height: 1.2; }
h1 { font-size: 1.15rem; margin: 0; }
h2 { font-size: 1.2rem; margin: 0 0 12px; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
#user-box { display: flex; gap: 8px; align-items: center; color: var(--muted); font-size: 0.9rem; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}
.narrow { max-width: 420px; margin: 32px auto; }

label { display: block; font-size: 0.9rem; font-weight: 600; margin-bottom: 12px; }
input, select, textarea {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 9px 10px;
  font: inherit;
  font-weight: 400;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
}
textarea { resize: vertical; }

button {
  font: inherit;
  font-weight: 600;
  padding: 9px 14px;
  border-radius: 8px;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: var(--accent-text);
  cursor: pointer;
}
button:disabled { opacity: 0.6; cursor: default; }
button.secondary { background: transparent; color: var(--text); border-color: var(--border); }
button.small { padding: 5px 10px; font-size: 0.85rem; }
button.danger { background: transparent; color: var(--bad); border-color: var(--border); }
button.link { background: none; border: none; padding: 0; color: var(--accent); font-weight: 600; }

.hint { color: var(--muted); font-size: 0.88rem; margin: -6px 0 12px; }
.error { color: var(--bad); font-size: 0.9rem; min-height: 1em; margin: 8px 0 0; }
.error:empty { display: none; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; }
.tabs button {
  background: none;
  border: none;
  border-bottom: 3px solid transparent;
  border-radius: 0;
  color: var(--muted);
  padding: 10px 14px;
}
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }
.badge {
  display: inline-block;
  min-width: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--bad);
  color: #fff;
  font-size: 0.75rem;
  text-align: center;
}

.summary { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.pill { padding: 4px 10px; border-radius: 999px; font-size: 0.85rem; font-weight: 600; }
.pill.ok { background: var(--ok-bg); color: var(--ok); }
.pill.warn { background: var(--warn-bg); color: var(--warn); }
.pill.bad { background: var(--bad-bg); color: var(--bad); }
.pill.neutral { background: var(--border); color: var(--text); }
.pill.frost { background: var(--frost-bg); color: var(--frost); }
.defrost {
  margin: 8px 0 0;
  padding: 6px 10px;
  border-radius: 8px;
  background: var(--frost-bg);
  color: var(--frost);
  font-size: 0.88rem;
  font-weight: 600;
}

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
.toolbar select, .toolbar input { width: auto; margin: 0; }
.toolbar input { flex: 1 1 180px; }

.food-list { display: grid; gap: 10px; }
.food {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 6px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
}
.food.ok { border-left-color: var(--ok); }
.food.warn { border-left-color: var(--warn); }
.food.bad { border-left-color: var(--bad); }
.food-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.food-head h3 { margin: 0; font-size: 1.05rem; overflow-wrap: anywhere; }
.food-meta { color: var(--muted); font-size: 0.88rem; margin: 4px 0 0; overflow-wrap: anywhere; }
.food-dates { display: flex; flex-wrap: wrap; gap: 4px 16px; font-size: 0.88rem; margin: 8px 0 0; }
.food-dates b { font-weight: 600; }
.food-notes { font-size: 0.9rem; margin: 6px 0 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.food-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.empty { color: var(--muted); text-align: center; padding: 32px 0; }

.message-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 60vh;
  overflow-y: auto;
  padding: 4px 2px 12px;
}
.message {
  max-width: 85%;
  align-self: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 12px;
}
.message.mine { align-self: flex-end; background: var(--ok-bg); }
.message-who { font-size: 0.8rem; color: var(--muted); margin: 0 0 2px; }
.message-body { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.message-form { display: flex; gap: 8px; align-items: flex-end; }
.message-form textarea { margin: 0; }

/* iPhone date fields have a wide built-in minimum size; let them fit their column. */
input[type="date"], input[type="datetime-local"] {
  -webkit-appearance: none;
  appearance: none;
  min-width: 0;
  min-height: 42px;
}
input[type="date"]::-webkit-date-and-time-value,
input[type="datetime-local"]::-webkit-date-and-time-value { text-align: left; }

/* Food form: header and Save/Cancel stay put, only the fields scroll. */
dialog {
  width: min(520px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  overflow: hidden;
}
dialog[open] { display: flex; flex-direction: column; }
dialog::backdrop { background: rgb(0 0 0 / 0.45); }
dialog form { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.sheet-header { padding: 18px 20px 8px; border-bottom: 1px solid var(--border); }
.sheet-header h2 { margin: 0 0 8px; }
.sheet-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 16px 20px 8px;
}
.sheet-footer { padding: 12px 20px 16px; border-top: 1px solid var(--border); background: var(--surface); }
.sheet-footer .error { margin: 0 0 8px; }
.sheet-footer .actions { margin: 0; }

/* Stop the page behind the form from scrolling while it's open. */
html.modal-open, html.modal-open body { overflow: hidden; overscroll-behavior: none; }

@media (max-width: 600px) {
  /* On phones the form fills the screen, with Save always visible. */
  dialog {
    width: 100%;
    max-width: none;
    height: 100dvh;
    max-height: none;
    margin: 0;
    border: 0;
    border-radius: 0;
  }
  .sheet-header { padding-top: calc(14px + env(safe-area-inset-top)); }
  .sheet-footer { padding-bottom: calc(14px + env(safe-area-inset-bottom)); }
  .sheet-footer .actions button { flex: 1; padding: 12px; }
}
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }

.banner {
  margin: 0 0 12px;
  padding: 10px 14px;
  border-radius: var(--radius);
  background: var(--warn-bg);
  color: var(--warn);
  font-size: 0.9rem;
  font-weight: 600;
}
.switch { margin: 16px 0 0; font-size: 0.9rem; color: var(--muted); }
.notice { color: var(--ok); font-size: 0.9rem; margin: 8px 0 0; }
.notice:empty { display: none; }

label.check { display: flex; gap: 8px; align-items: flex-start; font-weight: 400; }
label.check input { width: auto; margin: 3px 0 0; }

.section-title { font-size: 1rem; margin: 20px 0 8px; }
#invite-panel h2 { font-size: 1.05rem; }
.invite-result { border-top: 1px solid var(--border); margin-top: 16px; padding-top: 12px; }
.invite-result p { margin: 0 0 8px; }
.invite-code { font: 700 1.4rem/1.2 ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: 0.08em; }
.actions.start { justify-content: flex-start; }

.team-list { display: grid; gap: 8px; }
.person {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
}
.person-name { margin: 0; font-weight: 600; overflow-wrap: anywhere; }
.person-sub { margin: 2px 0 0; color: var(--muted); font-size: 0.85rem; overflow-wrap: anywhere; }
.person-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.person .pill { margin-left: 6px; font-size: 0.75rem; padding: 2px 8px; }

@media (max-width: 480px) {
  .tabs { gap: 0; }
  .tabs button { flex: 1 1 0; padding: 10px 4px; font-size: 0.92rem; white-space: nowrap; }
  .row { grid-template-columns: 1fr; gap: 0; }
  .toolbar input, #add-food-btn { flex: 1 1 100%; }
  .toolbar select { flex: 1 1 auto; }
}
