:root {
    --bg-primary: #1a1a2e;
    --bg-secondary: #16213e;
    --bg-tertiary: #0f3460;
    --text-primary: #e0e0e0;
    --text-secondary: #a0a0b0;
    --accent: #4a9eff;
    --accent-hover: #3a8eef;
    --user-bubble: #2a4a7f;
    --assistant-bubble: #252540;
    --tool-bg: #1e1e3a;
    --tool-border: #3a3a5c;
    --border: #2a2a4a;
    --input-bg: #1e1e38;
    --shadow: rgba(0, 0, 0, 0.3);
}

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

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
}

.app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    max-width: 900px;
    margin: 0 auto;
}

.header {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    text-align: center;
}

.header h1 {
    font-size: 1.3rem;
    color: var(--accent);
    font-weight: 600;
}

.subtitle {
    font-size: 0.8rem;
    color: var(--text-secondary);
    margin-top: 4px;
}

.chat-container {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
}

.messages {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

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

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

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

.message.assistant {
    align-self: flex-start;
}

.message-content {
    padding: 12px 16px;
    border-radius: 16px;
    line-height: 1.6;
    font-size: 0.95rem;
}

.message.user .message-content {
    background: var(--user-bubble);
    border-bottom-right-radius: 4px;
}

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

.message-content p {
    margin: 0.4em 0;
}

.message-content p:first-child {
    margin-top: 0;
}

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

.message-content h1, .message-content h2, .message-content h3,
.message-content h4, .message-content h5, .message-content h6 {
    margin: 0.8em 0 0.4em;
    color: var(--accent);
}

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

.message-content li {
    margin: 0.2em 0;
}

.message-content code {
    background: rgba(255, 255, 255, 0.08);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 0.88em;
}

.message-content pre {
    background: rgba(0, 0, 0, 0.3);
    padding: 12px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 0.5em 0;
}

.message-content pre code {
    background: none;
    padding: 0;
}

.message-content table {
    border-collapse: collapse;
    margin: 0.5em 0;
    width: 100%;
    font-size: 0.9em;
}

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

.message-content th {
    background: rgba(255, 255, 255, 0.05);
}

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

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

.message-content strong {
    color: #f0f0f0;
}

/* Tool usage indicators */
.tool-indicator {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 14px;
    background: var(--tool-bg);
    border: 1px solid var(--tool-border);
    border-radius: 10px;
    font-size: 0.82rem;
    color: var(--text-secondary);
    margin-bottom: 4px;
    max-width: 85%;
}

.tool-indicator .spinner {
    width: 14px;
    height: 14px;
    border: 2px solid var(--tool-border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

.tool-indicator.done .spinner {
    display: none;
}

.tool-indicator.done::before {
    content: "\2713";
    color: #4caf50;
    font-weight: bold;
    font-size: 0.9rem;
}

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

/* PDF reference buttons */
.pdf-ref-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    background: var(--bg-tertiary);
    border: 1px solid var(--accent);
    border-radius: 8px;
    color: var(--accent);
    font-size: 0.85rem;
    cursor: pointer;
    margin: 4px 4px 4px 0;
    transition: background 0.2s;
}

.pdf-ref-btn:hover {
    background: var(--user-bubble);
}

/* Input area */
.input-area {
    padding: 12px 20px 20px;
    border-top: 1px solid var(--border);
}

.input-wrapper {
    display: flex;
    gap: 8px;
    align-items: center;
    background: var(--input-bg);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 8px 12px;
    transition: border-color 0.2s;
}

.input-wrapper:focus-within {
    border-color: var(--accent);
}

#userInput {
    flex: 1;
    background: none;
    border: none;
    color: var(--text-primary);
    font-size: 0.95rem;
    font-family: inherit;
    resize: none;
    outline: none;
    max-height: 150px;
    line-height: 1.5;
}

#userInput::placeholder {
    color: var(--text-secondary);
}

#sendBtn {
    background: var(--accent);
    border: none;
    border-radius: 10px;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: white;
    transition: background 0.2s;
    flex-shrink: 0;
}

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

#sendBtn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* PDF Modal */
.modal {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    z-index: 100;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.modal.active {
    display: flex;
}

.modal-content {
    background: var(--bg-secondary);
    border-radius: 12px;
    width: 100%;
    max-width: 800px;
    height: 85vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    box-shadow: 0 8px 32px var(--shadow);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
}

.modal-title {
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
}

.pdf-nav {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 0 auto;
}

.pdf-nav-btn {
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    color: var(--text-primary);
    width: 36px;
    height: 32px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 1.1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

.pdf-nav-btn:hover {
    background: var(--user-bubble);
}

.pdf-nav-btn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

.pdf-page-input-wrap {
    display: flex;
    align-items: center;
    gap: 4px;
}

.pdf-page-input {
    width: 52px;
    text-align: center;
    background: var(--input-bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text-primary);
    font-size: 0.9rem;
    padding: 4px 6px;
    outline: none;
    -moz-appearance: textfield;
}

.pdf-page-input::-webkit-inner-spin-button,
.pdf-page-input::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.pdf-page-input:focus {
    border-color: var(--accent);
}

.pdf-page-total {
    color: var(--text-secondary);
    font-size: 0.85rem;
}

.modal-close {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0 4px;
    line-height: 1;
}

.modal-close:hover {
    color: var(--text-primary);
}

.pdf-canvas-wrap {
    flex: 1;
    overflow: auto;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    background: #525659;
    position: relative;
}

.pdf-canvas-wrap canvas {
    max-width: 100%;
    height: auto;
    display: block;
    box-shadow: 0 2px 12px rgba(0,0,0,0.4);
}

.pdf-loading {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: var(--text-secondary);
    font-size: 0.95rem;
    display: none;
}

.pdf-loading.visible {
    display: block;
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 6px;
}

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

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

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

/* Mobile */
@media (max-width: 640px) {
    .app {
        max-width: 100%;
    }

    .message {
        max-width: 92%;
    }

    .tool-indicator {
        max-width: 92%;
    }

    .header h1 {
        font-size: 1.1rem;
    }

    .subtitle {
        font-size: 0.72rem;
    }

    .modal-content {
        height: 90vh;
        border-radius: 8px;
    }
}
