:root {
    --wa-green: #075E54;
    --wa-green-light: #128C7E;
    --wa-green-dark: #054640;
    --wa-teal: #25D366;
    --wa-bg: #ECE5DD;
    --wa-chat-bg: #E5DDD5;
    --wa-bubble-out: #DCF8C6;
    --wa-bubble-in: #FFFFFF;
    --wa-text: #111B21;
    --wa-text-secondary: #667781;
    --wa-border: #E9EDEF;
    --wa-danger: #EA4335;
    --wa-warning: #FBBC04;
    --shadow: 0 1px 3px rgba(0,0,0,0.12);
    --radius: 12px;
    --nav-height: 64px;
    --header-height: 56px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    font-size: 16px;
    color: var(--wa-text);
    background: var(--wa-bg);
    overflow: hidden;
}

a { color: inherit; text-decoration: none; }

.app {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    max-width: 480px;
    margin: 0 auto;
    background: #fff;
    position: relative;
    box-shadow: 0 0 20px rgba(0,0,0,0.1);
}

/* Top Bar */
.top-bar {
    background: var(--wa-green);
    color: #fff;
    flex-shrink: 0;
    z-index: 10;
}

.top-bar-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    min-height: var(--header-height);
}

.top-bar-title {
    font-size: 18px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.top-bar-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.user-badge {
    font-size: 12px;
    background: rgba(255,255,255,0.2);
    padding: 4px 10px;
    border-radius: 20px;
}

.btn-icon {
    font-size: 20px;
    padding: 4px;
}

/* Main Content */
.main-content {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: calc(var(--nav-height) + var(--safe-bottom));
}

.main-chat {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    padding-bottom: 0;
}

/* Bottom Nav */
.bottom-nav {
    display: flex;
    align-items: center;
    justify-content: space-around;
    background: #fff;
    border-top: 1px solid var(--wa-border);
    height: calc(var(--nav-height) + var(--safe-bottom));
    padding-bottom: var(--safe-bottom);
    position: fixed;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 100%;
    max-width: 480px;
    z-index: 100;
}

.nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: 8px 16px;
    color: var(--wa-text-secondary);
    font-size: 11px;
    transition: color 0.2s;
}

.nav-item.active { color: var(--wa-green); }
.nav-icon { font-size: 22px; }

.nav-fab {
    position: relative;
    top: -16px;
}

.nav-icon-fab {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 52px;
    height: 52px;
    background: var(--wa-teal);
    color: #fff;
    border-radius: 50%;
    font-size: 28px;
    font-weight: 300;
    box-shadow: 0 4px 12px rgba(37,211,102,0.4);
}

/* Search Bar */
.search-bar {
    padding: 8px 12px;
    background: var(--wa-green);
}

.search-input {
    width: 100%;
    padding: 10px 16px;
    border: none;
    border-radius: 24px;
    background: #fff;
    font-size: 15px;
    outline: none;
}

/* Chat List */
.chat-list { list-style: none; }

.chat-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    border-bottom: 1px solid var(--wa-border);
    cursor: pointer;
    transition: background 0.15s;
}

.chat-item:active { background: #f5f5f5; }

.chat-avatar {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background: var(--wa-green-light);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    font-weight: 600;
    flex-shrink: 0;
}

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

.chat-name {
    font-weight: 600;
    font-size: 16px;
    margin-bottom: 2px;
}

.chat-preview {
    font-size: 14px;
    color: var(--wa-text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-meta {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 4px;
    flex-shrink: 0;
}

.chat-time {
    font-size: 12px;
    color: var(--wa-text-secondary);
}

.unread-badge {
    background: var(--wa-teal);
    color: #fff;
    font-size: 12px;
    font-weight: 600;
    min-width: 20px;
    height: 20px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 6px;
}

/* Chat View */
.chat-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 12px;
    background: var(--wa-green);
    color: #fff;
    flex-shrink: 0;
}

.chat-header .back-btn {
    font-size: 24px;
    padding: 4px 8px;
    color: #fff;
}

.chat-header-info { flex: 1; min-width: 0; }
.chat-header-name { font-weight: 600; font-size: 16px; }
.chat-header-phone { font-size: 13px; opacity: 0.85; }

.chat-messages {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 12px;
    background: var(--wa-chat-bg);
    background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23d4cdc6' fill-opacity='0.4'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
}

.message {
    display: flex;
    margin-bottom: 4px;
}

.message.outbound { justify-content: flex-end; }
.message.inbound { justify-content: flex-start; }

.message-bubble {
    max-width: 80%;
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 15px;
    line-height: 1.4;
    box-shadow: var(--shadow);
    word-wrap: break-word;
}

.message.outbound .message-bubble {
    background: var(--wa-bubble-out);
    border-top-right-radius: 0;
}

.message.inbound .message-bubble {
    background: var(--wa-bubble-in);
    border-top-left-radius: 0;
}

.message-time {
    font-size: 11px;
    color: var(--wa-text-secondary);
    text-align: right;
    margin-top: 2px;
}

.message-status {
    font-size: 11px;
    margin-left: 4px;
}

.message-media img {
    max-width: 100%;
    border-radius: 6px;
    margin-bottom: 4px;
}

.message-media a {
    color: var(--wa-green);
    text-decoration: underline;
}

/* Chat Input */
.chat-input-bar {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 8px 12px;
    padding-bottom: calc(8px + var(--safe-bottom));
    background: #f0f0f0;
    flex-shrink: 0;
}

.chat-input {
    flex: 1;
    padding: 10px 16px;
    border: none;
    border-radius: 24px;
    background: #fff;
    font-size: 16px;
    resize: none;
    max-height: 120px;
    outline: none;
    font-family: inherit;
    line-height: 1.4;
}

.btn-send {
    width: 44px;
    height: 44px;
    border: none;
    border-radius: 50%;
    background: var(--wa-teal);
    color: #fff;
    font-size: 20px;
    cursor: pointer;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

.btn-send:disabled {
    background: #ccc;
    cursor: not-allowed;
}

.btn-attach {
    width: 40px;
    height: 40px;
    border: none;
    background: none;
    font-size: 22px;
    cursor: pointer;
    flex-shrink: 0;
}

/* Login Page */
.login-page {
    min-height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, var(--wa-green) 0%, var(--wa-green-light) 100%);
    padding: 20px;
}

.login-card {
    background: #fff;
    border-radius: 16px;
    padding: 32px 24px;
    width: 100%;
    max-width: 400px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.2);
}

.login-logo {
    text-align: center;
    margin-bottom: 24px;
}

.login-logo .icon {
    font-size: 48px;
    margin-bottom: 8px;
}

.login-logo h1 {
    font-size: 22px;
    color: var(--wa-green);
}

.login-logo p {
    color: var(--wa-text-secondary);
    font-size: 14px;
}

.form-group {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    font-size: 14px;
    font-weight: 500;
    margin-bottom: 6px;
    color: var(--wa-text);
}

.form-input {
    width: 100%;
    padding: 12px 16px;
    border: 1px solid var(--wa-border);
    border-radius: 8px;
    font-size: 16px;
    outline: none;
    transition: border-color 0.2s;
}

.form-input:focus {
    border-color: var(--wa-green);
}

.btn-primary {
    width: 100%;
    padding: 14px;
    background: var(--wa-teal);
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s;
}

.btn-primary:active { background: #1da851; }

.alert {
    padding: 12px 16px;
    border-radius: 8px;
    margin-bottom: 16px;
    font-size: 14px;
}

.alert-error {
    background: #fce8e6;
    color: var(--wa-danger);
    border: 1px solid #f5c6c2;
}

.alert-success {
    background: #e6f4ea;
    color: #1e7e34;
    border: 1px solid #b7dfb9;
}

.alert-warning {
    background: #fef7e0;
    color: #b06000;
    border: 1px solid #fce8a8;
}

/* Forms Page */
.page-form {
    padding: 16px;
}

.page-form h2 {
    font-size: 18px;
    margin-bottom: 16px;
}

/* Contact List */
.contact-filters {
    display: flex;
    gap: 8px;
    padding: 12px 16px;
    overflow-x: auto;
}

.filter-chip {
    padding: 6px 16px;
    border-radius: 20px;
    font-size: 13px;
    background: #f0f0f0;
    color: var(--wa-text-secondary);
    white-space: nowrap;
    border: none;
    cursor: pointer;
}

.filter-chip.active {
    background: var(--wa-green);
    color: #fff;
}

/* Empty State */
.empty-state {
    text-align: center;
    padding: 48px 24px;
    color: var(--wa-text-secondary);
}

.empty-state .icon { font-size: 48px; margin-bottom: 12px; }
.empty-state h3 { font-size: 18px; margin-bottom: 8px; color: var(--wa-text); }
.empty-state p { font-size: 14px; margin-bottom: 16px; }

/* Status badges */
.status-badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 10px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
}

.status-new { background: #e3f2fd; color: #1565c0; }
.status-active { background: #e8f5e9; color: #2e7d32; }
.status-closed { background: #f5f5f5; color: #757575; }

/* Template notice */
.template-notice {
    background: #fff3cd;
    border: 1px solid #ffc107;
    border-radius: 8px;
    padding: 12px;
    margin: 8px 12px;
    font-size: 13px;
}

.template-notice select {
    width: 100%;
    margin-top: 8px;
    padding: 8px;
    border-radius: 6px;
    border: 1px solid #ddd;
}

.hidden { display: none !important; }

/* Loading */
.loading {
    text-align: center;
    padding: 24px;
    color: var(--wa-text-secondary);
}

.spinner {
    display: inline-block;
    width: 24px;
    height: 24px;
    border: 3px solid #f3f3f3;
    border-top: 3px solid var(--wa-green);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Desktop wider view */
@media (min-width: 481px) {
    .app { border-left: 1px solid var(--wa-border); border-right: 1px solid var(--wa-border); }
}

/* Hide bottom nav on chat page */
.chat-page .bottom-nav { display: none; }
.chat-page .main-content { padding-bottom: 0; }

/* Admin styles */
.admin-page {
    max-width: 900px;
    margin: 0 auto;
    padding: 20px;
    overflow-y: auto;
    height: 100dvh;
}

.admin-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
}

.admin-card {
    background: #fff;
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 16px;
    box-shadow: var(--shadow);
}

.admin-card h3 {
    margin-bottom: 16px;
    font-size: 16px;
    color: var(--wa-green);
}

.admin-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
}

.admin-table th, .admin-table td {
    padding: 10px 12px;
    text-align: left;
    border-bottom: 1px solid var(--wa-border);
}

.admin-table th {
    background: #f8f9fa;
    font-weight: 600;
}

.btn-sm {
    padding: 6px 12px;
    border: none;
    border-radius: 6px;
    font-size: 13px;
    cursor: pointer;
}

.btn-danger { background: var(--wa-danger); color: #fff; }
.btn-secondary { background: #f0f0f0; color: var(--wa-text); }
.btn-green { background: var(--wa-green); color: #fff; }

.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 12px;
    margin-bottom: 20px;
}

.stat-card {
    background: #fff;
    border-radius: var(--radius);
    padding: 16px;
    text-align: center;
    box-shadow: var(--shadow);
}

.stat-value {
    font-size: 28px;
    font-weight: 700;
    color: var(--wa-green);
}

.stat-label {
    font-size: 12px;
    color: var(--wa-text-secondary);
    margin-top: 4px;
}
