:root {
  --bg: #faf7f5;
  --surface: #ffffff;
  --text: #2a2320;
  --text-muted: #6b5f5a;
  --border: #e7ded9;
  --accent: #b6694a;
  --accent-hover: #a05a3d;
  --accent-soft: #f3e2da;
  --good: #3f7a52;
  --good-bg: #e7f3ea;
  --warn: #8a6420;
  --warn-bg: #fbf1de;
  --danger: #b3453f;
  --danger-bg: #fbe9e7;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(42, 35, 32, 0.06), 0 4px 14px rgba(42, 35, 32, 0.04);
  font-family: -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1715;
    --surface: #262019;
    --text: #f2e9e4;
    --text-muted: #b7a89f;
    --border: #3a322c;
    --accent: #e08a63;
    --accent-hover: #eb9d7a;
    --accent-soft: #3a2a22;
    --good: #7cc492;
    --good-bg: #1e2e22;
    --warn: #e0b45c;
    --warn-bg: #332a15;
    --danger: #e58a83;
    --danger-bg: #34201d;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  line-height: 1.55;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.wrap { max-width: 980px; margin: 0 auto; padding: 24px 16px 40px; }

a { color: var(--accent); }

/* ---------- Header ---------- */
header.app-header { text-align: center; margin-bottom: 24px; }
.logo-row { display: flex; align-items: center; justify-content: center; gap: 12px; }
.logo { border-radius: 12px; }
header.app-header h1 { margin: 0; font-size: clamp(1.25rem, 4.5vw, 1.7rem); letter-spacing: -0.01em; }
header.app-header p { margin: 8px auto 0; color: var(--text-muted); font-size: 0.92rem; max-width: 520px; }

/* ---------- Cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}
.card h2 { margin: 0; font-size: 1.02rem; }
.card > h2 { margin-bottom: 12px; }

.card-toggle {
  display: flex; width: 100%; align-items: center; justify-content: space-between;
  background: none; border: none; color: inherit; font: inherit; padding: 0; cursor: pointer;
}
.card-toggle .chevron { color: var(--text-muted); transition: transform 0.2s; font-size: 1.1rem; }
.card-toggle[aria-expanded="false"] .chevron { transform: rotate(-90deg); }
#profile-body { margin-top: 14px; }
#profile-card.collapsed #profile-body { display: none; }
.profile-summary { font-weight: 400; color: var(--text-muted); font-size: 0.85rem; margin-left: 6px; }

.profile-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
@media (max-width: 640px) { .profile-grid { grid-template-columns: 1fr; } }
.field-label { font-size: 0.8rem; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 8px; }

/* ---------- Chips ---------- */
.chip-group { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 999px;
  padding: 7px 14px;
  font-size: 0.85rem;
  cursor: pointer;
  user-select: none;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  min-height: 36px;
}
.chip:hover { border-color: var(--accent); }
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ---------- Tabs ---------- */
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin-bottom: 16px; overflow-x: auto; }
.tab-btn {
  background: none; border: none; padding: 10px 14px; font-size: 0.92rem;
  color: var(--text-muted); cursor: pointer; border-bottom: 2px solid transparent;
  white-space: nowrap; font-family: inherit;
}
.tab-btn.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.tab-panel { display: none; }
.tab-panel.active { display: block; animation: fadeIn 0.2s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(3px); } to { opacity: 1; transform: none; } }

/* ---------- Inputs & buttons ---------- */
textarea, input[type="text"] {
  width: 100%; border: 1px solid var(--border); border-radius: 10px;
  padding: 11px 13px; font-size: 16px; background: var(--bg); color: var(--text);
  font-family: inherit;
}
textarea:focus, input[type="text"]:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
textarea { min-height: 130px; resize: vertical; }

button.btn {
  background: var(--accent); color: #fff; border: none; border-radius: 10px;
  padding: 11px 18px; font-size: 0.92rem; cursor: pointer; font-weight: 600;
  font-family: inherit; min-height: 42px;
  transition: background 0.15s, opacity 0.15s;
}
button.btn:hover { background: var(--accent-hover); }
button.btn.secondary { background: var(--accent-soft); color: var(--accent); }
button.btn.secondary:hover { background: var(--accent-soft); filter: brightness(0.97); }
button.btn.ghost { background: none; color: var(--accent); border: 1px solid var(--border); font-weight: 500; }
button.btn.ghost:hover { border-color: var(--accent); background: none; }
button.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-top: 12px; }
.input-row { flex-wrap: nowrap; }
.input-row input { flex: 1; min-width: 0; }
.input-row .btn { flex-shrink: 0; }

.hint { font-size: 0.83rem; color: var(--text-muted); margin-top: 8px; }
.hint-inline { font-size: 0.8rem; color: var(--text-muted); }
.hint.error { color: var(--danger); }

/* ---------- Photo tab ---------- */
.file-drop {
  display: flex; align-items: center; gap: 14px;
  border: 2px dashed var(--border); border-radius: 12px;
  padding: 18px; cursor: pointer; margin-top: 4px;
  transition: border-color 0.15s, background 0.15s;
}
.file-drop:hover { border-color: var(--accent); background: var(--accent-soft); }
.file-drop-icon { font-size: 1.8rem; }

.progress-wrap { margin-top: 14px; }
.progress-bar { height: 8px; border-radius: 999px; background: var(--border); overflow: hidden; }
.progress-fill {
  height: 100%; width: 0%; border-radius: 999px; background: var(--accent);
  transition: width 0.25s ease;
}

/* ---------- Barcode / search ---------- */
#barcode-scan-region { width: 100%; max-width: 340px; margin: 12px auto; border-radius: 12px; overflow: hidden; }
#barcode-scan-region:not(:empty) { border: 1px solid var(--border); }

.search-results { margin-top: 14px; display: grid; gap: 10px; }
.search-result-item {
  display: flex; gap: 12px; align-items: center; padding: 12px; border: 1px solid var(--border);
  border-radius: 12px; cursor: pointer; background: var(--bg);
  transition: border-color 0.15s, transform 0.1s;
}
.search-result-item:hover { border-color: var(--accent); }
.search-result-item:active { transform: scale(0.99); }
.search-result-item img { width: 48px; height: 48px; object-fit: cover; border-radius: 8px; background: var(--surface); flex-shrink: 0; }
.search-result-item .thumb-placeholder {
  width: 48px; height: 48px; border-radius: 8px; background: var(--accent-soft); flex-shrink: 0;
  display: flex; align-items: center; justify-content: center; font-size: 1.3rem;
}
.search-result-item .meta { min-width: 0; }
.search-result-item .meta strong { display: block; font-size: 0.92rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.search-result-item .meta span { font-size: 0.78rem; color: var(--text-muted); }

.spinner {
  display: inline-block; width: 14px; height: 14px; border-radius: 50%;
  border: 2px solid var(--border); border-top-color: var(--accent);
  animation: spin 0.7s linear infinite; vertical-align: -2px; margin-right: 6px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Results: fit score ---------- */
.fit-score { display: flex; align-items: center; gap: 20px; }
.score-ring { flex-shrink: 0; }
.score-ring .ring-track { stroke: var(--border); }
.score-ring .ring-value { transition: stroke-dashoffset 0.8s ease; }
.score-ring text { fill: var(--text); font-weight: 700; font-size: 26px; }
.score-ring .ring-label { font-size: 10px; font-weight: 500; fill: var(--text-muted); }
.fit-score .details { flex: 1; min-width: 0; }
.fit-score .details strong { font-size: 0.88rem; }
.fit-score ul { margin: 4px 0 10px; padding-left: 18px; font-size: 0.85rem; }
.fit-score li { margin-bottom: 2px; }
@media (max-width: 520px) {
  .fit-score { flex-direction: column; align-items: flex-start; gap: 12px; }
}

/* ---------- Results: warnings ---------- */
.warning-item {
  border-radius: 10px; padding: 12px 14px; margin-bottom: 10px; font-size: 0.88rem;
  display: flex; gap: 10px; align-items: flex-start;
}
.warning-item .w-icon { flex-shrink: 0; font-size: 1.05rem; line-height: 1.4; }
.warning-item.warning { background: var(--danger-bg); color: var(--danger); }
.warning-item.caution { background: var(--warn-bg); color: var(--warn); }
.warning-item.info { background: var(--accent-soft); color: var(--accent); }
.warning-item strong { display: block; margin-bottom: 3px; font-size: 0.92rem; }
.warning-item .involved { opacity: 0.85; font-size: 0.8rem; margin-top: 5px; }

/* ---------- Results: ingredient cards ---------- */
.section-note { font-size: 0.82rem; color: var(--text-muted); margin: -6px 0 14px; }
.ingredient-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(270px, 1fr)); gap: 12px; }
.ing-card {
  border: 1px solid var(--border); border-radius: 12px; padding: 14px; background: var(--bg);
  display: flex; flex-direction: column;
}
.ing-card.flagged { border-color: color-mix(in srgb, var(--danger) 45%, var(--border)); }
.ing-card h3 { margin: 0 0 2px; font-size: 0.97rem; }
.ing-card .category { font-size: 0.72rem; color: var(--accent); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.ing-card .position { font-size: 0.72rem; color: var(--text-muted); float: right; }
.ing-card p { font-size: 0.84rem; margin: 8px 0; color: var(--text-muted); flex: 1; }
.badge-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: auto; }
.badge {
  font-size: 0.72rem; padding: 4px 9px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--border);
  display: inline-flex; align-items: center; gap: 4px;
}
.badge.good { background: var(--good-bg); color: var(--good); border-color: transparent; }
.badge.warn { background: var(--warn-bg); color: var(--warn); border-color: transparent; }
.badge.danger { background: var(--danger-bg); color: var(--danger); border-color: transparent; }

.dots { display: inline-flex; gap: 3px; vertical-align: middle; }
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--border); }
.dot.filled { background: var(--warn); }
.dot.filled.high { background: var(--danger); }

.unmatched-list { font-size: 0.85rem; color: var(--text-muted); line-height: 2; }
.unmatched-list code {
  background: var(--accent-soft); color: var(--text); padding: 2px 8px; border-radius: 6px;
  margin-right: 4px; display: inline-block; font-size: 0.8rem;
}

/* ---------- Empty state ---------- */
.empty-state { text-align: center; padding: 20px 16px; color: var(--text-muted); font-size: 0.9rem; }
.empty-state .big { font-size: 2rem; display: block; margin-bottom: 8px; }

/* ---------- Footer ---------- */
.app-footer { margin-top: 28px; text-align: center; }
.disclaimer { font-size: 0.78rem; color: var(--text-muted); max-width: 640px; margin: 0 auto 8px; }
.credits { font-size: 0.74rem; color: var(--text-muted); margin: 0; }

@media (max-width: 480px) {
  .wrap { padding: 16px 12px 32px; }
  .card { padding: 14px; }
  .row .btn { flex: 1; }
  .row .btn.ghost { flex: 0 1 auto; }
}
