:root {
    --blue: #164f9f;
    --blue-dark: #0f2d57;
    --yellow: #f3c623;
    --green: #25d366;
    --bg: #f4f7fb;
    --text: #1d2733;
    --muted: #6d7a89;
    --border: #dfe6ef;
    --white: #ffffff;
    --danger: #b42318;
}

* { box-sizing: border-box; }
body {
    margin: 0;
    font-family: Arial, Helvetica, sans-serif;
    background: var(--bg);
    color: var(--text);
}
a { color: inherit; }
input, select, textarea, button {
    font: inherit;
}
.app-shell {
    min-height: 100vh;
    display: grid;
    grid-template-columns: 260px 1fr;
}
.sidebar {
    background: var(--blue-dark);
    color: #eef5ff;
    padding: 24px 18px;
    display: flex;
    flex-direction: column;
    gap: 24px;
}
.brand, .login-brand {
    display: flex;
    align-items: center;
    gap: 12px;
}
.brand img, .login-brand img {
    width: auto;
    max-width: 130px;
    height: 40px;
    object-fit: contain;
    background: #fff;
    border-radius: 8px;
    padding: 4px 8px;
}
.brand strong, .login-brand strong {
    display: block;
    font-size: 18px;
}
.brand span, .login-brand span {
    color: #b8c8dc;
    font-size: 13px;
}
.sidebar nav {
    display: grid;
    gap: 6px;
}
.sidebar a {
    text-decoration: none;
    padding: 12px 14px;
    border-radius: 8px;
    color: #dce8f7;
}
.sidebar a.active, .sidebar a:hover {
    background: rgba(255,255,255,.12);
    color: #fff;
}
.logout {
    margin-top: auto;
    background: rgba(243,198,35,.16);
}
.main {
    padding: 28px;
    min-width: 0;
}
.topbar {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 18px;
    margin-bottom: 22px;
}
h1, h2, p { margin-top: 0; }
h1 { font-size: 28px; margin-bottom: 4px; }
h2 { font-size: 18px; margin-bottom: 0; }
.topbar p, .muted, small { color: var(--muted); }
.user-pill, .badge {
    background: #e8f1ff;
    color: var(--blue);
    border-radius: 999px;
    padding: 8px 12px;
    font-size: 13px;
    white-space: nowrap;
}
.status-strip, .cards-grid, .detail-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 16px;
    margin-bottom: 18px;
}
.cards-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.status-strip > div, .card, .panel, .detail-grid > div {
    background: var(--white);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 18px;
    box-shadow: 0 10px 25px rgba(20, 50, 90, .04);
}
.status-strip span, .card span, .detail-grid span {
    display: block;
    color: var(--muted);
    font-size: 13px;
    margin-bottom: 8px;
}
.status-strip strong, .card strong, .detail-grid strong {
    font-size: 24px;
}
.status-strip strong.ok { color: var(--green); }
.status-strip strong.warn { color: #a15c00; }
.panel {
    margin-bottom: 18px;
}
.panel.narrow {
    max-width: 840px;
}
.panel-title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 16px;
}
.split {
    display: grid;
    grid-template-columns: minmax(320px, 420px) minmax(0, 1fr);
    gap: 18px;
    align-items: start;
}
.form {
    display: grid;
    gap: 14px;
}
label {
    display: grid;
    gap: 7px;
    font-weight: 700;
    font-size: 14px;
}
input, select, textarea {
    width: 100%;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 11px 12px;
    background: #fff;
    color: var(--text);
    font-weight: 400;
}
textarea { min-height: 100px; resize: vertical; }
.two-cols {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}
.fieldset {
    display: grid;
    gap: 14px;
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 14px 16px 16px;
    margin: 0;
}
.fieldset legend {
    font-weight: 700;
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: .04em;
    color: var(--muted);
    padding: 0 6px;
}
.checks {
    display: flex;
    gap: 16px;
    flex-wrap: wrap;
}
.checks label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 500;
}
.checks input { width: auto; }
.btn {
    border: 0;
    border-radius: 8px;
    padding: 11px 14px;
    cursor: pointer;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
}
.btn.primary { background: var(--blue); color: #fff; }
.btn.secondary { background: var(--yellow); color: #1f2937; }
.btn.ghost { background: #edf2f7; color: var(--blue-dark); }
.button-row, .inline-form {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    align-items: center;
}
.inline-form select { max-width: 360px; }
.link {
    border: 0;
    background: transparent;
    color: var(--blue);
    padding: 0;
    cursor: pointer;
    text-decoration: none;
    font-weight: 700;
}
.danger { color: var(--danger); }
.table-wrap {
    overflow-x: auto;
}
table {
    width: 100%;
    border-collapse: collapse;
    min-width: 680px;
}
th, td {
    text-align: left;
    padding: 12px 10px;
    border-bottom: 1px solid var(--border);
    vertical-align: top;
}
th {
    color: var(--muted);
    font-size: 13px;
}
.actions {
    display: flex;
    gap: 12px;
}
.actions form { margin: 0; }
.search input, .search select { min-width: 220px; }
.alert {
    padding: 12px 14px;
    border-radius: 8px;
    margin-bottom: 16px;
    background: #eaf7ed;
    color: #166534;
    border: 1px solid #bde5c8;
}
.alert.error {
    background: #fff1f1;
    color: var(--danger);
    border-color: #ffd0d0;
}
.login-body {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 20px;
    background: linear-gradient(135deg, #f4f7fb, #eaf1fb);
}
.login-card {
    width: min(420px, 100%);
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 28px;
    box-shadow: 0 20px 50px rgba(15,45,87,.14);
}
.login-brand {
    margin-bottom: 24px;
}
.login-brand span { color: var(--muted); }
.list-menu {
    display: grid;
    gap: 8px;
    margin-top: 20px;
}
.list-menu a {
    text-decoration: none;
    display: flex;
    justify-content: space-between;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
}
.list-menu a.active {
    border-color: var(--blue);
    background: #eef5ff;
}
.preview-panel {
    position: sticky;
    top: 18px;
}
.phone-preview {
    background: #e5ddd5;
    min-height: 360px;
    border-radius: 8px;
    padding: 20px;
    display: flex;
    align-items: flex-end;
    justify-content: flex-end;
}
.whatsapp-bubble {
    max-width: 82%;
    background: #dcf8c6;
    border-radius: 8px 8px 2px 8px;
    padding: 12px 14px;
    line-height: 1.45;
    white-space: pre-wrap;
}
.message-box {
    background: #f8fafc;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 16px;
}
.conversation-list {
    display: grid;
    gap: 12px;
}
.conversation-item {
    display: flex;
    flex-direction: column;
    gap: 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 14px;
}
.conversation-head {
    display: flex;
    justify-content: space-between;
    gap: 16px;
}
.conversation-item span {
    display: block;
    color: var(--muted);
    font-size: 13px;
    margin-top: 4px;
}
.conversation-item p { margin: 10px 0 0; }
.status-form {
    display: flex;
    gap: 8px;
    align-items: flex-start;
}
.reply-form {
    display: flex;
    gap: 8px;
    align-items: flex-end;
    border-top: 1px dashed var(--border);
    padding-top: 12px;
}
.reply-form textarea {
    flex: 1;
    min-height: 44px;
}
.chat-card {
    display: flex;
    flex-direction: column;
    gap: 12px;
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 14px;
}
.chat-card-head {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 16px;
}
.chat-card-head span {
    display: block;
    color: var(--muted);
    font-size: 13px;
    margin-top: 4px;
}
.chat-thread {
    display: flex;
    flex-direction: column;
    gap: 8px;
    max-height: 320px;
    overflow-y: auto;
    padding: 4px;
}
.bubble {
    max-width: 75%;
    padding: 8px 12px;
    border-radius: 12px;
    font-size: 14px;
}
.bubble p { margin: 0; white-space: pre-wrap; word-break: break-word; }
.bubble time {
    display: block;
    margin-top: 4px;
    font-size: 11px;
    color: var(--muted);
}
.bubble.in {
    align-self: flex-start;
    background: var(--surface-2, #f1f5f9);
    border-bottom-left-radius: 3px;
}
.bubble.out {
    align-self: flex-end;
    background: #dcf8c6;
    border-bottom-right-radius: 3px;
}
.bubble.out time { color: #5a7a52; }
.inline-form { display: inline-flex; gap: 6px; align-items: center; }
.campaign-actions { display: flex; gap: 16px; flex-wrap: wrap; align-items: flex-start; }

/* Conversas: layout de duas colunas estilo WhatsApp */
.chat-app {
    display: grid;
    grid-template-columns: 320px 1fr;
    padding: 0;
    overflow: hidden;
    height: calc(100vh - 190px);
    min-height: 480px;
}
.chat-sidebar {
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--border);
    min-width: 0;
}
.chat-sidebar-head {
    display: grid;
    gap: 8px;
    padding: 14px;
    border-bottom: 1px solid var(--border);
}
.chat-sidebar-head .search { width: 100%; }
.chat-sidebar-head select { width: 100%; }
.chat-sidebar-head input[type="search"] { width: 100%; }
.chat-thread-list {
    flex: 1;
    overflow-y: auto;
}
.thread-item {
    display: block;
    text-decoration: none;
    color: inherit;
    padding: 12px 14px;
    border-bottom: 1px solid var(--border);
}
.thread-item:hover { background: #f4f7fb; }
.thread-item.active { background: #e8f1ff; }
.thread-item-top {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 8px;
}
.thread-item-top strong { font-size: 14px; }
.thread-time { font-size: 11px; color: var(--muted); white-space: nowrap; }
.thread-item-bottom {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    margin-top: 4px;
}
.thread-preview {
    font-size: 13px;
    color: var(--muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.thread-item.unread .thread-preview { color: var(--text); font-weight: 700; }
.thread-item.unread strong { color: var(--blue); }
.thread-dot {
    width: 9px;
    height: 9px;
    border-radius: 50%;
    background: var(--green);
    flex-shrink: 0;
}
.chat-panel {
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    background: #efeae2;
}
.chat-panel-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    padding: 14px 18px;
    background: var(--white);
    border-bottom: 1px solid var(--border);
}
.chat-panel-head span {
    display: block;
    color: var(--muted);
    font-size: 13px;
}
.chat-messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 18px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.chat-window-note {
    padding: 0 18px;
    font-size: 12px;
}
.chat-app .reply-form {
    border-top: 1px solid var(--border);
    background: var(--white);
    padding: 12px 18px;
    margin: 0;
}
.chat-empty {
    flex: 1;
    display: grid;
    place-items: center;
    color: var(--muted);
}

.chat-panel-head-info { display: flex; align-items: center; gap: 10px; }
.chat-back {
    display: none;
    border: 0;
    background: #edf2f7;
    color: var(--blue-dark);
    width: 34px;
    height: 34px;
    border-radius: 50%;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    cursor: pointer;
}

@media (max-width: 920px) {
    .chat-app { grid-template-columns: 1fr; height: auto; }
    .chat-sidebar { border-right: 0; border-bottom: 1px solid var(--border); max-height: 320px; }
    .chat-app.show-panel .chat-sidebar { display: none; }
    .chat-app:not(.show-panel) .chat-panel { display: none; }
    .chat-panel-head { position: relative; }
    .chat-back {
        display: inline-flex;
    }
}

/* Tablet: reduz colunas dos cards antes de empilhar tudo */
@media (max-width: 1100px) {
    .cards-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .status-strip, .detail-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .split { grid-template-columns: 1fr; }
}

/* Layout de uma coluna: sidebar vira topo, conteudo empilha */
@media (max-width: 920px) {
    .app-shell { grid-template-columns: 1fr; }
    .sidebar {
        position: static;
        flex-direction: row;
        flex-wrap: wrap;
        align-items: center;
        gap: 12px;
        padding: 14px 16px;
    }
    .sidebar nav {
        grid-auto-flow: column;
        grid-template-columns: none;
        grid-auto-columns: max-content;
        overflow-x: auto;
        flex: 1 1 100%;
        gap: 4px;
    }
    .sidebar a { padding: 9px 12px; white-space: nowrap; }
    .logout { margin: 0; }
    .two-cols { grid-template-columns: 1fr; }
    .topbar, .panel-title { flex-direction: column; align-items: stretch; }
    .chat-card-head, .campaign-actions { flex-direction: column; align-items: stretch; }
    .main { padding: 18px; }
    .preview-panel { position: static; }
}

/* Celular: empilha formularios e ajusta tipografia */
@media (max-width: 600px) {
    .status-strip, .cards-grid, .detail-grid { grid-template-columns: 1fr; }
    .main { padding: 14px; }
    h1 { font-size: 22px; }
    .search input, .search select { min-width: 0; width: 100%; }
    .search { width: 100%; }
    .reply-form, .status-form { flex-direction: column; align-items: stretch; }
    .reply-form .btn, .status-form .btn { width: 100%; }
    .actions { flex-wrap: wrap; gap: 8px; }
    .inline-form { flex-wrap: wrap; }
    .bubble { max-width: 88%; }
    .button-row { flex-wrap: wrap; }
    .button-row .btn { flex: 1 1 auto; }
    .user-pill { align-self: flex-start; }
}

