:root {
    --blue: #0866ff;
    --blue-dark: #0758da;
    --ink: #111827;
    --muted: #6b7280;
    --line: #e5e7eb;
    --soft: #f3f4f6;
    --panel: #ffffff;
    --shadow: 0 18px 60px rgba(17, 24, 39, .12);
}

* { box-sizing: border-box; }

html, body { height: 100%; overflow: hidden; }

body {
    margin: 0;
    color: var(--ink);
    background: #f8fafc;
    font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button, input, textarea { font: inherit; }
button { color: inherit; }
.hidden { display: none !important; }

.auth-page {
    min-height: 100%;
    display: grid;
    grid-template-columns: minmax(0, 1.1fr) minmax(360px, .9fr);
    align-items: center;
    gap: 8vw;
    padding: clamp(32px, 8vw, 120px);
    background:
        radial-gradient(circle at 15% 18%, rgba(8, 102, 255, .18), transparent 28%),
        radial-gradient(circle at 78% 85%, rgba(139, 92, 246, .13), transparent 28%),
        #f8fafc;
}

.auth-brand { max-width: 620px; }
.brand-mark { width: 78px; color: var(--blue); filter: drop-shadow(0 14px 20px rgba(8, 102, 255, .22)); }
.brand-mark svg, .welcome-logo svg { display: block; width: 100%; }
.auth-brand h1 { margin: 28px 0 16px; font-size: clamp(42px, 5vw, 72px); line-height: 1.02; letter-spacing: -.055em; }
.auth-brand p { max-width: 510px; margin: 0; color: var(--muted); font-size: 20px; line-height: 1.6; }

.auth-card {
    width: 100%;
    max-width: 470px;
    justify-self: end;
    padding: 42px;
    border: 1px solid rgba(229, 231, 235, .8);
    border-radius: 28px;
    background: rgba(255, 255, 255, .9);
    box-shadow: var(--shadow);
    backdrop-filter: blur(16px);
}

.mobile-brand { display: none; color: var(--blue); font-weight: 800; }
.auth-card h2 { margin: 0; font-size: 30px; letter-spacing: -.03em; }
.auth-subtitle { margin: 10px 0 28px; color: var(--muted); line-height: 1.5; }
.auth-form { display: grid; gap: 18px; }
.form-field { display: grid; gap: 8px; }
.auth-form label { font-size: 13px; font-weight: 600; }
.auth-form input {
    width: 100%;
    padding: 14px 16px;
    border: 1px solid var(--line);
    border-radius: 12px;
    outline: 0;
    background: #fff;
    transition: border-color .2s, box-shadow .2s;
}
.auth-form input:focus { border-color: var(--blue); box-shadow: 0 0 0 4px rgba(8, 102, 255, .12); }
.password-field {
    position: relative;
    display: flex;
    align-items: center;
}
.password-field input {
    width: 100%;
    padding-right: 52px;
}
.password-toggle {
    width: 40px;
    height: 40px;
    position: absolute;
    top: 50%;
    right: 6px;
    z-index: 2;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 0;
    border-radius: 10px;
    color: var(--muted);
    background: transparent;
    cursor: pointer;
    transform: translateY(-50%);
    font-size: 18px;
    line-height: 1;
    padding: 0;
}
.password-toggle:hover { color: var(--ink); background: var(--soft); }
.password-toggle:focus-visible { outline: 2px solid var(--blue); outline-offset: -2px; }
.password-toggle svg { display: block; flex: 0 0 auto; }

.primary-button {
    border: 0;
    border-radius: 12px;
    padding: 14px 20px;
    color: #fff;
    background: var(--blue);
    font-weight: 700;
    cursor: pointer;
    transition: transform .15s, background .15s;
}
.primary-button:hover { background: var(--blue-dark); transform: translateY(-1px); }
.primary-button.compact { padding: 11px 18px; }
.auth-switch { margin: 24px 0 0; color: var(--muted); text-align: center; font-size: 14px; }
.auth-switch a { color: var(--blue); font-weight: 700; text-decoration: none; }
.alert { margin-bottom: 20px; padding: 12px 14px; border-radius: 10px; color: #b91c1c; background: #fef2f2; font-size: 14px; }

.app-shell { height: 100vh; height: 100dvh; min-height: 0; display: grid; grid-template-columns: 370px minmax(0, 1fr); background: var(--panel); overflow: hidden; }
.sidebar { min-width: 0; min-height: 0; display: grid; grid-template-rows: auto auto minmax(0, 1fr) auto; border-right: 1px solid var(--line); background: #fff; overflow: hidden; z-index: 2; }
.sidebar-header, .conversation-header, .modal header { display: flex; align-items: center; justify-content: space-between; }
.sidebar-header { padding: 24px 22px 14px; }
.eyebrow { margin: 0 0 3px; color: var(--blue); font-size: 11px; font-weight: 800; letter-spacing: .12em; text-transform: uppercase; }
.sidebar-header h1, .modal h2 { margin: 0; font-size: 28px; letter-spacing: -.04em; }
.header-actions { display: flex; gap: 8px; }
.header-actions form { margin: 0; }

.icon-button, .attach-button, .send-button {
    width: 40px;
    height: 40px;
    display: grid;
    place-items: center;
    flex: 0 0 auto;
    border: 0;
    border-radius: 50%;
    background: var(--soft);
    cursor: pointer;
}
.icon-button:hover, .attach-button:hover { background: #e5e7eb; }
.icon-button.notifications-on { color: #fff; background: var(--blue); }
.icon-button.notifications-on:hover { background: var(--blue-dark); }
.icon-button svg, .attach-button svg, .send-button svg, .search-box svg { width: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.search-box { margin: 0 18px 14px; position: relative; }
.search-box svg { width: 18px; position: absolute; left: 13px; top: 50%; color: #9ca3af; transform: translateY(-50%); }
.search-box input {
    width: 100%;
    height: 42px;
    padding: 0 14px 0 40px;
    border: 1px solid transparent;
    border-radius: 13px;
    outline: 0;
    background: var(--soft);
}
.search-box input:focus { border-color: rgba(8, 102, 255, .35); background: #fff; box-shadow: 0 0 0 3px rgba(8, 102, 255, .08); }

.contact-list { padding: 0 10px 18px; overflow-y: auto; }
.contact-card {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 13px;
    padding: 10px;
    border: 0;
    border-radius: 14px;
    background: transparent;
    text-align: left;
    cursor: pointer;
}
.contact-card:hover { background: #f7f7f8; }
.contact-card.active { background: #edf4ff; }
.avatar {
    width: 50px;
    height: 50px;
    display: grid;
    place-items: center;
    flex: 0 0 auto;
    border-radius: 50%;
    color: #fff;
    background: var(--avatar, var(--blue));
    font-size: 16px;
    font-weight: 700;
    letter-spacing: -.04em;
}
.avatar.small { width: 38px; height: 38px; font-size: 13px; }
.contact-copy { min-width: 0; display: grid; gap: 4px; }
.contact-copy strong { overflow: hidden; font-size: 14px; text-overflow: ellipsis; white-space: nowrap; }
.contact-copy small { overflow: hidden; color: var(--muted); font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }
.profile-bar { display: flex; align-items: center; gap: 11px; padding: 14px 20px; border-top: 1px solid var(--line); }
.profile-bar > span:last-child { min-width: 0; display: grid; gap: 2px; }
.profile-bar strong, .profile-bar small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.profile-bar strong { font-size: 13px; }
.profile-bar small { color: var(--muted); font-size: 11px; }

.empty-list { min-height: 280px; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 26px; color: var(--muted); text-align: center; }
.empty-list strong { margin: 12px 0 6px; color: var(--ink); font-size: 14px; }
.empty-list span { font-size: 12px; line-height: 1.5; }
.empty-icon { width: 46px; height: 46px; display: grid; place-items: center; border-radius: 50%; color: var(--blue); background: #edf4ff; font-size: 25px; }
.text-button { margin-top: 12px; border: 0; color: var(--blue); background: transparent; font-size: 13px; font-weight: 700; cursor: pointer; }

.chat-panel { min-width: 0; min-height: 0; position: relative; background: #fff; overflow: hidden; }
.welcome-state { height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 24px; text-align: center; }
.welcome-logo { width: 72px; color: var(--blue); }
.welcome-state h2 { margin: 22px 0 8px; font-size: 27px; letter-spacing: -.04em; }
.welcome-state p { margin: 0 0 22px; color: var(--muted); }
.conversation { height: 100%; min-height: 0; display: grid; grid-template-rows: auto minmax(0, 1fr) auto auto auto; overflow: hidden; }
.conversation-header { min-height: 72px; justify-content: flex-start; gap: 12px; padding: 10px 20px; border-bottom: 1px solid var(--line); box-shadow: 0 2px 12px rgba(17, 24, 39, .035); }
.conversation-header .avatar { width: 42px; height: 42px; font-size: 14px; }
.conversation-header > div { display: grid; gap: 3px; }
.conversation-header strong { font-size: 15px; }
.conversation-header small { color: var(--muted); font-size: 11px; }
.mobile-back { display: none; }

.messages { min-height: 0; display: flex; flex-direction: column; gap: 6px; padding: 24px clamp(18px, 4vw, 54px); overflow-y: auto; overscroll-behavior: contain; scroll-behavior: smooth; }
.message-row { max-width: min(68%, 620px); display: grid; gap: 5px; align-self: flex-start; position: relative; }
.message-row.mine { align-self: flex-end; justify-items: end; }
.message-main {
    display: flex;
    align-items: center;
    gap: 6px;
    max-width: 100%;
}
.message-row.mine .message-main { flex-direction: row-reverse; }
.message-stack {
    min-width: 0;
    display: grid;
    gap: 5px;
}
.message-row.mine .message-stack { justify-items: end; }
.message-actions {
    position: relative;
    display: flex;
    flex: 0 0 auto;
    align-self: center;
    gap: 2px;
}
.message-action {
    width: 32px;
    height: 32px;
    display: grid;
    place-items: center;
    border: 1px solid var(--line);
    border-radius: 50%;
    color: var(--muted);
    background: #fff;
    box-shadow: 0 1px 4px rgba(17, 24, 39, .12);
    cursor: pointer;
    pointer-events: auto;
    touch-action: manipulation;
}
.message-action:hover,
.message-action:focus-visible { color: var(--blue); background: #edf4ff; outline: 0; }
.message-action svg { width: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.reaction-picker {
    position: absolute;
    left: 0;
    bottom: calc(100% + 8px);
    display: flex;
    gap: 2px;
    padding: 7px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: #fff;
    box-shadow: var(--shadow);
    z-index: 8;
}
.message-row.mine .reaction-picker { right: 0; left: auto; }
.reaction-picker button {
    width: 34px;
    height: 34px;
    display: grid;
    place-items: center;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: transparent;
    font-size: 20px;
    cursor: pointer;
    transition: background .12s, transform .12s;
}
.reaction-picker button:hover,
.reaction-picker button:focus-visible {
    background: var(--soft);
    outline: 0;
    transform: scale(1.12);
}
.message-reactions {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    margin-top: -2px;
    padding: 0 5px;
}
.message-row.mine .message-reactions { justify-content: flex-end; }
.reaction-chip {
    min-width: 28px;
    height: 25px;
    padding: 1px 7px;
    border: 1px solid var(--line);
    border-radius: 999px;
    color: var(--ink);
    background: #fff;
    box-shadow: 0 2px 5px rgba(17, 24, 39, .12);
    font-size: 15px;
    line-height: 1;
    cursor: pointer;
}
.reaction-chip.mine {
    border-color: #93c5fd;
    background: #eff6ff;
}
.reply-header {
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 0 4px;
    color: var(--muted);
    font-size: 11px;
    font-weight: 500;
}
.reply-header svg { width: 14px; flex: 0 0 auto; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.reply-quote {
    max-width: 100%;
    padding: 8px 12px;
    border-radius: 14px;
    color: var(--ink);
    background: #e8eaed;
    font-size: 13px;
    line-height: 1.35;
    overflow-wrap: anywhere;
}
.mine .reply-quote { color: #1e3a5f; background: rgba(255, 255, 255, .72); }
.reply-quote-text { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.reply-preview {
    display: flex;
    align-items: center;
    gap: 10px;
    margin: 0 18px;
    padding: 10px 12px 10px 14px;
    border-left: 3px solid var(--blue);
    border-radius: 12px;
    background: var(--soft);
}
.reply-preview-copy { min-width: 0; display: grid; gap: 2px; flex: 1; }
.reply-preview-copy strong { color: var(--blue); font-size: 12px; }
.reply-preview-copy span {
    overflow: hidden;
    color: var(--muted);
    font-size: 12px;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.reply-cancel { width: 32px; height: 32px; flex: 0 0 auto; }
.bubble { padding: 10px 14px; border-radius: 18px 18px 18px 5px; background: var(--soft); line-height: 1.4; overflow-wrap: anywhere; white-space: pre-wrap; }
.mine .bubble { color: #fff; background: var(--blue); border-radius: 18px 18px 5px 18px; }
.message-time { padding: 0 4px; color: #9ca3af; font-size: 10px; }
.seen-status { padding: 0 4px; color: var(--blue); font-size: 10px; font-weight: 600; }
.conversation-header small.typing { color: var(--blue); font-weight: 600; }
.attachment-media-wrap {
    max-width: min(380px, 100%);
    display: flex;
    justify-self: start;
    justify-content: flex-start;
}
.message-row.mine .attachment-media-wrap {
    justify-self: end;
    justify-content: flex-end;
}
.attachment-media {
    display: block;
    width: auto;
    max-width: 100%;
    max-height: 420px;
    border-radius: 16px;
    object-fit: cover;
    background: #111;
}
.attachment-file { max-width: 340px; display: flex; align-items: center; gap: 12px; padding: 13px 15px; border: 1px solid var(--line); border-radius: 14px; color: var(--ink); background: #fff; text-decoration: none; }
.attachment-file:hover { border-color: #bfdbfe; background: #f8fbff; }
.file-icon { font-size: 24px; }
.attachment-file span { min-width: 0; display: grid; gap: 3px; }
.attachment-file strong { overflow: hidden; font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }
.attachment-file small { color: var(--muted); font-size: 10px; }
.message-row.mine .attachment-file { text-align: left; }

.composer { position: sticky; bottom: 0; z-index: 4; display: flex; align-items: flex-end; gap: 9px; padding: 12px 18px 17px; border-top: 1px solid var(--line); background: #fff; }
.composer textarea {
    width: 100%;
    min-height: 42px;
    max-height: 130px;
    padding: 11px 16px;
    border: 0;
    border-radius: 20px;
    outline: 0;
    resize: none;
    background: var(--soft);
    line-height: 20px;
}
.attach-button { color: var(--blue); }
.attachment-menu {
    position: absolute;
    left: 18px;
    bottom: calc(100% + 8px);
    min-width: 180px;
    display: grid;
    gap: 3px;
    padding: 8px;
    border: 1px solid var(--line);
    border-radius: 14px;
    background: #fff;
    box-shadow: var(--shadow);
    z-index: 5;
}
.attachment-menu button {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border: 0;
    border-radius: 9px;
    background: transparent;
    font-size: 13px;
    text-align: left;
    cursor: pointer;
}
.attachment-menu button:hover, .attachment-menu button:focus-visible { background: var(--soft); outline: 0; }
.attachment-menu span { width: 22px; font-size: 18px; text-align: center; }
.emoji-button { flex: 0 0 auto; }
.emoji-picker {
    position: absolute;
    left: 62px;
    bottom: calc(100% + 8px);
    width: min(320px, calc(100vw - 36px));
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 3px;
    padding: 10px;
    border: 1px solid var(--line);
    border-radius: 16px;
    background: #fff;
    box-shadow: var(--shadow);
    z-index: 5;
}
.emoji-picker button {
    width: 38px;
    height: 38px;
    border: 0;
    border-radius: 9px;
    background: transparent;
    font-size: 22px;
    cursor: pointer;
}
.emoji-picker button:hover, .emoji-picker button:focus-visible { background: var(--soft); outline: 0; }
.send-button { color: #fff; background: var(--blue); }
.send-button:hover { background: var(--blue-dark); }
.upload-preview { margin: 0 18px; padding: 10px 14px; border-radius: 12px; color: var(--muted); background: var(--soft); font-size: 12px; }
.upload-preview strong { color: var(--ink); }

.modal-backdrop { position: fixed; inset: 0; display: grid; place-items: center; padding: 20px; z-index: 20; background: rgba(17, 24, 39, .48); backdrop-filter: blur(5px); }
.modal { width: min(520px, 100%); max-height: min(680px, 90vh); display: grid; grid-template-rows: auto auto minmax(180px, 1fr); border-radius: 22px; background: #fff; box-shadow: var(--shadow); overflow: hidden; }
.camera-modal {
    width: min(640px, 100%);
    max-height: min(780px, 92vh);
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
    border-radius: 22px;
    background: #0f172a;
    color: #fff;
    box-shadow: var(--shadow);
    overflow: hidden;
}
.camera-modal header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 18px 18px 12px;
}
.camera-modal .eyebrow { color: #93c5fd; }
.camera-modal h2 { margin: 0; font-size: 22px; letter-spacing: -.03em; }
.camera-modal .icon-button { color: #fff; background: rgba(255, 255, 255, .1); }
.camera-modal .icon-button:hover { background: rgba(255, 255, 255, .18); }
.camera-stage {
    position: relative;
    min-height: 280px;
    margin: 0 16px;
    border-radius: 18px;
    background: #020617;
    overflow: hidden;
}
.camera-stage video,
.camera-stage img,
.camera-stage canvas {
    display: block;
    width: 100%;
    height: min(52vh, 420px);
    object-fit: cover;
    background: #020617;
}
.camera-error {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    margin: 0;
    padding: 24px;
    color: #e2e8f0;
    text-align: center;
    line-height: 1.5;
    background: rgba(2, 6, 23, .92);
}
.camera-rec-badge {
    position: absolute;
    top: 14px;
    left: 14px;
    padding: 6px 10px;
    border-radius: 999px;
    color: #fff;
    background: #dc2626;
    font-size: 11px;
    font-weight: 800;
    letter-spacing: .08em;
}
.camera-actions {
    display: flex;
    justify-content: center;
    gap: 10px;
    padding: 16px 18px 20px;
}
.secondary-button {
    border: 0;
    border-radius: 12px;
    padding: 11px 18px;
    color: #fff;
    background: rgba(255, 255, 255, .14);
    font-weight: 700;
    cursor: pointer;
}
.secondary-button:hover { background: rgba(255, 255, 255, .22); }
#camera-shutter.recording { background: #dc2626; }
#camera-shutter.recording:hover { background: #b91c1c; }
.modal header { padding: 22px 22px 17px; }
.modal .search-box { margin-bottom: 15px; }
.people-results { min-height: 250px; padding: 0 12px 18px; overflow-y: auto; }
.people-results > p { padding: 46px 20px; color: var(--muted); text-align: center; font-size: 13px; }
.person-row { display: flex; align-items: center; gap: 12px; padding: 10px; border-radius: 13px; }
.person-row:hover { background: #f8fafc; }
.person-row > span:nth-child(2) { min-width: 0; display: grid; flex: 1; gap: 3px; }
.person-row strong, .person-row small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.person-row strong { font-size: 14px; }
.person-row small { color: var(--muted); font-size: 11px; }
.add-button { border: 0; border-radius: 9px; padding: 8px 12px; color: var(--blue); background: #edf4ff; font-size: 12px; font-weight: 700; cursor: pointer; }
.add-button:hover { background: #dceaff; }

.toast { position: fixed; right: 24px; bottom: 24px; max-width: 360px; padding: 13px 18px; border-radius: 12px; color: #fff; background: #111827; box-shadow: 0 12px 30px rgba(17, 24, 39, .22); z-index: 30; font-size: 13px; }

@media (max-width: 800px) {
    .auth-page { grid-template-columns: 1fr; padding: 28px 20px; }
    .auth-brand { display: none; }
    .auth-card { max-width: 460px; justify-self: center; padding: 30px 24px; }
    .mobile-brand { display: block; margin-bottom: 30px; }
    .app-shell { display: block; position: relative; }
    .sidebar, .chat-panel { position: absolute; inset: 0; }
    .sidebar { border: 0; }
    .chat-panel { display: none; z-index: 5; }
    .app-shell.chat-open .sidebar { display: none; }
    .app-shell.chat-open .chat-panel { display: block; }
    .mobile-back { display: grid; }
    .profile-bar { padding-bottom: max(14px, env(safe-area-inset-bottom)); }
    .composer { padding-bottom: max(17px, env(safe-area-inset-bottom)); }
    .message-row { max-width: 88%; }
}

@media (max-width: 430px) {
    .auth-card { padding: 26px 20px; border-radius: 20px; }
    .modal-backdrop { align-items: end; padding: 0; }
    .modal { width: 100%; max-height: 88vh; border-radius: 22px 22px 0 0; }
}
