/* cdr.css — presentation for the CDR call-log grid (Session 6b).
   Ported in substance from the recording-manager grid.css pattern,
   restyled onto this dashboard's design tokens (tokens.css): no
   palette values here, tokens only. The grid lives inside the
   full-screen #cdr-dialog (D8: in-page <dialog>, no navigation). */

/* --- the full-screen dialog housing the grid ---------------------- */

dialog#cdr-dialog {
  width: min(96vw, 1500px);
  max-width: 96vw;
  height: min(92vh, 1000px);
  max-height: 92vh;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--bg-raised);
  color: var(--text);
  box-shadow: var(--shadow-2);
}

dialog#cdr-dialog::backdrop {
  background: var(--bg-overlay);
}

dialog#cdr-dialog .dialog-head {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px;
  background: var(--bg-raised);
  border-bottom: 1px solid var(--border);
}
dialog#cdr-dialog .dialog-head h2 { margin: 0; font-size: 16px; }
dialog#cdr-dialog .dialog-head .source-badge { margin-left: 4px; }
dialog#cdr-dialog .dialog-head .spacer { flex: 1; }

dialog#cdr-dialog .dialog-body {
  display: flex; flex-direction: column;
  height: calc(100% - 53px); /* the head's height; keeps the footers visible */
  overflow: hidden;
}

/* --- the grid toolbar ---------------------------------------------- */

.cdr-toolbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  padding: 8px 16px;
  background: var(--bg-raised);
  border-bottom: 1px solid var(--border);
}
.cdr-toolbar input[type="search"] {
  flex: 1; min-width: 200px; max-width: 380px;
  padding: 6px 10px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.cdr-toolbar select {
  padding: 6px 8px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.cdr-toolbar .cdr-count {
  color: var(--text-muted); font-size: 12px; white-space: nowrap;
}
.cdr-status {
  color: var(--text-muted); font-size: 12px; margin-left: auto;
  max-width: 320px; overflow: hidden; text-overflow: ellipsis;
  white-space: nowrap;
}

/* --- the scroll region + the table --------------------------------- */

.cdr-scroll {
  flex: 1;
  overflow: auto;
  background: var(--bg-raised);
}

table.cdr-grid {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.cdr-grid th, .cdr-grid td {
  text-align: left;
  padding: 5px 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.cdr-grid thead th {
  position: sticky; top: 0; z-index: 2;
  background: var(--bg-inset);
  color: var(--text);
  font-weight: 600;
  border-bottom: 1px solid var(--border-strong);
  padding-left: 22px; /* room for the help button */
}
.cdr-grid tbody tr:hover td { background: var(--bg-inset); }
.cdr-grid th[hidden], .cdr-grid td[hidden] { display: none; }

.cdr-grid td.col-num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* Direction + disposition chips reuse the live-call chip classes
   (live.css): dir-chip dir-chip--in/out, state-chip. The disposition
   chip reuses state-chip--active/terminated colouring per value. */
.cdr-grid .badge {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 10px;
  font-size: 12px;
  background: var(--bg-inset);
  border: 1px solid var(--border);
}
.cdr-grid .badge.badge--err { background: var(--err-soft); }
.cdr-grid .badge.badge--warn { background: var(--warn-soft); }
.cdr-grid .badge.badge--ok { background: var(--ok-soft); }
.cdr-grid .badge.badge--idle { background: var(--idle-soft); }
.cdr-grid td.col-code { font-family: var(--mono-bg); }

/* --- sortable headers (grid standard §7.1) -------------------------- */

th .sort-btn {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 0; margin: 0; border: 0;
  background: none;
  font: inherit; font-weight: 600; color: inherit;
  cursor: pointer;
}
th .sort-btn:hover { text-decoration: underline; }
.sort-ind {
  color: var(--accent);
  min-width: 1em;
  display: inline-block;
}

/* --- header value-filter affordance (§7.1) -------------------------- */

th .filter-btn {
  display: inline-flex; align-items: center; gap: 3px;
  margin-left: 6px; padding: 0 4px;
  border: 0; background: none;
  font: inherit; color: var(--text-muted);
  cursor: pointer; border-radius: 4px;
}
th .filter-btn:hover { background: var(--bg-inset); color: var(--accent); }
th .filter-btn.active { color: var(--accent); background: var(--accent-soft); }
.filter-ind { font-size: 11px; font-weight: 600; }

/* --- the value picker popup (anchored, non-modal; §5.4) ------------- */

.filter-pop {
  position: fixed; z-index: 30;
  width: 300px; max-height: 60vh;
  display: flex; flex-direction: column; gap: 6px;
  padding: 8px;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-2);
  color: var(--text);
}
.filter-pop[hidden] { display: none; }
.filter-pop-search {
  width: 100%; padding: 5px 8px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.filter-pop-list {
  flex: 1; overflow-y: auto;
  min-height: 40px; max-height: 40vh;
  padding: 4px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.filter-pop-row {
  display: flex; align-items: center; gap: 6px;
  padding: 3px 4px;
  border-radius: 4px;
  cursor: pointer;
}
.filter-pop-row:hover { background: var(--bg-inset); }
.filter-pop-value {
  flex: 1;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.filter-pop-count { color: var(--text-muted); font-size: 12px; }
.filter-pop-note { color: var(--text-muted); padding: 6px; font-size: 12px; }
.filter-pop-actions { display: flex; align-items: center; gap: 6px; }
.filter-pop-spacer { flex: 1; }

/* --- data states (grid standard §21) -------------------------------- */

.cdr-grid tr.state td {
  text-align: center;
  color: var(--text-muted);
  padding: 26px;
  white-space: normal;
}
.cdr-grid tr.state-error td { color: var(--err); }
.cdr-grid tr.state-filtered-empty td { color: var(--text-muted); }

.cdr-sentinel {
  min-height: 22px;
  padding: 8px 0 14px;
  text-align: center;
  color: var(--text-muted);
  font-size: 12px;
}

/* stale marker (§21): a failed fetch with rows on screen keeps them,
   tinted; the sentinel text names the state — never color alone. */
.cdr-grid.stale thead th { background: var(--warn-soft); }

/* --- selection (§7.4): distinct from focus -------------------------- */

.cdr-grid td.sel { background: var(--accent-soft) !important; }
.cdr-grid td.sel-focus { outline: 2px solid var(--accent); outline-offset: -2px; }
.cdr-grid.sel-all tbody td { background: var(--accent-soft); }

/* while a drag-select gesture is live, suppress native selection */
body.cdr-sel-dragging { user-select: none; }
body.cdr-sel-dragging td { cursor: cell; }

.sel-status {
  color: var(--text-muted);
  font-size: 12px;
  padding-left: 10px;
  border-left: 1px solid var(--border);
  white-space: nowrap;
}

/* --- column layout affordances (§7.2/§7.7/§3) ------------------------ */

th.col-dragging { opacity: 0.5; }
th.col-drop-target { box-shadow: inset 3px 0 0 var(--accent); }

th .col-resize-grip {
  position: absolute; top: 0; right: 0;
  width: 7px; height: 100%;
  cursor: col-resize;
  user-select: none;
  touch-action: none;
}
th .col-resize-grip:hover,
th .col-resize-grip.dragging { background: var(--accent); opacity: 0.35; }
body.col-resizing { cursor: col-resize; user-select: none; }

th .col-help-btn {
  position: absolute; top: 50%; left: 2px;
  transform: translateY(-50%);
  width: 15px; height: 15px; padding: 0;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--bg-raised);
  color: var(--text-muted);
  font-size: 10px; line-height: 13px;
  cursor: help;
}
th .col-help-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.col-help-pop {
  position: fixed; z-index: 31;
  width: 320px; padding: 10px 12px;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-2);
  white-space: normal;
  color: var(--text);
}
.col-help-pop[hidden] { display: none; }
.col-help-title { font-weight: 600; margin-bottom: 4px; }
.col-help-body {
  color: var(--text-muted);
  font-size: 13px;
  line-height: 1.45;
}

/* Columns popup (toolbar "Columns"): live checkboxes + the obvious
   reset-to-default (§3). Anchored, non-modal (§5.4). */
.columns-pop {
  position: fixed; z-index: 30;
  width: 250px; padding: 8px;
  display: flex; flex-direction: column; gap: 6px;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-2);
  color: var(--text);
}
.columns-pop[hidden] { display: none; }
.columns-pop-list { display: flex; flex-direction: column; }
.columns-pop-row {
  display: flex; align-items: center; gap: 6px;
  padding: 3px 4px;
  border-radius: 4px;
  cursor: pointer;
}
.columns-pop-row:hover { background: var(--bg-inset); }
.columns-pop-actions { display: flex; align-items: center; gap: 6px; margin-top: 6px; }
.columns-pop-actions .filter-pop-spacer { flex: 1; }

/* --- the context menu (§9.2) ----------------------------------------- */

.ctx-menu {
  position: fixed; z-index: 40;
  min-width: 240px; max-width: 360px;
  padding: 4px;
  display: flex; flex-direction: column;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-2);
  color: var(--text);
}
.ctx-menu[hidden] { display: none; }
.ctx-item {
  display: block; width: 100%;
  padding: 6px 10px;
  text-align: left;
  border: 0; border-radius: 5px;
  background: none;
  font: inherit; color: inherit;
  cursor: pointer;
}
.ctx-item:hover:not(:disabled) { background: var(--accent-soft); }
.ctx-item:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}
.ctx-item:disabled { color: var(--text-faint); cursor: default; }
.ctx-sep {
  height: 1px; margin: 4px 6px;
  background: var(--border);
}

/* --- small buttons inside popups / state rows ------------------------ */

button.state-btn {
  padding: 3px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-inset);
  color: var(--text);
  font: inherit; font-size: 12px;
  cursor: pointer;
}
button.state-btn:hover { background: var(--accent-soft); }
button.state-btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--text-inverse);
}
button.state-btn.primary:hover { filter: brightness(0.95); }

/* --- the grid footer -------------------------------------------------- */

.cdr-foot {
  display: flex; align-items: center; gap: 12px;
  padding: 6px 16px;
  font-size: 12px;
  color: var(--text-muted);
  background: var(--bg-raised);
  border-top: 1px solid var(--border);
}
.cdr-foot span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
