* {
    box-sizing: border-box;
}

:root {
    --bg: #0a0d12;
    --panel: #0f141b;
    --panel2: #121922;
    --line: #202a35;
    --muted: #7f8b98;
    --text: #edf2f7;
    --green: #24d366;
}

html,
body {
    height: 100%;
    min-height: 100%;
    overflow: hidden;
}

body {
    margin: 0;
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
        "Segoe UI", Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
}

/* =========================================================
   ESTRUTURA PRINCIPAL
   ========================================================= */

.app {
    display: grid;
    grid-template-columns: 220px 360px minmax(0, 1fr);
    width: 100%;
    height: 100vh;
    height: 100dvh;
    min-height: 0;
    overflow: hidden;
}

/* =========================================================
   SIDEBAR
   ========================================================= */

.sidebar {
    background: #0c1117;
    border-right: 1px solid var(--line);
    padding: 20px 14px;
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    overflow: hidden;
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 800;
    font-size: 17px;
    padding: 0 7px 22px;
}

.brand > span {
    width: 34px;
    height: 34px;
    display: grid;
    place-items: center;
    border-radius: 10px;
    background: var(--green);
    color: #06130b;
}

.brand small {
    display: block;
    font-size: 9px;
    color: #60707f;
    letter-spacing: 1.6px;
    margin-top: 2px;
}

.nav-group {
    display: flex;
    flex-direction: column;
    gap: 3px;
    overflow-y: auto;
    min-height: 0;
}

.nav {
    border: 0;
    background: transparent;
    color: #98a5b3;
    text-align: left;
    padding: 10px 11px;
    border-radius: 8px;
    cursor: pointer;
    font: inherit;
    transition: 0.15s ease;
}

.nav:hover,
.nav.active {
    background: #17202a;
    color: #fff;
}

.sidebar-bottom {
    margin-top: auto;
    border-top: 1px solid var(--line);
    padding-top: 16px;
    flex-shrink: 0;
}

.sidebar-bottom label {
    font-size: 10px;
    color: #657383;
    letter-spacing: 1.2px;
}

.sidebar select,
.settings-btn {
    width: 100%;
    margin-top: 8px;
    background: #111923;
    color: #dce4eb;
    border: 1px solid #25313d;
    padding: 9px;
    border-radius: 8px;
}

.settings-btn {
    text-align: left;
    cursor: pointer;
}

/* =========================================================
   PAINEL DE CONTATOS
   ========================================================= */

.contacts-panel {
    background: #0e131a;
    border-right: 1px solid var(--line);
    overflow: hidden;
    min-width: 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.panel-title {
    min-height: 78px;
    padding: 17px 18px 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
}

.panel-title h1 {
    font-size: 20px;
    margin: 0;
}

.panel-title span {
    color: var(--muted);
    font-size: 12px;
}

.icon-btn {
    width: 34px;
    height: 34px;
    border: 1px solid #283541;
    border-radius: 8px;
    background: #141c25;
    color: #cbd5df;
    font-size: 18px;
    cursor: pointer;
}

.spin {
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

.toolbar {
    padding: 0 14px 13px;
    border-bottom: 1px solid var(--line);
    flex-shrink: 0;
}

input,
textarea,
select {
    font: inherit;
}

.toolbar input,
.details input,
.details textarea,
.chat-header select,
.composer textarea,
.form-grid input,
.copy-row input {
    width: 100%;
    background: #0c1218;
    border: 1px solid #283542;
    color: var(--text);
    border-radius: 8px;
    padding: 10px 11px;
    outline: none;
}

.toolbar input:focus,
.form-grid input:focus,
.composer textarea:focus,
.details input:focus,
.details textarea:focus {
    border-color: #4b6479;
}

.contacts {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    scrollbar-gutter: stable;
}

.contact {
    display: flex;
    gap: 11px;
    padding: 13px 14px;
    border-bottom: 1px solid #18212b;
    cursor: pointer;
    min-width: 0;
}

.contact:hover,
.contact.active {
    background: #141d26;
}

.avatar {
    width: 38px;
    height: 38px;
    flex: 0 0 38px;
    border-radius: 50%;
    display: grid;
    place-items: center;
    background: #263441;
    font-weight: 800;
}

.contact-body {
    min-width: 0;
    flex: 1;
}

.contact-top,
.preview-row {
    display: flex;
    gap: 8px;
    align-items: center;
    justify-content: space-between;
    min-width: 0;
}

.contact-name {
    font-weight: 700;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.time {
    color: #687684;
    font-size: 10px;
    flex: 0 0 auto;
}

.preview {
    color: #8794a0;
    font-size: 12px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.unread {
    background: var(--green);
    color: #05200e;
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    border-radius: 9px;
    display: grid;
    place-items: center;
    font-size: 10px;
    font-weight: 800;
}

.instance-tag {
    margin-top: 5px;
    font-size: 10px;
    color: #657482;
    display: flex;
    align-items: center;
    gap: 5px;
}

.instance-tag i {
    width: 6px;
    height: 6px;
    border-radius: 50%;
}

.list-empty {
    padding: 45px 20px;
    text-align: center;
    color: #64717e;
}

/* =========================================================
   PAINEL DO CHAT
   ========================================================= */

.chat-panel {
    position: relative;
    background: #090d12;
    min-width: 0;
    min-height: 0;
    overflow: hidden;
}

.empty-state {
    height: 100%;
    display: grid;
    place-content: center;
    text-align: center;
    color: #697683;
}

.empty-state h2 {
    color: #b6c0c9;
    margin: 12px 0 4px;
}

.empty-state p {
    margin: 0;
}

.empty-icon {
    font-size: 58px;
    color: #25313c;
}

.hidden {
    display: none !important;
}

.chat {
    height: 100%;
    min-height: 0;
    overflow: hidden;

    display: grid;
    grid-template-rows:
        auto
        minmax(0, 1fr)
        auto
        auto;
}

.chat-header {
    min-height: 70px;
    padding: 12px 20px;
    border-bottom: 1px solid var(--line);
    background: #0e141a;

    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;

    flex-shrink: 0;
    min-width: 0;
}

.chat-header > div {
    min-width: 0;
}

.chat-header h2 {
    margin: 0 0 3px;
    font-size: 17px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.muted {
    font-size: 11px;
    color: #788693;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-header select {
    width: auto;
    min-width: 150px;
    flex-shrink: 0;
}

/* =========================================================
   MENSAGENS
   ========================================================= */

.messages {
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;

    padding: 22px;

    display: flex;
    flex-direction: column;
    gap: 8px;

    background:
        radial-gradient(
            circle at 50% -10%,
            #141e29,
            #090d12 55%
        );

    scrollbar-gutter: stable;
    overscroll-behavior: contain;
}

.msg {
    max-width: min(720px, 72%);
    padding: 10px 12px;
    border-radius: 11px;
    line-height: 1.45;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    word-break: break-word;
}

.msg.in {
    align-self: flex-start;
    background: #19232d;
}

.msg.out {
    align-self: flex-end;
    background: #123c28;
}

.msg small {
    display: block;
    text-align: right;
    color: #7f9189;
    font-size: 9px;
    margin-top: 4px;
}

/* =========================================================
   DETALHES DO CONTATO
   ========================================================= */

.details {
    display: grid;
    grid-template-columns:
        minmax(160px, 220px)
        minmax(220px, 1fr)
        auto;

    gap: 8px;
    padding: 9px 12px;

    border-top: 1px solid var(--line);
    background: #0d1319;

    flex-shrink: 0;
    min-width: 0;
}

.details textarea {
    height: 39px;
    min-height: 39px;
    max-height: 90px;
    resize: vertical;
    overflow-y: auto;
}

.secondary,
.primary {
    border: 1px solid #2a3946;
    border-radius: 8px;
    padding: 9px 14px;
    color: #dce5ec;
    background: #19232d;
    cursor: pointer;
}

.primary {
    background: var(--green);
    border-color: var(--green);
    color: #04160b;
    font-weight: 800;
}

/* =========================================================
   COMPOSER
   ========================================================= */

.composer {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 100px;
    gap: 9px;

    padding: 11px 12px;

    background: #0e141a;
    border-top: 1px solid var(--line);

    flex-shrink: 0;
    min-width: 0;
}

.composer textarea {
    min-height: 48px;
    max-height: 120px;
    resize: vertical;
    overflow-y: auto;
    line-height: 1.4;
}

.composer button {
    border: 0;
    border-radius: 8px;
    background: var(--green);
    color: #04160b;
    font-weight: 800;
    cursor: pointer;
    min-height: 48px;
}

.composer button:hover {
    filter: brightness(1.06);
}

/* =========================================================
   MODAL
   ========================================================= */

.modal {
    position: fixed;
    inset: 0;
    z-index: 20;

    background: #0009;

    display: grid;
    place-items: center;

    padding: 20px;
}

.modal-card {
    width: min(760px, 96vw);
    max-height: 92vh;
    overflow-y: auto;
    overflow-x: hidden;

    background: #101720;
    border: 1px solid #2a3643;
    border-radius: 16px;

    box-shadow: 0 24px 80px #0008;

    padding: 22px;
}

.modal-head {
    display: flex;
    justify-content: space-between;
    gap: 20px;
}

.modal-head h2 {
    margin: 0;
    font-size: 21px;
}

.modal-head p,
.section-head p,
.webhook-box p {
    color: #7e8b98;
    margin: 5px 0 0;
}

.close {
    background: none;
    border: 0;
    color: #aab4bd;
    font-size: 27px;
    cursor: pointer;
}

/* =========================================================
   FORMULÁRIOS
   ========================================================= */

.form-grid {
    display: grid;
    grid-template-columns: 1fr 2fr;
    gap: 12px;
    margin-top: 20px;
}

.form-grid label {
    font-size: 11px;
    color: #8996a3;
}

.form-grid input {
    margin-top: 6px;
    color: #fff;
}

.form-grid .full {
    grid-column: 1 / -1;
}

.actions {
    display: flex;
    gap: 8px;
    margin-top: 13px;
    flex-wrap: wrap;
}

.actions.right {
    justify-content: flex-end;
}

/* =========================================================
   AVISOS
   ========================================================= */

.notice {
    margin-top: 14px;
    border-radius: 8px;
    padding: 10px 12px;
    font-size: 12px;
}

.notice.ok {
    background: #0c2e20;
    color: #9de6bc;
}

.notice.bad {
    background: #3a1719;
    color: #ffb7ba;
}

/* =========================================================
   INSTÂNCIAS
   ========================================================= */

.instance-section {
    margin-top: 22px;
    padding-top: 18px;
    border-top: 1px solid #26323e;
}

.section-head h3 {
    margin: 0;
}

.remote-list {
    margin-top: 12px;
    border: 1px solid #27333f;
    border-radius: 10px;
    overflow: hidden;
}

.remote-item {
    min-height: 58px;
    padding: 10px 12px;

    display: grid;
    grid-template-columns: 20px minmax(0, 1fr) auto auto;

    gap: 10px;
    align-items: center;

    border-bottom: 1px solid #202a34;

    cursor: pointer;
}

.remote-item:last-child {
    border-bottom: 0;
}

.remote-main {
    min-width: 0;
}

.remote-main b,
.remote-main small {
    display: block;
}

.remote-main b {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.remote-main small,
.last-sync {
    color: #6f7d89;
    font-size: 10px;
    margin-top: 3px;
}

.state {
    font-size: 10px;
    color: #c7a16b;
    background: #2d2418;
    padding: 4px 7px;
    border-radius: 999px;
}

.state.open {
    color: #91e7b3;
    background: #143122;
}

.placeholder {
    padding: 28px;
    text-align: center;
    color: #687582;
}

/* =========================================================
   WEBHOOK
   ========================================================= */

.webhook-box {
    margin-top: 22px;
    background: #0c1218;
    border: 1px solid #25323e;
    border-radius: 10px;
    padding: 14px;
}

.copy-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
    margin: 10px 0;
}

.webhook-box small {
    color: #6f7c88;
}

/* =========================================================
   TOAST
   ========================================================= */

.toast {
    position: fixed;
    right: 18px;
    bottom: 18px;
    z-index: 40;

    padding: 11px 14px;
    border-radius: 9px;

    background: #163b29;
    color: #b9efce;

    box-shadow: 0 10px 40px #0008;
}

.toast.bad {
    background: #461d21;
    color: #ffc1c4;
}

/* =========================================================
   SCROLLBAR
   ========================================================= */

.contacts::-webkit-scrollbar,
.messages::-webkit-scrollbar,
.modal-card::-webkit-scrollbar,
textarea::-webkit-scrollbar {
    width: 8px;
}

.contacts::-webkit-scrollbar-track,
.messages::-webkit-scrollbar-track,
.modal-card::-webkit-scrollbar-track,
textarea::-webkit-scrollbar-track {
    background: transparent;
}

.contacts::-webkit-scrollbar-thumb,
.messages::-webkit-scrollbar-thumb,
.modal-card::-webkit-scrollbar-thumb,
textarea::-webkit-scrollbar-thumb {
    background: #26333e;
    border-radius: 999px;
}

.contacts::-webkit-scrollbar-thumb:hover,
.messages::-webkit-scrollbar-thumb:hover,
.modal-card::-webkit-scrollbar-thumb:hover,
textarea::-webkit-scrollbar-thumb:hover {
    background: #344452;
}

/* =========================================================
   RESPONSIVO - TABLET
   ========================================================= */

@media (max-width: 1150px) {

    .app {
        grid-template-columns: 190px 320px minmax(0, 1fr);
    }

    .sidebar {
        padding-left: 10px;
        padding-right: 10px;
    }

    .msg {
        max-width: 80%;
    }

    .details {
        grid-template-columns: 170px minmax(0, 1fr) auto;
    }
}

@media (max-width: 950px) {

    .app {
        grid-template-columns: 175px 290px minmax(0, 1fr);
    }

    .details {
        grid-template-columns: 1fr auto;
    }

    .details textarea {
        grid-column: 1 / -1;
    }

    .details button {
        padding: 10px;
    }

    .chat-header {
        padding-left: 14px;
        padding-right: 14px;
    }

    .messages {
        padding: 16px;
    }
}

/* =========================================================
   RESPONSIVO - MOBILE
   ========================================================= */

@media (max-width: 720px) {

    html,
    body {
        overflow: hidden;
    }

    .app {
        display: block;
        width: 100%;
        height: 100dvh;
        overflow: hidden;
    }

    .sidebar {
        display: none;
    }

    .contacts-panel {
        position: absolute;
        inset: 0;

        display: flex;
        flex-direction: column;

        width: 100%;
        height: 100dvh;
    }

    .chat-panel {
        position: absolute;
        inset: 0;

        display: none;

        width: 100%;
        height: 100dvh;

        z-index: 5;
    }

    .chat-panel:has(.chat:not(.hidden)) {
        display: block;
    }

    .panel-title {
        min-height: 70px;
    }

    .chat-header {
        min-height: 62px;
        padding: 9px 12px;
        gap: 8px;
    }

    .chat-header h2 {
        font-size: 15px;
    }

    .chat-header select {
        min-width: 110px;
        max-width: 130px;
        padding: 8px;
    }

    .messages {
        padding: 12px;
    }

    .msg {
        max-width: 87%;
    }

    .details {
        grid-template-columns: 1fr;
        padding: 8px;
    }

    .details textarea {
        grid-column: auto;
    }

    .composer {
        grid-template-columns: minmax(0, 1fr) 74px;
        padding: 8px;
        gap: 7px;
    }

    .composer textarea {
        min-height: 44px;
        max-height: 100px;
    }

    .composer button {
        min-height: 44px;
        padding-left: 8px;
        padding-right: 8px;
    }

    .form-grid {
        grid-template-columns: 1fr;
    }

    .form-grid .full {
        grid-column: auto;
    }

    .modal {
        padding: 10px;
    }

    .modal-card {
        width: 100%;
        max-height: 96dvh;
        border-radius: 12px;
        padding: 16px;
    }

    .remote-item {
        grid-template-columns: 20px minmax(0, 1fr) auto;
    }

    .last-sync {
        display: none;
    }

    .copy-row {
        grid-template-columns: 1fr;
    }

    .copy-row button {
        width: 100%;
    }
}