:root {
    --wa-green: #00a884;
    --wa-green-dark: #008069;
    --wa-green-hover: #06cf9c;
    --wa-green-light: #d9fdd3;
    --wa-teal: #025144;
    
    /* Light Theme (Default) */
    --bg-app: #e2e8f0;
    --bg-sidebar: #ffffff;
    --bg-header: #f0f2f5;
    --bg-chat: #efeae2;
    --bg-bubble-in: #ffffff;
    --bg-bubble-out: #d9fdd3;
    --bg-input: #ffffff;
    --bg-card: #ffffff;
    --bg-hover: #f5f6f6;
    --bg-active: #ebebeb;
    --bg-badge: #00a884;
    --bg-pill: #ffffff;
    --bg-modal-overlay: rgba(11, 20, 26, 0.45);
    
    --text-primary: #111b21;
    --text-secondary: #667781;
    --text-muted: #8696a0;
    --text-link: #027eb5;
    --text-bubble-in: #111b21;
    --text-bubble-out: #111b21;
    
    --border-color: #e9edef;
    --border-subtle: #f0f2f5;
    --divider: #d1d7db;
    
    --tick-default: #8696a0;
    --tick-read: #53bdeb;
    --tick-failed: #ea0038;
    
    --shadow-sm: 0 1px 2px rgba(11, 20, 26, 0.06);
    --shadow-md: 0 4px 12px rgba(11, 20, 26, 0.08);
    --shadow-lg: 0 12px 32px rgba(11, 20, 26, 0.16);
    --shadow-bubble: 0 1px 0.5px rgba(11, 20, 26, 0.13);
    
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 18px;
    --radius-full: 9999px;
    
    --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --chat-pattern-opacity: 0.06;
}

[data-theme="dark"] {
    --bg-app: #0c1317;
    --bg-sidebar: #111b21;
    --bg-header: #202c33;
    --bg-chat: #0b141a;
    --bg-bubble-in: #202c33;
    --bg-bubble-out: #005c4b;
    --bg-input: #2a3942;
    --bg-card: #111b21;
    --bg-hover: #202c33;
    --bg-active: #2a3942;
    --bg-badge: #00a884;
    --bg-pill: #202c33;
    --bg-modal-overlay: rgba(0, 0, 0, 0.75);
    
    --text-primary: #e9edef;
    --text-secondary: #8696a0;
    --text-muted: #667781;
    --text-link: #53bdeb;
    --text-bubble-in: #e9edef;
    --text-bubble-out: #e9edef;
    
    --border-color: #222e35;
    --border-subtle: #182229;
    --divider: #2a3942;
    
    --tick-default: #8696a0;
    --tick-read: #53bdeb;
    --tick-failed: #f15c6d;
    
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.25);
    --shadow-md: 0 4px 14px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 16px 40px rgba(0, 0, 0, 0.6);
    --shadow-bubble: 0 1px 0.5px rgba(0, 0, 0, 0.25);
    
    --chat-pattern-opacity: 0.04;
}

/* Reset & Base */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    height: 100%;
    width: 100%;
    overflow: hidden;
    font-family: var(--font-sans);
    background-color: var(--bg-app);
    color: var(--text-primary);
    -webkit-font-smoothing: antialiased;
}

/* Custom Scrollbar */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}
::-webkit-scrollbar-track {
    background: transparent;
}
::-webkit-scrollbar-thumb {
    background: rgba(134, 150, 160, 0.35);
    border-radius: var(--radius-full);
}
::-webkit-scrollbar-thumb:hover {
    background: rgba(134, 150, 160, 0.55);
}

/* Main Layout */
.app {
    display: grid;
    grid-template-columns: 400px 1fr;
    height: 100vh;
    max-width: 1800px;
    margin: 0 auto;
    background: var(--bg-sidebar);
    box-shadow: var(--shadow-lg);
    position: relative;
    overflow: hidden;
}

/* Sidebar */
.sidebar {
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--border-color);
    background: var(--bg-sidebar);
    height: 100%;
    position: relative;
    z-index: 2;
    min-width: 0;
}

.sidebar-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 16px;
    background: var(--bg-header);
    min-height: 60px;
    border-bottom: 1px solid var(--border-subtle);
    gap: 8px;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
    flex: 1;
}

.logo-mark {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--wa-green) 0%, var(--wa-green-dark) 100%);
    color: #ffffff;
    display: grid;
    place-items: center;
    font-weight: 800;
    font-size: 14px;
    letter-spacing: 0.5px;
    box-shadow: 0 2px 6px rgba(0, 168, 132, 0.3);
    flex-shrink: 0;
}

.brand-info {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.brand-title {
    font-size: 15px;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.status-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    color: var(--text-secondary);
    margin-top: 1px;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background-color: var(--wa-green);
    display: inline-block;
    flex-shrink: 0;
    box-shadow: 0 0 0 2px rgba(0, 168, 132, 0.2);
    animation: pulse 2.5s infinite;
}

.status-dot.waiting {
    background-color: #f59e0b;
    box-shadow: 0 0 0 2px rgba(245, 158, 11, 0.2);
}

@keyframes pulse {
    0% { transform: scale(0.95); opacity: 0.85; }
    50% { transform: scale(1.15); opacity: 1; }
    100% { transform: scale(0.95); opacity: 0.85; }
}

.head-actions {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
}

.icon-btn {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    display: grid;
    place-items: center;
    cursor: pointer;
    transition: all 0.2s ease;
    text-decoration: none;
    position: relative;
}

.icon-btn:hover {
    background-color: var(--bg-hover);
    color: var(--text-primary);
}

.icon-btn:active {
    transform: scale(0.95);
}

.user-profile-badge {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 4px 10px 4px 6px;
    background: var(--bg-hover);
    border-radius: var(--radius-full);
    border: 1px solid var(--border-subtle);
    font-size: 12px;
    font-weight: 500;
    color: var(--text-primary);
    max-width: 140px;
}

.user-avatar-mini {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: var(--wa-green);
    color: #fff;
    display: grid;
    place-items: center;
    font-size: 10px;
    font-weight: 700;
}

.user-name-label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Search Bar & Filters */
.search-container {
    padding: 8px 14px 10px;
    background: var(--bg-sidebar);
    border-bottom: 1px solid var(--border-subtle);
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.search-input-wrapper {
    position: relative;
    display: flex;
    align-items: center;
}

.search-icon {
    position: absolute;
    left: 12px;
    color: var(--text-muted);
    pointer-events: none;
    display: grid;
    place-items: center;
}

.search-input {
    width: 100%;
    height: 36px;
    padding: 0 34px 0 38px;
    background: var(--bg-header);
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 14px;
    font-family: inherit;
    outline: none;
    transition: all 0.2s ease;
}

.search-input:focus {
    background: var(--bg-card);
    border-color: var(--wa-green);
    box-shadow: 0 0 0 2px rgba(0, 168, 132, 0.15);
}

.search-clear {
    position: absolute;
    right: 8px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: none;
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
    display: grid;
    place-items: center;
    font-size: 13px;
    transition: color 0.15s;
}

.search-clear:hover {
    color: var(--text-primary);
}

.filter-pills {
    display: flex;
    gap: 6px;
}

.filter-pill {
    padding: 5px 12px;
    border-radius: var(--radius-full);
    font-size: 12.5px;
    font-weight: 500;
    color: var(--text-secondary);
    background: var(--bg-header);
    border: 1px solid transparent;
    cursor: pointer;
    transition: all 0.15s ease;
    display: inline-flex;
    align-items: center;
    gap: 5px;
}

.filter-pill:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.filter-pill.active {
    background: var(--wa-green-light);
    color: var(--wa-teal);
    font-weight: 600;
}

[data-theme="dark"] .filter-pill.active {
    background: #005c4b;
    color: #e9edef;
}

.filter-count {
    font-size: 10.5px;
    padding: 1px 6px;
    border-radius: var(--radius-full);
    background: var(--wa-green);
    color: #ffffff;
    font-weight: 700;
}

/* Chat List */
.chat-list {
    flex: 1;
    overflow-y: auto;
    background: var(--bg-sidebar);
}

.chat-row {
    display: grid;
    grid-template-columns: 48px 1fr auto;
    gap: 14px;
    padding: 12px 16px;
    cursor: pointer;
    border-bottom: 1px solid var(--border-subtle);
    transition: background-color 0.15s ease;
    position: relative;
    user-select: none;
}

.chat-row:hover {
    background-color: var(--bg-hover);
}

.chat-row.active {
    background-color: var(--bg-active);
}

.chat-row.active::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 4px;
    background: var(--wa-green);
    border-radius: 0 4px 4px 0;
}

.avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    display: grid;
    place-items: center;
    color: #ffffff;
    font-weight: 600;
    font-size: 16px;
    flex-shrink: 0;
    letter-spacing: 0.5px;
    box-shadow: var(--shadow-sm);
    text-transform: uppercase;
}

.chat-row-main {
    min-width: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 4px;
}

.chat-row-title {
    font-size: 15px;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-row-preview {
    font-size: 13.5px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: flex;
    align-items: center;
    gap: 4px;
}

.preview-dir-icon {
    flex-shrink: 0;
    color: var(--text-muted);
}

.chat-row-meta {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: center;
    gap: 6px;
    flex-shrink: 0;
}

.chat-row-time {
    font-size: 11.5px;
    color: var(--text-muted);
    font-weight: 400;
}

.chat-row-time.unread {
    color: var(--wa-green);
    font-weight: 600;
}

.unread-badge {
    background-color: var(--wa-green);
    color: #ffffff;
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    border-radius: var(--radius-full);
    font-size: 11.5px;
    font-weight: 700;
    display: grid;
    place-items: center;
    box-shadow: 0 1px 3px rgba(0, 168, 132, 0.4);
    line-height: 1;
}

.empty-list {
    padding: 60px 24px;
    text-align: center;
    color: var(--text-secondary);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
}

.empty-list-icon {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    background: var(--bg-header);
    display: grid;
    place-items: center;
    color: var(--text-muted);
    margin-bottom: 4px;
}

.empty-list p {
    font-size: 14px;
    line-height: 1.5;
    max-width: 260px;
}

/* Right Chat Pane */
.pane {
    position: relative;
    min-width: 0;
    height: 100%;
    background-color: var(--bg-chat);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* Empty State / Welcome Screen */
.empty-pane {
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 32px;
    background: var(--bg-header);
    border-bottom: 6px solid var(--wa-green);
    position: relative;
}

.empty-art {
    margin-bottom: 24px;
    filter: drop-shadow(0 8px 24px rgba(0, 168, 132, 0.15));
}

.empty-pane h2 {
    color: var(--text-primary);
    font-size: 26px;
    font-weight: 300;
    margin-bottom: 10px;
    letter-spacing: -0.5px;
}

.empty-pane p {
    color: var(--text-secondary);
    font-size: 14px;
    line-height: 1.6;
    max-width: 440px;
    margin-bottom: 20px;
}

.empty-quick-badges {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    justify-content: center;
}

.quick-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 14px;
    background: var(--bg-sidebar);
    border-radius: var(--radius-full);
    font-size: 12px;
    color: var(--text-secondary);
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-sm);
}

/* Active Chat View */
.chat {
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%;
    overflow: hidden;
}

.chat-head {
    display: flex;
    align-items: center;
    padding: 10px 16px;
    background: var(--bg-header);
    border-bottom: 1px solid var(--border-subtle);
    gap: 12px;
    min-height: 60px;
    z-index: 5;
}

.back-btn {
    display: none;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: none;
    background: transparent;
    color: var(--text-primary);
    cursor: pointer;
}

.chat-head .avatar {
    width: 42px;
    height: 42px;
    font-size: 15px;
}

.chat-meta {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.chat-meta strong {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-meta-phone {
    font-size: 12.5px;
    color: var(--text-secondary);
}

.chat-head-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.window-pill {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    font-weight: 500;
    padding: 4px 10px;
    border-radius: var(--radius-full);
    border: 1px solid transparent;
}

.window-pill.open {
    background-color: rgba(0, 168, 132, 0.12);
    color: var(--wa-green-dark);
    border-color: rgba(0, 168, 132, 0.25);
}

[data-theme="dark"] .window-pill.open {
    background-color: rgba(0, 168, 132, 0.2);
    color: #25d366;
}

.window-pill.closed {
    background-color: rgba(234, 0, 56, 0.08);
    color: #c51838;
    border-color: rgba(234, 0, 56, 0.2);
}

[data-theme="dark"] .window-pill.closed {
    background-color: rgba(234, 0, 56, 0.2);
    color: #ff6b81;
}

/* Chat Message Thread */
.thread {
    flex: 1;
    overflow-y: auto;
    padding: 18px 5% 18px;
    background-color: var(--bg-chat);
    background-image: 
        radial-gradient(circle at 15% 15%, rgba(0,0,0, var(--chat-pattern-opacity)) 1.5px, transparent 1.5px),
        radial-gradient(circle at 85% 65%, rgba(0,0,0, var(--chat-pattern-opacity)) 1.5px, transparent 1.5px);
    background-size: 28px 28px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.day-chip {
    display: flex;
    justify-content: center;
    margin: 14px 0 10px;
    position: sticky;
    top: 8px;
    z-index: 2;
}

.day-chip span {
    background: var(--bg-pill);
    color: var(--text-secondary);
    font-size: 12px;
    font-weight: 500;
    padding: 5px 14px;
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border-subtle);
    backdrop-filter: blur(8px);
}

.bubble-row {
    display: flex;
    margin: 2px 0;
    width: 100%;
}

.bubble-row.in {
    justify-content: flex-start;
}

.bubble-row.out {
    justify-content: flex-end;
}

.bubble {
    max-width: min(75%, 620px);
    padding: 7px 10px 6px 12px;
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-bubble);
    position: relative;
    font-size: 14.2px;
    line-height: 1.45;
    overflow-wrap: anywhere;
    transition: transform 0.1s ease;
}

.bubble-row.in .bubble {
    background: var(--bg-bubble-in);
    color: var(--text-bubble-in);
    border-top-left-radius: 0;
}

.bubble-row.out .bubble {
    background: var(--bg-bubble-out);
    color: var(--text-bubble-out);
    border-top-right-radius: 0;
}

/* Bubble tail */
.bubble-row.in .bubble::before {
    content: '';
    position: absolute;
    top: 0;
    left: -8px;
    width: 0;
    height: 0;
    border-top: 10px solid var(--bg-bubble-in);
    border-left: 8px solid transparent;
}

.bubble-row.out .bubble::before {
    content: '';
    position: absolute;
    top: 0;
    right: -8px;
    width: 0;
    height: 0;
    border-top: 10px solid var(--bg-bubble-out);
    border-right: 8px solid transparent;
}

.bubble .caption {
    margin-top: 4px;
    color: inherit;
    font-size: 14.2px;
}

.bubble .kind-tag {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--wa-green-dark);
    margin-bottom: 4px;
}

[data-theme="dark"] .bubble .kind-tag {
    color: #25d366;
}

.bubble .meta {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 4px;
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 4px;
    user-select: none;
    float: right;
    margin-left: 12px;
}

.ticks {
    display: inline-flex;
    align-items: center;
}

.ticks svg {
    width: 15px;
    height: 15px;
    fill: var(--tick-default);
}

.ticks.read svg {
    fill: var(--tick-read);
}

.ticks.failed {
    color: var(--tick-failed);
    font-weight: 800;
}

/* Media styling inside bubbles */
.media-container {
    margin: -3px -6px 4px -8px;
    border-radius: 6px;
    overflow: hidden;
}

.media-container img {
    max-width: 320px;
    max-height: 320px;
    width: 100%;
    object-fit: cover;
    display: block;
    cursor: pointer;
    transition: transform 0.2s ease;
    border-radius: 6px;
}

.media-container img:hover {
    opacity: 0.95;
}

.media-container video {
    max-width: 340px;
    width: 100%;
    border-radius: 6px;
    display: block;
    outline: none;
}

.media-audio-box {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 6px 4px;
    min-width: 240px;
}

.media-audio-box audio {
    width: 100%;
    height: 36px;
    outline: none;
}

.doc-card {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    background: rgba(0, 0, 0, 0.04);
    border-radius: var(--radius-sm);
    text-decoration: none;
    color: var(--text-primary);
    transition: background 0.15s ease;
    margin: 2px 0;
}

[data-theme="dark"] .doc-card {
    background: rgba(255, 255, 255, 0.06);
}

.doc-card:hover {
    background: rgba(0, 0, 0, 0.08);
}

[data-theme="dark"] .doc-card:hover {
    background: rgba(255, 255, 255, 0.1);
}

.doc-icon {
    width: 38px;
    height: 38px;
    border-radius: 8px;
    background: var(--wa-green);
    color: #fff;
    display: grid;
    place-items: center;
    flex-shrink: 0;
}

.doc-info {
    flex: 1;
    min-width: 0;
}

.doc-name {
    font-size: 13.5px;
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.doc-ext {
    font-size: 11px;
    color: var(--text-muted);
    text-transform: uppercase;
}

.loc-card {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    background: rgba(0, 168, 132, 0.08);
    border-radius: var(--radius-sm);
    text-decoration: none;
    color: var(--wa-green-dark);
    font-weight: 600;
    font-size: 13.5px;
    margin-top: 4px;
}

[data-theme="dark"] .loc-card {
    color: #25d366;
}

/* Quick Replies Dropdown */
.quick-replies-bar {
    display: flex;
    gap: 6px;
    padding: 6px 16px;
    background: var(--bg-header);
    border-top: 1px solid var(--border-subtle);
    overflow-x: auto;
    white-space: nowrap;
}

.quick-reply-chip {
    padding: 4px 10px;
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-full);
    font-size: 12px;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.15s ease;
    user-select: none;
}

.quick-reply-chip:hover {
    background: var(--wa-green);
    color: #ffffff;
    border-color: var(--wa-green);
}

/* Composer / Input Area */
.composer-wrapper {
    background: var(--bg-header);
    display: flex;
    flex-direction: column;
}

.composer {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 10px 16px 12px;
}

.composer-input-box {
    flex: 1;
    background: var(--bg-input);
    border-radius: var(--radius-sm);
    border: 1px solid transparent;
    display: flex;
    align-items: flex-end;
    padding: 6px 12px;
    box-shadow: var(--shadow-sm);
    transition: all 0.2s ease;
}

.composer-input-box:focus-within {
    border-color: var(--wa-green);
    box-shadow: 0 0 0 2px rgba(0, 168, 132, 0.15);
}

.composer textarea {
    flex: 1;
    border: none;
    background: transparent;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 15px;
    line-height: 1.4;
    resize: none;
    max-height: 140px;
    outline: none;
    padding: 4px 0;
}

.composer textarea::placeholder {
    color: var(--text-muted);
}

.send-btn {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: none;
    background: var(--wa-green);
    color: #ffffff;
    display: grid;
    place-items: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: all 0.2s ease;
    box-shadow: 0 2px 6px rgba(0, 168, 132, 0.3);
}

.send-btn:hover:not(:disabled) {
    background: var(--wa-green-hover);
    transform: scale(1.05);
}

.send-btn:active:not(:disabled) {
    transform: scale(0.96);
}

.send-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    box-shadow: none;
}

.composer-note {
    margin: 0;
    padding: 8px 16px 10px;
    font-size: 12.5px;
    color: #b54708;
    background: rgba(245, 158, 11, 0.08);
    display: flex;
    align-items: center;
    gap: 8px;
    border-top: 1px solid rgba(245, 158, 11, 0.2);
}

[data-theme="dark"] .composer-note {
    color: #fbbf24;
}

/* Modal Dialog */
.modal {
    position: fixed;
    inset: 0;
    background: var(--bg-modal-overlay);
    display: grid;
    place-items: center;
    z-index: 50;
    padding: 20px;
    backdrop-filter: blur(4px);
    animation: fadeIn 0.15s ease-out;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.modal[hidden], [hidden] {
    display: none !important;
}

.modal-card {
    background: var(--bg-card);
    width: min(680px, 100%);
    max-height: 90vh;
    overflow-y: auto;
    border-radius: var(--radius-md);
    padding: 24px;
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border-color);
    animation: slideUp 0.2s ease-out;
}

@keyframes slideUp {
    from { transform: translateY(12px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

.modal-card header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-bottom: 16px;
    border-bottom: 1px solid var(--border-color);
    margin-bottom: 16px;
}

.modal-card h2 {
    font-size: 20px;
    font-weight: 600;
    color: var(--text-primary);
}

.modal-card h3 {
    font-size: 15px;
    font-weight: 600;
    color: var(--text-primary);
    margin: 22px 0 10px;
}

.modal-card label, .setup-form label {
    display: block;
    margin: 14px 0 6px;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
}

.modal-card input, .setup-form input, .modal-card select {
    width: 100%;
    border: 1px solid var(--divider);
    border-radius: var(--radius-sm);
    padding: 10px 14px;
    font: inherit;
    font-size: 14px;
    background: var(--bg-header);
    color: var(--text-primary);
    outline: none;
    transition: all 0.2s ease;
}

.modal-card input:focus, .setup-form input:focus, .modal-card select:focus {
    border-color: var(--wa-green);
    background: var(--bg-card);
    box-shadow: 0 0 0 2px rgba(0, 168, 132, 0.15);
}

.input-with-copy {
    display: flex;
    gap: 8px;
    align-items: center;
}

.input-with-copy input {
    flex: 1;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    border: none;
    border-radius: var(--radius-sm);
    padding: 10px 18px;
    font-family: inherit;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    transition: all 0.15s ease;
}

.btn.primary {
    background: var(--wa-green);
    color: #ffffff;
    margin-top: 16px;
    box-shadow: 0 2px 6px rgba(0, 168, 132, 0.3);
}

.btn.primary:hover {
    background: var(--wa-green-hover);
    transform: translateY(-1px);
}

.btn.primary:active {
    transform: translateY(0);
}

.btn.secondary {
    background: var(--bg-header);
    color: var(--text-primary);
    border: 1px solid var(--divider);
}

.btn.secondary:hover {
    background: var(--bg-hover);
}

/* Stats Cards Grid */
.stats {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    margin: 14px 0 16px;
}

.stat {
    background: var(--bg-header);
    border-radius: var(--radius-sm);
    padding: 12px 14px;
    border: 1px solid var(--border-subtle);
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.stat b {
    font-size: 22px;
    font-weight: 700;
    color: var(--text-primary);
}

.stat span {
    font-size: 12px;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* User Management Rows */
.user-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 14px;
    background: var(--bg-header);
    border-radius: var(--radius-sm);
    margin-bottom: 6px;
    font-size: 13.5px;
}

.user-role-pill {
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    padding: 2px 8px;
    border-radius: var(--radius-full);
    background: rgba(0, 168, 132, 0.15);
    color: var(--wa-green-dark);
}

[data-theme="dark"] .user-role-pill {
    color: #25d366;
}

.log-box {
    background: #0b141a;
    color: #d1f4ea;
    border-radius: var(--radius-sm);
    padding: 14px;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    font-size: 12px;
    line-height: 1.5;
    max-height: 200px;
    overflow: auto;
    white-space: pre-wrap;
    border: 1px solid #222e35;
}

/* Toast Notification */
.toast {
    position: fixed;
    left: 50%;
    bottom: 30px;
    transform: translateX(-50%);
    background: var(--bg-header);
    color: var(--text-primary);
    padding: 10px 20px;
    border-radius: var(--radius-full);
    z-index: 100;
    font-size: 14px;
    font-weight: 500;
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border-color);
    animation: toastPop 0.25s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes toastPop {
    from { transform: translate(-50%, 20px) scale(0.9); opacity: 0; }
    to { transform: translate(-50%, 0) scale(1); opacity: 1; }
}

/* Auth / Setup Pages */
.setup-body {
    min-height: 100vh;
    display: grid;
    place-items: center;
    background: linear-gradient(180deg, var(--wa-green) 0%, var(--wa-green) 220px, var(--bg-app) 220px, var(--bg-app) 100%);
    padding: 24px;
}

.setup-card {
    width: min(500px, 100%);
    background: var(--bg-card);
    border-radius: var(--radius-md);
    padding: 36px 32px;
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border-color);
    position: relative;
    z-index: 5;
}

.setup-brand {
    display: flex;
    gap: 16px;
    align-items: center;
    margin-bottom: 24px;
}

.setup-brand h1 {
    font-size: 22px;
    font-weight: 700;
    color: var(--text-primary);
}

.setup-brand p {
    margin-top: 4px;
    color: var(--text-secondary);
    font-size: 13.5px;
}

.grid-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0 14px;
}

.banner {
    padding: 12px 16px;
    border-radius: var(--radius-sm);
    margin-bottom: 18px;
    font-size: 13.5px;
    line-height: 1.45;
}

.banner.ok {
    background: rgba(0, 168, 132, 0.12);
    color: var(--wa-green-dark);
    border: 1px solid rgba(0, 168, 132, 0.3);
}

.banner.err {
    background: rgba(234, 0, 56, 0.1);
    color: #c51838;
    border: 1px solid rgba(234, 0, 56, 0.3);
}

/* Lightbox Modal */
.lightbox-modal {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.9);
    display: grid;
    place-items: center;
    z-index: 100;
    cursor: zoom-out;
}

.lightbox-modal img {
    max-width: 90vw;
    max-height: 90vh;
    object-fit: contain;
    border-radius: 8px;
    box-shadow: var(--shadow-lg);
}

/* Responsive Mobile Layout */
@media (max-width: 860px) {
    body {
        background-color: var(--bg-sidebar);
    }

    .app {
        grid-template-columns: 1fr;
        height: 100dvh;
        max-width: 100%;
        box-shadow: none;
        border-radius: 0;
    }
    
    .sidebar {
        width: 100%;
        height: 100dvh;
        border-right: none;
    }

    .sidebar.hide-mobile {
        display: none !important;
    }
    
    .pane {
        width: 100%;
        height: 100dvh;
        display: none;
    }

    .pane.show-mobile {
        display: flex !important;
        flex-direction: column;
        height: 100dvh;
    }
    
    .pane.show-mobile .back-btn {
        display: grid;
        place-items: center;
        flex-shrink: 0;
    }
    
    .pane:not(.show-mobile) {
        display: none !important;
    }

    /* Mobile Header optimizations */
    .sidebar-head, .chat-head {
        padding: 8px 12px;
        min-height: 54px;
    }

    .user-profile-badge {
        max-width: 90px;
        padding: 3px 6px;
    }

    .user-profile-badge .user-name-label {
        font-size: 11px;
    }

    .brand-title {
        font-size: 14px;
    }

    .status-badge {
        font-size: 11px;
    }

    /* Mobile Thread & Bubbles */
    .thread {
        padding: 12px 10px 14px;
    }

    .bubble {
        max-width: 88%;
        font-size: 13.5px;
        padding: 6px 9px 5px 10px;
    }

    .media-container img {
        max-width: 240px;
        max-height: 240px;
    }

    .media-container video {
        max-width: 240px;
    }

    .media-audio-box {
        min-width: 190px;
    }

    /* Composer on Mobile */
    .composer-wrapper {
        padding-bottom: env(safe-area-inset-bottom, 0px);
    }

    .composer {
        padding: 6px 10px 8px;
        gap: 6px;
    }

    .composer textarea {
        font-size: 14px;
    }

    .send-btn {
        width: 38px;
        height: 38px;
    }

    .send-btn svg {
        width: 17px;
        height: 17px;
    }

    .quick-replies-bar {
        padding: 6px 10px;
    }

    /* Modal on Mobile */
    .modal {
        padding: 10px;
        align-items: flex-end;
    }

    .modal-card {
        max-height: 92dvh;
        padding: 18px 16px;
        border-radius: var(--radius-md) var(--radius-md) 0 0;
        width: 100%;
    }

    .grid-2 {
        grid-template-columns: 1fr;
        gap: 0;
    }
    
    .stats {
        grid-template-columns: 1fr 1fr;
        gap: 6px;
    }

    .stat {
        padding: 8px 10px;
    }

    .stat b {
        font-size: 18px;
    }

    /* Auth pages on mobile */
    .setup-body {
        padding: 12px;
        background: var(--bg-card);
    }

    .setup-card {
        padding: 24px 18px;
        box-shadow: none;
        border: 1px solid var(--border-color);
    }
}
