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

:root {
    --bg: #F9F7F3;
    --surface: #FFFFFF;
    --text: #2D2D2D;
    --text-secondary: #6B6560;
    --text-muted: #9B9590;
    --border: #E8E5E0;
    --border-light: #F0EDE8;
    --user-bg: #EDE9E3;
    --accent: #C96442;
    --accent-hover: #B55838;
    --tool-bg: #F5F0EB;
    --tool-border: #DDD7D0;
    --error-bg: #FEF2F2;
    --error-border: #EF4444;
    --error-text: #991B1B;
    --system-bg: #F0EDE8;
    --max-width: 720px;
    --radius-sm: 8px;
    --radius-md: 16px;
    --radius-lg: 24px;
    --radius-full: 9999px;
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

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

/* ── Header ── */
header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 24px;
    background: transparent;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.header-brand {
    display: flex;
    align-items: center;
    gap: 10px;
}

.header-brand svg {
    color: var(--accent);
    flex-shrink: 0;
}

header h1 {
    font-size: 16px;
    font-weight: 600;
    color: var(--text);
}

.header-actions {
    display: flex;
    gap: 6px;
}

.header-actions button {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 7px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text-secondary);
    font-size: 13px;
    cursor: pointer;
    transition: all 0.15s ease;
}

.header-actions button:hover:not(:disabled) {
    background: var(--border-light);
    border-color: var(--tool-border);
    color: var(--text);
}

.header-actions button:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.header-actions button svg {
    flex-shrink: 0;
}

/* ── Chat container ── */
#chat-container {
    flex: 1;
    overflow-y: auto;
    padding: 24px 16px;
}

#messages {
    display: flex;
    flex-direction: column;
    gap: 4px;
    max-width: var(--max-width);
    margin: 0 auto;
}

/* ── Messages ── */
.message {
    font-size: 15px;
    line-height: 1.65;
    word-wrap: break-word;
    overflow-wrap: break-word;
}

.message.user {
    align-self: flex-end;
    max-width: 80%;
    background: var(--user-bg);
    color: var(--text);
    padding: 10px 16px;
    border-radius: var(--radius-md);
    border-bottom-right-radius: 4px;
    margin-top: 16px;
    white-space: pre-wrap;
}

.message.assistant {
    position: relative;
    padding: 8px 0;
    margin-bottom: 4px;
}

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

.message-content {
    line-height: 1.7;
}

/* ── Copy button ── */
.copy-btn {
    position: absolute;
    top: 8px;
    right: -8px;
    opacity: 0;
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 4px 8px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--surface);
    color: var(--text-muted);
    font-size: 12px;
    cursor: pointer;
    transition: all 0.15s ease;
    z-index: 2;
}

.copy-btn:hover {
    background: var(--border-light);
    color: var(--text-secondary);
}

.copy-btn.copied {
    color: var(--accent);
    border-color: var(--accent);
}

/* ── Tool activity ── */
.message.tool-activity {
    margin: 8px 0;
}

.tool-toggle {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 12px;
    background: var(--tool-bg);
    border: 1px solid var(--tool-border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 13px;
    color: var(--text-secondary);
    transition: background 0.15s ease;
    user-select: none;
}

.tool-toggle:hover {
    background: var(--border-light);
}

.tool-toggle .chevron {
    transition: transform 0.2s ease;
    flex-shrink: 0;
}

.tool-toggle.expanded .chevron {
    transform: rotate(90deg);
}

.tool-toggle .tool-label {
    flex: 1;
}

.tool-toggle .tool-status {
    font-size: 12px;
    color: var(--text-muted);
}

.tool-details {
    display: none;
    padding: 8px 12px;
    margin-top: 4px;
    background: var(--tool-bg);
    border: 1px solid var(--tool-border);
    border-radius: var(--radius-sm);
    font-size: 12px;
    color: var(--text-secondary);
    white-space: pre-wrap;
    word-break: break-word;
    max-height: 200px;
    overflow-y: auto;
}

.tool-details.visible {
    display: block;
}

/* ── Error and system messages ── */
.message.error {
    padding: 10px 16px;
    background: var(--error-bg);
    border: 1px solid var(--error-border);
    border-radius: var(--radius-sm);
    color: var(--error-text);
    font-size: 13px;
    margin: 8px 0;
}

.message.system {
    align-self: center;
    background: var(--system-bg);
    color: var(--text-secondary);
    font-size: 13px;
    text-align: center;
    padding: 6px 16px;
    border-radius: var(--radius-full);
    margin: 16px 0;
}

/* ── Typing indicator ── */
.typing-indicator {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 12px 4px;
}

.typing-indicator span {
    display: inline-block;
    width: 6px;
    height: 6px;
    background: var(--text-muted);
    border-radius: 50%;
    animation: typing 1.4s infinite;
}

.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing {
    0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
    30% { opacity: 1; transform: translateY(-3px); }
}

/* ── Downloads modal overlay ── */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 100;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(2px);
}

.modal-overlay.visible {
    display: flex;
}

.modal-content {
    background: var(--surface);
    border-radius: var(--radius-md);
    padding: 24px;
    width: min(90vw, 480px);
    max-height: 70vh;
    overflow-y: auto;
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.16);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
}

.modal-header h3 {
    font-size: 16px;
    font-weight: 600;
}

.modal-close {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
    transition: background 0.15s ease;
}

.modal-close:hover {
    background: var(--border-light);
    color: var(--text);
}

#downloads-list {
    list-style: none;
}

#downloads-list li {
    padding: 10px 12px;
    border-bottom: 1px solid var(--border-light);
    display: flex;
    align-items: center;
    gap: 10px;
}

#downloads-list li:last-child {
    border-bottom: none;
}

#downloads-list li svg {
    flex-shrink: 0;
    color: var(--text-muted);
}

#downloads-list a {
    color: var(--accent);
    text-decoration: none;
    font-size: 14px;
    transition: color 0.15s ease;
}

#downloads-list a:hover {
    color: var(--accent-hover);
    text-decoration: underline;
}

#downloads-list .empty-state {
    color: var(--text-muted);
    font-size: 14px;
    text-align: center;
    padding: 24px 0;
}

/* ── Footer / Input area ── */
footer {
    padding: 16px 24px 20px;
    background: var(--bg);
    flex-shrink: 0;
}

.input-wrapper {
    position: relative;
    max-width: var(--max-width);
    margin: 0 auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.input-wrapper:focus-within {
    border-color: var(--tool-border);
    box-shadow: 0 0 0 3px rgba(201, 100, 66, 0.08);
}

#user-input {
    width: 100%;
    padding: 14px 52px 14px 16px;
    border: none;
    border-radius: var(--radius-md);
    font-size: 15px;
    font-family: inherit;
    resize: none;
    max-height: 160px;
    line-height: 1.5;
    outline: none;
    background: transparent;
    color: var(--text);
}

#user-input::placeholder {
    color: var(--text-muted);
}

#user-input:disabled {
    background: var(--border-light);
}

.input-actions {
    position: absolute;
    right: 8px;
    bottom: 8px;
    display: flex;
    gap: 4px;
}

#btn-send {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: none;
    border-radius: var(--radius-full);
    background: var(--accent);
    color: white;
    cursor: pointer;
    transition: all 0.15s ease;
}

#btn-send:hover:not(:disabled) {
    background: var(--accent-hover);
    transform: scale(1.05);
}

#btn-send:disabled {
    background: var(--border);
    color: var(--text-muted);
    cursor: not-allowed;
    transform: none;
}

#btn-stop {
    display: none;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: 2px solid var(--text-secondary);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.15s ease;
}

#btn-stop:hover {
    background: var(--error-bg);
    border-color: var(--error-border);
    color: var(--error-text);
}

#btn-stop.visible {
    display: flex;
}

#status-bar {
    max-width: var(--max-width);
    margin: 8px auto 0;
    text-align: center;
}

#status-text {
    font-size: 12px;
    color: var(--text-muted);
}

/* ── Markdown content styles ── */
.message-content h1,
.message-content h2,
.message-content h3,
.message-content h4 {
    margin-top: 20px;
    margin-bottom: 8px;
    font-weight: 600;
    line-height: 1.3;
}

.message-content h1 { font-size: 1.4em; }
.message-content h2 { font-size: 1.2em; }
.message-content h3 { font-size: 1.05em; }
.message-content h4 { font-size: 1em; }

.message-content h1:first-child,
.message-content h2:first-child,
.message-content h3:first-child {
    margin-top: 0;
}

.message-content p {
    margin-bottom: 12px;
}

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

.message-content ul,
.message-content ol {
    margin: 8px 0 12px 24px;
}

.message-content li {
    margin-bottom: 4px;
}

.message-content li > ul,
.message-content li > ol {
    margin-top: 4px;
    margin-bottom: 4px;
}

.message-content strong {
    font-weight: 600;
}

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

.message-content code {
    font-family: 'SF Mono', 'Menlo', 'Consolas', monospace;
    font-size: 0.88em;
    background: var(--tool-bg);
    padding: 2px 6px;
    border-radius: 4px;
}

.message-content pre {
    margin: 12px 0;
    background: #1E1E1E;
    color: #D4D4D4;
    border-radius: var(--radius-sm);
    overflow-x: auto;
}

.message-content pre code {
    display: block;
    padding: 14px 16px;
    background: transparent;
    font-size: 13px;
    line-height: 1.5;
    color: inherit;
}

.message-content blockquote {
    margin: 12px 0;
    padding: 8px 16px;
    border-left: 3px solid var(--accent);
    background: var(--tool-bg);
    color: var(--text-secondary);
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

.message-content table {
    width: 100%;
    border-collapse: collapse;
    margin: 12px 0;
    font-size: 14px;
}

.message-content th,
.message-content td {
    padding: 8px 12px;
    border: 1px solid var(--border);
    text-align: left;
}

.message-content th {
    background: var(--tool-bg);
    font-weight: 600;
}

.message-content hr {
    margin: 16px 0;
    border: none;
    border-top: 1px solid var(--border);
}

.message-content a {
    color: var(--accent);
    text-decoration: none;
}

.message-content a:hover {
    text-decoration: underline;
}

/* ── Scrollbar ── */
#chat-container::-webkit-scrollbar {
    width: 6px;
}

#chat-container::-webkit-scrollbar-track {
    background: transparent;
}

#chat-container::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 3px;
}

#chat-container::-webkit-scrollbar-thumb:hover {
    background: var(--tool-border);
}

/* ── Login screen ── */
#login-screen {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: var(--bg);
    padding: 24px;
}

.login-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 40px 36px;
    width: min(90vw, 400px);
    box-shadow: 0 4px 24px rgba(0, 0, 0, 0.06);
}

.login-brand {
    text-align: center;
    margin-bottom: 32px;
}

.login-brand svg {
    color: var(--accent);
    margin-bottom: 12px;
}

.login-brand h2 {
    font-size: 22px;
    font-weight: 700;
    color: var(--text);
    margin-bottom: 4px;
}

.login-brand p {
    font-size: 14px;
    color: var(--text-secondary);
}

.form-group {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

.form-group input {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 15px;
    font-family: inherit;
    color: var(--text);
    background: var(--surface);
    outline: none;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.form-group input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(201, 100, 66, 0.1);
}

.form-group input::placeholder {
    color: var(--text-muted);
}

.login-error {
    font-size: 13px;
    color: var(--error-text);
    margin-bottom: 12px;
    min-height: 18px;
}

.login-btn {
    width: 100%;
    padding: 11px 16px;
    border: none;
    border-radius: var(--radius-sm);
    background: var(--accent);
    color: white;
    font-size: 15px;
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    transition: background 0.15s ease;
}

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

/* ── Responsive ── */
@media (max-width: 640px) {
    header {
        padding: 10px 16px;
    }

    header h1 {
        font-size: 14px;
    }

    .header-actions button span {
        display: none;
    }

    .header-actions button {
        padding: 7px 10px;
    }

    #chat-container {
        padding: 16px 12px;
    }

    .message.user {
        max-width: 90%;
    }

    footer {
        padding: 12px 12px 16px;
    }

    #user-input {
        font-size: 16px; /* prevent zoom on iOS */
    }

    .copy-btn {
        opacity: 1;
    }
}
