:root {
    --bg: #0d1117;
    --surface: #161b22;
    --surface-2: #1c2430;
    --surface-3: #242d3a;
    --border: #30363d;
    --text: #c9d1d9;
    --muted: #8b949e;
    --primary: #1f6feb;
    --primary-hover: #388bfd;
    --danger: #da3633;
    --online: #3fb950;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    font-size: 14px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    overscroll-behavior: none;
}

button, input, textarea, select {
    font-family: inherit;
    font-size: inherit;
    color: inherit;
    border-radius: 0;
}
button { cursor: pointer; }

#app {
    height: 100dvh;
    display: flex;
    flex-direction: column;
    position: relative;
    overflow: hidden;
}

.screen { display: none; flex: 1; min-height: 0; }
.screen.active { display: flex; }
#screen-loading, #screen-auth { flex-direction: column; }
#screen-chat { flex-direction: row; }

h1, h2 { margin: 0; font-weight: 600; }
h1 { font-size: 22px; }
h2 { font-size: 14px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }

.loading { margin: auto; color: var(--muted); font-size: 14px; }

#screen-auth { justify-content: center; align-items: center; padding: 24px; }
.auth-box { width: 100%; max-width: 320px; display: flex; flex-direction: column; gap: 14px; }
.auth-box h1 { text-align: center; margin-bottom: 4px; }
.tabs { display: flex; border-bottom: 1px solid var(--border); }
.tab {
    flex: 1;
    padding: 10px;
    background: transparent;
    border: none;
    color: var(--muted);
    border-bottom: 2px solid transparent;
    font-size: 14px;
}
.tab.active { color: var(--text); border-bottom-color: var(--primary); }
#auth-form { display: flex; flex-direction: column; gap: 8px; }

.btn-primary, .btn-secondary, .btn-send, .btn-ai {
    padding: 9px 14px;
    border: 1px solid transparent;
    font-size: 14px;
    font-weight: 500;
    transition: background 0.1s, border-color 0.1s;
}
.btn-primary {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-primary:disabled { opacity: 0.6; cursor: not-allowed; }
.btn-secondary {
    background: var(--surface);
    color: var(--text);
    border-color: var(--border);
}
.btn-secondary:hover { background: var(--surface-2); }
.link-btn {
    background: transparent;
    border: none;
    color: var(--primary);
    font-size: 13px;
    padding: 4px 6px;
}
.link-btn:hover { color: var(--primary-hover); }

input, textarea {
    display: block;
    width: 100%;
    padding: 9px 11px;
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text);
    outline: none;
    resize: vertical;
    font-size: 14px;
}
input:focus, textarea:focus { border-color: var(--primary); }

label {
    display: block;
    font-size: 11px;
    color: var(--muted);
    margin: 8px 0 0;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    font-weight: 500;
}

.sidebar {
    width: 260px;
    background: var(--surface);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    min-height: 0;
}
.sidebar-left { border-right: 1px solid var(--border); }
.sidebar-right { width: 320px; border-left: 1px solid var(--border); }

.sidebar-head {
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: 48px;
}
.user-label {
    font-weight: 600;
    font-size: 13px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sidebar-actions {
    padding: 10px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    border-bottom: 1px solid var(--border);
}

.rooms-list {
    flex: 1;
    overflow-y: auto;
    padding: 6px 0;
}
.rooms-list::-webkit-scrollbar { width: 6px; }
.rooms-list::-webkit-scrollbar-thumb { background: var(--border); }

.room-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 14px;
    cursor: pointer;
    border-left: 2px solid transparent;
    min-height: 48px;
}
.room-item:hover { background: var(--surface-2); }
.room-item.active {
    background: var(--surface-2);
    border-left-color: var(--primary);
}
.room-item-avatar {
    width: 30px;
    height: 30px;
    object-fit: cover;
    background: var(--surface-3);
    border: 1px solid var(--border);
    flex-shrink: 0;
}
.room-item-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.room-item-name {
    font-size: 13px;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.room-item-code {
    font-size: 11px;
    color: var(--muted);
    font-family: ui-monospace, 'SF Mono', Menlo, monospace;
}
.rooms-list-empty {
    padding: 16px;
    text-align: center;
    color: var(--muted);
    font-size: 12px;
}

#join-code-input {
    text-align: center;
    letter-spacing: 3px;
    font-weight: 600;
    text-transform: uppercase;
    font-family: ui-monospace, 'SF Mono', Menlo, monospace;
}

.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
}

.chat-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 12px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
    min-height: 48px;
}
.char-info { flex: 1; display: flex; align-items: center; gap: 10px; min-width: 0; }
.char-text { min-width: 0; flex: 1; }
.char-avatar {
    width: 32px;
    height: 32px;
    object-fit: cover;
    background: var(--surface-2);
    border: 1px solid var(--border);
    flex-shrink: 0;
}
.char-name {
    font-weight: 600;
    font-size: 14px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.char-meta {
    font-size: 11px;
    color: var(--muted);
    display: flex;
    gap: 10px;
    align-items: center;
}
.room-code {
    padding: 1px 6px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    font-family: ui-monospace, 'SF Mono', Menlo, monospace;
    font-weight: 600;
    color: var(--primary);
    cursor: pointer;
    user-select: none;
}
.presence::before {
    content: '●';
    color: var(--online);
    margin-right: 3px;
    font-size: 9px;
    vertical-align: middle;
}

.panel-toggle {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text);
    padding: 3px 9px;
    font-size: 14px;
    display: none;
    min-width: 32px;
    min-height: 32px;
}
.panel-toggle:hover { background: var(--surface-2); }

.messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px 20px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    overscroll-behavior: contain;
}
.messages::-webkit-scrollbar { width: 6px; }
.messages::-webkit-scrollbar-thumb { background: var(--border); }

.msg {
    display: flex;
    gap: 8px;
    max-width: 720px;
    width: fit-content;
}
.msg-self { align-self: flex-end; flex-direction: row-reverse; }
.msg-body { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.msg-meta {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 0 2px;
}
.msg-self .msg-meta { flex-direction: row-reverse; }
.msg-name { font-size: 11px; color: var(--muted); }
.msg-ai .msg-name { color: var(--primary); }

.msg-pin {
    background: transparent;
    border: none;
    color: var(--muted);
    font-size: 12px;
    line-height: 1;
    padding: 2px 4px;
    opacity: 0.35;
    transition: opacity 0.15s, color 0.15s;
}
.msg:hover .msg-pin { opacity: 0.9; }
.msg-pin:hover { color: #e3b341; }
.msg.pinned .msg-pin {
    opacity: 1;
    color: #e3b341;
}
.msg.pinned .msg-content {
    border-color: #e3b341;
    box-shadow: inset 0 0 0 1px rgba(227, 179, 65, 0.2);
}
.msg.pinned.msg-self .msg-content { border-color: #e3b341; }

.msg-content {
    padding: 8px 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    word-wrap: break-word;
    overflow-wrap: break-word;
    line-height: 1.5;
}

.msg-content > *:first-child { margin-top: 0; }
.msg-content > *:last-child { margin-bottom: 0; }
.msg-content p { margin: 0 0 0.5em; }
.msg-content p:last-child { margin-bottom: 0; }
.msg-content strong { font-weight: 700; }
.msg-content em { font-style: italic; }
.msg-content del { opacity: 0.6; }
.msg-content code {
    font-family: ui-monospace, 'SF Mono', Menlo, monospace;
    font-size: 0.9em;
    background: rgba(255, 255, 255, 0.06);
    padding: 1px 5px;
    border: 1px solid rgba(255, 255, 255, 0.1);
}
.msg-content pre {
    background: var(--bg);
    border: 1px solid var(--border);
    padding: 8px 10px;
    overflow-x: auto;
    margin: 0.5em 0;
    font-size: 12px;
}
.msg-content pre code {
    background: transparent;
    border: none;
    padding: 0;
    font-size: inherit;
}
.msg-content blockquote {
    border-left: 3px solid var(--border);
    padding: 2px 10px;
    margin: 0.5em 0;
    color: var(--muted);
}
.msg-content ul, .msg-content ol {
    margin: 0.3em 0;
    padding-left: 22px;
}
.msg-content li { margin: 0.15em 0; }
.msg-content a {
    color: var(--primary);
    text-decoration: underline;
}
.msg-content h1, .msg-content h2, .msg-content h3,
.msg-content h4, .msg-content h5, .msg-content h6 {
    font-size: 1em;
    font-weight: 700;
    margin: 0.4em 0 0.2em;
}
.msg-content hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 0.6em 0;
}
.msg-self .msg-content a { color: #cfe3ff; }
.msg-self .msg-content code {
    background: rgba(255, 255, 255, 0.18);
    border-color: rgba(255, 255, 255, 0.25);
}
.msg-self .msg-content blockquote {
    border-left-color: rgba(255, 255, 255, 0.4);
    color: rgba(255, 255, 255, 0.85);
}
.msg-self .msg-content {
    background: var(--primary);
    border-color: var(--primary);
    color: white;
}
.msg-ai .msg-content { border-left: 3px solid var(--primary); }

.msg-avatar {
    width: 28px;
    height: 28px;
    object-fit: cover;
    background: var(--surface-2);
    border: 1px solid var(--border);
    flex-shrink: 0;
    align-self: flex-end;
}

.msg.streaming .msg-content::after {
    content: '▊';
    animation: blink 0.9s infinite;
    color: var(--primary);
    margin-left: 1px;
}
.msg.streaming .msg-content:empty::before {
    content: '...';
    color: var(--muted);
    letter-spacing: 3px;
}
@keyframes blink { 50% { opacity: 0.3; } }

.composer {
    display: flex;
    gap: 4px;
    padding: 10px;
    border-top: 1px solid var(--border);
    background: var(--surface);
}
.composer textarea {
    flex: 1;
    min-height: 38px;
    max-height: 160px;
    padding: 9px 12px;
    resize: none;
    line-height: 1.4;
}
.btn-send {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
    padding: 0 16px;
}
.btn-send:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-ai {
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border);
    padding: 0 14px;
}
.btn-ai:hover { background: var(--surface-3); }
.btn-ai:disabled { opacity: 0.5; cursor: not-allowed; }

.settings-panel {
    padding: 12px 14px 20px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    overflow-y: auto;
    flex: 1;
}
.settings-panel::-webkit-scrollbar { width: 6px; }
.settings-panel::-webkit-scrollbar-thumb { background: var(--border); }
.settings-panel .btn-primary { margin-top: 10px; }

.section-title {
    font-size: 11px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    font-weight: 600;
    padding: 2px 0 6px;
}
.section-divider {
    height: 1px;
    background: var(--border);
    margin: 16px -14px 8px;
}

.hint {
    font-size: 11px;
    color: var(--muted);
    line-height: 1.4;
    margin-top: 2px;
    margin-bottom: 4px;
}

textarea::placeholder, input::placeholder {
    color: #555c66;
}

.avatar-row {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 4px 0;
}
.avatar-row img {
    width: 48px;
    height: 48px;
    object-fit: cover;
    background: var(--surface-2);
    border: 1px solid var(--border);
}
#avatar-btn {
    padding: 7px 12px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    color: var(--text);
    font-size: 13px;
}
#avatar-btn:hover { background: var(--surface-3); }

.danger-btn {
    color: var(--danger);
    background: transparent;
    border: 1px solid var(--danger);
    padding: 7px 12px;
    font-size: 13px;
}
.danger-btn:hover { background: rgba(218, 54, 51, 0.1); }

.toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--surface-2);
    border: 1px solid var(--border);
    padding: 10px 16px;
    font-size: 13px;
    z-index: 200;
    max-width: 90%;
}

.overlay {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 40;
    display: none;
}

@media (max-width: 900px) {
    .sidebar {
        position: absolute;
        top: 0;
        bottom: 0;
        z-index: 50;
        transform: translateX(-100%);
        transition: transform 0.2s;
        width: min(300px, 85vw);
    }
    .sidebar-left { left: 0; }
    .sidebar-right {
        right: 0;
        left: auto;
        transform: translateX(100%);
    }
    .sidebar.open { transform: translateX(0); }
    .overlay.show { display: block; }
    .panel-toggle { display: inline-flex; align-items: center; justify-content: center; }
}

@media (max-width: 640px) {
    .messages { padding: 12px; }
    .msg { max-width: 92%; }
    .sidebar-right { width: 85vw; }
}
