:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #f0efea;
  --border: #e2e0d8;
  --text: #1d1f22;
  --muted: #6b6e73;
  --accent: #2f7d5b;
  --accent-soft: #e2f1e9;
  --warn: #b7791f;
  --warn-soft: #fbf0dc;
  --danger: #c0392b;
  --danger-soft: #fbe5e2;
  --info: #2c6fb3;
  --info-soft: #e3eefa;
  --kcal: #e07a3f;
  --prot: #3a86c8;
  --fat: #d8a51d;
  --carb: #6aa84f;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .05), 0 2px 8px rgba(0, 0, 0, .04);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16181b;
    --surface: #1f2226;
    --surface-2: #272b30;
    --border: #33383e;
    --text: #e8e9ea;
    --muted: #9a9fa6;
    --accent: #4fb386;
    --accent-soft: #1f3a2e;
    --warn: #e0a84a;
    --warn-soft: #3a2f1c;
    --danger: #e5675a;
    --danger-soft: #3d2320;
    --info: #6aa6e6;
    --info-soft: #1e2d3e;
    --shadow: none;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #16181b;
  --surface: #1f2226;
  --surface-2: #272b30;
  --border: #33383e;
  --text: #e8e9ea;
  --muted: #9a9fa6;
  --accent: #4fb386;
  --accent-soft: #1f3a2e;
  --warn: #e0a84a;
  --warn-soft: #3a2f1c;
  --danger: #e5675a;
  --danger-soft: #3d2320;
  --info: #6aa6e6;
  --info-soft: #1e2d3e;
  --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
.boot { padding: 40px; color: var(--muted); }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 22px; font-weight: 650; }
h2 { font-size: 17px; font-weight: 620; }
h3 { font-size: 15px; font-weight: 600; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.tiny { font-size: 12px; }
.num { font-variant-numeric: tabular-nums; }
.nowrap { white-space: nowrap; }
.grow { flex: 1; min-width: 0; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.tight { gap: 4px; }
.row.between { justify-content: space-between; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.stack.tight { gap: 6px; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- Layout ---------- */
.layout { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.sidebar {
  position: sticky; top: 0; height: 100vh;
  border-right: 1px solid var(--border);
  background: var(--surface);
  padding: 18px 12px;
  display: flex; flex-direction: column; gap: 4px;
}
.brand { font-weight: 700; font-size: 17px; padding: 4px 10px 16px; }
.nav-link {
  display: flex; gap: 10px; align-items: center;
  padding: 9px 10px; border-radius: 9px;
  color: var(--text); text-decoration: none;
}
.nav-link:hover { background: var(--surface-2); }
.nav-link.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-icon { width: 20px; text-align: center; }
.sidebar-foot { margin-top: auto; padding: 10px; border-top: 1px solid var(--border); }
.main { padding: 22px 28px 60px; min-width: 0; }
.page-head { display: flex; gap: 12px; align-items: center; justify-content: space-between; flex-wrap: wrap; margin-bottom: 18px; }

.tabbar { display: none; }

@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .main { padding: 16px 16px 96px; }
  .tabbar {
    display: flex; position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
    background: var(--surface); border-top: 1px solid var(--border);
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  }
  .tabbar a {
    flex: 1; min-width: 0; text-align: center; text-decoration: none; color: var(--muted);
    font-size: 10px; padding: 4px 0; border-radius: 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }
  .tabbar a .nav-icon { display: block; font-size: 19px; width: auto; }
  .tabbar a.active { color: var(--accent); font-weight: 600; }
}

/* ---------- Controls ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px;
}
.card.flush { padding: 0; }
button, .btn {
  font: inherit; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 9px; padding: 7px 12px; line-height: 1.2;
  display: inline-flex; gap: 6px; align-items: center; justify-content: center;
  text-decoration: none;
}
button:hover:not(:disabled) { background: var(--surface-2); }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover:not(:disabled) { filter: brightness(1.07); background: var(--accent); }
button.danger { color: var(--danger); }
button.danger.solid { background: var(--danger); border-color: var(--danger); color: #fff; }
button.ghost { border-color: transparent; background: transparent; }
button.ghost:hover:not(:disabled) { background: var(--surface-2); }
button.icon { padding: 5px 7px; min-width: 30px; }
button.sm { padding: 4px 9px; font-size: 13px; }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 9px;
  padding: 8px 10px; width: 100%; min-width: 0;
}
input:focus, select:focus, textarea:focus, button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
input[type="checkbox"] { width: auto; accent-color: var(--accent); }
textarea { resize: vertical; min-height: 70px; }
label.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
label.field > span { font-weight: 500; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
@media (max-width: 560px) {
  .grid-2 { grid-template-columns: 1fr; }
  .grid-4 { grid-template-columns: 1fr 1fr; }
}

.segmented { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; }
.segmented button { border: none; background: transparent; padding: 6px 12px; border-radius: 8px; }
.segmented button.on { background: var(--surface); box-shadow: var(--shadow); font-weight: 600; }

.badge {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 12px; padding: 2px 8px; border-radius: 99px;
  background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.badge.ok { background: var(--accent-soft); color: var(--accent); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.badge.info { background: var(--info-soft); color: var(--info); }

.alert { padding: 10px 12px; border-radius: 10px; font-size: 14px; }
.alert.error { background: var(--danger-soft); color: var(--danger); }
.alert.info { background: var(--info-soft); color: var(--info); }
.alert.warn { background: var(--warn-soft); color: var(--warn); }

.empty { padding: 28px; text-align: center; color: var(--muted); }

table.tbl { width: 100%; border-collapse: collapse; }
.tbl th, .tbl td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.tbl th { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.tbl td.r, .tbl th.r { text-align: right; }
.tbl tr:last-child td { border-bottom: none; }
.table-wrap { overflow-x: auto; }

/* ---------- Toasts ---------- */
.toasts { position: fixed; right: 16px; bottom: 16px; z-index: 100; display: flex; flex-direction: column; gap: 8px; max-width: min(420px, calc(100vw - 32px)); }
.toast { background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 10px; box-shadow: 0 6px 20px rgba(0,0,0,.2); font-size: 14px; }
.toast.error { background: var(--danger); color: #fff; }
@media (max-width: 820px) { .toasts { bottom: 84px; left: 16px; right: 16px; } }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 50; background: rgba(10, 12, 14, .45);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 6vh 16px 16px; overflow-y: auto;
}
.modal {
  background: var(--surface); border-radius: 14px; width: 100%; max-width: 560px;
  box-shadow: 0 20px 60px rgba(0,0,0,.25); border: 1px solid var(--border);
}
.modal.wide { max-width: 820px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--border); gap: 8px; }
.modal-body { padding: 16px 18px; display: flex; flex-direction: column; gap: 12px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--border); flex-wrap: wrap; }
@media (max-width: 560px) {
  .modal-backdrop { padding: 0; align-items: flex-end; }
  .modal { border-radius: 14px 14px 0 0; max-height: 92vh; overflow-y: auto; }
}

/* ---------- Picker ---------- */
.picker { position: relative; }
.picker-list {
  position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 60;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 8px 28px rgba(0,0,0,.14); max-height: 280px; overflow-y: auto;
}
.picker-item { padding: 8px 12px; cursor: pointer; display: flex; justify-content: space-between; gap: 10px; }
.picker-item:hover, .picker-item.hl { background: var(--surface-2); }
.picker-group { padding: 6px 12px 2px; font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }

/* ---------- Nutrition ---------- */
.macros { display: flex; gap: 10px; flex-wrap: wrap; font-size: 13px; font-variant-numeric: tabular-nums; }
.macro { display: inline-flex; align-items: center; gap: 4px; }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.dot.k { background: var(--kcal); } .dot.p { background: var(--prot); }
.dot.f { background: var(--fat); } .dot.c { background: var(--carb); }
.meter { height: 6px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.meter > i { display: block; height: 100%; border-radius: 99px; }
.meter.k > i { background: var(--kcal); } .meter.p > i { background: var(--prot); }
.meter.f > i { background: var(--fat); } .meter.c > i { background: var(--carb); }
.meter > i.over { background: var(--danger); }
.target-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; }
@media (max-width: 560px) { .target-grid { grid-template-columns: 1fr 1fr; } }

/* ---------- Planner ---------- */
.week { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 10px; }
@media (max-width: 1280px) { .week { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
@media (max-width: 980px) { .week { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 600px) { .week { grid-template-columns: 1fr; } }
.day { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); display: flex; flex-direction: column; min-width: 0; }
.day.today { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.day-head { padding: 10px 12px 8px; border-bottom: 1px solid var(--border); cursor: pointer; }
.day-head:hover { background: var(--surface-2); border-radius: var(--radius) var(--radius) 0 0; }
.day-title { font-weight: 650; }
.meal { padding: 8px 10px 6px; border-bottom: 1px dashed var(--border); }
.meal:last-child { border-bottom: none; }
.meal-head { display: flex; justify-content: space-between; align-items: center; font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.entry { display: flex; gap: 6px; align-items: flex-start; padding: 5px 6px; margin: 3px -4px 0; border-radius: 8px; cursor: pointer; }
.entry:hover { background: var(--surface-2); }
.entry .ico { width: 18px; flex: none; text-align: center; font-size: 13px; padding-top: 1px; }
.entry .name { font-size: 14px; line-height: 1.3; word-break: break-word; }
.entry.fact .name { color: var(--muted); }
.entry .meta { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.meal.drop-on { background: var(--accent-soft); border-radius: 8px; }
.entry.dragging { opacity: .4; }
.add-btn { font-size: 18px; line-height: 1; padding: 0 6px; color: var(--muted); border: none; background: none; }
.add-btn:hover { color: var(--accent); background: none !important; }

.day-detail-grid { display: grid; grid-template-columns: 1fr 320px; gap: 16px; align-items: start; }
@media (max-width: 980px) { .day-detail-grid { grid-template-columns: 1fr; } }

.grid-tiles { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
@media (max-width: 680px) { .grid-tiles { grid-template-columns: 1fr; } }

/* ---------- Shopping ---------- */
.shop-cat { padding: 8px 14px; background: var(--surface-2); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.shop-item { display: flex; align-items: center; gap: 12px; padding: 11px 14px; border-bottom: 1px solid var(--border); cursor: pointer; }
.shop-item:last-child { border-bottom: none; }
.shop-item.done .shop-name { text-decoration: line-through; color: var(--muted); }
.shop-item input { width: 20px; height: 20px; }

/* ---------- Lists ---------- */
.list-item { display: flex; gap: 12px; align-items: center; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.list-item:last-child { border-bottom: none; }
.list-item.clickable { cursor: pointer; }
.list-item.clickable:hover { background: var(--surface-2); }
.progress-pot { height: 8px; border-radius: 99px; background: var(--surface-2); overflow: hidden; position: relative; }
.progress-pot > i { position: absolute; top: 0; bottom: 0; left: 0; background: var(--accent); }
.progress-pot > b { position: absolute; top: 0; bottom: 0; background: repeating-linear-gradient(45deg, var(--warn), var(--warn) 4px, transparent 4px, transparent 7px); opacity: .8; }

.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; }
