* {
    box-sizing: border-box;
}

:root {
    color-scheme: dark;
    --bg: #0b141a;
    --panel: #111b21;
    --panel-soft: #202c33;
    --bubble-own: #005c4b;
    --bubble-other: #202c33;
    --border: rgba(255, 255, 255, 0.08);
    --text: #e9edef;
    --muted: #8696a0;
    --accent: #53bdeb;
    --accent-strong: #00a884;
    --danger: #ff6b6b;
    --shadow: rgba(0, 0, 0, 0.24);
}

body {
    margin: 0;
    min-height: 100vh;
    background: linear-gradient(180deg, #111b21 0, #0b141a 160px, #0b141a 100%);
    color: var(--text);
    font-family: Arial, sans-serif;
    overflow-x: hidden; /* Prevent horizontal scroll on entire body */
}

.auth-page,
.shell {
    min-height: 100vh;
}

.auth-page {
    display: grid;
    place-items: center;
    padding: 24px;
}

.auth-card,
.sidebar-card,
.chat-panel {
    background: rgba(17, 27, 33, 0.98);
    border: 1px solid var(--border);
    border-radius: 18px;
    box-shadow: 0 18px 40px var(--shadow);
}

.auth-card {
    width: min(100%, 460px);
    padding: 28px;
    display: grid;
    gap: 18px;
}

.shell {
    display: grid;
    grid-template-columns: 300px minmax(0, 1fr);
    gap: 20px;
    padding: 20px;
    max-width: 100vw;
    overflow-x: hidden;
}

.sidebar {
    display: grid;
    gap: 16px;
    align-content: start;
}

.sidebar-card {
    padding: 18px;
}

.chat-panel {
    position: relative;
    display: grid;
    grid-template-rows: auto 1fr auto;
    min-height: calc(100vh - 40px);
    overflow: hidden;
}

.chat-header,
.composer,
.panel-header,
.composer-row,
.message-meta,
.message-actions,
.tabs,
.message-edit-actions,
.header-status,
.message-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.chat-header,
.composer {
    padding: 16px 18px;
}

.chat-header {
    border-bottom: 1px solid var(--border);
}

.composer {
    border-top: 1px solid var(--border);
    display: grid;
    gap: 12px;
    background: rgba(17, 27, 33, 0.99);
}

.messages {
    padding: 18px;
    overflow-y: auto;
    overflow-anchor: none;
    display: grid;
    gap: 12px;
    align-content: start;
    background-image: radial-gradient(rgba(255,255,255,0.02) 1px, transparent 1px);
    background-size: 18px 18px;
}

.chat-scroll-nav {
    position: absolute;
    right: 16px;
    bottom: 150px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    z-index: 6;
    pointer-events: none;
}

.chat-scroll-btn {
    pointer-events: auto;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: 1px solid var(--border);
    background: rgba(17, 27, 33, 0.92);
    color: var(--text);
    font-size: 1.1rem;
    cursor: pointer;
    box-shadow: 0 8px 20px var(--shadow);
    transition: transform 0.15s ease, background 0.15s ease;
}

.chat-scroll-btn:hover {
    background: var(--accent);
    color: #041b17;
    transform: scale(1.05);
}

.typing-indicator {
    padding: 0 18px 12px;
    color: var(--muted);
    font-size: 0.92rem;
}

.message-row {
    align-items: flex-end;
    justify-content: flex-start;
}

.message-row.own {
    justify-content: flex-end;
}

.message-avatar {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: rgba(83, 189, 235, 0.2);
    color: var(--accent);
    display: grid;
    place-items: center;
    font-size: 0.85rem;
    font-weight: 700;
    flex: 0 0 34px;
}

.message-row.own .message-avatar {
    order: 2;
    background: rgba(0, 168, 132, 0.22);
    color: #7fffd9;
}

.message {
    width: 100%;
    max-width: 760px;
}

.message-bubble {
    background: var(--bubble-other);
    border: 1px solid rgba(255, 255, 255, 0.03);
    border-radius: 16px 16px 16px 4px;
    padding: 12px 14px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    box-shadow: 0 10px 20px rgba(0,0,0,0.14);
    max-width: 90%; /* Bubble won't take full width on mobile */
    overflow-wrap: anywhere;
    word-break: break-word;
    overflow: hidden;
    min-width: 0;
    width: fit-content;
}

.message.own .message-row {
    justify-content: flex-end;
}

.message.own .message-bubble {
    background: var(--bubble-own);
    border-radius: 16px 16px 4px 16px;
    align-self: flex-end;
}

.message.own .message-avatar {
    order: 2;
}

.message.flash .message-bubble {
    animation: flash-message 0.9s ease;
}

@keyframes flash-message {
    0% { box-shadow: 0 0 0 0 rgba(83, 189, 235, 0); }
    50% { box-shadow: 0 0 0 4px rgba(83, 189, 235, 0.28); }
    100% { box-shadow: 0 0 0 0 rgba(83, 189, 235, 0); }
}

.message-meta {
    justify-content: space-between;
    gap: 16px;
    font-size: 0.9rem;
}

.message-time,
.message-actions,
.eyebrow,
.subtle,
.badge {
    color: var(--muted);
}

.message-content {
    white-space: pre-wrap;
    line-height: 1.5;
    overflow-wrap: anywhere;
    word-break: break-word;
}

.message-attachment {
    display: flex;
    flex-direction: column;
    gap: 10px;
    max-width: 100%;
    min-width: 0;
    overflow: hidden;
    min-height: 0;
    width: 100%;
}

.message-attachment .attachment-media-link {
    display: block;
    max-width: 100%;
    line-height: 0;
    overflow: hidden;
    border-radius: 12px;
}

.message-attachment .attachment-media,
.message-attachment img,
.message-attachment video {
    max-width: 100%;
    width: 100%;
    height: auto;
    max-height: min(400px, 70vh);
    border-radius: 12px;
    background: #000;
    display: block;
    object-fit: contain;
    flex-shrink: 1;
}

.message-attachment .attachment-audio,
.message-attachment audio {
    width: 100%;
    max-width: 100%;
    min-height: 40px;
}

.message-attachment .attachment-gif,
.message-attachment img.attachment-image[src$=".gif"],
.message-attachment img[src*="gifs/"],
.message-attachment video[src*="gifs/"],
.message-attachment video.attachment-gif {
    max-height: min(300px, 50vh);
}

.download-link {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    max-width: 100%;
    text-decoration: none;
    color: var(--text);
    padding: 10px 12px;
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.07);
    font-size: 0.85rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.reply-preview,
.reply-banner {
    width: 100%;
    border: none;
    text-align: left;
    background: rgba(255, 255, 255, 0.08);
    border-left: 3px solid var(--accent);
    border-radius: 12px;
    padding: 10px 12px;
    color: var(--text);
}

.reply-preview {
    cursor: pointer;
}

.reply-preview strong,
.reply-banner strong {
    display: block;
    margin-bottom: 4px;
    color: var(--accent);
}

.reply-preview span,
.reply-banner p {
    display: block;
    margin: 0;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.stack {
    display: grid;
    gap: 14px;
}

.gap-xs {
    gap: 6px;
}

.badge {
    background: rgba(83, 189, 235, 0.12);
    color: var(--accent);
    border-radius: 999px;
    padding: 4px 10px;
}

.alert {
    border-radius: 12px;
    padding: 12px 14px;
}

.alert.error {
    background: rgba(255, 107, 107, 0.12);
    border: 1px solid rgba(255, 107, 107, 0.3);
    color: #ffd4d4;
}

input,
textarea,
.primary-button,
.link-button,
.tab-button,
.ghost-button,
.file-picker {
    border-radius: 12px;
    border: 1px solid var(--border);
    padding: 12px 14px;
    font: inherit;
}

input,
textarea {
    width: 100%;
    background: #0f1a20;
    color: var(--text);
}

textarea {
    resize: vertical;
    min-height: 88px;
}

input[type="file"] {
    display: none;
}

.primary-button,
.link-button,
.tab-button,
.ghost-button,
.file-picker {
    background: var(--panel-soft);
    color: var(--text);
    cursor: pointer;
    text-decoration: none;
    text-align: center;
}

.primary-button {
    background: linear-gradient(135deg, var(--accent), var(--accent-strong));
    color: #041b17;
    border: none;
    font-weight: 700;
}

.ghost-button,
.message-action {
    background: transparent;
    color: var(--muted);
    border: none;
    padding: 0;
    cursor: pointer;
}

.message-actions {
    opacity: 0.9;
    flex-wrap: wrap;
    justify-content: flex-end;
    font-size: 0.82rem;
}

.message-row:hover .message-actions {
    opacity: 1;
}

.message.own .message-actions .subtle:last-child {
    color: #8de9c9;
}

.message-action.delete-action {
    color: var(--danger);
}

.tab-button.active {
    border-color: var(--accent);
    color: var(--accent);
}

.online-user {
    padding: 10px 12px;
    background: var(--panel-soft);
    border-radius: 12px;
    border: 1px solid rgba(255, 255, 255, 0.04);
}

.file-meta {
    padding: 10px 12px;
    border-radius: 12px;
    background: rgba(255,255,255,0.06);
    color: var(--muted);
}

.upload-progress {
    display: grid;
    gap: 8px;
}

.upload-progress-bar {
    width: 100%;
    height: 10px;
    border-radius: 999px;
    background: rgba(255,255,255,0.08);
    overflow: hidden;
}

.upload-progress-bar span {
    display: block;
    width: 0;
    height: 100%;
    background: linear-gradient(90deg, var(--accent), var(--accent-strong));
    transition: width 0.15s ease;
}

.message-edit-wrap {
    display: grid;
    gap: 10px;
    width: 100%;
    max-width: 100%;
}

.message-edit-input {
    min-height: 84px;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
}

.message-edit-actions {
    flex-wrap: wrap;
    gap: 8px;
}

.hidden {
    display: none !important;
}

/* Voice Recording Styles */
.voice-record-btn {
    background: var(--panel-soft);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 12px 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--text);
    font: inherit;
    transition: all 0.2s ease;
}

.voice-record-btn:hover {
    background: var(--accent);
    color: #041b17;
}

.voice-record-btn.recording {
    background: var(--danger);
    animation: pulse 1s infinite;
}

@keyframes pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.05); }
}

.voice-icon {
    font-size: 1.2rem;
}

.voice-timer {
    font-family: monospace;
    font-size: 0.9rem;
}

.voice-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.85);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.voice-overlay-content {
    background: var(--panel);
    border-radius: 24px;
    padding: 40px;
    text-align: center;
    max-width: 400px;
    width: 90%;
}

.voice-wave {
    width: 80px;
    height: 80px;
    background: linear-gradient(135deg, var(--accent), var(--accent-strong));
    border-radius: 50%;
    margin: 0 auto 24px;
    animation: wave 1s ease-in-out infinite;
}

@keyframes wave {
    0%, 100% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.1); opacity: 0.8; }
}

.voice-status {
    font-size: 1.2rem;
    margin-bottom: 8px;
    color: var(--text);
}

.voice-timer-display {
    font-family: monospace;
    font-size: 2rem;
    color: var(--accent);
    margin-bottom: 24px;
}

.voice-actions {
    display: flex;
    gap: 16px;
    justify-content: center;
}

/* Composer icon buttons (voice, GIF, file, send) */
.composer-icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
    padding: 10px 12px;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: var(--panel-soft);
    color: var(--text);
    cursor: pointer;
    font: inherit;
    transition: background 0.2s ease, color 0.2s ease, transform 0.15s ease;
}

.composer-icon-btn:hover {
    background: var(--accent);
    color: #041b17;
}

.composer-icon {
    font-size: 1.25rem;
    line-height: 1;
}

.composer-send-btn.primary-button {
    background: linear-gradient(135deg, var(--accent), var(--accent-strong));
    color: #041b17;
    border: none;
    font-weight: 700;
}

.voice-record-btn.recording {
    background: var(--danger);
    color: #fff;
    animation: pulse 1s infinite;
}

/* GIF Modal Styles */
.gif-modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.85);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 20px;
}

.gif-modal-content {
    background: var(--panel);
    border-radius: 24px;
    padding: 20px;
    max-width: 520px;
    width: 95%;
    max-height: 75vh;
    display: flex;
    flex-direction: column;
}

.gif-modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
}

.gif-modal-header h3 {
    margin: 0;
    color: var(--text);
}

.gif-modal-header button {
    font-size: 1.5rem;
    padding: 4px 12px;
}

.gif-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
    gap: 8px;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 2px;
    flex: 1;
    min-height: 0;
}

.gif-item {
    aspect-ratio: 1;
    border-radius: 8px;
    overflow: hidden;
    cursor: pointer;
    transition: transform 0.15s ease;
    background: var(--panel-soft);
    border: 1px solid transparent;
}

.gif-item:hover {
    transform: scale(1.08);
    border-color: var(--accent);
    z-index: 1;
}

.gif-item img,
.gif-item video {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.gif-item .gif-label {
    position: absolute;
    bottom: 4px;
    right: 4px;
    background: rgba(0, 0, 0, 0.75);
    color: var(--text);
    font-size: 0.6rem;
    padding: 1px 4px;
    border-radius: 3px;
    font-weight: 600;
}

/* Composer Row Responsive */
.composer-row {
    flex-wrap: wrap;
    gap: 8px;
}

.composer-row button,
.composer-row label {
    flex-shrink: 0;
}

/* Responsive Improvements */
@media (max-width: 920px) {
    .shell {
        grid-template-columns: 1fr;
    }

    .chat-panel {
        min-height: 78vh;
    }

    .message {
        width: 100%;
    }

    .message-avatar {
        display: none;
    }
    
    .message-bubble {
        max-width: 100%;
    }

    .message-attachment .attachment-media,
    .message-attachment img,
    .message-attachment video {
        max-height: min(360px, 65vh);
    }

    .message-attachment .attachment-gif,
    .message-attachment video.attachment-gif {
        max-height: min(280px, 45vh);
    }
    
    .composer-row {
        justify-content: flex-start;
    }
}

@media (max-width: 640px) {
    .shell {
        padding: 10px;
    }
    
    .messages {
        padding: 10px;
    }
    
    .chat-header,
    .composer {
        padding: 12px;
    }
    
    .message-bubble {
        padding: 10px 12px;
    }

    .message-attachment .attachment-media,
    .message-attachment img,
    .message-attachment video {
        max-height: min(320px, 55vh);
    }

    .message-attachment .attachment-gif,
    .message-attachment video.attachment-gif {
        max-height: min(240px, 40vh);
    }
    
    .gif-modal-content {
        max-height: 92vh;
        padding: 14px;
        border-radius: 16px;
    }

    .gif-grid {
        grid-template-columns: repeat(auto-fill, minmax(65px, 1fr));
        gap: 6px;
    }

    .gif-item {
        border-radius: 6px;
    }
    
    .voice-overlay-content {
        padding: 30px 20px;
    }
}

/* Additional Styles for Fixed Elements */
.composer {
    position: relative;
}

.message-edit-wrap {
    position: relative;
    z-index: 1;
}

.message-attachment img,
.message-attachment video {
    transition: opacity 0.2s ease;
}

.message-attachment img:hover,
.message-attachment video:hover {
    opacity: 0.95;
}

/* Programmatic scroll uses JS; avoid smooth scroll jumping during sync */
.messages {
    scroll-behavior: auto;
}

/* Better touch targets on mobile */
@media (max-width: 640px) {
    .message-action,
    .composer-icon-btn {
        min-height: 44px;
        min-width: 44px;
    }

    .chat-scroll-nav {
        right: 10px;
        bottom: 130px;
    }
}
