:root {
  --bg: #f2f2ef; --surface: #e8e7e2; --border: #d8d7d0;
  --text: #171716; --text2: #5f5e5a; --accent: #2a78d6; --accent-bg: #dfeafa;
  --zebra: #ececea; --shadow: 0 1px 2px rgba(23,23,22,0.08);
  --success-bg: #e0f0e4; --success-text: #1e4620;
  --danger-bg: #fbe6e4; --danger-text: #7a1f1f;
  --warning-bg: #fbf1d4; --warning-text: #7a5b00;
  --info-text: #0c447c;
  /* Separate from --accent-bg (badges/flash-info) so the body gradient can be
     tuned for visibility on its own without making those UI accents more vivid. */
  --gradient-glow: #cfe3fa;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14161a; --surface: #1c1f24; --border: #2c2f36;
    --text: #e9eaec; --text2: #9a9da4; --accent: #5b9df0; --accent-bg: #1c3450;
    --zebra: #191b20; --shadow: 0 1px 2px rgba(0,0,0,0.4);
    --success-bg: #16321f; --success-text: #7fd99a;
    --danger-bg: #3a1a1a; --danger-text: #f28b82;
    --warning-bg: #3a2f0f; --warning-text: #f5c451;
    --info-text: #8ec3f5;
    --gradient-glow: #1c3450;
  }
}
:root[data-theme="dark"] {
  --bg: #14161a; --surface: #1c1f24; --border: #2c2f36;
  --text: #e9eaec; --text2: #9a9da4; --accent: #5b9df0; --accent-bg: #1c3450;
  --zebra: #191b20; --shadow: 0 1px 2px rgba(0,0,0,0.4);
  --success-bg: #16321f; --success-text: #7fd99a;
  --danger-bg: #3a1a1a; --danger-text: #f28b82;
  --warning-bg: #3a2f0f; --warning-text: #f5c451;
  --info-text: #8ec3f5;
  --gradient-glow: #1c3450;
}

* { box-sizing: border-box; }
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif;
  background: radial-gradient(circle at 50% -10%, var(--gradient-glow) 0%, var(--bg) 60%);
  background-attachment: fixed; color: var(--text); margin: 0; min-height: 100vh;
  display: flex; flex-direction: column;
  transition: background-color 0.15s ease, color 0.15s ease; }
a { color: var(--accent); }
.topnav { display: flex; align-items: center; gap: 18px; padding: 14px 24px;
  border-bottom: 1px solid var(--border); }
.topnav a { color: var(--text); text-decoration: none; font-size: 14px; }
.topnav a.brand { font-weight: 700; font-size: 16px; }
.topnav .spacer { flex: 1; }
.topnav .whoami { color: var(--text2); font-size: 13px; }
.link-button { background: none; border: none; padding: 0; margin: 0; font: inherit;
  color: var(--accent); text-decoration: underline; cursor: pointer; }
.link-button:hover { filter: none; }
.link-button.danger { color: var(--danger-text); }
.topnav .link-button { color: var(--text); text-decoration: none; font-size: 14px; }
.topnav .link-button:hover { text-decoration: underline; }
.inline-form { display: inline; margin: 0; }
.container { max-width: 960px; margin: 0 auto; padding: 2rem; width: 100%; flex: 1 0 auto; }
.flash { padding: 10px 14px; border-radius: 8px; margin-bottom: 14px; font-size: 14px; }
.flash-success { background: var(--success-bg); color: var(--success-text); }
.flash-danger, .flash-error { background: var(--danger-bg); color: var(--danger-text); }
.flash-warning { background: var(--warning-bg); color: var(--warning-text); }
.flash-info { background: var(--accent-bg); color: var(--info-text); }
form .field { margin-bottom: 14px; }
label { display: block; font-size: 13px; color: var(--text2); margin-bottom: 4px; }
input[type=text], input[type=email], input[type=password], input[type=file],
input[type=date], input[type=search], select {
  width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px;
  font-size: 14px; background: var(--bg); color: var(--text); }
button, .btn { background: var(--accent); color: #fff; border: none; border-radius: 8px;
  padding: 10px 16px; font-size: 14px; cursor: pointer; text-decoration: none; display: inline-block;
  white-space: nowrap; flex-shrink: 0; transition: filter 0.1s ease; }
button:hover, .btn:hover { filter: brightness(0.93); }
button.secondary, .btn.secondary { background: var(--surface); color: var(--text); border: 1px solid var(--border); }
button.danger, .btn.danger { background: var(--danger-bg); color: var(--danger-text); border: 1px solid transparent; }
button.danger:hover, .btn.danger:hover { filter: brightness(0.95); }
.btn-sm { padding: 5px 10px; font-size: 12px; border-radius: 6px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; margin-top: 12px; }
th { text-align: left; padding: 8px; border-bottom: 1px solid var(--border); color: var(--text2); font-weight: 600; }
td { padding: 8px; border-bottom: 1px solid var(--border); }
tbody tr:nth-child(even) { background: var(--zebra); }
tbody tr:hover { background: var(--accent-bg); }
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin: 1rem 0; }
.kpi-card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 1rem;
  box-shadow: var(--shadow); }
.kpi-label { font-size: 12px; color: var(--text2); margin: 0 0 4px; text-transform: uppercase; letter-spacing: 0.03em; }
.kpi-value { font-size: 24px; font-weight: 700; margin: 0; }
.pill { display: inline-block; padding: 2px 8px; border-radius: 12px; font-size: 12px; background: var(--surface); }
.pill.complete { background: var(--success-bg); color: var(--success-text); }
.pill.failed { background: var(--danger-bg); color: var(--danger-text); }

.row-actions { display: flex; align-items: center; gap: 6px; }
tr[data-href] { cursor: pointer; }

.page-header { display: flex; align-items: center; justify-content: space-between; padding-bottom: 20px; }
h2 { font-size: 22px; margin: 0 0 4px; }
h3 { font-size: 16px; margin: 36px 0 0; }
h4 { font-size: 14px; color: var(--text2); margin: 28px 0 0; text-transform: uppercase; letter-spacing: 0.03em; }

.table-card { background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
  margin-top: 12px; overflow: hidden; box-shadow: var(--shadow); }
.table-card-header { display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 10px 14px; background: var(--surface); border-bottom: 1px solid var(--border); }
.table-card-header h3, .table-card-header h4 { margin: 0; }
.table-card-filters { display: flex; align-items: center; gap: 8px; }
.table-card-filters input.table-filter { width: 200px; flex-shrink: 0; padding: 6px 10px; font-size: 13px; }
.table-card-filters select.table-supplier-filter { width: 170px; flex-shrink: 0; padding: 6px 10px; font-size: 13px; }
.filter-autocomplete-list { position: absolute; z-index: 20; margin: 4px 0 0; padding: 4px 0; list-style: none;
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow);
  max-height: 220px; overflow: auto; }
.filter-autocomplete-list li { padding: 6px 10px; font-size: 13px; cursor: pointer; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; }
.filter-autocomplete-list li:hover, .filter-autocomplete-list li.filter-autocomplete-active { background: var(--accent-bg); }
.table-scroll { overflow: auto; max-height: 420px; }
.table-card table { margin-top: 0; }
.table-card table:last-child tr:last-child td { border-bottom: none; }
.table-card thead th { position: sticky; top: 0; background: var(--surface); cursor: pointer;
  user-select: none; white-space: nowrap; }
.table-card thead th:hover { color: var(--text); }
.table-card thead th.sorted-asc::after { content: " \25B2"; font-size: 10px; }
.table-card thead th.sorted-desc::after { content: " \25BC"; font-size: 10px; }
.table-empty { padding: 14px; color: var(--text2); font-size: 13px; }

.chart-card { background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
  margin-top: 12px; padding: 16px; box-shadow: var(--shadow); }
.chart-box { position: relative; width: 100%; height: 360px; }
.badge { background: var(--accent-bg); color: var(--info-text); font-size: 13px; padding: 8px 12px;
  border-radius: 8px; display: inline-block; margin-top: 8px; }
.row { display: flex; align-items: center; gap: 10px; }
.row label { font-size: 14px; color: var(--text2); }
.row select { width: auto; min-width: 200px; }

.theme-toggle { background: transparent; border: 1px solid var(--border); color: var(--text);
  border-radius: 8px; padding: 5px 10px; font-size: 12px; }
.theme-toggle:hover { filter: none; background: var(--surface); }

/* Auth pages (login/register/password reset) - centered card on the shared
   body background, leaving room for real branding to land later. Theme here
   follows the browser's preference only - the manual toggle only appears
   once logged in (see base.html), though a choice made there still applies
   here too since it's read from the same stored preference. */
.auth-wrap { flex: 1 0 auto; width: 100%; display: flex; align-items: center;
  justify-content: center; padding: 2rem; box-sizing: border-box; }
.auth-card { width: 100%; max-width: 400px; background: var(--bg); border: 1px solid var(--border);
  border-radius: 16px; padding: 2.5rem 2rem; box-shadow: 0 12px 32px rgba(23,23,22,0.10); text-align: center; }
.auth-logo { width: 52px; height: 52px; margin: 0 auto 12px; display: flex; align-items: center;
  justify-content: center; background: var(--accent); color: #fff; border-radius: 14px; }
.auth-brand { font-size: 13px; font-weight: 600; color: var(--text2); text-transform: uppercase;
  letter-spacing: 0.04em; margin: 0 0 28px; }
.auth-card h2 { font-size: 18px; margin: 0 0 20px; text-align: center; }
.auth-card form { text-align: left; }
.auth-card .flash { text-align: left; }
.auth-card p { font-size: 13px; color: var(--text2); text-align: center; margin: 14px 0 0; }
.auth-card button[type=submit] { width: 100%; margin-top: 4px; }
.auth-card .errorlist { list-style: none; padding: 10px 12px; margin: 0 0 14px; border-radius: 8px;
  background: var(--danger-bg); color: var(--danger-text); font-size: 13px; }
.auth-card ul { font-size: 12px; color: var(--text2); padding-left: 20px; margin: 4px 0 14px; }

.site-footer { flex-shrink: 0; padding: 20px 24px; border-top: 1px solid var(--border); text-align: center; }
.site-footer-links { display: flex; justify-content: center; align-items: center; gap: 16px; flex-wrap: wrap; }
.site-footer-links a { color: var(--text2); font-size: 12px; text-decoration: none; }
.site-footer-links a:hover { color: var(--text); text-decoration: underline; }

/* Global search */
.global-search { position: relative; margin-bottom: 16px; }
.global-search-box { position: relative; display: flex; align-items: center; }
.global-search-input { padding: 10px 40px 10px 14px; font-size: 15px; }
.global-search-clear { position: absolute; right: 8px; background: none; border: none; padding: 4px 8px;
  font-size: 18px; line-height: 1; color: var(--text2); cursor: pointer; border-radius: 6px; }
.global-search-clear:hover { background: var(--surface); filter: none; }
.global-search-suggestions { position: absolute; top: calc(100% + 4px); left: 0; right: 0; z-index: 20;
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow);
  max-height: 360px; overflow: auto; list-style: none; margin: 0; padding: 6px 0; }
.gs-suggestion-group { padding: 6px 14px 4px; font-size: 11px; font-weight: 600; color: var(--text2);
  text-transform: uppercase; letter-spacing: 0.03em; }
.gs-suggestion { display: flex; align-items: baseline; justify-content: space-between; gap: 10px;
  padding: 7px 14px; cursor: pointer; font-size: 13px; }
.gs-suggestion:hover, .gs-suggestion--active { background: var(--accent-bg); }
.gs-suggestion-title { color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.gs-suggestion-meta { color: var(--text2); font-size: 12px; white-space: nowrap; flex-shrink: 0; }

.search-results-panel { background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: var(--shadow); margin-bottom: 16px; }
.search-results-header { display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 10px 14px; background: var(--surface); border-bottom: 1px solid var(--border); }
.search-results-header h4 { margin: 0; text-transform: none; letter-spacing: normal; font-size: 14px; }
.search-results-body { padding: 6px 14px 14px; max-height: 420px; overflow: auto; }
.search-results-group { margin-top: 12px; }
.search-results-group-label { font-size: 11px; font-weight: 600; color: var(--text2);
  text-transform: uppercase; letter-spacing: 0.03em; margin-bottom: 4px; }
.search-result-row { padding: 8px 10px; border-radius: 8px; cursor: pointer; }
.search-result-row:hover, .search-result-row:focus { background: var(--accent-bg); outline: none; }
.search-result-title { font-size: 13px; color: var(--text); }
.search-result-sub { font-size: 12px; color: var(--text2); }
.search-result-metrics { font-size: 12px; color: var(--text2); margin-top: 2px; }
.search-results-empty { color: var(--text2); padding: 12px; font-size: 13px; }

@keyframes search-row-flash {
  from { background: var(--accent-bg); }
  to { background: transparent; }
}
tr.search-highlight { animation: search-row-flash 2s ease-out; }
tr.search-highlight td:first-child { box-shadow: inset 3px 0 0 var(--accent); }

/* Print: force a plain light palette regardless of the active theme (dark-mode
   light-on-dark text would otherwise be invisible on white paper), hide
   screen-only chrome/controls, and let tables print in full instead of being
   clipped to their on-screen scroll height. !important is deliberate here -
   print output must reliably override arbitrary screen-context styles. */
@media print {
  :root {
    --bg: #ffffff !important; --surface: #ffffff !important; --border: #999999 !important;
    --text: #000000 !important; --text2: #333333 !important; --accent: #000000 !important; --accent-bg: #eeeeee !important;
    --zebra: #f0f0f0 !important; --shadow: none !important;
    --success-bg: #ffffff !important; --success-text: #000000 !important;
    --danger-bg: #ffffff !important; --danger-text: #000000 !important;
    --warning-bg: #ffffff !important; --warning-text: #000000 !important;
    --info-text: #000000 !important;
    --gradient-glow: #ffffff !important;
  }
  @page { margin: 1.5cm; }
  body { background: #fff !important; }

  .topnav, .site-footer, .global-search, .search-results-panel,
  .table-filter, .table-supplier-filter, [data-export-csv],
  .page-header .row { display: none !important; }

  .container { max-width: 100%; padding: 0; }
  .chart-card, .kpi-card, .kpi-grid, .badge { break-inside: avoid; }
  .table-scroll { overflow: visible !important; max-height: none !important; }
  table { font-size: 11px; }
  thead { display: table-header-group; }
  tr { break-inside: avoid; }
  h3, h4, .badge { break-after: avoid; }

  /* A heading (or the "Top performer" badge, or a table's own header bar -
     e.g. the supplier-drilldown label+select, which stays visible in print
     for context) must never end up alone at the bottom of a page with the
     table/chart it introduces starting fresh on the next - that's a stray
     title. break-after on the element before doesn't cover every case here
     since some sections have a badge *between* the heading and its table, so
     also pin the table/chart header bar itself to whatever follows it. */
  .table-card-header { break-inside: avoid; break-after: avoid; }

  /* break-after:avoid on a heading is only a soft hint - if honoring it would
     require pushing a large chunk of blank space, real print engines will
     still strand the heading alone rather than waste the space (confirmed:
     "Top 10 by profit" and "Catalog & stock overview" both did this in an
     actual rendered PDF despite the heading-only rule above). For sections
     whose content is always small and bounded (a fixed top-10/top-20 list, a
     5-card KPI grid, a single chart) the template wraps heading+content in
     .print-keep so the whole group is forced to move together - safe here
     specifically because it's never taller than a page. Sections with
     unbounded row counts (the true-dead-stock/restock-priority/etc stock
     tables, and the supplier-performance table itself) are deliberately NOT
     wrapped this way, since forcing an arbitrarily long table to stay intact
     would fight its own necessary pagination - those rely on the lighter
     table-card-header rule above instead, which already keeps their heading
     attached to at least the header bar and first row. */
  .print-keep { break-inside: avoid; }

  /* Long tables spanning multiple pages must never show a stray half-drawn
     border/shadow/rounded-corner at the page fragment boundary - flatten the
     card chrome so a split card reads as plain continuous content, and make
     sure the (position:sticky) column headers don't fight the browser's
     native repeating-thead-per-page mechanism. */
  .table-card { border-radius: 0; box-decoration-break: slice; -webkit-box-decoration-break: slice; }
  .table-card thead th { position: static !important; }
}
