:root {
  --bg: #0f1419;
  --surface: #1a2129;
  --surface-2: #222b35;
  --line: #2d3742;
  --text: #e6edf3;
  --muted: #8b97a3;
  --accent: #3b82f6;
  --accent-2: #60a5fa;
  --good: #22c55e;
  --warn: #f59e0b;
  --bad: #ef4444;
  --chevy: #d1a23a;
  --kia: #c0392b;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0,0,0,.4);
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
a { color: var(--accent-2); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---- Top bar ---- */
.topbar {
  display: flex; align-items: center; gap: 24px;
  padding: 0 20px; height: 56px;
  background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 10;
}
.brand a { color: var(--text); font-weight: 700; font-size: 16px; display: inline-flex; }
.brand-logo { height: 30px; width: auto; display: block; }
.brand-sub { color: var(--muted); font-size: 11px; display: block; margin-top: 1px; }
.login-logo { display: block; height: 46px; width: auto; margin: 0 auto 16px; }
.nav { display: flex; gap: 4px; flex: 1; }
.nav a {
  color: var(--muted); padding: 8px 12px; border-radius: 8px; font-weight: 500;
}
.nav a:hover { color: var(--text); background: var(--surface-2); text-decoration: none; }
.nav a.active { color: var(--text); background: var(--surface-2); }
.user-chip { display: flex; align-items: center; gap: 10px; }
.user-chip .who { font-weight: 600; }
.role-tag {
  font-size: 11px; color: var(--muted); background: var(--surface-2);
  padding: 2px 8px; border-radius: 999px; text-transform: capitalize;
}
.logout-form button {
  background: transparent; border: 1px solid var(--line); color: var(--muted);
  padding: 6px 10px; border-radius: 8px; cursor: pointer; font-size: 12px;
}
.logout-form button:hover { color: var(--text); border-color: var(--muted); }

/* Hamburger toggle — hidden on desktop, shown on mobile via the media query. */
.nav-toggle {
  display: none; margin-left: auto;
  background: transparent; border: 1px solid var(--line); border-radius: 8px;
  width: 40px; height: 34px; cursor: pointer; padding: 0;
  flex-direction: column; align-items: center; justify-content: center; gap: 4px;
}
.nav-toggle:hover { border-color: var(--muted); }
.nav-toggle-bar { display: block; width: 18px; height: 2px; background: var(--text); border-radius: 2px; }

@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; height: auto; min-height: 56px; gap: 0 12px; padding: 8px 14px; }
  .brand { flex: 1; min-width: 0; }
  .nav-toggle { display: flex; }
  /* Collapse the nav + user chip; reveal them stacked when the bar is open. */
  .nav, .user-chip { display: none; flex-basis: 100%; }
  .topbar.open .nav {
    display: flex; flex-direction: column; gap: 2px; padding: 8px 0 4px;
  }
  .topbar.open .nav a { padding: 11px 12px; }
  .topbar.open .user-chip {
    display: flex; flex-wrap: wrap; align-items: center; gap: 10px;
    padding: 10px 0 4px; margin-top: 4px; border-top: 1px solid var(--line);
  }
  .logout-form { margin-left: auto; }
}

.container { max-width: 1340px; margin: 0 auto; padding: 22px 20px 60px; }

/* ---- KPI cards ---- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 18px; }
.kpi {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow);
}
.kpi .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
.kpi .value { font-size: 26px; font-weight: 700; margin-top: 4px; }
.kpi .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.kpi.alert .value { color: var(--warn); }
.kpi.cost .value { color: var(--bad); }

/* ---- Panels ---- */
.panel {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; margin-bottom: 18px; box-shadow: var(--shadow);
}
.panel h2 { margin: 0 0 12px; font-size: 15px; }
.panel-row { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
@media (max-width: 900px) { .panel-row { grid-template-columns: 1fr; } }

/* ---- Aging bucket bars ---- */
.buckets { display: flex; flex-direction: column; gap: 6px; }
.bucket-row {
  display: grid; grid-template-columns: 90px 1fr 170px; align-items: center; gap: 12px;
  padding: 5px 8px; margin: 0 -8px; border-radius: 8px; cursor: pointer;
  color: var(--text); text-decoration: none; border: 1px solid transparent; transition: background .12s, border-color .12s;
}
.bucket-row:hover { background: var(--surface-2); text-decoration: none; }
.bucket-row.active { background: var(--surface-2); border-color: var(--accent); }
.bucket-name { font-weight: 600; }
.bucket-bar { background: var(--surface-2); border-radius: 6px; height: 26px; overflow: hidden; position: relative; }
.bucket-fill { height: 100%; border-radius: 6px 0 0 6px; display: flex; align-items: center; padding-left: 8px; font-size: 12px; font-weight: 600; color: #fff; white-space: nowrap; }
.bucket-meta { text-align: right; color: var(--muted); font-size: 12px; }
.b0 { background: #16a34a; }
.b1 { background: #65a30d; }
.b2 { background: #ca8a04; }
.b3 { background: #ea580c; }
.b4, .b5, .b6 { background: #dc2626; }

/* ---- Filters ---- */
.filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.filters .field { display: flex; flex-direction: column; gap: 4px; }
.filters label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.filters select, .filters input {
  background: var(--surface-2); border: 1px solid var(--line); color: var(--text);
  padding: 7px 9px; border-radius: 8px; font-size: 13px; min-width: 120px;
}
.filters .actions { display: flex; gap: 8px; }
.btn {
  background: var(--accent); color: #fff; border: none; padding: 8px 16px;
  border-radius: 8px; cursor: pointer; font-size: 13px; font-weight: 600;
}
.btn:hover { background: var(--accent-2); }
.btn.secondary { background: var(--surface-2); border: 1px solid var(--line); color: var(--text); }
.btn.ghost { background: transparent; border: 1px solid var(--line); color: var(--muted); }
.btn.danger { background: var(--bad); }

/* ---- Tables ---- */
.table-wrap { overflow: auto; max-height: calc(100vh - 210px); border-radius: var(--radius); border: 1px solid var(--line); }
table.grid { width: 100%; border-collapse: collapse; font-size: 13px; background: var(--surface); }
table.grid th, table.grid td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.grid th { background: var(--surface-2); color: var(--muted); font-weight: 600; position: sticky; top: 0; z-index: 2; }
table.grid th a { color: var(--muted); }
table.grid tr:hover td { background: var(--surface-2); }
table.grid td.num, table.grid th.num { text-align: right; font-variant-numeric: tabular-nums; }
.notes-cell { white-space: normal; color: var(--muted); font-size: 12px; }
.notes-cell .clip {
  max-width: 320px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.4;
  overflow-wrap: anywhere;
  word-break: break-word;
  cursor: default;
}

/* ---- Compact filter bar ---- */
.filter-panel { padding: 14px 16px; }
.filter-bar { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }

/* Dropdown filter (native <details> disclosure, no JS required) */
.dropdown { position: relative; }
.dropdown > summary {
  list-style: none; cursor: pointer; user-select: none;
  display: inline-flex; align-items: center; gap: 9px; white-space: nowrap;
  background: var(--surface-2); border: 1px solid var(--line); color: var(--text);
  padding: 9px 12px; border-radius: 8px; font-size: 13px; font-weight: 500;
}
.dropdown > summary::-webkit-details-marker { display: none; }
.dropdown > summary::after { content: '▾'; color: var(--muted); font-size: 10px; margin-left: -2px; }
.dropdown > summary:hover { border-color: var(--muted); }
.dropdown[open] > summary { border-color: var(--accent); background: var(--surface); }
.dropdown > summary .count { background: var(--accent); color: #fff; border-radius: 999px; font-size: 11px; font-weight: 700; padding: 1px 8px; }
.dropdown > summary .all { color: var(--muted); font-size: 12px; }

.dropdown-panel {
  position: absolute; top: calc(100% + 6px); left: 0; z-index: 30; min-width: 190px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  padding: 7px; box-shadow: 0 10px 28px rgba(0,0,0,.55);
  display: flex; flex-direction: column; gap: 1px; max-height: 300px; overflow-y: auto;
  scrollbar-width: thin; scrollbar-color: var(--line) transparent;
}
.dropdown-panel::-webkit-scrollbar { width: 9px; }
.dropdown-panel::-webkit-scrollbar-thumb { background: var(--line); border-radius: 5px; border: 2px solid var(--surface); }
.dropdown-panel::-webkit-scrollbar-track { background: transparent; }
.dropdown-panel label {
  display: flex; align-items: center; gap: 9px; font-size: 13px; padding: 6px 8px;
  border-radius: 6px; cursor: pointer; white-space: nowrap; color: var(--text); transition: background .12s;
}
.dropdown-panel label:hover { background: var(--surface-2); }
.dropdown-panel label:has(input:checked) { color: var(--accent-2); font-weight: 600; }

/* Search box inside a dropdown panel (sticks to the top while the list scrolls) */
.dd-search { position: sticky; top: 0; z-index: 1; background: var(--surface); padding-bottom: 7px; margin-bottom: 2px; }
.dd-search-input {
  width: 100%; background: var(--surface-2); border: 1px solid var(--line); color: var(--text);
  padding: 7px 9px; border-radius: 7px; font-size: 13px;
}
.dd-search-input::placeholder { color: var(--muted); }
.dd-search-input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 2px rgba(59,130,246,.25); }
.dd-empty { padding: 6px 8px; font-size: 12px; }

/* Custom dark-theme checkbox (used in dropdown panels) */
.dropdown-panel input[type=checkbox] {
  appearance: none; -webkit-appearance: none; margin: 0; flex: none;
  width: 17px; height: 17px; border: 1.5px solid var(--line); border-radius: 5px;
  background: var(--surface-2); cursor: pointer; position: relative; transition: all .12s;
}
.dropdown-panel input[type=checkbox]:hover { border-color: var(--accent); }
.dropdown-panel input[type=checkbox]:checked { background: var(--accent); border-color: var(--accent); }
.dropdown-panel input[type=checkbox]:checked::after {
  content: ''; position: absolute; left: 5px; top: 1.5px;
  width: 4px; height: 9px; border: solid #fff; border-width: 0 2px 2px 0; transform: rotate(45deg);
}

/* Vertical divider + age/search fields + actions */
.bar-divider { width: 1px; align-self: stretch; background: var(--line); margin: 0 2px; }
.bar-field { display: flex; flex-direction: column; gap: 5px; }
.bar-field.grow { flex: 1 1 200px; }
.bar-field label { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; font-weight: 700; }
.bar-field input {
  background: var(--surface-2); border: 1px solid var(--line); color: var(--text);
  padding: 9px 11px; border-radius: 8px; font-size: 13px; width: 100%;
}
.bar-field input[type=number] { width: 92px; }
.bar-field select {
  background: var(--surface-2); border: 1px solid var(--line); color: var(--text);
  padding: 9px 11px; border-radius: 8px; font-size: 13px; cursor: pointer;
}
.bar-field select:focus { outline: none; border-color: var(--accent); }
.bar-field input::placeholder { color: var(--muted); }
.bar-field input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(59,130,246,.25); }
.bar-actions { display: flex; gap: 8px; margin-left: auto; }

.age-pill { display: inline-block; padding: 2px 8px; border-radius: 999px; font-weight: 700; font-size: 12px; color: #fff; }
.store-tag { font-size: 11px; padding: 1px 7px; border-radius: 999px; font-weight: 600; }
.store-Chevrolet { background: rgba(209,162,58,.18); color: var(--chevy); }
.store-Kia { background: rgba(192,57,43,.20); color: #e8897e; }
.store-Other { background: var(--surface-2); color: var(--muted); }

.cost-cell { color: #f0a; }
.interest { color: var(--bad); font-weight: 600; }

/* ---- Login ---- */
.login-wrap { max-width: 360px; margin: 8vh auto; }
.login-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 28px; box-shadow: var(--shadow); }
.login-card h1 { margin: 0 0 4px; font-size: 20px; }
.login-card .sub { color: var(--muted); margin: 0 0 20px; font-size: 13px; }
.login-card .field { margin-bottom: 14px; }
.login-card label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 5px; }
.login-card input { width: 100%; background: var(--surface-2); border: 1px solid var(--line); color: var(--text); padding: 10px; border-radius: 8px; font-size: 14px; }
.login-card .btn { width: 100%; padding: 11px; }

/* ---- Forms / admin ---- */
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px,1fr)); gap: 12px; align-items: end; }
.form-grid .field { display: flex; flex-direction: column; gap: 5px; }
.form-grid label { font-size: 12px; color: var(--muted); }
.form-grid input, .form-grid select { background: var(--surface-2); border: 1px solid var(--line); color: var(--text); padding: 8px; border-radius: 8px; }
.inline-form { display: inline; }
.muted { color: var(--muted); }
.notice { background: rgba(34,197,94,.12); border: 1px solid rgba(34,197,94,.4); color: #86efac; padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; }
.error-banner { background: rgba(239,68,68,.12); border: 1px solid rgba(239,68,68,.4); color: #fca5a5; padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; }
.pill-good { color: var(--good); }
.pill-bad { color: var(--bad); }

.page-head { display: flex; align-items: baseline; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head h1 { font-size: 22px; margin: 0; }
.page-head .meta { color: var(--muted); font-size: 13px; }

.action-badge { font-size: 11px; padding: 2px 8px; border-radius: 999px; font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.a-login { background: rgba(34,197,94,.15); color: #86efac; }
.a-login_fail { background: rgba(239,68,68,.15); color: #fca5a5; }
.a-logout { background: var(--surface-2); color: var(--muted); }
.a-view { background: rgba(59,130,246,.15); color: var(--accent-2); }
.a-export { background: rgba(168,85,247,.15); color: #d8b4fe; }
.a-upload { background: rgba(245,158,11,.15); color: #fcd34d; }
.a-admin { background: rgba(236,72,153,.15); color: #f9a8d4; }
.a-password_change { background: rgba(20,184,166,.15); color: #5eead4; }
.chip-link { color: var(--muted); font-size: 12px; border: 1px solid var(--line); padding: 6px 10px; border-radius: 8px; }
.chip-link:hover { color: var(--text); text-decoration: none; border-color: var(--muted); }

/* ---- FAQ / Help ---- */
.faq h2 { margin-bottom: 6px; }
.faq details { border-bottom: 1px solid var(--line); padding: 4px 0; }
.faq details:last-child { border-bottom: none; }
.faq summary {
  cursor: pointer; padding: 9px 4px; font-weight: 600; font-size: 14px; list-style: none;
  display: flex; align-items: center; gap: 8px;
}
.faq summary::-webkit-details-marker { display: none; }
.faq summary::before { content: '▸'; color: var(--muted); font-size: 12px; transition: transform .12s; }
.faq details[open] summary::before { transform: rotate(90deg); }
.faq summary:hover { color: var(--accent-2); }
.faq details > div { padding: 2px 4px 12px 24px; color: var(--text); line-height: 1.7; font-size: 13px; }
.faq details > div strong { color: var(--text); }

/* ---- History charts ---- */
.bars { display: flex; align-items: flex-end; gap: 8px; min-height: 150px; padding-top: 8px; overflow-x: auto; }
.bar-col { flex: 1 0 44px; display: flex; flex-direction: column; align-items: center; gap: 4px; }
.bar-val { font-size: 11px; color: var(--muted); white-space: nowrap; }
.bar { width: 70%; max-width: 48px; border-radius: 4px 4px 0 0; }
.bar-lbl { font-size: 10px; color: var(--muted); }
.mix-rows { display: flex; flex-direction: column; gap: 7px; margin-top: 6px; }
.mix-row { display: grid; grid-template-columns: 110px 1fr; align-items: center; gap: 12px; }
.mix-date { font-size: 12px; color: var(--muted); }
.stackbar { display: flex; height: 22px; border-radius: 5px; overflow: hidden; background: var(--surface-2); }
.stackbar > div { height: 100%; }
.mix-legend { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 4px; font-size: 12px; color: var(--muted); }
.mix-legend span { display: inline-flex; align-items: center; gap: 5px; }
.mix-legend i { width: 11px; height: 11px; border-radius: 3px; display: inline-block; }

.detail-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px,1fr)); gap: 14px; }
.detail-grid .item .k { color: var(--muted); font-size: 12px; }
.detail-grid .item .v { font-size: 15px; font-weight: 600; }
.notes-block { background: var(--surface-2); border-radius: 8px; padding: 12px; white-space: pre-wrap; line-height: 1.6; }
.pagination { display: flex; gap: 8px; align-items: center; margin-top: 14px; }
.tabbar { display: flex; gap: 8px; margin-bottom: 16px; flex-wrap: wrap; }
.tabbar a { padding: 6px 12px; border-radius: 8px; background: var(--surface); border: 1px solid var(--line); color: var(--muted); }
.tabbar a.active { color: var(--text); border-color: var(--accent); }

/* ===================== Mobile optimizations ===================== */
@media (max-width: 760px) {
  .container { padding: 14px 12px 48px; }

  /* Page head + panels */
  .page-head { gap: 8px 14px; margin-bottom: 14px; }
  .page-head h1 { font-size: 19px; }
  .panel { padding: 13px; margin-bottom: 14px; }
  .panel h2 { font-size: 14px; }

  /* KPI cards: 2-up, smaller numbers */
  .kpis { grid-template-columns: 1fr 1fr; gap: 10px; }
  .kpi { padding: 12px; }
  .kpi .value { font-size: 21px; }

  /* Aging buckets: drop the wide fixed meta column; meta wraps below the bar */
  .bucket-row { grid-template-columns: 52px 1fr; gap: 8px 10px; padding: 8px; margin: 0; }
  .bucket-name { font-size: 13px; }
  .bucket-meta { grid-column: 1 / -1; text-align: left; }

  /* Generic filter rows: controls go full-width */
  .filters { gap: 8px; }
  .filters .field { flex: 1 1 100%; }
  .filters select, .filters input { width: 100%; min-width: 0; }
  .filters .actions { margin-left: 0; width: 100%; }
  .filters .actions .btn { flex: 1; text-align: center; }

  /* Dashboard/trends filter bar: stacked full-width dropdowns + fields */
  .filter-bar { gap: 8px; }
  .bar-divider { display: none; }
  .filter-bar .dropdown { flex: 1 1 100%; }
  .filter-bar .dropdown > summary { width: 100%; justify-content: space-between; }
  .dropdown-panel { min-width: 0; width: 100%; max-width: 100%; left: 0; }
  .bar-field, .bar-field.grow { flex: 1 1 100%; }
  .bar-field input[type=number] { width: 100%; }
  .bar-actions { margin-left: 0; width: 100%; }
  .bar-actions .btn { flex: 1; text-align: center; }

  /* Tables: denser cells; horizontal scroll stays inside the wrap */
  .table-wrap { max-height: calc(100vh - 165px); -webkit-overflow-scrolling: touch; }
  table.grid { font-size: 12px; }
  table.grid th, table.grid td { padding: 7px 8px; }
  .notes-cell .clip { max-width: 200px; }

  /* Detail grids already auto-fit; tighten spacing */
  .detail-grid { gap: 12px; }
  .detail-grid .item .v { font-size: 14px; }

  /* History charts (tab hidden, kept sane) */
  .mix-row { grid-template-columns: 74px 1fr; gap: 8px; }
  .bar-col { flex-basis: 38px; }

  /* Comfortable touch targets */
  .btn { padding: 10px 16px; }
  .dropdown > summary { padding: 10px 12px; }
  .chip-link, .logout-form button { padding: 8px 12px; }

  /* Login / password card */
  .login-wrap { margin: 4vh auto; max-width: 100%; }
  .login-card { padding: 22px 18px; }
}

@media (max-width: 380px) {
  .kpis { grid-template-columns: 1fr; }
  .page-head h1 { font-size: 18px; }
}
