/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --sidebar-w: 380px;
    --bg-sidebar: #111318;
    --bg-chat: #191b22;
    --bg-bubble-roy: #2a2d35;
    --bg-bubble-user: #1a73e8;
    --bg-input: #1e2028;
    --text-primary: #e4e4e7;
    --text-secondary: #909098;
    --text-bubble-roy: #e4e4e7;
    --text-bubble-user: #fff;
    --border: #252730;
    --accent: #1a73e8;
    --danger: #e74c3c;
    --online: #4caf50;
    --hover: rgba(255,255,255,0.04);
    --active: rgba(255,255,255,0.08);
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
}

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

body {
    background: #0d0e12;
    color: var(--text-primary);
    display: flex;
    justify-content: center;
    align-items: center;
}

.app {
    display: flex;
    width: 100vw;
    height: 100vh;
    max-width: 1400px;
    max-height: 100vh;
    background: var(--bg-chat);
    box-shadow: 0 0 80px rgba(0,0,0,0.5);
}

/* === Sidebar === */
.sidebar {
    width: var(--sidebar-w);
    min-width: var(--sidebar-w);
    background: var(--bg-sidebar);
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--border);
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 18px;
    border-bottom: 1px solid var(--border);
}

.sidebar-header h1 {
    font-size: 1.25rem;
    font-weight: 600;
    letter-spacing: -0.01em;
}

.btn-icon {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 1.2rem;
    cursor: pointer;
    padding: 6px 8px;
    border-radius: 8px;
    transition: background .15s, color .15s;
}
.btn-icon:hover { background: var(--hover); color: var(--text-primary); }

.contact-list {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
}

.contact-list::-webkit-scrollbar { width: 4px; }
.contact-list::-webkit-scrollbar-thumb { background: #333; border-radius: 4px; }

.contact-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 18px;
    cursor: pointer;
    transition: background .12s;
    border-bottom: 1px solid rgba(255,255,255,0.03);
}
.contact-item:hover { background: var(--hover); }
.contact-item.active { background: var(--active); }

.contact-pfp {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
}

.contact-initials {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 1.1rem;
    flex-shrink: 0;
    color: #fff;
}

.contact-info {
    flex: 1;
    min-width: 0;
}

.contact-name {
    display: block;
    font-weight: 500;
    font-size: 0.95rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.contact-preview {
    display: block;
    font-size: 0.8rem;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 2px;
}

.contact-meta {
    font-size: 0.7rem;
    color: var(--text-secondary);
    flex-shrink: 0;
    align-self: flex-start;
    margin-top: 4px;
}

/* === Chat Area === */
.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    position: relative;
}

.chat-placeholder {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.placeholder-content {
    text-align: center;
    color: var(--text-secondary);
}
.placeholder-icon { font-size: 4rem; margin-bottom: 12px; }
.placeholder-content h2 { font-size: 1.3rem; color: var(--text-primary); margin-bottom: 6px; }
.placeholder-content p { font-size: 0.9rem; line-height: 1.5; }

.chat-view { display: flex; flex-direction: column; height: 100%; }
.chat-view.hidden, .chat-placeholder.hidden { display: none; }

/* Chat Header */
.chat-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 18px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-sidebar);
}

.chat-header-pfp {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    object-fit: cover;
}

.chat-header-initials {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 0.95rem;
    color: #fff;
}

.chat-header-info { flex: 1; }
.chat-header-name { display: block; font-weight: 600; font-size: 0.95rem; }
.chat-header-status { display: block; font-size: 0.75rem; color: var(--text-secondary); }

/* Messages */
.messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px 20px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.messages::-webkit-scrollbar { width: 4px; }
.messages::-webkit-scrollbar-thumb { background: #333; border-radius: 4px; }

/* Empty-chat hint */
.empty-chat-hint {
    align-self: center;
    color: var(--text-secondary);
    font-size: 0.85rem;
    font-style: italic;
    margin-top: 40px;
}

/* Bubbles */
.msg-row {
    display: flex;
    flex-direction: column;
    max-width: 70%;
    animation: msgIn .25s ease-out;
}

@keyframes msgIn {
    from { opacity: 0; transform: translateY(8px); }
    to   { opacity: 1; transform: translateY(0); }
}

.msg-row.user { align-self: flex-end; }
.msg-row.character  { align-self: flex-start; }

.msg-bubble {
    padding: 10px 14px;
    border-radius: 18px;
    font-size: 0.92rem;
    line-height: 1.45;
    word-wrap: break-word;
    white-space: pre-wrap;
}

.msg-row.user .msg-bubble {
    background: var(--bg-bubble-user);
    color: var(--text-bubble-user);
    border-bottom-right-radius: 6px;
}

.msg-row.character .msg-bubble {
    background: var(--bg-bubble-roy);
    color: var(--text-bubble-roy);
    border-bottom-left-radius: 6px;
}

.msg-time {
    font-size: 0.68rem;
    color: var(--text-secondary);
    margin-top: 3px;
}
.msg-row.user .msg-time { text-align: right; }

/* Typing indicator */
.typing-indicator {
    align-self: flex-start;
    padding: 12px 16px;
    background: var(--bg-bubble-roy);
    border-radius: 18px;
    border-bottom-left-radius: 6px;
    display: flex;
    gap: 4px;
}
.typing-indicator span {
    width: 7px; height: 7px;
    background: var(--text-secondary);
    border-radius: 50%;
    animation: typeBounce 1.2s infinite;
}
.typing-indicator span:nth-child(2) { animation-delay: .15s; }
.typing-indicator span:nth-child(3) { animation-delay: .3s; }

@keyframes typeBounce {
    0%, 60%, 100% { transform: translateY(0); opacity: .4; }
    30% { transform: translateY(-6px); opacity: 1; }
}

/* Input Bar */
.input-bar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 18px;
    border-top: 1px solid var(--border);
    background: var(--bg-input);
}

.input-bar input {
    flex: 1;
    padding: 10px 16px;
    border-radius: 24px;
    border: 1px solid var(--border);
    background: var(--bg-sidebar);
    color: var(--text-primary);
    font-size: 0.92rem;
    outline: none;
    transition: border-color .15s;
}
.input-bar input::placeholder { color: var(--text-secondary); }
.input-bar input:focus { border-color: var(--accent); }

.btn-send {
    background: var(--accent);
    color: #fff;
    border: none;
    padding: 10px 20px;
    border-radius: 24px;
    font-weight: 600;
    font-size: 0.9rem;
    cursor: pointer;
    transition: background .15s, transform .1s;
}
.btn-send:hover { background: #1669d3; }
.btn-send:active { transform: scale(0.96); }

/* Annoyance level badge (Roy's header) */
.annoyance-meter {
    font-size: 0.7rem;
    padding: 2px 8px;
    border-radius: 10px;
    margin-left: 6px;
    font-weight: 600;
}
.annoyance-calm { background: #1b5e20; color: #81c784; }
.annoyance-irked { background: #5d4e00; color: #ffe082; }
.annoyance-annoyed { background: #6d2800; color: #ffb74d; }
.annoyance-furious { background: #7f0000; color: #ef9a9a; }

/* PFP upload button in contact items */
.pfp-wrapper {
    position: relative;
    flex-shrink: 0;
}

.pfp-upload-label {
    position: absolute;
    inset: 0;
    border-radius: 50%;
    cursor: pointer;
    opacity: 0;
    transition: opacity .15s;
    background: rgba(0,0,0,0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 0.65rem;
    font-weight: 600;
    text-align: center;
    line-height: 1.1;
}
.pfp-wrapper:hover .pfp-upload-label { opacity: 1; }
.pfp-upload-label input { display: none; }

/* Responsive */
@media (max-width: 700px) {
    .app { flex-direction: column; }
    .sidebar { width: 100%; min-width: 100%; max-height: 40vh; }
    .chat-main { height: 60vh; }
    .msg-row { max-width: 85%; }
}
