/* Haven — Hearth Theme
   Warm, intimate, private. Like firelight in a study. */

@import url('https://fonts.googleapis.com/css2?family=Cormorant+Garamond:ital,wght@0,400;0,600;1,400&family=DM+Sans:ital,wght@0,400;0,500;0,600&display=swap');

:root {
    --bg-deep: #121014;
    --bg-panel: #1a181e;
    --bg-surface: #221f27;
    --bg-hover: #2a2730;
    --bg-active: rgba(212, 165, 116, 0.08);
    --border: #2e2a34;
    --border-subtle: #252230;
    --text-primary: #e8e0d4;
    --text-secondary: #9e9494;
    --text-muted: #6b6065;
    --accent: #d4a574;
    --accent-soft: rgba(212, 165, 116, 0.15);
    --accent-glow: rgba(212, 165, 116, 0.06);
    --green: #7ec49e;
    --red: #c47e7e;
    --blue: #7ea8c4;
    --font-display: 'Cormorant Garamond', Georgia, serif;
    --font-body: 'DM Sans', -apple-system, sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }

body {
    font-family: var(--font-body);
    background: var(--bg-deep);
    color: var(--text-primary);
    -webkit-font-smoothing: antialiased;
}

/* Subtle noise texture overlay */
body::before {
    content: '';
    position: fixed;
    inset: 0;
    background: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.015'/%3E%3C/svg%3E");
    pointer-events: none;
    z-index: 9999;
}

/* Scrollbar */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* === LOGIN === */

#login-screen {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    background: radial-gradient(ellipse at 50% 40%, #1f1a24 0%, var(--bg-deep) 70%);
}

.login-card {
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 3rem 2.5rem;
    max-width: 380px;
    width: 100%;
    margin: 1rem;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4), 0 0 80px var(--accent-glow);
    animation: fadeIn 0.6s ease-out;
}

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

.login-title {
    font-family: var(--font-display);
    font-size: 2.4rem;
    font-weight: 600;
    color: var(--accent);
    text-align: center;
    letter-spacing: 0.04em;
    margin-bottom: 0.25rem;
}

.login-subtitle {
    font-size: 0.85rem;
    color: var(--text-muted);
    text-align: center;
    font-style: italic;
    font-family: var(--font-display);
    margin-bottom: 2rem;
}

.login-input {
    width: 100%;
    padding: 0.65rem 1rem;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text-primary);
    font-family: var(--font-body);
    font-size: 0.9rem;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
    margin-bottom: 0.75rem;
}

.login-input::placeholder { color: var(--text-muted); }
.login-input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}

.login-btn {
    width: 100%;
    padding: 0.7rem;
    background: var(--accent);
    color: var(--bg-deep);
    border: none;
    border-radius: 8px;
    font-family: var(--font-body);
    font-weight: 600;
    font-size: 0.9rem;
    cursor: pointer;
    transition: opacity 0.2s, transform 0.1s;
    margin-top: 0.25rem;
}
.login-btn:hover { opacity: 0.9; }
.login-btn:active { transform: scale(0.98); }
.login-btn:disabled { opacity: 0.5; cursor: default; }

.login-error {
    color: var(--red);
    font-size: 0.8rem;
    text-align: center;
    margin-top: 0.75rem;
}

/* Google button */
.google-btn {
    width: 100%;
    padding: 0.65rem;
    background: #fff;
    color: #333;
    border: none;
    border-radius: 8px;
    font-weight: 500;
    font-size: 0.9rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    transition: background 0.2s;
}
.google-btn:hover { background: #f0f0f0; }

.login-divider {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin: 1.25rem 0;
}
.login-divider::before, .login-divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--border);
}
.login-divider span { color: var(--text-muted); font-size: 0.8rem; }

/* === CHAT LAYOUT === */

#chat-app {
    height: 100%;
    display: flex;
}

/* === SIDEBAR === */

#sidebar {
    width: 220px;
    background: var(--bg-panel);
    border-right: 1px solid var(--border-subtle);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

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

.sidebar-title {
    font-family: var(--font-display);
    font-size: 1.4rem;
    font-weight: 600;
    color: var(--accent);
    letter-spacing: 0.03em;
}

.sidebar-section-label {
    font-size: 0.65rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-muted);
    padding: 0 0.75rem;
    margin-bottom: 0.25rem;
    margin-top: 1rem;
}

.sidebar-content {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem;
}

/* Room items */
.room-item {
    display: flex;
    align-items: center;
    padding: 6px 10px;
    border-radius: 6px;
    cursor: pointer;
    color: var(--text-secondary);
    font-size: 0.85rem;
    transition: all 0.15s;
}
.room-item:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}
.room-item.active {
    background: var(--bg-active);
    color: var(--accent);
}
.room-item.has-unread {
    color: var(--text-primary);
    font-weight: 500;
}

.unread-badge {
    background: var(--accent);
    color: var(--bg-deep);
    font-size: 0.65rem;
    font-weight: 600;
    min-width: 18px;
    height: 18px;
    border-radius: 9px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 5px;
    flex-shrink: 0;
    margin-left: auto;
}

/* Sidebar footer */
.sidebar-footer {
    padding: 0.6rem 1rem;
    border-top: 1px solid var(--border-subtle);
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.85rem;
}

.sidebar-footer .status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
    background: var(--green);
}

.admin-link {
    margin-left: auto;
    font-size: 0.7rem;
    color: var(--text-muted);
    text-decoration: none;
    opacity: 0.6;
    transition: opacity 0.2s;
}
.admin-link:hover { opacity: 1; color: var(--accent); }

/* Push notifications opt-in button */
.push-notify-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    margin-left: auto;
    padding: 0.25rem 0.5rem;
    font-size: 0.7rem;
    color: var(--text-muted);
    background: transparent;
    border: 1px solid var(--border-subtle);
    border-radius: 4px;
    cursor: pointer;
    opacity: 0.6;
    transition: opacity 0.2s, color 0.2s;
    white-space: nowrap;
}
.push-notify-btn:hover:not(:disabled) { opacity: 1; color: var(--accent); }
.push-notify-btn:disabled {
    opacity: 0.35;
    cursor: default;
}

/* === MAIN CHAT === */

.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    background: var(--bg-deep);
}

.chat-header {
    padding: 0.75rem 1.25rem;
    border-bottom: 1px solid var(--border-subtle);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
    background: var(--bg-panel);
}

.room-name {
    font-weight: 600;
    font-size: 0.95rem;
}

/* Header typing indicator — hidden, we use inline instead */
.typing-indicator { display: none; }

/* Inline typing indicator — appears in message list like Discord */
.typing-row {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.35rem 0.5rem;
    animation: typingFadeIn 0.2s ease-out;
}

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

.typing-row .msg-author {
    font-weight: 500;
    font-size: 0.85rem;
    color: var(--accent);
}

.typing-dots {
    display: flex;
    gap: 3px;
    align-items: center;
    padding-top: 2px;
}

.typing-dots span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--accent);
    opacity: 0.4;
    animation: typingBounce 1.4s ease-in-out infinite;
}
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }

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

.header-actions {
    display: flex;
    gap: 0.25rem;
}
.header-actions button {
    padding: 6px;
    color: var(--text-muted);
    background: none;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    transition: color 0.2s, background 0.2s;
}
.header-actions button:hover {
    color: var(--text-primary);
    background: var(--bg-hover);
}

/* === MESSAGES === */

#messages {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem 1rem;
    display: flex;
    flex-direction: column;
}

#load-more {
    text-align: center;
    padding: 0.5rem;
}
#load-more button {
    font-size: 0.8rem;
    color: var(--text-muted);
    background: none;
    border: none;
    cursor: pointer;
    transition: color 0.2s;
}
#load-more button:hover { color: var(--accent); }

.message-row {
    display: flex;
    gap: 0.75rem;
    padding: 0.35rem 0.5rem;
    border-radius: 6px;
    transition: background 0.1s;
}
.message-row:hover {
    background: rgba(255, 255, 255, 0.015);
}

.msg-time {
    font-size: 0.7rem;
    color: var(--text-muted);
    flex-shrink: 0;
    width: 3.5rem;
    text-align: right;
    margin-top: 3px;
    opacity: 0.6;
}
.message-row:hover .msg-time { opacity: 1; }

.msg-author {
    font-weight: 500;
    flex-shrink: 0;
    font-size: 0.85rem;
}
.msg-author.self { color: var(--blue); }
.msg-author.entity { color: var(--accent); }
.msg-author.human { color: var(--green); }

.message-content {
    color: var(--text-primary);
    word-break: break-word;
    min-width: 0;
    font-size: 0.9rem;
    line-height: 1.5;
}

.message-content h1, .message-content h2, .message-content h3 {
    font-weight: 600;
    margin: 0.3em 0;
}
.message-content code {
    background: rgba(212, 165, 116, 0.08);
    padding: 1px 5px;
    border-radius: 3px;
    font-family: 'JetBrains Mono', Consolas, monospace;
    font-size: 0.85em;
}
.message-content pre {
    background: rgba(0, 0, 0, 0.3);
    padding: 0.75rem;
    border-radius: 6px;
    overflow-x: auto;
    margin: 0.4rem 0;
    border: 1px solid var(--border-subtle);
}
.message-content pre code {
    background: none;
    padding: 0;
}
.message-content ul, .message-content ol {
    margin-left: 1.5em;
}
.message-content p { margin: 0; }
.message-content p + p { margin-top: 0.4em; }
.message-content a {
    color: var(--accent);
    text-decoration: underline;
    text-decoration-color: rgba(212, 165, 116, 0.3);
}
.message-content a:hover { text-decoration-color: var(--accent); }
.message-content blockquote {
    border-left: 2px solid var(--accent);
    padding-left: 0.75rem;
    color: var(--text-secondary);
    margin: 0.3rem 0;
}

.system-message {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-style: italic;
    padding: 0.25rem 0.5rem;
    text-align: center;
}

/* Copy button */
.copy-btn {
    margin-left: auto;
    flex-shrink: 0;
    align-self: flex-start;
    margin-top: 3px;
    opacity: 0;
    color: var(--text-muted);
    background: none;
    border: none;
    cursor: pointer;
    padding: 2px;
    border-radius: 3px;
    transition: opacity 0.15s, color 0.15s, transform 0.1s;
    line-height: 0;
}
.message-row:hover .copy-btn { opacity: 1; }
.copy-btn:hover { color: var(--text-primary); }
.copy-btn:focus-visible { opacity: 1; outline: 2px solid var(--accent); outline-offset: 2px; }
.copy-btn:active { transform: scale(0.92); }
.copy-btn.copied { color: var(--green); opacity: 1; }

/* Touch devices: no hover, so reveal action affordances at low opacity always.
   Tap activates them with a clear active state. */
@media (hover: none) {
    .copy-btn { opacity: 0.5; }
    .message-row .msg-time { opacity: 0.6; }
}

/* === INPUT === */

.chat-input {
    padding: 0.75rem 1.25rem;
    border-top: 1px solid var(--border-subtle);
    flex-shrink: 0;
    background: var(--bg-panel);
}
.chat-input-row {
    display: flex;
    gap: 0.5rem;
}

.chat-textarea {
    flex: 1;
    padding: 0.6rem 1rem;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    color: var(--text-primary);
    font-family: var(--font-body);
    font-size: 0.9rem;
    outline: none;
    resize: none;
    overflow-y: hidden;
    transition: border-color 0.2s;
    line-height: 1.5;
}
.chat-textarea::placeholder { color: var(--text-muted); }
.chat-textarea:focus { border-color: var(--accent); }

.send-btn {
    padding: 0.6rem 1.25rem;
    background: var(--accent);
    color: var(--bg-deep);
    border: none;
    border-radius: 10px;
    font-weight: 600;
    font-size: 0.85rem;
    cursor: pointer;
    transition: opacity 0.2s;
    align-self: flex-end;
}
.send-btn:hover { opacity: 0.9; }
.send-btn:disabled { opacity: 0.3; cursor: default; }

.attach-btn {
    padding: 0.6rem 0.7rem;
    background: var(--bg-surface);
    color: var(--text-secondary);
    border: 1px solid var(--border);
    border-radius: 10px;
    cursor: pointer;
    transition: opacity 0.2s, color 0.2s;
    align-self: flex-end;
    display: flex;
    align-items: center;
    justify-content: center;
}
.attach-btn:hover { color: var(--accent); }
.attach-btn:disabled { opacity: 0.3; cursor: default; }

.image-preview {
    position: relative;
    display: inline-block;
    margin-bottom: 0.5rem;
    max-width: 200px;
}
.image-preview img {
    max-width: 200px;
    max-height: 150px;
    border-radius: 8px;
    border: 1px solid var(--border);
    display: block;
}
.image-preview button {
    position: absolute;
    top: -8px;
    right: -8px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: var(--bg-deep);
    color: var(--text-primary);
    border: 1px solid var(--border);
    cursor: pointer;
    font-size: 14px;
    line-height: 1;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}
.image-preview button:hover { background: var(--accent); color: var(--bg-deep); }

.chat-input.drag-over {
    background: var(--bg-surface);
    outline: 2px dashed var(--accent);
    outline-offset: -8px;
}

/* === USERS PANEL === */

#users-panel {
    width: 180px;
    background: var(--bg-panel);
    border-left: 1px solid var(--border-subtle);
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
}

.users-header {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border-subtle);
    font-size: 0.65rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-muted);
}

#user-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem;
}

.user-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 5px 8px;
    font-size: 0.85rem;
    color: var(--text-secondary);
    border-radius: 6px;
    cursor: pointer;
    transition: background 0.15s;
}
.user-item:hover {
    background: var(--bg-hover);
}
.user-item .status-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    flex-shrink: 0;
}
.user-item .status-dot.online { background: var(--green); }
.user-item .status-dot.offline { background: var(--text-muted); opacity: 0.4; }
.user-item .bot-tag {
    font-size: 0.6rem;
    color: var(--accent);
    opacity: 0.6;
    margin-left: auto;
}

/* === RESPONSIVE === */
@media (max-width: 768px) {
    #sidebar { width: 60px; }
    .sidebar-title, .sidebar-section-label, .sidebar-footer span,
    .room-item span, .admin-link, .push-notify-btn { display: none; }
    .room-item { justify-content: center; padding: 8px; }
    #users-panel { display: none !important; }
}

/* === SHARED IMAGES === */
/* Inline thumbnail — viewport-responsive so it doesn't dominate narrow phones.
   Caps at 320px on desktop, ~60vw on phones, ~50vh tall on landscape. */
.message-image-wrap {
    margin-top: 6px;
    grid-column: 2;
}
.message-image {
    max-width: min(320px, 60vw);
    max-height: min(320px, 50vh);
    border-radius: 6px;
    cursor: zoom-in;
    display: block;
    border: 1px solid var(--border);
    transition: transform 0.15s ease;
    /* Hint to browsers/iOS: this is a tap target, no callout menu */
    -webkit-touch-callout: none;
}
.message-image:hover { transform: scale(1.02); }
.message-image:active { transform: scale(0.98); }

/* === LIGHTBOX === */
.lightbox-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.92);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10000;
    cursor: zoom-out;
    animation: lb-fade 0.15s ease;
    /* iOS Safari: don't let pinch-zoom or rubber-band escape the overlay */
    touch-action: pinch-zoom;
}
.lightbox-image {
    max-width: 92vw;
    max-height: 92vh;
    border-radius: 8px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
    user-select: none;
    -webkit-user-select: none;
}
.lightbox-close {
    position: absolute;
    top: max(env(safe-area-inset-top, 0px), 1rem);
    right: max(env(safe-area-inset-right, 0px), 1rem);
    width: 44px;            /* min iOS tap target */
    height: 44px;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.5);
    color: var(--text-primary);
    border: 1px solid rgba(255, 255, 255, 0.2);
    font-size: 1.5rem;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    transition: background 0.15s, transform 0.1s;
}
.lightbox-close:hover { background: rgba(0, 0, 0, 0.75); }
.lightbox-close:active { transform: scale(0.92); }
.lightbox-close:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

@keyframes lb-fade {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* === RECONNECTING BANNER === */
.reconnecting-banner {
    position: fixed;
    top: max(env(safe-area-inset-top, 0px), 0.75rem);
    left: 50%;
    transform: translateX(-50%);
    background: var(--bg-panel);
    color: var(--text-secondary);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 0.4rem 1rem;
    font-size: 0.8rem;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
    z-index: 9998;
    animation: lb-fade 0.2s ease;
    pointer-events: none;
}
.reconnecting-banner::before {
    content: '';
    display: inline-block;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--red);
    margin-right: 0.5rem;
    vertical-align: middle;
    animation: reconnectPulse 1.4s ease-in-out infinite;
}
@keyframes reconnectPulse {
    0%, 100% { opacity: 0.4; }
    50% { opacity: 1; }
}

/* Hidden utility */
.hidden { display: none !important; }

/* === SHARED IMAGES === */
.message-image-wrap {
    margin-top: 6px;
    grid-column: 2;
}
.message-image {
    max-width: 320px;
    max-height: 320px;
    border-radius: 6px;
    cursor: zoom-in;
    display: block;
    border: 1px solid var(--border, rgba(255,255,255,0.08));
    transition: transform 0.15s ease;
}
.message-image:hover { transform: scale(1.02); }

.lightbox-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.88);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    cursor: zoom-out;
    animation: lb-fade 0.12s ease;
}
.lightbox-image {
    max-width: 92vw;
    max-height: 92vh;
    border-radius: 8px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.6);
}
@keyframes lb-fade {
    from { opacity: 0; }
    to   { opacity: 1; }
}
