:root {
  --bg: #0f1115;
  --surface: #1a1d24;
  --surface-2: #23272f;
  --border: #2e333d;
  --text: #f3f4f6;
  --text-dim: #9ca3af;
  --accent: #22c55e;
  --accent-2: #3b82f6;
  --danger: #ef4444;
  --warn: #f59e0b;
  --radius: 14px;
  --nav-h: 76px;
  --top-h: 60px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 18px;
  -webkit-tap-highlight-color: transparent;
}

body {
  padding-top: var(--top-h);
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom));
  min-height: 100vh;
}

h1, h2, h3 { margin: 0 0 8px; }

/* Icons */
.icon { width: 22px; height: 22px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; display: block; }

.topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--top-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 0 16px;
  z-index: 20;
}
.topbar h1 { font-size: 22px; flex-shrink: 0; }
.topbar-date {
  color: var(--text-dim);
  font-size: 16px;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#app {
  padding: 12px 14px 24px;
  max-width: 520px;
  margin: 0 auto;
}

/* Bottom nav */
.bottom-nav {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: stretch;
  justify-content: space-around;
  z-index: 20;
}
.nav-btn {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-dim);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-size: 13px;
  padding: 4px;
}
.nav-btn.active { color: var(--accent); }
.nav-btn .icon { width: 26px; height: 26px; }

/* Dashboard */
.dashboard {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
  border: 1px solid var(--border);
}
.macro-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}
.macro-row:last-child { margin-bottom: 0; }
.macro-label { width: 82px; font-size: 16px; color: var(--text-dim); flex-shrink: 0; }
.macro-bar-track {
  flex: 1;
  height: 12px;
  background: var(--surface-2);
  border-radius: 6px;
  overflow: hidden;
}
.macro-bar-fill { height: 100%; border-radius: 6px; background: var(--accent); transition: width .3s; }
.macro-bar-fill.over { background: var(--warn); }
.macro-nums { width: 118px; text-align: right; font-size: 16px; color: var(--text-dim); flex-shrink: 0; }
.macro-nums strong { color: var(--text); }

/* Today log */
.today-log { margin-bottom: 20px; }
.today-log h3 { font-size: 16px; color: var(--text-dim); text-transform: uppercase; letter-spacing: .04em; margin-bottom: 10px; }
.log-entry {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  margin-bottom: 8px;
}
.log-entry .le-name { font-size: 17px; }
.log-entry .le-sub { font-size: 15px; color: var(--text-dim); }
.log-entry .le-actions { display: flex; gap: 8px; }
.icon-btn {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 8px;
  width: 40px; height: 40px;
  display: flex; align-items: center; justify-content: center;
}
.icon-btn .icon { width: 19px; height: 19px; }
.icon-btn.danger { color: var(--danger); }

/* Add menu */
.menu-card {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  text-align: left;
  color: var(--text);
  font: inherit;
  width: 100%;
}
.menu-card .menu-icon { flex-shrink: 0; color: var(--accent); }
.menu-card .menu-icon .icon { width: 30px; height: 30px; }
.menu-card strong { display: block; font-size: 19px; margin-bottom: 2px; }

/* Item list (frequent, search results, recipe list) */
.frequent h2 { font-size: 16px; color: var(--text-dim); text-transform: uppercase; letter-spacing: .04em; margin-bottom: 10px; }
.item-list { display: flex; flex-direction: column; gap: 8px; }
.item-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  text-align: left;
  color: var(--text);
  font: inherit;
}
.item-card .ic-name { font-size: 18px; font-weight: 500; }
.item-card .ic-sub { font-size: 15px; color: var(--text-dim); margin-top: 3px; }
.item-card .ic-tap { font-size: 15px; color: var(--accent); flex-shrink: 0; margin-left: 10px; }
.empty-state { color: var(--text-dim); font-size: 17px; padding: 20px 4px; text-align: center; }

/* Forms */
.form-view, .confirm-view, .search-view, .scan-view, .add-menu, .library-view, .history-view {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* Library */
.library-header { display: flex; align-items: center; justify-content: space-between; }
.library-header h2 { margin: 0; }
.library-header .icon-btn { width: 44px; height: 44px; }
.library-header .icon-btn .icon { width: 22px; height: 22px; }
#lib-search { background: var(--surface); border: 1px solid var(--border); color: var(--text); border-radius: 10px; padding: 14px; font-size: 18px; }
.item-card .ic-actions { display: flex; align-items: center; gap: 10px; flex-shrink: 0; margin-left: 10px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 16px; color: var(--text-dim); flex: 1; }
.field input, .field select {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 14px;
  font-size: 18px;
}
.field-row { display: flex; gap: 10px; }

.btn-primary, .btn-secondary, .btn-danger {
  border: none;
  border-radius: 12px;
  padding: 16px;
  font-size: 18px;
  font-weight: 600;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}
.btn-primary { background: var(--accent); color: #0b0f0c; }
.btn-secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn-danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); }

/* Search */
.search-form { display: flex; gap: 8px; }
.search-form input { flex: 1; background: var(--surface); border: 1px solid var(--border); color: var(--text); border-radius: 10px; padding: 14px; font-size: 18px; }
.search-form button { border: none; background: var(--accent-2); color: #fff; border-radius: 10px; padding: 0 18px; font-weight: 600; font-size: 18px; }
.hint { color: var(--text-dim); font-size: 15px; }
.hint.warning { color: var(--warn); background: color-mix(in srgb, var(--warn) 14%, transparent); border: 1px solid var(--warn); border-radius: 10px; padding: 12px 14px; }

/* Confirm quantity */
.confirm-macros { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; display: flex; justify-content: space-between; font-size: 15px; color: var(--text-dim); }
.confirm-macros span strong { color: var(--text); display: block; font-size: 18px; }
.confirm-macros-preview { border-color: var(--accent); }

/* Scan */
.camera-wrap { position: relative; width: 100%; aspect-ratio: 3/4; background: #000; border-radius: var(--radius); overflow: hidden; }
.camera-wrap video { width: 100%; height: 100%; object-fit: cover; }
.scan-frame { position: absolute; inset: 20% 12%; border: 3px solid var(--accent); border-radius: 12px; pointer-events: none; }
.divider { text-align: center; color: var(--text-dim); font-size: 15px; margin: 4px 0; }

/* Recipe editor */
.ingredient-list { display: flex; flex-direction: column; gap: 8px; }
.ingredient-row { display: flex; justify-content: space-between; align-items: center; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; }
.ingredient-row input { width: 74px; background: var(--surface-2); border: 1px solid var(--border); color: var(--text); border-radius: 8px; padding: 8px; text-align: right; font-size: 17px; }

/* History */
.history-row { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; }
.history-row .hr-date { font-size: 17px; font-weight: 600; margin-bottom: 8px; }
.history-row .hr-macros { display: flex; gap: 16px; font-size: 15px; color: var(--text-dim); flex-wrap: wrap; }
.history-row .hr-macros strong { color: var(--text); }

/* Toast */
.toast {
  position: fixed;
  bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 12px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 12px 18px;
  border-radius: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  z-index: 50;
  box-shadow: 0 6px 18px rgba(0,0,0,.4);
}
.toast button { background: none; border: none; color: var(--accent); font-weight: 700; font-size: 16px; }
.hidden { display: none !important; }

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f5f6f8;
    --surface: #ffffff;
    --surface-2: #eef0f3;
    --border: #e2e4e9;
    --text: #14161a;
    --text-dim: #6b7280;
  }
}
