:root {
    --bg: #111318;
    --panel: #181b22;
    --panel-2: #20242d;
    --panel-3: #252a34;
    --border: rgba(255,255,255,0.08);
    --text: #f5f7fb;
    --muted: #9aa3b2;
    --muted-2: #6f7a8d;
    --primary: #475569;
    --primary-2: #64748b;
    --danger: #ff5573;
    --success: #50d890;
    --bubble-agent: #1f6f4a;
    --bubble-mine: #2563eb;
    --radius: 18px;
    --header-height: 58px;
    --composer-min: 74px;
    color-scheme: dark;
}

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html,
body {
    margin: 0;
    height: 100%;
    width: 100%;
    overflow: hidden;
    background: var(--bg);
    color: var(--text);
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
textarea {
    font: inherit;
}

button {
    cursor: pointer;
}

.hidden {
    display: none !important;
}

.app-shell,
.chat-view,
.chat-screen {
    height: 100dvh;
    width: 100%;
}

.login-view {
    height: 100dvh;
    width: 100%;
    display: grid;
    place-items: center;
    padding: 24px;
    background:
        radial-gradient(circle at top left, rgba(71,85,105,0.28), transparent 35%),
        radial-gradient(circle at bottom right, rgba(80,216,144,0.10), transparent 35%),
        var(--bg);
}

.login-card {
    width: min(100%, 420px);
    background: rgba(24,27,34,0.96);
    border: 1px solid var(--border);
    border-radius: 28px;
    padding: 28px;
    box-shadow: 0 24px 80px rgba(0,0,0,0.35);
}

.brand-mark {
    width: 58px;
    height: 58px;
    display: grid;
    place-items: center;
    border-radius: 20px;
    background: linear-gradient(135deg, #334155, #1f6f4a);
    color: white;
    font-weight: 800;
    letter-spacing: 0.5px;
    margin-bottom: 18px;
}

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

h1 {
    font-size: 28px;
    margin-bottom: 8px;
}

h2 {
    font-size: 20px;
    margin-bottom: 4px;
}

.muted {
    color: var(--muted);
    line-height: 1.45;
}

.error-text {
    color: var(--danger);
    margin: 14px 0 0;
}

label {
    display: block;
    color: var(--muted);
    font-size: 13px;
    font-weight: 650;
    margin: 16px 0 8px;
}

input,
textarea {
    width: 100%;
    border: 1px solid var(--border);
    outline: 0;
    border-radius: 16px;
    background: var(--panel-3);
    color: var(--text);
    padding: 13px 14px;
    font-size: 16px;
}

textarea {
    resize: none;
    line-height: 1.35;
}

input:focus,
textarea:focus {
    border-color: rgba(100,116,139,0.78);
    box-shadow: 0 0 0 3px rgba(100,116,139,0.18);
}

.primary,
.secondary,
.ghost,
.danger {
    border: 0;
    border-radius: 12px;
    min-height: 44px;
    padding: 0 16px;
    color: white;
    font-weight: 750;
}

.primary {
    background: var(--primary);
}

.primary:active,
.secondary:active,
.ghost:active,
.icon-btn:active {
    transform: translateY(1px);
}

.secondary {
    background: var(--panel-3);
}

.ghost {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text);
}

.danger {
    background: var(--danger);
}

.danger.ghost {
    background: transparent;
    color: var(--danger);
    border: 1px solid rgba(255,85,115,0.35);
}

.full-width {
    width: 100%;
    margin-top: 20px;
}

.chat-view {
    display: flex;
    overflow: hidden;
    background: var(--bg);
}

.chat-screen {
    display: flex;
    flex-direction: column;
    position: relative;
    overflow: hidden;
}

.chat-header {
    min-height: calc(var(--header-height) + env(safe-area-inset-top));
    padding: env(safe-area-inset-top) 12px 0;
    background: rgba(24,27,34,0.96);
    backdrop-filter: blur(14px);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
    z-index: 10;
}

.icon-btn {
    width: 42px;
    height: 42px;
    min-width: 42px;
    display: grid;
    place-items: center;
    border: 0;
    border-radius: 14px;
    background: var(--panel-3);
    color: var(--text);
    font-size: 22px;
    line-height: 1;
}

.room-heading {
    min-width: 0;
    flex: 1;
}

.room-title {
    font-size: 16px;
    font-weight: 800;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.room-subtitle {
    margin-top: 2px;
    color: var(--muted);
    font-size: 12px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.messages {
    flex: 1;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 14px 12px calc(var(--composer-min) + 38px + env(safe-area-inset-bottom));
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
}

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

.empty-state strong {
    color: var(--text);
    display: block;
    font-size: 18px;
    margin-bottom: 8px;
}

.message {
    width: fit-content;
    max-width: 84%;
}

.message.mine {
    align-self: flex-end;
}

.message.agent,
.message.system {
    align-self: flex-start;
}

.sender-row {
    display: flex;
    align-items: center;
    gap: 7px;
    margin: 0 0 4px 8px;
    color: var(--muted);
    font-size: 12px;
}

.avatar {
    width: 22px;
    height: 22px;
    display: inline-grid;
    place-items: center;
    border-radius: 999px;
    background: var(--panel-3);
    font-size: 10px;
    color: var(--text);
    font-weight: 800;
}

.bubble {
    border-radius: 15px;
    padding: 11px 14px;
    line-height: 1.38;
    word-break: break-word;
    white-space: pre-wrap;
    box-shadow: 0 2px 10px rgba(0,0,0,0.12);
}

.message.mine .bubble {
    background: var(--bubble-mine);
    color: white;
    border-bottom-right-radius: 6px;
}

.message.agent .bubble {
    background: var(--bubble-agent);
    color: var(--text);
    border-bottom-left-radius: 6px;
}

.message.system .bubble {
    background: rgba(255,255,255,0.06);
    color: var(--muted);
    border: 1px solid var(--border);
    font-size: 13px;
}

.message-meta {
    margin-top: 4px;
    font-size: 11px;
    color: var(--muted-2);
    text-align: right;
}

.composer-wrap {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 9;
    background: rgba(24,27,34,0.98);
    backdrop-filter: blur(14px);
    border-top: 1px solid var(--border);
    padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
}

.composer {
    display: flex;
    align-items: flex-end;
    gap: 8px;
}

.composer textarea {
    min-height: 44px;
    max-height: 140px;
    padding-top: 12px;
    padding-bottom: 12px;
    border-radius: 18px;
    overflow-y: auto;
}

.circle {
    width: 44px;
    min-width: 44px;
    padding: 0;
    border-radius: 15px;
}

.send-btn {
    min-width: 64px;
}

.send-mode-bar {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding: 0 0 8px;
}

.send-chip {
    flex: 0 0 auto;
    border: 1px solid var(--border);
    border-radius: 12px;
    color: var(--text);
    background: var(--panel-3);
    padding: 8px 11px;
    font-size: 13px;
}

.send-chip.active {
    background: var(--primary);
    border-color: var(--primary);
}

.scrim {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.48);
    z-index: 40;
}

.room-drawer {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(86vw, 340px);
    z-index: 50;
    transform: translateX(-102%);
    transition: transform 0.22s ease;
    background: var(--panel);
    border-right: 1px solid var(--border);
    box-shadow: 16px 0 60px rgba(0,0,0,0.35);
    padding: calc(12px + env(safe-area-inset-top)) 12px calc(12px + env(safe-area-inset-bottom));
    display: flex;
    flex-direction: column;
}

.room-drawer.open {
    transform: translateX(0);
}

.drawer-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    margin-bottom: 12px;
}

.drawer-title {
    font-size: 22px;
    font-weight: 850;
}

.drawer-subtitle {
    color: var(--muted);
    font-size: 13px;
    margin-top: 2px;
}

.new-group-btn {
    width: 100%;
    border: 1px solid rgba(148,163,184,0.22);
    color: white;
    background: #334155;
    min-height: 46px;
    border-radius: 12px;
    font-weight: 800;
    margin: 4px 0 16px;
}

.drawer-section-title {
    color: var(--muted);
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    margin: 8px 4px;
    font-weight: 800;
}

.room-list {
    overflow-y: auto;
    flex: 1;
    padding-right: 2px;
}

.room-item {
    width: 100%;
    border: 0;
    background: transparent;
    color: var(--text);
    text-align: left;
    border-radius: 12px;
    padding: 12px;
    margin-bottom: 6px;
    display: grid;
    grid-template-columns: 42px 1fr auto;
    gap: 10px;
    align-items: center;
}

.room-item.active,
.room-item:active {
    background: var(--panel-3);
}

.room-icon {
    width: 42px;
    height: 42px;
    border-radius: 12px;
    display: grid;
    place-items: center;
    background: var(--panel-3);
    font-weight: 850;
    color: white;
}

.room-info {
    min-width: 0;
}

.room-name {
    font-weight: 760;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.room-preview {
    color: var(--muted);
    font-size: 12px;
    margin-top: 2px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.room-badge {
    min-width: 22px;
    height: 22px;
    border-radius: 10px;
    background: var(--primary);
    color: white;
    display: grid;
    place-items: center;
    padding: 0 7px;
    font-size: 12px;
    font-weight: 850;
}

.action-menu {
    position: fixed;
    top: calc(var(--header-height) + env(safe-area-inset-top) + 8px);
    right: 12px;
    z-index: 30;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 18px;
    box-shadow: 0 14px 60px rgba(0,0,0,0.35);
    overflow: hidden;
    min-width: 210px;
}

.action-menu button {
    width: 100%;
    border: 0;
    background: transparent;
    color: var(--text);
    padding: 14px 16px;
    text-align: left;
}

.action-menu button:active {
    background: var(--panel-3);
}

.modal {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: grid;
    place-items: end center;
    padding: 16px 10px calc(16px + env(safe-area-inset-bottom));
    background: rgba(0,0,0,0.58);
}

.modal-card {
    width: min(100%, 560px);
    max-height: min(88dvh, 760px);
    overflow-y: auto;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 26px;
    padding: 18px;
    box-shadow: 0 20px 90px rgba(0,0,0,0.45);
}

.modal-header {
    display: flex;
    gap: 12px;
    align-items: flex-start;
    justify-content: space-between;
    margin-bottom: 10px;
}

.form-row.split {
    display: grid;
    grid-template-columns: 1fr 92px;
    gap: 12px;
    align-items: end;
}

.toggle-row {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 48px;
    margin-top: 16px;
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 0 12px;
}

.toggle-row input {
    width: 18px;
    height: 18px;
}

.agent-picker-title {
    margin: 18px 0 10px;
    color: var(--muted);
    font-size: 13px;
    font-weight: 800;
}

.agent-checklist {
    display: grid;
    gap: 8px;
}

.agent-option {
    display: grid;
    grid-template-columns: 24px 40px 1fr;
    gap: 10px;
    align-items: center;
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 10px;
}

.agent-option input {
    width: 20px;
    height: 20px;
}

.agent-name {
    font-weight: 800;
}

.agent-meta {
    color: var(--muted);
    font-size: 12px;
    margin-top: 2px;
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 18px;
}

.modal-actions .danger {
    margin-right: auto;
}

.toast {
    position: fixed;
    left: 50%;
    bottom: calc(88px + env(safe-area-inset-bottom));
    z-index: 150;
    transform: translateX(-50%);
    background: var(--panel-3);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 10px 14px;
    box-shadow: 0 10px 50px rgba(0,0,0,0.35);
    max-width: min(92vw, 480px);
    text-align: center;
}

@media (min-width: 860px) {
    body {
        overflow: auto;
    }

    .chat-view {
        max-width: 1180px;
        margin: 0 auto;
        border-left: 1px solid var(--border);
        border-right: 1px solid var(--border);
    }

    .room-drawer {
        position: relative;
        transform: none;
        width: 330px;
        min-width: 330px;
        z-index: 0;
        box-shadow: none;
    }

    .scrim,
    #openDrawerBtn,
    #closeDrawerBtn {
        display: none !important;
    }

    .composer-wrap {
        left: calc(50% - 590px + 330px);
        right: calc(50% - 590px);
    }

    .typing-bar {
        left: calc(50% - 590px + 342px);
    }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.001ms !important;
        transition-duration: 0.001ms !important;
        scroll-behavior: auto !important;
    }
}
