
:root {
    --bg: #16181d; --card: #1f2229; --text: #e7e9ee; --muted: #9aa1ad;
    --line: #2c3038; --accent: #5b8cff; --ok: #35c46a; --warn: #e0a33a; --bad: #ef6a5e;
    --scroll: #3d434d;
    /* funnel slices - validated for dark surfaces */
    --f1: #199e70; --f2: #c98500; --f3: #3987e5; --f4: #d95926; --f5: #8f6fd6;
    color-scheme: dark;
}
/* Light theme only when the user picks it with the switch in the menu */
:root[data-theme="light"] {
    --bg: #f3f5f8; --card: #ffffff; --text: #1c1e21; --muted: #6b7280;
    --line: #e1e5ea; --accent: #2f6fed; --ok: #15803d; --warn: #b45309; --bad: #b42318;
    --f1: #1baf7a; --f2: #eda100; --f3: #2a78d6; --f4: #eb6834; --f5: #8a63d9;
    --scroll: #c9cfd8;
    color-scheme: light;
}
.theme-btn { padding: 5px 10px; font-size: 13px; }
/* Thin scrollbars that match the theme (tables, dashboard columns) */
.leads, .scroll, .dash .col.side { scrollbar-width: thin; scrollbar-color: var(--scroll) transparent; }
.leads::-webkit-scrollbar, .scroll::-webkit-scrollbar, .dash .col.side::-webkit-scrollbar { width: 8px; height: 8px; }
.leads::-webkit-scrollbar-track, .scroll::-webkit-scrollbar-track, .dash .col.side::-webkit-scrollbar-track { background: transparent; }
.leads::-webkit-scrollbar-thumb, .scroll::-webkit-scrollbar-thumb, .dash .col.side::-webkit-scrollbar-thumb {
    background: var(--scroll); border-radius: 999px; border: 2px solid var(--card); }
.leads::-webkit-scrollbar-thumb:hover, .scroll::-webkit-scrollbar-thumb:hover { background: var(--muted); }
.leads { scrollbar-gutter: stable; }
.switch { flex-wrap: wrap; }



* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text);
       font: 15px/1.5 -apple-system, "Segoe UI", Roboto, sans-serif; }
.wrap { max-width: 1700px; margin: 0 auto; padding: 16px 24px; }
.footer { margin-top: 24px; padding-top: 12px; border-top: 1px solid var(--line); color: var(--muted); font-size: 12px; text-align: center; }
h1 { font-size: 20px; margin: 0; }
h2 { font-size: 16px; margin: 24px 0 10px; }
a { color: var(--accent); }
.head { display: flex; flex-wrap: wrap; gap: 12px; align-items: center;
        justify-content: space-between; margin-bottom: 16px; }
.funnel { display: flex; flex-direction: column; gap: 12px; align-items: center; }
.donut { width: 160px; height: 160px; }
.donut .f1 { stroke: var(--f1); } .donut .f2 { stroke: var(--f2); } .donut .f3 { stroke: var(--f3); }
.donut .f4 { stroke: var(--f4); } .donut .f5 { stroke: var(--f5); }
.donut-n { font-size: 24px; font-weight: 600; fill: var(--text); }
.donut-l { font-size: 9px; fill: var(--muted); }
.fl { width: 100%; display: flex; flex-direction: column; gap: 6px; font-size: 13px; }
.fl-row { display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.fl-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.fl-pct { width: 36px; text-align: right; }
.dot { width: 10px; height: 10px; border-radius: 3px; flex: 0 0 10px; }
.dot.f1 { background: var(--f1); } .dot.f2 { background: var(--f2); } .dot.f3 { background: var(--f3); }
.dot.f4 { background: var(--f4); } .dot.f5 { background: var(--f5); }
.topic { margin-bottom: 10px; }
.topic-ex { font-size: 12px; margin-top: 3px; }
.advice { margin: 6px 0 8px; padding-left: 18px; }
.advice li { margin-bottom: 3px; }
button.mini { padding: 3px 8px; font-size: 12px; }
.srv { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; font-size: 13px;
       padding: 6px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--card); }
.srv .sep { width: 1px; height: 14px; background: var(--line); }
.srv b.ok { color: var(--ok); } .srv b.warn { color: var(--warn); } .srv b.bad { color: var(--bad); }
.srv a { text-decoration: none; }
.pill.warn-pill { color: var(--warn); border-color: var(--warn); text-decoration: none; }
.sugg textarea { min-height: 56px; font-family: inherit; }
button.mini { padding: 4px 10px; font-size: 12px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 14px; }
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; }
.tile .n { font-size: 24px; font-weight: 600; }
.tile .l { color: var(--muted); font-size: 13px; }
.switch { display: flex; align-items: center; gap: 10px; }
.pill { padding: 3px 10px; border-radius: 999px; font-size: 12px; border: 1px solid var(--line); white-space: nowrap; }
.pill.on { background: rgba(20,128,60,.12); color: var(--ok); border-color: transparent; }
.pill.off { background: rgba(180,35,24,.12); color: var(--bad); border-color: transparent; }
.pill.free { background: rgba(20,128,60,.12); color: var(--ok); border-color: transparent; }
.pill.taken { background: rgba(180,35,24,.12); color: var(--bad); border-color: transparent; }
button, .btn { font: inherit; line-height: 1.3; padding: 8px 14px; border-radius: 8px; border: 1px solid var(--line);
               background: var(--card); color: var(--text); cursor: pointer; text-decoration: none;
               display: inline-flex; align-items: center; justify-content: center; box-sizing: border-box; vertical-align: middle; }
.row form:not(.tgl) { display: contents; }
td .row { flex-wrap: nowrap; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.danger { background: var(--bad); border-color: var(--bad); color: #fff; }
input, select, textarea { font: inherit; padding: 8px 10px; border-radius: 8px;
                          border: 1px solid var(--line); background: var(--card); color: var(--text); }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 8px; border-bottom: 1px solid var(--line); font-size: 14px; }
th { color: var(--muted); font-weight: 500; font-size: 13px; }
tr:last-child td { border-bottom: none; }
.scroll { overflow-x: auto; }
/* Leads table: about ten rows tall, then scrolls inside the card */
.leads { overflow: auto; max-height: 640px; border: 1px solid var(--line); border-radius: 8px; }
.leads table { min-width: 760px; }
/* Dashboard: stats column | leads | analytics. On wide screens all three columns share
   one height that fits the viewport; each scrolls inside itself. Stacks when narrow. */
.dash { display: grid; grid-template-columns: 1fr; gap: 16px; align-items: stretch; }
.dash h2 { margin-top: 0; }
.dash .card + h2 { margin-top: 18px; }
.dash .col > .card { margin-bottom: 0; }
.dash .tiles { grid-template-columns: 1fr; }
.dash td, .dash th { white-space: nowrap; }
.dash td:first-child { white-space: normal; }
@media (min-width: 1200px) {
    .dash { grid-template-columns: 170px minmax(0, 1fr) 384px; height: calc(100vh - 250px); min-height: 640px; }
    .dash .col { display: flex; flex-direction: column; min-height: 0; }
    .dash .col.stats .tiles { flex: 1; display: flex; flex-direction: column; }
    .dash .col.stats .tile { flex: 1; display: flex; flex-direction: column; justify-content: center; }
    .dash .col.main > .card { flex: 1; display: flex; flex-direction: column; min-height: 0; }
    .dash .col.main .leads { flex: 1; max-height: none; min-height: 0; }
    /* the side column is as tall as the other two; the open card fills it and scrolls inside */
    .dash .col.side { overflow: hidden; flex-direction: row; align-items: stretch; }
    .dash .col.side .side-body { flex: 1; min-width: 0; }
    .dash .col.side .side-panel:not([hidden]) { display: flex; flex-direction: column; flex: 1; min-height: 0; }
    .dash .col.side .side-panel > .card { flex: 1; min-height: 0; overflow: auto; border-top-right-radius: 0; border-bottom-right-radius: 0; }
    /* bookmark tabs: a column of rotated labels glued to the card's right edge */
    .dash .side-tabs { order: 2; flex: 0 0 auto; flex-direction: column; flex-wrap: nowrap; gap: 6px; margin: 14px 0 0 -1px; padding: 0; overflow: visible; }
    .dash .side-tabs .tab-btn { flex: none; writing-mode: vertical-rl; padding: 14px 7px; font-size: 13px;
        border-radius: 0 9px 9px 0; border-left: 1px solid var(--line); background: var(--card); color: var(--muted); }
    .dash .side-tabs .tab-btn:hover { color: var(--text); }
    .dash .side-tabs .tab-btn.on { background: var(--accent); border-color: var(--accent); color: #fff; }
}
.leads thead th { position: sticky; top: 0; background: var(--card); z-index: 1;
                  box-shadow: 0 1px 0 var(--line); }
.msg { padding: 9px 12px; border-radius: 10px; margin: 8px 0; max-width: 78%; white-space: pre-wrap;
       word-wrap: break-word; }
.msg.in { background: var(--bg); border: 1px solid var(--line); }
.msg.out { background: rgba(47,111,237,.12); margin-left: auto; }
.msg .t { display: block; color: var(--muted); font-size: 11px; margin-top: 5px; }
.muted { color: var(--muted); }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
/* search and the status filters share one line; the search field gives way first */
.lead-filters input[name=q] { flex: 1 1 150px; min-width: 110px; }
.lead-filters .sep { width: 1px; align-self: stretch; background: var(--line); margin: 2px 2px; }
.note { font-size: 13px; color: var(--muted); margin-top: 6px; }
.nav { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 16px; align-items: center; }
.nav-links { display: contents; }
.nav a { padding: 7px 14px; border-radius: 8px; border: 1px solid var(--line);
         background: var(--card); text-decoration: none; color: var(--text); font-size: 14px; }
.nav a.on { background: var(--accent); border-color: var(--accent); color: #fff; }
textarea { width: 100%; min-height: 120px; font-family: ui-monospace, Menlo, Consolas, monospace;
           font-size: 13px; line-height: 1.45; resize: vertical; }
.field { margin-bottom: 18px; }
.field label { display: block; font-weight: 600; margin-bottom: 4px; font-size: 14px; }
.field .hint { color: var(--muted); font-size: 13px; margin-bottom: 6px; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 16px; }
.bar { height: 8px; background: var(--line); border-radius: 999px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--accent); }
.ok { color: var(--ok); } .warn { color: var(--warn); } .bad { color: var(--bad); }
/* iOS-style switch for "bot on/off for this number" */
.tgl { display: inline-flex; align-items: center; gap: 8px; margin: 0; }
.tgl button.sw { position: relative; width: 46px; height: 26px; border-radius: 999px; border: none; padding: 0;
                 cursor: pointer; background: #b9c0ca; transition: background .2s; flex: none; }
.tgl button.sw::after { content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%;
                        background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3); transition: left .2s; }
.tgl button.sw.on { background: var(--ok); }
.tgl button.sw.on::after { left: 23px; }
.tgl button.sw.paused { background: var(--warn); }
.tgl button.sw.paused::after { left: 23px; }
.tgl button.sw:hover { filter: brightness(1.08); }
.tgl .lbl { font-size: 13px; color: var(--muted); white-space: nowrap; }
.tgl .lbl.on { color: var(--ok); }
.tgl .lbl.paused { color: var(--warn); }
.msg .fix { color: var(--accent); text-decoration: none; }
.msg .fix:hover { text-decoration: underline; }
details > summary { list-style: none; }
details > summary::-webkit-details-marker { display: none; }
/* Chats: messenger layout, both panes scroll independently, fits the viewport */
.chats { display: grid; grid-template-columns: 320px minmax(0, 1fr); gap: 14px; height: calc(100vh - 150px); min-height: 480px; }
@supports (height: 100dvh) { .chats { height: calc(100dvh - 150px); } }
.chats-list { display: flex; flex-direction: column; min-height: 0; min-width: 0; padding: 10px; overflow: hidden; }
.chat-item { min-width: 0; overflow: hidden; }
.chats-list input { width: 100%; margin-bottom: 8px; }
.chats-items { overflow-y: auto; flex: 1; min-height: 0; scrollbar-width: thin; scrollbar-color: var(--scroll) transparent; }
.chats-items::-webkit-scrollbar { width: 8px; } .chats-items::-webkit-scrollbar-thumb { background: var(--scroll); border-radius: 999px; }
.chat-item { padding: 9px 10px; border-radius: 8px; cursor: pointer; border-bottom: 1px solid var(--line); }
.chat-item:hover { background: var(--bg); }
.chat-item.on { background: rgba(91,140,255,.14); }
.chat-last { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.chats-pane { display: flex; flex-direction: column; min-height: 0; padding: 0; overflow: hidden; }
.chats-head { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.chats-title { flex: 1; min-width: 0; }
.chats-back { display: none; }
.chats-thread { flex: 1; min-height: 0; overflow-y: auto; padding: 12px 14px; display: flex; flex-direction: column;
                scrollbar-width: thin; scrollbar-color: var(--scroll) transparent; }
.chats-thread::-webkit-scrollbar { width: 8px; } .chats-thread::-webkit-scrollbar-thumb { background: var(--scroll); border-radius: 999px; }
.chats-thread .msg { flex: none; }
.chats-send { display: flex; gap: 8px; padding: 10px 14px; border-top: 1px solid var(--line); }
.chats-send textarea { flex: 1; min-height: 44px; font-family: inherit; }
@media (max-width: 900px) {
    .chats { grid-template-columns: minmax(0, 1fr); height: calc(100vh - 150px); min-height: 420px; }
    @supports (height: 100dvh) { .chats { height: calc(100dvh - 150px); } }
    .chats-pane { display: none; }
    .chats.pane-open .chats-list { display: none; }
    .chats.pane-open .chats-pane { display: flex; }
    .chats-back { display: inline-flex; flex: none; }
    .chats-head { padding: 8px 10px; }
    .chats-head .btn { padding: 6px 10px; font-size: 13px; }
    .chats-thread { padding: 10px; }
    .chats-thread .msg { max-width: 88%; }
    .chats-send { padding: 8px 10px; }
    .chats-send textarea, #chats-search { font-size: 16px; } /* 16px stops iOS from zooming into the field */
    .chats-send button { padding: 8px 12px; }
}
/* Settings tabs */
.tabs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 14px; }
.tab-btn { padding: 7px 13px; font-size: 14px; }
.tab-btn.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.tab-panel[hidden], .side-panel[hidden] { display: none; }
.side-tabs { margin-bottom: 10px; }
.side-tabs .tab-btn { flex: 1; padding: 7px 8px; }
.side-body { display: flex; flex-direction: column; min-height: 0; }
.side-panel h3 { font-size: 14px; margin: 0 0 8px; }
.sep-line { border: 0; border-top: 1px solid var(--line); margin: 16px 0; }
.side-panel .sub { margin-bottom: 16px; }
.side-panel .sub:last-child { margin-bottom: 0; }
.side-panel .sub table { width: 100%; }
.side-panel .sub td, .side-panel .sub th { white-space: nowrap; padding: 6px 6px; font-size: 13px; }
.save-bar { position: sticky; bottom: 0; background: var(--bg); padding: 12px 0; display: flex; gap: 8px; align-items: center;
            flex-wrap: wrap; border-top: 1px solid var(--line); }

/* ---------- phones and small tablets ---------- */
@media (max-width: 900px) {
    .wrap { padding: 10px 12px; }
    h1 { font-size: 18px; }
    /* menu: one scrollable row, user block on its own row */
    .nav { margin-bottom: 8px; }
    .nav-links { display: flex; flex: 1 0 100%; gap: 6px; flex-wrap: nowrap; overflow-x: auto;
                 -webkit-overflow-scrolling: touch; padding-bottom: 4px; scrollbar-width: none; }
    .nav-links::-webkit-scrollbar { display: none; }
    .tabs { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; padding-bottom: 4px; }
    .tabs::-webkit-scrollbar { display: none; }
    .tab-btn { flex: none; }
    .nav a { flex: none; padding: 7px 12px; font-size: 13px; }
    /* theme + logout leave the scrolling strip and sit on their own compact row */
    .nav > .row { flex: 1 0 100%; margin-left: 0 !important; justify-content: flex-end; order: -1; }
    /* status line wraps instead of overflowing the card */
    .switch { gap: 6px; }
    /* stat tiles: compact 2-column grid */
    .dash .tiles, .tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
    .tile { padding: 10px 12px; }
    .tile .n { font-size: 20px; }
    .tile .l { font-size: 12px; }
    /* leads: cards instead of a table */
    .leads:not(.keep-table) { max-height: none; overflow: visible; border: none; border-radius: 0; }
    .leads:not(.keep-table) table { min-width: 0; display: block; }
    .leads:not(.keep-table) thead { display: none; }
    .leads:not(.keep-table) tbody { display: block; }
    .leads:not(.keep-table) tr { display: block; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-bottom: 10px;
                background: var(--card); }
    .leads:not(.keep-table) td { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 5px 0;
                border-bottom: none; white-space: normal; }
    .leads:not(.keep-table) td[data-label]::before { content: attr(data-label); color: var(--muted); font-size: 12px; flex: none; }
    .leads:not(.keep-table) td.contact { display: block; padding-bottom: 8px; margin-bottom: 4px; border-bottom: 1px solid var(--line); }
    .leads:not(.keep-table) td.contact a { font-size: 16px; font-weight: 600; }
    .leads:not(.keep-table) td select { max-width: 60%; }
    /* forms and inputs stretch */
    .row > input:not([type=hidden]), .row > select { flex: 1 1 140px; min-width: 0; }
    .head form, .head .row { width: 100%; }
    .head .row > * { flex: 1; }
    .head .srv { flex: 1 1 100%; }
    .msg { max-width: 92%; }
    .scroll table { min-width: 0; }
    .scroll th, .scroll td { padding: 7px 6px; font-size: 13px; }
    .dash h2 { margin-top: 14px; }
}

#help-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 900; }
#help-box { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); width: min(520px, calc(100vw - 32px));
            background: var(--card); color: var(--text); border: 1px solid var(--line); border-radius: 14px;
            padding: 16px 18px; z-index: 902; box-shadow: 0 12px 40px rgba(0,0,0,.35); }
#help-box h3 { margin: 4px 0 6px; font-size: 17px; }
#help-box p { margin: 0; line-height: 1.5; }
#help-box .help-step { font-size: 12px; }
.help-spot { position: relative; z-index: 901; outline: 3px solid var(--accent); outline-offset: 4px; border-radius: 10px;
             box-shadow: 0 0 0 9999px rgba(0,0,0,.55); background: var(--card); }
.help-btn { white-space: nowrap; }
@media (max-width: 900px) { #help-box { bottom: 12px; padding: 14px; } }
