/* ==================================================================
   PanelBot UI
   Light is the default palette; dark is applied either by the
   data-theme attribute or by the system preference when the user has
   not chosen. Every colour is a token so both modes stay in step.
   ================================================================== */

:root {
    --bg:            #f5f6f8;
    --surface:       #ffffff;
    --surface-2:     #fafbfc;
    --border:        #e2e5ea;
    --border-strong: #cdd2da;

    --text:          #16191f;
    --text-muted:    #626b7a;
    --text-subtle:   #8b94a3;

    --brand:         #1f7a4d;
    --brand-strong:  #15613c;
    --brand-soft:    #e6f4ec;

    --success:       #157a3f;
    --success-soft:  #e5f4ea;
    --warning:       #8a5a00;
    --warning-soft:  #fdf1dc;
    --danger:        #b3261e;
    --danger-soft:   #fdeceb;
    --info:          #1c5fb0;
    --info-soft:     #e7f0fb;

    --radius:        10px;
    --radius-sm:     6px;
    --radius-lg:     16px;

    --shadow-sm: 0 1px 2px rgba(16, 24, 40, .06);
    --shadow:    0 4px 14px rgba(16, 24, 40, .08);
    --shadow-lg: 0 16px 40px rgba(16, 24, 40, .14);

    --sidebar-w: 252px;
    --topbar-h:  60px;

    --speed: 160ms;
}

[data-theme="dark"] {
    --bg:            #0f1216;
    --surface:       #171b21;
    --surface-2:     #1d222a;
    --border:        #2a3038;
    --border-strong: #3a424d;

    --text:          #e8eaee;
    --text-muted:    #a2abb8;
    --text-subtle:   #78818f;

    --brand:         #35c07a;
    --brand-strong:  #2ba468;
    --brand-soft:    #14301f;

    --success:       #35c07a;
    --success-soft:  #123020;
    --warning:       #e0a33a;
    --warning-soft:  #302511;
    --danger:        #f0736a;
    --danger-soft:   #331817;
    --info:          #63a4ee;
    --info-soft:     #14243a;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, .4);
    --shadow:    0 4px 14px rgba(0, 0, 0, .45);
    --shadow-lg: 0 16px 40px rgba(0, 0, 0, .55);
}

@media (prefers-color-scheme: dark) {
    [data-theme="system"] {
        --bg:            #0f1216;
        --surface:       #171b21;
        --surface-2:     #1d222a;
        --border:        #2a3038;
        --border-strong: #3a424d;
        --text:          #e8eaee;
        --text-muted:    #a2abb8;
        --text-subtle:   #78818f;
        --brand:         #35c07a;
        --brand-strong:  #2ba468;
        --brand-soft:    #14301f;
        --success:       #35c07a;
        --success-soft:  #123020;
        --warning:       #e0a33a;
        --warning-soft:  #302511;
        --danger:        #f0736a;
        --danger-soft:   #331817;
        --info:          #63a4ee;
        --info-soft:     #14243a;
        --shadow-sm: 0 1px 2px rgba(0, 0, 0, .4);
        --shadow:    0 4px 14px rgba(0, 0, 0, .45);
        --shadow-lg: 0 16px 40px rgba(0, 0, 0, .55);
    }
}

*, *::before, *::after { box-sizing: border-box; }

html, body { height: 100%; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 { margin: 0 0 .5rem; line-height: 1.25; font-weight: 650; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1.02rem; }
p  { margin: 0 0 .75rem; }

a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Visible focus everywhere; never removed, only restyled. */
:focus-visible {
    outline: 2px solid var(--brand);
    outline-offset: 2px;
    border-radius: 4px;
}

.skip-link {
    position: absolute; left: -9999px; top: 0;
    background: var(--brand); color: #fff; padding: .6rem 1rem; z-index: 100;
}
.skip-link:focus { left: .5rem; top: .5rem; }

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: .01ms !important;
        transition-duration: .01ms !important;
    }
}

/* ---------------------------------------------------------------- Layout */

.layout { display: flex; min-height: 100vh; }

.layout__main {
    flex: 1;
    min-width: 0;              /* lets wide tables scroll instead of pushing */
    display: flex;
    flex-direction: column;
}

.content { padding: 1.5rem; flex: 1; }

@media (max-width: 900px) {
    .content { padding: 1rem; }
}

/* --------------------------------------------------------------- Sidebar */

.sidebar {
    width: var(--sidebar-w);
    flex-shrink: 0;
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
}

.sidebar__head {
    height: var(--topbar-h);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1rem;
    border-bottom: 1px solid var(--border);
}

.brand { display: inline-flex; align-items: center; gap: .55rem; color: var(--text); font-weight: 700; }
.brand:hover { text-decoration: none; }

.brand__mark {
    width: 30px; height: 30px;
    display: grid; place-items: center;
    border-radius: 8px;
    background: var(--brand); color: #fff;
    font-size: .78rem; letter-spacing: .04em;
}

.sidebar__close { display: none; background: none; border: 0; color: var(--text-muted); font-size: 1.5rem; cursor: pointer; }

.sidebar__nav { padding: .75rem .5rem; flex: 1; }

.sidebar__group {
    margin: .9rem .75rem .35rem;
    font-size: .68rem;
    text-transform: uppercase;
    letter-spacing: .07em;
    color: var(--text-subtle);
    font-weight: 700;
}

.sidebar__list { list-style: none; margin: 0; padding: 0; }

.sidebar__link {
    display: flex; align-items: center; gap: .6rem;
    padding: .5rem .75rem;
    margin-bottom: 1px;
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    font-size: .9rem;
    transition: background var(--speed), color var(--speed);
}

.sidebar__link:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }

.sidebar__link.is-active {
    background: var(--brand-soft);
    color: var(--brand-strong);
    font-weight: 600;
}

[data-theme="dark"] .sidebar__link.is-active { color: var(--brand); }

.sidebar__foot {
    border-top: 1px solid var(--border);
    padding: .75rem 1rem;
    display: flex; align-items: center; gap: .5rem;
    font-size: .8rem; color: var(--text-muted);
}

.sidebar__scrim { display: none; }

@media (max-width: 900px) {
    .sidebar {
        position: fixed; inset: 0 auto 0 0; z-index: 60;
        transform: translateX(-100%);
        transition: transform var(--speed) ease;
        box-shadow: var(--shadow-lg);
    }
    .sidebar.is-open { transform: none; }
    .sidebar__close { display: block; }
    .sidebar__scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 55; }
    .sidebar__scrim[hidden] { display: none; }
}

/* ---------------------------------------------------------------- Topbar */

.topbar {
    height: var(--topbar-h);
    display: flex; align-items: center; gap: .75rem;
    padding: 0 1.25rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky; top: 0; z-index: 40;
}

.topbar__title { font-size: 1.05rem; margin: 0; flex: 1; }
.topbar__actions { display: flex; align-items: center; gap: .5rem; }

.topbar__burger {
    display: none; flex-direction: column; gap: 4px;
    background: none; border: 0; cursor: pointer; padding: .35rem;
}
.topbar__burger span { width: 20px; height: 2px; background: var(--text); border-radius: 2px; }

@media (max-width: 900px) { .topbar__burger { display: flex; } }

/* ----------------------------------------------------------- Components */

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
    padding: 1.15rem;
    margin-bottom: 1rem;
}

.card__head {
    display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem;
    margin-bottom: .9rem;
}
.card__title { margin: 0; font-size: 1rem; }
.card__sub { margin: .2rem 0 0; color: var(--text-muted); font-size: .85rem; }

.grid { display: grid; gap: 1rem; }
.grid--2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid--3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid--4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }

@media (max-width: 1100px) { .grid--4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 780px)  { .grid--2, .grid--3, .grid--4 { grid-template-columns: minmax(0, 1fr); } }

.stat { display: flex; flex-direction: column; gap: .25rem; }
.stat__label { font-size: .78rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: .05em; font-weight: 600; }
.stat__value { font-size: 1.7rem; font-weight: 680; line-height: 1.1; }
.stat__hint { font-size: .8rem; color: var(--text-subtle); }

/* Status pill. The colour classes are produced by status_badge_class(). */
.badge {
    display: inline-flex; align-items: center; gap: .35rem;
    padding: .18rem .55rem;
    border-radius: 999px;
    font-size: .74rem; font-weight: 650;
    line-height: 1.5;
    white-space: nowrap;
}
.badge-success { background: var(--success-soft); color: var(--success); }
.badge-warning { background: var(--warning-soft); color: var(--warning); }
.badge-danger  { background: var(--danger-soft);  color: var(--danger); }
.badge-info    { background: var(--info-soft);    color: var(--info); }
.badge-muted   { background: var(--surface-2);    color: var(--text-muted); border: 1px solid var(--border); }

.status-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--text-subtle); flex-shrink: 0; }
.status-dot[data-state="online"], .status-dot[data-state="connected"] { background: var(--success); }
.status-dot[data-state="pairing"], .status-dot[data-state="connecting"] { background: var(--warning); }
.status-dot[data-state="offline"], .status-dot[data-state="disconnected"],
.status-dot[data-state="failed"], .status-dot[data-state="whatsapp_offline"],
.status-dot[data-state="panel_offline"], .status-dot[data-state="subscription_expired"] { background: var(--danger); }

/* Live pulse only while a connection is genuinely being established. */
.status-dot[data-state="pairing"], .status-dot[data-state="connecting"] {
    animation: pulse 1.6s ease-in-out infinite;
}
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .35; } }

/* ---------------------------------------------------------------- Forms */

.field { margin-bottom: .9rem; }

.label { display: block; font-size: .85rem; font-weight: 600; margin-bottom: .3rem; }
.help  { font-size: .8rem; color: var(--text-muted); margin: .3rem 0 0; }

.input, .select, .textarea {
    width: 100%;
    padding: .55rem .7rem;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    font: inherit;
    transition: border-color var(--speed), box-shadow var(--speed);
}

.input:focus, .select:focus, .textarea:focus {
    outline: none;
    border-color: var(--brand);
    box-shadow: 0 0 0 3px var(--brand-soft);
}

.input:disabled, .textarea:disabled { background: var(--surface-2); color: var(--text-muted); }

.textarea { min-height: 96px; resize: vertical; }

.checkbox { display: flex; align-items: flex-start; gap: .55rem; margin-bottom: .55rem; font-size: .9rem; }
.checkbox input { margin-top: .2rem; width: 16px; height: 16px; accent-color: var(--brand); }

.form-row { display: flex; gap: .75rem; flex-wrap: wrap; }
.form-row > * { flex: 1; min-width: 180px; }

/* --------------------------------------------------------------- Buttons */

.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
    padding: .52rem .95rem;
    border-radius: var(--radius-sm);
    border: 1px solid transparent;
    background: var(--brand); color: #fff;
    font: inherit; font-weight: 600; font-size: .9rem;
    cursor: pointer;
    transition: background var(--speed), border-color var(--speed), opacity var(--speed);
}

.btn:hover { background: var(--brand-strong); text-decoration: none; }
.btn:disabled, .btn.is-loading { opacity: .6; cursor: not-allowed; }

.btn--secondary { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
.btn--secondary:hover { background: var(--surface-2); }

.btn--danger { background: var(--danger); }
.btn--danger:hover { background: var(--danger); filter: brightness(.92); }

.btn--ghost { background: transparent; color: var(--text-muted); }
.btn--ghost:hover { background: var(--surface-2); color: var(--text); }

.btn--icon { padding: .45rem; width: 34px; height: 34px; }
.btn--sm { padding: .35rem .65rem; font-size: .82rem; }
.btn--block { width: 100%; }

/* Inline spinner shown while a fetch() call is in flight. */
.btn.is-loading::before {
    content: "";
    width: 13px; height: 13px;
    border: 2px solid currentColor;
    border-right-color: transparent;
    border-radius: 50%;
    animation: spin .6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* --------------------------------------------------------------- Tables */

/* Wide tables scroll inside their own container so the page body never
   scrolls sideways on a phone. */
.table-wrap { overflow-x: auto; margin: 0 -1.15rem; padding: 0 1.15rem; }

.table { width: 100%; border-collapse: collapse; font-size: .88rem; min-width: 520px; }
.table th, .table td { padding: .6rem .7rem; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table th { font-size: .74rem; text-transform: uppercase; letter-spacing: .05em; color: var(--text-muted); font-weight: 700; white-space: nowrap; }
.table tbody tr:hover { background: var(--surface-2); }
.table tbody tr:last-child td { border-bottom: 0; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.table .mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .82rem; }

.empty { text-align: center; padding: 2.5rem 1rem; color: var(--text-muted); }
.empty__title { font-weight: 650; color: var(--text); margin-bottom: .3rem; }

/* --------------------------------------------------------------- Alerts */

.alert {
    display: flex; gap: .6rem; align-items: flex-start;
    padding: .75rem .9rem;
    border-radius: var(--radius-sm);
    border: 1px solid transparent;
    font-size: .88rem;
    margin-bottom: 1rem;
}
.alert--success { background: var(--success-soft); color: var(--success); border-color: currentColor; }
.alert--warning { background: var(--warning-soft); color: var(--warning); border-color: currentColor; }
.alert--danger  { background: var(--danger-soft);  color: var(--danger);  border-color: currentColor; }
.alert--info    { background: var(--info-soft);    color: var(--info);    border-color: currentColor; }

/* --------------------------------------------------------------- Toasts */

.toast-host {
    position: fixed; bottom: 1rem; right: 1rem; z-index: 200;
    display: flex; flex-direction: column; gap: .5rem;
    max-width: min(360px, calc(100vw - 2rem));
}

.toast {
    background: var(--surface);
    border: 1px solid var(--border);
    border-left: 3px solid var(--text-subtle);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow);
    padding: .7rem .85rem;
    font-size: .88rem;
    display: flex; gap: .6rem; align-items: flex-start;
    animation: toast-in var(--speed) ease;
}
.toast--success { border-left-color: var(--success); }
.toast--error   { border-left-color: var(--danger); }
.toast--warning { border-left-color: var(--warning); }
.toast--info    { border-left-color: var(--info); }
.toast.is-leaving { opacity: 0; transform: translateX(8px); transition: all var(--speed); }
.toast__close { margin-left: auto; background: none; border: 0; color: var(--text-subtle); cursor: pointer; font-size: 1rem; line-height: 1; }

@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* ----------------------------------------------------------------- Menu */

.menu { position: relative; }
.menu__trigger { display: flex; align-items: center; gap: .5rem; background: none; border: 0; cursor: pointer; color: var(--text); font: inherit; padding: .25rem .4rem; border-radius: var(--radius-sm); }
.menu__trigger:hover { background: var(--surface-2); }
.menu__name { font-size: .88rem; font-weight: 600; }
@media (max-width: 640px) { .menu__name { display: none; } }

.avatar { width: 28px; height: 28px; border-radius: 50%; background: var(--brand); color: #fff; display: grid; place-items: center; font-size: .78rem; font-weight: 700; }

.menu__panel {
    position: absolute; right: 0; top: calc(100% + .4rem);
    min-width: 190px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow);
    padding: .3rem;
    z-index: 50;
}
.menu__meta { padding: .4rem .6rem; font-size: .78rem; color: var(--text-muted); margin: 0; border-bottom: 1px solid var(--border); word-break: break-all; }
.menu__item { display: block; width: 100%; text-align: left; padding: .45rem .6rem; border: 0; background: none; color: var(--text); font: inherit; font-size: .88rem; border-radius: 4px; cursor: pointer; }
.menu__item:hover { background: var(--surface-2); text-decoration: none; }
.menu__item--danger { color: var(--danger); }

/* -------------------------------------------------------------- Utility */

.stack > * + * { margin-top: .75rem; }
.row { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; }
.row--between { justify-content: space-between; }
.muted { color: var(--text-muted); }
.subtle { color: var(--text-subtle); font-size: .82rem; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.nowrap { white-space: nowrap; }
.text-right { text-align: right; }
.mb-0 { margin-bottom: 0; }
.mt-1 { margin-top: 1rem; }
.hidden { display: none !important; }

.progress { height: 7px; background: var(--surface-2); border-radius: 999px; overflow: hidden; border: 1px solid var(--border); }
.progress__bar { height: 100%; background: var(--brand); transition: width var(--speed); }
.progress__bar.is-warning { background: var(--warning); }
.progress__bar.is-danger  { background: var(--danger); }

/* Simple icon shapes; no external font or sprite is loaded. */
.ico { width: 16px; height: 16px; flex-shrink: 0; border-radius: 3px; background: currentColor; opacity: .55; }
.sidebar__link.is-active .ico { opacity: 1; }

/* ------------------------------------------------------- Auth + public */

.auth { display: grid; place-items: center; min-height: 100vh; padding: 1.5rem; background: var(--bg); }
.auth__wrap { width: 100%; max-width: 420px; }
.auth__brand { text-align: center; margin-bottom: 1.25rem; }
.auth__tagline { color: var(--text-muted); font-size: .88rem; margin: .5rem 0 0; }
.auth__card { padding: 1.5rem; }
.auth__footer { text-align: center; margin-top: 1rem; font-size: .88rem; color: var(--text-muted); }

/* --------------------------------------------------- WhatsApp / setup */

.qr-box {
    display: grid; place-items: center;
    padding: 1.25rem;
    background: #fff;                 /* QR codes need a light ground */
    border: 1px solid var(--border);
    border-radius: var(--radius);
    min-height: 240px;
}
.qr-box img, .qr-box svg { max-width: 240px; width: 100%; height: auto; display: block; }
.qr-box__hint { color: #444; font-size: .82rem; text-align: center; margin-top: .6rem; }

.setup-steps { list-style: none; margin: 0; padding: 0; display: grid; gap: .6rem; }
.setup-step {
    display: flex; align-items: center; gap: .7rem;
    padding: .7rem .85rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface-2);
}
.setup-step.is-done { border-color: var(--success); background: var(--success-soft); }
.setup-step__mark { width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; font-size: .72rem; font-weight: 700; background: var(--border); color: var(--text-muted); flex-shrink: 0; }
.setup-step.is-done .setup-step__mark { background: var(--success); color: #fff; }
.setup-step__label { flex: 1; font-size: .9rem; font-weight: 550; }

/* ------------------------------------------------------- Conversations */

.chat { display: flex; flex-direction: column; gap: .6rem; max-height: 62vh; overflow-y: auto; padding: .25rem; }

.bubble {
    max-width: min(76%, 520px);
    padding: .55rem .8rem;
    border-radius: 12px;
    font-size: .89rem;
    white-space: pre-wrap;
    word-break: break-word;
}
.bubble--in  { align-self: flex-start; background: var(--surface-2); border: 1px solid var(--border); border-bottom-left-radius: 3px; }
.bubble--out { align-self: flex-end; background: var(--brand-soft); border: 1px solid var(--brand); border-bottom-right-radius: 3px; }
[data-theme="dark"] .bubble--out { background: var(--brand-soft); border-color: var(--brand-strong); }

.bubble__meta { font-size: .7rem; color: var(--text-subtle); margin-top: .3rem; display: flex; gap: .4rem; align-items: center; }

/* Marks which PerfectPanel endpoint supplied the facts in a reply. */
.bubble__source { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .66rem; padding: .05rem .3rem; border-radius: 3px; background: var(--info-soft); color: var(--info); }

/* -------------------------------------------------------------- Charts */

.spark { display: flex; align-items: flex-end; gap: 2px; height: 60px; }
.spark__bar { flex: 1; min-width: 3px; background: var(--brand); border-radius: 2px 2px 0 0; opacity: .8; transition: opacity var(--speed); }
.spark__bar:hover { opacity: 1; }

/* --------------------------------------------------------------- Plans */

.plan { display: flex; flex-direction: column; height: 100%; }
.plan.is-current { border-color: var(--brand); box-shadow: 0 0 0 1px var(--brand); }
.plan__price { font-size: 2rem; font-weight: 700; line-height: 1; margin: .5rem 0; }
.plan__period { font-size: .85rem; color: var(--text-muted); font-weight: 400; }
.plan__features { list-style: none; margin: .9rem 0; padding: 0; font-size: .88rem; display: grid; gap: .4rem; flex: 1; }
.plan__features li { display: flex; gap: .45rem; align-items: flex-start; color: var(--text-muted); }
.plan__features li::before { content: "+"; color: var(--success); font-weight: 700; }

/* ---------------------------------------------------------- Pagination */

.pager { display: flex; gap: .35rem; align-items: center; justify-content: center; margin-top: 1rem; flex-wrap: wrap; }
.pager__link { padding: .35rem .7rem; border: 1px solid var(--border); border-radius: var(--radius-sm); font-size: .85rem; color: var(--text-muted); background: var(--surface); }
.pager__link:hover { background: var(--surface-2); text-decoration: none; }
.pager__link.is-active { background: var(--brand); color: #fff; border-color: var(--brand); }
.pager__link.is-disabled { opacity: .45; pointer-events: none; }

/* -------------------------------------------------------------- Public */

.hero { padding: 4rem 1.5rem 2.5rem; text-align: center; max-width: 780px; margin: 0 auto; }
.hero h1 { font-size: clamp(1.8rem, 4vw, 2.6rem); }
.hero p { color: var(--text-muted); font-size: 1.05rem; }
.public-nav { display: flex; align-items: center; justify-content: space-between; padding: 1rem 1.5rem; border-bottom: 1px solid var(--border); background: var(--surface); }
.public-wrap { max-width: 1080px; margin: 0 auto; padding: 0 1.5rem 4rem; }
