/* AutoClass — Racing Theme */

:root {
    /* Dark motorsport palette */
    --bg-primary: #0a0a0f;
    --bg-secondary: #101018;
    --bg-input: #14141e;
    --bg-user-msg: #1a1220;
    --bg-assistant-msg: #0f0f18;
    --bg-tool: #0c0c14;
    --bg-hover: rgba(220, 60, 30, 0.04);

    /* Text */
    --text-primary: #e0ddd8;
    --text-secondary: #9a9590;
    --text-muted: #5e5a56;

    /* Racing red/orange accent */
    --accent: #d44020;
    --accent-hover: #e85530;
    --accent-glow: rgba(212, 64, 32, 0.12);
    --accent-subtle: rgba(212, 64, 32, 0.06);
    --accent-secondary: #e88a30;

    /* Supporting colors */
    --border: #1e1e2a;
    --border-subtle: rgba(255, 255, 255, 0.04);
    --error: #e85d52;
    --error-bg: rgba(232, 93, 82, 0.08);
    --success: #4ecca3;
    --success-bg: rgba(78, 204, 163, 0.08);
    --link: #7d9ddb;

    /* Spacing & shape */
    --radius: 14px;
    --radius-lg: 18px;

    /* Typography — system fonts */
    --font-body: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    --font-mono: 'SF Mono', 'Consolas', 'Liberation Mono', Menlo, monospace;
}

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

body {
    font-family: var(--font-body);
    background: var(--bg-primary);
    color: var(--text-primary);
    height: 100vh;
    overflow: hidden;
    -webkit-font-smoothing: antialiased;
}

/* ==================== Layout ==================== */

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

/* ==================== Header ==================== */

#header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 20px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-secondary);
}

#header::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 1px;
    background: linear-gradient(90deg, transparent, var(--accent-glow), transparent);
}

#header h1 {
    font-size: 1.2rem;
    font-weight: 700;
    color: var(--accent);
    letter-spacing: 0.02em;
    display: flex;
    align-items: baseline;
    gap: 10px;
}

.subtitle {
    font-size: 0.75rem;
    font-weight: 400;
    color: var(--text-muted);
    letter-spacing: 0.03em;
    text-transform: uppercase;
}

.btn-new-chat {
    padding: 6px 14px;
    background: transparent;
    color: var(--text-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    cursor: pointer;
    font-family: var(--font-body);
    font-size: 0.84rem;
    font-weight: 500;
    transition: color 0.15s, border-color 0.15s, background 0.15s;
}

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

.beta-tag {
    font-size: 0.6rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--accent);
    border: 1px solid var(--accent);
    border-radius: 4px;
    padding: 1px 5px;
    vertical-align: super;
    margin-left: 2px;
}

/* ==================== Main Chat Area ==================== */

#main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    overflow: hidden;
    background:
        radial-gradient(ellipse at 50% 0%, rgba(212, 64, 32, 0.015) 0%, transparent 60%),
        var(--bg-primary);
}

#messages {
    flex: 1;
    overflow-y: auto;
    padding: 24px 20px;
}

/* ==================== User Messages ==================== */

.message {
    max-width: 780px;
    margin: 0 auto 16px;
    padding: 12px 16px;
    border-radius: var(--radius);
    line-height: 1.65;
}

.message-user {
    background: var(--bg-user-msg);
    margin-left: auto;
    margin-right: 0;
    max-width: 600px;
    border: 1px solid var(--border);
    border-radius: var(--radius) var(--radius) 6px var(--radius);
}

.message-role {
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--accent);
    margin-bottom: 4px;
    text-transform: uppercase;
    letter-spacing: 0.1em;
}

.message-content {
    font-size: 0.95rem;
    word-wrap: break-word;
    line-height: 1.65;
}

/* ==================== Turn Container (Streaming) ==================== */

.turn {
    max-width: 780px;
    margin: 0 auto 16px;
}

.turn-text {
    background: var(--bg-assistant-msg);
    padding: 14px 18px;
    border-radius: var(--radius);
    margin-bottom: 4px;
    line-height: 1.65;
    font-size: 0.95rem;
    word-wrap: break-word;
}

.turn-text p { margin-bottom: 0.5em; }
.turn-text p:last-child { margin-bottom: 0; }

/* ==================== Tool Blocks ==================== */

.tool-block {
    background: var(--bg-tool);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin: 6px 0;
    overflow: hidden;
    font-size: 0.86rem;
}

.tool-block-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    color: var(--text-secondary);
    font-weight: 600;
    letter-spacing: 0.01em;
}

.tool-block-header .spinner {
    width: 13px;
    height: 13px;
    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); }
}

.tool-check {
    color: var(--success);
    font-weight: bold;
    font-size: 0.92rem;
    flex-shrink: 0;
}

.tool-block-name {
    flex: 1;
}

.tool-block-details {
    display: none;
    padding: 6px 12px 8px;
    border-top: 1px solid var(--border-subtle);
}

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

.tool-block-input {
    color: var(--text-muted);
    font-family: var(--font-mono);
    font-size: 0.80rem;
    line-height: 1.55;
    white-space: pre-wrap;
    word-break: break-all;
    margin-bottom: 4px;
}

.tool-block-input code {
    background: none;
    padding: 0;
    font-size: inherit;
    color: var(--text-secondary);
}

.tool-block-result {
    color: var(--text-muted);
    font-size: 0.80rem;
    font-family: var(--font-mono);
    white-space: pre-wrap;
    word-break: break-all;
    max-height: 150px;
    overflow-y: auto;
}

.tool-block-result:empty {
    display: none;
}

.tool-block-toggle {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 0.75rem;
    padding: 2px 6px;
    border-radius: 3px;
    margin-left: auto;
}

.tool-block-toggle:hover {
    color: var(--text-secondary);
    background: var(--accent-subtle);
}

.tool-block.complete {
    border-color: rgba(78, 204, 163, 0.15);
}

.tool-block.error {
    border-color: rgba(220, 80, 80, 0.25);
}

.tool-block.error .tool-block-result {
    color: var(--error);
}

.tool-error-icon {
    color: var(--error);
    font-weight: bold;
    font-size: 0.92rem;
    flex-shrink: 0;
}

.stopped-note {
    color: var(--text-muted);
    font-size: 0.84rem;
    font-style: italic;
    padding: 4px 0;
}

/* ==================== Loading Indicator ==================== */

.loading-indicator {
    max-width: 780px;
    margin: 8px auto;
    display: flex;
    justify-content: center;
}

.loading-indicator-inner {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    padding: 10px 20px;
    background: var(--bg-assistant-msg);
    border-radius: var(--radius);
}

.loading-wheel {
    width: 26px;
    height: 26px;
    flex-shrink: 0;
    animation: spin-wheel 2.5s linear infinite;
}

.loading-wheel svg {
    width: 100%;
    height: 100%;
    display: block;
}

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

.loading-text {
    font-size: 0.86rem;
    font-style: italic;
    color: var(--text-secondary);
    min-width: 110px;
    text-align: center;
    white-space: nowrap;
}

/* ==================== Block Feedback ==================== */

.block-feedback {
    display: flex;
    align-items: center;
    gap: 2px;
    margin-top: 4px;
    opacity: 0;
    transition: opacity 0.15s;
}

.turn-text:hover .block-feedback,
.tool-block:hover .block-feedback,
.block-feedback.has-feedback {
    opacity: 1;
}

.feedback-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 3px 5px;
    border-radius: 4px;
    font-size: 0.78rem;
    transition: color 0.15s, background 0.15s;
    line-height: 1;
    display: inline-flex;
    align-items: center;
}

.feedback-btn:hover {
    background: var(--accent-subtle);
    color: var(--text-secondary);
}

.feedback-btn.active-up {
    color: var(--success);
}

.feedback-btn.active-down {
    color: var(--error);
}

/* ==================== Feedback Popover ==================== */

.feedback-popover {
    position: absolute;
    bottom: calc(100% + 4px);
    right: 0;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 8px;
    width: 250px;
    z-index: 200;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
}

.feedback-popover textarea {
    width: 100%;
    padding: 6px 8px;
    background: var(--bg-input);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 4px;
    font-family: var(--font-body);
    font-size: 0.84rem;
    resize: vertical;
    min-height: 40px;
    margin-bottom: 6px;
}

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

.feedback-popover textarea::placeholder {
    color: var(--text-muted);
    font-style: italic;
}

.feedback-submit-btn {
    display: block;
    width: 100%;
    padding: 5px 10px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-family: var(--font-body);
    font-size: 0.82rem;
    font-weight: 600;
}

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

/* ==================== Citation Links ==================== */

.citation-link {
    color: var(--link);
    text-decoration: none;
    border-bottom: 1px dotted var(--link);
    transition: color 0.15s, border-color 0.15s;
}

.citation-link:hover {
    color: var(--accent);
    border-color: var(--accent);
}

/* ==================== Markdown Rendering ==================== */

.message-content h1, .message-content h2, .message-content h3,
.turn-text h1, .turn-text h2, .turn-text h3 {
    margin-top: 0.8em;
    margin-bottom: 0.4em;
    color: var(--text-primary);
    letter-spacing: -0.01em;
}

.message-content h1, .turn-text h1 { font-size: 1.3rem; font-weight: 700; }
.message-content h2, .turn-text h2 { font-size: 1.1rem; font-weight: 600; }
.message-content h3, .turn-text h3 { font-size: 1.0rem; font-weight: 600; }

.message-content code, .turn-text code {
    background: rgba(212, 64, 32, 0.06);
    padding: 2px 6px;
    border-radius: 3px;
    font-family: var(--font-mono);
    font-size: 0.88em;
    color: var(--accent-secondary);
}

.message-content pre, .turn-text pre {
    background: rgba(0, 0, 0, 0.35);
    padding: 12px 14px;
    border-radius: var(--radius);
    overflow-x: auto;
    margin: 8px 0;
    border: 1px solid var(--border);
}

.message-content pre code, .turn-text pre code {
    background: none;
    padding: 0;
    color: var(--text-primary);
    font-size: 0.84rem;
    line-height: 1.55;
}

.message-content ul, .message-content ol,
.turn-text ul, .turn-text ol {
    padding-left: 1.5em;
    margin: 0.5em 0;
}

.message-content li, .turn-text li {
    margin-bottom: 0.25em;
}

.message-content a, .turn-text a {
    color: var(--link);
    text-decoration: none;
    transition: color 0.15s;
}

.message-content a:hover, .turn-text a:hover {
    color: var(--accent);
    text-decoration: underline;
}

.message-content p, .turn-text p {
    margin-bottom: 0.5em;
}

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

/* ==================== Error Messages ==================== */

.error-message {
    background: var(--error-bg);
    border: 1px solid rgba(232, 93, 82, 0.25);
    border-radius: var(--radius);
    padding: 10px 14px;
    color: var(--error);
    font-size: 0.92rem;
    margin: 8px auto;
    max-width: 780px;
}

/* ==================== Input Area ==================== */

#input-area {
    padding: 12px 20px 16px;
    max-width: 840px;
    margin: 0 auto;
    width: 100%;
}

#input-area-inner {
    display: flex;
    align-items: flex-end;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: 22px;
    padding: 6px 6px 6px 18px;
    transition: border-color 0.2s, box-shadow 0.2s;
}

#input-area-inner:focus-within {
    border-color: rgba(212, 64, 32, 0.4);
    box-shadow: 0 0 0 3px rgba(212, 64, 32, 0.08);
}

#user-input {
    flex: 1;
    padding: 5px 8px 5px 0;
    background: transparent;
    color: var(--text-primary);
    border: none;
    font-family: var(--font-body);
    font-size: 0.95rem;
    resize: none;
    min-height: 24px;
    max-height: 150px;
    line-height: 1.5;
    overflow-y: auto;
    word-wrap: break-word;
}

#user-input:focus {
    outline: none;
}

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

#send-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    min-width: 32px;
    min-height: 32px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    flex-shrink: 0;
    transition: background 0.15s, transform 0.1s;
    margin-bottom: 1px;
}

#send-btn:hover {
    background: var(--accent-hover);
}

#send-btn:active {
    transform: scale(0.93);
}

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

#send-btn.stop {
    background: #555;
}

#send-btn.stop:hover {
    background: #666;
}

/* ==================== Context Warning ==================== */

.context-warning {
    display: flex;
    align-items: center;
    justify-content: center;
    color: #e8a020;
    cursor: pointer;
    flex-shrink: 0;
    padding: 4px;
    position: relative;
}

.context-warning.hidden {
    display: none;
}

.context-warning-tooltip {
    position: absolute;
    bottom: calc(100% + 8px);
    left: 0;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 10px 14px;
    width: 260px;
    font-size: 0.82rem;
    color: var(--text-secondary);
    line-height: 1.5;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
    z-index: 200;
}

/* ==================== Disclaimer ==================== */

#disclaimer {
    text-align: center;
    font-size: 0.72rem;
    color: var(--text-muted);
    padding: 4px 20px 12px;
    max-width: 840px;
    margin: 0 auto;
    width: 100%;
    line-height: 1.4;
}

#disclaimer a {
    color: var(--text-muted);
    text-decoration: underline;
    text-underline-offset: 2px;
}

#disclaimer a:hover {
    color: var(--text-secondary);
}

/* ==================== Scrollbar ==================== */

::-webkit-scrollbar {
    width: 5px;
    height: 5px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

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

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* ==================== Responsive ==================== */

@supports (height: 100dvh) {
    body, #app { height: 100dvh; }
}

@media (max-width: 768px) {
    body, #app {
        height: 100dvh;
        height: 100vh;
    }

    #header {
        padding: 10px 14px;
    }

    #header h1 {
        font-size: 1.05rem;
    }

    .subtitle {
        display: none;
    }

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

    #input-area {
        padding: 8px 10px 12px;
    }

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

    #send-btn {
        width: 38px;
        height: 38px;
    }

    #messages {
        padding: 16px 12px;
    }

    .message {
        padding: 10px 12px;
    }

    /* Feedback always visible on touch */
    .block-feedback {
        opacity: 1;
    }

    .feedback-btn {
        padding: 6px 8px;
        font-size: 0.88rem;
    }

    #disclaimer {
        font-size: 0.68rem;
        padding: 4px 12px 8px;
    }
}
