:root {
    --bg: #f4f7fb;
    --surface: #ffffff;
    --surface-soft: #eef3f8;
    --text: #17212b;
    --muted: #667789;
    --line: #d9e2ec;
    --primary: #0f9f6e;
    --primary-strong: #067a54;
    --danger: #d94343;
    --warning: #b7791f;
    --shadow: 0 12px 30px rgba(23, 33, 43, 0.08);
    --radius: 8px;
}

[data-theme="dark"] {
    --bg: #101418;
    --surface: #171d23;
    --surface-soft: #202832;
    --text: #eef4f8;
    --muted: #9aacb8;
    --line: #2d3944;
    --primary: #28c98b;
    --primary-strong: #19ad75;
    --danger: #ff6b6b;
    --warning: #f0b44c;
    --shadow: 0 12px 30px rgba(0, 0, 0, 0.25);
}

* { box-sizing: border-box; }

body {
    margin: 0;
    min-height: 100vh;
    background: var(--bg);
    color: var(--text);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    letter-spacing: 0;
}

.auth-body {
    display: grid;
    place-items: center;
    padding: 24px;
}

.auth-shell {
    width: min(430px, 100%);
}

.auth-card {
    display: grid;
    gap: 18px;
    padding: 28px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: var(--shadow);
}

.auth-brand {
    padding-bottom: 14px;
}

.auth-error {
    padding: 10px 12px;
    border: 1px solid rgba(217, 67, 67, 0.45);
    border-radius: var(--radius);
    color: var(--danger);
    background: rgba(217, 67, 67, 0.08);
}

.auth-help {
    display: grid;
    gap: 6px;
    padding-top: 14px;
    border-top: 1px solid var(--line);
    color: var(--muted);
    font-size: 0.9rem;
}

button, input, select, textarea {
    font: inherit;
}

button {
    cursor: pointer;
}

.app-shell {
    display: grid;
    grid-template-columns: 260px minmax(0, 1fr);
    min-height: 100vh;
}

.sidebar {
    display: flex;
    flex-direction: column;
    gap: 20px;
    background: var(--surface);
    border-right: 1px solid var(--line);
    padding: 18px;
    position: sticky;
    top: 0;
    height: 100vh;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 4px 18px;
    border-bottom: 1px solid var(--line);
}

.brand-mark {
    display: grid;
    place-items: center;
    width: 42px;
    aspect-ratio: 1;
    border-radius: 8px;
    background: var(--primary);
    color: #fff;
    font-weight: 800;
}

.brand strong, .brand small {
    display: block;
}

.brand small {
    color: var(--muted);
    margin-top: 2px;
}

.nav {
    display: grid;
    gap: 6px;
}

.nav-item {
    width: 100%;
    text-align: left;
    border: 0;
    border-radius: var(--radius);
    padding: 12px 14px;
    color: var(--muted);
    background: transparent;
}

.nav-item:hover, .nav-item.active {
    color: var(--text);
    background: var(--surface-soft);
}

.sidebar-footer {
    margin-top: auto;
    color: var(--muted);
    display: grid;
    gap: 10px;
}

.user-pill {
    display: grid;
    gap: 2px;
    padding: 10px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface-soft);
    color: var(--text);
}

.user-pill span {
    color: var(--muted);
    font-size: 0.85rem;
    text-transform: capitalize;
}

.logout-link {
    text-align: center;
    text-decoration: none;
}

.theme-toggle {
    display: flex;
    align-items: center;
    gap: 10px;
}

.workspace {
    min-width: 0;
    padding: 22px;
}

.topbar {
    display: flex;
    justify-content: space-between;
    gap: 16px;
    align-items: center;
    margin-bottom: 18px;
}

.eyebrow {
    margin: 0 0 4px;
    color: var(--muted);
    font-size: 13px;
    text-transform: uppercase;
}

h1, h2, h3, p {
    margin-top: 0;
}

h1 {
    margin-bottom: 0;
    font-size: 30px;
}

h2 {
    font-size: 16px;
    margin-bottom: 0;
}

.topbar-actions, .inline-controls, .builder-toolbar, .panel-header {
    display: flex;
    align-items: center;
    gap: 10px;
}

.btn, .icon-btn {
    border: 1px solid var(--line);
    border-radius: var(--radius);
    color: var(--text);
    background: var(--surface);
    min-height: 38px;
    padding: 8px 12px;
}

.btn.primary {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
}

.btn.primary:hover {
    background: var(--primary-strong);
}

.btn.ghost {
    background: var(--surface-soft);
}

.btn.danger {
    background: transparent;
    color: var(--danger);
    border-color: color-mix(in srgb, var(--danger) 50%, var(--line));
}

.btn.small {
    min-height: 32px;
    padding: 6px 10px;
    font-size: 13px;
}

.icon-btn {
    min-width: 38px;
    padding-inline: 10px;
}

.view {
    display: none;
}

.view.active {
    display: block;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(5, minmax(130px, 1fr));
    gap: 14px;
    margin-bottom: 14px;
}

.stat-card, .panel {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.stat-card {
    padding: 16px;
}

.stat-card span {
    display: block;
    color: var(--muted);
    font-size: 13px;
}

.stat-card strong {
    display: block;
    margin-top: 8px;
    font-size: 28px;
}

.content-grid {
    display: grid;
    gap: 14px;
}

.content-grid.two {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    margin-bottom: 14px;
}

.panel {
    padding: 16px;
}

.panel-header {
    justify-content: space-between;
    margin-bottom: 14px;
}

.activity-list, .conversation-list {
    display: grid;
    gap: 10px;
}

.wa-chat-shell {
    display: grid;
    grid-template-columns: 340px minmax(0, 1fr);
    min-height: calc(100vh - 140px);
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow);
}

.wa-chat-list-panel {
    border-right: 1px solid var(--line);
    padding: 14px;
    min-width: 0;
}

.wa-search {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface-soft);
    color: var(--text);
    min-height: 38px;
    padding: 8px 10px;
    margin-bottom: 12px;
}

.wa-list {
    max-height: calc(100vh - 250px);
    overflow: auto;
}

.wa-list .conversation-item {
    cursor: pointer;
}

.wa-list .conversation-item.active {
    outline: 2px solid var(--primary);
    background: color-mix(in srgb, var(--primary) 12%, var(--surface-soft));
}

.wa-chat-panel {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto auto;
    min-width: 0;
}

.wa-chat-header {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    align-items: center;
    padding: 14px 16px;
    border-bottom: 1px solid var(--line);
    background: var(--surface);
}

.wa-chat-header strong,
.wa-chat-header span {
    display: block;
}

.wa-chat-header span {
    color: var(--muted);
    font-size: 13px;
    margin-top: 2px;
}

.assign-control {
    display: grid;
    gap: 4px;
    min-width: 190px;
}

.conversation-header-actions {
    display: flex;
    gap: 10px;
    align-items: center;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.assign-control select {
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--surface);
    color: var(--text);
    padding: 8px 10px;
}

.wa-messages {
    display: grid;
    align-content: start;
    gap: 8px;
    overflow: auto;
    padding: 18px;
    background: #e8f0ea;
}

[data-theme="dark"] .wa-messages {
    background: #15211c;
}

.message-bubble {
    max-width: min(620px, 78%);
    padding: 10px 12px;
    border-radius: 8px;
    background: #fff;
    color: #1f2a33;
    white-space: pre-wrap;
}

.message-bubble.out,
.message-bubble.system {
    justify-self: end;
    background: #d8f7df;
}

.message-bubble small {
    display: block;
    margin-top: 6px;
    opacity: 0.65;
    font-size: 11px;
}

.prospect-strip,
.wa-reply-box {
    display: grid;
    gap: 8px;
    padding: 10px;
    border-top: 1px solid var(--line);
    background: var(--surface);
}

.prospect-strip {
    grid-template-columns: minmax(120px, 1fr) minmax(120px, 1fr) auto;
}

.wa-reply-box {
    grid-template-columns: minmax(0, 1fr) auto;
}

.prospect-strip input,
.wa-reply-box input {
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface-soft);
    color: var(--text);
    min-height: 38px;
    padding: 8px 10px;
}

.activity-item, .conversation-item, .flow-row {
    display: flex;
    justify-content: space-between;
    gap: 14px;
    padding: 12px;
    background: var(--surface-soft);
    border-radius: var(--radius);
    border: 1px solid var(--line);
}

.activity-item p, .conversation-item p {
    margin: 4px 0 0;
    color: var(--muted);
    font-size: 13px;
}

.badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 24px;
    padding: 3px 8px;
    border-radius: 999px;
    background: var(--surface);
    color: var(--muted);
    border: 1px solid var(--line);
    font-size: 12px;
    white-space: nowrap;
}

.badge.success { color: var(--primary-strong); }
.badge.danger { color: var(--danger); }
.badge.warning { color: var(--warning); }

.quick-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
}

.action-tile {
    min-height: 88px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface-soft);
    color: var(--text);
    font-weight: 700;
}

.builder-layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 340px;
    gap: 14px;
}

.builder-main {
    min-width: 0;
}

.builder-toolbar {
    flex-wrap: wrap;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 10px;
    margin-bottom: 14px;
    box-shadow: var(--shadow);
}

.builder-toolbar input, .builder-toolbar select, .inline-controls input, .inline-controls select, .settings-grid input, .settings-grid select, .form-stack input, .form-stack select, .form-stack textarea, table select {
    border: 1px solid var(--line);
    background: var(--surface);
    color: var(--text);
    border-radius: var(--radius);
    min-height: 38px;
    padding: 8px 10px;
}

.builder-toolbar #flowName {
    min-width: 190px;
}

.builder-toolbar #flowKeywords {
    min-width: 230px;
    flex: 1 1 230px;
}

.builder-body {
    display: grid;
    grid-template-columns: 170px minmax(0, 1fr);
    min-height: 580px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    overflow: hidden;
    background: var(--surface);
    box-shadow: var(--shadow);
}

.node-palette {
    padding: 12px;
    border-right: 1px solid var(--line);
    background: var(--surface);
    overflow: auto;
}

.node-palette h2 {
    margin-bottom: 10px;
}

.node-palette button {
    width: 100%;
    min-height: 34px;
    margin-bottom: 8px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface-soft);
    color: var(--text);
    text-align: left;
    padding: 7px 9px;
}

.canvas-wrap {
    position: relative;
    overflow: auto;
    background-color: var(--surface-soft);
    background-image:
        linear-gradient(var(--line) 1px, transparent 1px),
        linear-gradient(90deg, var(--line) 1px, transparent 1px);
    background-size: 24px 24px;
}

.canvas {
    position: relative;
    width: 1800px;
    height: 1200px;
    transform-origin: 0 0;
}

.edges-layer {
    position: absolute;
    inset: 0;
    width: 1800px;
    height: 1200px;
    pointer-events: none;
    z-index: 1;
}

.flow-node {
    position: absolute;
    z-index: 2;
    width: 220px;
    min-height: 92px;
    border: 1px solid var(--line);
    border-top: 4px solid var(--primary);
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: var(--shadow);
    padding: 12px;
    user-select: none;
}

.flow-node.selected {
    outline: 3px solid color-mix(in srgb, var(--primary) 35%, transparent);
}

.flow-node strong {
    display: block;
    font-size: 14px;
    margin-bottom: 8px;
}

.flow-node p {
    margin: 0;
    color: var(--muted);
    font-size: 12px;
    line-height: 1.4;
}

.flow-node .node-type {
    display: inline-block;
    margin-bottom: 8px;
    color: var(--muted);
    font-size: 11px;
    text-transform: uppercase;
}

.connector-row {
    display: flex;
    gap: 8px;
    margin-top: 10px;
}

.connector-row button {
    flex: 1;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface-soft);
    color: var(--muted);
    min-height: 28px;
    font-size: 12px;
}

.connector-row button.active-connect {
    color: #fff;
    background: var(--primary);
}

.inspector {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 16px;
    box-shadow: var(--shadow);
    align-self: start;
    position: sticky;
    top: 18px;
}

.form-stack {
    display: grid;
    gap: 12px;
}

.form-stack label, .settings-grid label {
    display: grid;
    gap: 6px;
    color: var(--muted);
    font-size: 13px;
}

.form-stack textarea {
    resize: vertical;
    min-height: 88px;
}

.form-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 12px;
}

.form-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.hidden {
    display: none !important;
}

.compact-form {
    margin-bottom: 12px;
    padding: 10px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface-soft);
}

.broadcast-summary {
    display: grid;
    gap: 4px;
    margin-bottom: 12px;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface-soft);
}

.broadcast-summary strong {
    font-size: 28px;
}

.broadcast-summary span {
    color: var(--muted);
}

.crm-summary .stat-card strong {
    text-transform: capitalize;
}

.order-list {
    display: grid;
    gap: 10px;
    max-height: 680px;
    overflow: auto;
}

.order-card {
    display: grid;
    gap: 8px;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface-soft);
}

.order-card header {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    align-items: center;
}

.order-card p {
    margin: 0;
    color: var(--muted);
    font-size: 13px;
}

.chip-list {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 14px;
}

.chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 6px 10px;
    background: var(--surface-soft);
    color: var(--text);
    font-size: 13px;
}

.qr-connect-grid {
    display: block;
}

.panel-actions {
    display: inline-flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
}

.qr-slot-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 14px;
}

.qr-empty {
    padding: 18px;
    border: 1px dashed var(--line);
    border-radius: var(--radius);
    background: var(--surface-soft);
    color: var(--muted);
}

.qr-card-header {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    align-items: flex-start;
}

.qr-card-body {
    display: grid;
    gap: 10px;
}

.qr-card {
    display: grid;
    gap: 12px;
    align-content: start;
    min-height: 260px;
    padding: 18px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface-soft);
}

.qr-card h3 {
    margin: 0;
}

.qr-card p {
    margin: 0;
    color: var(--muted);
}

.qr-preview {
    place-items: center;
    text-align: center;
}

.qr-box {
    display: grid;
    place-items: center;
    width: min(260px, 100%);
    aspect-ratio: 1;
    padding: 14px;
    border-radius: var(--radius);
    border: 1px dashed var(--line);
    background: var(--surface);
}

.qr-box img {
    max-width: 100%;
    max-height: 100%;
}

.qr-box.status-ready {
    border-style: solid;
    border-color: rgba(39, 179, 118, 0.45);
    color: var(--primary-strong);
    font-size: 1.5rem;
    font-weight: 800;
    background: rgba(39, 179, 118, 0.12);
}

.meta-validation {
    min-height: 0;
}

.validation-box {
    display: grid;
    gap: 6px;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
}

.validation-box span {
    color: var(--muted);
    font-size: 0.88rem;
}

.validation-box.success {
    border-color: rgba(39, 179, 118, 0.45);
}

.validation-box.danger {
    border-color: rgba(229, 83, 83, 0.45);
}

.advanced-panel summary {
    cursor: pointer;
    font-weight: 700;
}

.muted-empty {
    color: var(--muted);
}

.chat-preview {
    margin-top: 14px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 14px;
    box-shadow: var(--shadow);
}

.phone-frame {
    display: grid;
    gap: 8px;
    align-content: start;
    min-height: 190px;
    max-height: 330px;
    overflow: auto;
    padding: 14px;
    border-radius: var(--radius);
    background: #e8f0ea;
}

[data-theme="dark"] .phone-frame {
    background: #15211c;
}

.bubble {
    max-width: 78%;
    padding: 10px 12px;
    border-radius: 8px;
    background: #fff;
    color: #1f2a33;
    font-size: 14px;
}

.bubble.bot {
    justify-self: end;
    background: #d8f7df;
}

.table-wrap {
    overflow: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
}

th, td {
    text-align: left;
    padding: 12px 10px;
    border-bottom: 1px solid var(--line);
    vertical-align: top;
}

th {
    color: var(--muted);
    font-size: 13px;
}

.chart {
    display: grid;
    gap: 10px;
}

.bar-row {
    display: grid;
    grid-template-columns: 120px minmax(0, 1fr) 52px;
    gap: 10px;
    align-items: center;
    color: var(--muted);
    font-size: 13px;
}

.bar {
    height: 12px;
    border-radius: 999px;
    background: var(--surface-soft);
    overflow: hidden;
}

.bar span {
    display: block;
    height: 100%;
    background: var(--primary);
}

.rate-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
}

.rate-grid div {
    padding: 18px;
    border-radius: var(--radius);
    background: var(--surface-soft);
    border: 1px solid var(--line);
}

.rate-grid strong, .rate-grid span {
    display: block;
}

.rate-grid strong {
    font-size: 30px;
}

.rate-grid span {
    color: var(--muted);
    margin-top: 6px;
}

.settings-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 14px;
    margin-bottom: 16px;
}

.toast {
    position: fixed;
    right: 18px;
    bottom: 18px;
    min-width: 220px;
    max-width: min(360px, calc(100vw - 36px));
    background: var(--text);
    color: var(--surface);
    padding: 12px 14px;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    transform: translateY(140%);
    transition: transform 180ms ease;
    z-index: 20;
}

.toast.show {
    transform: translateY(0);
}

@media (max-width: 1180px) {
    .stats-grid {
        grid-template-columns: repeat(3, minmax(130px, 1fr));
    }

    .builder-layout {
        grid-template-columns: 1fr;
    }

    .inspector {
        position: static;
    }
}

@media (max-width: 820px) {
    .app-shell {
        grid-template-columns: 1fr;
    }

    .sidebar {
        position: static;
        height: auto;
    }

    .nav {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .topbar, .content-grid.two, .settings-grid {
        grid-template-columns: 1fr;
        display: grid;
    }

    .topbar-actions, .inline-controls {
        flex-wrap: wrap;
    }

    .stats-grid {
        grid-template-columns: repeat(2, minmax(130px, 1fr));
    }

    .builder-body {
        grid-template-columns: 1fr;
    }

    .wa-chat-shell {
        grid-template-columns: 1fr;
    }

    .wa-chat-list-panel {
        border-right: 0;
        border-bottom: 1px solid var(--line);
    }

    .wa-list {
        max-height: 280px;
    }

    .prospect-strip,
    .wa-reply-box {
        grid-template-columns: 1fr;
    }

    .node-palette {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 8px;
        border-right: 0;
        border-bottom: 1px solid var(--line);
    }

    .node-palette h2 {
        grid-column: 1 / -1;
    }

    .node-palette button {
        margin-bottom: 0;
    }

    .form-row {
        grid-template-columns: 1fr;
    }

    .qr-connect-grid {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 540px) {
    .workspace {
        padding: 14px;
    }

    .stats-grid, .quick-actions, .rate-grid {
        grid-template-columns: 1fr;
    }

    h1 {
        font-size: 24px;
    }
}
