:root {
    --bg: #0f1419;
    --surface: #1a2332;
    --surface-hover: #243044;
    --border: #2d3a4f;
    --text: #e8eaee;
    --text-muted: #8b9cb3;
    --accent: #6366f1;
    --accent-hover: #818cf8;
    --user-bg: #1e3a5f;
    --assistant-bg: #1a2332;
    --error: #f87171;
    --radius: 12px;
    --shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    background: linear-gradient(160deg, #0a0e17 0%, #121a28 50%, #0f1419 100%);
    color: var(--text);
    height: 100vh;
    overflow: hidden;
    line-height: 1.5;
}

.layout {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

.sidebar {
    width: 280px;
    flex-shrink: 0;
    background: rgba(15, 20, 25, 0.95);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    padding: 1rem 0.85rem;
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.85rem;
    padding: 0 0.25rem;
}

.sidebar-header h2 {
    font-size: 0.95rem;
    font-weight: 600;
}

.btn-new {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-size: 1.2rem;
    line-height: 1;
    cursor: pointer;
    transition: all 0.15s;
}

.btn-new:hover {
    background: var(--accent);
    border-color: var(--accent);
    color: white;
}

.search-form {
    margin-bottom: 0.85rem;
    padding: 0 0.25rem;
}

.search-form input {
    width: 100%;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text);
    padding: 0.6rem 0.75rem;
    font: inherit;
    font-size: 0.85rem;
}

.search-form input:focus {
    outline: none;
    border-color: var(--accent);
}

.conversation-list {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    padding: 0 0.25rem;
    min-height: 0;
}

.conversation-empty {
    color: var(--text-muted);
    font-size: 0.85rem;
    padding: 1rem 0.5rem;
    text-align: center;
}

.conversation-item {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 0.2rem;
    width: 100%;
    padding: 0.65rem 2rem 0.65rem 0.75rem;
    border: 1px solid transparent;
    border-radius: 8px;
    background: transparent;
    color: var(--text);
    text-align: left;
    cursor: pointer;
    transition: all 0.15s;
}

.conversation-item:hover,
.conversation-item.active {
    background: var(--surface-hover);
    border-color: var(--border);
}

.conversation-item.active {
    border-color: rgba(99, 102, 241, 0.45);
    box-shadow: inset 3px 0 0 var(--accent);
}

.conversation-title {
    font-size: 0.85rem;
    font-weight: 500;
    line-height: 1.3;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 100%;
}

.conversation-meta {
    font-size: 0.68rem;
    color: var(--text-muted);
}

.conversation-preview {
    font-size: 0.75rem;
    color: var(--text-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 100%;
}

.conversation-delete {
    position: absolute;
    top: 0.45rem;
    right: 0.45rem;
    width: 22px;
    height: 22px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    color: var(--text-muted);
    font-size: 1.1rem;
    line-height: 1;
    opacity: 0;
    transition: all 0.15s;
}

.conversation-item:hover .conversation-delete {
    opacity: 1;
}

.conversation-delete:hover {
    background: rgba(248, 113, 113, 0.15);
    color: var(--error);
}

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

.app {
    max-width: 860px;
    width: 100%;
    height: 100%;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    padding: 1rem;
    min-height: 0;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.btn-sidebar-toggle {
    display: none;
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--text);
    border-radius: 8px;
    width: 36px;
    height: 36px;
    cursor: pointer;
    font-size: 1rem;
}

.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: 1rem 0 1.25rem;
    border-bottom: 1px solid var(--border);
    margin-bottom: 1rem;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    flex-shrink: 0;
}

.user-badge {
    font-size: 0.75rem;
    color: var(--text-muted);
    background: var(--surface);
    padding: 0.35rem 0.75rem;
    border-radius: 999px;
    border: 1px solid var(--border);
}

.btn-logout {
    font-size: 0.8rem;
    color: var(--text-muted);
    text-decoration: none;
    padding: 0.4rem 0.85rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    transition: all 0.15s;
}

.btn-logout:hover {
    background: var(--surface-hover);
    color: var(--text);
    border-color: var(--text-muted);
}

.login-page {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.login-card {
    width: 100%;
    max-width: 400px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 2rem;
    box-shadow: var(--shadow);
}

.login-card h1 {
    font-size: 1.35rem;
    margin-bottom: 0.35rem;
}

.login-subtitle {
    color: var(--text-muted);
    font-size: 0.9rem;
    margin-bottom: 1.25rem;
}

.login-form {
    display: flex;
    flex-direction: column;
    gap: 0.65rem;
}

.login-form label {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-top: 0.25rem;
}

.login-form input {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text);
    padding: 0.75rem 0.85rem;
    font: inherit;
}

.login-form input:focus {
    outline: none;
    border-color: var(--accent);
}

.login-submit {
    width: 100%;
    margin-top: 0.75rem;
}

.login-card .error-banner {
    margin-bottom: 1rem;
}

.success-banner {
    background: rgba(74, 222, 128, 0.12);
    border: 1px solid rgba(74, 222, 128, 0.35);
    color: #86efac;
    padding: 0.75rem 1rem;
    border-radius: 8px;
    font-size: 0.85rem;
    margin-bottom: 1rem;
    display: none;
}

.success-banner.visible {
    display: block;
}

.form-hint {
    font-size: 0.78rem;
    color: var(--text-muted);
    margin-top: 0.15rem;
}

.login-back {
    margin-top: 1.25rem;
    text-align: center;
    font-size: 0.85rem;
}

.login-back a {
    color: var(--text-muted);
    text-decoration: none;
}

.login-back a:hover {
    color: var(--accent-hover);
}

.header h1 {
    font-size: 1.25rem;
    font-weight: 600;
    letter-spacing: -0.02em;
}

.messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    padding: 0.5rem 0 1rem;
    scroll-behavior: auto;
}

.message {
    display: flex;
    flex-direction: column;
    max-width: 85%;
    animation: fadeIn 0.25s ease;
}

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

.message.assistant {
    align-self: flex-start;
    max-width: 95%;
}

.message-label {
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
    margin-bottom: 0.35rem;
    padding: 0 0.25rem;
}

.message.user .message-label {
    text-align: right;
}

.message-bubble {
    padding: 0.85rem 1.1rem;
    border-radius: var(--radius);
    word-break: break-word;
    box-shadow: var(--shadow);
}

.message.user .message-bubble {
    white-space: pre-wrap;
    background: var(--user-bg);
    border: 1px solid #2a5080;
    border-bottom-right-radius: 4px;
}

.message.assistant .message-bubble {
    background: var(--assistant-bg);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
}

.message-bubble p {
    margin: 0 0 0.65rem;
}

.message-bubble p:last-child {
    margin-bottom: 0;
}

.message-bubble h1,
.message-bubble h2,
.message-bubble h3,
.message-bubble h4,
.message-bubble h5,
.message-bubble h6 {
    margin: 1rem 0 0.5rem;
    line-height: 1.35;
    font-weight: 600;
}

.message-bubble h1:first-child,
.message-bubble h2:first-child,
.message-bubble h3:first-child,
.message-bubble h4:first-child,
.message-bubble h5:first-child,
.message-bubble h6:first-child {
    margin-top: 0;
}

.message-bubble h1 { font-size: 1.35rem; }
.message-bubble h2 { font-size: 1.2rem; }
.message-bubble h3 { font-size: 1.08rem; }
.message-bubble h4 { font-size: 1rem; }
.message-bubble h5 { font-size: 0.95rem; }
.message-bubble h6 { font-size: 0.9rem; color: var(--text-muted); }

.message-bubble ul,
.message-bubble ol {
    margin: 0.5rem 0 0.75rem;
    padding-left: 1.4rem;
}

.message-bubble li {
    margin: 0.25rem 0;
}

.message-bubble li > ul,
.message-bubble li > ol {
    margin: 0.25rem 0 0;
}

.message-bubble strong {
    font-weight: 600;
    color: var(--text);
}

.message-bubble em {
    font-style: italic;
}

.message-bubble a {
    color: var(--accent-hover);
    text-decoration: underline;
    text-underline-offset: 2px;
}

.message-bubble a:hover {
    color: #a5b4fc;
}

.message-bubble blockquote {
    margin: 0.65rem 0;
    padding: 0.5rem 0.85rem;
    border-left: 3px solid var(--accent);
    background: rgba(99, 102, 241, 0.08);
    border-radius: 0 6px 6px 0;
    color: var(--text-muted);
}

.message-bubble blockquote p:last-child {
    margin-bottom: 0;
}

.message-bubble hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 0.85rem 0;
}

.message-bubble .inline-code {
    background: rgba(99, 102, 241, 0.18);
    border: 1px solid rgba(99, 102, 241, 0.25);
    padding: 0.12rem 0.4rem;
    border-radius: 4px;
    font-family: 'Cascadia Code', 'Fira Code', Consolas, 'Courier New', monospace;
    font-size: 0.88em;
    white-space: pre-wrap;
}

.code-block {
    margin: 0.65rem 0;
    border-radius: 8px;
    overflow: hidden;
    border: 1px solid #1e2d45;
    background: #0a0e14;
}

.code-block.incomplete {
    border-color: var(--accent);
    box-shadow: 0 0 0 1px rgba(99, 102, 241, 0.25);
}

.code-lang {
    display: block;
    padding: 0.35rem 0.85rem;
    font-size: 0.68rem;
    font-weight: 600;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--text-muted);
    background: rgba(0, 0, 0, 0.35);
    border-bottom: 1px solid #1e2d45;
    font-family: system-ui, sans-serif;
}

.code-block pre {
    margin: 0;
    padding: 0.9rem 1rem;
    overflow-x: auto;
}

.code-block code {
    display: block;
    font-family: 'Cascadia Code', 'Fira Code', Consolas, 'Courier New', monospace;
    font-size: 0.84rem;
    line-height: 1.55;
    white-space: pre;
    tab-size: 4;
    color: #e6edf3;
}

.welcome {
    text-align: center;
    color: var(--text-muted);
    padding: 3rem 1rem;
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

.welcome h2 {
    font-size: 1.1rem;
    font-weight: 500;
    color: var(--text);
}

.welcome p {
    font-size: 0.9rem;
    max-width: 420px;
}

.input-area {
    border-top: 1px solid var(--border);
    padding-top: 1rem;
    display: flex;
    gap: 0.75rem;
    align-items: flex-end;
}

.input-area textarea {
    flex: 1;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text);
    padding: 0.85rem 1rem;
    font: inherit;
    font-size: 0.95rem;
    resize: none;
    min-height: 48px;
    max-height: 160px;
    line-height: 1.45;
    transition: border-color 0.15s;
}

.input-area textarea:focus {
    outline: none;
    border-color: var(--accent);
}

.input-area textarea::placeholder {
    color: var(--text-muted);
}

.btn-send {
    background: var(--accent);
    color: white;
    border: none;
    border-radius: var(--radius);
    padding: 0.85rem 1.25rem;
    font: inherit;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s;
    white-space: nowrap;
}

.btn-send:hover:not(:disabled) {
    background: var(--accent-hover);
}

.btn-send:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.error-banner {
    background: rgba(248, 113, 113, 0.12);
    border: 1px solid rgba(248, 113, 113, 0.35);
    color: var(--error);
    padding: 0.75rem 1rem;
    border-radius: 8px;
    font-size: 0.85rem;
    margin-bottom: 0.75rem;
    display: none;
}

.error-banner.visible {
    display: block;
}

.message-bubble.streaming::after {
    content: '▋';
    display: inline-block;
    margin-left: 2px;
    color: var(--accent);
    animation: cursorBlink 0.9s step-end infinite;
    vertical-align: baseline;
}

.message-bubble.streaming:empty::before {
    content: '…';
    color: var(--text-muted);
    animation: pulse 1.2s ease-in-out infinite;
}

@keyframes cursorBlink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0; }
}

@keyframes pulse {
    0%, 100% { opacity: 0.35; }
    50% { opacity: 1; }
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes blink {
    0%, 60%, 100% { opacity: 0.3; }
    30% { opacity: 1; }
}

@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        inset: 0 auto 0 0;
        z-index: 100;
        transform: translateX(-100%);
        transition: transform 0.2s ease;
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .btn-sidebar-toggle {
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    .app {
        padding: 0.75rem;
    }
}

@media (max-width: 600px) {
    .message { max-width: 92%; }
    .header { flex-wrap: wrap; gap: 0.5rem; }
    .header-actions { width: 100%; justify-content: flex-end; }
    .user-badge { display: none; }
}
