:root {
    --bg: #0f1420;
    --panel: #18202f;
    --panel-2: #1f2937;
    --border: #2b3748;
    --text: #e6e9ef;
    --muted: #8b95a7;
    --accent: #4f7cff;
    --accent-hover: #6b8fff;
    --ok: #34d399;
    --warn: #fbbf24;
    --err: #f87171;
    --radius: 8px;
    --link: #8fb4ff;
    --link-hover: #c3d6ff;
    --row-hover: rgba(255,255,255,0.03);
    color-scheme: dark;
}

/* Light theme: same layout, light surfaces. Toggled via the nav button
   (html[data-theme]), defaulting to the OS preference. */
[data-theme="light"] {
    --bg: #f4f6f9;
    --panel: #ffffff;
    --panel-2: #f0f3f7;
    --border: #d9dfe8;
    --text: #1c2530;
    --muted: #5d6879;
    --accent: #0082d5;
    --accent-hover: #006cb3;
    --ok: #15803d;
    --warn: #a16207;
    --err: #b91c1c;
    --link: #0069b0;
    --link-hover: #004a7c;
    --row-hover: rgba(0,0,0,0.025);
    color-scheme: light;
}

* { box-sizing: border-box; }

/* Links: readable on both themes (browser default blue is unreadable on dark) */
a { color: var(--link); text-decoration: none; }
a:hover { color: var(--link-hover); text-decoration: underline; }
a strong, td a, .kv a { font-weight: 600; }
body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
    line-height: 1.5;
}

/* --- Nav --- */
.topnav {
    display: flex; align-items: center; gap: 16px;
    padding: 0 24px; height: 52px;
    background: var(--panel); border-bottom: 1px solid var(--border);
    position: sticky; top: 0; z-index: 10;
}
.topnav a { color: var(--muted); text-decoration: none; font-weight: 500; }
.topnav a:hover { color: var(--text); }
.brand { color: var(--text) !important; font-weight: 700; font-size: 16px; letter-spacing: 0.5px; }
.nav-sep { color: var(--border); }
.nav-right { margin-left: auto; display: flex; align-items: center; gap: 14px; }
.nav-user { color: var(--muted); font-size: 13px; }

/* --- Layout --- */
.container { max-width: 1240px; margin: 0 auto; padding: 24px; }
h1 { font-size: 22px; margin: 0 0 16px; }
h2 { font-size: 15px; margin: 0 0 12px; }
h3 { margin: 0 0 12px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 16px; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
@media (max-width: 900px) { .two-col { grid-template-columns: 1fr; } }

.panel {
    background: var(--panel); border: 1px solid var(--border);
    border-radius: var(--radius); padding: 20px; margin-bottom: 20px;
}

/* --- Stats --- */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 14px; margin-bottom: 24px; }
.stat { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; display: flex; flex-direction: column; }
.stat-num { font-size: 26px; font-weight: 700; }
.stat-label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px; }

/* --- Tables --- */
.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--muted); padding: 8px 10px; border-bottom: 1px solid var(--border); }
.table td { padding: 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
.table tr:hover td { background: var(--row-hover); }
.table .num { text-align: right; }
.table-tools { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; gap: 12px; }
.msg-body pre { font-family: inherit; white-space: pre-wrap; margin: 0; font-size: 12.5px; color: var(--muted); max-width: 420px; }

/* --- Forms --- */
label { display: block; margin-bottom: 14px; font-weight: 500; font-size: 13px; }
label span { display: block; margin-bottom: 4px; }
input, textarea, select {
    width: 100%; padding: 8px 10px; margin-top: 4px;
    background: var(--panel-2); border: 1px solid var(--border);
    border-radius: 6px; color: var(--text); font: inherit;
}
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--accent); }
input[type="checkbox"] { width: auto; margin: 0; }
input[type="search"] { max-width: 280px; }
.inline-form { display: flex; gap: 10px; margin-top: 16px; align-items: end; flex-wrap: wrap; }
.inline-form input, .inline-form select { width: auto; margin-top: 0; }
.inline { display: inline; }
.prompt textarea { resize: vertical; }

/* --- Buttons --- */
.btn {
    display: inline-block; padding: 8px 16px; border: 1px solid var(--border);
    background: var(--panel-2); color: var(--text); border-radius: 6px;
    font: inherit; font-weight: 500; cursor: pointer; text-decoration: none;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.small { padding: 4px 10px; font-size: 12.5px; }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-row { display: flex; gap: 10px; }
button.link { background: none; border: none; color: var(--muted); font: inherit; cursor: pointer; padding: 0; }
button.link:hover { color: var(--text); }

/* --- Badges --- */
.badge { display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.3px; }
.status-new { background: #2b3748; color: #9fb3d1; }
.status-approved, .status-running { background: #1e3a2f; color: var(--ok); }
.status-paused_leads, .status-paused_messages, .status-draft { background: #3a3320; color: var(--warn); }
.status-sent, .status-contacted { background: #1f2f4a; color: #7ea4f7; }
.status-replied, .status-completed { background: #1e3a2f; color: var(--ok); }
.status-rejected, .status-failed { background: #3d2020; color: var(--err); }
.route-outreach { background: #1e3a2f; color: var(--ok); }
.route-nurture { background: #1f2f4a; color: #7ea4f7; }
.route-long_term { background: #3a3320; color: var(--warn); }
.route-reject { background: #3d2020; color: var(--err); }
.actor-agent { background: #2b2545; color: #a89bf5; }
.actor-human { background: #1f2f4a; color: #7ea4f7; }
.actor-system { background: #2b3748; color: #9fb3d1; }
.evt-opened, .evt-sent { background: #1f2f4a; color: #7ea4f7; }
.evt-clicked { background: #2b2545; color: #a89bf5; }
.evt-replied { background: #1e3a2f; color: var(--ok); }
.evt-bounced, .evt-unsubscribed { background: #3d2020; color: var(--err); }

/* --- Flash --- */
.flash { padding: 12px 16px; border-radius: var(--radius); margin-bottom: 16px; }
.flash-ok { background: #1e3a2f; color: var(--ok); border: 1px solid #2a5243; }
.flash-err { background: #3d2020; color: var(--err); border: 1px solid #5a3030; }

/* --- Auth --- */
.auth-card {
    max-width: 440px; margin: 8vh auto 0; padding: 32px;
    background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
}
.auth-card h1 { margin-bottom: 8px; }
.auth-card form { margin-top: 20px; }
.auth-card .btn { width: 100%; }
.qr-box { display: flex; justify-content: center; margin: 18px 0; }
.qr-box svg { width: 200px; height: 200px; background: #fff; padding: 10px; border-radius: 8px; }
.totp-secret { display: flex; align-items: center; gap: 10px; margin: 16px 0; }
.totp-secret code { font-size: 16px; letter-spacing: 2px; background: var(--panel-2); padding: 8px 14px; border-radius: 6px; }
code.wrap { word-break: break-all; display: block; margin: 8px 0; font-size: 12px; }
.recovery-codes { columns: 2; list-style: none; padding: 0; }
.recovery-codes li { margin-bottom: 6px; }
.recovery-codes code { background: var(--panel-2); padding: 4px 10px; border-radius: 4px; }

/* --- Misc --- */
.muted { color: var(--muted); }
.small { font-size: 12px; }
.kv { display: grid; grid-template-columns: 160px 1fr; gap: 6px 16px; margin: 12px 0 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.audit-list { list-style: none; padding: 0; margin: 0; }
.audit-list li { padding: 8px 0; border-bottom: 1px solid var(--border); display: flex; gap: 10px; align-items: baseline; flex-wrap: wrap; }
dialog { background: var(--panel); color: var(--text); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; width: min(560px, 92vw); }
dialog::backdrop { background: rgba(0,0,0,0.6); }
details summary { cursor: pointer; color: var(--muted); }
details summary:hover { color: var(--text); }

/* --- Onboarding wizard & prefill --- */
.prefill-panel { margin-bottom: 20px; }
.prefill-panel h3 { margin: 0 0 12px; font-size: 15px; }
.prefill-form label { display: block; margin-bottom: 10px; font-size: 13px; color: var(--muted); }
.prefill-row { display: flex; gap: 10px; align-items: flex-end; }
.prefill-row label { flex: 1; margin-bottom: 0; }
.prefill-row .btn { flex-shrink: 0; }
.brand-form label, .brand-panel > label { display: block; margin-bottom: 10px; font-size: 13px; color: var(--muted); }
.brand-interview { margin-bottom: 12px; }
.brand-interview summary { cursor: pointer; font-size: 13px; color: var(--accent); margin-bottom: 10px; }
.brand-row { display: flex; gap: 10px; }
.brand-row label { flex: 1; }
.brand-profile-view textarea { font-family: ui-monospace, monospace; font-size: 12px; }
form.inline { display: inline; }
.btn.danger { background: var(--danger, #c0392b); color: #fff; border-color: transparent; }
.archived-list { margin-top: 12px; }
.archived-list summary { cursor: pointer; }
.danger-zone .danger-row { display: flex; gap: 12px; align-items: center; margin: 10px 0; }
.danger-zone .danger-row .btn { flex-shrink: 0; }
.app-footer { text-align: center; padding: 18px 0 10px; font-size: 11px; color: var(--muted); }
.badge.unverified { background: #7a5d00; color: #ffe9a8; font-size: 10px; vertical-align: super; margin-left: 4px; cursor: help; }
.wizard-progress { display: flex; align-items: center; gap: 14px; margin: 18px 0 10px; }
.wizard-bar { flex: 1; height: 6px; background: var(--panel-2); border-radius: 3px; overflow: hidden; }
.wizard-fill { height: 100%; width: 0; background: var(--accent); border-radius: 3px; transition: width .25s ease; }
.wizard-count { font-size: 12px; color: var(--muted); white-space: nowrap; }
.wizard { border: none; padding: 0; }
.wizard-step { border: none; padding: 0; margin: 0; }
.wizard-step legend { font-size: 15px; font-weight: 600; margin-bottom: 12px; padding: 0; }
.wizard-nav { display: flex; gap: 10px; margin-top: 18px; }
.wizard-nav .btn.primary { margin-left: auto; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; margin: -6px 0 14px; }
.chip { background: var(--panel-2); border: 1px solid var(--border); color: var(--muted); border-radius: 999px; padding: 4px 14px; font-size: 12.5px; cursor: pointer; }
.chip:hover { border-color: var(--accent); color: var(--text); }
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* --- Background cycle banner --- */
.cycle-banner { background: #1d2f45; border: 1px solid #2e4a6b; color: #9ec3ea; padding: 12px 16px; border-radius: var(--radius); margin-bottom: 16px; font-size: 13.5px; }
.cycle-banner.failed { background: #3d2020; border-color: #5a3030; color: #f87171; }
.badge.status-queued, .badge.status-running { background: #1d2f45; color: #9ec3ea; border-color: #2e4a6b; }
.add-lead summary { cursor: pointer; }
.lead-add-form { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.lead-add-form input { flex: 1 1 160px; }
.doc-preview { line-height: 1.65; }
.doc-preview h2, .doc-preview h3 { margin-top: 1.2em; }
select { padding: 8px 10px; font-size: 14px; }
.badge.status-pending { background: #5b4a00; color: #ffe9a8; }
.badge.status-approved { background: #14532d; color: #b7f7cd; }
.badge.status-published { background: #1e3a5f; color: #bcd8ff; }
.badge.status-draft { background: #3a4152; color: #c8cfdd; }
.badge.status-used { background: #3a4152; color: #9aa4bb; }
.badge.status-rejected { background: #4a2020; color: #f0b6b6; }
.lead-views { display: flex; gap: 10px; align-items: center; margin: 14px 0 8px; font-size: 13px; }
.research-kv dd { margin-bottom: 10px; }
ul.tight { margin: 0; padding-left: 18px; }
ul.flags li, .flag { color: #f0a35a; }
tbody.sequence tr.sequence-head td { background: rgba(0,130,213,.08); border-top: 2px solid rgba(0,130,213,.35); padding-top: 10px; }
td.nowrap { white-space: nowrap; }
.badge.status-cancelled { background: #3a4152; color: #9aa4bb; }
.badge.status-sent { background: #14532d; color: #b7f7cd; }

/* --- Theme toggle + light-theme fixes for hard-coded dark colours --- */
.theme-toggle { background: none; border: 1px solid var(--border); color: var(--muted); border-radius: 999px;
    padding: 3px 10px; font: inherit; font-size: 12px; cursor: pointer; }
.theme-toggle:hover { color: var(--text); border-color: var(--accent); }
.topnav .theme-toggle { margin-left: auto; }
.topnav .nav-right { margin-left: 14px; }
[data-theme="light"] .status-new, [data-theme="light"] .actor-system { background: #e6ebf2; color: #3d4b60; }
[data-theme="light"] .status-approved, [data-theme="light"] .status-running, [data-theme="light"] .status-replied,
[data-theme="light"] .status-completed, [data-theme="light"] .route-outreach, [data-theme="light"] .evt-replied,
[data-theme="light"] .badge.status-approved, [data-theme="light"] .badge.status-sent { background: #dcfce7; color: #166534; }
[data-theme="light"] .status-paused_leads, [data-theme="light"] .status-paused_messages, [data-theme="light"] .status-draft,
[data-theme="light"] .route-long_term, [data-theme="light"] .badge.status-pending, [data-theme="light"] .badge.status-draft { background: #fef3c7; color: #92400e; }
[data-theme="light"] .status-contacted, [data-theme="light"] .route-nurture, [data-theme="light"] .actor-human,
[data-theme="light"] .evt-opened, [data-theme="light"] .evt-sent, [data-theme="light"] .badge.status-published,
[data-theme="light"] .badge.status-queued, [data-theme="light"] .badge.status-running { background: #dbeafe; color: #1e40af; }
[data-theme="light"] .status-rejected, [data-theme="light"] .status-failed, [data-theme="light"] .route-reject,
[data-theme="light"] .evt-bounced, [data-theme="light"] .evt-unsubscribed, [data-theme="light"] .badge.status-rejected { background: #fee2e2; color: #991b1b; }
[data-theme="light"] .actor-agent, [data-theme="light"] .evt-clicked { background: #ede9fe; color: #5b21b6; }
[data-theme="light"] .badge.status-used, [data-theme="light"] .badge.status-cancelled { background: #eceff3; color: #5d6879; }
[data-theme="light"] .badge.unverified { background: #fef3c7; color: #92400e; }
[data-theme="light"] .flash-ok { background: #dcfce7; color: #166534; border-color: #bbf7d0; }
[data-theme="light"] .flash-err { background: #fee2e2; color: #991b1b; border-color: #fecaca; }
[data-theme="light"] .cycle-banner { background: #e0f0fb; border-color: #b6dcf5; color: #0b4f7c; }
[data-theme="light"] .cycle-banner.failed { background: #fee2e2; border-color: #fecaca; color: #991b1b; }
[data-theme="light"] ul.flags li, [data-theme="light"] .flag { color: #b45309; }
[data-theme="light"] dialog::backdrop { background: rgba(20,30,50,0.35); }

/* --- Inbound tabs, filters, tags, images --- */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 20px; }
.tab { padding: 9px 16px; font-weight: 600; color: var(--muted); border: 1px solid transparent;
    border-bottom: none; border-radius: 8px 8px 0 0; }
.tab:hover { color: var(--text); text-decoration: none; }
.tab.active { color: var(--text); background: var(--panel); border-color: var(--border); position: relative; top: 1px; }
.tab-count { background: var(--panel-2); border: 1px solid var(--border); border-radius: 999px;
    padding: 0 7px; font-size: 11px; margin-left: 4px; }
.tab.active .tab-count { background: var(--accent); border-color: var(--accent); color: #fff; }

.filters { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin: 0 0 14px; }
.filters input, .filters select { width: auto; margin-top: 0; }
.filters input[type="search"] { min-width: 220px; }

.tag-chip { display: inline-block; background: var(--panel-2); border: 1px solid var(--border);
    color: var(--muted); border-radius: 999px; padding: 1px 10px; font-size: 11.5px; margin: 1px 2px; }
.tag-chip:hover { border-color: var(--accent); color: var(--text); text-decoration: none; }
.tag-edit summary { display: inline-block; }
.tag-form { position: absolute; z-index: 5; background: var(--panel); border: 1px solid var(--border);
    border-radius: var(--radius); padding: 12px; min-width: 240px; box-shadow: 0 8px 24px rgba(0,0,0,.35); }
.tag-edit { position: relative; }
.tag-opt { display: block; font-size: 13px; font-weight: 400; margin-bottom: 4px; }
.tag-opt input { margin-right: 6px; }
.tag-form input[name="new_tags"] { margin: 6px 0; font-size: 12.5px; }
.tag-manage { margin-top: 16px; }
.tag-manage summary { cursor: pointer; color: var(--muted); }
.tag-manage .chip { cursor: default; }
.tag-manage .chip form { display: inline; margin-left: 4px; }
.tag-manage .chip .link { color: var(--muted); font-size: 13px; }
.tag-manage .chip .link:hover { color: var(--err); }
.tag-fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 16px; margin: 14px 0; }
.tag-fieldset legend { font-size: 12px; color: var(--muted); padding: 0 6px; }
.tag-fieldset .chip { display: inline-block; }
.tag-fieldset .chip input[hidden] { display: none; }

.img-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 12px; margin: 14px 0; }
.img-grid figure { margin: 0; }
.img-grid img { width: 100%; height: 110px; object-fit: cover; border-radius: 6px; display: block; }
.img-grid figcaption { margin: 4px 0 6px; }
.img-current { display: flex; gap: 16px; align-items: flex-start; margin-bottom: 14px; }
.img-current img { width: 240px; border-radius: 6px; }
.img-credit p { margin: 0 0 12px; }
.doc-img { max-width: 100%; border-radius: 6px; }
.share-post { white-space: pre-wrap; font: inherit; font-size: 13.5px; background: var(--panel-2);
    border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; margin: 0 0 10px; }
