/* ==========================================================================
   Ellie IA — Inteligência de Pricing
   Folha de estilo. Paleta esmeralda/dark preservada da identidade BCB App.
   Escala de espaçamento base 4px. Modo light incluído.
   ========================================================================== */

:root {
    /* Paleta — tons escuros em camadas, com leve cast azul-esverdeado */
    --bg: #060910;
    --bg-elev: #0a0f1c;
    --surface: #0e1526;
    --card: #131c33;
    --card-2: #172341;
    --text: #f1f5f9;
    --text-soft: #b6c2d4;
    --muted: #6b7a90;
    --primary: #10b981;
    --primary-soft: #34d399;
    --primary-strong: #14d493;
    --primary-glow: rgba(16, 185, 129, 0.18);
    --border: rgba(148, 178, 210, 0.09);
    --border-strong: rgba(148, 178, 210, 0.18);

    /* Semânticos — ligam na auditoria de matches */
    --ok: #10b981;
    --ok-bg: rgba(16, 185, 129, 0.13);
    --warn: #f59e0b;
    --warn-bg: rgba(245, 158, 11, 0.13);
    --bad: #f43f5e;
    --bad-bg: rgba(244, 63, 94, 0.13);

    /* Escala de espaçamento (base 4px) */
    --sp-1: 4px;
    --sp-2: 8px;
    --sp-3: 12px;
    --sp-4: 16px;
    --sp-5: 20px;
    --sp-6: 24px;
    --sp-8: 32px;
    --sp-10: 40px;

    --radius: 18px;
    --radius-sm: 12px;
    --radius-pill: 999px;

    --sidebar-width: 260px;
    --read-width: 780px;

    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.25);
    --shadow-md: 0 8px 30px rgba(0, 0, 0, 0.30);
    --shadow-lg: 0 16px 50px rgba(0, 0, 0, 0.40);
}

body.light {
    --bg: #f4f6fb;
    --bg-elev: #ffffff;
    --surface: #ffffff;
    --card: #ffffff;
    --card-2: #f3f6fc;
    --text: #0f172a;
    --text-soft: #3d4a5e;
    --muted: #8595aa;
    --border: rgba(15, 40, 80, 0.09);
    --border-strong: rgba(15, 40, 80, 0.16);
    --primary-glow: rgba(16, 185, 129, 0.10);
    --ok-bg: rgba(16, 185, 129, 0.10);
    --warn-bg: rgba(245, 158, 11, 0.12);
    --bad-bg: rgba(239, 68, 68, 0.10);
    --shadow-lg: 0 10px 40px rgba(15, 23, 42, 0.12);
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
    background: var(--bg);
    color: var(--text);
    display: flex;
    min-height: 100vh;
    overflow: hidden;
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.001ms !important;
        transition-duration: 0.001ms !important;
    }
}

/* ==========================================================================
   Sidebar (renderizada por sidebar.php — estilos-base mantidos aqui)
   ========================================================================== */
.sidebar {
    width: var(--sidebar-width);
    background: #0b1120;
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 1000;
    transition: transform 0.3s ease;
}
body.light .sidebar { background: #0b1120; }

.sidebar-brand {
    padding: var(--sp-6);
    font-size: 18px;
    font-weight: 700;
    color: #fff;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: var(--sp-3);
}

.sidebar-menu { list-style: none; padding: var(--sp-4) 0; margin: 0; flex: 1; overflow-y: auto; }

.menu-category {
    padding: var(--sp-4) var(--sp-6) var(--sp-2);
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    color: var(--muted);
    letter-spacing: 1px;
}

.sidebar-item a {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    padding: var(--sp-3) var(--sp-6);
    color: #94a3b8;
    text-decoration: none;
    font-size: 14px;
    font-weight: 500;
    transition: color 0.2s ease, background 0.2s ease;
}
.sidebar-item a:hover { color: #fff; background: rgba(255, 255, 255, 0.03); }
.sidebar-item.active a {
    color: var(--primary);
    background: rgba(16, 185, 129, 0.06);
    border-left: 4px solid var(--primary);
    padding-left: calc(var(--sp-6) - 4px);
    font-weight: 600;
}

.sidebar-footer { padding: var(--sp-4) var(--sp-6); border-top: 1px solid var(--border); font-size: 12px; color: var(--muted); }

.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 999;
    backdrop-filter: blur(2px);
}

/* ==========================================================================
   Área principal
   ========================================================================== */
.main-wrapper {
    margin-left: var(--sidebar-width);
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100vh;
    background:
        radial-gradient(1100px 500px at 78% -8%, rgba(16, 185, 129, 0.07), transparent 60%),
        radial-gradient(900px 600px at 10% 110%, rgba(20, 60, 120, 0.10), transparent 55%),
        var(--bg);
}

.header {
    background: var(--surface);
    padding: var(--sp-4) var(--sp-8);
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: var(--sp-4);
}
.header-titles { min-width: 0; }
.header h1 { margin: 0; font-size: 20px; font-weight: 700; display: flex; align-items: center; gap: var(--sp-2); letter-spacing: -0.01em; }
.header-mark { color: var(--primary-soft); }
.header p { margin: var(--sp-1) 0 0; color: var(--muted); font-size: 13px; display: flex; align-items: center; gap: var(--sp-2); }
.status-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--primary); box-shadow: 0 0 0 3px var(--ok-bg); animation: pulse 2.4s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.4; } }

.header-actions { display: flex; align-items: center; gap: var(--sp-2); }

.menu-toggle {
    display: none;
    background: var(--card);
    border: 1px solid var(--border);
    color: var(--text);
    padding: var(--sp-2) var(--sp-3);
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 14px;
}

.theme-btn {
    background: var(--card);
    color: var(--text);
    border: 1px solid var(--border);
    padding: var(--sp-2) var(--sp-4);
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    transition: background 0.2s, border-color 0.2s;
    white-space: nowrap;
}
.theme-btn:hover { background: var(--card); border-color: var(--border-strong); }

/* ==========================================================================
   Empty state — hero de boas-vindas (some após a primeira mensagem)
   ========================================================================== */
.welcome {
    max-width: var(--read-width);
    margin: auto;
    width: 100%;
    padding: var(--sp-8) var(--sp-4);
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    animation: fadeIn 0.4s ease-out both;
}
.welcome-badge {
    width: 96px !important;
    height: 96px !important;
    min-width: 96px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), #059669);
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 0 0 4px var(--ok-bg), 0 8px 30px var(--primary-glow);
    margin-bottom: var(--sp-5);
    overflow: hidden;
    flex: 0 0 auto;
}
.welcome-badge img {
    width: 100% !important;
    height: 100% !important;
    max-width: 96px;
    object-fit: cover;
    display: block;
}
.welcome-badge .fallback { font-size: 38px; }
.welcome h2 {
    margin: 0 0 var(--sp-2);
    font-size: 26px;
    font-weight: 700;
    letter-spacing: -0.02em;
    color: var(--text);
}
.welcome h2 span { color: var(--primary-soft); }
.welcome p {
    margin: 0 0 var(--sp-8);
    font-size: 15px;
    color: var(--muted);
    max-width: 460px;
    line-height: 1.6;
}
.welcome-cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: var(--sp-3);
    width: 100%;
    max-width: 100%;
}
.wcard {
    text-align: left;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: var(--sp-4);
    cursor: pointer;
    transition: border-color 0.18s, transform 0.18s, background 0.18s;
    display: flex !important;
    flex-direction: column !important;
    align-items: flex-start;
    gap: var(--sp-2);
    min-width: 0;
    font-family: inherit;
    color: inherit;
}
.wcard > * { display: block; width: 100%; }
.wcard .wcard-ic { width: 34px; }
.wcard:hover {
    border-color: var(--border-strong);
    transform: translateY(-2px);
    background: var(--card);
}
.wcard:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.wcard-ic {
    width: 34px;
    height: 34px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 17px;
    background: var(--ok-bg);
    color: var(--primary-soft);
}
.wcard-ic.blue { background: rgba(56, 138, 221, 0.14); color: #7ab7ff; }
.wcard-ic.amber { background: var(--warn-bg); color: var(--warn); }
.wcard-title { font-size: 14px; font-weight: 600; color: var(--text); }
.wcard-desc { font-size: 12.5px; color: var(--muted); line-height: 1.45; }

/* ==========================================================================
   Chat
   ========================================================================== */
.chat-area { flex: 1; display: flex; flex-direction: column; position: relative; overflow: hidden; }

.chat-messages {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: var(--sp-8) var(--sp-6);
    display: flex;
    flex-direction: column;
    gap: var(--sp-6);
    scroll-behavior: smooth;
}
.chat-inner { width: 100%; max-width: var(--read-width); margin: 0 auto; display: flex; flex-direction: column; gap: var(--sp-6); min-width: 0; }

.message-wrapper { display: flex; gap: var(--sp-4); max-width: 92%; animation: fadeIn 0.3s ease-out both; }
.message-wrapper.user { align-self: flex-end; flex-direction: row-reverse; }
.message-wrapper.assistant { align-self: flex-start; }

.avatar {
    width: 40px;
    height: 40px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 800;
    flex-shrink: 0;
    box-shadow: var(--shadow-sm);
    overflow: hidden;
}
.user .avatar { background: var(--card); border: 1px solid var(--border); color: var(--text-soft); }
.assistant .avatar { background: linear-gradient(135deg, var(--primary), #059669); color: #000; box-shadow: 0 0 15px var(--primary-glow); }
.avatar-img { width: 100%; height: 100%; object-fit: cover; border-radius: inherit; }

.bubble {
    padding: var(--sp-5) var(--sp-5);
    border-radius: var(--radius);
    font-size: 15px;
    line-height: 1.65;
    color: var(--text-soft);
    box-shadow: var(--shadow-md);
    word-wrap: break-word;
    overflow-wrap: anywhere;
}
.user .bubble {
    background: var(--card-2);
    color: var(--text);
    border: 1px solid var(--border);
    border-bottom-right-radius: 4px;
}
.assistant .bubble {
    position: relative;
    background: linear-gradient(180deg, var(--card), var(--surface));
    border: 1px solid var(--border-strong);
    border-bottom-left-radius: 4px;
    min-width: 0;
    flex: 1;
}
.assistant .bubble::before {
    content: "";
    position: absolute;
    left: 0;
    top: 16px;
    bottom: 16px;
    width: 3px;
    border-radius: 3px;
    background: linear-gradient(180deg, var(--primary), transparent);
}
body.light .assistant .bubble { background: var(--card); }

.bubble p { margin: 0 0 var(--sp-3); }
.bubble p:last-child { margin-bottom: 0; }
.bubble ul { margin: 0 0 var(--sp-3); padding-left: var(--sp-5); }
.bubble li { margin-bottom: var(--sp-2); }
.bubble li::marker { color: var(--primary); }
.bubble strong { color: var(--text); font-weight: 600; }
.assistant .bubble strong { color: var(--primary-soft); font-weight: 600; }
.user .bubble strong { color: inherit; }

/* Tabela renderizada a partir de markdown no texto da IA */
.bubble .ia-table-wrap { overflow-x: auto; margin: var(--sp-3) 0; border: 1px solid var(--border); border-radius: var(--radius-sm); }
.bubble table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.bubble thead th {
    text-align: left;
    padding: var(--sp-2) var(--sp-3);
    background: rgba(255, 255, 255, 0.03);
    color: var(--text-soft);
    font-weight: 600;
    border-bottom: 1px solid var(--border-strong);
    white-space: nowrap;
    cursor: pointer;
    user-select: none;
}
.bubble thead th .sort-caret { color: var(--muted); font-size: 10px; margin-left: var(--sp-1); }
.bubble tbody td { padding: var(--sp-3) var(--sp-3); border-bottom: 1px solid var(--border); vertical-align: top; }
.bubble td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.bubble .cmp-table td:first-child { font-weight: 500; color: var(--text); }
/* colunas de descrição (as duas primeiras) ganham largura mínima e quebram por palavra */
.bubble .cmp-table td:first-child,
.bubble .cmp-table td:nth-child(2) { min-width: 150px; max-width: 220px; white-space: normal; overflow-wrap: anywhere; line-height: 1.35; }
.bubble .cmp-table th:nth-child(2) { text-align: left; }
.bubble tbody tr:last-child td { border-bottom: none; }
.bubble tbody tr:hover { background: rgba(255, 255, 255, 0.02); }

/* Badges de confiança — ligados na auditoria (BATE / REVISAR / NÃO BATE) */
.badge {
    display: inline-flex;
    align-items: center;
    gap: var(--sp-1);
    padding: 2px var(--sp-2);
    border-radius: var(--radius-pill);
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.02em;
    white-space: nowrap;
    line-height: 1.5;
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.ok { color: var(--ok); background: var(--ok-bg); }
.badge.warn { color: var(--warn); background: var(--warn-bg); }
.badge.bad { color: var(--bad); background: var(--bad-bg); }

/* Loader — agora dentro do fluxo, alinhado ao avatar da IA */
.loader-wrapper { display: none; }
.message-wrapper.loading .bubble { padding: var(--sp-3) var(--sp-5); }
.dots { display: flex; gap: var(--sp-1); align-items: center; height: 24px; }
.dot { width: 7px; height: 7px; background: var(--primary); border-radius: 50%; animation: blink 1.4s infinite both; }
.dot:nth-child(2) { animation-delay: 0.2s; }
.dot:nth-child(3) { animation-delay: 0.4s; }

/* ==========================================================================
   Rodapé / input
   ========================================================================== */
.chat-footer { padding: var(--sp-4) var(--sp-6) var(--sp-6); background: linear-gradient(180deg, transparent, var(--bg) 45%); }
.input-container { max-width: var(--read-width); margin: 0 auto; display: flex; flex-direction: column; gap: var(--sp-3); }

.chips-row { display: flex; align-items: center; gap: var(--sp-2); flex-wrap: wrap; }
.chips-label { font-size: 12px; color: var(--muted); margin-right: var(--sp-1); }
.chip-btn {
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--text-soft);
    padding: var(--sp-2) var(--sp-4);
    border-radius: var(--radius-pill);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: color 0.2s, border-color 0.2s, background 0.2s;
}
.chip-btn:hover { color: var(--text); border-color: var(--primary); background: rgba(16, 185, 129, 0.05); }
.chip-btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

.input-box {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: var(--sp-3) var(--sp-4);
    display: flex;
    align-items: flex-end;
    gap: var(--sp-3);
    box-shadow: var(--shadow-lg);
    transition: border-color 0.2s, box-shadow 0.2s;
}
.input-box:focus-within { border-color: rgba(16, 185, 129, 0.4); box-shadow: var(--shadow-lg), 0 0 0 3px var(--primary-glow); }

.input-box textarea {
    flex: 1;
    background: transparent;
    color: var(--text);
    border: none;
    outline: none;
    font-size: 15px;
    resize: none;
    min-height: 24px;
    max-height: 160px;
    overflow-y: auto;
    font-family: inherit;
    line-height: 24px;
    padding: var(--sp-1) 0;
}
.input-box textarea::placeholder { color: var(--muted); }

.input-box button {
    background: var(--primary);
    color: #070a13;
    border: none;
    width: 38px;
    height: 38px;
    border-radius: 12px;
    cursor: pointer;
    font-weight: bold;
    font-size: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.2s, transform 0.2s;
    box-shadow: 0 4px 12px var(--primary-glow);
}
.input-box button:hover:not(:disabled) { background: var(--primary-strong); transform: translateY(-1px); }
.input-box button:focus-visible { outline: 2px solid var(--text); outline-offset: 2px; }
.input-box button:disabled { background: var(--muted); color: var(--surface); box-shadow: none; cursor: not-allowed; transform: none; }

.input-hint { font-size: 11px; color: var(--muted); text-align: right; padding-right: var(--sp-1); }
.input-hint kbd {
    font-family: inherit;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 0 5px;
    font-size: 10px;
}

/* ==========================================================================
   Animações
   ========================================================================== */
@keyframes blink { 0%, 80%, 100% { opacity: 0.2; transform: scale(0.8); } 40% { opacity: 1; transform: scale(1.2); } }
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

/* ==========================================================================
   Responsividade
   ========================================================================== */
@media (max-width: 1024px) {
    .main-wrapper { margin-left: 0; }
    .menu-toggle { display: block; }
    body.sidebar-open .sidebar { transform: translateX(0); }
    body.sidebar-open .sidebar-overlay { display: block; }
    .sidebar { transform: translateX(-100%); }
}

@media (max-width: 640px) {
    .header { padding: var(--sp-3) var(--sp-4); }
    .header h1 { font-size: 17px; }
    .header p { display: none; }
    .chat-messages { padding: var(--sp-5) var(--sp-4); }
    .chat-footer { padding: var(--sp-3) var(--sp-4) var(--sp-5); }
    .message-wrapper { max-width: 100%; }
    .chips-label { display: none; }
}