:root {
    --bg: #f0f2f5;
    --card: #ffffff;
    --primary: #128c7e;
    --primary-dark: #075e54;
    --text: #1f2937;
    --muted: #6b7280;
    --border: #e5e7eb;
    --danger: #dc2626;
    --success: #059669;
}

* { box-sizing: border-box; }
body {
    margin: 0;
    font-family: Inter, Segoe UI, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
}
a { color: var(--primary-dark); text-decoration: none; }
.app-shell {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}
.topbar {
    display: flex; justify-content: space-between; align-items: center;
    padding: 14px 24px; background: var(--primary-dark); color: #fff;
    flex-shrink: 0;
}
.topbar a, .topbar .btn-link { color: #fff; margin-left: 16px; background: none; border: 0; cursor: pointer; }
.brand {
    display: flex; align-items: center; gap: 10px;
    font-weight: 700; font-size: 18px;
}
.brand-logo {
    width: 28px; height: 28px; object-fit: contain; flex-shrink: 0;
}
.nav-links { display: flex; align-items: center; flex-wrap: wrap; gap: 4px; }
.main-content {
    flex: 1;
    width: 100%;
    max-width: 1200px;
    margin: 0 auto;
    padding: 24px 16px;
}
.main-content--chat {
    max-width: none;
    padding: 12px 16px;
    display: flex;
    flex-direction: column;
    min-height: 0;
}
.main-content--chat .chat-app {
    flex: 1;
    min-height: 0;
}
.site-footer {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 14px 16px;
    background: #fff;
    border-top: 1px solid var(--border);
    color: var(--muted);
    font-size: 13px;
    text-align: center;
}
.footer-logo {
    width: 20px; height: 20px; object-fit: contain; flex-shrink: 0;
}
.card, .auth-card {
    background: var(--card); border-radius: 12px; padding: 20px;
    box-shadow: 0 8px 24px rgba(0,0,0,.06); margin-bottom: 20px;
}
.auth-card { max-width: 420px; width: 100%; margin: 0 auto; }
.auth-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: calc(100vh - 120px);
    padding: 24px 0;
}
.auth-logo-wrap { text-align: center; margin-bottom: 16px; }
.auth-logo {
    width: 72px; height: 72px; object-fit: contain;
}
.auth-card h1 { margin-top: 0; text-align: center; }
.auth-card .muted { text-align: center; }
.form-grid { display: grid; gap: 10px; }
.form-grid.two-col { grid-template-columns: 1fr 1fr; }
.form-grid label { font-size: 14px; font-weight: 600; }
.form-grid input, .form-grid textarea, .search-input {
    width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px;
}
.form-actions, .actions-row, .card-head { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.span-2 { grid-column: span 2; }
.btn, .btn-primary, .btn-success, .btn-info {
    display: inline-block; padding: 10px 16px; border-radius: 8px; border: 0; cursor: pointer;
    background: #e5e7eb; color: var(--text); font-weight: 600;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-success { background: var(--success); color: #fff; }
.btn-info { background: #0284c7; color: #fff; }
.btn-link { background: none; border: 0; color: var(--primary-dark); cursor: pointer; padding: 0; }
.btn-link.danger { color: var(--danger); }
.inline-form { display: inline; }
.alert { padding: 12px 14px; border-radius: 8px; margin-bottom: 16px; }
.alert-success { background: #d1fae5; color: #065f46; }
.alert-error { background: #fee2e2; color: #991b1b; }
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit,minmax(180px,1fr)); gap: 16px; margin-bottom: 20px; }
.stat-card { background: var(--card); border-radius: 12px; padding: 18px; box-shadow: 0 8px 24px rgba(0,0,0,.05); }
.stat-card span { display: block; color: var(--muted); font-size: 13px; }
.stat-card strong { font-size: 28px; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 10px; border-bottom: 1px solid var(--border); text-align: left; }
.muted { color: var(--muted); font-size: 13px; }
.code-block, .log-box {
    display: block; background: #111; color: #9fef9f; padding: 12px; border-radius: 8px;
    overflow: auto; word-break: break-all; font-size: 12px;
}
.checkbox-line { display: flex; align-items: center; gap: 8px; }

.chat-app {
    display: grid; grid-template-columns: 340px 1fr; gap: 0;
    height: calc(100vh - 130px);
    min-height: 480px;
    max-height: calc(100vh - 130px);
    background: var(--card); border-radius: 16px;
    box-shadow: 0 12px 32px rgba(0,0,0,.08);
    overflow: hidden;
}
.chat-sidebar {
    border-right: 1px solid var(--border);
    display: flex; flex-direction: column;
    background: #fff;
    min-height: 0;
    overflow: hidden;
}
.chat-sidebar-head { display: flex; justify-content: space-between; align-items: center; padding: 16px; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.search-input { margin: 12px; width: calc(100% - 24px); flex-shrink: 0; }
.conversation-list {
    overflow-y: auto;
    overflow-x: hidden;
    flex: 1 1 auto;
    min-height: 0;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}
.conversation-list::-webkit-scrollbar,
.chat-messages::-webkit-scrollbar {
    width: 8px;
}
.conversation-list::-webkit-scrollbar-track,
.chat-messages::-webkit-scrollbar-track {
    background: transparent;
}
.conversation-list::-webkit-scrollbar-thumb,
.chat-messages::-webkit-scrollbar-thumb {
    background: #c5c9ce;
    border-radius: 8px;
}
.conversation-list::-webkit-scrollbar-thumb:hover,
.chat-messages::-webkit-scrollbar-thumb:hover {
    background: #a8adb3;
}
.conv-item {
    padding: 14px 16px; border-bottom: 1px solid var(--border); cursor: pointer;
}
.conv-item:hover, .conv-item.active { background: #f9fafb; }
.conv-item .title { font-weight: 600; display: flex; justify-content: space-between; gap: 8px; }
.conv-item .preview { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge-unread {
    background: var(--primary); color: #fff; border-radius: 999px; font-size: 11px;
    padding: 2px 7px; min-width: 20px; text-align: center;
}
.chat-main {
    display: flex; flex-direction: column;
    background: #ece5dd;
    min-height: 0;
    overflow: hidden;
}
.chat-header { padding: 16px 20px; background: #f0f2f5; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.chat-messages {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}
.msg {
    max-width: 70%; padding: 10px 12px; border-radius: 12px; font-size: 14px; line-height: 1.45;
    box-shadow: 0 1px 1px rgba(0,0,0,.08);
}
.msg.in { align-self: flex-start; background: #fff; }
.msg.out { align-self: flex-end; background: #dcf8c6; }
.msg-meta { font-size: 11px; color: var(--muted); margin-top: 4px; }
.msg.failed { border: 1px solid var(--danger); }
.chat-notice { padding: 10px 16px; background: #fff7ed; color: #9a3412; border-top: 1px solid #fed7aa; flex-shrink: 0; }
.chat-form {
    display: grid; grid-template-columns: 1fr auto; gap: 10px; padding: 14px;
    background: #f0f2f5; border-top: 1px solid var(--border);
    flex-shrink: 0;
    align-items: end;
}
.chat-compose {
    display: flex;
    gap: 8px;
    align-items: flex-end;
}
.chat-compose textarea {
    flex: 1;
    resize: vertical;
    min-height: 44px;
    max-height: 120px;
}
.btn-attach {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 42px;
    height: 42px;
    padding: 0;
    font-size: 18px;
    cursor: pointer;
    flex-shrink: 0;
}
.msg-media { margin-bottom: 6px; }
.msg-media img {
    display: block;
    max-width: min(280px, 100%);
    border-radius: 10px;
    cursor: pointer;
}
.msg-media audio {
    width: min(280px, 100%);
    max-width: 100%;
}
.chat-form textarea {
    resize: vertical;
    min-height: 44px;
    max-height: 120px;
}
.hidden { display: none !important; }
.btn-icon { background: none; border: 0; font-size: 18px; cursor: pointer; }

@media (max-width: 900px) {
    .topbar {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
        padding: 12px 16px;
    }
    .brand { font-size: 16px; }
    .brand-logo { width: 24px; height: 24px; }
    .nav-links { margin-left: 0; }
    .topbar a, .topbar .btn-link { margin-left: 0; margin-right: 12px; }
    .chat-app {
        grid-template-columns: 1fr;
        height: calc(100vh - 160px);
        max-height: calc(100vh - 160px);
        min-height: 420px;
    }
    .chat-sidebar {
        max-height: 38vh;
        min-height: 180px;
        border-right: 0;
        border-bottom: 1px solid var(--border);
    }
    .main-content--chat { padding: 8px; }
    .form-grid.two-col { grid-template-columns: 1fr; }
    .span-2 { grid-column: span 1; }
    .site-footer { font-size: 12px; padding: 12px; }
}
