/* Chat P2P - Modern Flat Design */
/* Base Variables & Theme */
:root,
[data-theme="dark"] {
    /* Palette: Slate & Indigo */
    --cp-primary: #6366f1;
    /* Indigo 500 */
    --cp-primary-hover: #4f46e5;
    /* Indigo 600 */
    --cp-secondary: #475569;
    /* Slate 600 */

    --cp-bg-main: #0f172a;
    /* Slate 900 */
    --cp-bg-sidebar: #1e293b;
    /* Slate 800 */
    --cp-bg-card: #1e293b;
    /* Slate 800 */
    --cp-bg-hover: #334155;
    /* Slate 700 */
    --cp-bg-input: #334155;
    /* Slate 700 */
    --cp-bg-message-received: #334155;
    /* Slate 700 */

    --cp-text: #f8fafc;
    /* Slate 50 */
    --cp-text-muted: #94a3b8;
    /* Slate 400 */

    --cp-border: #334155;
    /* Slate 700 */
    --cp-border-light: rgba(255, 255, 255, 0.05);

    --cp-success: #10b981;
    /* Emerald 500 */
    --cp-danger: #ef4444;
    /* Red 500 */
    --cp-warning: #f59e0b;
    /* Amber 500 */

    --cp-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --cp-radius-lg: 16px;
    --cp-radius-md: 12px;
    --cp-radius-sm: 8px;

    --cp-gradient: linear-gradient(135deg, #6366f1 0%, #4f46e5 100%);
}

[data-theme="light"] {
    --cp-primary: #4f46e5;
    /* Indigo 600 */
    --cp-primary-hover: #4338ca;
    /* Indigo 700 */
    --cp-secondary: #cbd5e1;
    /* Slate 300 */

    --cp-bg-main: #f1f5f9;
    /* Slate 100 */
    --cp-bg-sidebar: #ffffff;
    /* White */
    --cp-bg-card: #ffffff;
    /* White */
    --cp-bg-hover: #f8fafc;
    /* Slate 50 */
    --cp-bg-input: #f1f5f9;
    /* Slate 100 */
    --cp-bg-message-received: #ffffff;

    --cp-text: #0f172a;
    /* Slate 900 */
    --cp-text-muted: #64748b;
    /* Slate 500 */

    --cp-border: #e2e8f0;
    /* Slate 200 */
    --cp-border-light: rgba(0, 0, 0, 0.05);

    --cp-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05), 0 2px 4px -1px rgba(0, 0, 0, 0.03);

    --cp-gradient: linear-gradient(135deg, #4f46e5 0%, #4338ca 100%);
}

* {
    box-sizing: border-box;
}

body {
    font-family: 'Inter', system-ui, -apple-system, sans-serif;
    background: var(--cp-bg-main);
    color: var(--cp-text);
    margin: 0;
    height: 100vh;
    overflow: hidden;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}


/* Layout - Topbar + Sidebar + Main */
body {
    display: flex;
    flex-direction: column;
}

/* App Header - Full Width Topbar */
.app-header {
    height: 60px;
    background: var(--cp-bg-card);
    border-bottom: 1px solid var(--cp-border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1.5rem;
    flex-shrink: 0;
    z-index: 100;
}

.header-left,
.header-center,
.header-right {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.header-center {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
}

.app-logo {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.app-logo i {
    color: var(--cp-primary);
    font-size: 1.3rem;
}

.app-logo h1 {
    font-size: 1.1rem;
    font-weight: 700;
    margin: 0;
    color: var(--cp-text);
}

.connection-pill {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.35rem 0.75rem;
    background: var(--cp-bg-input);
    border-radius: 20px;
    font-size: 0.75rem;
}

/* App Layout - Container for Sidebar + Main */
.app-layout {
    display: flex;
    flex: 1;
    overflow: hidden;
    height: calc(100vh - 60px);
}

/* Sidebar */
.chat-sidebar {
    width: 260px;
    background: var(--cp-bg-sidebar);
    border-right: 1px solid var(--cp-border);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    height: 100%;
    overflow: hidden;
}

.chat-sidebar {
    width: 260px;
    /* More compact */
    background: var(--cp-bg-sidebar);
    border-right: 1px solid var(--cp-border);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    z-index: 10;
}

.sidebar-section {
    padding: 0;
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    /* Important for scroll */
}

/* Fix vertical layout for sidebar sections */
aside.chat-sidebar {
    gap: 0;
}

.sidebar-section:first-child {
    flex: 0 0 auto;
    max-height: 50%;
}

.sidebar-section:last-child {
    flex: 1;
    border-top: 1px solid var(--cp-border-light);
}


.sidebar-header {
    padding: 1rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.25rem;
}

.sidebar-header h2 {
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--cp-text-muted);
    margin: 0;
}

/* Room & Peer Items - Flat interactive list */
.rooms-list,
.peers-list {
    padding: 0 0.5rem;
    overflow-y: auto;
    flex: 1;
    /* Scrollable area */
}

.room-item,
.peer-item {
    display: flex;
    align-items: center;
    padding: 0.5rem 0.75rem;
    /* Compact padding */
    margin-bottom: 2px;
    border-radius: var(--cp-radius-sm);
    cursor: pointer;
    transition: all 0.2s ease;
    color: var(--cp-text-muted);
    text-decoration: none;
    border: 1px solid transparent;
}

.room-item:hover,
.peer-item:hover {
    background: var(--cp-bg-hover);
    color: var(--cp-text);
}

.room-item.active {
    background: rgba(99, 102, 241, 0.1);
    /* Transparent primary */
    color: var(--cp-primary);
    font-weight: 500;
}

.room-icon,
.peer-avatar {
    margin-right: 0.75rem;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.peer-avatar {
    width: 32px;
    /* Smaller avatar */
    height: 32px;
    border-radius: 50%;
    background: var(--cp-bg-input);
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--cp-text);
    position: relative;
    border: 2px solid var(--cp-bg-sidebar);
    /* Cutout effect */
}

/* Peer Status Dot */
.peer-avatar::after {
    content: '';
    position: absolute;
    bottom: 0px;
    right: 0px;
    width: 8px;
    height: 8px;
    background: var(--cp-success);
    border-radius: 50%;
    border: 1.5px solid var(--cp-bg-sidebar);
}

.room-info,
.peer-info {
    flex: 1;
    min-width: 0;
}

.room-name,
.peer-name {
    font-size: 0.9rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.2;
}

.room-meta,
.peer-status {
    font-size: 0.75rem;
    opacity: 0.7;
}

/* Main Chat Area */
.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--cp-bg-main);
    min-width: 0;
    position: relative;
}

/* Header - Floating/Flat */
.chat-header {
    height: 64px;
    padding: 0 1.5rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--cp-bg-card);
    /* Opaque for scrolling */
    border-bottom: 1px solid var(--cp-border);
    z-index: 5;
    flex-shrink: 0;
}

.chat-header-logo {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-weight: 600;
    font-size: 1.1rem;
    color: var(--cp-text);
}

.chat-header-logo i {
    color: var(--cp-primary);
    font-size: 1.25rem;
}

.chat-header-right {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

/* Connection Status */
.connection-status {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.35rem 0.75rem;
    background: var(--cp-bg-input);
    border-radius: 2rem;
    font-size: 0.75rem;
    font-weight: 500;
}

.status-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--cp-success);
}

.status-dot.connecting {
    background: var(--cp-warning);
    animation: blink 1s infinite;
}

.status-dot.offline {
    background: var(--cp-danger);
}

@keyframes blink {
    50% {
        opacity: 0.5;
    }
}

/* User Info */
.user-info {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-left: 0.5rem;
    margin-right: 0.5rem;
    padding-left: 1rem;
    border-left: 1px solid var(--cp-border);
}

.user-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--cp-gradient);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.8rem;
    font-weight: 600;
    color: white;
}

.user-details {
    display: flex;
    flex-direction: column;
    line-height: 1.1;
}

.user-name {
    font-size: 0.85rem;
    font-weight: 600;
}

.user-role {
    font-size: 0.7rem;
    color: var(--cp-text-muted);
}

/* Buttons - Modern Flat */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.5rem;
    border-radius: var(--cp-radius-sm);
    font-size: 0.9rem;
    font-weight: 500;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    cursor: pointer;
    border: none;
    outline: none;
    gap: 0.5rem;
    line-height: 1;
}

/* Primary Button (Gradient) */
.btn-primary {
    background: var(--cp-gradient);
    color: white;
    padding: 0.5rem 1rem;
    box-shadow: 0 2px 4px rgba(99, 102, 241, 0.3);
}

.btn-primary:hover {
    box-shadow: 0 4px 8px rgba(99, 102, 241, 0.4);
    transform: translateY(-1px);
}

.btn-secondary {
    background: transparent;
    color: var(--cp-text-muted);
    border: 1px solid transparent;
}

.btn-secondary:hover {
    background: var(--cp-bg-hover);
    color: var(--cp-text);
}

.btn-outline-primary {
    background: transparent;
    border: 1px solid var(--cp-border);
    color: var(--cp-primary);
    padding: 0.35rem 0.5rem;
    /* Tiny button */
}

.btn-outline-primary:hover {
    background: rgba(99, 102, 241, 0.1);
    border-color: var(--cp-primary);
}

/* Messages Area - Compact Bubbles */
.messages-container {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    /* Reduced gap */
}

/* Scrollbar Styling */
::-webkit-scrollbar {
    width: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--cp-border);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--cp-text-muted);
}

.message {
    display: flex;
    align-items: flex-start;
    /* Avatar aligné avec le haut de la bulle */
    gap: 0.75rem;
    max-width: 70%;
    margin-bottom: 0.5rem;
    animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(5px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message.sent {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.message-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background-size: cover;
    background-position: center;
    background-color: var(--cp-secondary);
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7rem;
    color: #fff;
    border: 2px solid var(--cp-bg-main);
    /* Separation from bg */
}

.message.sent .message-avatar {
    background-color: var(--cp-primary);
}

.message-content {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    min-width: 0;
    /* Prevent overflow */
}

.message-bubble {
    padding: 0.6rem 1rem;
    border-radius: var(--cp-radius-lg);
    font-size: 0.95rem;
    line-height: 1.5;
    position: relative;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
    word-break: break-word;
    /* Important for long words */
}

/* Received Message */
.message:not(.sent) .message-bubble {
    background: var(--cp-bg-message-received);
    color: var(--cp-text);
    border-bottom-left-radius: 4px;
}

/* Sent Message */
.message.sent .message-bubble {
    background: var(--cp-primary);
    color: #fff;
    border-bottom-right-radius: 4px;
}

.message-meta {
    font-size: 0.7rem;
    color: var(--cp-text-muted);
    margin-top: 0.1rem;
    padding: 0 0.5rem;
}

.message.sent .message-meta {
    text-align: right;
}

/* Input Area - Floating Bar */
.message-input-container {
    padding: 1rem 1.5rem;
    /* More spacing */
    background: var(--cp-bg-main);
    /* Match main bg, not card */
    flex-shrink: 0;
}

.message-input-wrapper {
    background: var(--cp-bg-input);
    border-radius: var(--cp-radius-lg);
    /* Pill shape container */
    padding: 0.5rem;
    display: flex;
    align-items: flex-end;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
    border: 1px solid var(--cp-border-light);
    transition: box-shadow 0.2s;
}

.message-input-wrapper:focus-within {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    border-color: var(--cp-primary);
}

.message-input {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--cp-text);
    padding: 0.6rem 1rem;
    resize: none;
    max-height: 100px;
    font-family: inherit;
    font-size: 0.95rem;
}

.message-input:focus {
    outline: none;
}

.message-input::placeholder {
    color: var(--cp-text-muted);
    opacity: 0.7;
}

.send-button {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--cp-primary);
    color: white;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: transform 0.2s;
    margin-bottom: 2px;
    /* Align with single line input */
}

.send-button:hover {
    transform: scale(1.1);
    box-shadow: 0 2px 8px rgba(99, 102, 241, 0.4);
}


/* Login Page Overhaul */
.login-container {
    background: var(--cp-bg-main);
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    padding: 1rem;
}

.login-card {
    background: var(--cp-bg-card);
    padding: 2.5rem;
    border-radius: var(--cp-radius-lg);
    width: 100%;
    max-width: 400px;
    box-shadow: var(--cp-shadow);
    border: 1px solid var(--cp-border);
    animation: fadeInUp 0.4s ease-out;
}

@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.login-logo {
    text-align: center;
    margin-bottom: 2rem;
}

.login-logo i {
    font-size: 3rem;
    color: var(--cp-primary);
    margin-bottom: 0.5rem;
    display: inline-block;
}

.login-logo h1 {
    font-size: 1.5rem;
    font-weight: 700;
    margin: 0;
}

.login-logo p {
    color: var(--cp-text-muted);
    font-size: 0.9rem;
    margin-top: 0.5rem;
}

/* Form Elements */
.form-label {
    font-size: 0.85rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
    color: var(--cp-text);
    display: block;
}

.form-control,
.form-select {
    width: 100%;
    background: var(--cp-bg-input);
    border: 1px solid var(--cp-border);
    color: var(--cp-text);
    border-radius: var(--cp-radius-sm);
    padding: 0.6rem 1rem;
    font-size: 0.95rem;
    transition: border-color 0.2s;
}

.form-control:focus,
.form-select:focus {
    outline: none;
    border-color: var(--cp-primary);
    box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
}

/* Modals - Clean & Blur */
.modal-content {
    background: var(--cp-bg-card);
    border: 1px solid var(--cp-border);
    border-radius: var(--cp-radius-lg);
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
}

.modal-header {
    border-bottom: 1px solid var(--cp-border);
    padding: 1.25rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.modal-body {
    padding: 1.5rem;
}

.modal-footer {
    border-top: 1px solid var(--cp-border);
    padding: 1rem;
    display: flex;
    justify-content: flex-end;
    gap: 0.75rem;
}

.modal-backdrop.show {
    opacity: 0.5;
    backdrop-filter: blur(4px);
}

.modal-title {
    font-weight: 600;
    font-size: 1.1rem;
    margin: 0;
}

.btn-close {
    filter: invert(0.5);
    background-size: 0.8em;
}

[data-theme="dark"] .btn-close {
    filter: invert(1);
}


/* Badges & Utilities */
.badge {
    padding: 0.35em 0.65em;
    font-size: 0.75em;
    font-weight: 600;
    border-radius: 4px;
    display: inline-block;
}

.bg-primary {
    background-color: var(--cp-primary) !important;
    color: white;
}

.bg-success {
    background-color: var(--cp-success) !important;
    color: white;
}

.bg-danger {
    background-color: var(--cp-danger) !important;
    color: white;
}

.bg-warning {
    background-color: var(--cp-warning) !important;
    color: white;
}

.text-muted {
    color: var(--cp-text-muted) !important;
}

.d-none {
    display: none !important;
}

/* Responsive */
@media (max-width: 768px) {
    .chat-sidebar {
        position: fixed;
        left: -260px;
        height: 100%;
        transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        box-shadow: 10px 0 25px rgba(0, 0, 0, 0.1);
    }

    .chat-sidebar.open {
        left: 0;
    }

    .chat-main {
        width: 100%;
    }

    .sidebar-overlay {
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 9;
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.3s;
        backdrop-filter: blur(2px);
    }

    .chat-sidebar.open+.sidebar-overlay {
        opacity: 1;
        pointer-events: auto;
    }

    .message {
        max-width: 85%;
    }

    .btn-secondary span {
        display: none;
    }

    /* Hide text on mobile for secondary buttons in header */
    .btn-secondary i {
        margin: 0;
    }
}

/* Empty State */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--cp-text-muted);
    text-align: center;
    padding: 2rem;
}

.empty-state i {
    font-size: 3rem;
    margin-bottom: 1rem;
    opacity: 0.5;
    color: var(--cp-primary);
}

.empty-state h3 {
    font-size: 1.25rem;
    font-weight: 600;
    margin: 0 0 0.5rem 0;
    color: var(--cp-text);
}

.empty-state p {
    font-size: 0.9rem;
    margin: 0;
}