* { box-sizing: border-box; }
html, body { margin: 0; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: #f1f5f9;
    color: #0f172a;
}

/* ---------- Login / register / reset / profile pages ---------- */
.auth-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 16px;
}
.auth-box {
    background: #fff;
    padding: 32px;
    border-radius: 12px;
    box-shadow: 0 2px 12px rgba(0,0,0,0.08);
    width: 100%;
    max-width: 340px;
}
.auth-box h1 { font-size: 20px; margin: 0 0 12px; }
.auth-box label { display: block; margin-bottom: 14px; font-size: 13px; color: #555; }
.auth-box input {
    width: 100%;
    padding: 12px;
    margin-top: 4px;
    border: 1px solid #ccc;
    border-radius: 8px;
    font-size: 16px;
}
.auth-box button {
    width: 100%;
    padding: 12px;
    background: #2563eb;
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 16px;
    cursor: pointer;
}
.auth-box button:hover { background: #1d4ed8; }
.error { color: #b91c1c; font-size: 13px; }
.success { color: #15803d; font-size: 13px; }
.hint { font-size: 12px; color: #777; }

/* ---------- Chat app layout ---------- */
.app-body { height: 100vh; height: 100dvh; overflow: hidden; }
.app { display: flex; height: 100%; background: #fff; }

.avatar {
    width: 44px; height: 44px; border-radius: 50%;
    color: #fff; font-weight: 700; font-size: 18px;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0; overflow: hidden;
}
.avatar-img { width: 100%; height: 100%; border-radius: 50%; object-fit: cover; }

/* Left pane: people list */
.list-pane {
    width: 340px; flex-shrink: 0;
    display: flex; flex-direction: column;
    border-right: 1px solid #e2e8f0; background: #fff;
}
.list-top {
    display: flex; align-items: center; justify-content: space-between;
    padding: 12px 16px; padding-top: calc(12px + env(safe-area-inset-top));
    background: #1e293b; color: #fff;
}
.me { display: flex; align-items: center; gap: 10px; text-decoration: none; color: inherit; min-width: 0; }
.me .avatar { width: 38px; height: 38px; font-size: 16px; }
.me-text { display: flex; flex-direction: column; line-height: 1.25; min-width: 0; }
.me-text span { font-size: 11px; opacity: 0.7; }
.list-top-actions { display: flex; align-items: center; gap: 4px; }
.logout { color: #93c5fd; text-decoration: none; font-size: 14px; padding: 8px; }
.theme-toggle:hover { background: rgba(255,255,255,0.12); }

.search { padding: 10px 12px; border-bottom: 1px solid #e2e8f0; }
.search input {
    width: 100%; padding: 10px 14px;
    border: 1px solid #e2e8f0; border-radius: 999px;
    background: #f8fafc; font-size: 16px; outline: none; color: inherit;
}
.search input:focus { border-color: #2563eb; background: #fff; }

.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; -webkit-overflow-scrolling: touch; }
.conv {
    display: flex; align-items: center; gap: 12px;
    padding: 12px 16px; cursor: pointer;
    border-bottom: 1px solid #f1f5f9;
    -webkit-tap-highlight-color: transparent;
}
.conv:hover { background: #f8fafc; }
.conv.active { background: #dbeafe; }
.conv-mid { flex: 1; min-width: 0; }
.conv-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-last { font-size: 13px; color: #64748b; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv.unread .conv-last { color: #0f172a; font-weight: 600; }
.conv-right { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; min-width: 44px; }
.conv-time { font-size: 11px; color: #94a3b8; }
.conv.unread .conv-time { color: #2563eb; }
.badge {
    background: #2563eb; color: #fff; font-size: 12px; font-weight: 700;
    min-width: 22px; height: 22px; padding: 0 6px; border-radius: 11px;
    display: inline-flex; align-items: center; justify-content: center;
}
.no-results { padding: 24px; text-align: center; color: #94a3b8; }

/* Right pane: conversation */
.chat-pane { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; background: #e8edf3; }
.chat-top {
    display: flex; align-items: center; gap: 12px;
    padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top));
    background: #fff; border-bottom: 1px solid #e2e8f0;
}
.chat-top .avatar { width: 38px; height: 38px; font-size: 16px; }
.chat-top-text { display: flex; flex-direction: column; line-height: 1.25; min-width: 0; }
.chat-top-text span { font-size: 12px; color: #64748b; }
.back {
    display: none; border: none; background: none;
    font-size: 34px; line-height: 1; color: #2563eb;
    padding: 0 6px 4px 0; cursor: pointer;
}

.messages {
    flex: 1; min-height: 0; overflow-y: auto; padding: 16px;
    display: flex; flex-direction: column; gap: 6px;
    -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
}
.msg { display: flex; flex-shrink: 0; }
.msg.new { animation: fadein .15s ease; }
@keyframes fadein { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: translateY(0); } }
.msg.mine { justify-content: flex-end; }
.bubble {
    max-width: 75%; padding: 8px 12px; border-radius: 16px;
    box-shadow: 0 1px 1px rgba(0,0,0,0.06);
    -webkit-tap-highlight-color: transparent;
}
.msg.theirs .bubble { background: #fff; border-bottom-left-radius: 4px; }
.msg.mine .bubble { background: #2563eb; color: #fff; border-bottom-right-radius: 4px; }
.msg-user { font-size: 12px; font-weight: 700; margin-bottom: 2px; }
.msg-body { white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-body.deleted { font-style: italic; opacity: 0.6; }
.msg-forwarded { font-size: 11px; font-style: italic; opacity: 0.7; margin-bottom: 2px; }
.msg-image { max-width: 220px; max-height: 280px; border-radius: 10px; display: block; min-width: 100px; min-height: 70px; background: rgba(0,0,0,0.08); }
.msg-meta { display: flex; align-items: center; gap: 4px; justify-content: flex-end; margin-top: 2px; }
.msg-time { font-size: 11px; opacity: 0.6; }
.msg-seen { color: rgba(255,255,255,0.6); display: inline-flex; }
.msg-seen.seen { color: #4ade80; }

/* Reply / Forward / Delete: appear on hover (desktop) or when a message is tapped (phone) */
.msg-actions { display: none; gap: 12px; margin-top: 4px; }
.msg:hover .msg-actions, .msg.show-actions .msg-actions { display: flex; }
.msg-actions button {
    border: none; background: none; padding: 2px 0; font-size: 12px; cursor: pointer;
    color: inherit; text-decoration: underline; opacity: 0.8;
}

.quote { border-left: 3px solid rgba(0,0,0,0.2); padding: 4px 8px; margin-bottom: 4px; border-radius: 4px; background: rgba(0,0,0,0.05); font-size: 12px; }
.quote-user { font-weight: 700; }
.quote-preview { overflow-wrap: anywhere; }
.msg.mine .quote { background: rgba(255,255,255,0.15); border-left-color: rgba(255,255,255,0.5); }

.sticker-bubble { background: none !important; box-shadow: none !important; padding: 0 !important; }
.sticker-emoji { font-size: 64px; line-height: 1; }

/* Sending / failed states */
.msg.pending .bubble { opacity: 0.65; }
.msg.failed .bubble { background: #fee2e2 !important; color: #991b1b !important; cursor: pointer; }
.empty-hint, .retry-box { margin: auto; text-align: center; color: #64748b; font-size: 14px; }
.retry-box button {
    margin-top: 10px; padding: 8px 18px; border: none; border-radius: 999px;
    background: #2563eb; color: #fff; cursor: pointer; font-size: 14px;
}

.empty { flex: 1; display: flex; align-items: center; justify-content: center; color: #64748b; }
.app.has-chat .empty { display: none; }
.app:not(.has-chat) .chat-top,
.app:not(.has-chat) .messages,
.app:not(.has-chat) .send-form { display: none; }

/* Composer */
.send-form {
    display: flex; align-items: center; gap: 6px;
    padding: 10px 12px; padding-bottom: calc(10px + env(safe-area-inset-bottom));
    background: #fff; border-top: 1px solid #e2e8f0;
}
.send-form input[type="text"] {
    flex: 1; min-width: 0; padding: 12px 16px;
    border: 1px solid #e2e8f0; border-radius: 999px;
    background: #f8fafc; font-size: 16px; outline: none; color: inherit;
}
.send-form input[type="text"]:focus { border-color: #2563eb; background: #fff; }
.send-btn {
    width: 46px; height: 46px; flex-shrink: 0;
    border: none; border-radius: 50%; background: #2563eb; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
}
.send-btn:hover { background: #1d4ed8; }
.send-btn svg { width: 20px; height: 20px; fill: #fff; }

.icon-btn { width: 40px; height: 40px; flex-shrink: 0; border: none; background: none; font-size: 20px; cursor: pointer; border-radius: 50%; }
.icon-btn:hover { background: #f1f5f9; }

.emoji-panel {
    display: none; grid-template-columns: repeat(8, 1fr); gap: 4px;
    max-height: 160px; overflow-y: auto;
    padding: 8px; background: #fff; border-top: 1px solid #e2e8f0;
}
.emoji-panel.open { display: grid; }
.emoji-panel button { border: none; background: none; font-size: 22px; cursor: pointer; padding: 4px; }
.emoji-panel button:hover { background: #f1f5f9; border-radius: 6px; }
.sticker-panel button { font-size: 32px; }

.reply-bar {
    display: none; align-items: center; justify-content: space-between;
    padding: 8px 14px; background: #eef2ff; border-top: 1px solid #e2e8f0; font-size: 13px;
}
.reply-bar.show { display: flex; }
.reply-bar-text { display: flex; flex-direction: column; overflow: hidden; }
.reply-bar-text span { color: #64748b; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 240px; }
.reply-bar button { border: none; background: none; font-size: 20px; cursor: pointer; color: #64748b; }

/* Forward picker */
.modal-overlay {
    display: none; position: fixed; top: 0; right: 0; bottom: 0; left: 0;
    background: rgba(0,0,0,0.4); align-items: center; justify-content: center; z-index: 50;
}
.modal-overlay.open { display: flex; }
.modal-box { background: #fff; border-radius: 12px; width: 280px; max-height: 70vh; overflow: hidden; display: flex; flex-direction: column; }
.modal-title { padding: 14px 16px; font-weight: 700; border-bottom: 1px solid #e2e8f0; }
.modal-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; }
.modal-list li { padding: 12px 16px; cursor: pointer; border-bottom: 1px solid #f1f5f9; }
.modal-list li:hover { background: #f8fafc; }
.modal-cancel { padding: 12px; border: none; background: none; color: #2563eb; cursor: pointer; font-weight: 600; }

/* Delete all chats (owner only) */
.clear-box { width: 320px; max-width: 92vw; }
.clear-text { margin: 0; padding: 12px 16px 0; font-size: 14px; line-height: 1.45; color: #475569; }
.clear-box input { margin: 12px 16px; padding: 10px 12px; font-size: 16px; border: 1px solid #cbd5e1; border-radius: 8px; color: inherit; background: #fff; }
.clear-go { margin: 0 16px 6px; padding: 12px; border: none; border-radius: 8px; background: #dc2626; color: #fff; font-size: 15px; font-weight: 600; cursor: pointer; }
.clear-go:hover:not(:disabled) { background: #b91c1c; }
.clear-go:disabled { opacity: 0.4; cursor: not-allowed; }

/* Editing */
.msg-edited { font-size: 11px; font-style: italic; opacity: 0.6; }
.edit-bar { background: #fef3c7; }
.send-form.editing .send-btn { background: #16a34a; }
.send-form.editing .send-btn:hover { background: #15803d; }

/* Connection banner and toast */
.conn-banner {
    display: none; position: fixed; top: calc(8px + env(safe-area-inset-top)); left: 50%; transform: translateX(-50%);
    background: #f59e0b; color: #1c1917; font-size: 13px; font-weight: 600;
    padding: 6px 14px; border-radius: 999px; z-index: 60; box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}
.conn-banner.show { display: block; }
.toast {
    position: fixed; bottom: 90px; left: 50%; transform: translateX(-50%) translateY(10px);
    background: #0f172a; color: #fff; font-size: 14px; padding: 10px 16px; border-radius: 10px;
    opacity: 0; pointer-events: none; transition: opacity .2s ease, transform .2s ease; z-index: 70; max-width: 90%;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- Phones: list and conversation are separate screens ---------- */
@media (max-width: 768px) {
    .list-pane { width: 100%; border-right: none; }
    .app.has-chat .list-pane { display: none; }
    .app:not(.has-chat) .chat-pane { display: none; }
    .back { display: block; }
    .bubble { max-width: 85%; }
}

/* ---------- Dark mode ---------- */
body, .list-pane, .chat-pane, .conv, .search input, .send-form, .send-form input, .modal-box, .auth-box {
    transition: background-color .2s ease, color .2s ease, border-color .2s ease;
}
html[data-theme="dark"] body { background: #0b1220; color: #e2e8f0; }
html[data-theme="dark"] .app { background: #0b1220; }
html[data-theme="dark"] .list-pane { background: #111827; border-right-color: #1f2937; }
html[data-theme="dark"] .search { border-bottom-color: #1f2937; }
html[data-theme="dark"] .search input { background: #1f2937; border-color: #334155; color: #e2e8f0; }
html[data-theme="dark"] .conv { border-bottom-color: #1f2937; }
html[data-theme="dark"] .conv:hover { background: #1f2937; }
html[data-theme="dark"] .conv.active { background: #1e3a8a; }
html[data-theme="dark"] .conv-last { color: #94a3b8; }
html[data-theme="dark"] .conv.unread .conv-last { color: #e2e8f0; }
html[data-theme="dark"] .no-results { color: #64748b; }
html[data-theme="dark"] .chat-pane { background: #0b1220; }
html[data-theme="dark"] .chat-top { background: #111827; border-bottom-color: #1f2937; }
html[data-theme="dark"] .chat-top-text span { color: #94a3b8; }
html[data-theme="dark"] .empty, html[data-theme="dark"] .empty-hint, html[data-theme="dark"] .retry-box { color: #64748b; }
html[data-theme="dark"] .msg.theirs .bubble { background: #1f2937; color: #e2e8f0; }
html[data-theme="dark"] .msg.theirs .quote { background: rgba(255,255,255,0.06); border-left-color: rgba(255,255,255,0.3); }
html[data-theme="dark"] .msg.failed .bubble { background: #7f1d1d !important; color: #fecaca !important; }
html[data-theme="dark"] .send-form { background: #111827; border-top-color: #1f2937; }
html[data-theme="dark"] .send-form input[type="text"] { background: #1f2937; border-color: #334155; color: #e2e8f0; }
html[data-theme="dark"] .icon-btn:hover { background: #1f2937; }
html[data-theme="dark"] .emoji-panel { background: #111827; border-top-color: #1f2937; }
html[data-theme="dark"] .emoji-panel button:hover { background: #1f2937; }
html[data-theme="dark"] .reply-bar { background: #1e293b; border-top-color: #1f2937; }
html[data-theme="dark"] .edit-bar { background: #3b3417; }
html[data-theme="dark"] .modal-box { background: #111827; }
html[data-theme="dark"] .modal-title { border-bottom-color: #1f2937; }
html[data-theme="dark"] .modal-list li { border-bottom-color: #1f2937; }
html[data-theme="dark"] .modal-list li:hover { background: #1f2937; }
html[data-theme="dark"] .auth-page { background: #0b1220; }
html[data-theme="dark"] .auth-box { background: #111827; color: #e2e8f0; }
html[data-theme="dark"] .auth-box input { background: #1f2937; border-color: #334155; color: #e2e8f0; }
html[data-theme="dark"] .hint { color: #94a3b8; }
html[data-theme="dark"] .clear-text { color: #94a3b8; }
html[data-theme="dark"] .clear-box input { background: #1f2937; border-color: #334155; color: #e2e8f0; }
