/* ===== Theme system: blue (default) / red / green / white =====
   Same theme system as golfshot and pdecker.com: a class on <html>, persisted
   in localStorage under "theme". Every color is a CSS variable. */
:root, html.blue {
  --bg: #0f1720;
  --panel: #16212e;
  --line: #24313f;
  --text: #e6edf3;
  --muted: #93a1b0;
  --accent: #3fb950;
  --accent-2: #58a6ff;
  --raised: #111c27;
  --hover: #1d2a38;
  --selected: #23384a;
}
html.red {
  --bg: #501212;
  --panel: #7d2a2a;
  --line: rgba(255, 255, 255, 0.14);
  --text: #ffffff;
  --muted: rgba(255, 255, 255, 0.7);
  --accent: #ffaaaa;
  --accent-2: #ffd0d0;
  --raised: #6a2020;
  --hover: #8f3232;
  --selected: #9c3c3c;
}
html.green {
  --bg: #0b3a14;
  --panel: #1e5f2e;
  --line: rgba(255, 255, 255, 0.14);
  --text: #ffffff;
  --muted: rgba(255, 255, 255, 0.7);
  --accent: #aaffaa;
  --accent-2: #b6f2c4;
  --raised: #164a22;
  --hover: #256b37;
  --selected: #2e7a42;
}
html.white {
  --bg: #eceff1;
  --panel: #ffffff;
  --line: #cccccc;
  --text: #1a1a1a;
  --muted: rgba(0, 0, 0, 0.62);
  --accent: #2f7d3a;
  --accent-2: #0645ad;
  --raised: #eef2f6;
  --hover: #f2f6fb;
  --selected: #dbe6f2;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.4;
}

.muted { color: var(--muted); }
main { padding: 1rem 2rem 3rem; }

input, select, button {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.4rem 0.6rem;
  font-size: 0.9rem;
}
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent-2); outline-offset: 2px; }

/* ---- Sticky top bar ---- */
.sticky-top {
  position: sticky;
  top: 0;
  z-index: 30;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  padding: 0.75rem 2rem;
}
.topbar h1 { margin: 0; }
.topbar .title p { margin: 0.15rem 0 0; }
.tools { display: flex; gap: 0.75rem; align-items: center; flex-wrap: wrap; }
.filters { display: flex; gap: 0.75rem; align-items: center; flex-wrap: wrap; }
.filters label { display: inline-flex; gap: 0.35rem; align-items: center; color: var(--muted); font-size: 0.9rem; }
.filters .date-input { width: 8.5rem; cursor: pointer; }
.filters .toggle:disabled { opacity: 0.4; cursor: default; }

.theme-btn, .toggle {
  border: 1px solid var(--accent-2);
  border-radius: 8px;
  font-weight: 600;
}
.theme-btn:hover, .toggle:hover { background: var(--hover); }

/* ---- Tables ---- */
table {
  width: 100%;
  border-collapse: separate; /* required for sticky headers */
  border-spacing: 0;
  background: var(--panel);
  border-radius: 8px;
}
th, td {
  padding: 0.45rem 0.7rem;
  text-align: left;
  border-bottom: 1px solid var(--line);
  font-size: 0.9rem;
}
th { color: var(--muted); font-weight: 600; }
.num { text-align: right; font-variant-numeric: tabular-nums; }

.days thead th {
  position: sticky;
  top: var(--thead-top, 0px);
  background: var(--raised);
  z-index: 20;
}
tr.day-row { cursor: pointer; }
tr.day-row:hover, tr.day-row:focus { background: var(--hover); }
tr.day-row.empty-day td { color: var(--muted); }
tr.selected { background: var(--selected) !important; }

th.sortable { cursor: pointer; user-select: none; white-space: nowrap; }
th.sortable:hover { color: var(--text); }
th .arrow { font-size: 0.75rem; color: var(--accent-2); }
th.sort-asc .arrow::after { content: "▲"; }
th.sort-desc .arrow::after { content: "▼"; }

.empty { color: var(--muted); text-align: center; font-style: italic; }

/* ---- Day detail ---- */
.detail h2 { margin: 0 2rem 0.25rem 0; }
.table-scroll { overflow-x: auto; }
table.diary { margin-top: 1rem; }
table.diary caption {
  text-align: left;
  font-weight: 700;
  padding: 0.25rem 0;
  color: var(--accent-2);
}
table.diary tr.meal-row th { color: var(--accent); background: var(--raised); }
table.diary tr.subtotal td { color: var(--muted); font-style: italic; }
table.diary tr.agg td { font-weight: 700; background: var(--raised); }
table.diary tr.goal td { color: var(--muted); }

/* ---- Modals ---- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  z-index: 100;
  padding: 4vh 1rem;
}
.modal-overlay[hidden] { display: none; }
.modal-dialog {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
  padding: 1.25rem 1.5rem;
  width: min(1000px, 100%);
  max-height: 92vh;
  overflow: auto;
}
.modal-close {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  padding: 0.2rem 0.5rem;
  line-height: 1;
}
.modal-close:hover { background: #b03636; color: #fff; border-color: #b03636; }

@media (max-width: 768px) {
  main { padding: 0.5rem; }
  .topbar { padding: 0.5rem; }
  .modal-dialog { padding: 1rem 0.75rem; }
  .days { overflow-x: auto; }
}

/* ---- Top foods, grouped by meal ---- */
.meal-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 0.25rem 0 0.5rem;
}
.meal-nav a {
  color: var(--accent-2);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0.2rem 0.75rem;
  text-decoration: none;
  font-size: 0.85rem;
}
.meal-nav a:hover { background: var(--hover); }
.meal-foods { margin-top: 1.25rem; scroll-margin-top: 1rem; }
.meal-foods h3 { margin: 0 0 0.25rem; color: var(--accent); }
.meal-foods table.diary { margin-top: 0.25rem; }

/* ---- Where to eat? ---- */
.suggest-dialog { width: min(640px, 100%); }
.suggest-dialog h2 { margin: 0 2rem 0.75rem 0; }
.suggest-form { display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: center; }
.suggest-form label { display: inline-flex; gap: 0.35rem; align-items: center; color: var(--muted); }
.suggest-form button { border-color: var(--accent); }
.suggestions { list-style: none; padding: 0; margin: 0.75rem 0; display: grid; gap: 0.6rem; }
.suggestion {
  background: var(--raised);
  border: 1px solid var(--line);
  border-left: 4px solid var(--accent);
  border-radius: 8px;
  padding: 0.6rem 0.8rem;
}
.suggestion-name { font-size: 1.1rem; font-weight: 700; color: var(--accent-2); }
.badge {
  font-size: 0.7rem;
  font-weight: 600;
  padding: 0.05rem 0.4rem;
  border-radius: 999px;
  border: 1px solid var(--muted);
  color: var(--muted);
  vertical-align: middle;
}
.suggest-actions { display: flex; justify-content: flex-end; }

/* ---- ChatMFP ---- */
.chat-dialog { width: min(680px, 100%); }
.chat-dialog h2 { margin: 0 2rem 0.25rem 0; }
.chat-form { display: flex; gap: 0.6rem; align-items: flex-end; margin-top: 0.75rem; }
.chat-form textarea {
  flex: 1;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.5rem 0.6rem;
  font: inherit;
  font-size: 0.9rem;
  resize: vertical;
  min-height: 2.6rem;
}
.chat-form button {
  border-color: var(--accent);
  font-weight: 600;
  white-space: nowrap;
}
.chat-form button:disabled { opacity: 0.6; cursor: default; }

.chat-progress { margin-top: 1rem; }
.chat-bar {
  height: 8px;
  background: var(--raised);
  border: 1px solid var(--line);
  border-radius: 999px;
  overflow: hidden;
}
.chat-bar-fill {
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 0.3s ease;
}
.chat-progress p { margin: 0.4rem 0 0; }

.chat-result { margin-top: 1rem; }
.chat-answer {
  background: var(--raised);
  border: 1px solid var(--line);
  border-left: 4px solid var(--accent);
  border-radius: 8px;
  padding: 0.75rem 0.9rem;
  font-size: 1rem;
  line-height: 1.5;
}
.chat-meta { margin: 0.5rem 0 0; }
.chat-sql { margin-top: 0.75rem; }
.chat-sql summary { cursor: pointer; color: var(--accent-2); font-size: 0.85rem; }
.chat-sql pre {
  background: var(--raised);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.6rem 0.8rem;
  overflow-x: auto;
  font-size: 0.85rem;
  margin: 0.5rem 0 0;
  white-space: pre-wrap;
}
.chat-error, .chat-timeout {
  background: var(--raised);
  border: 1px solid var(--line);
  border-left: 4px solid #b03636;
  border-radius: 8px;
  padding: 0.75rem 0.9rem;
}
.chat-timeout { border-left-color: var(--accent-2); }
.chat-retry { margin-left: 0.5rem; padding: 0.15rem 0.6rem; font-size: 0.85rem; }
