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

body {
    font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, Roboto, sans-serif;
    background: #eef1f5;
    height: 100vh;
    display: flex;
    flex-direction: column;
}

.app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    max-width: 860px;
    margin: 0 auto;
    width: 100%;
    background: #fff;
    box-shadow: 0 0 40px rgba(0, 0, 0, 0.08);
}

/* Header */
.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 24px;
    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
    color: #fff;
    flex-shrink: 0;
}

.header h1 {
    font-size: 1.125rem;
    font-weight: 600;
    letter-spacing: 0.3px;
}

.product-selector {
    display: flex;
    align-items: center;
    gap: 8px;
}

.product-selector label {
    font-size: 0.8125rem;
    opacity: 0.7;
    font-weight: 400;
}

.product-selector select {
    padding: 5px 10px;
    border-radius: 6px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    background: rgba(255, 255, 255, 0.08);
    color: #fff;
    font-size: 0.8125rem;
    cursor: pointer;
    transition: border-color 0.2s;
}

.product-selector select:focus {
    outline: none;
    border-color: #7c74ff;
}

.product-selector select option {
    color: #333;
    background: #fff;
}

.auth-btn {
    background: none;
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 6px;
    color: rgba(255, 255, 255, 0.7);
    cursor: pointer;
    padding: 5px 7px;
    display: flex;
    align-items: center;
    transition: border-color 0.2s, color 0.2s;
    margin-left: 4px;
}

.auth-btn:hover {
    border-color: #7c74ff;
    color: #fff;
}

.auth-btn.has-token {
    border-color: #22c55e;
    color: #22c55e;
}

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

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

.modal {
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
    width: 400px;
    max-width: calc(100vw - 32px);
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid #e8eaed;
    font-size: 0.9375rem;
    font-weight: 600;
    color: #1e293b;
}

.modal-close {
    background: none;
    border: none;
    font-size: 1.25rem;
    color: #94a3b8;
    cursor: pointer;
    line-height: 1;
    padding: 2px 4px;
    border-radius: 4px;
}

.modal-close:hover {
    color: #1e293b;
    background: #f1f5f9;
}

.modal-body {
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.modal-body label {
    font-size: 0.8125rem;
    font-weight: 500;
    color: #64748b;
}

.modal-body input[type="password"] {
    padding: 9px 12px;
    border: 1.5px solid #d1d5db;
    border-radius: 8px;
    font-size: 0.875rem;
    outline: none;
    font-family: inherit;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.modal-body input[type="password"]:focus {
    border-color: #4f46e5;
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

.modal-footer {
    padding: 12px 20px 16px;
    display: flex;
    justify-content: flex-end;
}

.modal-save {
    padding: 8px 20px;
    background: #4f46e5;
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    font-family: inherit;
    transition: background 0.15s;
}

.modal-save:hover {
    background: #4338ca;
}

/* Messages */
.chat-container {
    flex: 1;
    overflow-y: auto;
    padding: 20px 24px;
    background: #f7f8fa;
}

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

.message {
    display: flex;
    max-width: 88%;
    animation: fadeIn 0.2s ease-out;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(4px); }
    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: 14px;
    font-size: 0.875rem;
    line-height: 1.65;
    word-wrap: break-word;
}

.message.user .message-content {
    background: #4f46e5;
    color: #fff;
    border-bottom-right-radius: 4px;
}

.message.assistant .message-content {
    background: #fff;
    color: #1e293b;
    border-bottom-left-radius: 4px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
    border: 1px solid #e8eaed;
}

/* Markdown formatting in assistant messages */
.message.assistant .message-content h3,
.message.assistant .message-content h4 {
    font-size: 0.9375rem;
    font-weight: 700;
    color: #0f172a;
    margin: 12px 0 4px;
}

.message.assistant .message-content h3:first-child,
.message.assistant .message-content h4:first-child {
    margin-top: 0;
}

.message.assistant .message-content strong {
    font-weight: 700;
    color: #0f172a;
}

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

.message.assistant .message-content code {
    background: #f1f3f5;
    padding: 1px 5px;
    border-radius: 4px;
    font-size: 0.8125rem;
    font-family: 'Cascadia Code', 'Consolas', monospace;
    color: #c2255c;
    border: 1px solid #e9ecef;
}

.message.assistant .message-content ol,
.message.assistant .message-content ul {
    margin: 8px 0;
    padding-left: 22px;
}

.message.assistant .message-content li {
    margin: 5px 0;
}

.message.assistant .message-content li::marker {
    color: #4f46e5;
    font-weight: 600;
}

.message.assistant .message-content p {
    margin: 6px 0;
}

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

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

/* Inline citation badges */
.citation-ref {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 0.5625rem;
    font-weight: 700;
    background: #4f46e5;
    color: #fff;
    min-width: 15px;
    height: 15px;
    border-radius: 7px;
    padding: 0 3px;
    margin: 0 1px;
    vertical-align: super;
    line-height: 1;
    cursor: default;
}

/* Sources */
.sources-container {
    margin-top: 14px;
    padding-top: 12px;
    border-top: 1px solid #e5e7eb;
}

.sources-label {
    font-size: 0.6875rem;
    color: #94a3b8;
    margin-bottom: 8px;
    text-transform: uppercase;
    letter-spacing: 0.6px;
    font-weight: 600;
}

.sources-list {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.source-chip {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 4px 10px 4px 4px;
    background: #f0eeff;
    border: 1px solid #e0ddf7;
    border-radius: 20px;
    font-size: 0.6875rem;
    color: #4338ca;
    cursor: default;
    transition: all 0.15s ease;
    font-weight: 500;
}

.source-chip:hover {
    background: #e4e1fb;
    border-color: #c7c2f0;
}

.source-chip .citation-num {
    font-weight: 700;
    font-size: 0.5625rem;
    background: #4f46e5;
    color: #fff;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.source-chip .source-icon {
    font-size: 0.625rem;
    opacity: 0.5;
}

.source-chip .source-icon-svg {
    color: #dc2626;
    opacity: 0.7;
    flex-shrink: 0;
}

/* Tool activity / thinking section (collapsible) */
.tool-activity {
    margin-bottom: 10px;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    font-size: 0.8125rem;
}

.tool-activity > .tool-summary {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    cursor: pointer;
    color: #64748b;
    font-size: 0.8125rem;
    user-select: none;
    list-style: none;
}

.tool-activity > .tool-summary::-webkit-details-marker {
    display: none;
}

.tool-activity > .tool-summary::before {
    content: '\25B6';
    font-size: 0.5rem;
    transition: transform 0.2s;
    flex-shrink: 0;
}

.tool-activity[open] > .tool-summary::before {
    transform: rotate(90deg);
}

.tool-steps {
    padding: 0 12px 8px;
}

.tool-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 4px 0;
    font-size: 0.8125rem;
    color: #64748b;
}

.tool-item.done {
    color: #94a3b8;
}

.tool-spinner {
    display: inline-flex;
    gap: 3px;
    align-items: center;
    flex-shrink: 0;
}

.tool-spinner::before,
.tool-spinner::after,
.tool-spinner span {
    content: '';
    display: block;
    width: 4px;
    height: 4px;
    border-radius: 50%;
    background: #4f46e5;
    animation: bounce 1.2s ease-in-out infinite;
}

.tool-spinner::after {
    animation-delay: 0.2s;
}

.tool-spinner span {
    animation-delay: 0.4s;
}

@keyframes bounce {
    0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); }
    40% { opacity: 1; transform: scale(1); }
}

.tool-check {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 14px;
    height: 14px;
    font-size: 0.625rem;
    color: #22c55e;
    flex-shrink: 0;
    font-weight: 700;
}

.tool-message {
    flex: 1;
}

.tool-result {
    font-size: 0.6875rem;
    color: #94a3b8;
    font-style: italic;
}

/* Input area */
.input-area {
    padding: 14px 24px;
    background: #fff;
    border-top: 1px solid #e8eaed;
    flex-shrink: 0;
}

#chat-form {
    display: flex;
    gap: 8px;
}

#chat-input {
    flex: 1;
    padding: 10px 16px;
    border: 1.5px solid #d1d5db;
    border-radius: 22px;
    font-size: 0.875rem;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
    font-family: inherit;
}

#chat-input:focus {
    border-color: #4f46e5;
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

#chat-input:disabled {
    background: #f9fafb;
    cursor: not-allowed;
    color: #9ca3af;
}

#send-btn {
    padding: 10px 20px;
    background: #4f46e5;
    color: #fff;
    border: none;
    border-radius: 22px;
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s;
    font-family: inherit;
}

#send-btn:hover:not(:disabled) {
    background: #4338ca;
}

#send-btn:disabled {
    background: #c7d2fe;
    cursor: not-allowed;
}


/* Scrollbar */
.chat-container::-webkit-scrollbar {
    width: 5px;
}

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

.chat-container::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 3px;
}

.chat-container::-webkit-scrollbar-thumb:hover {
    background: #94a3b8;
}
