:root {
    --bg: #ffffff;
    --bg-secondary: #f5f5f5;
    --text: #1a1a1a;
    --text-muted: #666666;
    --border: #e0e0e0;
    --user-bg: #0066cc;
    --user-text: #ffffff;
    --assistant-bg: #f0f0f0;
    --assistant-text: #1a1a1a;
    --accent: #0066cc;
    --accent-hover: #0052a3;
    --code-bg: #282c34;
    --input-bg: #ffffff;
    --shadow: rgba(0, 0, 0, 0.1);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #1a1a2e;
        --bg-secondary: #16213e;
        --text: #e0e0e0;
        --text-muted: #999999;
        --border: #2a2a4a;
        --user-bg: #0066cc;
        --user-text: #ffffff;
        --assistant-bg: #252545;
        --assistant-text: #e0e0e0;
        --accent: #4499ee;
        --accent-hover: #3388dd;
        --code-bg: #1e1e3a;
        --input-bg: #252545;
        --shadow: rgba(0, 0, 0, 0.3);
    }
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    height: 100dvh;
    overflow: hidden;
}

#app {
    height: 100dvh;
    display: flex;
    flex-direction: column;
}

.hidden {
    display: none !important;
}

.screen {
    height: 100%;
}

/* Login */
#login-screen {
    display: flex;
    align-items: center;
    justify-content: center;
}

.login-box {
    text-align: center;
    padding: 3rem;
    background: var(--bg-secondary);
    border-radius: 12px;
    border: 1px solid var(--border);
    box-shadow: 0 4px 12px var(--shadow);
}

.login-box h1 {
    margin-bottom: 0.5rem;
    font-size: 1.75rem;
}

.login-box p {
    margin-bottom: 1.5rem;
    color: var(--text-muted);
}

/* Buttons */
.btn {
    display: inline-block;
    padding: 0.6rem 1.2rem;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.9rem;
    text-decoration: none;
    transition: background 0.2s;
}

.btn-primary {
    background: var(--accent);
    color: #ffffff;
}

.btn-primary:hover {
    background: var(--accent-hover);
}

.btn-small {
    padding: 0.3rem 0.7rem;
    font-size: 0.8rem;
    background: var(--bg-secondary);
    color: var(--text-muted);
    border: 1px solid var(--border);
}

.btn-small:hover {
    background: var(--border);
}

.btn-logout {
    color: var(--text-muted);
}

/* Chat screen */
#chat-screen {
    display: flex;
    flex-direction: row;
    height: 100%;
}

/* Sidebar */
#sidebar {
    width: 260px;
    flex-shrink: 0;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

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

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

#convo-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem;
}

.convo-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.5rem 0.75rem;
    border-radius: 6px;
    cursor: pointer;
    transition: background 0.15s;
    gap: 0.25rem;
}

.convo-item:hover {
    background: var(--border);
}

.convo-active {
    background: var(--border);
    font-weight: 600;
}

.convo-info {
    flex: 1;
    min-width: 0;
    overflow: hidden;
}

.convo-title {
    font-size: 0.85rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.convo-date {
    font-size: 0.7rem;
    color: var(--text-muted);
}

.convo-delete {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 0.85rem;
    padding: 0.15rem 0.35rem;
    border-radius: 4px;
    opacity: 0;
    transition: opacity 0.15s, background 0.15s;
    flex-shrink: 0;
}

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

.convo-delete:hover {
    background: rgba(204, 51, 51, 0.15);
    color: #cc3333;
}

/* Chat main area */
#chat-main {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    height: 100%;
}

#sidebar-toggle {
    display: none;
    font-size: 1.1rem;
    line-height: 1;
    padding: 0.3rem 0.5rem;
}

header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
    background: var(--bg-secondary);
    flex-shrink: 0;
}

header h1 {
    font-size: 1.1rem;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

#username {
    color: var(--text-muted);
    font-size: 0.85rem;
}

/* Messages */
#messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.message {
    max-width: 80%;
    padding: 0.75rem 1rem;
    border-radius: 12px;
    line-height: 1.5;
    word-wrap: break-word;
    overflow-wrap: break-word;
}

.message-user {
    align-self: flex-end;
    background: var(--user-bg);
    color: var(--user-text);
    border-bottom-right-radius: 4px;
}

.message-assistant {
    align-self: flex-start;
    background: var(--assistant-bg);
    color: var(--assistant-text);
    border-bottom-left-radius: 4px;
}

.message-error {
    align-self: center;
    background: #cc3333;
    color: #ffffff;
    font-size: 0.85rem;
}

/* Markdown content */
.message-assistant p {
    margin-bottom: 0.5rem;
}

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

.message-assistant ul,
.message-assistant ol {
    margin: 0.5rem 0;
    padding-left: 1.5rem;
}

.message-assistant h1,
.message-assistant h2,
.message-assistant h3 {
    margin: 0.75rem 0 0.25rem;
}

.message-assistant h1 { font-size: 1.2rem; }
.message-assistant h2 { font-size: 1.1rem; }
.message-assistant h3 { font-size: 1rem; }

.message-assistant table {
    border-collapse: collapse;
    margin: 0.5rem 0;
    font-size: 0.85rem;
    width: 100%;
}

.message-assistant th,
.message-assistant td {
    border: 1px solid var(--border);
    padding: 0.4rem 0.6rem;
    text-align: left;
}

.message-assistant th {
    background: var(--bg-secondary);
}

.message-assistant a {
    color: var(--accent);
}

/* Code blocks */
.message-assistant pre {
    background: var(--code-bg);
    border-radius: 6px;
    padding: 0.75rem;
    overflow-x: auto;
    margin: 0.5rem 0;
    position: relative;
}

.message-assistant code {
    font-family: "SF Mono", "Fira Code", "Cascadia Code", monospace;
    font-size: 0.85rem;
}

.message-assistant :not(pre) > code {
    background: var(--code-bg);
    padding: 0.15rem 0.35rem;
    border-radius: 3px;
    color: #e06c75;
}

/* Code block copy button */

.copy-btn {
    position: absolute;
    top: 0.4rem;
    right: 0.4rem;
    padding: 0.2rem 0.5rem;
    font-size: 0.7rem;
    background: rgba(255, 255, 255, 0.1);
    color: #ccc;
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 4px;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.2s;
}

.message-assistant pre:hover .copy-btn {
    opacity: 1;
}

.copy-btn:hover {
    background: rgba(255, 255, 255, 0.2);
    color: #fff;
}

/* Resend button on user messages */
.message-user {
    position: relative;
}

.resend-btn {
    position: absolute;
    top: 0.3rem;
    right: 0.3rem;
    padding: 0.15rem 0.4rem;
    font-size: 0.65rem;
    background: rgba(255, 255, 255, 0.15);
    color: rgba(255, 255, 255, 0.8);
    border: 1px solid rgba(255, 255, 255, 0.25);
    border-radius: 4px;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.2s;
}

.message-user:hover .resend-btn {
    opacity: 1;
}

.resend-btn:hover {
    background: rgba(255, 255, 255, 0.3);
    color: #fff;
}

/* Message action bar */
.msg-actions {
    display: flex;
    gap: 0.4rem;
    margin-top: 0.5rem;
    padding-top: 0.4rem;
    border-top: 1px solid var(--border);
}

.msg-action-btn {
    padding: 0.2rem 0.5rem;
    font-size: 0.7rem;
    background: transparent;
    color: var(--text-muted);
    border: 1px solid var(--border);
    border-radius: 4px;
    cursor: pointer;
    transition: background 0.2s, color 0.2s;
}

.msg-action-btn:hover {
    background: var(--bg-secondary);
    color: var(--text);
}

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

/* Thinking indicator */
.thinking {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    color: var(--text-muted);
    font-size: 0.85rem;
    padding: 0.5rem 1rem;
}

.thinking-spinner {
    width: 18px;
    height: 18px;
    border: 2px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    flex-shrink: 0;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.thinking-text::after {
    content: "";
    animation: dots 1.5s steps(4, end) infinite;
}

@keyframes dots {
    0%   { content: ""; }
    25%  { content: "."; }
    50%  { content: ".."; }
    75%  { content: "..."; }
}

.thinking-elapsed {
    font-size: 0.75rem;
    opacity: 0.6;
    font-variant-numeric: tabular-nums;
}

/* Input area */
#chat-form {
    border-top: 1px solid var(--border);
    padding: 0.75rem 1rem;
    background: var(--bg-secondary);
    flex-shrink: 0;
}

.input-wrapper {
    display: flex;
    gap: 0.5rem;
    align-items: flex-end;
}

#message-input {
    flex: 1;
    padding: 0.6rem 0.8rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--input-bg);
    color: var(--text);
    font-family: inherit;
    font-size: 0.95rem;
    resize: none;
    max-height: 150px;
    line-height: 1.4;
}

#message-input:focus {
    outline: none;
    border-color: var(--accent);
}

#send-btn {
    flex-shrink: 0;
    padding: 0.6rem 1rem;
}

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

/* Mobile */
@media (max-width: 600px) {
    .message {
        max-width: 90%;
    }

    header h1 {
        font-size: 0.95rem;
    }

    .login-box {
        margin: 1rem;
        padding: 2rem;
    }

    #sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        z-index: 100;
        width: 280px;
        transform: translateX(-100%);
        transition: transform 0.25s ease;
        box-shadow: 4px 0 12px var(--shadow);
    }

    #chat-screen.sidebar-open #sidebar {
        transform: translateX(0);
    }

    #sidebar-toggle {
        display: inline-block;
    }

    /* Overlay behind sidebar */
    #chat-screen.sidebar-open::before {
        content: "";
        position: fixed;
        inset: 0;
        z-index: 99;
        background: rgba(0, 0, 0, 0.4);
    }
}
