:root {
  color-scheme: dark;
  --navy: #0d1f3c;
  --navy-deep: #081426;
  --panel: #122947;
  --panel-light: #183451;
  --red: #e03c31;
  --red-hover: #f04b40;
  --white: #ffffff;
  --text: #e9eef5;
  --muted: #9caec2;
  --line: #29425e;
  --success: #51c58b;
  --warning: #f1bd5b;
  --error: #ff766d;
  --blue: #83b4ff;
  font-family: Inter, "Segoe UI", Arial, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; min-width: 900px; background: var(--navy-deep); color: var(--text); line-height: 1.5; }
button, input, textarea, select { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .45; }
a { color: inherit; }

.topbar {
  min-height: 82px;
  padding: 16px 36px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  background: var(--navy);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 13px; flex-shrink: 0; }
.brand-mark {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  background: var(--red);
  color: var(--white);
  font-weight: 800;
  letter-spacing: -.05em;
}
.brand strong { display: block; color: var(--white); font-size: 17px; letter-spacing: .02em; }
.brand span:not(.brand-mark) { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }
.auth { display: flex; align-items: center; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.auth input { width: 210px; }
.auth-status { min-width: 8px; color: var(--muted); font-size: 11px; }
.auth-status.ok { color: var(--success); }

.nav { display: flex; flex-wrap: wrap; padding: 0 28px; background: var(--navy); border-bottom: 1px solid var(--line); }
.nav-link {
  padding: 14px 12px 12px;
  border: 0;
  border-bottom: 3px solid transparent;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  text-decoration: none;
}
.nav-link:hover { color: var(--white); }
.nav-link.active { color: var(--white); border-bottom-color: var(--red); }
.live-dot { display: inline-block; width: 7px; height: 7px; margin-left: 5px; border-radius: 50%; background: var(--muted); }
.live-dot.active { background: var(--success); box-shadow: 0 0 0 4px #51c58b20; }

main { width: min(1500px, calc(100% - 72px)); margin: 34px auto 60px; }
.page-heading { display: flex; align-items: flex-end; justify-content: space-between; gap: 30px; margin-bottom: 24px; }
.page-heading > p { max-width: 590px; margin: 0; color: var(--muted); font-size: 14px; }
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 0; color: var(--white); font-size: 30px; line-height: 1.2; }
h2 { margin-bottom: 8px; color: var(--white); font-size: 18px; }
h3 { color: var(--white); font-size: 14px; }
.eyebrow, .step { display: block; margin-bottom: 6px; color: var(--red); font-size: 11px; font-weight: 800; letter-spacing: .14em; text-transform: uppercase; }
.muted { color: var(--muted); font-size: 12px; }
.text-link { color: var(--blue); font-size: 12px; text-decoration: none; }
.text-link:hover { text-decoration: underline; }
.empty-state, .backend-pending { margin: 0; padding: 26px 14px; color: var(--muted); text-align: center; }
.backend-pending { border: 1px dashed var(--line); border-radius: 5px; color: var(--warning); }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 22px; }
.section-heading { display: flex; align-items: center; justify-content: space-between; gap: 20px; margin-bottom: 18px; }
.section-heading h2, .section-heading .eyebrow { margin-bottom: 0; }
.button { border: 1px solid transparent; border-radius: 5px; padding: 9px 16px; font-weight: 650; transition: background .15s, border-color .15s; }
.button.primary { background: var(--red); color: var(--white); }
.button.primary:hover:not(:disabled) { background: var(--red-hover); }
.button.secondary { background: var(--panel-light); border-color: var(--line); color: var(--text); }
.button.secondary:hover { border-color: var(--muted); }
.button.ghost { background: transparent; border-color: var(--line); color: var(--muted); }
.button.ghost:hover { color: var(--white); }

input, textarea, select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 9px 11px;
  background: var(--navy-deep);
  color: var(--text);
  outline: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--red); box-shadow: 0 0 0 2px #e03c3125; }
textarea { resize: vertical; margin-bottom: 14px; }
label { display: block; color: var(--muted); font-size: 12px; }
label input, label select { margin-top: 5px; }

.notice { margin-bottom: 18px; padding: 12px 16px; border-left: 4px solid var(--red); background: #e03c3115; color: var(--text); }
.notice.success { border-color: var(--success); background: #51c58b12; }
.notice.pending { border-color: var(--warning); background: #f1bd5b12; }
.connection-status { padding: 5px 9px; border: 1px solid var(--line); border-radius: 4px; color: var(--muted); font-size: 11px; white-space: nowrap; }
.connection-status.online { border-color: var(--success); color: var(--success); }

.stats { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 16px; }
.stat { min-width: 110px; padding: 12px 15px; border-left: 3px solid var(--line); background: var(--navy-deep); }
.stat strong { display: block; color: var(--white); font-size: 22px; overflow-wrap: anywhere; }
.stat span { color: var(--muted); font-size: 11px; text-transform: uppercase; }
.stat.good { border-color: var(--success); }
.stat.bad { border-color: var(--error); }
.stat.warn { border-color: var(--warning); }
.dashboard-stats { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); margin-bottom: 20px; }
.dashboard-stats .stat { min-width: 0; border: 1px solid var(--line); border-left: 3px solid var(--red); border-radius: 6px; }
.dashboard-stats .stat strong { font-size: 24px; }
.home-grid { display: grid; grid-template-columns: minmax(260px, .7fr) minmax(0, 1.3fr); gap: 20px; }
.qualification-list { display: grid; gap: 8px; }
.qualification-row { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 11px 13px; background: var(--navy-deep); border-radius: 4px; }
.qualification-row span { color: var(--muted); }
.qualification-row strong { color: var(--white); }

.toolbar { display: flex; align-items: flex-end; gap: 16px; margin-bottom: 16px; padding: 15px 18px; }
.toolbar label { width: 220px; }
.toolbar-note { margin-left: auto; color: var(--muted); font-size: 12px; }
.count { margin-left: auto; display: flex; align-items: baseline; gap: 8px; }
.count strong { color: var(--white); font-size: 28px; }
.count span { color: var(--muted); }
.table-card { padding: 0; border: 0; background: transparent; }

.table-wrap { overflow: auto; max-height: 560px; border: 1px solid var(--line); border-radius: 5px; background: var(--panel); }
.preview-table { max-height: 500px; }
.contacts-table { max-height: calc(100vh - 330px); }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { position: sticky; top: 0; z-index: 1; padding: 11px 13px; background: var(--navy); color: var(--muted); text-align: left; font-size: 11px; letter-spacing: .05em; text-transform: uppercase; white-space: nowrap; }
td { padding: 11px 13px; border-top: 1px solid var(--line); color: var(--text); vertical-align: top; }
tbody tr:hover td { background: #ffffff05; }
.empty-row td { padding: 32px; color: var(--muted); text-align: center; }
.badge { display: inline-block; margin: 2px 4px 2px 0; padding: 3px 7px; border-radius: 3px; background: #51c58b18; color: var(--success); font-size: 11px; white-space: nowrap; }
.badge.duplicate { background: #f1bd5b18; color: var(--warning); }
.badge.error { background: #e03c3122; color: var(--error); }
.badge.neutral { background: #9caec218; color: var(--muted); }
.badge.handoff { background: #548bd826; color: var(--blue); }
.daily-cell { min-width: 190px; }
.daily-cell progress { display: block; width: 100%; height: 8px; margin-bottom: 5px; accent-color: var(--red); }
.daily-cell span { color: var(--muted); font-size: 11px; }

.messenger-grid { display: grid; grid-template-columns: 340px minmax(0, 1fr); gap: 0; height: 650px; padding: 0; overflow: hidden; }
.leads-pane { min-width: 0; border-right: 1px solid var(--line); overflow: hidden; }
.pane-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 18px; border-bottom: 1px solid var(--line); }
.pane-heading h2 { margin: 0; }
.lead-list { height: calc(100% - 62px); overflow: auto; }
.lead-item { display: block; width: 100%; padding: 14px 18px; border: 0; border-bottom: 1px solid var(--line); background: transparent; color: var(--text); text-align: left; }
.lead-item:hover, .lead-item.active { background: var(--panel-light); }
.lead-item.active { box-shadow: inset 3px 0 var(--red); }
.lead-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.lead-top strong { overflow: hidden; color: var(--white); text-overflow: ellipsis; }
.lead-meta { display: block; margin: 6px 0; overflow: hidden; color: var(--muted); font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }
.lead-item time { color: var(--muted); font-size: 10px; }
.dialog-pane { display: flex; min-width: 0; flex-direction: column; }
.dialog-heading { display: flex; align-items: center; justify-content: space-between; gap: 20px; min-height: 82px; padding: 14px 20px; border-bottom: 1px solid var(--line); }
.dialog-heading h2, .dialog-heading .eyebrow { margin-bottom: 0; }
.dialog-messages { flex: 1; min-height: 0; padding: 20px; overflow: auto; }
.message-row { display: flex; margin-bottom: 14px; }
.message-row.user { justify-content: flex-end; }
.message-row.system { justify-content: center; }
.message-bubble { max-width: 78%; padding: 11px 14px; border-radius: 10px 10px 10px 2px; background: var(--panel-light); }
.message-row.user .message-bubble { border-radius: 10px 10px 2px 10px; background: #82332f; }
.message-row.system .message-bubble { max-width: 92%; padding: 7px 12px; background: transparent; color: var(--muted); text-align: center; }
.message-bubble p { margin: 3px 0 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.message-meta { color: var(--muted); font-size: 10px; }
.dialog-form { display: flex; align-items: flex-end; gap: 10px; padding: 14px 18px; border-top: 1px solid var(--line); }
.dialog-form textarea { margin: 0; resize: none; }
.dialog-form .button { flex-shrink: 0; }

.prompts-grid { display: grid; grid-template-columns: 300px minmax(0, 1fr); gap: 20px; }
.prompts-sidebar { padding: 0; overflow: hidden; }
.prompt-list { max-height: 650px; overflow: auto; }
.prompt-item { display: block; width: 100%; padding: 13px 18px; border: 0; border-bottom: 1px solid var(--line); background: transparent; color: var(--muted); text-align: left; overflow-wrap: anywhere; }
.prompt-item:hover, .prompt-item.active { background: var(--panel-light); color: var(--white); }
.prompt-item.active { box-shadow: inset 3px 0 var(--red); }
.prompt-editor textarea { min-height: 430px; font-family: Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.55; }
.editor-actions { display: flex; align-items: center; justify-content: flex-end; gap: 16px; }
.prompt-history { margin-top: 18px; color: var(--muted); font-size: 12px; }
.prompt-history ul { margin-bottom: 0; padding-left: 20px; }

.details-card { max-width: 980px; }
.details-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; margin: 0; }
.detail-item { padding: 16px; background: var(--navy-deep); border-radius: 5px; }
.detail-item dt { margin-bottom: 7px; color: var(--muted); font-size: 11px; text-transform: uppercase; }
.detail-item dd { margin: 0; color: var(--white); font-size: 18px; overflow-wrap: anywhere; }
.report-qualification { max-width: 980px; margin-top: 20px; }

.events-list { min-height: 280px; max-height: calc(100vh - 290px); overflow: auto; }
.events-list.compact { min-height: 280px; max-height: 470px; }
.event { display: grid; grid-template-columns: 165px 120px minmax(0, 1fr); gap: 16px; padding: 13px 4px; border-bottom: 1px solid var(--line); font-size: 13px; }
.compact .event { grid-template-columns: 135px 80px minmax(0, 1fr); gap: 10px; font-size: 11px; }
.event time { color: var(--muted); font-variant-numeric: tabular-nums; }
.event-type { color: var(--red); font-weight: 700; word-break: break-word; }
.event-data { color: var(--text); white-space: pre-wrap; word-break: break-word; }

.import-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.import-grid .card { display: flex; flex-direction: column; align-items: stretch; }
.import-grid .card > p { min-height: 42px; color: var(--muted); font-size: 13px; }
.import-grid .button { margin-top: auto; align-self: flex-start; }
.preview-panel, .history-card { margin-top: 20px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 14px; }
.file-drop { margin-bottom: 14px; cursor: pointer; }
.file-drop input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.file-drop span { display: block; padding: 18px; border: 1px dashed #4c6580; border-radius: 5px; text-align: center; color: var(--muted); }
.file-drop:hover span { border-color: var(--red); color: var(--white); }
.mapping-block { margin-bottom: 16px; border: 1px solid var(--line); border-radius: 5px; padding: 12px 14px; }
.mapping-block summary { cursor: pointer; color: var(--text); font-weight: 600; }
.mapping-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; padding-top: 16px; }

.modal { position: fixed; inset: 0; z-index: 10; display: grid; place-items: center; padding: 40px; background: #020814cc; }
.modal-card { width: min(900px, 100%); max-height: calc(100vh - 80px); overflow: auto; box-shadow: 0 24px 80px #0008; }
.modal-card pre { margin: 0; padding: 18px; background: var(--navy-deep); border: 1px solid var(--line); border-radius: 5px; color: var(--text); white-space: pre-wrap; overflow-wrap: anywhere; }

footer { padding: 20px 36px; border-top: 1px solid var(--line); color: var(--muted); font-size: 12px; text-align: center; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

@media (max-width: 1200px) {
  .dashboard-stats { grid-template-columns: repeat(3, 1fr); }
  .topbar { align-items: flex-start; }
  .auth { max-width: 520px; }
  .details-grid { grid-template-columns: repeat(2, 1fr); }
}
