/* FarmDirect B2B – main styles
   Map-first layout, transparent top bar, sidebar, filter card, floating cards, modals.
   Supports dark theme via [data-theme="dark"]. Responsive for phone (progressive disclosure). */

:root {
  --bg: #ffffff;
  --fg: #1a1a1a;
  --muted: #5c6670;
  --accent: #2d6a4f;
  --accent-2: #40916c;
  --surface: #f7f7f5;
  --border: #e2e2dc;
  --shadow: 0 4px 24px rgba(0,0,0,.12);
  --radius: 12px;
  --topbar-h: 56px;
  --sidebar-w: 360px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

[data-theme="dark"] {
  --bg: #121417;
  --fg: #e8eaed;
  --muted: #9aa0a6;
  --accent: #52b788;
  --accent-2: #74c69d;
  --surface: #1c1f24;
  --border: #2d333b;
  --shadow: 0 4px 24px rgba(0,0,0,.45);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif; color: var(--fg); background: var(--bg); }

/* Offline banner */
.offline-banner {
  position: fixed; top: 0; left: 0; right: 0; z-index: 2000;
  background: #fff3cd; color: #664d03; padding: 8px 12px; display: flex; align-items: center; gap: 12px;
  font-size: 14px; border-bottom: 1px solid #ffe69c;
}
[data-theme="dark"] .offline-banner { background: #3d2f00; color: #ffe69c; border-color: #664d03; }
.offline-banner[hidden] { display: none; }

/* Top bar */
.top-bar {
  position: fixed; top: var(--safe-top); left: 0; right: 0; height: var(--topbar-h);
  display: flex; align-items: center; gap: 12px; padding: 0 12px;
  background: rgba(255,255,255,.72); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid rgba(0,0,0,.06); z-index: 1000;
}
[data-theme="dark"] .top-bar { background: rgba(18,20,23,.72); border-bottom-color: rgba(255,255,255,.08); }
.brand { font-weight: 700; color: var(--accent); white-space: nowrap; }
.search-box { position: relative; flex: 1; max-width: 520px; }
.search-box input {
  width: 100%; height: 40px; border: 1px solid var(--border); border-radius: 999px;
  padding: 0 40px 0 16px; background: var(--surface); color: var(--fg); font-size: 15px;
}
.search-box .icon-btn { position: absolute; right: 4px; top: 4px; }
.search-suggestions {
  position: absolute; top: 44px; left: 0; right: 0; background: var(--bg);
  border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  max-height: 320px; overflow: auto; z-index: 1100;
}
.search-suggestions[hidden] { display: none; }
.suggestion-item { padding: 10px 12px; cursor: pointer; border-bottom: 1px solid var(--border); }
.suggestion-item:hover, .suggestion-item.active { background: var(--surface); }
.suggestion-swatch { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; }
.suggestion-meta { font-size: 12px; color: var(--muted); }
.top-actions { display: flex; align-items: center; gap: 6px; margin-left: auto; }
.icon-btn {
  width: 40px; height: 40px; border: none; background: transparent; border-radius: 50%;
  color: var(--fg); font-size: 18px; cursor: pointer; display: grid; place-items: center;
  position: relative;
}
.icon-btn:hover { background: var(--surface); }
.badge {
  position: absolute; top: 2px; right: 2px; background: var(--accent); color: #fff;
  font-size: 10px; min-width: 16px; height: 16px; border-radius: 8px; display: grid; place-items: center;
  padding: 0 4px;
}
.badge[hidden] { display: none; }
.data-source-badge {
  font-size: 11px; font-weight: 700; letter-spacing: .04em; padding: 4px 8px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--border); cursor: pointer;
}
.data-source-badge[data-source="live"] { background: #d1fae5; color: #065f46; }
[data-theme="dark"] .data-source-badge[data-source="live"] { background: #064e3b; color: #a7f3d0; }
.data-source-badge[data-source="seed"] { background: #fef3c7; color: #92400e; border-color: #fbbf24; }
.data-source-badge[data-source="demo"] { background: #dbeafe; color: #1e40af; border-color: #60a5fa; }
.data-source-badge[data-source="api-empty"] { background: #f3e8ff; color: #6b21a8; border-color: #c084fc; }
[data-theme="dark"] .data-source-badge[data-source="seed"] { background: #451a03; color: #fde68a; }
[data-theme="dark"] .data-source-badge[data-source="demo"] { background: #1e3a8a; color: #bfdbfe; }
[data-theme="dark"] .data-source-badge[data-source="api-empty"] { background: #581c87; color: #e9d5ff; }

/* Map */
#map { position: fixed; inset: 0; z-index: 0; }
.leaflet-container { font: inherit; }
.map-legend {
  position: fixed; bottom: 16px; right: 16px; background: var(--bg); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 10px 12px; box-shadow: var(--shadow); z-index: 500; font-size: 13px;
  max-width: 200px;
}
.map-legend[hidden] { display: none; }
.map-legend h4 { margin: 0 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.legend-item { display: flex; align-items: center; gap: 8px; padding: 4px 0; cursor: pointer; }
.legend-item:hover { color: var(--accent); }
.swatch { width: 12px; height: 12px; border-radius: 3px; flex-shrink: 0; }

/* Sidebar */
.sidebar {
  position: fixed; top: calc(var(--topbar-h) + var(--safe-top)); left: 0; bottom: var(--safe-bottom);
  width: var(--sidebar-w); background: var(--bg); border-right: 1px solid var(--border);
  z-index: 800; display: flex; flex-direction: column; transform: translateX(-100%); transition: transform .2s;
  box-shadow: var(--shadow);
}
.sidebar.open { transform: translateX(0); }
.sidebar-header { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--border); }
.sidebar-header h2 { margin: 0; font-size: 16px; flex: 1; }
.count-btn { font-size: 13px; color: var(--muted); }
.sidebar-tools { display: flex; flex-wrap: wrap; gap: 6px; padding: 8px 12px; border-bottom: 1px solid var(--border); align-items: center; }
.listings-list { flex: 1; overflow: auto; padding: 8px; }
.listing-item {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 10px 12px; border-radius: 8px; cursor: pointer; margin-bottom: 6px; border: 1px solid transparent;
}
.listing-item:hover, .listing-item.selected { background: var(--surface); border-color: var(--border); }
.list-thumb {
  width: 56px; height: 56px; border-radius: 6px; object-fit: cover; flex-shrink: 0; background: var(--surface);
}
.list-thumb.placeholder {
  display: grid; place-items: center; font-size: 11px; font-weight: 700; color: var(--muted); text-align: center;
}
.list-body { flex: 1; min-width: 0; }
.list-title { font-weight: 600; margin-bottom: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list-meta { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; font-size: 13px; }
.list-seller { margin-top: 2px; }
.fav-btn { margin-left: auto; flex-shrink: 0; }
.seller-group { margin-bottom: 10px; }
.group-header {
  margin: 6px 0 4px; padding: 4px 0; font-size: 13px; font-weight: 700; cursor: pointer; color: var(--accent);
  border-bottom: 1px solid var(--border);
}
.group-header:hover { text-decoration: underline; }
.empty-state { padding: 24px 12px; text-align: center; color: var(--muted); }
.empty-state p { margin: 0 0 12px; }
.listing-item .price { font-weight: 600; color: var(--accent); }
.small-btn {
  padding: 6px 10px; border: 1px solid var(--border); background: var(--surface); color: var(--fg);
  border-radius: 8px; font-size: 13px; cursor: pointer;
}
.small-btn:hover { border-color: var(--accent); }
.small-btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.small-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.small-label { font-size: 13px; display: flex; align-items: center; gap: 4px; }

/* Filter chips (seed/demo UX) */
.filter-chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 8px 12px 4px; }
.filter-chip {
  display: inline-flex; align-items: center; gap: 4px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 999px; padding: 2px 8px; font-size: 12px;
}
.filter-chip button {
  border: none; background: transparent; cursor: pointer;
  color: var(--muted); font-size: 14px; padding: 0; line-height: 1;
}
.filter-chip button:hover { color: var(--fg); }

/* Filter card */
.filter-card {
  position: fixed; top: calc(var(--topbar-h) + var(--safe-top) + 8px); right: 12px; width: 280px;
  background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); z-index: 900; padding: 0;
}
.filter-card[hidden] { display: none; }
.card-header, .menu-header { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px; border-bottom: 1px solid var(--border); }
.filter-body, .menu-body { padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.filter-body label, .menu-body label { font-size: 13px; display: flex; flex-direction: column; gap: 4px; }
.filter-body select, .filter-body input[type="number"], .api-row input {
  height: 36px; border: 1px solid var(--border); border-radius: 8px; padding: 0 10px; background: var(--surface); color: var(--fg);
}
.filter-actions { display: flex; gap: 8px; margin-top: 6px; }

/* Options menu */
.options-menu {
  position: fixed; top: calc(var(--topbar-h) + var(--safe-top) + 8px); right: 12px; width: 260px;
  background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); z-index: 900;
}
.options-menu[hidden] { display: none; }
.menu-btn {
  width: 100%; text-align: left; padding: 8px 10px; border: none; background: transparent;
  color: var(--fg); border-radius: 6px; cursor: pointer; font-size: 14px;
}
.menu-btn:hover { background: var(--surface); }
.theme-row { flex-direction: row; align-items: center; justify-content: space-between; }
.api-row { display: flex; gap: 6px; }

/* Floating product / seller cards */
.floating-card {
  position: fixed; top: calc(var(--topbar-h) + var(--safe-top) + 16px); left: calc(var(--sidebar-w) + 16px);
  width: min(420px, calc(100vw - var(--sidebar-w) - 32px)); max-height: calc(100vh - var(--topbar-h) - 40px);
  background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); z-index: 850; overflow: auto; display: none;
}
.floating-card.open { display: block; }
.card-image { width: 100%; height: 180px; object-fit: cover; cursor: zoom-in; }
.card-body { padding: 14px; }
.card-body h3 { margin: 0 0 8px; }
.card-meta { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-bottom: 8px; font-size: 13px; }
.category-badge, .visibility-badge, .units-badge {
  padding: 2px 8px; border-radius: 999px; font-size: 12px; border: 1px solid var(--border); background: var(--surface); cursor: pointer;
}
.price { font-weight: 700; color: var(--accent); }
.card-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.close-card {
  position: absolute; top: 8px; right: 8px; width: 32px; height: 32px; border: none; background: rgba(0,0,0,.4);
  color: #fff; border-radius: 50%; cursor: pointer; font-size: 18px;
}
.address-link { color: var(--accent); text-decoration: none; }
.address-link:hover { text-decoration: underline; }

/* Lightbox */
.lightbox {
  position: fixed; inset: 0; background: rgba(0,0,0,.85); z-index: 3000; display: none;
  align-items: center; justify-content: center; cursor: zoom-out;
}
.lightbox.open { display: flex; }
.lightbox img { max-width: 90vw; max-height: 90vh; border-radius: 8px; }

/* Modals */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 2500; display: none;
  align-items: center; justify-content: center; padding: 20px;
}
.modal.open { display: flex; }
.modal-content {
  background: var(--bg); border-radius: var(--radius); padding: 20px; max-width: 520px; width: 100%;
  max-height: 80vh; overflow: auto; position: relative; box-shadow: var(--shadow);
}
.close-modal {
  position: absolute; top: 10px; right: 10px; border: none; background: transparent;
  font-size: 22px; cursor: pointer; color: var(--fg);
}

/* Toast */
.toast-container {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 4000;
  display: flex; flex-direction: column; gap: 8px; pointer-events: none;
}
.toast {
  background: var(--fg); color: var(--bg); padding: 10px 16px; border-radius: 8px; font-size: 14px;
  box-shadow: var(--shadow); opacity: 1; transition: opacity .3s;
}
.toast.fade-out { opacity: 0; }
.toast-success { background: #065f46; }
.toast-error { background: #991b1b; }
.toast-info { background: #1e3a8a; }

/* Markers */
.fd-marker { background: none !important; border: none !important; }
.marker-pin {
  width: 28px; height: 28px; border-radius: 50% 50% 50% 0; transform: rotate(-45deg);
  display: grid; place-items: center; border: 2px solid #fff; box-shadow: 0 2px 6px rgba(0,0,0,.3);
}
.marker-pin .fav-star { transform: rotate(45deg); font-size: 12px; color: #fff; }

/* Responsive / phone */
@media (max-width: 900px) {
  .sidebar { width: 100%; max-width: 100%; }
  .sidebar.open { box-shadow: none; }
  .floating-card { left: 12px; right: 12px; width: auto; top: auto; bottom: 12px; max-height: 50vh; }
  .filter-card, .options-menu { right: 8px; left: 8px; width: auto; }
  .top-bar { padding: 0 8px; }
  .brand { display: none; }
  .search-box { max-width: none; }
}

/* Utility */
.muted { color: var(--muted); }
.small { font-size: 12px; }
.link-btn { background: none; border: none; color: var(--accent); cursor: pointer; font-size: inherit; padding: 0; }
.link-btn:hover { text-decoration: underline; }
kbd { background: var(--surface); border: 1px solid var(--border); border-radius: 4px; padding: 1px 5px; font-size: 12px; }

/* Demo status inspector */
.demo-diff-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  margin-bottom: 8px;
  background: var(--surface);
}
.demo-diff-head { display: flex; flex-direction: column; gap: 2px; margin-bottom: 4px; }
.demo-diff-meta { margin-bottom: 6px; }
.demo-diff-actions { display: flex; gap: 6px; }

/* Local data manager */
.local-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  margin-bottom: 8px;
  background: var(--surface);
}
.local-head { display: flex; flex-direction: column; gap: 2px; margin-bottom: 4px; }
.local-preview {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  background: var(--bg);
  padding: 4px 6px;
  border-radius: 4px;
  margin: 4px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.local-actions { display: flex; gap: 6px; margin-top: 6px; }

/* Data quality inspector */
.quality-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  margin-bottom: 8px;
  background: var(--surface);
}
.quality-item.quality-error { border-left: 3px solid #dc2626; }
.quality-item.quality-warning { border-left: 3px solid #d97706; }
.quality-head { display: flex; flex-direction: column; gap: 2px; margin-bottom: 4px; }
.quality-issues { margin: 4px 0 6px; padding-left: 18px; font-size: 13px; }
.quality-issues li { margin: 2px 0; }
.quality-error { color: #dc2626; }
.quality-warning { color: #d97706; }
.quality-info { color: var(--muted); }
.quality-sev { font-weight: 700; font-size: 11px; text-transform: uppercase; margin-right: 4px; }
.quality-actions { display: flex; gap: 6px; }

/* Filter presets */
.preset-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  margin-bottom: 8px;
  background: var(--surface);
}
.preset-head { display: flex; flex-direction: column; gap: 2px; margin-bottom: 4px; }
.preset-summary { margin: 4px 0 6px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.preset-actions { display: flex; gap: 6px; }

/* Private notes */
.listing-note {
  margin: 0.5rem 0;
  padding: 0.5rem 0.75rem;
  background: var(--bg-elevated, rgba(0,0,0,0.04));
  border-left: 3px solid var(--accent, #2d6a4f);
  border-radius: 4px;
  font-size: 0.9rem;
}
.notes-list .note-item {
  padding: 0.75rem 0;
  border-bottom: 1px solid var(--border, #ddd);
}
.notes-list .note-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.5rem;
}
.notes-list .note-preview {
  margin: 0.35rem 0;
  font-size: 0.9rem;
  white-space: pre-wrap;
}
.notes-list .note-actions {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}

/* Listing tags */
.listing-tags {
  margin: 0.5rem 0;
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}
.tag-pill {
  display: inline-block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.15rem 0.55rem;
  font-size: 0.75rem;
  color: var(--fg);
}
.tags-list .tag-item {
  padding: 0.75rem 0;
  border-bottom: 1px solid var(--border);
}
.tags-list .tag-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.5rem;
  margin-bottom: 0.35rem;
}
.tags-list .tag-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-bottom: 0.5rem;
}
.tags-list .tag-actions {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}

/* Listing collections */
.listing-collections {
  margin: 0.5rem 0;
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}
.collection-pill {
  display: inline-block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.15rem 0.55rem;
  font-size: 0.75rem;
  color: var(--fg);
}
.collections-list .collection-item {
  padding: 0.75rem 0;
  border-bottom: 1px solid var(--border);
}
.collections-list .collection-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.5rem;
  margin-bottom: 0.35rem;
}
.collections-list .collection-preview {
  margin-bottom: 0.5rem;
}
.collections-list .collection-actions {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}
.collection-create {
  display: flex;
  gap: 0.5rem;
  margin: 0.75rem 0 1rem;
}

/* Search history */
.history-list .history-item {
  padding: 0.75rem 0;
  border-bottom: 1px solid var(--border);
}
.history-list .history-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.5rem;
  margin-bottom: 0.35rem;
}
.history-list .history-actions {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}

/* Saved map views */
.mapviews-list .mapview-item {
  padding: 0.75rem 0;
  border-bottom: 1px solid var(--border);
}
.mapviews-list .mapview-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.5rem;
  margin-bottom: 0.25rem;
}
.mapviews-list .mapview-meta {
  margin-bottom: 0.5rem;
  font-size: 0.8rem;
}
.mapviews-list .mapview-actions {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}
.mapview-create {
  display: flex;
  gap: 0.5rem;
  margin: 0.75rem 0 1rem;
}
