:root {
    color-scheme: light;
    --bg: #eef3f8;
    --surface: #ffffff;
    --surface-soft: #f7fafc;
    --surface-strong: #e8eef5;
    --text: #172033;
    --muted: #66758a;
    --border: #d8e0ea;
    --accent: #1769c2;
    --accent-dark: #0e4f99;
    --success: #16794c;
    --danger: #b42318;
    --warning: #b7791f;
    --shadow: 0 18px 50px rgba(23, 32, 51, 0.09);
    --radius: 8px;
    --page-max: 1180px;
    font-family: Inter, "Segoe UI", Roboto, Arial, sans-serif;
}

* {
    box-sizing: border-box;
}

html {
    min-height: 100%;
}

body {
    min-height: 100vh;
    margin: 0;
    color: var(--text);
    background: linear-gradient(180deg, #f8fbfd 0%, var(--bg) 100%);
    font-size: 15px;
    line-height: 1.5;
}

a {
    color: var(--accent);
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

h1,
h2,
h3,
p {
    margin-top: 0;
}

h1 {
    margin-bottom: 8px;
    font-size: clamp(1.55rem, 2vw, 2.1rem);
    line-height: 1.15;
    letter-spacing: 0;
}

h2 {
    margin-bottom: 14px;
    font-size: 1.05rem;
    line-height: 1.25;
    letter-spacing: 0;
}

p {
    color: var(--muted);
}

.auth-page,
.page {
    min-height: 100vh;
}

.auth-page {
    display: grid;
    place-items: center;
    padding: 22px;
}

.auth-card {
    width: min(100%, 430px);
    padding: 28px;
    border: 1px solid rgba(216, 224, 234, 0.9);
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: var(--shadow);
}

.auth-brand {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 42px;
    height: 42px;
    margin-bottom: 18px;
    border-radius: var(--radius);
    color: #ffffff;
    background: var(--accent);
    font-weight: 800;
}

.page-shell {
    width: min(100%, var(--page-max));
    margin: 0 auto;
    padding: 22px;
}

.page-header,
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
}

.page-header {
    margin-bottom: 18px;
}

.page-title p,
.title p {
    margin: 4px 0 0;
    color: var(--muted);
}

.nav,
.page-actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 10px;
}

.panel,
.card {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: 0 8px 22px rgba(23, 32, 51, 0.05);
}

.panel {
    padding: 20px;
}

.stack {
    display: grid;
    gap: 16px;
}

.mt-16 {
    margin-top: 16px;
}

.grid-2 {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 16px;
}

.form-grid {
    display: grid;
    gap: 14px;
}

.field {
    display: grid;
    gap: 6px;
}

label,
.field-label {
    color: #3c4a5f;
    font-size: 0.9rem;
    font-weight: 650;
}

input[type="text"],
input[type="password"],
input[type="url"],
input[type="file"],
select,
textarea {
    width: 100%;
    min-height: 42px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 9px 11px;
    color: var(--text);
    background: #ffffff;
    font: inherit;
    transition: border-color 0.16s ease, box-shadow 0.16s ease;
}

textarea {
    min-height: 96px;
    resize: vertical;
}

input[type="color"] {
    width: 58px;
    height: 38px;
    padding: 3px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: #ffffff;
}

input:focus,
select:focus,
textarea:focus {
    outline: none;
    border-color: rgba(23, 105, 194, 0.72);
    box-shadow: 0 0 0 3px rgba(23, 105, 194, 0.13);
}

.check-row {
    display: flex;
    align-items: center;
    gap: 9px;
    color: var(--text);
}

.help-text {
    margin: 2px 0 0;
    color: var(--muted);
    font-size: 0.88rem;
}

button,
.button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 40px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 8px 13px;
    color: var(--text);
    background: var(--surface);
    font: inherit;
    font-weight: 650;
    cursor: pointer;
    text-decoration: none;
    transition: background-color 0.16s ease, border-color 0.16s ease, color 0.16s ease;
}

button:hover,
.button:hover {
    border-color: #aebbd0;
    text-decoration: none;
}

.button.primary,
button.primary,
button[type="submit"] {
    border-color: var(--accent);
    color: #ffffff;
    background: var(--accent);
}

.button.primary:hover,
button.primary:hover,
button[type="submit"]:hover {
    border-color: var(--accent-dark);
    background: var(--accent-dark);
}

.button.danger,
button.danger {
    color: var(--danger);
}

button:disabled {
    cursor: not-allowed;
    opacity: 0.58;
}

.alert {
    margin-bottom: 14px;
    padding: 11px 12px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--surface-soft);
}

.alert.error {
    color: var(--danger);
    border-color: #f2c7c2;
    background: #fff5f4;
}

.alert.success {
    color: var(--success);
    border-color: #bfe5d1;
    background: #f2fbf6;
}

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

.list-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface-soft);
}

.inline-edit-form {
    display: grid;
    grid-template-columns: minmax(160px, 1fr) 120px auto;
    gap: 8px;
    align-items: center;
    width: 100%;
}

.inline-edit-form input {
    min-height: 38px;
}

.inline-edit-form button {
    min-height: 38px;
}

.inline-password-form {
    display: grid;
    grid-template-columns: minmax(150px, 1fr) auto;
    gap: 8px;
    align-items: center;
    min-width: 280px;
}

.inline-password-form input,
.inline-password-form button {
    min-height: 38px;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.table-wrap {
    overflow-x: auto;
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

table {
    width: 100%;
    border-collapse: collapse;
    min-width: 720px;
    background: var(--surface);
}

th,
td {
    padding: 11px 12px;
    border-bottom: 1px solid var(--border);
    text-align: left;
    vertical-align: middle;
}

th {
    color: #415066;
    background: var(--surface-soft);
    font-size: 0.84rem;
    font-weight: 750;
}

tr:last-child td {
    border-bottom: 0;
}

.swatch {
    display: inline-block;
    width: 24px;
    height: 16px;
    margin-right: 7px;
    border: 1px solid rgba(0, 0, 0, 0.12);
    border-radius: 4px;
    vertical-align: middle;
}

.app {
    min-height: 100vh;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
    background: var(--bg);
}

.topbar {
    gap: 10px;
    padding: 8px 14px;
    border-bottom: 1px solid var(--border);
    background: rgba(255, 255, 255, 0.94);
}

.topbar .nav {
    gap: 8px;
    font-size: 0.88rem;
}

.title h1 {
    margin: 0;
    font-size: 1.02rem;
    line-height: 1.2;
}

.status {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 4px 8px;
    border: 1px solid var(--border);
    border-radius: 999px;
    color: var(--muted);
    background: var(--surface-soft);
    font-size: 0.84rem;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--warning);
}

.status.is-online .status-dot {
    background: var(--success);
}

.status.is-offline .status-dot {
    background: var(--danger);
}

.chat-body {
    min-height: 0;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 260px;
}

.messages {
    overflow-y: auto;
    padding: 18px;
}

.message-list {
    max-width: 920px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

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

.online-panel {
    overflow-y: auto;
    border-left: 1px solid var(--border);
    background: #fbfdff;
    padding: 16px;
}

.online-panel h2 {
    margin: 0 0 12px;
    font-size: 0.95rem;
}

.online-count {
    color: var(--muted);
    font-weight: 400;
}

.online-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin: 0;
    padding: 0;
    list-style: none;
}

.online-user {
    display: grid;
    grid-template-columns: 32px minmax(0, 1fr);
    gap: 9px;
    align-items: center;
    min-height: 36px;
}

.online-avatar,
.avatar {
    border-radius: 50%;
    display: grid;
    place-items: center;
    overflow: hidden;
    border: 1px solid var(--border);
    color: #27364d;
    background: #d9e2ef;
    font-weight: 750;
}

.online-avatar {
    width: 32px;
    height: 32px;
    font-size: 0.82rem;
}

.avatar {
    width: 40px;
    height: 40px;
}

.online-avatar img,
.avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.online-name {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 0.9rem;
}

.message {
    display: grid;
    grid-template-columns: 40px minmax(0, 1fr);
    gap: 10px;
    align-items: start;
}

.message.is-own {
    grid-template-columns: minmax(0, 1fr) 40px;
}

.message.is-own .avatar {
    grid-column: 2;
    grid-row: 1;
}

.message.is-own .bubble {
    grid-column: 1;
    grid-row: 1;
    justify-self: end;
}

.bubble {
    position: relative;
    min-width: 0;
    max-width: 720px;
    padding: 11px 44px 11px 12px;
    border: 1px solid rgba(15, 23, 42, 0.12);
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: 0 1px 2px rgba(15, 23, 42, 0.05);
}

.message.is-own .bubble {
    padding-right: 12px;
    padding-left: 44px;
}

.meta {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: baseline;
    margin-bottom: 7px;
    color: var(--muted);
    font-size: 0.78rem;
}

.author {
    color: #111827;
    font-weight: 750;
}

.reply-preview {
    margin-bottom: 8px;
    padding: 8px 10px;
    border-left: 3px solid var(--accent);
    background: rgba(23, 105, 194, 0.08);
    color: #243044;
    font-size: 0.88rem;
}

.reply-preview strong {
    display: block;
    margin-bottom: 3px;
}

.body-text {
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.message-image {
    display: block;
    max-width: min(100%, 520px);
    max-height: 360px;
    margin-top: 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface-soft);
    object-fit: contain;
}

.image-link {
    display: inline-block;
    margin-top: 8px;
    overflow-wrap: anywhere;
}

.view-once-card {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    max-width: min(100%, 420px);
    margin-top: 10px;
    padding: 10px;
    border: 1px dashed var(--border);
    border-radius: var(--radius);
    color: var(--muted);
    background: rgba(255, 255, 255, 0.64);
    font-size: 0.9rem;
}

.view-once-card button {
    flex: 0 0 auto;
    min-height: 34px;
    padding: 7px 11px;
}

.view-once-card.is-viewed {
    justify-content: flex-start;
    opacity: 0.78;
}

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

.message.is-own .message-menu {
    right: auto;
    left: 8px;
}

.menu-trigger {
    min-width: 32px;
    min-height: 30px;
    padding: 4px 9px;
    border-color: transparent;
    color: var(--muted);
    background: rgba(255, 255, 255, 0.68);
}

.menu-trigger:hover,
.menu-trigger.is-open {
    border-color: var(--border);
    color: var(--text);
    background: #ffffff;
}

.context-menu {
    position: absolute;
    top: 34px;
    right: 0;
    z-index: 20;
    display: none;
    min-width: 150px;
    padding: 6px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: #ffffff;
    box-shadow: 0 16px 34px rgba(23, 32, 51, 0.16);
}

.message.is-own .context-menu {
    right: auto;
    left: 0;
}

.context-menu.is-open {
    display: grid;
    gap: 2px;
}

.context-menu button {
    justify-content: flex-start;
    width: 100%;
    min-height: 34px;
    border-color: transparent;
    padding: 7px 9px;
    background: transparent;
    font-size: 0.9rem;
}

.context-menu button:hover {
    border-color: transparent;
    background: var(--surface-soft);
}

.composer {
    border-top: 1px solid var(--border);
    background: rgba(255, 255, 255, 0.96);
}

.composer-inner {
    max-width: 920px;
    margin: 0 auto;
    padding: 7px 14px 9px;
}

.context-bar {
    display: none;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 7px;
    padding: 6px 8px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--muted);
    background: var(--surface-soft);
    font-size: 0.9rem;
}

.context-bar.is-visible {
    display: flex;
}

.context-bar strong {
    color: var(--text);
}

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

.message-field {
    min-width: 0;
}

.image-preview-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    margin-bottom: 7px;
    padding: 6px 8px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface-soft);
}

.image-preview-bar[hidden] {
    display: none;
}

.sr-only-file {
    position: absolute;
    width: 1px;
    height: 1px;
    opacity: 0;
    pointer-events: none;
}

.attach-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    margin: 0;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text);
    background: var(--surface);
    font-size: 1.3rem;
    font-weight: 750;
    line-height: 1;
    cursor: pointer;
}

.attach-button:hover {
    border-color: #aebbd0;
    background: var(--surface-soft);
}

.composer label {
    margin-bottom: 3px;
    font-size: 0.8rem;
}

.composer .attach-button {
    margin: 0;
    color: var(--text);
    font-size: 1.3rem;
    font-weight: 750;
}

.composer textarea {
    min-height: 40px;
    max-height: 112px;
    padding: 7px 9px;
    resize: none;
    overflow-y: auto;
}

.composer button {
    min-height: 40px;
    padding: 7px 10px;
}

.composer .send-button {
    min-width: 74px;
}

.check-row {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    color: var(--muted);
    font-size: 0.88rem;
}

.check-row input {
    width: 16px;
    height: 16px;
    margin: 0;
}

.image-note {
    flex: 1 1 180px;
    min-height: 1.2em;
    color: var(--muted);
    font-size: 0.82rem;
    overflow-wrap: anywhere;
}

.image-modal[hidden] {
    display: none;
}

.image-modal {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: grid;
    place-items: center;
    padding: 18px;
}

.image-modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(12, 18, 32, 0.76);
}

.image-modal-dialog {
    position: relative;
    z-index: 1;
    display: grid;
    max-width: min(94vw, 980px);
    max-height: 92vh;
}

.image-modal-dialog img {
    display: block;
    max-width: min(94vw, 980px);
    max-height: 88vh;
    border-radius: var(--radius);
    background: #ffffff;
    object-fit: contain;
    box-shadow: 0 24px 80px rgba(0, 0, 0, 0.32);
}

.image-modal-close {
    position: absolute;
    top: -12px;
    right: -12px;
    z-index: 2;
    width: 38px;
    height: 38px;
    min-height: 38px;
    padding: 0;
    border-radius: 999px;
    color: var(--text);
    background: #ffffff;
    box-shadow: 0 12px 28px rgba(0, 0, 0, 0.22);
}

body.has-modal {
    overflow: hidden;
}

@media (max-width: 860px) {
    .grid-2,
    .chat-body {
        grid-template-columns: 1fr;
    }

    .online-panel {
        order: -1;
        max-height: 150px;
        border-left: 0;
        border-bottom: 1px solid var(--border);
    }
}

@media (max-width: 720px) {
    body {
        font-size: 14px;
    }

    .auth-page {
        align-items: stretch;
        padding: 14px;
    }

    .auth-card,
    .panel {
        padding: 18px;
    }

    .page-shell {
        padding: 14px;
    }

    .page-header,
    .topbar {
        align-items: flex-start;
        flex-direction: column;
        gap: 8px;
    }

    .nav,
    .page-actions {
        width: 100%;
    }

    .nav a,
    .page-actions a,
    .page-actions .button {
        flex: 1 1 auto;
    }

    .messages {
        padding: 12px;
    }

    .list-item {
        align-items: stretch;
        flex-direction: column;
    }

    .inline-edit-form {
        grid-template-columns: 1fr;
    }

    .composer-inner {
        padding: 6px 8px calc(7px + env(safe-area-inset-bottom));
    }

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

    .composer textarea {
        min-height: 40px;
        max-height: 96px;
        padding: 8px 10px;
    }

    .composer button,
    .attach-button {
        min-height: 40px;
    }

    .composer .send-button {
        min-width: 58px;
        padding-right: 9px;
        padding-left: 9px;
    }

    .image-preview-bar,
    .context-bar {
        margin-bottom: 5px;
        padding: 5px 7px;
        font-size: 0.84rem;
    }

    .image-preview-bar .check-row {
        flex: 0 0 auto;
    }

    .image-preview-bar button,
    .context-bar button {
        min-height: 32px;
        padding: 5px 8px;
        font-size: 0.84rem;
    }

    .view-once-card {
        align-items: stretch;
        flex-direction: column;
    }
}
