/* WA Platform — UI styles. */

:root {
    --bg: #f0f2f5;
    --surface: #ffffff;
    --panel: #f7f8fa;
    --border: #e5e7eb;
    --text: #1c1c1e;
    --muted: #6b7280;
    --accent: #25d366;
    --accent-dark: #128c7e;
    --inbound: #ffffff;
    --outbound: #d9fdd3;
    --danger: #dc2626;
    --warning: #f59e0b;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

* { box-sizing: border-box; }

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

input, textarea, button, select {
    font-family: inherit;
    font-size: inherit;
}

.muted { color: var(--muted); }
.small { font-size: 12px; }

#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

/* ── Topbar ──────────────────────────────────────────── */

.topbar {
    display: grid;
    grid-template-columns: 200px 1fr auto;
    align-items: center;
    gap: 16px;
    padding: 10px 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.brand { font-weight: 600; font-size: 16px; }

.topnav { display: flex; gap: 4px; }
.topnav button {
    border: none;
    background: transparent;
    padding: 8px 14px;
    border-radius: 6px;
    cursor: pointer;
    color: var(--muted);
    font-weight: 500;
}
.topnav button:hover { background: var(--panel); color: var(--text); }
.topnav button.active { background: var(--panel); color: var(--text); }

.user-menu { display: flex; align-items: center; gap: 12px; }
.user-menu button {
    border: 1px solid var(--border);
    background: var(--surface);
    padding: 6px 12px;
    border-radius: 6px;
    cursor: pointer;
}

/* Force the HTML `hidden` attribute to always win, regardless of other rules.
   Without this, ID-level selectors like `#view-settings { display: block }`
   beat `.view[hidden] { display: none }` on specificity. */
[hidden] { display: none !important; }

.view {
    flex: 1;
    overflow: hidden;
    display: flex;
}

/* ── Inbox ───────────────────────────────────────────── */

#view-inbox {
    display: grid;
    grid-template-columns: 320px 1fr;
}

.sidebar {
    background: var(--surface);
    border-right: 1px solid var(--border);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    background: var(--surface);
}
.sidebar-header h2 { margin: 0; font-size: 14px; }
.sidebar-header button {
    border: none;
    background: transparent;
    cursor: pointer;
    font-size: 16px;
    color: var(--muted);
}

.conversation-list { flex: 1; }
.conversation-list .empty { padding: 16px; text-align: center; }

.conversation-row {
    display: grid;
    grid-template-columns: 36px 1fr auto;
    gap: 10px;
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
    cursor: pointer;
}
.conversation-row:hover { background: var(--panel); }
.conversation-row.active { background: #e7f3ff; }

.avatar {
    width: 36px; height: 36px;
    border-radius: 50%;
    background: var(--accent);
    color: white;
    display: flex; align-items: center; justify-content: center;
    font-weight: 600;
    font-size: 13px;
}

.conversation-row .meta {
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.conversation-row .name {
    font-weight: 500;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
}
.conversation-row .preview {
    color: var(--muted);
    font-size: 12px;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
}
.conversation-row .right {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 4px;
}
.conversation-row .time { font-size: 11px; color: var(--muted); }
.conversation-row .badge {
    background: var(--accent);
    color: white;
    border-radius: 10px;
    padding: 1px 7px;
    font-size: 11px;
    min-width: 18px;
    text-align: center;
}

/* ── Thread ──────────────────────────────────────────── */

.thread-pane {
    display: flex;
    flex-direction: column;
    background: var(--bg);
    overflow: hidden;
}

#thread-empty {
    margin: auto;
    max-width: 420px;
    text-align: center;
    padding: 24px;
}

#thread-active {
    display: flex;
    flex-direction: column;
    height: 100%;
}

.thread-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}
.thread-name { font-weight: 600; }

.thread-actions { display: flex; align-items: center; gap: 12px; }
.window-status { font-size: 12px; color: var(--muted); }
.window-status.open::before { content: '●'; color: var(--accent); margin-right: 4px; }
.window-status.closed::before { content: '●'; color: var(--warning); margin-right: 4px; }
.thread-actions button {
    border: 1px solid var(--border);
    background: var(--surface);
    padding: 6px 10px;
    border-radius: 6px;
    cursor: pointer;
}

.messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    background-image:
        linear-gradient(rgba(255,255,255,0.5), rgba(255,255,255,0.5)),
        radial-gradient(circle at top left, #d4ecdf 0, transparent 60%);
}

.bubble {
    max-width: 70%;
    padding: 8px 12px;
    border-radius: 8px;
    box-shadow: var(--shadow);
    background: var(--inbound);
    word-wrap: break-word;
    position: relative;
}
.bubble.outbound {
    align-self: flex-end;
    background: var(--outbound);
}
.bubble .meta {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 6px;
    margin-top: 4px;
    color: var(--muted);
    font-size: 11px;
}
.bubble .text { white-space: pre-wrap; }
.bubble.failed { border: 1px solid var(--danger); }
.bubble.failed .meta { color: var(--danger); }

.bubble img,
.bubble video { max-width: 100%; border-radius: 6px; display: block; }
.bubble audio { width: 100%; }
.bubble .file-attachment {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px;
    background: rgba(0,0,0,0.04);
    border-radius: 6px;
}

.status-tick { font-size: 11px; }
.status-pending { color: var(--muted); }
.status-sent { color: var(--muted); }
.status-delivered { color: var(--muted); }
.status-read { color: #4fc3f7; }
.status-failed { color: var(--danger); }

/* ── Composer ────────────────────────────────────────── */

.composer {
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding: 10px 12px;
    position: relative;
}

.window-warning {
    background: #fef3c7;
    border: 1px solid var(--warning);
    color: #92400e;
    padding: 8px 12px;
    border-radius: 6px;
    margin-bottom: 8px;
    font-size: 13px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}
.window-warning button {
    border: 1px solid var(--warning);
    background: white;
    padding: 4px 10px;
    border-radius: 4px;
    cursor: pointer;
    color: #92400e;
}

.composer-row {
    display: flex;
    align-items: flex-end;
    gap: 8px;
}

.icon-btn {
    background: transparent;
    border: none;
    padding: 6px 10px;
    font-size: 18px;
    cursor: pointer;
    color: var(--muted);
}
.icon-btn:hover { color: var(--accent); }
.icon-btn.recording { color: var(--danger); animation: pulse 1.4s infinite; }

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

#composer-text {
    flex: 1;
    border: 1px solid var(--border);
    border-radius: 18px;
    padding: 8px 14px;
    resize: none;
    max-height: 120px;
    line-height: 1.4;
}
#composer-text:focus {
    outline: 2px solid var(--accent);
    outline-offset: -1px;
    border-color: var(--accent);
}

button.primary {
    background: var(--accent);
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 18px;
    cursor: pointer;
    font-weight: 500;
}
button.primary:hover { background: var(--accent-dark); }
button:disabled { opacity: 0.5; cursor: not-allowed; }

.recording-state {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px;
    background: #fef2f2;
    border-radius: 8px;
}
.rec-dot {
    display: inline-block;
    width: 10px; height: 10px;
    border-radius: 50%;
    background: var(--danger);
    animation: pulse 1.2s infinite;
}
#rec-time { font-variant-numeric: tabular-nums; font-weight: 500; }
.recording-state button {
    border: 1px solid var(--border);
    background: white;
    padding: 6px 12px;
    border-radius: 6px;
    cursor: pointer;
    margin-left: auto;
}
/* Higher specificity than `.recording-state button` so the primary
   "Stop & send" button stays green instead of inheriting white. */
.recording-state button.primary {
    background: var(--accent);
    color: white;
    border: none;
}
.recording-state button.primary:hover {
    background: var(--accent-dark);
}

/* ── Link previews ───────────────────────────────────── */

.link-preview {
    margin-bottom: 6px;
}
.lp-card {
    display: block;
    border-left: 3px solid var(--accent);
    background: rgba(0, 0, 0, 0.04);
    border-radius: 6px;
    overflow: hidden;
    text-decoration: none;
    color: inherit;
    max-width: 320px;
    transition: background 0.12s;
}
.lp-card:hover {
    background: rgba(0, 0, 0, 0.07);
}
.bubble.outbound .lp-card {
    background: rgba(0, 0, 0, 0.06);
}
.bubble.outbound .lp-card:hover {
    background: rgba(0, 0, 0, 0.09);
}
.lp-card img {
    width: 100%;
    max-height: 180px;
    object-fit: cover;
    display: block;
    background: var(--border);
}
.lp-text {
    padding: 8px 10px;
}
.lp-site {
    font-size: 11px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.3px;
    margin-bottom: 2px;
}
.lp-title {
    font-weight: 600;
    font-size: 13px;
    line-height: 1.3;
    margin-bottom: 4px;
    color: var(--text);
}
.lp-desc {
    font-size: 12px;
    color: var(--muted);
    line-height: 1.4;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* ── Composer live link preview ──────────────────────── */

.composer-preview {
    position: relative;
    margin-bottom: 8px;
    padding: 10px;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    max-width: 340px;
}
.composer-preview .cp-loading {
    color: var(--muted);
    font-size: 12px;
    text-align: center;
    padding: 8px;
}
.composer-preview .cp-cleaned {
    font-size: 11px;
    color: var(--accent-dark);
    margin-bottom: 6px;
    padding: 6px 8px;
    background: rgba(37, 211, 102, 0.1);
    border-radius: 4px;
    line-height: 1.35;
}
.composer-preview .cp-dismiss {
    position: absolute;
    top: 4px;
    right: 4px;
    width: 22px;
    height: 22px;
    border: none;
    background: rgba(0, 0, 0, 0.08);
    color: var(--muted);
    border-radius: 50%;
    cursor: pointer;
    font-size: 16px;
    line-height: 1;
    padding: 0;
    z-index: 1;
}
.composer-preview .cp-dismiss:hover {
    background: rgba(0, 0, 0, 0.18);
    color: var(--text);
}
.composer-preview .lp-card {
    max-width: none;
}

/* ── Quick reply popup ───────────────────────────────── */

.quick-reply-popup {
    position: absolute;
    bottom: 100%;
    left: 16px;
    right: 16px;
    margin-bottom: 8px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: var(--shadow);
    max-height: 240px;
    overflow-y: auto;
}
.quick-reply-popup .qr-row {
    padding: 8px 12px;
    cursor: pointer;
    border-bottom: 1px solid var(--border);
}
.quick-reply-popup .qr-row:hover,
.quick-reply-popup .qr-row.active { background: var(--panel); }
.quick-reply-popup .qr-shortcut { font-weight: 600; color: var(--accent-dark); }
.quick-reply-popup .qr-body { color: var(--muted); font-size: 12px; }

/* ── Settings + Admin panels ─────────────────────────── */

#view-settings, #view-admin {
    display: block;
    overflow-y: auto;
    padding: 24px;
}

.panel {
    max-width: 800px;
    margin: 0 auto;
    background: var(--surface);
    padding: 24px;
    border-radius: 12px;
    box-shadow: var(--shadow);
}
.panel h2 { margin-top: 0; }

.panel-section {
    margin-top: 24px;
    padding-top: 16px;
    border-top: 1px solid var(--border);
}
.panel-section:first-of-type { border-top: 0; padding-top: 0; }
.panel-section h3 { margin: 0 0 8px; font-size: 15px; }

.row { display: flex; gap: 8px; align-items: center; }
.row select, .row input { flex: 1; padding: 8px; border: 1px solid var(--border); border-radius: 6px; }
.row button {
    padding: 8px 14px;
    border: 1px solid var(--border);
    background: var(--surface);
    border-radius: 6px;
    cursor: pointer;
}

.inline-form { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.inline-form input { padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; }
.inline-form button {
    padding: 8px 14px;
    border: none;
    background: var(--accent);
    color: white;
    border-radius: 6px;
    cursor: pointer;
}

.stacked-form { display: flex; flex-direction: column; gap: 10px; margin-top: 12px; }
.stacked-form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; }
.stacked-form input { padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; }
.stacked-form button {
    align-self: flex-start;
    padding: 8px 16px;
    border: none;
    background: var(--accent);
    color: white;
    border-radius: 6px;
    cursor: pointer;
}

.checkbox { flex-direction: row !important; align-items: center; gap: 8px; }

.qr-list, .tpl-list, #admin-waba-list, #admin-user-list {
    margin-top: 12px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.qr-item, .tpl-item, .waba-item, .user-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 12px;
    background: var(--panel);
    border-radius: 6px;
}
.qr-item code, .tpl-item code { color: var(--accent-dark); }
.tpl-item .badge {
    font-size: 11px;
    padding: 2px 6px;
    border-radius: 10px;
    background: var(--border);
}
.tpl-item .badge.APPROVED { background: #d1fae5; color: #065f46; }
.tpl-item .badge.PENDING { background: #fef3c7; color: #92400e; }
.tpl-item .badge.REJECTED { background: #fee2e2; color: #991b1b; }

/* ── Login ───────────────────────────────────────────── */

.login-body {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
}
.login-card {
    width: 360px;
    padding: 32px;
    background: var(--surface);
    border-radius: 12px;
    box-shadow: var(--shadow);
}
.login-card h1 { margin: 0 0 4px; font-size: 20px; }
.login-card form { display: flex; flex-direction: column; gap: 12px; margin-top: 20px; }
.login-card label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 500; }
.login-card input {
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font: inherit;
}
.login-card input:focus {
    outline: 2px solid var(--accent);
    outline-offset: -1px;
    border-color: var(--accent);
}
.login-card button {
    margin-top: 8px;
    padding: 10px;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
}
.error { color: var(--danger); font-size: 13px; margin: 0; }

/* ── Toast ───────────────────────────────────────────── */

.toast {
    position: fixed;
    bottom: 20px;
    right: 20px;
    background: #1f2937;
    color: white;
    padding: 12px 16px;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    z-index: 1000;
    max-width: 360px;
}
.toast.error { background: var(--danger); }
.toast.success { background: var(--accent); }

/* === bubble image size cap === */
/* Cap image bubbles. Without this, a tall product photo from a CDN
   renders at full natural resolution and disrupts scroll on send. */
.bubble img {
    max-width: min(100%, 340px);
    max-height: 380px;
    width: auto;
    height: auto;
    border-radius: 6px;
    display: block;
    cursor: zoom-in;
}
