:root {
  --ink: #1f2933;
  --muted: #6b7280;
  --line: #e2e8f0;
  --brand: #1d4ed8;
  --brand-dark: #1e40af;
  --warn: #b45309;
  --warn-bg: #fef3c7;
  --ok-bg: #dcfce7;
  --ok-ink: #166534;
  --err-bg: #fee2e2;
  --err-ink: #991b1b;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: #f8fafc;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.9rem 1.5rem;
  background: #fff;
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; font-size: 1.1rem; }
.who { color: var(--muted); font-size: 0.9rem; }
.who a { color: var(--brand); }

main { max-width: 1500px; margin: 1.5rem auto; padding: 0 1.5rem; }

/* Top navigation tabs */
.tabs {
  display: flex;
  gap: 0.3rem;
  padding: 0 1.5rem;
  background: #fff;
  border-bottom: 1px solid var(--line);
}
.tabs a {
  padding: 0.7rem 1.1rem;
  text-decoration: none;
  color: var(--muted);
  font-weight: 600;
  font-size: 0.92rem;
  border-bottom: 3px solid transparent;
}
.tabs a:hover { color: var(--ink); }
.tabs a.active { color: var(--brand); border-bottom-color: var(--brand); }

/* Date-range picker */
.daterange {
  display: flex;
  align-items: flex-end;
  gap: 0.8rem;
  flex-wrap: wrap;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.7rem 0.9rem;
  margin-bottom: 1rem;
}
.dr-field { display: flex; flex-direction: column; font-size: 0.78rem; color: var(--muted); gap: 0.2rem; }
.dr-field select {
  padding: 0.4rem 0.5rem;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  font-size: 0.9rem;
}
.dr-custom { display: flex; align-items: center; gap: 0.4rem; }
.dr-custom input[type="date"] {
  padding: 0.35rem 0.5rem;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  font-size: 0.85rem;
}
.dr-arrow { color: var(--muted); }
.dr-current { margin-left: auto; color: var(--muted); font-size: 0.85rem; align-self: center; }

h1 { font-size: 1.4rem; }

/* Flash messages */
.flash {
  padding: 0.7rem 1rem;
  border-radius: 8px;
  margin-bottom: 1rem;
  font-size: 0.95rem;
}
.flash-success { background: var(--ok-bg); color: var(--ok-ink); }
.flash-error { background: var(--err-bg); color: var(--err-ink); }

/* Confirmation banner for implausible values */
.confirm-banner {
  background: var(--warn-bg);
  border: 1px solid #fcd34d;
  border-radius: 8px;
  padding: 1rem 1.2rem;
  margin-bottom: 1.2rem;
}
.confirm-banner p { margin: 0 0 0.7rem; }
.confirm-banner form { display: flex; gap: 0.6rem; align-items: center; }

/* Table */
table.calls {
  width: 100%;
  border-collapse: collapse;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}
table.calls th, table.calls td {
  text-align: left;
  padding: 0.7rem 0.9rem;
  border-bottom: 1px solid var(--line);
  font-size: 0.95rem;
}
table.calls th {
  background: #f1f5f9;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
}
table.calls tr:last-child td { border-bottom: none; }
tr.row-flagged { background: var(--warn-bg); }

/* At-a-glance status: green accent = value entered, amber = still needs one. */
tr.row-valued td:first-child { border-left: 4px solid #16a34a; }
tr.row-empty  td:first-child { border-left: 4px solid #f59e0b; }
tr.row-empty  { background: #fffdf5; }
/* Ambiguous attribution: red accent — needs a human to pick the session. */
tr.row-ambiguous td:first-child { border-left: 4px solid #dc2626; }

/* Attribution filter bar (above the column toolbar) */
.attr-filter { margin: 0.6rem 0 0.2rem; font-size: 0.85rem; }
.attr-filter-link { font-weight: 600; color: var(--warn); }
.attr-filter-note, .attr-filter-clear { color: var(--muted); margin-left: 0.4rem; }

/* Ambiguous candidate picker inside the gclid cell */
.ambig > summary { cursor: pointer; list-style: none; }
.ambig > summary::-webkit-details-marker { display: none; }
.ambig-panel {
  margin-top: 0.4rem;
  padding: 0.4rem 0.5rem;
  background: var(--warn-bg);
  border: 1px solid #fca5a5;
  border-radius: 6px;
  min-width: 260px;
}
.ambig-note { margin: 0 0 0.4rem; font-size: 0.78rem; color: var(--ink); }
.ambig-cand {
  display: flex; align-items: center; justify-content: space-between;
  gap: 0.5rem; padding: 0.3rem 0; border-top: 1px solid #fecaca;
}
.ambig-cand:first-of-type { border-top: none; }
.ambig-meta { font-size: 0.75rem; color: var(--ink); }
.ambig-meta .sid { color: var(--muted); margin-left: 0.3rem; }
.btn-mini {
  font-size: 0.72rem; padding: 0.2rem 0.5rem; white-space: nowrap;
  background: var(--brand); color: #fff; border: none; border-radius: 4px;
  cursor: pointer;
}
.btn-mini:hover { background: var(--brand-dark); }

/* The per-row value entry form */
.value-form { display: flex; align-items: center; gap: 0.3rem; }
.value-form .dollar { color: var(--muted); }
.value-form input {
  width: 90px;
  padding: 0.35rem 0.5rem;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  font-size: 0.95rem;
}

/* Buttons */
button, .btn-cancel {
  font: inherit;
  cursor: pointer;
  border: none;
  border-radius: 6px;
  padding: 0.4rem 0.9rem;
}
.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); }
.save-btn.is-saved,
.save-btn.is-saved:hover { background: #16a34a; }
.btn-warn { background: var(--warn); color: #fff; }
.btn-cancel {
  background: #e2e8f0; color: var(--ink);
  text-decoration: none; display: inline-block;
}

.history-link { color: var(--muted); font-size: 0.85rem; }

.hint { color: var(--muted); font-size: 0.85rem; margin-top: 1rem; }

/* Login */
.login-box {
  max-width: 320px;
  margin: 3rem auto;
  background: #fff;
  padding: 1.8rem;
  border: 1px solid var(--line);
  border-radius: 10px;
}
.login-box h1 { margin-top: 0; }
.login-box label {
  display: block;
  margin-bottom: 1rem;
  font-size: 0.9rem;
  color: var(--muted);
}
.login-box input {
  display: block;
  width: 100%;
  margin-top: 0.3rem;
  padding: 0.5rem;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  font-size: 1rem;
}
.login-box button { width: 100%; padding: 0.6rem; font-size: 1rem; }

/* ---------------- Layout: sidebar + content ---------------- */
.layout { display: flex; gap: 1.5rem; align-items: flex-start; }
.sidebar {
  width: 230px;
  flex-shrink: 0;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.6rem;
}
.content { flex: 1; min-width: 0; }   /* min-width:0 lets the table scroll */

.side-link {
  display: block;
  padding: 0.35rem 0.55rem;
  border-radius: 6px;
  color: var(--ink);
  text-decoration: none;
  font-size: 0.9rem;
}
.side-link:hover { background: #f1f5f9; }
.side-link.is-active { background: var(--brand); color: #fff; }
.side-all { font-weight: 600; margin-bottom: 0.4rem; }

.sidebar details { margin-bottom: 0.25rem; }
.sidebar summary {
  cursor: pointer;
  font-weight: 600;
  font-size: 0.9rem;
  padding: 0.35rem 0.4rem;
  border-radius: 6px;
}
.sidebar summary:hover { background: #f1f5f9; }
.campaign-list { list-style: none; margin: 0.2rem 0 0.4rem; padding-left: 0.5rem; }
.campaign-list li { margin: 0; }

/* Account number is the real identifier (shown first); name beside it. */
.acct-num {
  font-family: ui-monospace, monospace;
  font-weight: 700;
  font-size: 0.88rem;
}
.acct-name {
  display: block;
  font-weight: 400;
  font-size: 0.78rem;
  color: var(--muted);
  margin-left: 0.1rem;
}
.scope-num {
  font-family: ui-monospace, monospace;
  background: #f1f5f9;
  padding: 0.05rem 0.4rem;
  border-radius: 5px;
  font-size: 0.95rem;
  margin-right: 0.4rem;
}

/* ---------------- Scope heading + summary cards ---------------- */
.scope { margin-top: 0; font-size: 1.25rem; }
.scope .sep { color: var(--muted); margin: 0 0.2rem; }

.summary-cards { display: flex; gap: 1rem; flex-wrap: wrap; margin-bottom: 0.8rem; }
.card {
  flex: 1;
  min-width: 150px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 1rem 1.1rem;
}
.card-num { font-size: 1.6rem; font-weight: 700; }
.card-label { color: var(--muted); font-size: 0.82rem; margin-top: 0.2rem; }
.card-accent { border-color: var(--brand); background: #eff4ff; }
.card-accent .card-num { color: var(--brand-dark); }

.overall-line {
  color: var(--muted);
  font-size: 0.88rem;
  margin: 0 0 1rem;
}

/* ---------------- Wide calls table ---------------- */
.table-scroll {
  overflow: auto;            /* scroll both axes within the table area */
  max-height: 68vh;         /* bounded height -> the body scrolls in here */
  border: 1px solid var(--line);
  border-radius: 8px;
}
/* overflow:visible (not hidden) so the sticky header sticks to .table-scroll
   rather than to the table itself. */
.table-scroll table.calls { border: none; border-radius: 0; overflow: visible; }
/* Keep the header row pinned while the call rows scroll underneath it. */
#callsTable thead th {
  position: sticky;
  top: 0;
  z-index: 2;
}
table.calls td.nowrap, table.calls th { white-space: nowrap; }
.gclid { font-family: ui-monospace, monospace; font-size: 0.8rem; color: var(--muted); }
/* Call duration; qualified calls (>= QUALIFY_SECONDS) are emphasised green. */
.dur { font-variant-numeric: tabular-nums; }
.dur-ok { color: #16a34a; font-weight: 600; }
.ts { color: var(--muted); font-size: 0.75rem; }
.err-text {
  color: var(--err-ink);
  font-size: 0.75rem;
  max-width: 200px;
  white-space: normal;
}

/* Status badges */
.badge {
  display: inline-block;
  padding: 0.1rem 0.45rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
}
.badge-ok { background: var(--ok-bg); color: var(--ok-ink); }
.badge-warn { background: var(--warn-bg); color: var(--warn); }
.badge-err { background: var(--err-bg); color: var(--err-ink); }
.badge-muted { background: #f1f5f9; color: var(--muted); }

/* ---------------- Per-agent performance ---------------- */
.agent-section { margin: 0.4rem 0 1.2rem; }
.agent-heading { font-size: 1.05rem; margin: 0 0 0.2rem; }
.agent-note { color: var(--muted); font-size: 0.82rem; margin: 0 0 0.6rem; }
.agent-table {
  border-collapse: collapse;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  min-width: 420px;
}
.agent-table th, .agent-table td {
  text-align: left;
  padding: 0.45rem 0.9rem;
  border-bottom: 1px solid var(--line);
  font-size: 0.9rem;
}
.agent-table th {
  background: #f1f5f9;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
}
.agent-table tr:last-child td { border-bottom: none; }
.agent-table td:nth-child(n+2), .agent-table th:nth-child(n+2) { text-align: right; }

/* ---------------- Agents leaderboard ---------------- */
.agents-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
#agentSearch {
  padding: 0.45rem 0.7rem;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  font-size: 0.9rem;
  min-width: 220px;
}
.lead-table {
  width: 100%;
  border-collapse: collapse;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}
.lead-table th, .lead-table td {
  padding: 0.55rem 0.9rem;
  border-bottom: 1px solid var(--line);
  font-size: 0.9rem;
  text-align: left;
}
.lead-table th {
  background: #f1f5f9;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
  position: sticky;
  top: 0;
}
.lead-table th.sorted { color: var(--brand); }
.lead-table th.num, .lead-table td.num { text-align: right; }
.lead-table tbody tr:hover { background: #f8fafc; }
.lead-table tr:last-child td { border-bottom: none; }

/* ---------------- Agent detail: period toggle + chart ---------------- */
.period-toggle { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; margin-bottom: 0.3rem; }
.period-toggle a {
  padding: 0.4rem 0.9rem;
  text-decoration: none;
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 600;
  background: #fff;
  border-right: 1px solid var(--line);
}
.period-toggle a:last-child { border-right: none; }
.period-toggle a.active { background: var(--brand); color: #fff; }

.chart-section { margin: 1rem 0 1.5rem; }
.linechart {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  max-width: 780px;
}
.cg-grid { stroke: #eef2f7; stroke-width: 1; }
.cg-axis { fill: var(--muted); font-size: 10px; }
.cg-line { fill: none; stroke: var(--brand); stroke-width: 2; }
.cg-dot { fill: var(--brand); }

/* ---------------- Calls search ---------------- */
.call-search {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0.25rem 0 0.6rem;
}
.call-search input[type="search"] {
  flex: 1;
  max-width: 460px;
  padding: 0.5rem 0.7rem;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  font-size: 0.95rem;
}
.search-clear {
  color: var(--muted);
  font-size: 0.85rem;
  text-decoration: none;
}
.search-clear:hover { color: var(--ink); text-decoration: underline; }
.search-summary {
  color: var(--muted);
  font-size: 0.88rem;
  margin: 0 0 0.6rem;
}
/* Why a row matched when it wasn't the phone number. */
.match-chip {
  display: inline-block;
  margin-top: 0.25rem;
  padding: 0.05rem 0.4rem;
  background: #eff4ff;
  border: 1px solid #c7d7fe;
  border-radius: 999px;
  color: var(--brand-dark);
  font-size: 0.72rem;
  white-space: nowrap;
}

/* ---------------- Toolbar + column show/hide menu ---------------- */
.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  margin-bottom: 0.5rem;
}
.toolbar-note { color: var(--muted); font-size: 0.82rem; }
.col-menu { position: relative; }
.col-menu summary {
  cursor: pointer;
  list-style: none;
  padding: 0.35rem 0.7rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  font-size: 0.85rem;
}
.col-menu summary::-webkit-details-marker { display: none; }
.col-menu[open] summary { background: #f1f5f9; }
.col-menu .menu-panel {
  position: absolute;
  right: 0;
  z-index: 10;
  margin-top: 0.3rem;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.6rem 0.8rem;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.08);
  min-width: 170px;
}
.menu-title {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
  margin-bottom: 0.4rem;
}
.col-menu .menu-panel label {
  display: block;
  font-size: 0.85rem;
  padding: 0.15rem 0;
  cursor: pointer;
}

/* ---------------- Narrow, resizable, fixed-width columns ----------------
   Column widths are controlled by the <colgroup><col> elements and a JS
   drag-resizer (grip on each header). table-layout:fixed + overflow:hidden
   means a column can be dragged down to ~8px — the content just clips. There
   is NO CSS min-width floor on the columns (the only floor is the 8px MIN in
   the JS resizer, kept just large enough to keep the grip grabbable). */
#callsTable { table-layout: fixed; width: auto; }
#callsTable th { position: relative; overflow: hidden; }
#callsTable th, #callsTable td {
  padding: 0.4rem 0.55rem;
  font-size: 0.85rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  vertical-align: top;
}
/* Cells that must not clip their contents */
#callsTable td.c-value { overflow: visible; white-space: normal; }
#callsTable td.c-error, #callsTable td.c-sent, #callsTable td.c-confirmed {
  white-space: normal;
}
#callsTable .value-form input { width: 70px; }

/* Default column widths live on the <col> elements (the resizer overrides
   these inline). No min-width, so dragging can shrink to a sliver. */
#callsTable col { min-width: 0; }
#callsTable col.col-datetime  { width: 165px; }
#callsTable col.col-caller    { width: 118px; }
#callsTable col.col-dialed    { width: 120px; }
#callsTable col.col-agent     { width: 115px; }
#callsTable col.col-city      { width: 92px; }
#callsTable col.col-state     { width: 50px; }
#callsTable col.col-ip        { width: 108px; }
#callsTable col.col-source    { width: 92px; }
#callsTable col.col-device    { width: 132px; }
#callsTable col.col-os        { width: 96px; }
#callsTable col.col-browser   { width: 116px; }
#callsTable col.col-gclid     { width: 96px; }
#callsTable col.col-campid    { width: 100px; }
#callsTable col.col-keyword   { width: 120px; }
#callsTable col.col-value     { width: 200px; }
#callsTable col.col-sent      { width: 100px; }
#callsTable col.col-confirmed { width: 110px; }
#callsTable col.col-error     { width: 120px; }

/* Drag grips on the header cells */
.col-grip {
  position: absolute;
  top: 0;
  right: 0;
  width: 8px;
  height: 100%;
  cursor: col-resize;
  user-select: none;
}
.col-grip:hover { background: rgba(29, 78, 216, 0.3); }
body.col-resizing { cursor: col-resize; user-select: none; }

/* Click-to-sort headers: pointer cursor + a direction caret on the active one.
   Padding-right leaves room for the caret without overlapping the resize grip. */
#callsTable th.sortable { cursor: pointer; padding-right: 1.4rem; }
#callsTable th.sorted-asc::after  { content: " ▲"; font-size: 0.7em; color: var(--accent, #1d4ed8); }
#callsTable th.sorted-desc::after { content: " ▼"; font-size: 0.7em; color: var(--accent, #1d4ed8); }

/* In-app webview override on the Browser column (red, like a warning). */
.inapp-name { font-weight: 600; }
.badge.inapp-badge { margin-left: 0.25rem; }

/* Visit-detail table on the per-call history page. */
.detail-h { font-size: 1rem; margin: 1.4rem 0 0.5rem; }
table.detail-table { width: auto; max-width: 100%; }
table.detail-table th { width: 120px; white-space: nowrap; }
table.detail-table td.detail-url { word-break: break-all; max-width: 640px; }

.menu-reset {
  margin-top: 0.5rem;
  width: 100%;
  background: #e2e8f0;
  color: var(--ink);
  border: none;
  border-radius: 6px;
  padding: 0.35rem;
  font-size: 0.82rem;
  cursor: pointer;
}

/* ---------------- Users / account management ---------------- */
.role-tag {
  display: inline-block;
  margin-left: 0.3rem;
  padding: 0.05rem 0.4rem;
  border-radius: 999px;
  background: #e2e8f0;
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.who .role-tag { background: var(--brand); color: #fff; }

.user-actions { white-space: nowrap; }
.inline-form { display: inline-flex; gap: 0.3rem; align-items: center; margin-right: 0.6rem; }
.inline-form input {
  padding: 0.3rem 0.45rem;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  font-size: 0.85rem;
}
.btn-small { padding: 0.3rem 0.6rem; font-size: 0.82rem; background: #e2e8f0; color: var(--ink); }
.btn-small:hover { background: #cbd5e1; }
.btn-danger { background: var(--err-bg); color: var(--err-ink); }
.btn-danger:hover { background: #fecaca; }

.add-user-form {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  align-items: flex-end;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 1rem 1.2rem;
  max-width: 720px;
}
.add-user-form label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.85rem;
  color: var(--muted);
}
.add-user-form input, .add-user-form select {
  padding: 0.45rem 0.55rem;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  font-size: 0.9rem;
}
