/* Image Viewer Modal */
#image-viewer {
    position: fixed;
    inset: 0;
    z-index: 1000;
}

#image-viewer .viewer-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.75);
}

#viewer-img {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(1);
    max-width: 90vw;
    max-height: 85vh;
    box-shadow: 0 10px 30px rgba(0,0,0,0.4);
    border-radius: 8px;
    transition: transform 120ms ease-out;
    will-change: transform;
}

#viewer-close {
    position: absolute;
    top: 16px;
    right: 16px;
    background: rgba(255,255,255,0.9);
    border: 0;
    border-radius: 8px;
    padding: 6px 10px;
    cursor: pointer;
}

.viewer-hint {
    position: absolute;
    left: 50%;
    bottom: 16px;
    transform: translateX(-50%);
    color: #f8f9fa;
    background: rgba(0,0,0,0.5);
    padding: 6px 10px;
    border-radius: 6px;
    font-size: 12px;
}
html, body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    background-color: #f0f2f5;
    color: #1c1e21;
    margin: 0;
    height: 100%;
    width: 100%;
    overflow: hidden;
}

.app-container {
    display: flex;
    height: 100%;
    width: 100%;
}

.sidebar {
    width: 100%; /* Default for mobile */
    background: #fff;
    border-right: 1px solid #dddfe2;
    padding: 20px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    transition: transform 0.3s ease-in-out;
}

.sidebar h2 {
    font-size: 1.1em;
    color: #606770;
    margin-top: 0;
}

#my-id {
    font-weight: bold;
    color: #1877f2;
    background-color: #e7f3ff;
    padding: 2px 6px;
    border-radius: 4px;
    user-select: all;
}

.connection-form {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 10px;
    margin-top: 15px;
    padding-bottom: 15px;
    border-bottom: 1px solid #ddd;
}

input[type='text'] {
    padding: 10px;
    border: 1px solid #dddfe2;
    border-radius: 6px;
    font-size: 1em;
}

/* Ensure both input fields in flex containers grow correctly */
.connection-form input[type='text'],
.message-input input[type='text'] {
    flex-grow: 1;
    min-width: 50px;
}

button {
    padding: 10px 15px;
    border: none;
    border-radius: 6px;
    background-color: #1877f2;
    color: white;
    font-size: 1em;
    font-weight: bold;
    cursor: pointer;
    transition: background-color 0.2s;
}

button:hover {
    background-color: #166fe5;
}

#connect-btn {
    flex-shrink: 0; /* Prevent the button from shrinking */
}

.sidebar hr {
    border: none;
    border-top: 1px solid #dddfe2;
    margin: 20px 0;
}

#chat-list {
    overflow-y: auto;
    flex-grow: 1;
}



@media (min-width: 768px) {
    .sidebar {
        width: 350px;
        min-width: 350px; /* Fixed width on desktop */
    }
}

/* Constrain overall app width on large desktops */
@media (min-width: 1200px) {
    .app-container {
        max-width: 1100px; /* adjust as desired */
        margin: 0 auto; /* center the app */
    }
}

.chat-list-item {
    display: flex;
    align-items: center;
    padding: 12px 10px;
    border-radius: 6px;
    cursor: pointer;
    margin-bottom: 5px;
    font-weight: 500;
    position: relative; /* Needed for absolute positioning of children */
}

.chat-list-item .peer-id-text {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex-grow: 1;
}

.online-indicator {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background-color: #ccc; /* Offline by default */
    margin-right: 10px;
    flex-shrink: 0;
}

.online-indicator.connected {
    background-color: #28a745; /* Green for online */
}

.unread-badge {
    background-color: #dc3545;
    color: white;
    font-size: 12px;
    font-weight: bold;
    padding: 2px 6px;
    border-radius: 10px;
    margin-left: auto;
    margin-right: 10px;
    flex-shrink: 0;
}

.delete-chat-btn {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 22px;
    line-height: 1;
    color: #aaa;
    padding: 0 5px;
    display: none; /* Hidden by default on desktop */
    flex-shrink: 0;
}

.chat-list-item:hover .delete-chat-btn {
    display: block;
}

.delete-chat-btn:hover {
    color: #dc3545;
}

.chat-list-item:hover {
    background-color: #f0f2f5;
}

.chat-list-item.active {
    background-color: #e7f3ff;
    color: #1877f2;
}

.main-content {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
}

#welcome-screen {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    height: 100%;
    text-align: center;
    padding: 20px;
}

#chat-area {
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%;
    position: relative; /* for absolute-positioned children like scroll button & overlay */
    /* Starry pattern: small stars + dots (inline SVG, seamless) */
    background-color: #FFF7E6; /* cream fallback */
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='140' height='140' viewBox='0 0 140 140'><defs><g id='star' fill='%23C8A97E' fill-opacity='0.28'><path d='M7 0 L9 5 L14 7 L9 9 L7 14 L5 9 L0 7 L5 5 Z'/></g></defs><rect width='100%25' height='100%25' fill='none'/><g><use href='%23star' x='15' y='18'/><use href='%23star' x='95' y='30' transform='scale(0.9)'/><use href='%23star' x='55' y='95' transform='scale(0.8)'/><use href='%23star' x='120' y='110' transform='scale(0.7)'/></g><g fill='%23C8A97E' fill-opacity='0.22'><circle cx='20' cy='70' r='1.6'/><circle cx='40' cy='20' r='1.4'/><circle cx='80' cy='50' r='1.6'/><circle cx='110' cy='20' r='1.2'/><circle cx='15' cy='120' r='1.4'/><circle cx='70' cy='15' r='1.2'/><circle cx='125' cy='60' r='1.4'/><circle cx='90' cy='120' r='1.6'/><circle cx='30' cy='95' r='1.2'/><circle cx='60' cy='75' r='1.2'/><circle cx='100' cy='95' r='1.2'/><circle cx='25' cy='35' r='1.2'/><circle cx='115' cy='45' r='1.2'/></g></svg>");
    background-repeat: repeat;
    background-size: 120px 120px; /* adjust density: smaller => denser */
    background-attachment: local;
}

.chat-header {
    padding: 15px 20px;
    border-bottom: 1px solid #dddfe2;
    background: #fff;
    display: flex;
    align-items: center;
    gap: 15px;
    flex-wrap: wrap;
}

.chat-header h2 {
    margin: 0;
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 1.1em;
    flex-wrap: wrap;
}

#back-to-chats {
    background: none;
    border: 1px solid #dddfe2;
    color: #606770;
    font-weight: normal;
    padding: 8px 12px;
}

.connection-status {
    font-size: 0.8em;
    color: #666;
    margin-left: 8px;
    font-weight: normal;
}

.connection-status[data-state="connected"] {
    color: #4caf50; /* Green for connected */
}

.connection-status[data-state="disconnected"],
.connection-status[data-state="failed"],
.connection-status[data-state="error"] {
    color: #f44336; /* Red for errors */
}

.connection-status::before {
    content: '•';
    display: inline-block;
    margin-right: 4px;
    font-size: 1.2em;
    line-height: 0;
    vertical-align: middle;
}

.connection-status[data-state="connected"]::before {
    color: #4caf50;
}

.connection-status[data-state="disconnected"]::before,
.connection-status[data-state="failed"]::before,
.connection-status[data-state="error"]::before {
    color: #f44336;
}

#messages {
    flex-grow: 1;
    padding: 20px;
    overflow-y: auto;
    /* Let the chat-area pattern be visible */
    background-color: transparent;
    /* Make each message its own flex item for shrink-to-fit bubbles */
    display: flex;
    flex-direction: column;
}

#typing-indicator {
    height: 20px;
    font-style: italic;
    color: #606770;
    padding: 0 20px 5px 20px;
    /* Match messages area: transparent over chat-area pattern */
    background: transparent;
}

.message-input {
    display: flex;
    gap: 10px;
    padding: 15px;
    border-top: 1px solid #dddfe2;
    background: #fff;
}

.message {
    /* Shrink bubble to its content */
    display: inline-block;
    padding: 8px 12px;
    border-radius: 18px;
    margin-bottom: 8px;
    max-width: 75%;
    width: fit-content;
    min-width: 40px; /* keep small tap target */
    line-height: 1.25;
    word-wrap: break-word;
    overflow-wrap: anywhere;
    align-self: flex-start; /* default alignment for received */
}

/* Images inside message bubbles */
.message img.chat-image {
    display: block;
    max-width: 100%;
    height: auto;
    border-radius: 12px; /* smooth corners */
    cursor: pointer; /* indicate clickable */
}

/* Videos inside message bubbles */
.message video.chat-video {
    display: block;
    max-width: 100%;
    height: auto;
    border-radius: 12px;
}

/* Compact padding for media-only bubbles */
.message.only-image,
.message.only-media {
    padding: 6px;
}

/* Inline text container to separate from status icon */
.message .message-text {
    display: inline;
}

/* Small inline status icon next to message text */
.message .message-status {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin-left: 8px;
    vertical-align: middle;
    filter: drop-shadow(0 0 1px rgba(0,0,0,0.35)); /* Improve visibility on varied backgrounds */
}

/* Subtle timestamp below content, right-aligned */
.message .meta {
    display: block;
    text-align: right;
    font-size: 11px;
    opacity: 0.6;
    line-height: 1;
    margin-top: 4px;
}

/* Progress bar for chunked transfers */
.message .progress-container {
    width: 220px;
    height: 8px;
    background: rgba(0,0,0,0.1);
    border-radius: 999px;
    overflow: hidden;
}
.message.sent .progress-container { background: rgba(255,255,255,0.35); }
.message.received .progress-container { background: rgba(0,0,0,0.12); }

.message .progress-bar {
    height: 100%;
    width: 0%;
    background: #2f9e44; /* green */
    transition: width 120ms linear;
}

.message .progress-text {
    margin-top: 6px;
    font-size: 12px;
    opacity: 0.85;
}

/* Progress actions (e.g., cancel) */
.message .progress-actions {
    margin-top: 6px;
    display: flex;
    gap: 8px;
}

/* Inline audio element styling */
.chat-audio {
    width: 100%;
    max-width: 100%;
    display: block;
}

/* Mini audio player */
.mini-audio { width: 100%; }
.mini-audio-hidden { display: none; }
.mini-audio .audio-controls {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
}
.mini-audio .audio-toggle {
    width: 28px; height: 28px;
    border-radius: 50%;
    border: 1px solid transparent;
    background: rgba(0,0,0,0.06);
    color: #212529;
    font-size: 14px;
    line-height: 1;
    display: inline-flex; align-items: center; justify-content: center;
    cursor: pointer;
    transition: background 120ms ease, border-color 120ms ease, transform 80ms ease;
}
.mini-audio .audio-toggle:hover { background: rgba(0,0,0,0.10); }
.mini-audio .audio-toggle:active { transform: translateY(1px); }
.mini-audio .audio-toggle:focus-visible { outline: 2px solid #2f9e44; outline-offset: 1px; }

/* Theme: sent bubble (blue) -> invert to light */
.message.sent .mini-audio .audio-toggle {
    background: rgba(255,255,255,0.18);
    border-color: rgba(255,255,255,0.45);
    color: #ffffff;
}
.message.sent .mini-audio .audio-toggle:hover {
    background: rgba(255,255,255,0.28);
    border-color: rgba(255,255,255,0.65);
}

.mini-audio .audio-seek {
    -webkit-appearance: none; appearance: none;
    flex: 1 1 auto;
    height: 4px;
    background: transparent;
}
/* WebKit track */
.mini-audio .audio-seek::-webkit-slider-runnable-track {
    height: 4px; border-radius: 999px;
    background: rgba(0,0,0,0.15);
}
.message.sent .mini-audio .audio-seek::-webkit-slider-runnable-track { background: rgba(255,255,255,0.5); }
/* WebKit thumb */
.mini-audio .audio-seek::-webkit-slider-thumb {
    -webkit-appearance: none; appearance: none;
    width: 12px; height: 12px; margin-top: -4px;
    border-radius: 50%; background: #2f9e44; border: 0;
}
/* Firefox track */
.mini-audio .audio-seek::-moz-range-track {
    height: 4px; border-radius: 999px;
    background: rgba(0,0,0,0.15);
}
.message.sent .mini-audio .audio-seek::-moz-range-track { background: rgba(255,255,255,0.5); }
/* Firefox thumb */
.mini-audio .audio-seek::-moz-range-thumb {
    width: 12px; height: 12px;
    border-radius: 50%; background: #2f9e44; border: 0;
}

.mini-audio .audio-time {
    font-size: 12px;
    opacity: 0.85;
    min-width: 36px;
    text-align: right;
}

/* Volume */
.mini-audio .audio-volicon {
    width: 28px; height: 28px;
    border-radius: 6px; border: 1px solid transparent;
    background: rgba(0,0,0,0.06); color: #212529;
    font-size: 14px; line-height: 1; cursor: pointer;
    display: inline-flex; align-items: center; justify-content: center;
}
.message.sent .mini-audio .audio-volicon {
    background: rgba(255,255,255,0.18);
    border-color: rgba(255,255,255,0.45);
    color: #ffffff;
}
.mini-audio .audio-volume {
    -webkit-appearance: none; appearance: none;
    width: 64px;
    height: 6px;
    border-radius: 999px;
    background: rgba(47,158,68,0.25);
}
.mini-audio .audio-volume::-webkit-slider-runnable-track { height: 6px; border-radius: 999px; background: transparent; }
.mini-audio .audio-volume::-webkit-slider-thumb {
    -webkit-appearance: none; appearance: none;
    width: 10px; height: 16px; margin-top: -5px;
    border-radius: 3px; background: #2f9e44; border: 0;
}
.mini-audio .audio-volume::-moz-range-track { height: 6px; border-radius: 999px; background: transparent; }
.mini-audio .audio-volume::-moz-range-thumb {
    width: 10px; height: 16px;
    border-radius: 3px; background: #2f9e44; border: 0;
}

/* Menu */
.message .progress-actions .progress-cancel {
    /* Small, distinct chip-like button */
    font-size: 12px;
    line-height: 1;
    padding: 4px 10px;
    border-radius: 999px;
    border: 1px solid transparent;
    background: transparent;
    cursor: pointer;
    transition: background 120ms ease, border-color 120ms ease, transform 80ms ease;
}
/* Sent (blue) bubble theme: use light translucent button with white text */
.message.sent .progress-actions .progress-cancel {
    background: rgba(255,255,255,0.18);
    border-color: rgba(255,255,255,0.45);
    color: #ffffff;
}
.message.sent .progress-actions .progress-cancel:hover {
    background: rgba(255,255,255,0.28);
    border-color: rgba(255,255,255,0.65);
}
/* Received (light) bubble theme: subtle gray button with dark text */
.message.received .progress-actions .progress-cancel {
    background: rgba(0,0,0,0.06);
    border-color: rgba(0,0,0,0.18);
    color: #212529;
}
.message.received .progress-actions .progress-cancel:hover {
    background: rgba(0,0,0,0.10);
    border-color: rgba(0,0,0,0.24);
}
/* Focus styles for accessibility */
.message .progress-actions .progress-cancel:focus-visible {
    outline: 2px solid #2f9e44;
    outline-offset: 1px;
}
/* Subtle press feedback */
.message .progress-actions .progress-cancel:active {
    transform: translateY(1px);
}

/* Colors are set in SVG stroke; optional opacity nuances */
.message .message-status.sending svg { opacity: 0.7; }
.message .message-status.failed svg { opacity: 0.95; }

/* Force strong contrast on sent (blue) bubbles */
.message.sent .message-status svg,
.message.sent .message-status svg * {
    stroke: #ffffff !important;
}

/* Fine-tune per status on sent bubbles */
.message.sent .message-status.sending svg,
.message.sent .message-status.sending svg * { stroke: rgba(255,255,255,0.85) !important; }

/* Keep failure noticeable (red) on blue background */
.message.sent .message-status.failed svg,
.message.sent .message-status.failed svg * {
    stroke: #ff5252 !important;
}

.message.sent {
    background-color: #1877f2;
    color: white;
    align-self: flex-end; /* push to right in flex column */
}

.message.received {
    background-color: #e4e6eb;
    color: #050505;
    align-self: flex-start; /* explicit for clarity */
}

/* Attach button styles */
.message-input #attach-btn {
    background: #f1f3f5;
    border: 1px solid #d0d7de;
    color: #333;
    border-radius: 8px;
    padding: 0 10px;
}
.message-input #attach-btn:hover {
    background: #e9ecef;
}

/* Drag-and-drop overlay */
#drag-overlay {
    position: absolute;
    inset: 0;
    background: rgba(33, 37, 41, 0.55);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10;
    pointer-events: none; /* allow drop to pass through to messages */
}

/* Scroll to bottom button */
#scroll-to-bottom {
    position: absolute;
    right: 12px;
    bottom: 76px; /* above input area */
    width: 42px;
    height: 42px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    border: 1px solid #d0d7de;
    background: #ffffff;
    box-shadow: 0 6px 18px rgba(0,0,0,0.16);
    cursor: pointer;
    z-index: 11; /* above overlay */
}
#scroll-to-bottom::before {
    content: "";
    width: 22px;
    height: 22px;
    display: block;
    background: center / contain no-repeat url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23212529'><path d='M6.7 8.7a1 1 0 0 1 1.4 0L12 12.59l3.9-3.9a1 1 0 1 1 1.4 1.42l-4.6 4.6a1 1 0 0 1-1.4 0l-4.6-4.6a1 1 0 0 1 0-1.42z'/></svg>");
}
#scroll-to-bottom:hover {
    background: #f6f8fa;
}

#drag-overlay .overlay-content {
    background: #ffffff;
    color: #212529;
    border-radius: 16px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.25);
    padding: 24px 28px;
    text-align: center;
    max-width: 90%;
}

#drag-overlay .overlay-icon {
    font-size: 36px;
    margin-bottom: 10px;
}

#drag-overlay .overlay-text {
    font-size: 16px;
    font-weight: 600;
}

.hidden {
    display: none !important;
}

/* --- Responsive Design --- */
@media (max-width: 768px) {
    html, body {
        font-size: 16px; /* Prevents iOS auto-zoom on input focus */
    }

    .app-container {
        overflow-x: hidden;
    }

    .sidebar {
        position: absolute;
        width: 100%;
        height: 100%;
        z-index: 10;
        transform: translateX(0);
    }

    .main-content {
        width: 100%;
        position: absolute;
        height: 100%;
        transform: translateX(100%);
        transition: transform 0.3s ease-in-out;
    }

    .app-container.chat-active .sidebar {
        transform: translateX(-100%);
    }

    .app-container.chat-active .main-content {
        transform: translateX(0);
    }

    #back-to-chats {
        display: block;
        font-size: 1.1em;
        padding: 10px 15px;
    }

    .chat-header h2 {
        font-size: 1em;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    input[type='text'], button {
        padding: 15px;
        font-size: 1em;
    }

    .message-input {
        padding: 10px;
    }

    .delete-chat-btn {
        display: block; /* Always visible on mobile */
    }
}

