:root {
    --bg: #f7f9fc;
    --surface: #ffffff;
    --surface-alt: #f1f5f9;
    --border: #d9e2ec;
    --text: #163172;
    --muted: #5b6b88;
    --primary: #173b8f;
    --primary-hover: #122f73;
    --secondary: #ffffff;
    --secondary-hover: #f5f7fb;
    --user-bubble: #173b8f;
    --assistant-bubble: #eef4ff;
    --assistant-text: #183153;
    --shadow: 0 10px 30px rgba(23, 59, 143, 0.08);
    --radius: 18px;
    --max-width: 1100px;
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    height: 100%;
    font-family: Inter, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
}

body {
    min-height: 100vh;
}

body.modal-open {
    overflow: hidden;
}

button,
input {
    font: inherit;
}

.app-shell {
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.topbar {
    height: 84px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 18px 28px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.brand {
    display: flex;
    align-items: center;
}

.brand-name {
    margin-left: 12px;
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--primary);
}

.brand-logo {
    height: 80px;
    width: auto;
    object-fit: contain;
}

.chat-shell {
    width: min(100%, var(--max-width));
    margin: 0 auto;
    padding: 24px;
    flex: 1;
    display: grid;
    grid-template-rows: 1fr auto;
    gap: 18px;
    min-height: 0;
    overflow: hidden;
}

.messages {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 28px;
    box-shadow: var(--shadow);
    padding: 24px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 14px;
    min-height: 0;
}

.empty-state {
    margin: auto;
    text-align: center;
    color: var(--muted);
}

.empty-state h1 {
    margin: 12px 0 8px;
    color: var(--text);
    font-size: 1.5rem;
}

.empty-state p {
    margin: 0;
}

.empty-icon {
    font-size: 40px;
    color: var(--primary);
}

.message-row {
    width: 100%;
    display: flex;
}

.message-row.user {
    justify-content: flex-end;
}

.message-row.assistant,
.message-row.system,
.message-row.error {
    justify-content: flex-start;
}

.message-bubble {
    display: inline-block;
    max-width: min(760px, 80%);
    padding: 14px 18px;
    border-radius: 18px;
    line-height: 1.5;
    font-size: 15px;
    word-break: break-word;
    box-shadow: 0 4px 14px rgba(15, 23, 42, 0.06);
}

.message-bubble-plain {
    white-space: pre-wrap;
}

.message-bubble-markdown> :first-child {
    margin-top: 0;
}

.message-bubble-markdown> :last-child {
    margin-bottom: 0;
}

.message-bubble-markdown p,
.message-bubble-markdown ul,
.message-bubble-markdown ol,
.message-bubble-markdown pre,
.message-bubble-markdown blockquote,
.message-bubble-markdown table,
.message-bubble-markdown hr {
    margin: 0 0 0.9em;
}

.message-bubble-markdown ul,
.message-bubble-markdown ol {
    padding-left: 1.3em;
}

.message-bubble-markdown li+li {
    margin-top: 0.3em;
}

.message-bubble-markdown a {
    color: var(--primary);
    font-weight: 600;
    text-decoration-thickness: 1.5px;
    text-underline-offset: 2px;
}

.message-bubble-markdown strong {
    font-weight: 700;
}

.message-bubble-markdown code {
    font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
    font-size: 0.92em;
    padding: 0.1em 0.35em;
    border-radius: 8px;
    background: rgba(23, 59, 143, 0.12);
}

.message-bubble-markdown pre {
    overflow-x: auto;
    padding: 14px 16px;
    border-radius: 14px;
    background: rgba(9, 19, 43, 0.08);
}

.message-bubble-markdown pre code {
    display: block;
    padding: 0;
    background: transparent;
}

.message-bubble-markdown blockquote {
    padding-left: 14px;
    border-left: 3px solid rgba(23, 59, 143, 0.3);
    color: var(--muted);
}

.message-bubble-markdown table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.95em;
}

.message-bubble-markdown th,
.message-bubble-markdown td {
    padding: 8px 10px;
    border: 1px solid rgba(23, 59, 143, 0.14);
    text-align: left;
    vertical-align: top;
}

.message-bubble-markdown th {
    background: rgba(23, 59, 143, 0.08);
}

.message-bubble-markdown hr {
    border: none;
    border-top: 1px solid rgba(23, 59, 143, 0.16);
}

.message-bubble sup {
    line-height: 0;
}

.reference-citation {
    border: none;
    background: transparent;
    color: var(--primary);
    font-size: 0.8em;
    font-weight: 700;
    padding: 0 0 0 2px;
    cursor: pointer;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.reference-citation:hover {
    color: var(--primary-hover);
}

.reference-citation:focus-visible,
.reference-modal-close:focus-visible,
.reference-modal-url:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

.message-row.user .message-bubble {
    background: var(--user-bubble);
    color: #fff;
    border-bottom-right-radius: 6px;
}

.message-row.assistant .message-bubble,
.message-row.system .message-bubble {
    background: var(--assistant-bubble);
    color: var(--assistant-text);
    border: 1px solid #dbe7ff;
    border-bottom-left-radius: 6px;
}

.message-row.user .message-bubble-markdown a,
.message-row.user .message-bubble-markdown blockquote {
    color: inherit;
}

.message-row.user .message-bubble-markdown code {
    background: rgba(255, 255, 255, 0.18);
}

.message-row.user .message-bubble-markdown pre {
    background: rgba(9, 19, 43, 0.18);
}

.reference-modal {
    position: fixed;
    inset: 0;
    z-index: 50;
    display: grid;
    place-items: center;
}

.reference-modal[hidden] {
    display: none;
}

.reference-modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(9, 19, 43, 0.52);
    backdrop-filter: blur(4px);
}

.reference-modal-dialog {
    position: relative;
    width: min(92vw, 680px);
    max-height: 85vh;
    overflow-y: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 24px;
    box-shadow: 0 24px 60px rgba(15, 23, 42, 0.2);
    padding: 22px;
}

.reference-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 16px;
}

.reference-modal-header h2 {
    margin: 0;
    font-size: 1.1rem;
    color: var(--text);
}

.reference-modal-close {
    border: none;
    background: transparent;
    color: var(--muted);
    cursor: pointer;
    font-size: 20px;
    padding: 4px;
}

.reference-modal-body {
    display: grid;
    gap: 10px;
}

.reference-modal-label {
    margin: 0;
    font-size: 0.85rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--muted);
}

.reference-modal-url {
    display: block;
    color: var(--primary);
    text-decoration: none;
    line-height: 1.6;
    word-break: break-word;
    padding: 14px 16px;
    border-radius: 14px;
    background: var(--surface-alt);
    border: 1px solid var(--border);
}

.reference-modal-url:hover {
    text-decoration: underline;
}

.reference-modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 18px;
}

.chat-composer {
    display: flex;
    gap: 12px;
    align-items: center;
}

.composer-input-wrap {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 0 18px;
    box-shadow: var(--shadow);
}

.composer-icon {
    color: var(--muted);
}

#messageInput {
    flex: 1;
    border: none;
    outline: none;
    background: transparent;
    color: var(--text);
    padding: 16px 0;
}

#messageInput::placeholder {
    color: #7b8ba7;
}

.btn {
    border: none;
    border-radius: 999px;
    padding: 14px 18px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    transition: background 0.2s ease, transform 0.2s ease;
}

.btn:hover {
    transform: translateY(-1px);
}

.btn-primary {
    background: var(--primary);
    color: white;
}

.btn-primary:hover {
    background: var(--primary-hover);
}

.btn-secondary {
    background: var(--secondary);
    color: var(--primary);
    border: 1px solid var(--border);
}

.btn-secondary:hover {
    background: var(--secondary-hover);
}

.btn:disabled,
#messageInput:disabled {
    opacity: 0.65;
    cursor: not-allowed;
}

.typing {
    opacity: 0.8;
    font-style: italic;
}

/* Feedback controls */
.feedback-bar {
    display: flex;
    align-items: center;
    gap: 4px;
    margin-top: 8px;
}

.feedback-btn {
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--muted);
    border-radius: 8px;
    padding: 4px 8px;
    cursor: pointer;
    font-size: 16px;
    line-height: 1;
    transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.feedback-btn:hover:not(.feedback-btn--selected):not(:disabled) {
    background: var(--surface-alt);
    color: var(--text);
    border-color: var(--primary);
}

.feedback-btn--selected {
    cursor: default;
}

.feedback-btn--up.feedback-btn--selected {
    background: #e8f5e9;
    color: #2e7d32;
    border-color: #81c784;
}

.feedback-btn--down.feedback-btn--selected {
    background: #fce4ec;
    color: #c62828;
    border-color: #e57373;
}

.feedback-btn:disabled:not(.feedback-btn--selected) {
    opacity: 0.4;
    cursor: default;
}

.feedback-comment-wrap {
    overflow: hidden;
    max-height: 0;
    opacity: 0;
    transition: max-height 0.25s ease, opacity 0.25s ease, margin-top 0.25s ease;
    margin-top: 0;
}

.feedback-comment-wrap--open {
    max-height: 220px;
    opacity: 1;
    margin-top: 8px;
}

.feedback-comment-inner {
    display: flex;
    gap: 8px;
    align-items: flex-end;
}

.feedback-comment-input {
    flex: 1;
    width: 100%;
    min-width: 0;
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 10px 12px;
    font: inherit;
    font-size: 13px;
    resize: none;
    background: var(--surface);
    color: var(--text);
    min-height: 64px;
    max-height: 120px;
}

.feedback-comment-input::placeholder {
    color: var(--muted);
}

.feedback-comment-submit {
    border: none;
    background: var(--primary);
    color: white;
    border-radius: 8px;
    padding: 8px 14px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.15s;
}

.feedback-comment-submit:hover {
    background: var(--primary-hover);
}

.feedback-thanks {
    font-size: 12px;
    color: var(--muted);
    margin-left: 4px;
}

@media (min-width: 769px) {
    .feedback-comment-input {
        min-width: 300px;
    }
}

@media (max-width: 768px) {
    .topbar {
        height: 72px;
        padding: 14px 16px;
    }

    .brand-logo {
        height: 34px;
    }

    .chat-shell {
        padding: 16px;
    }

    .messages {
        padding: 16px;
        border-radius: 20px;
    }

    .message-bubble {
        max-width: 90%;
    }

    .reference-modal-dialog {
        width: min(94vw, 560px);
        padding: 18px;
    }

    .reference-modal-actions {
        flex-direction: column-reverse;
    }

    .reference-modal-actions .btn {
        width: 100%;
        justify-content: center;
    }

    .btn span {
        display: none;
    }
}