* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: #f6f7f9;
  color: #222;
  line-height: 1.4;
}

/* topbar */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.6rem 1rem;
  background: #1f2937;
  color: #fff;
  gap: 1rem;
  flex-wrap: wrap;
}
.topbar a { color: #fff; text-decoration: none; }
.topbar .brand a { font-weight: 600; font-size: 1.05rem; }
.topbar nav.primary { display: flex; gap: 1.25rem; flex: 1; padding-left: 1rem; }
.topbar nav.primary a { opacity: 0.9; }
.topbar nav.primary a:hover { opacity: 1; text-decoration: underline; }
.topbar nav.account { display: flex; gap: 0.75rem; align-items: center; }
.topbar nav.account .user { font-size: 0.85rem; opacity: 0.85; }
.topbar form button {
  background: transparent;
  border: 1px solid #fff3;
  color: #fff;
  padding: 0.3rem 0.7rem;
  font-size: 0.85rem;
}
.topbar form button:hover { background: #ffffff14; }

main { max-width: 1100px; margin: 1.5rem auto; padding: 0 1rem; }
h1 { margin-top: 0; }
h1 small.muted { font-weight: 400; font-size: 0.7em; }

/* card / forms */
.card {
  background: #fff;
  padding: 1.5rem;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  max-width: 480px;
  margin: 1.5rem auto;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}
form label { display: block; margin: 0.75rem 0; font-size: 0.9rem; color: #374151; }
form input[type="text"], form input[type="search"], form input[type="password"], form input[type="number"], form input:not([type]), form textarea, form select {
  width: 100%;
  padding: 0.5rem;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: 0.95rem;
  margin-top: 0.25rem;
  font-family: inherit;
}
form input:focus, form textarea:focus, form select:focus { outline: 2px solid #2563eb; outline-offset: 1px; }
form input:disabled { background: #f3f4f6; color: #6b7280; }
form label.checkbox { display: flex; align-items: center; gap: 0.5rem; }
form label.checkbox input { width: auto; margin: 0; }
form .actions { display: flex; gap: 0.75rem; align-items: center; margin-top: 1rem; }

button {
  background: #2563eb;
  color: #fff;
  border: 0;
  padding: 0.55rem 1rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.95rem;
}
button:hover { background: #1d4ed8; }
.btn-link { color: #2563eb; text-decoration: none; padding: 0.5rem 0.5rem; }
.btn-link:hover { text-decoration: underline; }

.error { color: #b91c1c; background: #fef2f2; padding: 0.4rem 0.6rem; border-radius: 4px; display: inline-block; }
.success { color: #166534; background: #f0fdf4; padding: 0.4rem 0.6rem; border-radius: 4px; }
/* Default (light mode) muted text — bumped one shade darker for readability
   on white. Dark-mode overrides below lighten everything to compensate. */
.muted { color: #4b5563; }
.muted small { color: #6b7280; }
.inline { display: inline; }

/* admin home cards */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 1rem; margin: 1.5rem 0; }
.kpi {
  display: block;
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 1rem;
  text-decoration: none;
  color: inherit;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}
a.kpi:hover { border-color: #2563eb; }
.kpi-label { font-size: 0.8rem; color: #4b5563; text-transform: uppercase; letter-spacing: 0.05em; }
.kpi-value { font-size: 1.6rem; font-weight: 600; margin-top: 0.25rem; }
.kpi-sub { color: #4b5563; font-size: 0.85rem; margin-top: 0.4rem; }

/* tables */
.data {
  width: 100%;
  border-collapse: collapse;
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  overflow: hidden;
  margin-top: 1rem;
  font-size: 0.9rem;
}
.data thead th {
  text-align: left;
  background: #f3f4f6;
  padding: 0.5rem 0.75rem;
  font-weight: 600;
  border-bottom: 1px solid #e5e7eb;
  white-space: nowrap;
}
.data tbody td { padding: 0.45rem 0.75rem; border-bottom: 1px solid #f0f1f4; vertical-align: top; }
/* Tighter density variant for big admin tables. */
.data.dense thead th { padding: 0.35rem 0.55rem; }
.data.dense tbody td { padding: 0.3rem 0.55rem; }
.data.dense { font-size: 0.85rem; }
.data.dense td.check, .data.dense th.check { width: 28px; padding-left: 0.5rem; padding-right: 0.25rem; }
/* Sticky header: stays visible while scrolling long tables. */
.data.sticky-head thead th { position: sticky; top: 0; z-index: 1; box-shadow: 0 1px 0 #e5e7eb; }
.data thead th a { color: inherit; text-decoration: none; display: inline-block; }
.data thead th a:hover { color: #2563eb; }
/* bulk actions bar below the table */
.bulk-actions {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  margin: 0.75rem 0;
}
.bulk-actions button {
  padding: 0.35rem 0.85rem;
  font-size: 0.85rem;
  background: #fff;
  color: #374151;
  border: 1px solid #d1d5db;
}
.bulk-actions button:hover { background: #f3f4f6; }

/* ── Mobile breakpoints ────────────────────────────────────────────────── */
@media (max-width: 720px) {
  main { margin: 1rem auto; padding: 0 0.6rem; }
  h1 { font-size: 1.4rem; }
  .topbar { padding: 0.5rem 0.6rem; gap: 0.4rem; row-gap: 0.5rem; }
  .topbar nav.primary { padding-left: 0; gap: 0.7rem; font-size: 0.85rem; flex-wrap: wrap; }
  .topbar nav.account .user { display: none; }
  .topbar form button { padding: 0.25rem 0.55rem; font-size: 0.8rem; }
  /* Card grid collapses to a single column. */
  .card-grid { grid-template-columns: 1fr; }
  .price-card .card-prices { grid-template-columns: 1fr 1fr; row-gap: 0.5rem; }
  .stats-row { grid-template-columns: repeat(2, 1fr); }
  /* Tables get smaller text and horizontal scroll. */
  .data { font-size: 0.78rem; }
  .table-scroll { overflow-x: auto; }
  /* Filter rows wrap by default; tighten the gap on mobile. */
  .filter-bar { gap: 0.45rem; }
  .filter-bar input[type="search"] { min-width: 160px; max-width: 100%; }
  .commodity-header h1 { font-size: 1.2rem; flex-wrap: wrap; }
  .commodity-title .commodity-desc { white-space: normal; }
  .control-row { gap: 0.4rem; }
  .range-nav a, .currency-toggle a { padding: 0.3rem 0.55rem; font-size: 0.8rem; }
}

/* ── Dark mode (prefers-color-scheme) ──────────────────────────────────── */
@media (prefers-color-scheme: dark) {
  /* color-scheme tells browsers to render native form controls (select
     dropdowns, date pickers, scrollbars) in their dark variant. */
  html { color-scheme: dark; }
  body { background: #0f172a; color: #e5e7eb; }
  .topbar { background: #020617; }
  .topbar a { color: #e5e7eb; }
  .topbar form button { color: #e5e7eb; border-color: #ffffff22; }
  .topbar form button:hover { background: #ffffff14; }

  .card,
  .stat,
  .price-card,
  .kpi {
    background: #1e293b;
    border-color: #334155;
    box-shadow: 0 1px 2px rgba(0,0,0,0.4);
  }
  .stat.highlight { background: #052e1f; border-color: #15803d; }
  .stat-value.pos { color: #4ade80; }
  .stat-value.neg { color: #f87171; }

  .data { background: #1e293b; border-color: #334155; }
  .data thead th { background: #0f172a; border-bottom-color: #334155; color: #e5e7eb; }
  .data tbody td { border-bottom-color: #334155; }
  .data tbody tr.disabled { color: #64748b; }
  .data code { background: #0f172a; color: #e5e7eb; }

  /* form controls */
  form input, form textarea, form select,
  .filter-bar input, .filter-bar select {
    background: #0f172a;
    color: #e5e7eb;
    border-color: #334155;
  }
  form input::placeholder { color: #64748b; }
  form input:disabled { background: #1e293b; color: #64748b; }

  button { background: #2563eb; }
  button:hover { background: #3b82f6; }
  .bulk-actions button { background: #1e293b; color: #e5e7eb; border-color: #334155; }
  .bulk-actions button:hover { background: #334155; }
  button.danger { background: #b91c1c; }
  button.link-danger { color: #f87171; }

  .range-nav a, .currency-toggle a {
    background: #1e293b; border-color: #334155; color: #cbd5e1;
  }
  .range-nav a:hover, .currency-toggle a:hover { background: #334155; }
  .range-nav a.active, .currency-toggle a.active { background: #2563eb; border-color: #2563eb; color: #fff; }

  .pill { background: #334155; color: #e5e7eb; }
  .badge.ok { background: #052e1f; color: #4ade80; }
  .badge.run { background: #1e3a8a; color: #93c5fd; }
  .badge.err { background: #450a0a; color: #fca5a5; }

  .error { color: #fca5a5; background: #450a0a; }
  .success { color: #86efac; background: #052e1f; }
  /* Lighten every grey-on-grey text by one or two Tailwind shades for
     contrast on the dark slate background. */
  .muted { color: #cbd5e1; }
  .muted small { color: #94a3b8; }
  .muted strong { color: #f1f5f9; }
  .stat-label, .kpi-label { color: #94a3b8; }
  .stat-value small, .kpi-sub, .export-bar { color: #cbd5e1; }
  .price-card .price-zar .price-value { color: #cbd5e1; }
  .price-card .card-name, .price-card .card-desc { color: #cbd5e1; }
  .price-card .small { color: #94a3b8; }
  .commodity-title .commodity-desc { color: #cbd5e1; }
  .commodity-header .meta { color: #cbd5e1; }
  form label,
  .filter-bar label,
  .profile-pick label { color: #cbd5e1; }
  h1 small.muted, h2 small.muted { color: #94a3b8; }
  /* Force <select> options to render in the dark palette regardless of
     browser defaults — needed especially for the dashboard profile picker. */
  select option { background: #0f172a; color: #e5e7eb; }

  /* Chart canvas wrap */
  .chart-wrap { background: #1e293b; border-color: #334155; }

  /* Dropzone */
  .dropzone { background: #0f172a; border-color: #475569; color: #94a3b8; }
  .dropzone.drag { background: #1e3a8a; border-color: #93c5fd; }
}
.data tbody tr:last-child td { border-bottom: 0; }
.data tbody tr.disabled { color: #9ca3af; }
.data .num { text-align: right; font-variant-numeric: tabular-nums; }
.data code { background: #f3f4f6; padding: 1px 4px; border-radius: 3px; }

/* filter bar */
.filter-bar { display: flex; gap: 0.75rem; align-items: center; margin: 1rem 0; flex-wrap: wrap; }
.filter-bar input[type="search"] { flex: 1; min-width: 200px; max-width: 360px; padding: 0.4rem 0.6rem; border: 1px solid #ccc; border-radius: 4px; }
.filter-bar select { padding: 0.4rem 0.6rem; border: 1px solid #ccc; border-radius: 4px; background: #fff; }
/* Inline label/input pairs so the bar stays on one row instead of letting
   the global `form label { display: block }` rule stack them. */
.filter-bar label {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  margin: 0;
  font-size: 0.85rem;
  color: #374151;
  white-space: nowrap;
}
.filter-bar input[type="date"],
.filter-bar input[type="number"],
.filter-bar input[type="text"]:not([style]) {
  width: auto;
  margin: 0;
  padding: 0.35rem 0.5rem;
  font-size: 0.9rem;
  border: 1px solid #ccc;
  border-radius: 4px;
}
.filter-bar input[type="date"]   { width: 145px; }
.filter-bar input[type="number"] { width: 80px; }
.filter-bar button {
  padding: 0.4rem 0.95rem;
  font-size: 0.9rem;
  line-height: 1.4;
}

/* pagination */
.pagination { display: flex; gap: 1rem; align-items: center; justify-content: center; margin: 1rem 0; }
.pagination a { color: #2563eb; text-decoration: none; padding: 0.3rem 0.6rem; }
.pagination a:hover { text-decoration: underline; }

/* badges */
.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 0.75rem; font-weight: 600; }
.badge.ok { background: #dcfce7; color: #166534; }
.badge.run { background: #dbeafe; color: #1e40af; }
.badge.err { background: #fee2e2; color: #991b1b; }

/* danger / link-danger buttons */
button.danger { background: #b91c1c; }
button.danger:hover { background: #991b1b; }
button.link-danger {
  background: transparent;
  color: #b91c1c;
  padding: 0;
  font-size: 0.9rem;
  border: 0;
  cursor: pointer;
  text-decoration: underline;
}
button.link-danger:hover { color: #7f1d1d; background: transparent; }

/* commodity view */
.commodity-header h1 { display: flex; align-items: baseline; gap: 0.75rem; flex-wrap: wrap; margin-bottom: 0.25rem; }
.commodity-title { min-width: 0; }
.commodity-title code { flex-shrink: 0; }
.commodity-title .commodity-desc {
  font-size: 1rem;
  font-weight: 400;
  color: #4b5563;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.commodity-header .meta { display: flex; gap: 1rem; flex-wrap: wrap; font-size: 0.9rem; margin: 0.5rem 0; }
.pill { background: #e5e7eb; padding: 1px 8px; border-radius: 999px; font-size: 0.8rem; font-weight: 600; }
.control-row { display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: center; margin: 1rem 0 0.5rem; }
.range-nav, .currency-toggle { display: flex; gap: 0.25rem; }
.range-nav a, .currency-toggle a {
  display: inline-block;
  padding: 0.35rem 0.75rem;
  background: #fff;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  text-decoration: none;
  color: #374151;
  font-size: 0.85rem;
}
.range-nav a:hover, .currency-toggle a:hover { background: #f3f4f6; }
.range-nav a.active, .currency-toggle a.active { background: #2563eb; border-color: #2563eb; color: #fff; }
.currency-toggle a { font-weight: 600; }
.help-icon {
  background: transparent;
  border: 1px solid #d1d5db;
  color: #6b7280;
  width: 1.65rem;
  height: 1.65rem;
  border-radius: 999px;
  font-style: italic;
  font-weight: 700;
  font-size: 0.85rem;
  padding: 0;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.help-icon:hover { background: #f3f4f6; color: #374151; }
.help-popover {
  position: absolute;
  z-index: 10;
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 0.85rem 1rem;
  box-shadow: 0 6px 24px rgba(0,0,0,0.12);
  font-size: 0.85rem;
  margin-top: 0.4rem;
  max-width: 320px;
}
.help-popover ul { margin: 0.4rem 0 0 0; padding-left: 0; list-style: none; }
.help-popover li { margin: 0.25rem 0; color: #374151; }
.help-popover kbd {
  background: #f3f4f6;
  border: 1px solid #d1d5db;
  border-radius: 3px;
  padding: 1px 6px;
  font-size: 0.78rem;
  font-family: monospace;
}

/* stats row */
.stats-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 0.75rem; margin: 1rem 0; }
.stat { background: #fff; border: 1px solid #e5e7eb; border-radius: 8px; padding: 0.75rem 1rem; }
.stat.highlight { border-color: #10b981; background: #ecfdf5; }
.stat-label { font-size: 0.75rem; color: #4b5563; text-transform: uppercase; letter-spacing: 0.05em; }
.stat-value { font-size: 1.25rem; font-weight: 600; margin-top: 0.2rem; }
.stat-value.pos { color: #166534; }
.stat-value.neg { color: #991b1b; }
.stat-value small { font-size: 0.65rem; font-weight: 400; color: #4b5563; }

/* chart */
.chart-wrap {
  position: relative;
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 0.75rem;
  height: 420px;
  margin: 1rem 0;
}
.export-bar { color: #4b5563; font-size: 0.9rem; margin-top: 0.5rem; }
.export-bar a { color: #2563eb; text-decoration: none; }
.export-bar a:hover { text-decoration: underline; }

/* dashboard bar */
.dashboard-bar { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.profile-pick { display: flex; gap: 0.75rem; align-items: center; }
.profile-pick label { display: flex; gap: 0.4rem; align-items: center; font-size: 0.9rem; color: #374151; }
.profile-pick select { padding: 0.4rem 0.6rem; border: 1px solid #ccc; border-radius: 4px; background: #fff; font-size: 0.95rem; }

/* card grid + price card */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 1rem;
}
.price-card {
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  box-shadow: 0 1px 2px rgba(0,0,0,0.04);
}
.price-card .card-head { display: flex; flex-direction: column; gap: 0.2rem; }
.price-card .card-code { display: flex; align-items: center; gap: 0.5rem; min-width: 0; }
.price-card .card-code a { text-decoration: none; flex-shrink: 0; }
.price-card .card-code code { font-size: 1.05rem; font-weight: 600; background: #f3f4f6; padding: 1px 6px; border-radius: 3px; }
.price-card .card-code .pill { flex-shrink: 0; }
.price-card .card-desc {
  flex: 1;
  min-width: 0;
  font-size: 0.85rem;
  color: #374151;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.price-card .card-prices { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 0.6rem; align-items: end; }
.price-card .price-main .price-value { font-size: 1.4rem; font-weight: 600; }
.price-card .price-zar .price-value { font-size: 1.05rem; font-weight: 600; color: #4b5563; }
.price-card .price-delta .price-value { font-size: 0.95rem; font-weight: 600; }
.price-card .price-delta.pos .price-value { color: #166534; }
.price-card .price-delta.neg .price-value { color: #991b1b; }
.price-card .price-value small { font-weight: 400; font-size: 0.65rem; color: #6b7280; margin-left: 0.25rem; }
.price-card .small { font-size: 0.7rem; }
.price-card .spark-wrap {
  height: 80px;
  position: relative;
}
.price-card .range-row { display: flex; gap: 0.25rem; flex-wrap: wrap; }
.price-card .range-btn {
  background: #fff;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  padding: 0.2rem 0.55rem;
  font-size: 0.78rem;
  cursor: pointer;
  color: #374151;
}
.price-card .range-btn:hover { background: #f3f4f6; }
.price-card .range-btn.active { background: #2563eb; border-color: #2563eb; color: #fff; }

/* import dropzone */
.dropzone {
  border: 2px dashed #c7d2fe;
  border-radius: 8px;
  padding: 1.5rem;
  text-align: center;
  background: #f9fafb;
  transition: background 0.15s, border-color 0.15s;
}
.dropzone.drag { background: #eef2ff; border-color: #6366f1; }
.dropzone p { margin: 0.25rem 0; }

/* misc small links/buttons used by profile editor */
button.link-button {
  background: transparent;
  color: #2563eb;
  border: 0;
  padding: 0;
  font-size: 1rem;
  cursor: pointer;
  text-decoration: none;
}
button.link-button:hover { text-decoration: underline; background: transparent; }

/* ── Dark mode override tail ──────────────────────────────────────────────
   This block intentionally lives at the END of the file. The earlier
   `@media (prefers-color-scheme: dark)` block was being overridden by
   late-defined light-mode rules (.profile-pick label, .filter-bar label,
   etc.) that have identical specificity but come later in source order, so
   they win the cascade tie-breaker. Repeating the dark-mode rules here —
   AFTER every light-mode rule — fixes the cascade. */
@media (prefers-color-scheme: dark) {
  html { color-scheme: dark; }
  body { background: #0f172a; color: #e5e7eb; }

  .profile-pick label,
  .filter-bar label,
  form label { color: #cbd5e1; }
  .profile-pick select,
  .filter-bar select,
  .filter-bar input[type="search"],
  .filter-bar input[type="date"],
  .filter-bar input[type="number"],
  .filter-bar input[type="text"]:not([style]),
  form input,
  form textarea,
  form select {
    background: #0f172a;
    color: #e5e7eb;
    border-color: #334155;
  }
  form input::placeholder { color: #64748b; }
  form input:disabled { background: #1e293b; color: #64748b; }

  /* Card / table greys overrideable by late rules */
  .stat-label, .kpi-label { color: #94a3b8; }
  .kpi-sub, .export-bar, .stat-value small { color: #cbd5e1; }
  .price-card .price-zar .price-value { color: #cbd5e1; }
  .price-card .card-name, .price-card .card-desc { color: #cbd5e1; }
  .commodity-title .commodity-desc, .commodity-header .meta { color: #cbd5e1; }
  select option { background: #0f172a; color: #e5e7eb; }

  /* Card backgrounds need re-asserting here too — `.stat { background: #fff }`
     is defined later in the file and otherwise wins the cascade. */
  .card, .stat, .price-card, .kpi {
    background: #1e293b;
    border-color: #334155;
    box-shadow: 0 1px 2px rgba(0,0,0,0.4);
  }
  .stat.highlight { background: #052e1f; border-color: #15803d; }
  .stat-value, .kpi-value { color: #e5e7eb; }
  /* Chart canvas wrap — same problem, late-defined */
  .chart-wrap { background: #1e293b; border-color: #334155; }
  /* Tables */
  .data { background: #1e293b; border-color: #334155; }
  .data thead th { background: #0f172a; border-bottom-color: #334155; color: #e5e7eb; }
  .data tbody td { border-bottom-color: #334155; }
  .data code { background: #0f172a; color: #e5e7eb; }
  .data tbody tr.disabled { color: #64748b; }
  /* Pills (category badges on dashboard cards + commodity header) — late
     light-mode rule (`.pill { background: #e5e7eb }`) made these light-on-light
     in dark mode. Re-assert here. */
  .pill { background: #1e3a8a; color: #dbeafe; }
  /* Help icon + popover */
  .help-icon { background: transparent; border-color: #334155; color: #cbd5e1; }
  .help-icon:hover { background: #334155; color: #e5e7eb; }
  .help-popover { background: #1e293b; border-color: #334155; box-shadow: 0 6px 24px rgba(0,0,0,0.5); }
  .help-popover li { color: #cbd5e1; }
  .help-popover kbd { background: #0f172a; border-color: #334155; color: #e5e7eb; }
  /* Badges on the scheduler page */
  .badge.ok { background: #052e1f; color: #4ade80; }
  .badge.run { background: #1e3a8a; color: #93c5fd; }
  .badge.err { background: #450a0a; color: #fca5a5; }
}
