/* ─── CarSearch AI Partners — AI page (.pai-*) ───────────────────────────────
   Styles for public/js/partner-ai.js: the usage tab (range buttons, stat row,
   spend bars, dealership table, conversation drill-in) and the settings tab
   (dealership list, the per-dealership form, the apply-to picker). Tokens only,
   no literal colours, so every theme in app.css keeps working. Mobile target:
   390px wide with no page overflow and 16px inputs.                          */

.pai, .pai-panel { min-width: 0; }
.pai-panel > .card { margin-bottom: 14px; }
/* .card-h / .card-sub are scoped to .portal and #admin-body in the shared
   sheets, so the partner pages carry their own copy. */
.pai .card-h { font-size: 16px; font-weight: 700; margin: 0 0 4px; color: var(--text); }
.pai .card-sub { font-size: 13.5px; line-height: 1.55; color: var(--text-muted); margin: 0 0 14px; }

/* Tab bar (shared component, public/js/ui-tabs.js). */
.ui-tabs { flex-wrap: wrap; }
@media (max-width: 720px) {
  .ui-tabs { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
  .ui-tabs::-webkit-scrollbar { display: none; }
  .ui-tabs .admin-tab { flex: 0 0 auto; }
}

/* ── Card head with the range buttons on the right ──────────────────────── */
.pai-head { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-start; justify-content: space-between; }
.pai-head > div { min-width: 0; flex: 1 1 260px; }
.pai-range { display: inline-flex; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; flex: 0 0 auto; }
.pai-rangebtn {
  appearance: none; border: none; background: var(--surface); color: var(--text-muted);
  font: inherit; font-size: 13px; padding: 7px 14px; cursor: pointer;
  border-right: 1px solid var(--border);
}
.pai-rangebtn:last-child { border-right: none; }
.pai-rangebtn:hover { background: var(--surface-2); color: var(--text); }
.pai-rangebtn.active { background: var(--primary); color: var(--on-primary); }
.pai-rangebtn:focus-visible { outline: 2px solid var(--primary); outline-offset: -2px; }
.pai-back { display: inline-block; margin-bottom: 8px; }

/* ── Stat row ───────────────────────────────────────────────────────────── */
.pai-stats { display: flex; flex-wrap: wrap; gap: 22px; margin: 6px 0 16px; }
.pai-stat { display: flex; flex-direction: column; min-width: 0; }
.pai-stat-n { font-size: 22px; font-weight: 700; color: var(--text); line-height: 1.2; }
.pai-stat-l { font-size: 12px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .04em; }
.pai-stat-h { font-size: 11.5px; color: var(--text-muted); margin-top: 2px; }
.pai-empty { color: var(--text-muted); margin: 0; }
.pai-more { margin: 10px 0 0; font-size: 12.5px; }

/* ── Spend bars ─────────────────────────────────────────────────────────── */
.pai-chart {
  display: flex; align-items: flex-end; gap: 3px; height: 110px; padding: 8px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius);
}
.pai-barcell { flex: 1 1 0; min-width: 0; height: 100%; display: flex; align-items: flex-end; }
.pai-bar { display: block; width: 100%; border-radius: 2px 2px 0 0; background: var(--primary); }
.pai-chart-x { display: flex; justify-content: space-between; font-size: 11.5px; color: var(--text-muted); margin: 6px 2px 0; }

/* ── Tables ─────────────────────────────────────────────────────────────── */
.pai-toolbar { flex-wrap: wrap; gap: 10px; }
.pai-search { flex: 0 1 320px; min-width: 0; }
.pai-tablewrap { max-height: none; }
.pai-table td { vertical-align: top; }
.pai-table tbody tr { cursor: default; }
.pai-table .pai-row-act { text-align: right; white-space: nowrap; }
.pai-table .badge + .badge { margin-left: 4px; }
.pai-code-inline { font-size: 12px; overflow-wrap: anywhere; }
.pai-pager { display: flex; align-items: center; justify-content: center; gap: 12px; margin-top: 12px; }

/* ── Settings form ──────────────────────────────────────────────────────── */
.pai-group { border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px 16px; margin: 0 0 16px; min-width: 0; }
.pai-group > legend { padding: 0 6px; font-size: 13px; font-weight: 700; color: var(--text); }
.pai-group-hint { margin: 0 0 12px; font-size: 12.5px; color: var(--text-muted); line-height: 1.5; }
.pai-fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 14px 18px; min-width: 0; }
.pai-fields-wide { grid-template-columns: 1fr; }
.pai-fld { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.pai-fld-l { font-size: 13px; font-weight: 600; color: var(--text); }
.pai-fld-l small { font-weight: 400; color: var(--text-muted); }
.pai-fld input[type="text"], .pai-fld input[type="number"], .pai-fld select, .pai-fld textarea {
  width: 100%; min-width: 0; box-sizing: border-box;
  padding: 8px 10px; font: inherit; font-size: 14px;
  color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.pai-fld textarea { resize: vertical; line-height: 1.5; }
.pai-fld input:focus, .pai-fld select:focus, .pai-fld textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: var(--primary); }
.pai-fld-help { font-size: 12px; color: var(--text-muted); line-height: 1.5; }
.pai-fld-eff { font-size: 12px; color: var(--text); background: var(--surface-2); border-radius: var(--radius); padding: 5px 8px; line-height: 1.45; }
.pai-switch { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; color: var(--text); padding: 6px 0; }
.pai-switch input { width: 18px; height: 18px; accent-color: var(--primary); }
.pai-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 4px; }

/* ── Apply-to picker (inside the shared dialog) ─────────────────────────── */
.pai-picker { margin: 4px 0 10px; text-align: left; }
.pai-pick-all { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 14px; color: var(--text); }
.pai-picklist { max-height: 240px; overflow-y: auto; padding: 6px 0; }
.pai-pick { display: flex; align-items: center; gap: 8px; padding: 6px 0; font-size: 14px; color: var(--text); }
.pai-picker input[type="checkbox"] { width: 17px; height: 17px; accent-color: var(--primary); flex: 0 0 auto; }
.pai-pick span { min-width: 0; overflow-wrap: anywhere; }

/* ── Phone: 390px, stacked cards, 16px inputs, full-width buttons ───────── */
@media (max-width: 640px) {
  .pai-panel > .card { padding-left: 14px; padding-right: 14px; }
  /* Block, not column-flex: as a flex column the main axis is vertical, so the
     text side's flex-basis stretched the head to 260px tall and dragged the
     range buttons with it. */
  .pai-head { display: block; }
  .pai-range { display: flex; width: 100%; margin-top: 10px; }
  .pai-rangebtn { flex: 1 1 0; padding: 9px 6px; }
  .pai-stats { gap: 14px 20px; }
  .pai-stat { flex: 1 1 44%; }
  .pai-search { flex: 1 1 100%; }
  .pai-fields { grid-template-columns: 1fr; }
  .pai-actions { flex-direction: column; align-items: stretch; }
  .pai-actions .btn { width: 100%; }
  .pai-chart { height: 90px; gap: 2px; }
  .pai-panel input, .pai-panel select, .pai-panel textarea { font-size: 16px; }
  .pai-group { padding: 12px 12px 14px; }

  /* Tables become stacked cards. The selectors carry the table element so they
     beat the shared "table.grid scrolls sideways on a phone" rule in app.css. */
  .pai-tablewrap { border: none; background: none; box-shadow: none; overflow: visible; }
  table.pai-table { display: block; width: auto; overflow: visible; white-space: normal; }
  table.pai-table thead { display: none; }
  table.pai-table tbody, table.pai-table tr, table.pai-table td { display: block; width: auto; min-width: 0; }
  table.pai-table tbody tr { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 12px; margin-bottom: 10px; }
  table.pai-table tbody tr:hover { background: var(--surface); }
  table.pai-table td { border: none; padding: 5px 0; }
  table.pai-table td::before { content: attr(data-label); display: block; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--text-muted); margin-bottom: 2px; }
  table.pai-table td[data-label=""]::before { display: none; }
  table.pai-table .pai-row-act { text-align: left; }
  table.pai-table .pai-row-act .btn { width: 100%; }
  .pai-pager .btn { flex: 1 1 auto; }
}
