:root {
    color-scheme: light;
    --page: #eef2f7;
    --page-glow: rgba(91, 82, 220, .16);
    --surface: #ffffff;
    --surface-soft: #f7f8fc;
    --surface-raised: #ffffff;
    --line: #e2e6ef;
    --line-strong: #d4d9e6;
    --text: #1c2333;
    --muted: #71798c;
    --faint: #9aa2b3;
    --accent: #6657e8;
    --accent-hover: #5848da;
    --accent-soft: #eeebff;
    --accent-text: #ffffff;
    --bubble: #f0f2f7;
    --bubble-self: #6657e8;
    --bubble-self-text: #ffffff;
    --danger: #d54747;
    --shadow: 0 28px 80px rgba(35, 40, 70, .16);
    --font: Inter, "Segoe UI", "Microsoft YaHei UI", "Microsoft YaHei", sans-serif;
}

:root[data-theme="dark"] {
    color-scheme: dark;
    --page: #0d1017;
    --page-glow: rgba(105, 91, 235, .18);
    --surface: #171b26;
    --surface-soft: #121620;
    --surface-raised: #1d2230;
    --line: #292f40;
    --line-strong: #363d51;
    --text: #edf0f8;
    --muted: #9ba4ba;
    --faint: #737c92;
    --accent: #796bf2;
    --accent-hover: #8a7df8;
    --accent-soft: #29264d;
    --accent-text: #ffffff;
    --bubble: #252b39;
    --bubble-self: #6b5ee8;
    --bubble-self-text: #ffffff;
    --danger: #ff7777;
    --shadow: 0 30px 100px rgba(0, 0, 0, .42);
}

* { box-sizing: border-box; }

html, body { width: 100%; min-height: 100%; }

body {
    margin: 0;
    min-height: 100vh;
    min-height: 100dvh;
    overflow: hidden;
    color: var(--text);
    background:
        radial-gradient(circle at 50% -20%, var(--page-glow), transparent 42%),
        var(--page);
    font-family: var(--font);
    -webkit-font-smoothing: antialiased;
}

button, input, textarea, select { color: inherit; font: inherit; }
button, select { cursor: pointer; }
button, input, textarea, select { outline: none; }
button:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible, a:focus-visible {
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 30%, transparent);
}

a { color: inherit; text-decoration: none; }
.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; }

.chat-app {
    display: grid;
    grid-template-columns: 320px minmax(0, 1fr);
    width: min(1500px, calc(100vw - 48px));
    height: min(940px, calc(100dvh - 48px));
    margin: max(24px, calc((100dvh - 940px) / 2)) auto;
    overflow: hidden;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 24px;
    box-shadow: var(--shadow);
}

.room-sidebar {
    display: flex;
    min-width: 0;
    min-height: 0;
    flex-direction: column;
    padding: 24px 18px 18px;
    overflow: auto;
    background: var(--surface-soft);
    border-right: 1px solid var(--line);
}

.brand-row { display: flex; align-items: center; gap: 12px; padding: 0 5px 22px; }
.brand-row > span:last-child { display: flex; min-width: 0; flex-direction: column; gap: 3px; }
.brand-row strong { font-size: 16px; letter-spacing: -.02em; }
.brand-row small { color: var(--muted); font-size: 12px; }
.brand-mark, .active-room-icon {
    display: grid;
    width: 42px;
    height: 42px;
    flex: 0 0 auto;
    place-items: center;
    color: var(--accent-text);
    background: linear-gradient(145deg, #897cf5, #5c4cda);
    border-radius: 13px;
    font-size: 17px;
    font-weight: 800;
    box-shadow: 0 8px 22px color-mix(in srgb, var(--accent) 30%, transparent);
}

.profile-field { display: grid; gap: 7px; margin: 0 4px 24px; }
.profile-field span, .theme-field > span { color: var(--muted); font-size: 11px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; }
.profile-field input, .custom-room-form input, .theme-field select {
    width: 100%;
    min-width: 0;
    border: 1px solid var(--line);
    background: var(--surface-raised);
    border-radius: 11px;
}
.profile-field input { padding: 10px 12px; }
.profile-field input:focus, .custom-room-form input:focus, .theme-field select:focus, .composer textarea:focus { border-color: var(--accent); }

.room-section { margin-bottom: 23px; }
.section-title { display: flex; align-items: center; justify-content: space-between; padding: 0 5px 9px; }
.section-title h2 { margin: 0; color: var(--muted); font-size: 11px; letter-spacing: .07em; text-transform: uppercase; }
.section-title > span, .text-button { color: var(--faint); font-size: 11px; }
.text-button { padding: 2px 0; border: 0; background: transparent; }
.text-button:hover { color: var(--accent); }

.room-item, .recent-room {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    width: 100%;
    align-items: center;
    gap: 11px;
    padding: 11px;
    text-align: left;
    border: 1px solid transparent;
    background: transparent;
    border-radius: 13px;
}
.room-item:hover, .recent-room:hover { background: var(--surface-raised); border-color: var(--line); }
.room-item.active, .recent-room.active { background: var(--accent-soft); border-color: color-mix(in srgb, var(--accent) 22%, transparent); }
.room-item > span:nth-child(2), .recent-room > span:nth-child(2) { display: flex; min-width: 0; flex-direction: column; gap: 3px; }
.room-item strong, .recent-room strong { overflow: hidden; font-size: 14px; text-overflow: ellipsis; white-space: nowrap; }
.room-item small, .recent-room small { color: var(--muted); font-size: 11px; }
.room-chevron { width: 17px; height: 17px; color: var(--faint); stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.room-icon {
    display: grid;
    width: 35px;
    height: 35px;
    place-items: center;
    color: var(--accent);
    background: var(--accent-soft);
    border-radius: 10px;
    font-weight: 800;
}
.room-icon svg, .active-room-icon svg, .welcome-state > span svg, .robot-entry > span:first-child svg {
    width: 20px;
    height: 20px;
    stroke: currentColor;
    stroke-width: 1.7;
    stroke-linecap: round;
    stroke-linejoin: round;
}
.room-item.active .room-icon { color: var(--accent-text); background: var(--accent); }

.custom-room-form { display: flex; gap: 8px; padding: 0 4px; }
.custom-room-form input { flex: 1; padding: 10px 11px; }
.custom-room-form button, .send-button {
    border: 0;
    color: var(--accent-text);
    background: var(--accent);
    border-radius: 11px;
    font-weight: 700;
}
.custom-room-form button { padding: 0 13px; font-size: 13px; }
.custom-room-form button:hover, .send-button:hover { background: var(--accent-hover); }

.recent-section { min-height: 96px; }
.recent-rooms { display: grid; gap: 2px; }
.recent-empty { padding: 12px 6px; color: var(--faint); font-size: 12px; line-height: 1.6; }
.recent-room { padding: 8px 11px; }
.recent-room .room-icon { width: 31px; height: 31px; }

.sidebar-footer { display: grid; gap: 13px; margin-top: auto; padding: 16px 4px 0; border-top: 1px solid var(--line); }
.theme-field { display: grid; grid-template-columns: 1fr 124px; align-items: center; gap: 10px; }
.theme-field select { padding: 8px 9px; font-size: 12px; }
.robot-entry { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: center; gap: 10px; padding: 10px 11px; border-radius: 12px; }
.robot-entry:hover { background: var(--surface-raised); }
.robot-entry > span:first-child { display: grid; width: 32px; height: 32px; place-items: center; color: var(--accent); background: var(--accent-soft); border-radius: 9px; }
.robot-entry > span:nth-child(2) { display: flex; flex-direction: column; gap: 2px; }
.robot-entry strong { font-size: 13px; }
.robot-entry small { color: var(--muted); font-size: 11px; }

.conversation, .robot-shell {
    display: grid;
    min-width: 0;
    min-height: 0;
    grid-template-rows: auto minmax(0, 1fr) auto;
    background: var(--surface);
}
.conversation-header {
    display: flex;
    min-width: 0;
    min-height: 78px;
    align-items: center;
    gap: 13px;
    padding: 15px 22px;
    border-bottom: 1px solid var(--line);
}
.active-room-icon { width: 43px; height: 43px; box-shadow: none; }
.robot-avatar { font-size: 16px; }
.room-heading { display: flex; min-width: 0; flex: 1; flex-direction: column; gap: 3px; }
.room-heading strong { overflow: hidden; font-size: 16px; text-overflow: ellipsis; white-space: nowrap; }
.room-heading small { overflow: hidden; color: var(--muted); font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }
.header-actions { display: flex; align-items: center; gap: 9px; }
.icon-button, .mobile-room-button, .header-robot-link {
    display: grid;
    min-width: 38px;
    height: 38px;
    place-items: center;
    border: 1px solid var(--line);
    color: var(--muted);
    background: var(--surface-soft);
    border-radius: 11px;
}
.icon-button:hover, .mobile-room-button:hover, .header-robot-link:hover { color: var(--accent); border-color: color-mix(in srgb, var(--accent) 35%, var(--line)); }
.header-robot-link { padding: 0 14px; font-size: 12px; font-weight: 700; }
.mobile-room-button { display: none; padding: 0; }
.mobile-room-button svg, .theme-glyph { width: 19px; height: 19px; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.theme-glyph-sun { display: none; }
:root[data-theme="dark"] .theme-glyph-moon { display: none; }
:root[data-theme="dark"] .theme-glyph-sun { display: initial; }

.messages {
    display: flex;
    min-width: 0;
    min-height: 0;
    flex-direction: column;
    gap: 15px;
    padding: clamp(20px, 3vw, 42px);
    overflow: auto;
    overscroll-behavior: contain;
    scroll-behavior: smooth;
}
.welcome-state { display: grid; width: min(100%, 430px); min-width: 0; margin: auto; justify-items: center; text-align: center; }
.welcome-state > span { display: grid; width: 64px; height: 64px; margin-bottom: 17px; place-items: center; color: var(--accent); background: var(--accent-soft); border-radius: 20px; font-size: 25px; font-weight: 800; }
.welcome-state h1 { margin: 0 0 8px; font-size: clamp(21px, 2vw, 28px); letter-spacing: -.03em; }
.welcome-state p { margin: 0; overflow-wrap: anywhere; color: var(--muted); font-size: 14px; line-height: 1.7; }

.message-row { display: flex; min-width: 0; align-items: flex-end; gap: 9px; }
.message-row.self { justify-content: flex-end; }
.message-avatar { display: grid; width: 32px; height: 32px; flex: 0 0 auto; place-items: center; color: var(--muted); background: var(--surface-soft); border: 1px solid var(--line); border-radius: 10px; font-size: 12px; font-weight: 800; }
.message-row.self .message-avatar { display: none; }
.message-stack { display: flex; min-width: 0; max-width: min(76%, 720px); flex-direction: column; gap: 5px; }
.message-row.self .message-stack { align-items: flex-end; }
.message-meta { display: flex; align-items: center; gap: 7px; padding: 0 4px; color: var(--muted); font-size: 11px; }
.message-row.self .message-meta { justify-content: flex-end; }
.message-name { color: var(--muted); }
.message-time { color: var(--faint); font-variant-numeric: tabular-nums; }
.message-bubble {
    min-width: 0;
    padding: 11px 14px;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    word-break: break-word;
    background: var(--bubble);
    border-radius: 16px 16px 16px 5px;
    font-size: 14px;
    line-height: 1.58;
}
.message-row.self .message-bubble { color: var(--bubble-self-text); background: var(--bubble-self); border-radius: 16px 16px 5px 16px; }
.message-row.system .message-bubble { color: var(--danger); border: 1px solid color-mix(in srgb, var(--danger) 22%, transparent); background: color-mix(in srgb, var(--danger) 8%, var(--surface)); }
.message-bubble.markdown { max-width: 100%; white-space: normal; }
.message-bubble.markdown > :first-child { margin-top: 0; }
.message-bubble.markdown > :last-child { margin-bottom: 0; }
.message-bubble.markdown p, .message-bubble.markdown ul, .message-bubble.markdown ol,
.message-bubble.markdown blockquote, .message-bubble.markdown pre, .message-bubble.markdown table {
    margin: .65em 0;
}
.message-bubble.markdown h1, .message-bubble.markdown h2, .message-bubble.markdown h3,
.message-bubble.markdown h4, .message-bubble.markdown h5, .message-bubble.markdown h6 {
    margin: .9em 0 .45em;
    line-height: 1.3;
}
.message-bubble.markdown h1 { font-size: 1.35em; }
.message-bubble.markdown h2 { font-size: 1.22em; }
.message-bubble.markdown h3 { font-size: 1.12em; }
.message-bubble.markdown ul, .message-bubble.markdown ol { padding-left: 1.6em; }
.message-bubble.markdown li + li { margin-top: .25em; }
.message-bubble.markdown blockquote {
    padding-left: .8em;
    color: var(--muted);
    border-left: 3px solid color-mix(in srgb, var(--accent) 45%, var(--line));
}
.message-bubble.markdown code {
    padding: .12em .35em;
    background: color-mix(in srgb, var(--text) 8%, transparent);
    border-radius: 5px;
    font: .9em/1.5 Consolas, "Cascadia Code", monospace;
}
.message-bubble.markdown pre {
    max-width: 100%;
    padding: 12px 14px;
    overflow-x: auto;
    color: var(--text);
    background: color-mix(in srgb, var(--surface-soft) 75%, #111827 8%);
    border: 1px solid var(--line);
    border-radius: 10px;
    white-space: pre;
}
.message-bubble.markdown pre code { padding: 0; background: transparent; border-radius: 0; }
.message-bubble.markdown a { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }
.message-bubble.markdown hr { margin: .9em 0; border: 0; border-top: 1px solid var(--line-strong); }
.message-bubble.markdown table { display: block; max-width: 100%; overflow-x: auto; border-collapse: collapse; }
.message-bubble.markdown th, .message-bubble.markdown td { padding: 6px 9px; border: 1px solid var(--line-strong); }
.message-bubble.markdown img { max-width: 100%; height: auto; border-radius: 8px; }

.composer {
    display: flex;
    min-width: 0;
    align-items: flex-end;
    gap: 11px;
    padding: 14px 20px calc(14px + env(safe-area-inset-bottom));
    background: var(--surface);
    border-top: 1px solid var(--line);
}
.composer-input {
    display: flex;
    min-width: 0;
    flex: 1;
    flex-direction: column;
    gap: 5px;
    padding: 8px 13px 7px;
    border: 1px solid var(--line-strong);
    background: var(--surface-soft);
    border-radius: 14px;
}
.composer-input:focus-within { border-color: color-mix(in srgb, var(--accent) 65%, var(--line-strong)); }
.composer textarea {
    width: 100%;
    min-width: 0;
    min-height: 25px;
    max-height: 150px;
    padding: 2px 0;
    resize: none;
    overflow-y: auto;
    border: 0;
    background: transparent;
    line-height: 1.5;
}
.composer textarea::placeholder { color: var(--faint); }
.composer textarea:focus, .composer textarea:focus-visible { border: 0; outline: 0; box-shadow: none; }
.composer textarea:disabled { cursor: not-allowed; }
.composer-input small { color: var(--faint); font-size: 10px; }
.send-button { display: flex; height: 48px; align-items: center; gap: 7px; padding: 0 17px; }
.send-button svg { width: 18px; height: 18px; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.send-button:disabled { cursor: not-allowed; opacity: .45; }

.robot-shell {
    width: min(1040px, calc(100vw - 48px));
    height: min(900px, calc(100dvh - 48px));
    margin: max(24px, calc((100dvh - 900px) / 2)) auto;
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: 24px;
    box-shadow: var(--shadow);
}
.robot-shell .mobile-room-button { display: grid; text-decoration: none; }

@media (max-width: 900px) {
    body { background: var(--surface); }
    .chat-app, .robot-shell { width: 100vw; height: 100dvh; margin: 0; border: 0; border-radius: 0; box-shadow: none; }
    .chat-app { grid-template-columns: minmax(0, 1fr); }
    .room-sidebar { width: 100%; padding-top: calc(20px + env(safe-area-inset-top)); border-right: 0; }
    .chat-app:not(.room-active) .conversation { display: none; }
    .chat-app.room-active .room-sidebar { display: none; }
    .mobile-room-button { display: grid; }
    .conversation-header { min-height: 68px; padding: calc(10px + env(safe-area-inset-top)) 13px 10px; }
    .active-room-icon { width: 38px; height: 38px; border-radius: 11px; }
    .header-robot-link { display: none; }
    .messages { padding: 18px 13px; }
    .message-stack { max-width: 84%; }
    .composer { gap: 8px; padding: 10px 10px calc(10px + env(safe-area-inset-bottom)); }
    .composer-input { padding: 8px 11px; }
    .composer-input small { display: none; }
    .send-button { width: 48px; padding: 0; justify-content: center; }
    .send-button span { display: none; }
}

@media (max-width: 420px) {
    .room-sidebar { padding-right: 13px; padding-left: 13px; }
    .conversation-header { gap: 9px; }
    .room-heading strong { font-size: 15px; }
    .icon-button { display: none; }
    .messages { gap: 12px; }
    .message-stack { max-width: 88%; }
    .message-bubble { padding: 10px 12px; font-size: 14px; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}
