/* ==========================================================================
   Madina WhatsApp Inbox — Design System
   Palette: warm charcoal base + brass/gold signature accent + emerald status
   Type: Fraunces (display) / Inter (UI) / IBM Plex Mono (data)
   ========================================================================== */

:root {
    --bg:            #F5F6F8;
    --panel:         #FFFFFF;
    --panel-alt:     #EEF0F3;
    --panel-hover:   #E4E7EC;
    --border:        #E1E4EA;
    --border-soft:   #EAEDF1;

    --gold:          #A9791A;
    --gold-soft:     #D4AF37;
    --gold-dim:      rgba(169, 121, 26, 0.10);

    --emerald:       #1F8A65;
    --emerald-dim:   rgba(31, 138, 101, 0.12);
    --danger:        #D92D20;
    --danger-dim:    rgba(217, 45, 32, 0.10);

    --text:          #1B1E24;
    --text-muted:    #667085;
    --text-faint:    #98A2B3;

    --font-display: 'Fraunces', Georgia, serif;
    --font-body:    'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --font-mono:    'IBM Plex Mono', ui-monospace, monospace;

    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 16px;

    --shadow-lift: 0 12px 32px rgba(16, 24, 40, 0.12);
}

* { box-sizing: border-box; }

html, body {
    height: 100%;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font-body);
    font-size: 14px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

::selection { background: var(--gold-dim); color: var(--gold); }

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

/* Prominent scrollbars — thin but visibly always present so users know a
   pane can scroll. Firefox uses scrollbar-color for the same effect. */
* { scrollbar-width: thin; scrollbar-color: #C5CAD3 transparent; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #C5CAD3; border-radius: 8px; border: 2px solid var(--panel); }
::-webkit-scrollbar-thumb:hover { background: #8A94A5; }

:focus-visible {
    outline: 2px solid var(--gold);
    outline-offset: 2px;
    border-radius: 4px;
}

h1, h2, h3 { font-family: var(--font-display); font-weight: 600; margin: 0; }

.mono { font-family: var(--font-mono); }

/* ---------------------------------------------------------------
   Buttons & form fields
   --------------------------------------------------------------- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    font-family: var(--font-body);
    font-size: 13.5px;
    font-weight: 600;
    padding: 10px 18px;
    border-radius: var(--radius-md);
    border: 1px solid transparent;
    cursor: pointer;
    transition: background .15s ease, border-color .15s ease, transform .1s ease;
    white-space: nowrap;
}
.btn:active { transform: translateY(1px); }

.btn-gold {
    background: linear-gradient(180deg, var(--gold-soft), var(--gold));
    color: #1A1503;
}
.btn-gold:hover { filter: brightness(1.08); }

.btn-ghost {
    background: transparent;
    border-color: var(--border);
    color: var(--text);
}
.btn-ghost:hover { background: var(--panel-hover); }

.btn-danger-ghost {
    background: transparent;
    border-color: var(--danger-dim);
    color: var(--danger);
}
.btn-danger-ghost:hover { background: var(--danger-dim); }

.btn-icon {
    width: 38px;
    height: 38px;
    padding: 0;
    border-radius: 50%;
}

.field {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 16px;
}
.field label {
    font-size: 12.5px;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: .04em;
}
.field input,
.field select,
.field textarea {
    background: var(--panel-alt);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 11px 13px;
    border-radius: var(--radius-sm);
    font-family: var(--font-body);
    font-size: 14px;
    transition: border-color .15s ease;
}
.field input:focus,
.field select:focus,
.field textarea:focus {
    border-color: var(--gold);
    outline: none;
}
.field .hint { font-size: 12px; color: var(--text-faint); }

.badge {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 3px 10px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .04em;
}
.badge-gold   { background: var(--gold-dim); color: var(--gold); }
.badge-emerald{ background: var(--emerald-dim); color: var(--emerald); }
.badge-muted  { background: #0000000d; color: var(--text-muted); }
.badge-danger { background: var(--danger-dim); color: var(--danger); }

.avatar {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    font-family: var(--font-display);
    font-weight: 600;
    color: #17130A;
    flex-shrink: 0;
}

.alert {
    padding: 12px 14px;
    border-radius: var(--radius-sm);
    font-size: 13.5px;
    margin-bottom: 16px;
    border: 1px solid transparent;
}
.alert-danger { background: var(--danger-dim); border-color: rgba(217,45,32,.25); color: #A82318; }
.alert-success { background: var(--emerald-dim); border-color: rgba(31,138,101,.25); color: #15694B; }

/* ---------------------------------------------------------------
   Auth / login screen
   --------------------------------------------------------------- */
.auth-shell {
    min-height: 100vh;
    display: grid;
    grid-template-columns: 1fr 1fr;
}
.auth-visual {
    position: relative;
    background:
        radial-gradient(circle at 20% 20%, rgba(169,121,26,.10), transparent 45%),
        radial-gradient(circle at 80% 70%, rgba(31,138,101,.08), transparent 45%),
        linear-gradient(160deg, #FFFFFF, #F3EFE4);
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 56px;
    border-right: 1px solid var(--border);
}
.auth-visual .thread {
    position: absolute;
    top: 0; bottom: 0; left: 0;
    width: 3px;
    background: linear-gradient(180deg, transparent, var(--gold) 20%, var(--gold) 80%, transparent);
    opacity: .55;
}
.brand-mark {
    font-family: var(--font-display);
    font-style: italic;
    font-size: 26px;
    color: var(--gold);
    letter-spacing: .01em;
}
.brand-sub { color: var(--text-muted); font-size: 13px; margin-top: 4px; }
.auth-quote {
    font-family: var(--font-display);
    font-size: 28px;
    line-height: 1.3;
    color: var(--text);
    max-width: 440px;
}
.auth-quote span { color: var(--gold); font-style: italic; }
.auth-meta { color: var(--text-faint); font-size: 12.5px; font-family: var(--font-mono); }

.auth-form-wrap {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 40px;
}
.auth-form {
    width: 100%;
    max-width: 380px;
}
.auth-form h1 {
    font-size: 26px;
    margin-bottom: 6px;
}
.auth-form .lead {
    color: var(--text-muted);
    font-size: 13.5px;
    margin-bottom: 28px;
}
.auth-form .btn { width: 100%; padding: 12px; }

@media (max-width: 880px) {
    .auth-shell { grid-template-columns: 1fr; }
    .auth-visual { display: none; }
}

/* ---------------------------------------------------------------
   Dashboard shell
   --------------------------------------------------------------- */
.shell {
    display: grid;
    grid-template-columns: 76px 320px 1fr;
    height: 100vh;
    overflow: hidden;
}
.shell-admin {
    grid-template-columns: 76px 1fr;
}

/* Rail */
.rail {
    background: var(--panel);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 18px 0;
}
.rail .brand-dot {
    width: 40px; height: 40px;
    border-radius: 12px;
    background: linear-gradient(160deg, var(--gold-soft), var(--gold));
    display: flex; align-items: center; justify-content: center;
    font-family: var(--font-display); font-style: italic; font-weight: 700;
    color: #1A1503; font-size: 18px;
    margin-bottom: 28px;
}
.rail-nav { display: flex; flex-direction: column; gap: 6px; flex: 1; }
.rail-link {
    width: 46px; height: 46px;
    display: flex; align-items: center; justify-content: center;
    border-radius: var(--radius-md);
    color: var(--text-muted);
    position: relative;
}
.rail-link svg { width: 20px; height: 20px; }
.rail-link:hover { background: var(--panel-hover); color: var(--text); }
.rail-link.active { background: var(--gold-dim); color: var(--gold); }
.rail-link.active::before {
    content: '';
    position: absolute; left: -18px; top: 50%; transform: translateY(-50%);
    width: 3px; height: 22px; border-radius: 2px;
    background: var(--gold);
}
.rail-foot { display: flex; flex-direction: column; align-items: center; gap: 14px; }

/* Conversation list */
.convo-pane {
    background: var(--panel);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;    /* CRITICAL for the flex child .convo-list to scroll — without
                         this, flex children with `flex: 1; overflow-y: auto;` can
                         grow past the viewport instead of scrolling. */
    height: 100%;
    overflow: hidden;
}
.convo-pane-head {
    padding: 20px 18px 14px;
    border-bottom: 1px solid var(--border-soft);
}
.convo-pane-head h2 { font-size: 19px; }
.search-box {
    margin-top: 12px;
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--panel-alt);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 9px 12px;
}
.search-box input {
    background: transparent; border: none; color: var(--text);
    font-size: 13.5px; width: 100%; outline: none;
}
.search-box svg { width: 15px; height: 15px; color: var(--text-faint); flex-shrink: 0; }

.filter-tabs {
    display: flex; gap: 6px; padding: 12px 18px 0;
}
.filter-tab {
    font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em;
    padding: 6px 12px; border-radius: 999px; color: var(--text-muted);
    cursor: pointer; border: 1px solid var(--border);
}
.filter-tab.active { background: var(--gold-dim); border-color: transparent; color: var(--gold); }

.convo-list { flex: 1; overflow-y: auto; padding: 10px; }
.convo-item {
    display: flex; gap: 12px; padding: 12px; border-radius: var(--radius-md);
    cursor: pointer; border-left: 3px solid transparent; margin-bottom: 2px;
    transition: background .12s ease;
}
.convo-item:hover { background: var(--panel-hover); }
.convo-item.active {
    background: var(--panel-hover);
    border-left-color: var(--gold);
}
.convo-item .avatar { width: 44px; height: 44px; font-size: 15px; background: var(--gold-soft); }
.convo-item-body { min-width: 0; flex: 1; }
.convo-item-top { display: flex; justify-content: space-between; gap: 8px; }
.convo-item-name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.convo-item-time { font-size: 11px; color: var(--text-faint); font-family: var(--font-mono); flex-shrink: 0; }
.convo-item-preview {
    font-size: 12.5px; color: var(--text-muted); margin-top: 2px;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.convo-item-bottom { display: flex; justify-content: space-between; align-items: center; margin-top: 6px; }
.unread-dot {
    min-width: 18px; height: 18px; border-radius: 999px; background: var(--gold);
    color: #1A1503; font-size: 10.5px; font-weight: 800; display: flex; align-items: center; justify-content: center;
    padding: 0 5px;
}

.empty-state {
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    text-align: center; color: var(--text-faint); height: 100%; padding: 40px;
    gap: 10px;
}
.empty-state svg { width: 46px; height: 46px; opacity: .5; }

/* Chat pane */
.chat-pane {
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;   /* Same flex-scroll fix as .convo-pane — needed for .chat-log to scroll */
    height: 100%;
    overflow: hidden;
    background: var(--bg);
}
.chat-head {
    display: flex; align-items: center; justify-content: space-between;
    padding: 16px 24px; border-bottom: 1px solid var(--border-soft);
    background: var(--panel);
}
.chat-head-id { display: flex; align-items: center; gap: 12px; min-width: 0; }
.chat-head-id .avatar { width: 40px; height: 40px; font-size: 14px; background: var(--gold-soft); }
.chat-head-name { font-weight: 600; font-size: 15px; }
.chat-head-phone { font-size: 12px; color: var(--text-faint); font-family: var(--font-mono); }
.chat-head-actions { display: flex; align-items: center; gap: 10px; }
.chat-head-actions select {
    background: var(--panel-alt); border: 1px solid var(--border); color: var(--text);
    padding: 8px 10px; border-radius: var(--radius-sm); font-size: 12.5px;
}

.thread {
    flex: 1; overflow-y: auto; padding: 24px;
    display: flex; flex-direction: column; gap: 4px;
    background-image: radial-gradient(circle at 1px 1px, #00000010 1px, transparent 0);
    background-size: 22px 22px;
}
.day-divider {
    align-self: center; font-size: 11px; color: var(--text-faint);
    background: var(--panel-alt); padding: 4px 12px; border-radius: 999px;
    margin: 14px 0 10px; font-family: var(--font-mono);
}
.bubble-row { display: flex; margin-bottom: 4px; }
.bubble-row.out { justify-content: flex-end; }
.bubble {
    max-width: 62%; padding: 10px 13px; border-radius: 14px; font-size: 13.5px;
    line-height: 1.45; position: relative; box-shadow: 0 1px 0 rgba(0,0,0,.15);
}
.bubble.in {
    background: var(--panel-alt); color: var(--text);
    border-bottom-left-radius: 4px;
}
.bubble.out {
    background: linear-gradient(155deg, #FFF8E8, #FCEFC9);
    border: 1px solid var(--gold-dim);
    color: var(--text);
    border-bottom-right-radius: 4px;
}
.bubble-meta {
    display: flex; align-items: center; gap: 5px; margin-top: 5px;
    font-size: 10.5px; color: var(--text-faint); font-family: var(--font-mono);
}
.bubble-meta.out { justify-content: flex-end; }
.tick-read { color: var(--emerald); }

.composer {
    padding: 16px 24px; border-top: 1px solid var(--border-soft); background: var(--panel);
}
.composer-window-warning {
    display: flex; align-items: center; gap: 8px;
    background: var(--danger-dim); color: #A82318; font-size: 12.5px;
    padding: 8px 12px; border-radius: var(--radius-sm); margin-bottom: 10px;
}
.composer-row { display: flex; align-items: flex-end; gap: 10px; }
.composer-row textarea {
    flex: 1; resize: none; background: var(--panel-alt); border: 1px solid var(--border);
    color: var(--text); border-radius: var(--radius-md); padding: 12px 14px;
    font-family: var(--font-body); font-size: 14px; max-height: 140px; min-height: 44px;
}
.composer-row textarea:focus { outline: none; border-color: var(--gold); }

/* Users / settings pages reuse the shell but with a scroll pane */
.page-pane {
    flex: 1;
    min-height: 0;    /* Grid child needs this too, in case parent becomes flex */
    height: 100%;
    overflow-y: auto;
    padding: 32px 40px;
}
.page-head { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 28px; gap: 20px; }
.page-head h1 { font-size: 24px; }
.page-head p { color: var(--text-muted); font-size: 13.5px; margin-top: 6px; }

.card {
    background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius-lg);
    padding: 22px;
}
.table-card { padding: 0; overflow: hidden; }
table { width: 100%; border-collapse: collapse; }
th {
    text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .04em;
    color: var(--text-faint); font-weight: 700; padding: 12px 20px; border-bottom: 1px solid var(--border);
}
td { padding: 14px 20px; border-bottom: 1px solid var(--border-soft); font-size: 13.5px; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: var(--panel-hover); }

.row-user { display: flex; align-items: center; gap: 10px; }
.row-user .avatar { width: 32px; height: 32px; font-size: 12px; }

.modal-backdrop {
    position: fixed; inset: 0; background: rgba(10,11,13,.65); backdrop-filter: blur(2px);
    display: none; align-items: center; justify-content: center; z-index: 50;
}
.modal-backdrop.open { display: flex; }
.modal {
    background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius-lg);
    width: 100%; max-width: 420px; padding: 26px; box-shadow: var(--shadow-lift);
}
.modal h3 { font-size: 18px; margin-bottom: 4px; }
.modal .sub { color: var(--text-muted); font-size: 12.5px; margin-bottom: 20px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 6px; }

/* Mobile: collapse to single active pane, rail becomes a bottom tab bar */
.mobile-back { display: none; }
@media (max-width: 980px) {
    .shell, .shell-admin {
        grid-template-columns: 1fr;
        height: 100dvh;
    }

    .rail {
        position: fixed;
        left: 0; right: 0; bottom: 0;
        width: 100%;
        height: 64px;
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        padding: 0 14px;
        border-right: none;
        border-top: 1px solid var(--border);
        z-index: 40;
    }
    .rail .brand-dot { display: none; }
    .rail-nav {
        flex-direction: row;
        flex: 1;
        justify-content: space-around;
        align-items: center;
        gap: 4px;
    }
    .rail-link.active::before { display: none; }
    .rail-foot {
        flex-direction: row;
        gap: 10px;
    }

    .convo-pane, .chat-pane, .page-pane { grid-column: span 1; }
    body[data-view="chat"] .convo-pane { display: none; }
    body[data-view="list"] .chat-pane { display: none; }
    .mobile-back { display: inline-flex; }

    .convo-pane, .chat-pane { padding-bottom: 64px; }
    .page-pane { padding: 22px 18px 84px; }
    .convo-list { padding-bottom: 8px; }
}

@media (max-width: 460px) {
    .rail-link { width: 40px; height: 40px; }
    .rail-foot .avatar { width: 32px; height: 32px; font-size: 11px; }
}

.status-dot {
    width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex-shrink: 0;
}
.status-dot.open { background: var(--emerald); }
.status-dot.pending { background: var(--gold); }
.status-dot.closed { background: var(--text-faint); }
