:root {
    --bg-color: #0f172a;
    --surface-color: #1e293b;
    --primary-color: #8b5cf6;
    --primary-hover: #7c3aed;
    --secondary-color: #10b981;
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --error-color: #ef4444;
    --msg-sent: #8b5cf6;
    --msg-recv: #334155;
    --msg-text-sent: #ffffff;
    --msg-text-recv: #f8fafc;
    --border-color: #334155;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

html, body {
    height: 100%;
}

body {
    background-color: var(--bg-color);
    color: var(--text-primary);
    display: flex;
    overflow: hidden;
    width: 100%;
}

.hidden { display: none !important; }

/* Views */
.view {
    display: none;
    width: 100%;
    height: 100%;
}
.view.active { 
    display: flex;
    min-width: 0;
    min-height: 0;
}

/* Login */
#login-view {
    justify-content: center;
    align-items: center;
}
.login-container {
    background-color: var(--surface-color);
    padding: 2.5rem;
    border-radius: 12px;
    width: 90%;
    max-width: 400px;
    text-align: center;
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border-color);
}
.login-container h1 { margin-bottom: 0.5rem; color: var(--primary-color); font-weight: 700; }
.login-container p { margin-bottom: 2.5rem; color: var(--text-secondary); }

input, select, textarea {
    width: 100%;
    padding: 12px 16px;
    margin-bottom: 1rem;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background-color: var(--bg-color);
    color: var(--text-primary);
    transition: all 0.2s ease;
}
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(139, 92, 246, 0.2);
}
button {
    width: 100%;
    padding: 12px 16px;
    background-color: var(--primary-color);
    color: #fff;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 600;
    transition: all 0.2s ease;
    box-shadow: var(--shadow-sm);
}
button:hover { background-color: var(--primary-hover); transform: translateY(-1px); box-shadow: var(--shadow-md); }
button:active { transform: translateY(0); }
button:disabled { opacity: 0.6; cursor: not-allowed; transform: none; }
.secondary-btn { background-color: var(--surface-color); border: 1px solid var(--border-color); color: var(--text-primary); }
.secondary-btn:hover { background-color: var(--bg-color); }

.error-msg { color: var(--error-color); min-height: 20px; font-size: 0.9rem; }

/* App View - Sidebar */
.sidebar {
    width: 300px;
    background-color: var(--surface-color);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
}
.sidebar header {
    padding: 1rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.actions button {
    width: auto;
    padding: 6px 12px;
    font-size: 0.8rem;
    margin-left: 5px;
}
#chat-list {
    list-style: none;
    overflow-y: auto;
    flex: 1;
}
#chat-list li {
    padding: 1rem;
    border-bottom: 1px solid var(--border-color);
    cursor: pointer;
}
#chat-list li:hover, #chat-list li.active {
    background-color: rgba(255, 255, 255, 0.05);
}

/* Chat Area */
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    background-color: var(--bg-color);
    min-width: 0;
    min-height: 0;
}
.chat-header {
    padding: 1rem;
    background-color: var(--surface-color);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
}
.mobile-only { display: none; margin-right: 1rem; background: transparent; color: var(--text-primary); width: auto; font-size: 1.5rem; }

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

/* Message Bubbles */
.msg-wrapper { display: flex; margin-bottom: 1rem; }
.msg-wrapper.sent { justify-content: flex-end; }
.msg-wrapper.recv { justify-content: flex-start; }

.msg-bubble {
    max-width: 75%;
    padding: 12px 16px;
    border-radius: 18px;
    word-wrap: break-word;
    position: relative;
    line-height: 1.4;
    box-shadow: var(--shadow-sm);
}
.msg-wrapper.sent .msg-bubble {
    background-color: var(--msg-sent);
    color: var(--msg-text-sent);
    border-bottom-right-radius: 4px;
}
.msg-wrapper.recv .msg-bubble {
    background-color: var(--msg-recv);
    color: var(--msg-text-recv);
    border-bottom-left-radius: 4px;
}
.msg-img { max-width: 100%; border-radius: 8px; margin-bottom: 5px; }

.chat-input-area {
    padding: 1rem;
    background-color: var(--surface-color);
    border-top: 1px solid var(--border-color);
    display: flex;
    gap: 10px;
}
#btn-attach {
    width: 50px;
    background-color: transparent;
    color: var(--text-primary);
    font-size: 1.5rem;
}
.chat-input-area input[type="text"] {
    margin-bottom: 0;
    flex: 1;
    border-radius: 20px;
    padding: 12px 20px;
}
#btn-send {
    width: auto;
    padding: 12px 24px;
    border-radius: 20px;
}

/* Admin Modal */
.modal {
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.8);
    display: flex; justify-content: center; align-items: center;
    z-index: 1000;
}
.modal-content {
    background: var(--surface-color);
    padding: 2rem;
    border-radius: 8px;
    width: 90%; max-width: 500px;
    position: relative;
}
.close-btn { position: absolute; top: 10px; right: 20px; font-size: 1.5rem; cursor: pointer; }
.admin-section { margin-top: 1.5rem; }
.danger-btn { background-color: var(--error-color); color: white; }

/* Responsive */
@media (max-width: 768px) {
    .sidebar { width: 100%; }
    .chat-area { display: none; }
    
    /* When chat is active on mobile */
    body.chat-active .sidebar { display: none; }
    body.chat-active .chat-area { display: flex; }
    .mobile-only { display: block; }
    
    .tools-sidebar {
        display: none !important;
    }
    
    body.tools-active .tools-sidebar {
        display: flex !important;
        position: fixed !important;
        width: 100vw !important;
        height: 100vh !important;
        top: 0;
        right: 0;
        z-index: 1000;
        background-color: var(--surface-color);
    }
}

/* Admin Tabs */
.admin-tabs {
    display: flex;
    border-bottom: 1px solid var(--border-color);
    margin-bottom: 1rem;
}
.tab-btn {
    flex: 1;
    background: transparent;
    color: var(--text-secondary);
    border-radius: 0;
}
.tab-btn.active {
    color: var(--primary-color);
    border-bottom: 2px solid var(--primary-color);
}
.admin-tab-content { display: none; }
.admin-tab-content.active { display: block; }
.user-item { padding: 5px 0; border-bottom: 1px solid #333; display: flex; justify-content: space-between;}

/* Chat List Enhancements */
#chat-list li {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.chat-info {
    flex-grow: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.unread-badge {
    background-color: var(--primary-color);
    color: white;
    font-size: 0.75rem;
    padding: 2px 6px;
    border-radius: 10px;
    font-weight: bold;
}

.delete-chat {
    cursor: pointer;
    opacity: 0.6;
    transition: opacity 0.2s;
}

.delete-chat:hover {
    opacity: 1;
    color: var(--error-color);
}

/* Loading Overlay */
#chat-loading {
    position: absolute;
    top: 60px; /* Below header */
    left: 0;
    right: 0;
    bottom: 60px; /* Above input */
    background: rgba(0,0,0,0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 10;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 4px solid rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    border-top-color: var(--primary-color);
    animation: spin 1s ease-in-out infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Admin View Styles */
#admin-view {
    flex-direction: row;
    height: 100%;
    background-color: var(--bg-color);
}
#admin-view.hidden {
    display: none !important;
}

.admin-sidebar {
    width: 250px;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    height: 100%;
}
.admin-sidebar header {
    padding: 1.5rem;
    border-bottom: 1px solid var(--border-color);
}
.admin-sidebar header h2 {
    margin: 0;
    color: var(--primary-color);
}
.admin-nav {
    display: flex;
    flex-direction: column;
    padding: 1rem 0;
    gap: 4px;
}
.admin-nav .tab-btn {
    padding: 0.8rem 1.5rem;
    text-align: left;
    background: transparent;
    border: none;
    color: var(--text-color);
    font-size: 1rem;
    cursor: pointer;
    border-radius: 0;
    width: 100%;
    transition: background 0.2s, color 0.2s, padding-left 0.2s;
}
.admin-nav .tab-btn:hover {
    background: rgba(255,255,255,0.05);
    padding-left: 2rem;
}
.admin-nav .tab-btn.active {
    background: rgba(var(--primary-color-rgb), 0.1);
    color: var(--primary-color);
    border-left: 4px solid var(--primary-color);
}
.admin-nav .danger-tab.active {
    background: rgba(255, 68, 68, 0.1);
    color: var(--error-color);
    border-left: 4px solid var(--error-color);
}
.admin-sidebar-footer {
    padding: 1rem;
    border-top: 1px solid var(--border-color);
    margin-top: auto;
}

.admin-main {
    flex: 1;
    padding: 2rem;
    overflow-y: auto;
    background: var(--bg-color);
}

.admin-card {
    background: var(--surface-color);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 2rem;
    max-width: 800px;
    margin: 0 auto;
    box-shadow: var(--shadow-md);
}

.admin-form input,
.admin-form select {
    width: 100%;
    padding: 12px;
    margin-bottom: 1rem;
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    color: var(--text-color);
}
.checkbox-label {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 1.5rem;
}
.admin-form input[type="checkbox"] {
    width: auto;
    margin-bottom: 0;
}

.admin-list {
    list-style: none;
    padding: 0;
    max-height: 400px;
    overflow-y: auto;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--bg-color);
}
.admin-list .user-item {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: background 0.2s;
}
.admin-list .user-item:hover {
    background: rgba(255, 255, 255, 0.02);
}
.admin-list .user-item:last-child {
    border-bottom: none;
}
.list-select .user-item {
    justify-content: flex-start;
    padding: 0;
}
.list-select .user-item label {
    display: flex;
    align-items: center;
    gap: 12px;
    cursor: pointer;
    width: 100%;
    padding: 12px 16px;
}
.list-select .user-item input[type="checkbox"] {
    width: 18px;
    height: 18px;
    accent-color: var(--primary-color);
    cursor: pointer;
}

.admin-select {
    width: 100%;
    padding: 12px;
    margin-bottom: 1.5rem;
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    color: var(--text-color);
    font-size: 1rem;
    outline: none;
}
.admin-select:focus {
    border-color: var(--primary-color);
}

.audit-chat-area {
    min-height: 400px;
    max-height: 600px;
    overflow-y: auto;
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 1.5rem;
}

.danger-card {
    border-color: rgba(255, 68, 68, 0.3);
    background: linear-gradient(145deg, var(--bg-secondary) 0%, rgba(255, 68, 68, 0.05) 100%);
}
.danger-text {
    color: var(--error-color);
}
.help-text {
    color: var(--text-secondary);
    font-size: 0.9rem;
    margin-bottom: 1.5rem;
}
.msg-box {
    margin-top: 1rem;
    font-size: 0.9rem;
}

/* Admin Panel Mobile Responsiveness */
@media (max-width: 768px) {
    #admin-view {
        flex-direction: column;
    }
    
    .admin-sidebar {
        width: 100%;
        height: auto;
        border-right: none;
        border-bottom: 1px solid var(--border-color);
    }
    
    .admin-sidebar header {
        padding: 1rem;
    }

    .admin-nav {
        flex-direction: row;
        flex-wrap: wrap;
        padding: 0.5rem;
        gap: 0.5rem;
    }
    
    .admin-nav .tab-btn {
        width: auto;
        flex: 1 1 calc(50% - 0.5rem);
        padding: 0.8rem;
        text-align: center;
        border-left: none;
        border-bottom: 3px solid transparent;
    }
    
    .admin-nav .tab-btn:hover {
        padding-left: 0.8rem;
    }
    
    .admin-nav .tab-btn.active {
        border-left: none;
        border-bottom: 3px solid var(--primary-color);
    }
    
    .admin-nav .danger-tab.active {
        border-left: none;
        border-bottom: 3px solid var(--error-color);
    }
    
    .admin-sidebar-footer {
        padding: 1rem;
        border-top: none;
        margin-top: 0;
    }
    
    .admin-main {
        padding: 1rem;
    }
    
    .admin-card {
        padding: 1.5rem 1rem;
    }
}

/* Tools Sidebar */
.tools-sidebar {
    width: 33.333%;
    background-color: var(--surface-color);
    border-left: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
}

.tools-tabs {
    display: flex;
    border-bottom: 1px solid var(--border-color);
}
.tool-tab-btn {
    flex: 1;
    background: transparent;
    color: var(--text-secondary);
    border-radius: 0;
    padding: 10px;
    font-size: 0.9rem;
}
.tool-tab-btn.active {
    color: var(--primary-color);
    border-bottom: 2px solid var(--primary-color);
}
.tool-content {
    display: none;
    flex-direction: column;
    padding: 1rem;
    flex: 1;
    overflow-y: auto;
}
.tool-content.active {
    display: flex;
}

.tools-sidebar pre {
    white-space: pre-wrap;       /* Since CSS 2.1 */
    white-space: -moz-pre-wrap;  /* Mozilla, since 1999 */
    white-space: -pre-wrap;      /* Opera 4-6 */
    white-space: -o-pre-wrap;    /* Opera 7 */
    word-wrap: break-word;       /* Internet Explorer 5.5+ */
    overflow-x: hidden;
    background-color: var(--bg-color);
    padding: 10px;
    border-radius: 4px;
    font-size: 0.85rem;
}
