/* ── CSS Variables ─────────────────────────────────────────────────────────── */
:root {
    --bg: #ffffff;
    --surface: #f4f5f7;
    --border: #e1e4ea;
    --text: #1a1a1a;
    --text-muted: #6b7280;
    --text-heading: #111827;
    --blue: #1a6fa8;
    --blue-hover: #155e8e;
    --blue-light: #e8f1f8;
    --chip-bg: #1a6fa8;
    --chip-text: #ffffff;
    --chip-disabled-bg: #b8cfe0;
    --chip-disabled-text: #6a90a8;
    --user-bubble: #2563eb;
    --user-bubble-text: #ffffff;
    --bot-bubble: #f0f2f5;
    --bot-bubble-text: #111827;
    --citation-bg: #1a6fa8;
    --citation-text: #ffffff;
    --input-border: #d1d5db;
    --send-bg: #1a6fa8;
    --send-disabled: #d1d5db;
    --shadow-up: 0 -4px 24px rgba(0,0,0,.13);
}

[data-theme="dark"] {
    --bg: #1a1a1a;
    --surface: #242424;
    --border: #333;
    --text: #e5e7eb;
    --text-muted: #9ca3af;
    --text-heading: #f9fafb;
    --blue: #2e86c1;
    --blue-hover: #2471a3;
    --blue-light: #1a2d3d;
    --chip-bg: #2e86c1;
    --chip-text: #ffffff;
    --chip-disabled-bg: #2a3a45;
    --chip-disabled-text: #5a7a8a;
    --user-bubble: #2563eb;
    --user-bubble-text: #ffffff;
    --bot-bubble: #2a2a2a;
    --bot-bubble-text: #e5e7eb;
    --citation-bg: #1a4f6e;
    --citation-text: #e5e7eb;
    --input-border: #444;
    --send-bg: #2e86c1;
    --send-disabled: #404040;
    --shadow-up: 0 -4px 24px rgba(0,0,0,.35);
}

/* ── Reset ─────────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { width: 100%; height: 100%; overflow: hidden; }
[hidden] { display: none !important; }

body {
    font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
    line-height: 1.5;
}

.app-message { padding: 20px; color: var(--text-muted); font-size: 13px; }

/* ── App shell ─────────────────────────────────────────────────────────────── */
.app-shell { position: relative; height: 100%; }

#app-body {
    display: flex !important;
    flex-direction: column;
    height: 100%;
    position: relative;
}


/* ── Chat messages ─────────────────────────────────────────────────────────── */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 8px 14px 90px; /* bottom pad clears the floating input bar */
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
}

.chat-messages::-webkit-scrollbar { width: 4px; }
.chat-messages::-webkit-scrollbar-track { background: transparent; }
.chat-messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* ── Message rows ──────────────────────────────────────────────────────────── */
.msg {
    display: flex;
    gap: 8px;
    align-items: flex-start;
    animation: msg-in 0.16s ease;
}
@keyframes msg-in {
    from { opacity: 0; transform: translateY(5px); }
    to   { opacity: 1; transform: translateY(0); }
}

.msg--user { flex-direction: row-reverse; }

.msg__label {
    font-size: 11px;
    color: var(--text-muted);
    padding-left: 2px;
    margin-bottom: 3px;
}

/* ── Bubbles ───────────────────────────────────────────────────────────────── */
.msg__bubble {
    max-width: calc(100% - 0px);
    padding: 10px 13px;
    border-radius: 16px;
    font-size: 13.5px;
    line-height: 1.55;
    word-break: break-word;
    white-space: normal;
}
.msg--user .msg__bubble { white-space: pre-wrap; }

.msg--bot .msg__bubble {
    background: var(--bot-bubble);
    color: var(--bot-bubble-text);
    border-radius: 4px 16px 16px 16px;
}

.msg--user .msg__bubble {
    background: var(--user-bubble);
    color: var(--user-bubble-text);
    border-radius: 16px 4px 16px 16px;
    margin-left: auto;
}

/* Inline citation refs — [1] [2] placed right next to the fact */
.msg__bubble sup.citation-ref {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 15px;
    height: 15px;
    padding: 0 3px;
    margin-left: 2px;
    border-radius: 4px;
    background: var(--blue);
    color: #fff;
    font-size: 9px;
    font-weight: 700;
    vertical-align: super;
    line-height: 1;
    cursor: pointer;
    text-decoration: none;
    transition: background 0.15s;
}
.msg__bubble sup.citation-ref:hover { background: var(--blue-hover); }
[data-theme="dark"] .msg__bubble sup.citation-ref { background: #2563eb; }
[data-theme="dark"] .msg__bubble sup.citation-ref:hover { background: #1d4ed8; }

/* ── Markdown inside bot bubbles ───────────────────────────────────────────── */
.msg__bubble .md-p { margin: 0 0 6px; }
.msg__bubble .md-p:last-child { margin-bottom: 0; }
.msg__bubble .md-heading { font-size: 13px; font-weight: 700; margin: 8px 0 4px; color: var(--text-heading); }
.msg__bubble .md-list {
    margin: 4px 0 6px 0;
    padding-left: 18px;
    list-style: disc;
}
.msg__bubble .md-list li { margin-bottom: 3px; }
.msg__bubble .md-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 12px;
    margin: 6px 0;
}
.msg__bubble .md-table th,
.msg__bubble .md-table td {
    border: 1px solid var(--border);
    padding: 4px 8px;
    text-align: start;   /* follows dir="auto" so RTL (Arabic) aligns right */
}
.msg__bubble .md-table th { background: var(--surface); font-weight: 600; }
.msg__bubble br { display: block; content: ""; margin-top: 4px; }

/* ── Typing indicator ──────────────────────────────────────────────────────── */
.typing-indicator {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 2px 0;
}
.typing-indicator span {
    width: 6px; height: 6px;
    background: var(--text-muted);
    border-radius: 50%;
    animation: bounce 1.1s ease-in-out infinite;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.18s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.36s; }
@keyframes bounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
    30% { transform: translateY(-5px); opacity: 1; }
}

/* ── Apply bar (hidden — replaced by inline button) ────────────────────────── */
.apply-bar { display: none; }

/* ── Inline apply button below bot bubble ──────────────────────────────────── */
.msg-actions {
    padding: 4px 0 2px 4px;
}

.apply-inline-btn {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 5px 14px;
    border-radius: 20px;
    border: 1.5px solid var(--blue);
    background: transparent;
    color: var(--blue);
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s ease, color 0.15s ease;
}
.apply-inline-btn:hover { background: var(--blue); color: #fff; }
.apply-inline-btn:disabled { opacity: 0.5; cursor: not-allowed; border-color: var(--text-muted); color: var(--text-muted); }

/* ── Citation popup — grows out of the input box ───────────────────────────── */
.email-viewer {
    position: absolute;
    left: 12px;
    right: 12px;
    bottom: 68px;
    height: 38%;
    z-index: 20;
    background: var(--citation-bg);
    display: flex;
    flex-direction: column;
    border-radius: 14px;
    overflow: hidden;
    /* thin themed frame around the white "paper" so it reads as a deliberate
       document preview (not a stray white box) in dark mode */
    padding: 3px;
    transform-origin: bottom center;
    filter: drop-shadow(0 0 6px rgba(0,0,0,.28));
    will-change: transform, opacity;
}
[hidden].email-viewer { display: none !important; }

.email-viewer--in  { animation: popup-in  0.22s ease-out forwards; }
.email-viewer--out { animation: popup-out 0.18s ease-in  forwards; }

@keyframes popup-in {
    from { opacity: 0; transform: translateY(12px) scale(0.98); }
    to   { opacity: 1; transform: translateY(0)    scale(1);    }
}
@keyframes popup-out {
    from { opacity: 1; transform: translateY(0)    scale(1);    }
    to   { opacity: 0; transform: translateY(12px) scale(0.98); }
}

.email-viewer__frame {
    flex: 1;
    border: none;
    width: 100%;
    background: #fff;          /* light: white paper */
    border-radius: 11px;
}
/* dark: the email content is smart-inverted to a dark surface; match the frame
   element so there's no white flash at the edges or while loading */
[data-theme="dark"] .email-viewer__frame { background: #1f1f1f; }

/* ── Input bar — floats over chat with a gradient fade ─────────────────────── */
.input-bar {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 6px 12px 10px;
    background: linear-gradient(to bottom, transparent 0px, var(--bg) 24px);
    z-index: 5;
}
[data-theme="dark"] .input-bar {
    background: linear-gradient(to bottom, transparent 0px, var(--bg) 24px);
}

.status-text {
    font-size: 11.5px;
    color: var(--text-muted);
    min-height: 15px;
    margin-bottom: 3px;
}
.status-text.error   { color: #dc2626; }
.status-text.success { color: #16a34a; }

/* Attachment info — floating toast that doesn't push chat content */
.attachments-info {
    position: absolute;
    left: 12px;
    right: 12px;
    bottom: 64px;
    padding: 6px 10px;
    border-radius: 8px;
    background: var(--surface);
    border: 1px solid var(--border);
    box-shadow: 0 2px 8px rgba(0,0,0,.08);
    font-size: 11px;
    color: var(--text-muted);
    z-index: 6;
    pointer-events: none;
    animation: toast-in 0.2s ease-out;
}
@keyframes toast-in {
    from { opacity: 0; transform: translateY(4px); }
    to   { opacity: 1; transform: translateY(0); }
}

.input-row {
    display: flex;
    align-items: center;
    gap: 6px;
    background: var(--surface);
    border: 1.5px solid var(--input-border);
    border-radius: 22px;
    padding: 7px 8px 7px 10px;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.input-row:focus-within {
    border-color: var(--blue);
    box-shadow: 0 0 0 3px rgba(26,111,168,.12);
    background: var(--bg);
}

.attach-btn {
    border: none;
    background: transparent;
    color: var(--text-muted);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 4px;
    cursor: pointer;
    flex-shrink: 0;
    border-radius: 6px;
    transition: color 0.15s ease;
}
.attach-btn:hover { color: var(--blue); }

.chat-input {
    flex: 1;
    border: none;
    background: transparent;
    color: var(--text);
    font-family: inherit;
    font-size: 13.5px;
    line-height: 1.45;
    resize: none;
    outline: none;
    max-height: 100px;
    overflow-y: auto;
    padding: 0;
    scrollbar-width: none; /* hidden until user types */
}
.chat-input::-webkit-scrollbar { display: none; }
/* show scrollbar only when user has typed real content */
.chat-input.has-content { scrollbar-width: thin; }
.chat-input.has-content::-webkit-scrollbar { display: block; width: 4px; }
.chat-input.has-content::-webkit-scrollbar-track { background: transparent; }
.chat-input.has-content::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
.chat-input::placeholder { color: var(--text-muted); }

.send-btn {
    width: 30px; height: 30px;
    border-radius: 50%;
    border: none;
    background: var(--send-bg);
    color: #fff;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: background 0.15s ease;
}
.send-btn:hover:not(:disabled) { background: var(--blue-hover); }
.send-btn:disabled { background: var(--send-disabled); cursor: not-allowed; }

/* ── Icon button ───────────────────────────────────────────────────────────── */
.icon-btn {
    border: none; background: transparent; color: var(--text-muted);
    width: 32px; height: 32px; border-radius: 8px;
    display: inline-flex; align-items: center; justify-content: center;
    cursor: pointer; transition: background 0.15s ease; flex-shrink: 0;
}
.icon-btn:hover { background: var(--surface); color: var(--text); }

/* ── Settings panel ────────────────────────────────────────────────────────── */
.settings {
    position: fixed; inset: 0; z-index: 50;
    display: flex; align-items: flex-end; justify-content: center;
    pointer-events: none;
}
.settings:not(.settings--hidden) { pointer-events: auto; }

.settings__backdrop {
    position: absolute; inset: 0;
    background: rgba(0,0,0,.4);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    pointer-events: auto; opacity: 0;
    transition: opacity 0.2s ease;
}

.settings__panel {
    position: relative;
    margin: 0 12px 12px;
    background: var(--bg);
    border-radius: 16px;
    width: calc(100% - 24px); max-width: 400px;
    box-shadow: 0 4px 32px rgba(0,0,0,.18);
    transform: translateY(24px);
    transition: transform 0.25s ease;
    padding: 18px; pointer-events: auto;
    border: 1px solid var(--border);
}

.settings__header {
    display: flex; align-items: flex-start;
    justify-content: space-between; gap: 12px; margin-bottom: 14px;
}
.settings__header h2 { font-size: 16px; font-weight: 600; color: var(--text-heading); }
.settings__hint { font-size: 12.5px; color: var(--text-muted); margin-top: 3px; }
.settings__body { display: flex; flex-direction: column; gap: 10px; }
.settings-status { font-size: 12.5px; color: var(--text-muted); }
.settings-status.success { color: #16a34a; }
.settings-status.error   { color: #dc2626; }

.settings--hidden { visibility: hidden; }
.settings--hidden .settings__backdrop { opacity: 0; }
.settings--hidden .settings__panel { transform: translateY(32px); }
.settings:not(.settings--hidden) .settings__backdrop { opacity: 1; }
.settings:not(.settings--hidden) .settings__panel { transform: translateY(0); }

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
