:root {
  color-scheme: light;
  --bg: #f4f5f7; --panel: #fff; --panel-2: #eceef2; --line: #d9dce2;
  --text: #1c1e22; --muted: #666c78; --accent: #2a6df4; --accent-2: #1f55c2;
  --ok: #2e7d32; --warn: #a15c00; --err: #c62828; --rec: #d32f2f;
  --user-bubble: #dbe7ff; --bot-bubble: #fff;
  --shadow: 0 8px 30px rgba(20, 24, 33, .18);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 16px/1.4 -apple-system, system-ui, sans-serif; -webkit-text-size-adjust: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.muted { color: var(--muted); } .small { font-size: 13px; } .center { text-align: center; } .grow { flex: 1; }
.error { color: var(--err); min-height: 1.2em; }

#app { height: 100%; display: flex; flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left); }
.view { flex: 1; min-height: 0; }

button.primary { background: var(--accent); border: 0; border-radius: 10px; padding: 8px 14px; color: #fff; font-weight: 600; }
button.primary:active { background: var(--accent-2); }
button.secondary { background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 8px 14px; }
button.big { padding: 14px 22px; font-size: 18px; width: 100%; }
button.link { background: none; border: 0; color: var(--accent); padding: 4px 8px; }
button.icon { width: 44px; height: 44px; border-radius: 22px; border: 0; background: var(--panel-2); font-size: 20px; flex: none; }
button.icon.primary { background: var(--accent); padding: 0; }
button:disabled { opacity: .45; }

/* login */
#view-login { display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.login-box { width: min(380px, 100%); display: flex; flex-direction: column; gap: 12px; text-align: center; }
.login-box h1 { margin: 0; font-size: 32px; }
.problems { text-align: left; font-size: 14px; border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; }
.problems p { margin: 0 0 4px; }
.problems ul { margin: 0; padding-left: 18px; }
.login-box select { width: 100%; padding: 12px; border-radius: 10px; background: var(--panel); border: 1px solid var(--line); margin-bottom: 8px; }

/* profile form */
#view-profile { display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.profile-fields { display: flex; flex-direction: column; gap: 12px; text-align: left; }
.profile-fields label { display: flex; flex-direction: column; gap: 4px; font-weight: 600; }
.profile-fields .input-row { display: flex; align-items: center; gap: 8px; }
.profile-fields input { flex: 1; min-width: 0; padding: 12px; border-radius: 10px; background: var(--panel);
  border: 1px solid var(--line); font-weight: 400; font-size: 18px; }
.profile-fields input:focus-visible { outline: 2px solid var(--accent); }
.profile-fields .field-error { color: var(--err); font-weight: 400; font-size: 13px; min-height: 1.2em; }

/* top bar */
#topbar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--line); }
.tabs { display: flex; gap: 4px; flex: 1; }
.tab { background: none; border: 0; padding: 8px 12px; border-radius: 8px; color: var(--muted); font-weight: 600; }
.tab.active { background: var(--panel-2); color: var(--text); }

/* chat */
#view-chat { display: flex; flex-direction: column; }
/* On a wide screen the chat is a centred column, not the whole window (the data view stays wide). */
@media (min-width: 900px) {
  #view-chat { width: 100%; max-width: 720px; margin: 0 auto; border-left: 1px solid var(--line); border-right: 1px solid var(--line); }
}
#messages { flex: 1; overflow-y: auto; padding: 12px; -webkit-overflow-scrolling: touch; overflow-anchor: none; }
#load-older { display: block; margin: 0 auto 8px; }
.msg { display: flex; flex-direction: column; margin: 6px 0; max-width: 85%; }
.msg.user { margin-left: auto; align-items: flex-end; }
.msg.assistant { margin-right: auto; align-items: flex-start; }
.bubble { padding: 8px 12px; border-radius: 16px; white-space: pre-wrap; word-wrap: break-word; }
.msg.user .bubble { background: var(--user-bubble); border-bottom-right-radius: 4px; }
.msg.assistant .bubble { background: var(--bot-bubble); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.meta { font-size: 11px; color: var(--muted); margin: 2px 6px; display: flex; gap: 8px; align-items: center; }
.meta button { font-size: 11px; padding: 0; }
.msg .status { font-size: 13px; color: var(--muted); margin: 4px 6px; }
.msg .status.err { color: var(--err); }
.msg audio { height: 32px; margin-top: 4px; max-width: 240px; }
.changes { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 2px 0; }
.change { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 3px 10px; font-size: 13px; }
.change.incomplete { border-color: var(--warn); }
.change.delete { text-decoration: line-through; color: var(--muted); }
.typing::after { content: "…"; animation: blink 1s steps(3) infinite; }
@keyframes blink { 50% { opacity: .3; } }

.notice { margin: 0 12px 8px; padding: 10px 12px; background: var(--panel); border: 1px solid var(--warn); border-radius: 12px;
  display: flex; align-items: center; gap: 10px; justify-content: space-between; }

#composer, #recbar { display: flex; align-items: flex-end; gap: 8px; padding: 8px 12px max(8px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line); background: var(--bg); }
#input { flex: 1; resize: none; max-height: 140px; padding: 11px 14px; border-radius: 22px; background: var(--panel);
  border: 1px solid var(--line); line-height: 1.3; font-size: 16px; }
#input:disabled { opacity: .5; }
#recbar { align-items: center; padding-bottom: 8px; background: rgba(211, 47, 47, .12); }
.rec-dot { width: 14px; height: 14px; border-radius: 7px; background: var(--rec); animation: pulse 1.2s ease-in-out infinite; }
#rec-timer { font-variant-numeric: tabular-nums; font-size: 18px; }
@keyframes pulse { 50% { opacity: .35; transform: scale(.85); } }

/* data */
#view-data, #view-rules { overflow-y: auto; padding: 12px; padding-bottom: max(24px, env(safe-area-inset-bottom)); }
.subtabs { display: flex; gap: 4px; overflow-x: auto; margin-bottom: 10px; }
.subtab { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; white-space: nowrap; }
.subtab.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.filters { display: flex; gap: 8px 10px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.filter-group { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 4px 12px; font-size: 14px; }
.chip.active { border-color: var(--accent); color: #fff; background: var(--accent-2); }
.date-field { display: flex; align-items: center; gap: 6px; font-size: 14px; color: var(--muted); }
.date-field input, #data-search { padding: 6px 10px; border-radius: 10px; background: var(--panel); border: 1px solid var(--line); }
#data-search { flex: 1; min-width: 200px; }
.day { margin-bottom: 16px; }
.day h3 { font-size: 14px; color: var(--muted); margin: 0 0 6px; }
.day h3::first-letter { text-transform: uppercase; }
/* One record = one compact row: when | kind | products on one line, separated by ";" | actions */
.meal-row { display: grid; gap: 2px 12px; grid-template-columns: auto 1fr auto; align-items: baseline; background: var(--panel);
  border: 1px solid var(--line); border-radius: 8px; padding: 5px 10px; margin-bottom: 4px; cursor: pointer; }
.meal-row:hover { background: var(--panel-2); }
.meal-row.incomplete { border-color: var(--warn); }
.meal-row .when { font-variant-numeric: tabular-nums; font-weight: 600; }
.meal-row .items { grid-column: 1 / -1; font-size: 14px; line-height: 1.35; }
.meal-row .item .detail, .meal-row .sep { color: var(--muted); }
.meal-row .item .detail { font-size: 13px; }
.meal-row .note { color: var(--muted); font-size: 13px; }
.meal-row .items .badge { display: inline-block; margin-left: 4px; }
.meal-row .row-actions { display: flex; gap: 4px; align-self: center; grid-column: 3; grid-row: 1; }
.meal-row .row-actions button { padding: 1px 8px; font-size: 13px; }
.meal-row.sleep-row .when { white-space: nowrap; }
@media (min-width: 900px) {
  .meal-row.sleep-row { grid-template-columns: 108px 140px minmax(0, 1fr) auto; }
  .meal-row { grid-template-columns: 52px 140px minmax(0, 1fr) auto; }
  .meal-row .items { grid-column: auto; }
  .meal-row .row-actions { grid-column: auto; grid-row: auto; opacity: .35; }
  .meal-row:hover .row-actions { opacity: 1; }
}
button.danger { color: var(--err); }
.prep { color: var(--muted); font-size: 13px; }
.badge { font-size: 12px; padding: 1px 8px; border-radius: 10px; background: var(--panel-2); color: var(--muted); }
.badge.warn { background: rgba(240, 180, 41, .22); color: var(--warn); }
table.products { width: 100%; border-collapse: collapse; font-size: 14px; }
table.products th, table.products td { text-align: left; padding: 4px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.products th { color: var(--muted); font-weight: 500; font-size: 12px; }
table.products tr { cursor: pointer; }
.empty { color: var(--muted); text-align: center; padding: 32px 0; }

/* editor dialog */
dialog#editor { width: min(1100px, calc(100% - 24px)); max-height: 92vh; background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: 16px; padding: 0; box-shadow: var(--shadow); }
#editor-form { padding: 0 16px 16px; }
#editor-body h4 { margin: 16px 0 6px; font-size: 14px; color: var(--muted); }
.editor-top { display: flex; gap: 10px; flex-wrap: wrap; }
.field { display: flex; flex-direction: column; gap: 2px; font-size: 12px; color: var(--muted); }
.field.grow { flex: 1; min-width: 200px; }
.field input, .field select, .item-edit input, .item-edit select { background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 8px; padding: 7px 8px; min-width: 0; width: 100%; }
.items-edit { display: flex; flex-direction: column; gap: 6px; margin-bottom: 10px; }
.item-edit { display: grid; gap: 6px; align-items: center;
  grid-template-columns: minmax(0, 2.2fr) minmax(0, .7fr) minmax(0, .9fr) minmax(0, 1.2fr) minmax(0, 2.4fr) minmax(0, 1.4fr) 44px; }
.item-edit.head { font-size: 12px; color: var(--muted); }
.item-edit button.icon { width: 36px; height: 36px; font-size: 16px; }
.editor-actions { display: flex; gap: 8px; align-items: center; padding: 8px 16px 16px; flex-wrap: wrap; }
#editor-form .editor-actions { padding: 8px 0 0; }
@media (max-width: 899px) {
  .item-edit { grid-template-columns: 1fr 1fr; padding: 8px; border: 1px solid var(--line); border-radius: 10px; }
  .item-edit.head { display: none; }
  .item-edit > :first-child, .item-edit > :nth-child(5), .item-edit > :nth-child(6) { grid-column: 1 / -1; }
  .item-edit button.icon { justify-self: end; grid-column: 1 / -1; }
}

/* rules */
.rule { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 10px; margin-bottom: 10px; }
.rule textarea { width: 100%; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 8px; resize: vertical; }
.rule .row { display: flex; gap: 8px; align-items: center; margin-top: 8px; flex-wrap: wrap; }
.rule select { background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 6px; }
.rule.inactive textarea { opacity: .5; }

/* detail dialog */
dialog#detail { width: min(560px, calc(100% - 24px)); max-height: 85vh; background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: 16px; padding: 0; box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(20, 24, 33, .4); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 12px 4px 16px; position: sticky; top: 0; background: var(--panel); }
.dialog-head h2 { margin: 0; font-size: 18px; }
#detail-body { padding: 0 16px 16px; }
#detail-body h4 { margin: 16px 0 6px; font-size: 14px; color: var(--muted); }
.rev { border-left: 3px solid var(--line); padding: 4px 0 8px 10px; margin-bottom: 8px; }
.rev.create { border-color: var(--ok); } .rev.update { border-color: var(--accent); } .rev.delete { border-color: var(--err); }
.rev .when { font-size: 12px; color: var(--muted); }
.rev .quote { font-size: 14px; color: var(--text); margin: 4px 0; font-style: italic; }
.rev ul { margin: 4px 0 0; padding-left: 18px; font-size: 14px; }
.diff-old { color: var(--err); text-decoration: line-through; }
.diff-new { color: var(--ok); }

/* keep the version badge (public/version.js) clear of the message composer */
#app-version { top: calc(env(safe-area-inset-top) + 54px) !important; bottom: auto !important; opacity: .55; }
