:root {
  --bg: #eaf2ff;
  --surface: #ffffff;
  --surface-2: #dbe8ff;
  --text: #0a1a35;
  --muted: #47597c;
  --border: #bfd3f7;
  --accent: #1a6dff;
  --accent-strong: #0b53d6;
  --accent-text: #ffffff;
  --danger: #d62f2f;
  --warn: #f08a1c;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(20, 70, 160, 0.08), 0 4px 14px rgba(20, 70, 160, 0.08);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0a1226;
    --surface: #121c36;
    --surface-2: #1b2a4d;
    --text: #eaf1ff;
    --muted: #a0b3d6;
    --border: #2b3d68;
    --accent: #4d94ff;
    --accent-strong: #7db3ff;
    --accent-text: #04122b;
    --danger: #ff7a70;
    --warn: #ffb04d;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; font-size: 17px; }
@media (max-width: 640px) { html { font-size: 19px; } }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 1rem/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
p { margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }
.loading { padding: 3rem 1rem; text-align: center; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: 0.9rem; }
.hidden { display: none !important; }

/* ---------- layout ---------- */
.shell { max-width: 720px; margin: 0 auto; padding: 0 16px 96px; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 0;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 1.32rem; letter-spacing: -0.02em; }
.logo {
  width: 32px; height: 32px; border-radius: 9px; background: var(--accent); color: var(--accent-text);
  display: grid; place-items: center; font-weight: 800;
}
.whoami { display: flex; align-items: center; gap: 10px; font-size: 1rem; color: var(--muted); }
.whoami .btn.small { font-size: 0.95rem; }
.nav { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 12px; margin-bottom: 12px; }
.nav button {
  flex: 1; border: 0; background: transparent; padding: 10px 4px; border-radius: 9px; cursor: pointer;
  color: var(--muted); font-weight: 600; font-size: 0.85rem; white-space: nowrap;
}
.nav button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

@media (max-width: 640px) {
  .nav {
    position: fixed; left: 0; right: 0; bottom: 0; margin: 0; border-radius: 0; z-index: 5;
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    background: var(--surface); border-top: 1px solid var(--border); gap: 2px;
  }
  /* six tabs for admins must fit a phone: tabs size to their label ("Food History" is the widest) */
  .nav button { flex: 1 1 auto; min-width: 0; padding: 11px 2px; font-size: 0.653rem; }
  .nav button.active { background: var(--surface-2); box-shadow: none; color: var(--accent-strong); }
  .whoami .who-name { display: none; }
}
@media (max-width: 360px) { .nav button { font-size: 0.59rem; } } /* very small phones */

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; box-shadow: var(--shadow); margin-bottom: 12px;
}
.card > h2 { margin-bottom: 10px; }
.row { display: flex; align-items: center; gap: 10px; }
.row.between { justify-content: space-between; }
.row.wrap { flex-wrap: wrap; }
.stack { display: grid; gap: 10px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.mt { margin-top: 12px; }
.mb { margin-bottom: 12px; }

/* ---------- forms & buttons ---------- */
label.field { display: grid; gap: 6px; font-size: 0.95rem; font-weight: 600; color: var(--muted); }
input, select {
  width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); min-height: 48px;
}
input:focus, select:focus, button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent);
}
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 48px; padding: 8px 16px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; font-weight: 600; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }
.btn.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn.danger.solid { background: var(--danger); color: #fff; }
.btn.small { min-height: 40px; padding: 5px 12px; font-size: 0.9rem; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn[disabled] { opacity: 0.6; cursor: default; }
.icon-btn {
  width: 40px; height: 40px; border: 0; border-radius: 10px; background: transparent; cursor: pointer;
  color: var(--muted); font-size: 1.15rem; display: grid; place-items: center;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.error { color: var(--danger); font-size: 0.95rem; min-height: 1.2em; }
.notice {
  background: var(--surface-2); border-left: 4px solid var(--accent); padding: 10px 12px;
  border-radius: 8px; font-size: 0.95rem;
}

/* ---------- auth screens ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.auth .card { width: 100%; max-width: 380px; padding: 24px; }
.auth .brand { justify-content: center; margin-bottom: 6px; font-size: 1.6rem; }
.auth .sub { text-align: center; color: var(--muted); margin-bottom: 18px; }
.auth .sub-gap { height: 18px; }

/* ---------- log view ---------- */
.datebar { display: flex; align-items: center; gap: 6px; margin-bottom: 12px; }
.datebar .date-label { flex: 1; text-align: center; font-weight: 700; font-size: 1.1rem; }
.datebar .date-label small { display: block; font-weight: 500; color: var(--muted); font-size: 0.85rem; }
.datebar input[type="date"] { width: 0; height: 0; min-height: 0; padding: 0; border: 0; opacity: 0; position: absolute; }

.cal-total { display: flex; align-items: baseline; gap: 8px; margin-bottom: 8px; }
.cal-total .muted { white-space: nowrap; }
.btn.block { width: 100%; margin-top: 12px; }
.cal-total .big { font-size: 2.6rem; font-weight: 800; letter-spacing: -0.03em; }
.bar { height: 10px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--accent); border-radius: 999px; transition: width 0.3s; }
.bar.over > span { background: var(--warn); }

.meal { margin-bottom: 8px; padding: 8px 14px; }
.meal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 2px; }
.meal-head .meal-name { font-weight: 700; text-transform: capitalize; }
.meal-head .meal-cal { color: var(--muted); font-size: 0.95rem; margin-left: 8px; font-weight: 500; }
.entry {
  display: flex; align-items: center; gap: 8px; padding: 1px 0; border-top: 1px solid var(--border);
}
.entry:first-child { border-top: 0; }
.entry .e-main { flex: 1; min-width: 0; }
.entry .e-name { font-weight: 600; overflow-wrap: anywhere; }
.entry .e-cal { font-weight: 700; font-variant-numeric: tabular-nums; }
.empty { color: var(--muted); font-size: 0.95rem; padding: 6px 0; }
.meal .empty { padding: 2px 0; }
.entry .icon-btn { width: 36px; height: 36px; }

/* ---------- history ---------- */
@media (max-width: 640px) { .row.between > h2 { font-size: 1rem; } } /* keeps "Food History" on one line next to the 7/30 days switch */
.seg { display: inline-flex; flex: none; background: var(--surface-2); border-radius: 10px; padding: 3px; }
.seg button { border: 0; background: transparent; padding: 6px 14px; border-radius: 8px; cursor: pointer; font-weight: 600; color: var(--muted); white-space: nowrap; }
.seg button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-bottom: 12px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; }
.stat .v { font-size: 1.4rem; font-weight: 800; letter-spacing: -0.02em; }
.stat .l { font-size: 0.85rem; color: var(--muted); }
.day-row {
  display: grid; grid-template-columns: 105px 1fr; align-items: start; gap: 10px;
  width: 100%; padding: 10px 4px; border: 0; border-top: 1px solid var(--border); background: transparent;
  cursor: pointer; text-align: left; border-radius: 0;
}
.day-row:first-child { border-top: 0; }
.day-row:hover { background: var(--surface-2); }
.day-row .d-label { font-size: 0.9rem; font-weight: 600; }
.day-row.none .d-label, .day-row.none .d-foods { color: var(--muted); font-weight: 500; }
.day-row .d-foods { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; overflow-wrap: anywhere; }

/* ---------- notes ---------- */
.subtabs { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 12px; margin-bottom: 12px; }
.subtabs button {
  flex: 1; min-width: 0; border: 0; background: transparent; padding: 10px 4px; border-radius: 9px; cursor: pointer;
  color: var(--muted); font-weight: 600; font-size: 0.8rem; white-space: nowrap; position: relative;
  overflow: hidden; text-overflow: ellipsis; /* long custom names are cut short here; the full name is in the field below */
}
@media (max-width: 640px) { .subtabs button { font-size: 0.72rem; padding: 10px 2px; } }
.subtabs button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.subtabs button.has-text::after {
  content: ''; position: absolute; top: 6px; right: 6px; width: 7px; height: 7px; border-radius: 50%; background: var(--accent);
}
.note-card { display: grid; gap: 8px; }
.note-text {
  width: 100%; min-height: 55vh; padding: 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); resize: vertical; line-height: 1.5;
}
.note-text:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }

/* ---------- to do ---------- */
.todo-add { display: flex; gap: 8px; margin-bottom: 8px; }
.todo-add input { flex: 1; min-width: 0; }
.todo { display: flex; align-items: center; gap: 6px; padding: 7px 0; border-top: 1px solid var(--border); }
.todos > .todo:first-child { border-top: 0; }
.todo .todo-text { flex: 1; min-width: 0; font-weight: 600; overflow-wrap: anywhere; }
.todo.done .todo-text { text-decoration: line-through; color: var(--muted); font-weight: 500; }
.todo.done .btn { color: var(--muted); }
.todo > .btn { padding: 6px 12px; }
.todo > .icon-btn { width: 36px; flex: none; }
.todo.editing { flex-wrap: wrap; }
.todo.editing input { flex: 1 1 100%; }
.todo-list-pick { width: auto; flex: none; padding: 8px 6px; }
.todo.editing .todo-list-pick { min-height: 40px; }
.todo .badge { margin-left: 8px; }

/* ---------- admin ---------- */
.user-row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-top: 1px solid var(--border); flex-wrap: wrap; }
.user-row:first-child { border-top: 0; }
.user-row .u-main { flex: 1; min-width: 160px; }
.user-row .u-name { font-weight: 700; }
.user-row .u-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.badge {
  display: inline-block; font-size: 0.72rem; font-weight: 700; padding: 3px 9px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); margin-left: 6px; vertical-align: middle; text-transform: uppercase; letter-spacing: 0.04em;
}
.badge.admin { background: var(--accent); color: var(--accent-text); }
.badge.warn { background: var(--warn); color: #201200; }

/* ---------- dialog ---------- */
dialog {
  border: 1px solid var(--border); border-radius: 18px; background: var(--surface); color: var(--text);
  padding: 0; width: min(440px, calc(100vw - 24px)); box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.5); }
dialog form { padding: 18px; display: grid; gap: 12px; max-height: calc(100vh - 40px); overflow-y: auto; }
dialog .actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 4px; }
.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; }
.chip {
  flex: none; border: 1px solid var(--border); background: var(--surface-2); border-radius: 999px;
  padding: 8px 14px; font-size: 0.9rem; cursor: pointer; white-space: nowrap; max-width: 220px; overflow: hidden; text-overflow: ellipsis;
}
.chip:hover { border-color: var(--accent); }
.pw-row { display: flex; gap: 8px; }
.pw-row input { flex: 1; }

#toast {
  position: fixed; left: 50%; bottom: 84px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 0.9rem; z-index: 20;
  transition: opacity 0.2s, transform 0.2s; max-width: calc(100vw - 32px);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
@media (min-width: 641px) { #toast { bottom: 28px; } }

.datebar > .btn.ghost:not(.date-label) { font-size: 1.6rem; padding: 0 12px; }

body { overflow-x: hidden; }

/* ---------- exercise ---------- */
.ex-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 6px; }
@media (max-width: 640px) { .ex-grid { grid-template-columns: 1fr 1fr; } }
.ex-check {
  display: flex; align-items: center; gap: 10px; min-height: 48px; padding: 6px 10px;
  border: 1px solid var(--border); border-radius: 10px; background: var(--bg); cursor: pointer; font-weight: 600;
}
.ex-check input { width: 22px; height: 22px; min-height: 0; flex: none; margin: 0; accent-color: var(--accent); }
.ex-check:has(input:checked) { background: var(--surface-2); border-color: var(--accent); }

.ex-note { min-height: 7rem; }
.day-row .d-note { display: block; color: var(--muted); font-weight: 400; font-style: italic; }
.day-row.current { background: var(--surface-2); }

/* ---------- food history (under the Food Log) ---------- */
.food-history { margin-top: 20px; }

/* ---------- calendar ---------- */
.cal-card { padding: 8px; }
.cal-grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 4px; }
.cal-wd { text-align: center; font-size: 0.75rem; font-weight: 700; color: var(--muted); padding: 2px 0 4px; }
.cal-cell {
  min-height: 64px; min-width: 0; padding: 4px 5px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg); text-align: left; cursor: pointer; display: flex; flex-direction: column; gap: 2px; overflow: hidden;
}
.cal-cell.blank { border: 0; background: transparent; cursor: default; }
.cal-cell:hover { border-color: var(--accent); }
.cal-cell .cal-num { font-weight: 700; font-size: 0.85rem; }
.cal-cell.today .cal-num {
  background: var(--accent); color: var(--accent-text); border-radius: 999px; padding: 0 6px; align-self: flex-start;
}
.cal-cell.selected { border: 2px solid var(--accent); background: var(--surface-2); padding: 3px 4px; }
.cal-cell .cal-snip {
  font-size: 0.7rem; line-height: 1.25; color: var(--muted); overflow-wrap: anywhere;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
}
@media (max-width: 640px) {
  /* no room for text on a phone: a dot marks days with a note */
  .cal-cell { min-height: 46px; align-items: center; }
  .cal-cell .cal-snip { display: none; }
  .cal-cell.has-note::after { content: ''; width: 6px; height: 6px; border-radius: 50%; background: var(--accent); }
  .cal-cell.today .cal-num { align-self: center; }
}
.cal-note { min-height: 9rem; }
