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

:root {
    --bg: #f5f2ee;
    --surface: #ffffff;
    --border: #e2ddd8;
    --border-2: #cec9c2;
    --text: #1a1814;
    --text-2: #6b6560;
    --text-3: #9e9893;
    --accent: #c4571a;
    --accent-2: #e8ddd4;
    --user-bg: #1a1814;
    --user-text: #f5f2ee;
    --bot-bg: #ffffff;
    --bot-border: #e2ddd8;
    --error-bg: #fdf0ec;
    --error-txt: #9b2a10;
    --sidebar-w: 220px;
    --radius: 10px;
    --font-sans: 'DM Sans', sans-serif;
    --font-serif: 'Instrument Serif', Georgia, serif;
    --font-mono: 'DM Mono', monospace;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.06), 0 4px 16px rgba(0, 0, 0, 0.05);
    --shadow-lg: 0 4px 24px rgba(0, 0, 0, 0.1), 0 1px 4px rgba(0, 0, 0, 0.06);
}

html, body {
    height: 100%;
    font-family: var(--font-sans);
    background: var(--bg);
    color: var(--text);
    font-size: 15px;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
}

/* ── Utilities ──────────────────────────────────────────────────── */
.hidden {
    display: none !important;
}

/* ── Brand ──────────────────────────────────────────────────────── */
.brand-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    background: var(--accent);
    border-radius: 50%;
    margin-right: 7px;
    flex-shrink: 0;
}

.brand-name {
    font-family: var(--font-serif);
    font-size: 1.1rem;
    letter-spacing: -0.01em;
    color: var(--text);
}

/* ── AUTH OVERLAY ───────────────────────────────────────────────── */
.auth-overlay {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg);
    z-index: 100;
    padding: 20px;
}

.auth-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    width: 100%;
    max-width: 400px;
    padding: 36px 40px 40px;
}

.auth-brand {
    display: flex;
    align-items: center;
    margin-bottom: 28px;
}

.auth-panel {
    display: none;
}

.auth-panel.active {
    display: block;
}

.auth-panel h2 {
    font-family: var(--font-serif);
    font-size: 1.65rem;
    font-weight: 400;
    letter-spacing: -0.02em;
    margin-bottom: 6px;
    line-height: 1.2;
}

.auth-sub {
    color: var(--text-2);
    font-size: 0.9rem;
    margin-bottom: 24px;
}

/* Fields */
.field {
    margin-bottom: 16px;
}

.field label {
    display: block;
    font-size: 0.8rem;
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text-2);
    margin-bottom: 6px;
}

.field label .hint {
    font-weight: 400;
    text-transform: none;
    letter-spacing: 0;
    color: var(--text-3);
    margin-left: 6px;
    font-size: 0.78rem;
}

.field input {
    width: 100%;
    padding: 10px 13px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 7px;
    font-family: var(--font-sans);
    font-size: 0.95rem;
    color: var(--text);
    outline: none;
    transition: border-color 0.15s, box-shadow 0.15s;
}

.field input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(196, 87, 26, 0.1);
}

.field input::placeholder {
    color: var(--text-3);
}

.field-error {
    font-size: 0.85rem;
    color: var(--error-txt);
    background: var(--error-bg);
    border: 1px solid #f0c4b4;
    border-radius: 6px;
    padding: 8px 12px;
    margin-top: 4px;
    margin-bottom: 4px;
}

/* Buttons */
.btn-primary {
    width: 100%;
    padding: 11px 20px;
    background: var(--text);
    color: #f5f2ee;
    border: none;
    border-radius: 7px;
    font-family: var(--font-sans);
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    margin-top: 8px;
    transition: background 0.15s, transform 0.1s;
    letter-spacing: -0.01em;
}

.btn-primary:hover {
    background: #2e2c28;
}

.btn-primary:active {
    transform: scale(0.99);
}

.btn-primary:disabled {
    opacity: 0.5;
    cursor: default;
}

.auth-switch {
    text-align: center;
    font-size: 0.87rem;
    color: var(--text-2);
    margin-top: 18px;
}

.auth-switch a {
    color: var(--accent);
    text-decoration: none;
    font-weight: 600;
}

.auth-switch a:hover {
    text-decoration: underline;
}

/* ── APP LAYOUT ─────────────────────────────────────────────────── */
.app {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* ── SIDEBAR ────────────────────────────────────────────────────── */
.sidebar {
    width: var(--sidebar-w);
    flex-shrink: 0;
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    padding: 20px 0;
}

.sidebar-top {
    padding: 0 18px 18px;
    border-bottom: 1px solid var(--border);
    margin-bottom: 16px;
}

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

.sidebar-label {
    font-size: 0.72rem;
    font-weight: 600;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--text-3);
    margin-top: 3px;
    padding-left: 15px;
}

.sidebar-section {
    padding: 0 12px;
    flex: 1;
}

.section-heading {
    font-size: 0.7rem;
    font-weight: 600;
    letter-spacing: 0.07em;
    text-transform: uppercase;
    color: var(--text-3);
    padding: 0 6px;
    margin-bottom: 6px;
}

.sidebar-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    padding: 8px 10px;
    background: none;
    border: none;
    border-radius: 6px;
    font-family: var(--font-sans);
    font-size: 0.85rem;
    color: var(--text-2);
    cursor: pointer;
    text-align: left;
    transition: background 0.12s, color 0.12s;
}

.sidebar-btn:hover {
    background: var(--bg);
    color: var(--text);
}

.sidebar-btn svg {
    flex-shrink: 0;
    opacity: 0.6;
}

.sidebar-bottom {
    padding: 16px 12px 0;
    border-top: 1px solid var(--border);
    margin-top: auto;
}

.user-chip {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 8px;
    margin-bottom: 6px;
}

.user-avatar {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: var(--accent);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.78rem;
    font-weight: 700;
    color: white;
    flex-shrink: 0;
    text-transform: uppercase;
}

.user-info {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.user-name {
    font-size: 0.85rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.user-email {
    font-size: 0.72rem;
    color: var(--text-3);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.logout-btn {
    color: var(--text-3);
}

.logout-btn:hover {
    color: var(--error-txt);
    background: var(--error-bg);
}

/* ── CHAT AREA ──────────────────────────────────────────────────── */
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    overflow: hidden;
}

.chat-header {
    padding: 22px 28px 18px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
    flex-shrink: 0;
}

.chat-title {
    font-family: var(--font-serif);
    font-size: 1.4rem;
    font-weight: 400;
    letter-spacing: -0.02em;
    line-height: 1.2;
}

.chat-subtitle {
    font-size: 0.84rem;
    color: var(--text-2);
    margin-top: 3px;
}

/* Messages */
.messages {
    flex: 1;
    overflow-y: auto;
    padding: 24px 28px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    scroll-behavior: smooth;
}

.messages::-webkit-scrollbar {
    width: 5px;
}

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

.messages::-webkit-scrollbar-thumb {
    background: var(--border-2);
    border-radius: 10px;
}

.empty-state {
    margin: auto;
    text-align: center;
    color: var(--text-3);
    max-width: 280px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 14px;
}

.empty-icon {
    width: 52px;
    height: 52px;
    border: 1.5px solid var(--border);
    border-radius: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-3);
}

.empty-state p {
    font-size: 0.88rem;
    line-height: 1.55;
}

/* Message bubbles */
.message {
    display: flex;
    flex-direction: column;
    max-width: 75%;
    animation: msgIn 0.2s ease both;
}

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

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

.message.bot, .message.error {
    align-self: flex-start;
}

.message-label {
    font-size: 0.7rem;
    font-weight: 600;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--text-3);
    margin-bottom: 4px;
}

.message.user .message-label {
    text-align: right;
}

.message-bubble {
    padding: 12px 16px;
    border-radius: var(--radius);
    font-size: 0.93rem;
    line-height: 1.65;
    white-space: pre-wrap;
    word-break: break-word;
}

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

.message.bot .message-bubble {
    background: var(--bot-bg);
    border: 1px solid var(--bot-border);
    color: var(--text);
    border-bottom-left-radius: 3px;
    box-shadow: 0 6px 20px rgba(10, 10, 10, 0.04);
}

.message.error .message-bubble {
    background: var(--error-bg);
    color: var(--error-txt);
    border: 1px solid #f0c4b4;
}

/* Thinking indicator */
.thinking-dots {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 0;
}

.thinking-dots span {
    display: inline-block;
    width: 6px;
    height: 6px;
    background: var(--text-3);
    border-radius: 50%;
    animation: dot 1.2s infinite ease-in-out;
}

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

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

@keyframes dot {
    0%, 80%, 100% {
        transform: scale(0.7);
        opacity: 0.4;
    }
    40% {
        transform: scale(1);
        opacity: 1;
    }
}

/* Timestamp */
.message-time {
    font-size: 0.68rem;
    color: var(--text-3);
    margin-top: 4px;
    font-family: var(--font-mono);
}

.message.user .message-time {
    text-align: right;
}

/* ── COMPOSER ───────────────────────────────────────────────────── */
.composer {
    display: flex;
    align-items: flex-end;
    gap: 10px;
    padding: 16px 28px;
    border-top: 1px solid var(--border);
    background: var(--surface);
    flex-shrink: 0;
}

#question {
    flex: 1;
    resize: none;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg);
    padding: 11px 14px;
    font-family: var(--font-sans);
    font-size: 0.93rem;
    color: var(--text);
    outline: none;
    line-height: 1.5;
    max-height: 150px;
    overflow-y: auto;
    transition: border-color 0.15s, box-shadow 0.15s;
}

#question:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(196, 87, 26, 0.08);
}

#question::placeholder {
    color: var(--text-3);
}

#send {
    width: 40px;
    height: 40px;
    flex-shrink: 0;
    background: var(--text);
    color: #f5f2ee;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, transform 0.1s;
}

#send:hover {
    background: #2e2c28;
}

#send:active {
    transform: scale(0.95);
}

#send:disabled {
    opacity: 0.35;
    cursor: default;
}

.composer-note {
    text-align: center;
    font-size: 0.76rem;
    color: var(--text-3);
    padding: 0 28px 12px;
    background: var(--surface);
}

/* ── History dates ──────────────────────────────────────────────── */
.date-divider {
    display: flex;
    align-items: center;
    gap: 12px;
    color: var(--text-3);
    font-size: 0.72rem;
    font-family: var(--font-mono);
    letter-spacing: 0.04em;
    text-transform: uppercase;
    align-self: stretch;
}

.date-divider::before, .date-divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--border);
}

/* ── References / Sources ───────────────────────────────────────── */
.references {
    margin-top: 8px;
    align-self: flex-start;
    max-width: 75%;
}

.references-toggle {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    font-size: 0.74rem;
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text-3);
    background: none;
    border: 1px solid var(--border);
    border-radius: 5px;
    padding: 3px 9px 3px 8px;
    cursor: pointer;
    font-family: var(--font-sans);
    transition: color 0.12s, border-color 0.12s, background 0.12s;
    user-select: none;
}

.references-toggle:hover {
    color: var(--text-2);
    border-color: var(--border-2);
    background: var(--bg);
}

.references-toggle svg {
    transition: transform 0.2s;
}

.references-toggle.open svg {
    transform: rotate(180deg);
}

.references-toggle .ref-count {
    background: var(--accent-2);
    color: var(--accent);
    border-radius: 3px;
    padding: 0px 5px;
    font-size: 0.7rem;
    font-weight: 700;
    font-family: var(--font-mono);
    margin-left: 2px;
}

.references-list {
    display: none;
    margin-top: 6px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    overflow: hidden;
    max-height: 0;
    transition: max-height 0.25s ease;
}

.references-list.open {
    max-height: 600px;
}

.reference-card {
    background: var(--bg);
    border: 1px solid var(--border);
    border-left: 3px solid var(--accent);
    border-radius: 6px;
    padding: 10px 13px;
    font-size: 0.82rem;
    line-height: 1.5;
}

.ref-header {
    display: flex;
    align-items: baseline;
    gap: 8px;
    flex-wrap: wrap;
    margin-bottom: 4px;
}

.ref-section-id {
    font-family: var(--font-mono);
    font-size: 0.72rem;
    font-weight: 500;
    color: var(--accent);
    background: var(--accent-2);
    padding: 1px 6px;
    border-radius: 3px;
    white-space: nowrap;
}

.ref-section-title {
    font-weight: 600;
    color: var(--text);
    font-size: 0.84rem;
}

.ref-score {
    margin-left: auto;
    font-family: var(--font-mono);
    font-size: 0.68rem;
    color: var(--text-3);
    white-space: nowrap;
}

.ref-snippet {
    color: var(--text-2);
    font-size: 0.8rem;
    line-height: 1.55;
    margin-bottom: 5px;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.ref-link {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 0.76rem;
    color: var(--accent);
    text-decoration: none;
    font-weight: 500;
}

.ref-link:hover {
    text-decoration: underline;
}

.ref-link svg {
    opacity: 0.7;
}

/* ── Handbook switcher (sidebar) ────────────────────────────────── */
.handbook-switcher {
    display: flex;
    flex-direction: column;
    gap: 3px;
}

.hb-btn {
    display: flex;
    align-items: center;
        gap: 8px;
    width: 100%;
    padding: 8px 10px;
    background: none;
    border: none;
    border-radius: 6px;
    font-family: var(--font-sans);
    font-size: 0.875rem;
    color: var(--text-2);
    cursor: pointer;
    text-align: left;
    transition: background 0.12s, color 0.12s;
    font-weight: 500;
}

/* ── Feedback UI ───────────────────────────────────────────────── */
.feedback-container {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-top: 8px;
    align-self: flex-start;
    width: 100%;
    max-width: 400px;
}

.feedback-buttons {
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.feedback-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border-radius: 6px;
    border: none;
    background: transparent;
    cursor: pointer;
    color: var(--text-3);
    transition: background 0.15s, color 0.15s;
}

.feedback-btn:hover {
    background: var(--border);
    color: var(--text);
}

.feedback-btn.active {
    color: var(--accent);
    background: var(--accent-2);
}

.feedback-status {
    font-size: 0.75rem;
    color: var(--text-3);
    margin-left: 4px;
    animation: fadeIn 0.3s ease;
}

.feedback-comment-container {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 4px;
    animation: fadeIn 0.3s ease;
}

.feedback-comment-textarea {
    width: 100%;
    min-height: 60px;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    resize: vertical;
    background: var(--surface);
    color: var(--text);
    font-family: var(--font-sans);
    font-size: 0.85rem;
    box-shadow: 0 1px 2px rgba(0,0,0,0.02) inset;
    outline: none;
    transition: border-color 0.15s;
}

.feedback-comment-textarea:focus {
    border-color: var(--accent);
}

.feedback-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

.feedback-submit-btn, .feedback-cancel-btn {
    padding: 5px 12px;
    border-radius: 6px;
    border: none;
    cursor: pointer;
    font-weight: 600;
    font-size: 0.8rem;
    font-family: var(--font-sans);
    transition: opacity 0.15s, background 0.15s;
}

.feedback-submit-btn {
    background: var(--accent);
    color: #fff;
}

.feedback-submit-btn:hover { background: #a8440f; }
.feedback-submit-btn:disabled { opacity: 0.6; cursor: wait; }

.feedback-cancel-btn {
    background: transparent;
    color: var(--text-2);
}
.feedback-cancel-btn:hover {
    background: var(--border);
}

.thumbs-up-btn:hover {
    color: #15803d !important;      
    background: #f0fdf4 !important; 
}

.thumbs-up-btn.active {
    color: #16a34a !important;      
    background: #dcfce7 !important;
    border: 1px solid #bbf7d0;
}

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

.hb-btn:hover {
    background: var(--bg);
    color: var(--text);
}

.hb-btn.active {
    background: var(--bg);
    color: var(--text);
    font-weight: 600;
}

.hb-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}

.hb-dot.cochrane {
    background: #c4571a;
}

.hb-dot.cee {
    background: #2563a8;
}

/* ── Handbook badge (chat header) ───────────────────────────────── */
.handbook-badge {
    display: inline-block;
    padding: 1px 8px;
    border-radius: 4px;
    font-size: 0.8rem;
    font-weight: 700;
    font-family: var(--font-mono);
    letter-spacing: 0.02em;
    vertical-align: middle;
}

.handbook-badge.cochrane {
    background: rgba(196, 87, 26, 0.12);
    color: #c4571a;
}

.handbook-badge.cee {
    background: rgba(37, 99, 168, 0.1);
    color: #2563a8;
}

/* ── Per-message handbook tag ───────────────────────────────────── */
.msg-handbook-tag {
    display: inline-block;
    margin-left: 7px;
    padding: 0px 6px;
    border-radius: 3px;
    font-size: 0.66rem;
    font-weight: 700;
    font-family: var(--font-mono);
    letter-spacing: 0.04em;
    text-transform: uppercase;
    vertical-align: middle;
}

.msg-handbook-tag.cochrane {
    background: rgba(196, 87, 26, 0.15);
    color: #a8440f;
}

.msg-handbook-tag.cee {
    background: rgba(37, 99, 168, 0.12);
    color: #1a4f8a;
}