/* WESB Messenger — WhatsApp-desktop-style client.
   Self-contained (CSP-safe): no external assets; doodle background is an inline SVG data-URI.
   Light + dark themes via [data-theme]. */

:root {
    --wa-green: #00a884;
    --wa-green-deep: #008069;
    --tick-blue: #53bdeb;

    --bg: #eae6df;              /* app frame behind panels */
    --rail: #f0f2f5;
    --panel: #ffffff;
    --panel-2: #f0f2f5;
    --list-hover: #f5f6f6;
    --list-active: #f0f2f5;
    --header: #f0f2f5;
    --line: #e9edef;
    --ink: #111b21;
    --muted: #667781;
    --faint: #8696a0;
    --chat-bg: #efeae2;
    --bubble-out: #d9fdd3;
    --bubble-in: #ffffff;
    --badge: #25d366;
    --danger: #ea4335;
    --shadow-sm: 0 1px 1px rgba(11, 20, 26, .06);
    --shadow-lg: 0 12px 40px rgba(11, 20, 26, .18);
    --doodle: rgba(17, 27, 33, .04);
    --sans: system-ui, -apple-system, "Segoe UI", "Helvetica Neue", Helvetica, Arial, sans-serif;
}

[data-theme="dark"] {
    --bg: #0b141a;
    --rail: #202c33;
    --panel: #111b21;
    --panel-2: #202c33;
    --list-hover: #202c33;
    --list-active: #2a3942;
    --header: #202c33;
    --line: #2a3942;
    --ink: #e9edef;
    --muted: #8696a0;
    --faint: #8696a0;
    --chat-bg: #0b141a;
    --bubble-out: #005c4b;
    --bubble-in: #202c33;
    --shadow-sm: 0 1px 1px rgba(0, 0, 0, .3);
    --doodle: rgba(233, 237, 239, .045);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body { font-family: var(--sans); background: var(--bg); color: var(--ink); font-size: 14.5px; -webkit-font-smoothing: antialiased; }
button { font: inherit; cursor: pointer; color: inherit; }
input { font: inherit; color: inherit; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--wa-green); outline-offset: -1px; }
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-thumb { background: rgba(11,20,26,.2); border-radius: 3px; }

/* Chat wallpaper (soft pastel). `scroll` attachment keeps it stationary while
   messages scroll over it. Dark mode falls back to a solid dark background. */
.doodle {
    background-color: var(--chat-bg);
    background-image: url("/app/bg_web.jpg?v=1");
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    background-attachment: scroll;
}
[data-theme="dark"] .doodle { background-image: none; }

/* ===================== auth ===================== */
#screen-auth { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px; background: var(--panel-2); }
#screen-auth::before { content: ""; position: fixed; top: 0; left: 0; right: 0; height: 200px; background: var(--wa-green-deep); z-index: 0; }
.auth-card {
    position: relative; z-index: 1; width: 400px; max-width: 100%; background: var(--panel);
    border-radius: 12px; padding: 34px 30px 26px; box-shadow: var(--shadow-lg);
}
.brand-row { display: flex; align-items: center; gap: 12px; margin-bottom: 6px; }
.logo-badge {
    width: 40px; height: 40px; border-radius: 50%; background: var(--wa-green); color: #fff;
    display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.logo-badge svg { width: 22px; height: 22px; }
.wordmark { font-weight: 700; font-size: 19px; letter-spacing: .01em; }
.auth-sub { font-size: 13px; color: var(--muted); margin: 4px 0 24px; display: flex; align-items: center; gap: 6px; }
.auth-sub svg { width: 14px; height: 14px; }
.field { margin-bottom: 15px; }
.field label { display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
.field input { width: 100%; padding: 11px 13px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); }
.field input:focus { outline: none; border-color: var(--wa-green); box-shadow: 0 0 0 3px rgba(0,168,132,.15); }
.btn-primary { width: 100%; padding: 12px; margin-top: 6px; border: 0; border-radius: 24px; background: var(--wa-green); color: #fff; font-weight: 600; }
.btn-primary:hover { background: var(--wa-green-deep); }
.btn-primary:disabled { opacity: .6; cursor: default; }
.auth-alt { display: block; width: 100%; background: none; border: 0; color: var(--wa-green); font-size: 13.5px; margin-top: 18px; }
.auth-error { color: var(--danger); font-size: 13px; min-height: 18px; margin-top: 10px; }

/* ===================== app frame ===================== */
#screen-app { display: flex; height: 100vh; overflow: hidden; }

/* icon rail */
.rail { width: 62px; background: var(--rail); border-right: 1px solid var(--line); display: flex; flex-direction: column; align-items: center; padding: 14px 0; gap: 6px; }
.rail-btn { width: 42px; height: 42px; border: 0; background: none; border-radius: 50%; display: flex; align-items: center; justify-content: center; color: var(--muted); position: relative; }
.rail-btn svg { width: 24px; height: 24px; }
.rail-btn:hover { background: var(--list-hover); }
.rail-btn.active { color: var(--ink); }
.rail-btn.active::before { content: ""; position: absolute; left: -14px; top: 9px; bottom: 9px; width: 4px; border-radius: 3px; background: var(--wa-green); }
.rail-spacer { flex: 1; }
.rail-avatar { width: 38px; height: 38px; border-radius: 50%; object-fit: cover; cursor: pointer; }

/* list column */
.list-col { width: 380px; min-width: 300px; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; }
.list-header { padding: 16px 16px 8px; display: flex; align-items: center; justify-content: space-between; }
.list-header h1 { font-size: 21px; font-weight: 700; }
.icon-btn { width: 38px; height: 38px; border: 0; background: none; border-radius: 50%; color: var(--muted); display: inline-flex; align-items: center; justify-content: center; }
.icon-btn svg { width: 21px; height: 21px; }
.icon-btn:hover { background: var(--list-hover); }
.list-search { padding: 6px 12px 8px; }
.search-wrap { display: flex; align-items: center; gap: 8px; background: var(--panel-2); border-radius: 8px; padding: 7px 12px; }
.search-wrap svg { width: 16px; height: 16px; color: var(--muted); flex-shrink: 0; }
.search-wrap input { flex: 1; border: 0; background: none; outline: none; }
.list-scroll { flex: 1; overflow-y: auto; }
.list-section { padding: 14px 16px 6px; font-size: 12px; font-weight: 600; color: var(--wa-green); }

.row { display: flex; align-items: center; gap: 13px; padding: 10px 14px; cursor: pointer; border-bottom: 1px solid var(--line); }
.row:hover { background: var(--list-hover); }
.row.active { background: var(--list-active); }
.avatar { width: 48px; height: 48px; border-radius: 50%; flex-shrink: 0; display: flex; align-items: center; justify-content: center; font-weight: 600; color: #fff; font-size: 18px; }
.avatar.sm { width: 40px; height: 40px; font-size: 15px; }
.row-body { flex: 1; min-width: 0; }
.row-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.row-name { font-size: 16px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-time { font-size: 12px; color: var(--muted); flex-shrink: 0; }
.row-time.unread { color: var(--wa-green); }
.row-sub { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 2px; }
.row-preview { font-size: 13.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; display: flex; align-items: center; gap: 4px; }
.badge-count { background: var(--badge); color: #fff; font-size: 11px; font-weight: 600; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; display: flex; align-items: center; justify-content: center; }
.row .sub-email { font-size: 13px; color: var(--muted); }
.list-empty { padding: 22px 18px; color: var(--muted); font-size: 13.5px; text-align: center; }

/* chat column */
.chat-col { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#chat-empty {
    flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
    text-align: center; background: var(--panel-2); border-bottom: 6px solid var(--wa-green);
}
#chat-empty .hero-icon { width: 190px; height: 190px; border-radius: 50%; background: var(--panel); display: flex; align-items: center; justify-content: center; color: var(--faint); }
#chat-empty .hero-icon svg { width: 90px; height: 90px; }
#chat-empty h2 { font-size: 30px; font-weight: 300; color: var(--ink); }
#chat-empty p { color: var(--muted); max-width: 420px; }
#chat-empty .lock-note { font-size: 13px; color: var(--faint); display: flex; align-items: center; gap: 6px; margin-top: 8px; }
#chat-empty .lock-note svg { width: 13px; height: 13px; }

#chat { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.chat-topbar { display: flex; align-items: center; gap: 12px; padding: 9px 16px; background: var(--header); border-bottom: 1px solid var(--line); }
.chat-topbar .ct-body { flex: 1; min-width: 0; cursor: pointer; }
.chat-topbar .ct-name { font-size: 16px; font-weight: 500; }
.chat-topbar .ct-sub { font-size: 12.5px; color: var(--muted); }
.chat-topbar .ct-actions { display: flex; gap: 2px; }

#messages { flex: 1; overflow-y: auto; padding: 16px 6.5% 12px; display: flex; flex-direction: column; gap: 3px; }
.day-sep { align-self: center; background: var(--panel); color: var(--muted); font-size: 12px; padding: 5px 12px; border-radius: 8px; box-shadow: var(--shadow-sm); margin: 8px 0; text-transform: uppercase; letter-spacing: .02em; }
.enc-note { align-self: center; background: #fff3c4; color: #54656f; font-size: 12.5px; padding: 6px 14px; border-radius: 8px; margin: 4px 0 10px; text-align: center; max-width: 520px; }
[data-theme="dark"] .enc-note { background: #182229; color: #ffd279; }
.msg { max-width: 65%; padding: 6px 9px 8px; border-radius: 8px; box-shadow: var(--shadow-sm); position: relative; line-height: 1.4; }
.msg.out { align-self: flex-end; background: var(--bubble-out); border-top-right-radius: 2px; }
.msg.in { align-self: flex-start; background: var(--bubble-in); border-top-left-radius: 2px; }
.msg .text { white-space: pre-wrap; word-wrap: break-word; padding-right: 58px; }
.msg .stamp { position: absolute; right: 9px; bottom: 5px; font-size: 11px; color: var(--muted); display: flex; align-items: center; gap: 3px; }
.ticks { width: 15px; height: 11px; flex-shrink: 0; }
.msg .stamp .ticks.read { color: var(--tick-blue); }
.row-preview .ticks { color: var(--muted); }

.composer { display: flex; align-items: center; gap: 10px; padding: 9px 16px; background: var(--header); }
.composer .icon-btn { color: var(--muted); }
.composer .field-wrap { flex: 1; background: var(--panel); border-radius: 10px; padding: 9px 14px; }
.composer input { width: 100%; border: 0; background: none; outline: none; }
.composer .send { width: 42px; height: 42px; border: 0; border-radius: 50%; background: none; color: var(--muted); display: flex; align-items: center; justify-content: center; }
.composer .send svg { width: 24px; height: 24px; }

/* ===================== modals (settings / new-chat / encryption) ===================== */
.overlay { position: fixed; inset: 0; background: rgba(11,20,26,.4); display: flex; align-items: center; justify-content: center; padding: 24px; z-index: 20; }
.sheet { width: 720px; max-width: 100%; height: 78vh; max-height: 640px; background: var(--panel); border-radius: 12px; box-shadow: var(--shadow-lg); display: flex; overflow: hidden; }

/* settings modal (nav + pane) */
.settings-nav { width: 300px; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; }
.settings-nav h2 { font-size: 22px; font-weight: 700; padding: 20px 22px 12px; }
.settings-nav .search-wrap { margin: 0 16px 10px; }
.settings-profile { display: flex; align-items: center; gap: 14px; padding: 12px 20px; margin: 0 8px 6px; border-radius: 8px; cursor: pointer; }
.settings-profile:hover { background: var(--list-hover); }
.settings-profile strong { font-size: 17px; font-weight: 500; }
.settings-list { flex: 1; overflow-y: auto; padding: 4px 0 12px; }
.settings-item { display: flex; align-items: center; gap: 18px; padding: 11px 22px; cursor: pointer; }
.settings-item svg { width: 22px; height: 22px; color: var(--muted); }
.settings-item:hover { background: var(--list-hover); }
.settings-item.active { background: var(--list-active); }
.settings-item.danger { color: var(--danger); }
.settings-item.danger svg { color: var(--danger); }

.settings-pane { flex: 1; background: var(--panel-2); display: flex; flex-direction: column; }
.pane-head { padding: 22px 26px 10px; font-size: 19px; font-weight: 600; background: var(--panel-2); }
.pane-body { flex: 1; overflow-y: auto; padding: 6px 26px 26px; }
.pane-foot { padding: 12px 20px; display: flex; justify-content: flex-end; border-top: 1px solid var(--line); background: var(--panel-2); }
.btn-done { background: var(--wa-green); color: #fff; border: 0; border-radius: 8px; padding: 9px 22px; font-weight: 600; }

.profile-photo { display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 26px 0 20px; }
.profile-photo .avatar { width: 150px; height: 150px; font-size: 54px; }
.profile-photo .edit { color: var(--wa-green); font-size: 14px; background: none; border: 0; }
.pane-label { font-size: 13px; color: var(--wa-green); margin: 22px 0 6px; }
.pane-value { background: var(--panel); border-radius: 8px; padding: 14px 16px; display: flex; justify-content: space-between; align-items: center; box-shadow: var(--shadow-sm); }
.pane-hint { font-size: 12.5px; color: var(--muted); margin-top: 8px; }

.card-group { background: var(--panel); border-radius: 10px; box-shadow: var(--shadow-sm); overflow: hidden; margin-bottom: 18px; }
.card-row { display: flex; align-items: center; justify-content: space-between; padding: 15px 18px; border-bottom: 1px solid var(--line); }
.card-row:last-child { border-bottom: 0; }
.card-row .cr-label { font-size: 15px; }
.card-row .cr-value { color: var(--muted); display: flex; align-items: center; gap: 6px; }
.card-title { font-size: 13px; color: var(--muted); margin: 6px 2px 8px; }

/* toggle */
.toggle { position: relative; width: 40px; height: 24px; flex-shrink: 0; }
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle .track { position: absolute; inset: 0; background: #c4c9cc; border-radius: 12px; transition: background .15s; }
[data-theme="dark"] .toggle .track { background: #3b4a54; }
.toggle .track::before { content: ""; position: absolute; width: 18px; height: 18px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: transform .15s; box-shadow: 0 1px 2px rgba(0,0,0,.3); }
.toggle input:checked + .track { background: var(--wa-green); }
.toggle input:checked + .track::before { transform: translateX(16px); }

/* segmented (appearance) */
.seg { display: flex; gap: 8px; }
.seg button { flex: 1; padding: 10px; border: 1px solid var(--line); background: var(--panel); border-radius: 8px; font-size: 14px; }
.seg button.on { border-color: var(--wa-green); color: var(--wa-green); font-weight: 600; box-shadow: 0 0 0 1px var(--wa-green) inset; }

/* new-chat panel (slides over list) */
.newchat { position: absolute; inset: 0; background: var(--panel); z-index: 5; display: flex; flex-direction: column; }
.newchat-head { background: var(--wa-green-deep); color: #fff; padding: 22px 16px 14px; display: flex; align-items: center; gap: 20px; }
.newchat-head .icon-btn { color: #fff; }
.newchat-head h3 { font-size: 18px; font-weight: 500; }
.newchat form { padding: 12px 16px; display: flex; gap: 8px; border-bottom: 1px solid var(--line); }
.newchat form input { flex: 1; border: 1px solid var(--line); border-radius: 8px; padding: 9px 12px; background: var(--panel); }
.newchat form button { border: 0; background: var(--wa-green); color: #fff; border-radius: 8px; padding: 0 16px; font-weight: 600; }

/* encryption sheet */
.enc-sheet { width: 420px; max-width: 100%; max-height: 80vh; overflow-y: auto; background: var(--panel); border-radius: 12px; padding: 26px 26px 20px; box-shadow: var(--shadow-lg); }
.enc-sheet h3 { display: flex; align-items: center; gap: 8px; font-size: 17px; margin-bottom: 6px; }
.enc-sheet h3 svg { width: 18px; height: 18px; color: var(--wa-green); }
.enc-sheet .enc-desc { font-size: 13.5px; color: var(--muted); margin-bottom: 18px; }
.sn { margin-bottom: 16px; }
.sn h4 { font-size: 14px; margin-bottom: 6px; }
.sn code { display: block; font-family: ui-monospace, Menlo, monospace; font-size: 15px; letter-spacing: .04em; line-height: 1.8; background: var(--panel-2); border-radius: 8px; padding: 12px 14px; word-spacing: .3em; overflow-wrap: anywhere; }
.enc-sheet .btn-done { margin-top: 8px; }

/* ===================== responsive ===================== */
@media (max-width: 820px) {
    .list-col { width: 320px; }
}
@media (max-width: 680px) {
    .rail { width: 54px; }
    .list-col { width: 100%; }
    #screen-app.chat-open .list-col { display: none; }
    #screen-app:not(.chat-open) .chat-col { display: none; }
    .msg { max-width: 82%; }
    .sheet { flex-direction: column; height: 90vh; }
    .settings-nav { width: 100%; border-right: 0; border-bottom: 1px solid var(--line); }
}
@media (prefers-reduced-motion: no-preference) {
    .msg { animation: pop .12s ease-out; }
    @keyframes pop { from { transform: translateY(4px) scale(.99); opacity: .5; } to { transform: none; opacity: 1; } }
}

/* mobile back button: hidden on desktop, shown in the narrow breakpoint */
.chat-back { display: none; }
@media (max-width: 680px) { .chat-back { display: inline-flex; } }

/* deterministic avatar colours (CSP-safe: classes, not inline styles) */
.avatar.av-0 { background: hsl(4 48% 55%); }
.avatar.av-1 { background: hsl(24 55% 52%); }
.avatar.av-2 { background: hsl(122 34% 45%); }
.avatar.av-3 { background: hsl(174 45% 40%); }
.avatar.av-4 { background: hsl(199 55% 48%); }
.avatar.av-5 { background: hsl(262 42% 58%); }
.avatar.av-6 { background: hsl(291 38% 52%); }
.avatar.av-7 { background: hsl(340 52% 56%); }
#conversation-list, #contact-list { list-style: none; }

.card-title.spaced { margin-top: 22px; }

/* ===================== mobile compatibility (standing requirement) =====================
   ≤680px: WhatsApp-mobile behaviour — bottom tab bar (like the mobile reference shots),
   full-screen chat with back button, full-screen sheets, no iOS focus-zoom. */

#screen-app { height: 100vh; height: 100dvh; }   /* dvh: correct height under mobile URL bar */

@media (max-width: 680px) {
    /* frame: column layout, rail becomes a bottom tab bar */
    #screen-app { flex-direction: column; }
    .rail {
        order: 2; width: 100%; height: 60px; flex-direction: row; align-items: center;
        justify-content: space-around; padding: 0 18px; gap: 0;
        border-right: 0; border-top: 1px solid var(--line);
    }
    .rail-btn { width: 46px; height: 46px; }
    .rail-btn.active::before { left: 8px; right: 8px; top: auto; bottom: 2px; width: auto; height: 3px; }
    .rail-spacer { display: none; }

    .list-col { width: 100%; min-width: 0; flex: 1; min-height: 0; border-right: 0; }
    .chat-col { min-height: 0; }

    /* one screen at a time, WhatsApp-mobile style */
    #screen-app.chat-open .list-col { display: none; }
    #screen-app.chat-open .rail { display: none; }
    #screen-app:not(.chat-open) .chat-col { display: none; }

    .msg { max-width: 85%; }
    #messages { padding: 14px 4% 10px; }

    /* iOS zooms any focused input under 16px — keep all inputs at 16px on mobile */
    input { font-size: 16px; }

    /* sheets go full-screen */
    .overlay { padding: 0; }
    .sheet { width: 100%; height: 100%; max-height: 100%; border-radius: 0; flex-direction: column; }
    .settings-nav { width: 100%; border-right: 0; border-bottom: 1px solid var(--line); max-height: 45%; }
    .settings-nav h2 { padding: 16px 20px 10px; }
    .enc-sheet { width: 100%; max-height: 100%; height: 100%; border-radius: 0; }

    /* comfortable touch targets */
    .row { padding: 12px 14px; }
    .icon-btn { width: 44px; height: 44px; }
}

/* ===================== chat header/composer pinned (sticky when typing) ===================== */
#chat { position: relative; }
.chat-topbar { position: sticky; top: 0; z-index: 3; }
.composer { position: sticky; bottom: 0; z-index: 3; }

/* ===================== confirm dialog ===================== */
.confirm-box { width: 340px; max-width: 100%; background: var(--panel); border-radius: 12px; padding: 22px 22px 16px; box-shadow: var(--shadow-lg); }
.confirm-box h3 { font-size: 17px; margin-bottom: 8px; }
.confirm-box p { font-size: 14px; color: var(--muted); margin-bottom: 20px; line-height: 1.5; }
.confirm-actions { display: flex; justify-content: flex-end; gap: 10px; }
.btn-ghost { border: 0; background: none; color: var(--wa-green); font-weight: 600; padding: 9px 16px; border-radius: 8px; }
.btn-ghost:hover { background: var(--list-hover); }
.btn-danger { border: 0; background: var(--danger); color: #fff; font-weight: 600; padding: 9px 18px; border-radius: 8px; }
.btn-danger:hover { filter: brightness(.94); }

/* settings pane back button: desktop hidden, mobile shown */
.pane-head { display: flex; align-items: center; gap: 8px; }
.settings-back { display: none; }

/* ===================== mobile settings overhaul ===================== */
@media (max-width: 680px) {
    /* full-screen single column: nav is the list; a chosen pane slides over it */
    .settings-nav { width: 100%; max-height: none; flex: 1; border-bottom: 0; }
    .settings-pane { display: none; }
    .sheet.pane-open .settings-nav { display: none; }
    .sheet.pane-open .settings-pane { display: flex; flex: 1; }
    .settings-back { display: inline-flex; }
    .pane-head { background: var(--wa-green-deep); color: #fff; padding: 16px; font-size: 18px; }
    .pane-head .icon-btn { color: #fff; }
    /* comfy card list like the mobile reference */
    .settings-list { padding: 8px 0 16px; }
    .settings-item { padding: 14px 20px; }
}

/* settings nav header + close button (close is the mobile way out of the modal) */
.settings-nav-head { display: flex; align-items: center; justify-content: space-between; padding-right: 8px; }
.settings-close { display: none; }
@media (max-width: 680px) {
    .settings-close { display: inline-flex; }
    /* the two-pane Done lives in the hidden pane on mobile, so the close button is the exit */
}

/* ===================== iOS Safari keyboard: pin app to the visual viewport ===================== */
/* Fixed so the page never scrolls under the keyboard; JS sizes it to visualViewport (see app.js). */
html, body { overflow: hidden; overscroll-behavior: none; }
#screen-app { position: fixed; top: 0; left: 0; right: 0; height: 100dvh; }

/* portrait wallpaper on phones */
@media (max-width: 680px) {
    .doodle { background-image: url("/app/bg_mobile.jpg?v=1"); }
    [data-theme="dark"] .doodle { background-image: none; }
}
