/* === Reset & Base === */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary: #1a4b8c;
    --primary-dark: #0f3466;
    --primary-light: #2563a8;
    --accent: #e8f0fe;
    --bg: #f5f7fa;
    --white: #ffffff;
    --text: #1a1a2e;
    --text-light: #5a6474;
    --border: #d1d9e6;
    --shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
    --radius: 12px;
    --user-bg: #1a4b8c;
    --user-text: #ffffff;
    --assistant-bg: #ffffff;
    --assistant-text: #1a1a2e;
    --max-width: 860px;
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
}

/* === App Layout === */
.app-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh; /* Dynamic viewport height — accounts for mobile browser chrome */
    max-width: var(--max-width);
    margin: 0 auto;
}

/* === Header === */
.app-header {
    background: var(--primary);
    color: var(--white);
    padding: 20px 24px;
    text-align: center;
    flex-shrink: 0;
}

.app-header h1 {
    font-size: 1.4rem;
    font-weight: 700;
    letter-spacing: -0.01em;
}

.app-header .subtitle {
    font-size: 0.8rem;
    opacity: 0.85;
    margin-top: 4px;
    font-weight: 400;
}

/* === Chat Area === */
.chat-container {
    flex: 1;
    overflow-y: auto;
    padding: 16px 16px 8px;
}

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

/* === Messages === */
.message {
    display: flex;
    max-width: 88%;
}

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

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

.message-content {
    padding: 12px 16px;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    word-wrap: break-word;
    overflow-wrap: break-word;
}

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

.assistant-message .message-content {
    background: var(--assistant-bg);
    color: var(--assistant-text);
    border-bottom-left-radius: 4px;
    border: 1px solid var(--border);
}

/* === Message Typography === */
.message-content p {
    margin-bottom: 8px;
}

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

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

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

.message-content code {
    background: rgba(0, 0, 0, 0.06);
    padding: 1px 5px;
    border-radius: 4px;
    font-size: 0.9em;
}

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

/* === Sources === */
.sources-toggle {
    display: inline-block;
    margin-top: 10px;
    padding: 4px 10px;
    font-size: 0.78rem;
    color: var(--primary);
    background: var(--accent);
    border: 1px solid var(--border);
    border-radius: 6px;
    cursor: pointer;
    transition: background 0.15s;
}

.sources-toggle:hover {
    background: #d4e4fc;
}

.sources-list {
    display: none;
    margin-top: 8px;
    padding: 10px 12px;
    background: var(--accent);
    border-radius: 8px;
    font-size: 0.8rem;
    color: var(--text-light);
}

.sources-list.open {
    display: block;
}

.sources-list ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

.sources-list li {
    padding: 3px 0;
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}

.sources-list li:last-child {
    border-bottom: none;
}

/* === Loading Indicator === */
.loading-indicator {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 16px;
    background: var(--white);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    border-bottom-left-radius: 4px;
    box-shadow: var(--shadow);
    color: var(--text-light);
    font-size: 0.9rem;
}

.loading-dots {
    display: flex;
    gap: 4px;
}

.loading-dots span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--primary);
    animation: dot-pulse 1.4s ease-in-out infinite;
}

.loading-dots span:nth-child(2) {
    animation-delay: 0.2s;
}

.loading-dots span:nth-child(3) {
    animation-delay: 0.4s;
}

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

/* === Input Area === */
.input-area {
    padding: 12px 16px calc(16px + env(safe-area-inset-bottom, 0px));
    background: var(--white);
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}

.input-form {
    display: flex;
    gap: 8px;
}

.input-form input {
    flex: 1;
    padding: 12px 16px;
    font-size: 0.95rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    outline: none;
    transition: border-color 0.15s;
    background: var(--bg);
    color: var(--text);
}

.input-form input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(26, 75, 140, 0.1);
}

.input-form input::placeholder {
    color: #9aa5b4;
}

.input-form button {
    padding: 0 16px;
    background: var(--primary);
    color: var(--white);
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
}

.input-form button:hover {
    background: var(--primary-dark);
}

.input-form button:disabled {
    background: #8fa3bf;
    cursor: not-allowed;
}

.disclaimer {
    text-align: center;
    font-size: 0.72rem;
    color: var(--text-light);
    margin-top: 8px;
}

/* === Markdown Content === */
.message-content h1, .message-content h2, .message-content h3 {
    margin: 12px 0 6px;
    font-weight: 700;
    line-height: 1.3;
}

.message-content h1 { font-size: 1.15rem; }
.message-content h2 { font-size: 1.05rem; }
.message-content h3 { font-size: 0.95rem; }

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

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

.message-content table {
    width: 100%;
    border-collapse: collapse;
    margin: 10px 0;
    font-size: 0.85rem;
}

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

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

.message-content pre {
    background: #f0f2f5;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px 12px;
    overflow-x: auto;
    margin: 8px 0;
    font-size: 0.85rem;
}

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

.message-content blockquote {
    border-left: 3px solid var(--primary);
    padding: 4px 12px;
    margin: 8px 0;
    background: var(--accent);
    border-radius: 0 6px 6px 0;
}

.message-content a {
    color: var(--primary);
    text-decoration: underline;
}

/* === Reset Banner === */
.reset-banner {
    margin: 16px 0 8px;
    padding: 14px 18px;
    background: linear-gradient(135deg, var(--accent), #fff3e0);
    border: 1px solid #e0c88a;
    border-radius: var(--radius);
    text-align: center;
}

.reset-banner-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
}

.reset-banner-content span {
    font-size: 0.88rem;
    color: var(--text);
    line-height: 1.4;
}

.reset-button {
    padding: 8px 20px;
    background: var(--primary);
    color: var(--white);
    border: none;
    border-radius: 8px;
    font-size: 0.88rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s;
}

.reset-button:hover {
    background: var(--primary-dark);
}

/* === Error Message === */
.error-content {
    background: #fef2f2 !important;
    border-color: #fca5a5 !important;
    color: #991b1b !important;
}

/* === Mobile === */
@media (max-width: 640px) {
    .app-header {
        padding: 14px 16px;
    }

    .app-header h1 {
        font-size: 1.15rem;
    }

    .app-header .subtitle {
        font-size: 0.72rem;
    }

    .message {
        max-width: 94%;
    }

    .chat-container {
        padding: 12px 10px 6px;
    }

    .input-area {
        padding: 10px 10px calc(14px + env(safe-area-inset-bottom, 0px));
    }

    .input-form input {
        font-size: 1rem;
    }
}
