*,
*::before,
*::after {
    box-sizing: border-box;
}

:root {
    --bg: #0f1419;
    --surface: #1a2332;
    --surface-2: #243044;
    --border: #2d3a4f;
    --text: #e8edf4;
    --text-muted: #8b9cb3;
    --primary: #4f8cff;
    --primary-hover: #3d7aef;
    --success: #3ecf8e;
    --error: #ff6b6b;
    --radius: 12px;
    --font: "Segoe UI", system-ui, -apple-system, sans-serif;
}

body {
    margin: 0;
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
}

a {
    color: var(--primary);
    text-decoration: none;
}

/* Auth */
.auth-page {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1.5rem;
    background: radial-gradient(ellipse at top, #1a2a44 0%, var(--bg) 70%);
}

.auth-card {
    width: 100%;
    max-width: 400px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 2rem;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
}

.auth-card h1 {
    margin: 0 0 0.5rem;
    font-size: 1.5rem;
}

.subtitle {
    color: var(--text-muted);
    margin: 0 0 1.5rem;
    font-size: 0.95rem;
}

/* Forms */
.form {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.form label {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    font-size: 0.9rem;
    color: var(--text-muted);
}

.form input[type="text"],
.form input[type="password"] {
    padding: 0.75rem 1rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg);
    color: var(--text);
    font-size: 1rem;
    outline: none;
    transition: border-color 0.2s;
    font-family: inherit;
}

.form input:focus {
    border-color: var(--primary);
}

.form-inline {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: 1rem;
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.65rem 1.2rem;
    border: none;
    border-radius: 8px;
    font-size: 0.95rem;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s, opacity 0.2s;
    text-decoration: none;
}

.btn-primary {
    background: var(--primary);
    color: #fff;
}

.btn-primary:hover {
    background: var(--primary-hover);
}

.btn-secondary {
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border);
}

.btn-secondary:hover {
    background: var(--border);
}

.btn-ghost {
    background: transparent;
    color: var(--text-muted);
    border: 1px solid transparent;
}

.btn-ghost:hover {
    color: var(--text);
    background: var(--surface-2);
}

.btn-sm {
    padding: 0.35rem 0.75rem;
    font-size: 0.85rem;
}

/* Alerts */
.alert {
    padding: 0.75rem 1rem;
    border-radius: 8px;
    margin-bottom: 1rem;
    font-size: 0.9rem;
}

.alert-error {
    background: rgba(255, 107, 107, 0.15);
    border: 1px solid rgba(255, 107, 107, 0.3);
    color: var(--error);
}

.alert-success {
    background: rgba(62, 207, 142, 0.15);
    border: 1px solid rgba(62, 207, 142, 0.3);
    color: var(--success);
}

/* Chat header */
.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1.5rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    gap: 1rem;
    flex-wrap: wrap;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.chat-header h1 {
    margin: 0;
    font-size: 1.2rem;
}

.user-badge {
    background: var(--surface-2);
    padding: 0.25rem 0.75rem;
    border-radius: 20px;
    font-size: 0.85rem;
    color: var(--text-muted);
}

.header-right {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

/* Chat layout */
.chat-page {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

.chat-layout {
    flex: 1;
    display: flex;
    min-height: 0;
}

.contacts-panel {
    width: 280px;
    flex-shrink: 0;
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.contacts-title {
    margin: 0;
    padding: 1rem 1.25rem 0.75rem;
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.total-unread-badge {
    background: var(--error);
    color: #fff;
    font-size: 0.7rem;
    font-weight: 700;
    min-width: 1.25rem;
    height: 1.25rem;
    padding: 0 0.35rem;
    border-radius: 999px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    text-transform: none;
    letter-spacing: 0;
}

.contacts-empty {
    padding: 1rem 1.25rem;
    color: var(--text-muted);
    font-size: 0.9rem;
    line-height: 1.5;
}

.contacts-list {
    list-style: none;
    margin: 0;
    padding: 0 0.5rem 0.5rem;
    overflow-y: auto;
    flex: 1;
}

.contact-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem;
    border-radius: 8px;
    color: var(--text);
    transition: background 0.15s;
    position: relative;
}

.contact-item.has-unread {
    background: rgba(79, 140, 255, 0.08);
}

.contact-item.has-unread .contact-name {
    font-weight: 700;
    color: var(--text);
}

.unread-badge {
    margin-left: auto;
    flex-shrink: 0;
    background: var(--primary);
    color: #fff;
    font-size: 0.7rem;
    font-weight: 700;
    min-width: 1.35rem;
    height: 1.35rem;
    padding: 0 0.4rem;
    border-radius: 999px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.contact-item.has-unread .unread-badge {
    background: var(--error);
    animation: pulse-badge 1.5s ease infinite;
}

@keyframes pulse-badge {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.08); }
}

.contact-item:hover {
    background: var(--surface-2);
}

.contact-item.active {
    background: var(--surface-2);
    border: 1px solid var(--border);
}

.contact-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 1rem;
    flex-shrink: 0;
}

.contact-info {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.contact-name {
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.contact-meta {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.conversation-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    min-width: 0;
    position: relative;
}

.drop-overlay {
    position: absolute;
    inset: 0;
    z-index: 10;
    background: rgba(15, 20, 25, 0.85);
    border: 2px dashed var(--primary);
    border-radius: var(--radius);
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
}

.drop-overlay p {
    font-size: 1.2rem;
    color: var(--primary);
    font-weight: 600;
}

.conversation-panel .message-form {
    padding: 1rem 1.5rem 1.25rem;
    border-top: 1px solid var(--border);
    background: var(--surface);
}

.composer-shell {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 14px;
    overflow: hidden;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.composer-shell:focus-within {
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(79, 140, 255, 0.12);
}

.composer-toolbar {
    display: flex;
    gap: 0.5rem;
    padding: 0.5rem 0.75rem 0;
    margin-bottom: 0;
}

.btn-tool {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    padding: 0.4rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg);
    color: var(--text-muted);
    font-size: 0.85rem;
    cursor: pointer;
    transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.btn-tool:hover {
    background: var(--surface);
    color: var(--primary);
    border-color: rgba(79, 140, 255, 0.45);
}

.btn-tool-icon {
    font-family: Consolas, monospace;
    font-size: 0.8rem;
}

.composer-input-row {
    display: flex;
    gap: 0.75rem;
    align-items: flex-end;
    padding: 0.5rem 0.75rem 0.75rem;
}

.composer-input-row textarea,
#message-input {
    flex: 1;
    max-height: 200px;
    min-height: 2.75rem;
    padding: 0.65rem 0.85rem;
    border: none;
    border-radius: 8px;
    background: transparent;
    color: var(--text);
    font-size: 0.95rem;
    font-family: inherit;
    line-height: 1.45;
    resize: none;
    outline: none;
}

#message-input::placeholder {
    color: var(--text-muted);
    opacity: 0.85;
}

.btn-send {
    flex-shrink: 0;
    align-self: flex-end;
    min-width: 5.5rem;
}

.file-input-hidden {
    position: absolute;
    left: -9999px;
    width: 1px;
    height: 1px;
    opacity: 0;
}

.message-form {
    display: flex;
    flex-direction: column;
    gap: 0;
    position: relative;
}

.conversation-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
}

.conversation-header h2 {
    margin: 0;
    font-size: 1.1rem;
}

.conversation-empty {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    padding: 2rem;
    text-align: center;
}

.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    max-width: 800px;
    width: 100%;
    margin: 0 auto;
    padding: 1rem;
    min-height: 0;
}

.conversation-panel .messages {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    padding: 1rem 1.5rem;
    min-height: 0;
}

.chat-error {
    margin: 0 1.5rem 0.5rem;
}

.message {
    max-width: 80%;
    padding: 0.75rem 1rem;
    border-radius: var(--radius);
    background: var(--surface);
    border: 1px solid var(--border);
    animation: fadeIn 0.2s ease;
}

.message.own {
    align-self: flex-end;
    background: #1e3a5f;
    border-color: #2a5080;
}

.message.message-new {
    animation: messageHighlight 1.2s ease;
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(79, 140, 255, 0.25);
}

@keyframes messageHighlight {
    0% { background: rgba(79, 140, 255, 0.25); }
    100% { background: var(--surface); }
}

.message.message-new.own {
    animation-name: messageHighlightOwn;
}

@keyframes messageHighlightOwn {
    0% { background: rgba(79, 140, 255, 0.4); }
    100% { background: #1e3a5f; }
}

.message-header {
    display: flex;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 0.35rem;
    font-size: 0.8rem;
}

.message-author {
    font-weight: 600;
    color: var(--primary);
}

.message.own .message-author {
    color: #7eb8ff;
}

.message-time {
    color: var(--text-muted);
}

.message-content {
    line-height: 1.5;
    word-break: break-word;
    white-space: pre-wrap;
}

.code-block {
    margin: 0.5rem 0;
    border-radius: 8px;
    overflow: hidden;
    border: 1px solid var(--border);
    background: #0d1117;
}

.code-block-header {
    padding: 0.35rem 0.75rem;
    font-size: 0.75rem;
    color: var(--text-muted);
    background: #161b22;
    border-bottom: 1px solid var(--border);
    text-transform: lowercase;
}

.code-block pre {
    margin: 0;
    padding: 0.75rem 1rem;
    overflow-x: auto;
}

.code-block code {
    font-family: Consolas, "Courier New", monospace;
    font-size: 0.85rem;
    line-height: 1.45;
    color: #e6edf3;
    white-space: pre;
}

.file-attachment {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem 1rem;
    margin: 0.25rem 0;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text);
    text-decoration: none;
    transition: border-color 0.15s, background 0.15s;
}

.file-attachment:hover {
    border-color: var(--primary);
    background: var(--surface-2);
}

.file-icon {
    font-size: 1.5rem;
}

.file-details {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.file-name {
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.file-size {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.message-form-old {
    display: flex;
    gap: 0.75rem;
    padding-top: 0.5rem;
    border-top: 1px solid var(--border);
}

.message-form-old input {
    flex: 1;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(4px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Admin */
.admin-page {
    min-height: 100vh;
}

.admin-main {
    max-width: 900px;
    margin: 0 auto;
    padding: 1.5rem;
}

.admin-section {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.5rem;
    margin-bottom: 1.5rem;
}

.admin-section h2 {
    margin: 0 0 0.5rem;
    font-size: 1.1rem;
}

.hint {
    color: var(--text-muted);
    font-size: 0.9rem;
    margin: 0 0 1rem;
}

.users-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

.users-table th,
.users-table td {
    padding: 0.75rem;
    text-align: left;
    border-bottom: 1px solid var(--border);
}

.users-table th {
    color: var(--text-muted);
    font-weight: 500;
}

.status.active {
    color: var(--success);
}

.status.inactive {
    color: var(--error);
}

.inline-form {
    display: inline;
}

@media (max-width: 768px) {
    .chat-layout {
        flex-direction: column;
    }

    .contacts-panel {
        width: 100%;
        max-height: 40vh;
        border-right: none;
        border-bottom: 1px solid var(--border);
    }

    .conversation-panel:not(:has(.conversation-empty)) {
        flex: 1;
    }
}

@media (max-width: 600px) {
    .message {
        max-width: 95%;
    }

    .users-table {
        font-size: 0.8rem;
    }

    .users-table th:nth-child(4),
    .users-table td:nth-child(4),
    .users-table th:nth-child(5),
    .users-table td:nth-child(5) {
        display: none;
    }
}
