:root {
  color-scheme: light;
  --bg: #f4f6f8;
  --paper: #f8fafc;
  --surface: #ffffff;
  --sidebar: #f9fafb;
  --sidebar-active: #deebe7;
  --sidebar-text: #42515e;
  --text: #1e293b;
  --muted: #64748b;
  --border: #e2e8f0;
  --accent: #0d9488;
  --accent-dark: #0f766e;
  --danger: #a83232;
  --warning: #9a5b00;
  font-family: "Segoe UI", Arial, sans-serif;
}

* { box-sizing: border-box; }
body { margin: 0; color: var(--text); background: var(--bg); }
a { color: inherit; text-decoration: none; }
button, select, input { font: inherit; }

.topbar {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 12px; color: var(--text); }
.brand-copy { display: grid; gap: 1px; line-height: 1.15; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  color: white;
  background: var(--accent);
  font-weight: 700;
  font-size: 14px;
  flex-shrink: 0;
}
.brand strong { display: block; font-size: 14px; letter-spacing: -.01em; }
.brand small { display: block; color: var(--muted); font-size: 10px; }
.session { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.session select { border: 1px solid var(--border); border-radius: 6px; padding: 6px 28px 6px 9px; background: white; }

.shell { display: grid; grid-template-columns: 180px minmax(0, 1fr); min-height: calc(100vh - 56px); }
.sidebar { padding: 20px 10px; border-right: 1px solid var(--border); background: var(--sidebar); }
.sidebar a {
  display: block;
  padding: 8px 12px;
  margin-bottom: 2px;
  border-radius: 6px;
  color: var(--sidebar-text);
  font-size: 13px;
  line-height: 1.35;
}
.sidebar a:hover { background: var(--sidebar-active); color: var(--accent-dark); }
.sidebar a.active { color: var(--accent-dark); background: var(--sidebar-active); font-weight: 600; }
main { width: 100%; max-width: 1280px; padding: 28px 32px; }

.page-heading, .panel-heading, .queue-item, .action-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}
.page-heading { margin-bottom: 22px; }
h1, h2, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: 27px; letter-spacing: -.02em; }
h2 { margin-bottom: 0; font-size: 17px; }
.eyebrow { margin-bottom: 4px; color: var(--accent); font-size: 12px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; }
.muted, .queue-item p, .footnote { color: var(--muted); }
.muted { display: block; margin-top: 4px; font-size: 12px; }
.count { color: var(--muted); font-size: 13px; }

.metric-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; margin-bottom: 18px; }
.metric-card, .panel {
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  box-shadow: 0 1px 2px rgba(20, 35, 45, .04);
}
.metric-card { padding: 18px; }
.metric-card span, .metric-card small { display: block; color: var(--muted); }
.metric-card strong { display: block; margin: 8px 0 4px; font-size: 29px; }
.metric-card small { font-size: 11px; }
.panel { padding: 20px; margin-bottom: 18px; }
.panel-heading { padding-bottom: 15px; border-bottom: 1px solid var(--border); }
.two-column { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; }
.queue-item { padding: 14px 0; border-bottom: 1px solid var(--border); }
.queue-item:last-child { border-bottom: 0; }
.queue-item p { margin: 4px 0 0; font-size: 13px; }
.queue-item .excerpt { margin-top: 8px; font-size: 13px; line-height: 1.45; color: var(--text); }
.queue-item .link-row { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 8px; }
.message-list { list-style: none; margin: 12px 0 0; padding: 0; }
.message-item { padding: 12px 0; border-bottom: 1px solid var(--border); }
.message-item:last-child { border-bottom: 0; }
.message-item p { margin: 6px 0 0; font-size: 13px; line-height: 1.45; white-space: pre-wrap; }
.message-meta { display: flex; justify-content: space-between; gap: 12px; font-size: 12px; color: var(--muted); }
.message-item.outbound .message-meta strong { color: var(--accent-dark); }
.message-item.inbound .message-meta strong { color: #6b3a12; }

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 8px 13px;
  color: white;
  background: var(--accent);
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
}
.button:hover { background: var(--accent-dark); }
.button.secondary { color: #31434f; background: white; border-color: var(--border); }
.button.small { padding: 6px 10px; }
.button:disabled { cursor: wait; opacity: .6; }
.action-row { justify-content: flex-start; padding-top: 16px; }

.badge { display: inline-block; border-radius: 999px; padding: 4px 8px; color: #43515c; background: #e9edf0; font-size: 11px; font-weight: 700; white-space: nowrap; }
.badge-safe, .badge-ok { color: var(--accent-dark); background: var(--sidebar-active); }
.badge-urgent { color: #8a3900; background: #ffe6cc; }
.badge-danger { color: #8e2929; background: #f9dede; }
.notice { margin-bottom: 18px; padding: 11px 14px; border: 1px solid #edd3a8; border-radius: 7px; color: #71501d; background: #fff8e8; font-size: 13px; }

.table-wrap { overflow-x: auto; padding: 0; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 13px 16px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
th { color: var(--muted); background: var(--sidebar); font-size: 11px; letter-spacing: .04em; text-transform: uppercase; }
td strong { display: block; }
tbody tr:last-child td { border-bottom: 0; }
.empty { padding: 55px 20px; text-align: center; color: var(--muted); }
.empty h2 { margin-bottom: 8px; color: var(--text); }
.empty p { margin-bottom: 0; }
.empty.compact { padding: 24px 10px; }
.check-list { padding-top: 12px; }
.check-row { display: flex; justify-content: space-between; padding: 9px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.footnote { font-size: 12px; }
.text-link { color: var(--accent); font-weight: 600; }
.lead-layout { grid-template-columns: minmax(0, 1.35fr) minmax(320px, .65fr); align-items: start; }
.detail-list { margin: 15px 0 0; }
.detail-list div { display: grid; grid-template-columns: 160px 1fr; gap: 12px; padding: 9px 0; border-bottom: 1px solid var(--border); }
.detail-list dt { color: var(--muted); font-size: 12px; }
.detail-list dd { margin: 0; font-size: 13px; }
.source-text { margin: 16px 0 0; padding: 13px; border-radius: 7px; background: #f5f7f8; white-space: pre-wrap; font-size: 13px; line-height: 1.5; }
.source-text.compact { margin-top: 8px; padding: 10px; max-height: 7.5em; overflow: auto; }
.prompt-rule { align-items: flex-start; }
#pi-draft-edit {
  width: 100%;
  min-height: 120px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  resize: vertical;
  font: inherit;
  line-height: 1.45;
}

.modal-dialog {
  border: 0;
  padding: 0;
  margin: auto;
  max-width: min(720px, calc(100vw - 24px));
  width: 100%;
  background: transparent;
  color: inherit;
}
.modal-dialog::backdrop {
  background: rgba(18, 28, 36, 0.48);
}
.modal-card {
  margin: 0;
  padding: 22px 22px 18px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: 0 18px 48px rgba(16, 28, 36, 0.18);
  max-height: min(88vh, 900px);
  overflow: auto;
}
.modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}
.modal-header h2 { margin: 4px 0 0; font-size: 1.25rem; }
.modal-actions { margin-top: 8px; }

.channel-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 14px;
  margin-top: 14px;
}
.channel-card {
  display: grid;
  gap: 8px;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fbfcfd;
  min-height: 168px;
  align-content: start;
}
.channel-card.is-disabled { opacity: 0.72; background: #f3f5f7; }
.channel-card-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
.channel-account { margin: 0; font-size: 13px; line-height: 1.4; min-height: 2.6em; }
.channel-account .muted, .muted { color: var(--muted); }
.integration-fields { display: grid; gap: 10px; }
.button.danger-ghost {
  color: var(--danger);
  border-color: #e2bdbd;
  background: #fff;
}
.button.danger-ghost:hover { background: #fdf2f2; }
.contact-card { display: grid; gap: 5px; padding: 14px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.contact-card:last-child { border-bottom: 0; }
.contact-card span { color: var(--muted); }
.transition-form { display: grid; gap: 13px; margin-top: 16px; }
.transition-form label { display: grid; gap: 6px; color: #42515e; font-size: 13px; font-weight: 600; }
.transition-form select, .transition-form input { width: 100%; padding: 9px 10px; border: 1px solid var(--border); border-radius: 6px; background: white; }
.transition-form .button { justify-self: start; }
.timeline { margin: 17px 0 0; padding: 0; list-style: none; }
.timeline li { position: relative; margin-left: 7px; padding: 0 0 20px 20px; border-left: 2px solid var(--border); }
.timeline li::before { position: absolute; top: 2px; left: -6px; width: 10px; height: 10px; border-radius: 50%; background: var(--accent); content: ""; }
.timeline li:last-child { padding-bottom: 0; }
.timeline li > div { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.timeline p { margin: 5px 0 0; color: var(--muted); font-size: 12px; }
.timeline time { display: block; margin-top: 6px; color: #8a969f; font-size: 11px; }
.toast { position: fixed; right: 20px; bottom: 20px; display: none; max-width: 360px; padding: 12px 15px; border-radius: 7px; color: white; background: #25343e; font-size: 13px; box-shadow: 0 5px 20px rgba(0,0,0,.18); }
.toast.visible { display: block; }

.auth-page { display: grid; min-height: 100vh; place-items: center; padding: 24px; }
.auth-card { width: min(420px, 100%); padding: 32px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); box-shadow: 0 8px 30px rgba(20, 35, 45, .08); }
.auth-card h1 { margin: 26px 0 22px; }
.auth-card form { display: grid; gap: 15px; }
.auth-card label { display: grid; gap: 6px; color: #42515e; font-size: 13px; font-weight: 600; }
.auth-card input { width: 100%; padding: 10px 11px; border: 1px solid var(--border); border-radius: 6px; }
.auth-brand { justify-content: flex-start; }
.auth-switch { margin: 20px 0 0; color: var(--muted); font-size: 13px; }
.auth-switch a { color: var(--accent); font-weight: 600; }
.form-error { min-height: 18px; margin: 0; color: var(--danger); font-size: 13px; }
.button-row { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.data-table { width: 100%; border-collapse: collapse; margin-top: 12px; font-size: 13px; }
.data-table th, .data-table td { padding: 10px 8px; border-bottom: 1px solid var(--border); text-align: left; }
.data-table th { color: var(--muted); font-weight: 600; }
.transition-form textarea { width: 100%; padding: 9px 10px; border: 1px solid var(--border); border-radius: 6px; background: white; font: inherit; }
.inline-check {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 13px;
}
.inline-check.tight { margin: 0; }
.sources-form {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px 14px;
  align-items: end;
  margin-top: 8px;
}
.sources-form.compact { grid-template-columns: 1fr auto; }
.sources-form .field { display: grid; gap: 6px; }
.sources-form .field span { color: #42515e; font-size: 12px; font-weight: 600; }
.sources-form input,
.sources-form select {
  width: 100%;
  min-width: 0;
  padding: 10px 11px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: white;
}
.sources-form .actions { align-self: end; }
.sources-form .grow { grid-column: 1 / -2; }
.check-field { align-content: end; }
.subscription-form { margin-top: 12px; margin-bottom: 8px; }
.direction-list { display: grid; gap: 10px; margin-top: 14px; }
.direction-row {
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fbfcfd;
}
.direction-row-main {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}
.direction-row-main strong { display: block; font-size: 14px; }
.direction-row-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.direction-row-actions { margin-top: 10px; }
.direction-row-error { margin: 8px 0 0; min-height: 0; }

.channel-list, .discover-results { display: grid; gap: 8px; margin-top: 12px; }
.channel-row, .discover-card, .topic-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fbfcfd;
}
.discover-card { align-items: flex-start; }
.topics-editor { display: grid; gap: 8px; margin: 10px 0; }
.topic-row input { flex: 1; min-width: 0; padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; }
.topic-row .topic-code { max-width: 180px; }
@media (max-width: 960px) {
  .sources-form { grid-template-columns: 1fr 1fr; }
  .sources-form.compact { grid-template-columns: 1fr; }
  .sources-form .grow { grid-column: auto; }
}
.inline-form { display: flex; flex-wrap: wrap; gap: 10px; align-items: end; margin: 12px 0 8px; }
.inline-form label, .stack-form label { display: grid; gap: 6px; color: #42515e; font-size: 13px; font-weight: 600; }
.inline-form input, .stack-form input, .stack-form select {
  min-width: 220px; padding: 9px 10px; border: 1px solid var(--border); border-radius: 6px; background: white;
}
.stack-form { display: grid; gap: 12px; margin-top: 8px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
@media (max-width: 800px) {
  .form-grid { grid-template-columns: 1fr; }
}

.funnel-panel { overflow: visible; }
.sales-funnel {
  display: grid;
  gap: 10px;
  padding: 20px 8px;
  border-radius: 14px;
  background:
    radial-gradient(ellipse at 20% 0%, rgba(13, 148, 136, 0.10), transparent 50%),
    linear-gradient(180deg, #f7faf9, #eef3f1);
}
.sales-funnel-row {
  display: grid;
  grid-template-columns: minmax(140px, 180px) minmax(0, 1fr) 64px;
  align-items: center;
  gap: 10px;
}
.sf-side {
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: #1d2935;
  line-height: 1.2;
}
.sf-side-right {
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: #42515e;
}
.sales-funnel-band {
  width: var(--band, 100%);
  max-width: 100%;
  justify-self: center;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 48px;
  padding: 12px 20px;
  color: #fff;
  font-weight: 700;
  border-radius: 8px;
  box-shadow: 0 8px 18px rgba(16, 40, 34, 0.12);
}
.sf-count { font-size: 20px; font-variant-numeric: tabular-nums; }
.tone-0 .sales-funnel-band { background: linear-gradient(90deg, #19b7c8, #0e95a8); }
.tone-1 .sales-funnel-band { background: linear-gradient(90deg, #7b4fd6, #5b2fb8); }
.tone-2 .sales-funnel-band { background: linear-gradient(90deg, #e14b4b, #c23030); }
.tone-3 .sales-funnel-band { background: linear-gradient(90deg, #3aa35f, #2a8349); }
.tone-4 .sales-funnel-band { background: linear-gradient(90deg, #d4a017, #b8860b); color: #1d1808; }
@media (max-width: 800px) {
  .sales-funnel-row { grid-template-columns: 1fr; gap: 4px; }
  .sf-side-right { text-align: left; }
  .sales-funnel-band { width: 100% !important; }
}
.compose-box { display: grid; gap: 10px; margin-top: 12px; }
.compose-box textarea {
  width: 100%;
  min-height: 110px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  resize: vertical;
}

@media (max-width: 800px) {
  .topbar { padding: 0 15px; }
  .brand small, .session label { display: none; }
  .shell { grid-template-columns: 1fr; }
  .sidebar { display: flex; overflow-x: auto; padding: 8px; border-right: 0; border-bottom: 1px solid var(--border); }
  .sidebar a { white-space: nowrap; margin: 0 3px 0 0; }
  main { padding: 20px 14px; }
  .metric-grid, .two-column, .lead-layout { grid-template-columns: 1fr; }
  .page-heading { align-items: flex-end; }
}
