/* Save as: assets/app.css */
:root {
  --color-bg: #f7f6f3;
  --color-surface: #ffffff;
  --color-border: #e2e0da;
  --color-text: #26241f;
  --color-muted: #6f6b60;
  --color-primary: #c0392b;
  --color-primary-hover: #a5301f;
  --color-secondary: #eeece5;
  --color-secondary-hover: #e2dfd6;
  --color-success: #3d7a4d;
  --color-danger: #b23b3b;
  --color-sidebar-bg: #1e1c18;
  --color-sidebar-text: #d8d4c8;
  --color-sidebar-muted: #8a8579;
  --radius: 8px;
  --sidebar-width: 220px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-width: 960px; /* desktop-only tool, not designed for mobile */
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
}

/* ── App shell / sidebar ───────────────────────────────────────── */

.app-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  background: var(--color-sidebar-bg);
  color: var(--color-sidebar-text);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
}

.sidebar-brand {
  padding: 20px 20px 16px;
  font-weight: 700;
  font-size: 16px;
  letter-spacing: 0.02em;
  border-bottom: 1px solid rgba(255,255,255,0.08);
}

.sidebar-nav {
  list-style: none;
  margin: 0;
  padding: 12px 10px;
  flex: 1;
}

.sidebar-link {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border-radius: var(--radius);
  color: var(--color-sidebar-text);
  text-decoration: none;
  font-size: 13.5px;
  margin-bottom: 2px;
}

.sidebar-link svg { flex-shrink: 0; opacity: 0.75; }

.sidebar-link:hover {
  background: rgba(255,255,255,0.06);
}

.sidebar-link.active {
  background: var(--color-primary);
  color: #fff;
}
.sidebar-link.active svg { opacity: 1; }

.sidebar-footer {
  padding: 14px 20px;
  border-top: 1px solid rgba(255,255,255,0.08);
}

.sidebar-user {
  font-size: 12.5px;
  color: var(--color-sidebar-muted);
  margin-bottom: 6px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar-logout {
  font-size: 12.5px;
  color: var(--color-sidebar-text);
  text-decoration: none;
}
.sidebar-logout:hover { color: #fff; }

.main-content {
  flex: 1;
  padding: 28px 36px;
  max-width: 1200px;
}

.main-content.page-foods,
.main-content.page-importers,
.main-content.page-scrapers,
.main-content.page-dashboard {
  max-width: none;
}

.page-header {
  margin-bottom: 20px;
}

.page-header h1 {
  margin: 4px 0 0;
  font-size: 22px;
}

.back-link {
  color: var(--color-muted);
  text-decoration: none;
  font-size: 13px;
}
.back-link:hover { color: var(--color-text); }

.empty-state {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 28px;
  color: var(--color-muted);
  font-size: 14px;
  line-height: 1.6;
}
.empty-state a { color: var(--color-primary); text-decoration: none; font-weight: 600; }
.empty-state a:hover { text-decoration: underline; }

.error-text {
  color: var(--color-danger);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12.5px;
  white-space: pre-wrap;
  word-break: break-word;
}

.diff-warning {
  background: #fdf2ef;
  border: 1px solid #f0c9bd;
  color: #a5432b;
  border-radius: var(--radius);
  padding: 12px 14px;
  font-size: 13px;
  margin-bottom: 16px;
}

/* ── Login ─────────────────────────────────────────────────────── */

.login-page {
  min-width: 0; /* login page itself is fine at any width */
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

.login-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 32px;
  width: 300px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.login-card h1 {
  margin: 0;
  font-size: 20px;
}

.login-subtitle {
  margin: 0 0 4px;
  font-size: 13px;
  color: var(--color-muted);
}

.login-card input {
  padding: 10px 12px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font-size: 14px;
}

.login-card button {
  padding: 10px 12px;
  border: none;
  border-radius: var(--radius);
  background: var(--color-primary);
  color: #fff;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
}
.login-card button:hover { background: var(--color-primary-hover); }

.login-card .error {
  color: var(--color-danger);
  font-size: 13px;
  min-height: 16px;
  margin: 0;
}

/* ── Scraper cards ─────────────────────────────────────────────── */

.dashboard {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
}

.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 18px;
}

.card-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 14px;
}

.card-head h2 {
  margin: 0;
  font-size: 16px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.card-logo {
  height: 28px;
  max-width: 140px;
  object-fit: contain;
  vertical-align: middle;
}

.card-logo-fallback {
  font-size: 16px;
  font-weight: 700;
}

.logo-join {
  color: var(--color-muted);
  font-weight: 400;
  font-size: 13px;
}

.item-count {
  font-size: 12px;
  color: var(--color-muted);
}

.run-status {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin: 8px 0 14px;
}

.run-status-badge {
  font-size: 11px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 999px;
  white-space: nowrap;
}

.status-never {
  background: #f0f0f0;
  color: var(--color-muted);
}

.status-running {
  background: #e6f0ff;
  color: #2563eb;
}

.status-success {
  background: #eaf4ec;
  color: var(--color-success);
}

.status-failed {
  background: #fdf2ef;
  color: #a5432b;
}

.card-actions {
  display: flex;
  gap: 8px;
}

/* ── Buttons ───────────────────────────────────────────────────── */

.btn {
  border: none;
  border-radius: var(--radius);
  padding: 9px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.btn-primary {
  background: var(--color-primary);
  color: #fff;
}
.btn-primary:hover { background: var(--color-primary-hover); }
.btn-primary:disabled { background: var(--color-secondary); color: var(--color-muted); cursor: not-allowed; }

.btn-secondary {
  background: var(--color-secondary);
  color: var(--color-text);
}
.btn-secondary:hover { background: var(--color-secondary-hover); }

.btn[aria-disabled="true"] {
  opacity: 0.4;
  pointer-events: none;
}

.btn:disabled {
  opacity: 0.6;
  cursor: default;
}

/* ── Log tail ──────────────────────────────────────────────────── */

.log-tail {
  margin-top: 14px;
  background: #1e1c18;
  color: #d8d4c8;
  border-radius: var(--radius);
  padding: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  max-height: 220px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-word;
}

.done-banner[hidden] {
  display: none;
}

.done-banner {
  margin-top: 14px;
  padding: 14px 16px;
  background: #eaf4ec;
  border: 1px solid #bfe0c6;
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.done-banner-text {
  font-size: 13.5px;
  font-weight: 600;
  color: var(--color-success);
}

/* ── Review table ──────────────────────────────────────────────── */

.review-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
}

.review-status {
  font-size: 13px;
  color: var(--color-muted);
}

.review-status.success { color: var(--color-success); }
.review-status.error { color: var(--color-danger); }

.review-section {
  margin-bottom: 28px;
}

.review-section-heading {
  font-size: 15px;
  margin: 0 0 8px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.count-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--color-secondary);
  color: var(--color-muted);
  font-size: 12px;
  font-weight: 600;
}

.review-section[data-section="new"] .count-badge {
  background: #e3f0e5;
  color: var(--color-success);
}

.review-section[data-section="changed"] .count-badge {
  background: #fbeee0;
  color: #a5652b;
}

.empty-row {
  color: var(--color-muted);
  font-style: italic;
  text-align: center;
  padding: 16px !important;
}

.diff-note {
  font-size: 10.5px;
  color: var(--color-danger);
  margin-top: 1px;
  padding-left: 6px;
  white-space: nowrap;
}

.review-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  overflow: hidden;
}

.review-table th, .review-table td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--color-border);
  font-size: 13px;
  text-align: left;
}

.review-table th {
  background: var(--color-secondary);
  font-weight: 600;
  position: sticky;
  top: 0;
  z-index: 1;
}

.review-table tr.excluded {
  opacity: 0.4;
}

.unit-suffix {
  font-size: 11px;
  color: var(--color-muted);
}

.barcode-cell {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  color: var(--color-muted);
}

.units-row td {
  border-bottom: 1px solid var(--color-border);
  padding: 4px 10px 8px;
  background: var(--color-bg);
}

.units-toggle {
  background: none;
  border: none;
  color: var(--color-muted);
  font-size: 12px;
  cursor: pointer;
  padding: 2px 0;
}

.units-toggle:hover {
  color: var(--color-text);
}

.units-editor {
  margin-top: 6px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.unit-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.unit-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: var(--color-secondary);
  border-radius: 999px;
  padding: 3px 6px 3px 10px;
  font-size: 12px;
}

.unit-remove {
  background: none;
  border: none;
  color: var(--color-muted);
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  padding: 0 2px;
}

.unit-remove:hover {
  color: var(--color-danger);
}

.unit-add {
  display: flex;
  gap: 6px;
  align-items: center;
}

.unit-add input[type="text"] {
  width: 160px;
}

.unit-add input[type="number"] {
  width: 80px;
}

.unit-add input {
  padding: 5px 8px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font-size: 12.5px;
  font-family: inherit;
}

.review-table img.thumb {
  width: 40px;
  height: 40px;
  object-fit: cover;
  border-radius: 4px;
  background: var(--color-secondary);
}

.review-table input[type="text"],
.review-table input[type="number"] {
  width: 100%;
  border: 1px solid transparent;
  background: transparent;
  padding: 4px 6px;
  border-radius: 4px;
  font-size: 13px;
}

.review-table input[type="text"]:focus,
.review-table input[type="number"]:focus {
  border-color: var(--color-border);
  background: var(--color-bg);
  outline: none;
}

.review-table input[type="number"] {
  width: 70px;
}

/* Auto table layout otherwise squeezes text fields down to almost
   nothing once there are enough numeric columns competing for space —
   force sensible minimums so name/brand/URL stay usable. */
.review-table input[data-key="name"] { min-width: 180px; }
.review-table input[data-key="brand"] { min-width: 120px; }
.review-table input[data-key="source_url"] { min-width: 200px; }

.review-table-wrap {
  overflow-x: auto;
  overflow-y: auto;
  max-height: calc(100vh - 320px);
}

/* ── Dashboard ─────────────────────────────────────────────────── */

.stat-section-heading {
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--color-muted);
  margin: 26px 0 10px;
}

.stat-section-heading:first-of-type {
  margin-top: 0;
}

.period-switcher {
  display: flex;
  gap: 8px;
  margin-bottom: 18px;
  flex-wrap: wrap;
}

.period-pill {
  text-decoration: none;
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 600;
  background: var(--color-surface);
  color: var(--color-muted);
  border: 1px solid var(--color-border);
}

.period-pill:hover {
  border-color: var(--color-primary);
  color: var(--color-primary);
}

.period-pill.active {
  background: var(--color-primary);
  color: #fff;
  border-color: var(--color-primary);
}

/* ── AI Overview ──────────────────────────────────────────────── */

.ai-chat {
  max-width: 640px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.ai-chat-pair {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.ai-bubble {
  border-radius: var(--radius);
  padding: 12px 14px;
  max-width: 85%;
}

.ai-bubble-user {
  align-self: flex-end;
  background: var(--color-primary);
  color: #fff;
}

.ai-bubble-ai {
  align-self: flex-start;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
}

.ai-bubble-img {
  display: block;
  max-width: 100%;
  border-radius: calc(var(--radius) - 2px);
  margin-bottom: 8px;
}

.ai-bubble-text {
  font-size: 13.5px;
  line-height: 1.45;
}

.ai-bubble-empty {
  opacity: 0.75;
  font-style: italic;
}

.ai-bubble-meta {
  font-size: 11px;
  opacity: 0.8;
  margin-top: 6px;
}

.ai-bubble-brand {
  font-weight: 400;
  color: var(--color-muted);
}

.ai-bubble-macros {
  font-size: 12px;
  color: var(--color-muted);
  margin-top: 4px;
}

.ai-bubble-reasoning {
  font-size: 12px;
  color: var(--color-muted);
  font-style: italic;
  line-height: 1.45;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--color-border);
}

.ai-chat-date {
  align-self: center;
  font-size: 11px;
  font-weight: 600;
  color: var(--color-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin: 10px 0 4px;
}

.ai-bubble-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.ai-bubble-row.row-user {
  justify-content: flex-end;
}

.ai-bubble-row.row-ai {
  justify-content: flex-start;
}

.ai-avatar,
.ai-avatar-initial,
.ai-avatar-bot {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  flex-shrink: 0;
}

.ai-avatar {
  object-fit: cover;
}

.ai-avatar-initial {
  background: var(--color-primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
}

.ai-avatar-bot {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}

.stat-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 16px 18px;
}

.stat-value {
  font-size: 28px;
  font-weight: 700;
  line-height: 1.2;
  color: var(--color-text);
}

.stat-label {
  font-size: 12.5px;
  color: var(--color-muted);
  margin-top: 2px;
}

.stat-sub {
  font-size: 11.5px;
  color: var(--color-muted);
  margin-top: 6px;
}

.dash-columns {
  display: grid;
  grid-template-columns: 1.3fr 1fr;
  gap: 14px;
  margin-bottom: 14px;
}

@media (max-width: 860px) {
  .dash-columns { grid-template-columns: 1fr; }
}

.dash-panel {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 14px;
}

.dash-panel h2 {
  font-size: 15px;
  margin: 0 0 12px;
}

.dash-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.dash-table th {
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--color-muted);
  padding: 6px 8px;
  border-bottom: 1px solid var(--color-border);
}

.dash-table td {
  padding: 8px;
  border-bottom: 1px solid var(--color-border);
}

.dash-table tbody tr:last-child td {
  border-bottom: none;
}

.dash-link {
  display: inline-block;
  margin-top: 12px;
  font-size: 13px;
  color: var(--color-primary);
  text-decoration: none;
  font-weight: 600;
}

.dash-link:hover {
  text-decoration: underline;
}

.dash-empty {
  color: var(--color-muted);
  font-size: 13px;
}

.bar-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}

.bar-label {
  width: 90px;
  flex-shrink: 0;
  font-size: 12.5px;
  color: var(--color-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.bar-track {
  flex: 1;
  height: 10px;
  background: var(--color-secondary);
  border-radius: 999px;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  background: var(--color-primary);
  border-radius: 999px;
}

.bar-count {
  width: 36px;
  flex-shrink: 0;
  text-align: right;
  font-size: 12px;
  color: var(--color-muted);
}

/* ── Food database page ───────────────────────────────────────── */

.pagination {
  display: flex;
  align-items: center;
  gap: 14px;
  justify-content: center;
  margin: 18px 0;
}

.page-indicator {
  font-size: 13px;
  color: var(--color-muted);
}

.row-actions {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 110px;
}

.row-actions .btn {
  font-size: 12px;
  padding: 5px 10px;
}

.row-status {
  font-size: 11.5px;
  margin-top: 2px;
}

.row-status .review-status {
  font-size: 11.5px;
}

/* ── Food database: fixed-width table + serving-units modal ──────── */

.foods-table {
  table-layout: fixed;
}

.importer-table {
  table-layout: fixed;
}

.importer-table input[type="text"],
.importer-table input[type="number"] {
  width: 100%;
  box-sizing: border-box;
}

.foods-table input[type="text"],
.foods-table input[type="number"] {
  width: 100%;
  box-sizing: border-box;
}

.units-link {
  display: block;
  background: none;
  border: none;
  color: var(--color-primary);
  font-size: 11px;
  cursor: pointer;
  padding: 2px 0 0;
  text-align: left;
}

.units-link:hover {
  text-decoration: underline;
}

.source-url-cell {
  display: flex;
  align-items: center;
  gap: 4px;
}

.source-url-cell input[type="text"] {
  flex: 1;
}

.url-tool-btn {
  flex-shrink: 0;
  background: none;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font-size: 13px;
  line-height: 1;
  padding: 5px 6px;
  cursor: pointer;
}

.url-tool-btn:hover {
  background: var(--color-secondary);
}

.find-url-menu {
  position: absolute;
  z-index: 300;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
  padding: 4px;
  min-width: 190px;
}

.find-url-menu[hidden] {
  display: none;
}

.find-url-link {
  display: block;
  padding: 8px 10px;
  border-radius: var(--radius);
  font-size: 13px;
  color: var(--color-text);
  text-decoration: none;
  white-space: nowrap;
}

.find-url-link:hover {
  background: var(--color-secondary);
}

.modal-overlay[hidden] {
  display: none;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal-box {
  background: var(--color-surface);
  border-radius: var(--radius);
  width: 420px;
  max-width: 90vw;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--color-border);
}

.modal-header h3 {
  margin: 0;
  font-size: 15px;
}

.modal-close {
  background: none;
  border: none;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  color: var(--color-muted);
  padding: 0;
}

.modal-close:hover {
  color: var(--color-text);
}

.modal-body {
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal-body .unit-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.modal-body .unit-add {
  display: flex;
  gap: 6px;
}

.modal-body .unit-add input[type="text"] {
  flex: 1;
}

.modal-body .unit-add input[type="number"] {
  width: 80px;
}