:root {
    --bg: #f5f5f7;
    --surface: #ffffff;
    --text: #1d1d1f;
    --text-2: #424245;
    --muted: #86868b;
    --line: rgba(0, 0, 0, 0.08);
    --line-strong: rgba(0, 0, 0, 0.14);
    --accent: #0b8f89;
    --accent-hover: #087a75;
    --accent-soft: rgba(11, 143, 137, 0.1);
    --accent-ring: rgba(11, 143, 137, 0.35);
    --danger: #d70015;
    --danger-soft: rgba(215, 0, 21, 0.07);
    --radius-xl: 22px;
    --radius-md: 12px;
    --shadow-md: 0 1px 2px rgba(0, 0, 0, 0.04), 0 12px 40px rgba(0, 0, 0, 0.08);
    --font: 'Inter', -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', Roboto, sans-serif;
    --mono: ui-monospace, 'SFMono-Regular', Consolas, 'Liberation Mono', monospace;
    --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
}

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

html {
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
}

body {
    min-height: 100vh;
    min-height: 100dvh;
    margin: 0;
    padding: 28px 16px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font);
    font-size: 15px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

button,
textarea {
    font: inherit;
}

/* Mode selector: segmented control */
#mode-selector {
    display: inline-flex;
    gap: 2px;
    padding: 3px;
    margin-bottom: 14px;
    background: rgba(118, 118, 128, 0.12);
    border-radius: 999px;
}

.mode-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 7px 14px;
    border: 0;
    border-radius: 999px;
    background: transparent;
    color: var(--muted);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s var(--ease), color 0.2s var(--ease), box-shadow 0.2s var(--ease);
}

.mode-btn svg {
    width: 15px;
    height: 15px;
    stroke-width: 2;
}

.mode-btn:hover {
    color: var(--text-2);
}

.mode-btn:focus-visible {
    outline: 2px solid var(--accent-ring);
    outline-offset: 1px;
}

.mode-btn.active {
    background: var(--surface);
    color: var(--accent);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08), 0 0 0 0.5px rgba(0, 0, 0, 0.04);
}

/* Top bar */
#chat-controls {
    width: 100%;
    max-width: 760px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    height: 36px;
    margin-bottom: 6px;
}

#clear-data-container {
    display: flex;
    align-items: center;
}

/* Shared ghost icon button */
.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    padding: 0;
    flex-shrink: 0;
    border: 0;
    border-radius: 999px;
    background: transparent;
    color: var(--muted);
    cursor: pointer;
    transition: background 0.2s var(--ease), color 0.2s var(--ease), transform 0.15s var(--ease);
}

.icon-btn svg {
    width: 18px;
    height: 18px;
    stroke-width: 1.75;
}

.icon-btn:hover {
    background: rgba(0, 0, 0, 0.05);
    color: var(--text);
}

.icon-btn:active {
    transform: scale(0.94);
}

.icon-btn:focus-visible {
    outline: 2px solid var(--accent-ring);
    outline-offset: 1px;
}

#clear-cache-btn:hover,
#clear-btn:hover {
    background: var(--danger-soft);
    color: var(--danger);
}

/* Chat container: a single floating panel */
#chat-container {
    position: relative;
    width: 100%;
    max-width: 760px;
    height: min(calc(100dvh - 150px), 760px);
    min-height: 380px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-md);
}

#chat-box {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 28px 28px 12px;
    overscroll-behavior: contain;
    scroll-behavior: smooth;
}

#chat-box::-webkit-scrollbar {
    width: 8px;
}

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

#chat-box::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.14);
    border: 2px solid transparent;
    background-clip: content-box;
    border-radius: 999px;
}

/* Messages */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(4px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

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

.message {
    display: flex;
    flex-direction: column;
    width: 100%;
    margin-bottom: 22px;
    animation: fadeIn 0.18s var(--ease);
}

.message:last-child {
    margin-bottom: 4px;
}

.message-content {
    position: relative;
    min-width: 0;
    max-width: 100%;
    overflow-wrap: anywhere;
}

.message-text {
    white-space: pre-wrap;
    word-break: break-word;
    line-height: 1.5;
}

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

.user-message .message-content {
    width: fit-content;
    max-width: 80%;
    padding: 9px 14px;
    background: var(--accent);
    color: #ffffff;
    border-radius: 18px 18px 6px 18px;
}

.user-message .image-info {
    font-size: 12px;
    opacity: 0.85;
}

.user-message .chat-image {
    display: block;
    max-width: 100%;
    max-height: 260px;
    margin-bottom: 6px;
    border-radius: 12px;
    cursor: zoom-in;
}

.bot-message {
    align-items: flex-start;
}

.bot-message .message-content {
    width: 100%;
    color: var(--text);
}

.chat-image {
    max-width: 100%;
    max-height: 280px;
    border-radius: 12px;
    border: 1px solid var(--line);
    cursor: zoom-in;
}

.error-message {
    color: var(--danger);
    font-weight: 500;
}

/* Assistant response */
.assistant-response {
    width: 100%;
}

.assistant-response-body {
    color: var(--text);
    font-size: 15px;
    line-height: 1.7;
    word-break: break-word;
    overflow-wrap: anywhere;
}

.assistant-response-body > * {
    margin: 0 0 12px;
}

.assistant-response-body > *:last-child {
    margin-bottom: 0;
}

.assistant-response-body h1,
.assistant-response-body h2,
.assistant-response-body h3,
.assistant-response-body h4 {
    margin: 20px 0 8px;
    color: var(--text);
    font-weight: 600;
    line-height: 1.3;
    letter-spacing: -0.01em;
}

.assistant-response-body h1 { font-size: 1.35rem; }
.assistant-response-body h2 { font-size: 1.2rem; }
.assistant-response-body h3 { font-size: 1.05rem; }
.assistant-response-body h4 { font-size: 1rem; }

.assistant-response-body p {
    margin: 0 0 12px;
}

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

.assistant-response-body strong {
    font-weight: 600;
}

.assistant-response-body ul,
.assistant-response-body ol {
    padding-left: 1.4rem;
}

.assistant-response-body li + li {
    margin-top: 0.3rem;
}

.assistant-response-body li > p {
    margin: 0;
}

.assistant-response-body blockquote {
    padding: 2px 0 2px 14px;
    border-left: 2px solid var(--line-strong);
    color: var(--text-2);
}

.assistant-response-body blockquote > *:last-child {
    margin-bottom: 0;
}

.assistant-response-body hr {
    border: 0;
    border-top: 1px solid var(--line);
    margin: 18px 0;
}

.assistant-response-body a {
    color: var(--accent);
    text-decoration: underline;
    text-decoration-color: rgba(11, 143, 137, 0.35);
    text-underline-offset: 2px;
}

.assistant-response-body a:hover {
    text-decoration-color: var(--accent);
}

.assistant-response-body code {
    padding: 0.12em 0.4em;
    border-radius: 6px;
    background: rgba(0, 0, 0, 0.05);
    color: var(--text);
    font-family: var(--mono);
    font-size: 0.87em;
}

.response-callout {
    padding: 12px 14px;
    border-radius: var(--radius-md);
    background: var(--accent-soft);
    color: var(--text);
}

/* Code blocks */
.code-block-wrapper {
    overflow: hidden;
    background: var(--bg);
    border: 1px solid var(--line);
    border-radius: var(--radius-md);
}

.code-block-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 4px 6px 4px 14px;
    border-bottom: 1px solid var(--line);
}

.code-lang {
    font-size: 12px;
    font-weight: 500;
    color: var(--muted);
}

.code-copy-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 5px 8px;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--muted);
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s var(--ease), color 0.2s var(--ease);
}

.code-copy-btn svg {
    width: 14px;
    height: 14px;
    stroke-width: 2;
}

.code-copy-btn:hover {
    background: rgba(0, 0, 0, 0.05);
    color: var(--text);
}

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

.code-block-wrapper pre,
.assistant-response-body pre {
    margin: 0;
    padding: 14px 16px;
    overflow-x: auto;
    background: transparent;
    border: 0;
}

.code-block-wrapper pre code,
.assistant-response-body pre code {
    padding: 0;
    border: 0;
    background: transparent;
    color: var(--text);
    font-family: var(--mono);
    font-size: 13.5px;
    line-height: 1.6;
    white-space: pre;
}

/* Tables */
.table-scroll {
    overflow-x: auto;
    border: 1px solid var(--line);
    border-radius: var(--radius-md);
}

.assistant-response-body table {
    width: 100%;
    min-width: 360px;
    border-collapse: collapse;
    font-size: 14px;
}

.assistant-response-body th,
.assistant-response-body td {
    padding: 9px 12px;
    border-bottom: 1px solid var(--line);
    text-align: left;
    vertical-align: top;
}

.assistant-response-body th {
    background: rgba(0, 0, 0, 0.025);
    color: var(--text-2);
    font-weight: 600;
}

.assistant-response-body tr:last-child td {
    border-bottom: 0;
}

/* Math */
.math-block {
    overflow-x: auto;
    overflow-y: hidden;
    padding: 4px 0;
}

.math-inline {
    white-space: nowrap;
}

.assistant-response-body mjx-container {
    max-width: 100%;
}

.assistant-response-body mjx-container[display="true"] {
    display: block;
    margin: 10px 0 !important;
    overflow-x: auto;
    overflow-y: hidden;
}

.assistant-response-body mjx-container[jax="CHTML"] {
    font-size: 1.02em !important;
}

/* Message actions */
.message-toolbar {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 6px;
}

.copy-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin-left: -8px;
    padding: 5px 8px;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--muted);
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s var(--ease), color 0.2s var(--ease);
}

.copy-btn svg {
    width: 14px;
    height: 14px;
    stroke-width: 2;
}

.copy-btn:hover {
    background: rgba(0, 0, 0, 0.05);
    color: var(--text);
}

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

.message-meta {
    font-size: 12px;
    color: var(--muted);
}

/* Streaming */
.streaming-content {
    min-height: 1.6rem;
}

.streaming-indicator {
    display: flex;
    align-items: center;
    min-height: 1.4rem;
}

.streaming-cursor {
    display: inline-block;
    width: 2px;
    height: 1.1em;
    margin-left: 1px;
    background: var(--accent);
    border-radius: 1px;
    vertical-align: text-bottom;
    animation: cursorBlink 1s steps(2) infinite;
}

.btn-loading {
    position: relative;
    color: transparent !important;
}

.btn-loading::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 16px;
    height: 16px;
    margin: -8px 0 0 -8px;
    border: 2px solid #ffffff;
    border-top-color: transparent;
    border-radius: 50%;
    animation: spin 0.8s infinite linear;
}

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

/* Composer */
#input-container {
    position: relative;
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: 6px;
    padding: 10px 14px 14px;
    background: var(--surface);
    border-top: 1px solid var(--line);
}

#user-input {
    flex: 1;
    min-width: 0;
    min-height: 40px;
    max-height: 120px;
    margin: 0 4px;
    padding: 10px 14px;
    resize: none;
    overflow-y: auto;
    background: var(--bg);
    color: var(--text);
    border: 1px solid transparent;
    border-radius: 20px;
    font-size: 15px;
    line-height: 1.45;
    outline: none;
    white-space: pre-wrap;
    word-wrap: break-word;
    transition: background 0.2s var(--ease), border-color 0.2s var(--ease), box-shadow 0.2s var(--ease);
}

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

#user-input:focus {
    background: var(--surface);
    border-color: var(--accent-ring);
    box-shadow: 0 0 0 3px var(--accent-soft);
}

#user-input.drag-over {
    background: var(--accent-soft);
    border-color: var(--accent);
}

#user-input::-webkit-scrollbar {
    width: 4px;
}

#user-input::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.15);
    border-radius: 2px;
}

#send-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    padding: 0;
    flex-shrink: 0;
    border: 0;
    border-radius: 50%;
    background: var(--accent);
    color: #ffffff;
    cursor: pointer;
    transition: background 0.2s var(--ease), transform 0.15s var(--ease), opacity 0.2s var(--ease);
}

#send-btn svg {
    width: 18px;
    height: 18px;
    stroke-width: 2.25;
}

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

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

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

#send-btn:focus-visible {
    outline: 2px solid var(--accent-ring);
    outline-offset: 2px;
}

/* Image attachment */
#image-preview-container {
    order: -1;
    position: relative;
    flex-basis: 100%;
    width: fit-content;
    max-width: 220px;
    margin: 0 0 4px 4px;
}

#image-preview {
    display: block;
    max-width: 100%;
    max-height: 150px;
    border: 1px solid var(--line);
    border-radius: 12px;
}

#remove-image {
    position: absolute;
    top: -6px;
    right: -6px;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 22px;
    height: 22px;
    padding: 0;
    border: 2px solid var(--surface);
    border-radius: 50%;
    background: rgba(29, 29, 31, 0.85);
    color: #ffffff;
    cursor: pointer;
}

#remove-image svg {
    width: 12px;
    height: 12px;
    stroke-width: 2.5;
}

#remove-image:hover {
    background: var(--danger);
}

/* OCR progress */
#ocr-progress {
    display: none;
    padding: 12px 16px;
    background: var(--surface);
    border-top: 1px solid var(--line);
}

.progress-container {
    display: flex;
    align-items: center;
    gap: 14px;
}

.progress-info {
    min-width: 120px;
}

.progress-text {
    margin-bottom: 2px;
    font-size: 12px;
    color: var(--muted);
}

.progress-percentage {
    font-size: 14px;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    color: var(--text);
}

.progress-bar {
    flex: 1;
    height: 4px;
    background: rgba(0, 0, 0, 0.08);
    border-radius: 999px;
    overflow: hidden;
}

.progress-fill {
    height: 100%;
    width: 0%;
    background: var(--accent);
    border-radius: inherit;
    transition: width 0.4s var(--ease);
}

/* Image modal */
#image-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 9999;
    cursor: zoom-out;
}

#image-modal img {
    max-width: 90%;
    max-height: 90%;
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

/* Mobile */
@media (max-width: 768px) {
    body {
        height: 100vh;
        height: 100dvh;
        padding: 0;
        background: var(--surface);
        align-items: stretch;
        justify-content: flex-start;
    }

    #mode-selector {
        align-self: center;
        margin: 10px 0 8px;
    }

    #chat-controls {
        max-width: none;
        height: auto;
        margin: 0;
        padding: 0 12px;
    }

    #chat-controls .icon-btn {
        margin-bottom: 4px;
    }

    #chat-container {
        flex: 1;
        width: 100%;
        max-width: none;
        height: auto;
        min-height: 0;
        margin: 0;
        border: 0;
        border-radius: 0;
        box-shadow: none;
    }

    #chat-box {
        padding: 16px 16px 8px;
    }

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

    .assistant-response-body {
        font-size: 15px;
        line-height: 1.65;
    }

    #input-container {
        padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
    }

    #user-input {
        font-size: 16px;
    }
}
