* { box-sizing: border-box; }

:root {
    --bg-app: #eef2f8;
    --bg-card: #ffffff;
    --bg-header-1: #071a37;
    --bg-header-2: #0f3f97;
    --line: #dbe4ef;
    --text: #0f172a;
    --muted: #64748b;
    --accent: #2563eb;
    --accent-strong: #1d4ed8;
    --success: #22c55e;
    --success-soft: #dcfce7;
    --bubble-mine: #dcf8c6;
    --bubble-other: #ffffff;
    --shadow: 0 16px 40px rgba(15, 23, 42, 0.10);
    --radius-xl: 24px;
    --radius-lg: 18px;
    --radius-md: 14px;
    --top-space: env(safe-area-inset-top, 0px);
    --bottom-space: env(safe-area-inset-bottom, 0px);
}

html, body {
    margin: 0;
    height: 100%;
    overflow: hidden;
}

body {
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    font-family: Arial, Helvetica, sans-serif;
    color: var(--text);
    background: linear-gradient(180deg, #edf3ff 0%, #f7f9fc 100%);
}

a { color: inherit; }
img { max-width: 100%; }
.hidden { display: none !important; }

.topbar {
    flex: 0 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    padding: calc(10px + var(--top-space)) 18px 12px;
    background: linear-gradient(135deg, var(--bg-header-1) 0%, #0a2e6a 52%, var(--bg-header-2) 100%);
    color: #fff;
    box-shadow: 0 10px 28px rgba(2, 12, 27, 0.22);
}

.brand-block {
    display: flex;
    align-items: center;
    gap: 14px;
    min-width: 0;
}

.brand-logo {
    width: 46px;
    height: 46px;
    border-radius: 14px;
    flex: 0 0 46px;
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.18);
}

.brand-copy h1 {
    margin: 0;
    font-size: 27px;
    line-height: 1.06;
    letter-spacing: -0.02em;
}

.subtitle {
    margin: 4px 0 0;
    font-size: 13px;
    color: #bfdbfe;
}

.topbar-actions {
    display: flex;
    align-items: center;
    gap: 16px;
}

.user-badge {
    display: flex;
    flex-direction: column;
    text-align: right;
}

.user-badge strong {
    font-size: 14px;
}

.user-badge span {
    color: #bfdbfe;
    font-size: 12px;
}

.topnav {
    display: flex;
    gap: 10px;
}

.topnav a,
.btn-primary,
.btn-secondary {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 42px;
    padding: 10px 16px;
    border-radius: 13px;
    text-decoration: none;
    border: none;
    cursor: pointer;
    font-weight: 700;
    transition: transform .18s ease, box-shadow .18s ease, background .18s ease;
}

.topnav a {
    color: #fff;
    background: rgba(255, 255, 255, 0.12);
    border: 1px solid rgba(255, 255, 255, 0.12);
}

.topnav a:hover,
.btn-primary:hover,
.btn-secondary:hover {
    transform: translateY(-1px);
}

.btn-primary {
    background: linear-gradient(135deg, var(--accent), var(--accent-strong));
    color: #fff;
    box-shadow: 0 10px 20px rgba(37, 99, 235, 0.22);
}

.btn-secondary {
    background: #e2e8f0;
    color: var(--text);
}

.main-wrapper {
    flex: 1 1 auto;
    min-height: 0;
    padding: 16px;
    overflow: hidden;
}

.auth-card,
.card,
.sidebar-shell,
.chat-panel {
    background: rgba(255, 255, 255, 0.96);
    border: 1px solid rgba(148, 163, 184, 0.16);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow);
}

.auth-card,
.card { padding: 24px; }
.auth-card { max-width: 420px; margin: 36px auto; }
.form-card { display: grid; gap: 12px; }
.form-card input,
.form-card select,
.search-input,
textarea {
    width: 100%;
    padding: 12px 14px;
    border: 1px solid #cbd5e1;
    border-radius: 14px;
    font-size: 14px;
    background: #fff;
    color: var(--text);
}

.form-card input:focus,
.form-card select:focus,
.search-input:focus,
textarea:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.12);
}

.info-box,
.alert {
    margin-top: 16px;
    padding: 14px;
    border-radius: 14px;
}

.info-box { background: #eff6ff; color: #1e3a8a; }
.alert-success { background: #ecfdf5; color: #065f46; }
.alert-error { background: #fef2f2; color: #991b1b; }

.chat-layout {
    height: 100%;
    min-height: 0;
    display: grid;
    grid-template-columns: 320px minmax(0, 1fr);
    gap: 16px;
}

.sidebar,
.chat-panel {
    min-height: 0;
}

.sidebar-shell {
    height: 100%;
    min-height: 0;
    display: flex;
    flex-direction: column;
    padding: 18px;
    overflow: hidden;
}

.sidebar-header h2,
.chat-header h2,
.card h2,
.group-create-card h3 {
    margin: 0 0 6px;
}

.sidebar-header p,
.chat-header p,
.group-create-card p {
    margin: 0;
    color: var(--muted);
}

.sidebar-tabs {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin: 18px 0 14px;
}

.tab-link {
    text-align: center;
    text-decoration: none;
    color: #334155;
    padding: 12px 14px;
    border-radius: 999px;
    background: #e8edf5;
    font-weight: 700;
}

.tab-link.active {
    background: linear-gradient(135deg, var(--accent), var(--accent-strong));
    color: #fff;
    box-shadow: 0 10px 18px rgba(37, 99, 235, 0.18);
}

.sidebar-search-wrap {
    margin-bottom: 14px;
}

.sidebar-list-wrap {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    padding-right: 4px;
}

.user-list {
    list-style: none;
    padding: 0;
    margin: 0;
    display: grid;
    gap: 10px;
}

.user-item a {
    display: grid;
    grid-template-columns: 48px minmax(0, 1fr) auto;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    text-decoration: none;
    color: var(--text);
    border-radius: 18px;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
}

.user-item a.active,
.user-item a:hover {
    background: #e9f1ff;
    border-color: #bfdbfe;
}

.user-avatar,
.chat-avatar {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: #dbeafe;
    color: #1e40af;
    font-weight: 700;
    font-size: 20px;
    flex: 0 0 48px;
}

.group-avatar {
    background: #dcfce7;
    color: #15803d;
}

.user-copy,
.chat-header-copy {
    min-width: 0;
}

.user-copy strong,
.chat-header-copy h2 {
    display: block;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.user-copy span,
.user-item small {
    display: block;
    color: var(--muted);
    margin-top: 4px;
}

.group-create-card {
    margin-bottom: 14px;
    padding: 14px;
    border-radius: 18px;
    background: linear-gradient(180deg, #f8fbff, #ffffff);
    border: 1px solid #e2e8f0;
}

.group-create-card p { margin-bottom: 12px; }
.compact-form { gap: 10px; }
.field-label { font-size: 13px; color: #475569; font-weight: 700; }
.member-picker {
    display: grid;
    gap: 8px;
    max-height: 180px;
    overflow-y: auto;
    padding: 10px;
    border-radius: 14px;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
}
.member-option { display: flex; align-items: center; gap: 10px; font-size: 14px; }

.chat-panel {
    height: 100%;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: #f0f2f5;
}

.chat-header {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 18px;
    border-bottom: 1px solid var(--line);
    background: #f8fafc;
}

.chat-header-copy p {
    margin-top: 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-body {
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
    background:
        radial-gradient(circle at top left, rgba(255, 255, 255, 0.45), transparent 28%),
        radial-gradient(circle at bottom right, rgba(255, 255, 255, 0.28), transparent 24%),
        linear-gradient(180deg, #efeae2 0%, #e8e2d8 100%);
}

.chat-messages {
    height: 100%;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 16px 18px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    scroll-behavior: smooth;
}

.message-row {
    display: flex;
    width: 100%;
}

.message-row.mine { justify-content: flex-end; }
.message-row.theirs { justify-content: flex-start; }

.message-card {
    position: relative;
    display: inline-flex;
    flex-direction: column;
    max-width: min(76%, 580px);
    padding: 10px 12px 8px;
    border-radius: 16px;
    box-shadow: 0 3px 10px rgba(15, 23, 42, 0.10);
    word-break: break-word;
}

.message-row.mine .message-card {
    background: var(--bubble-mine);
    border-bottom-right-radius: 5px;
}

.message-row.theirs .message-card {
    background: var(--bubble-other);
    border-bottom-left-radius: 5px;
}

.sender-name {
    display: block;
    margin-bottom: 6px;
    font-size: 13px;
    font-weight: 700;
    color: #0f5132;
}

.message-text {
    margin: 0;
    line-height: 1.42;
    white-space: pre-wrap;
}

.deleted-text {
    margin: 0;
    color: #5b6474;
    font-style: italic;
}

.message-footer {
    display: flex;
    align-items: flex-end;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 6px;
    font-size: 11px;
    color: rgba(15, 23, 42, 0.64);
}

.message-meta { white-space: nowrap; }
.message-row.mine .message-meta { color: rgba(15, 23, 42, 0.7); }

.message-menu {
    position: absolute;
    top: 6px;
    right: 6px;
}

.message-menu-toggle,
.attach-toggle,
.clear-file-btn,
.send-button {
    border: none;
    cursor: pointer;
}

.message-menu-toggle {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: rgba(15, 23, 42, 0.08);
    color: #334155;
    font-size: 18px;
    line-height: 1;
}

.message-row.mine .message-menu-toggle {
    background: rgba(15, 23, 42, 0.08);
}

.message-menu-dropdown,
.attach-dropdown {
    position: absolute;
    min-width: 180px;
    padding: 8px;
    border-radius: 18px;
    background: #111827;
    box-shadow: 0 18px 42px rgba(15, 23, 42, 0.24);
    display: none;
    z-index: 30;
}

.message-menu.open .message-menu-dropdown,
.attach-menu.open .attach-dropdown {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.message-menu-dropdown {
    top: 32px;
    right: 0;
}

.attach-dropdown {
    left: 0;
    bottom: 56px;
}

.message-menu-item,
.attach-option {
    width: 100%;
    padding: 11px 12px;
    border-radius: 12px;
    border: none;
    background: transparent;
    color: #fff;
    text-align: left;
    cursor: pointer;
    font-size: 14px;
}

.message-menu-item:hover,
.attach-option:hover {
    background: rgba(255, 255, 255, 0.08);
}

.message-menu-item.danger { color: #fecaca; }

.attachment-image-link {
    display: block;
    margin-top: 8px;
}

.attachment-image {
    display: block;
    max-width: 280px;
    max-height: 320px;
    border-radius: 14px;
    object-fit: cover;
    border: 1px solid rgba(15, 23, 42, 0.06);
}

.attachment-file {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    margin-top: 8px;
    padding: 10px 12px;
    border-radius: 14px;
    text-decoration: none;
    background: rgba(15, 23, 42, 0.06);
    color: var(--text);
    font-weight: 700;
}

.message-row.mine .attachment-file {
    background: rgba(255, 255, 255, 0.5);
}

.empty-state {
    margin: auto;
    text-align: center;
    padding: 30px;
    color: var(--muted);
}

.full-empty-state {
    display: flex;
    flex: 1 1 auto;
    align-items: center;
    justify-content: center;
    flex-direction: column;
}

.empty-logo {
    width: 88px;
    height: 88px;
    margin-bottom: 18px;
}

.chat-composer {
    flex: 0 0 auto;
    border-top: 1px solid var(--line);
    background: #f0f2f5;
    padding: 12px 14px calc(12px + var(--bottom-space));
}

.chat-form {
    display: block;
}

.composer-main {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    align-items: end;
    gap: 10px;
}

.attach-menu {
    position: relative;
    align-self: end;
}

.attach-toggle {
    width: 46px;
    height: 46px;
    border-radius: 50%;
    background: linear-gradient(135deg, #22c55e, #16a34a);
    color: #fff;
    box-shadow: 0 8px 20px rgba(34, 197, 94, 0.25);
    font-size: 28px;
    line-height: 1;
}

.composer-input-shell {
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 8px;
    background: #fff;
    border: 1px solid #d3dce7;
    border-radius: 20px;
    padding: 10px 14px;
}

.composer-input-shell textarea {
    border: none;
    border-radius: 0;
    box-shadow: none !important;
    resize: none;
    padding: 0;
    min-height: 22px;
    max-height: 110px;
    line-height: 1.4;
    background: transparent;
}

.attachment-status {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.attachment-preview-box {
    width: 52px;
    height: 52px;
    border-radius: 12px;
    overflow: hidden;
    background: #e2e8f0;
    flex: 0 0 52px;
}

.attachment-preview-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.file-chip {
    display: inline-flex;
    align-items: center;
    min-height: 34px;
    padding: 8px 12px;
    border-radius: 999px;
    background: #eef2ff;
    color: #334155;
    font-size: 13px;
    border: 1px solid #dbe4ef;
    max-width: min(100%, 360px);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.clear-file-btn {
    min-height: 34px;
    padding: 8px 12px;
    border-radius: 999px;
    background: #fee2e2;
    color: #991b1b;
    font-weight: 700;
}

.send-button {
    width: 46px;
    height: 46px;
    border-radius: 50%;
    align-self: end;
    background: linear-gradient(135deg, var(--accent), var(--accent-strong));
    color: #fff;
    font-size: 19px;
    box-shadow: 0 10px 24px rgba(37, 99, 235, 0.24);
}

.admin-grid {
    display: grid;
    grid-template-columns: 360px 1fr;
    gap: 20px;
}

.card.wide { overflow: hidden; }
.table-wrapper { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 12px; border-bottom: 1px solid #e2e8f0; font-size: 14px; }
.checkbox-label { display: flex; align-items: center; gap: 10px; }
.button-row { display: flex; gap: 12px; flex-wrap: wrap; }
.single-form { display: flex; justify-content: center; }
.form-narrow { width: 100%; max-width: 520px; }

@media (max-width: 1100px) {
    .chat-layout {
        grid-template-columns: 280px minmax(0, 1fr);
    }
}

@media (max-width: 900px) {
    body { overflow: hidden; }

    .topbar {
        align-items: flex-start;
        flex-direction: column;
        padding: calc(10px + var(--top-space)) 14px 12px;
    }

    .topbar-actions {
        width: 100%;
        justify-content: space-between;
        gap: 12px;
    }

    .user-badge {
        text-align: left;
        min-width: 0;
    }

    .main-wrapper {
        padding: 10px;
    }

    .chat-layout,
    .admin-grid {
        grid-template-columns: 1fr;
        grid-template-rows: minmax(240px, 34%) minmax(0, 1fr);
        gap: 10px;
    }

    .sidebar-shell,
    .chat-panel {
        border-radius: 22px;
    }

    .sidebar-shell {
        padding: 14px;
    }

    .chat-header {
        padding: 12px 14px;
    }

    .chat-messages {
        padding: 14px 12px;
    }

    .message-card {
        max-width: 88%;
    }

    .composer-main {
        grid-template-columns: auto minmax(0, 1fr) auto;
        gap: 8px;
    }

    .attach-toggle,
    .send-button {
        width: 42px;
        height: 42px;
    }

    .composer-input-shell {
        padding: 9px 12px;
        border-radius: 18px;
    }

    .attachment-preview-box {
        width: 46px;
        height: 46px;
        flex-basis: 46px;
    }
}

@media (max-width: 640px) {
    .brand-copy h1 {
        font-size: 20px;
    }

    .subtitle {
        font-size: 12px;
    }

    .topbar-actions {
        flex-direction: row;
        align-items: center;
    }

    .topnav {
        gap: 8px;
    }

    .topnav a {
        min-height: 38px;
        padding: 8px 12px;
        font-size: 13px;
    }

    .chat-layout {
        grid-template-rows: minmax(200px, 32%) minmax(0, 1fr);
    }

    .sidebar-tabs {
        margin: 14px 0 12px;
    }

    .user-item a {
        grid-template-columns: 42px minmax(0, 1fr) auto;
        padding: 10px 12px;
    }

    .user-avatar,
    .chat-avatar {
        width: 42px;
        height: 42px;
        font-size: 18px;
        flex-basis: 42px;
    }

    .chat-header-copy h2 {
        font-size: 18px;
    }

    .chat-header-copy p {
        font-size: 13px;
    }

    .message-card {
        max-width: 92%;
        padding: 10px 10px 8px;
    }

    .attachment-image {
        max-width: 100%;
        max-height: 260px;
    }

    .message-menu-dropdown {
        right: -4px;
        min-width: 156px;
    }

    .attach-dropdown {
        left: -6px;
        min-width: 190px;
    }

    .file-chip {
        max-width: 100%;
    }
}
