:root {
    --aia-w: 420px;
}

/* --- Overlay-режим: панель поверх контента, без раздвигания --- */
@media (max-width: 640px) {
    :root { --aia-w: 100vw; }
}

/* --- Кнопка-лаунчер --- */
.aia-launcher {
    position: fixed;
    right: 20px;
    bottom: 20px;
    z-index: 9998;
    width: 48px;
    height: 48px;
    border-radius: 9999px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    background: #006B3C;
    box-shadow: 0 6px 20px rgba(0, 0, 0, .25);
    cursor: pointer;
    border: none;
    transition: transform .15s ease, background .15s ease;
}
.aia-launcher:hover { transform: scale(1.06); }

/* --- Панель --- */
.aia-drawer {
    position: fixed;
    top: 0;
    right: 0;
    height: 100vh;
    width: var(--aia-w);
    max-width: 100vw;
    z-index: 9999;
    display: flex;
    flex-direction: column;
    background: #fff;
    color: #111827;
    border-left: 1px solid rgba(0, 0, 0, .08);
    box-shadow: -8px 0 30px rgba(0, 0, 0, .12);
    transform: translateX(100%);
    transition: transform .25s ease;
}
.aia-drawer.aia-open { transform: translateX(0); }

html.dark .aia-drawer {
    background: #18181b;
    color: #e5e7eb;
    border-left-color: rgba(255, 255, 255, .08);
}

/* --- Шапка --- */
.aia-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 14px;
    border-bottom: 1px solid rgba(0, 0, 0, .08);
}
html.dark .aia-header { border-bottom-color: rgba(255, 255, 255, .08); }
.aia-title { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 14px; }
.aia-cost {
    font-size: 11px; font-weight: 500; opacity: .65;
    padding: 1px 7px; border-radius: 9999px;
    background: rgba(0, 0, 0, .06); font-feature-settings: "tnum";
}
html.dark .aia-cost { background: rgba(255, 255, 255, .1); }
.aia-dot { width: 8px; height: 8px; border-radius: 9999px; background: #16a34a; box-shadow: 0 0 0 3px rgba(22, 163, 74, .18); }
.aia-header-actions { display: flex; gap: 4px; }
.aia-icon-btn {
    display: flex; align-items: center; justify-content: center;
    width: 30px; height: 30px; border-radius: 8px; border: none; cursor: pointer;
    background: transparent; color: inherit; opacity: .7;
}
.aia-icon-btn:hover { opacity: 1; background: rgba(0, 0, 0, .06); }
html.dark .aia-icon-btn:hover { background: rgba(255, 255, 255, .08); }

.aia-warning {
    margin: 10px 14px 0;
    padding: 10px 12px;
    font-size: 13px;
    border-radius: 8px;
    background: #fef3c7;
    color: #92400e;
}

/* --- История диалогов --- */
[x-cloak] { display: none !important; }
.aia-history {
    position: absolute;
    top: 54px;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 5;
    background: #fff;
    overflow-y: auto;
    padding: 8px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}
html.dark .aia-history { background: #18181b; }
.aia-history-head {
    display: flex; align-items: center; justify-content: space-between;
    font-weight: 600; font-size: 13px; padding: 6px 8px; opacity: .8;
}
.aia-history-empty { padding: 16px 8px; font-size: 13px; opacity: .55; text-align: center; }
.aia-history-search {
    margin: 2px 4px 8px; padding: 8px 10px; border-radius: 8px;
    border: 1px solid rgba(0, 0, 0, .15); background: #fff; color: inherit;
    font: inherit; font-size: 13px; outline: none;
}
.aia-history-search:focus { border-color: #16a34a; }
html.dark .aia-history-search { background: #27272a; border-color: rgba(255, 255, 255, .15); }
.aia-history-group {
    font-size: 11px; text-transform: uppercase; letter-spacing: .04em;
    opacity: .5; padding: 8px 10px 4px; font-weight: 600;
}
.aia-history-item {
    display: flex; align-items: center; gap: 4px;
    border-radius: 8px; padding-right: 4px;
}
.aia-history-item:hover { background: rgba(0, 0, 0, .06); }
html.dark .aia-history-item:hover { background: rgba(255, 255, 255, .08); }
.aia-history-active { background: rgba(22, 163, 74, .12); }
.aia-history-open {
    flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px;
    text-align: left; border: none; cursor: pointer; background: transparent;
    padding: 9px 10px; color: inherit; font: inherit;
}
.aia-history-title { font-size: 13.5px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.aia-history-date { font-size: 11px; opacity: .5; }
.aia-history-actions { display: flex; gap: 2px; opacity: 0; transition: opacity .12s; }
.aia-history-item:hover .aia-history-actions { opacity: 1; }
.aia-icon-btn.aia-danger:hover { color: #dc2626; }

/* --- Сообщения --- */
.aia-messages {
    flex: 1;
    overflow-y: auto;
    padding: 14px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}
.aia-empty { margin: auto; text-align: center; font-size: 13px; opacity: .55; padding: 0 24px; }

.aia-msg { display: flex; }
.aia-msg-user { justify-content: flex-end; }
.aia-msg-assistant { justify-content: flex-start; }

.aia-bubble {
    max-width: 88%;
    padding: 9px 12px;
    border-radius: 14px;
    font-size: 14px;
    line-height: 1.5;
    word-wrap: break-word;
    overflow-wrap: anywhere;
}
.aia-msg-user .aia-bubble { background: #006B3C; color: #fff; border-bottom-right-radius: 4px; }
.aia-msg-assistant .aia-bubble { background: #f1f3f5; color: #111827; border-bottom-left-radius: 4px; }
html.dark .aia-msg-assistant .aia-bubble { background: #27272a; color: #e5e7eb; }

.aia-text :where(p) { margin: 0 0 8px; }
.aia-text :where(p):last-child { margin-bottom: 0; }
.aia-text :where(pre) { background: rgba(0, 0, 0, .06); padding: 8px; border-radius: 8px; overflow-x: auto; font-size: 12px; }
.aia-text :where(code) { font-family: ui-monospace, monospace; font-size: 12.5px; }
.aia-text :where(ul, ol) { margin: 4px 0; padding-left: 18px; }
.aia-text :where(li) { margin: 2px 0; }
.aia-text :where(a) { color: #2563eb; text-decoration: underline; }

/* Таблицы */
.aia-tbl-wrap { margin: 8px 0; overflow-x: auto; border: 1px solid rgba(0, 0, 0, .1); border-radius: 8px; }
html.dark .aia-tbl-wrap { border-color: rgba(255, 255, 255, .12); }
.aia-tbl { border-collapse: collapse; width: 100%; font-size: 12.5px; }
.aia-tbl th, .aia-tbl td { padding: 6px 10px; text-align: left; border-bottom: 1px solid rgba(0, 0, 0, .08); white-space: nowrap; }
html.dark .aia-tbl th, html.dark .aia-tbl td { border-bottom-color: rgba(255, 255, 255, .08); }
.aia-tbl th { background: rgba(0, 0, 0, .04); font-weight: 600; }
html.dark .aia-tbl th { background: rgba(255, 255, 255, .06); }
.aia-tbl tbody tr:last-child td { border-bottom: none; }
.aia-tbl tbody tr:hover { background: rgba(22, 163, 74, .06); }

/* --- Инструменты --- */
.aia-tool {
    margin-bottom: 6px; padding: 6px 8px;
    border-radius: 8px; font-size: 12px;
    background: rgba(0, 0, 0, .05); border-left: 3px solid #16a34a;
}
.aia-tool-fail { border-left-color: #dc2626; }
.aia-tool-head {
    display: flex; align-items: baseline; gap: 6px; width: 100%;
    background: transparent; border: none; cursor: pointer; padding: 0;
    color: inherit; font: inherit; text-align: left;
}
.aia-tool-name { font-weight: 600; }
.aia-tool-summary { opacity: .7; flex: 1; }
.aia-tool-caret { opacity: .5; font-size: 10px; }
.aia-tool-details { margin-top: 6px; }
.aia-tool-details pre {
    margin: 4px 0 0; padding: 6px 8px; border-radius: 6px;
    background: rgba(0, 0, 0, .06); font-size: 11px; line-height: 1.4;
    max-height: 220px; overflow: auto; white-space: pre-wrap; word-break: break-word;
}
html.dark .aia-tool { background: rgba(255, 255, 255, .06); }
html.dark .aia-tool-details pre { background: rgba(0, 0, 0, .25); }

/* --- Кнопки действий --- */
.aia-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.aia-action-btn {
    border: 1px solid #006B3C; color: #006B3C; background: transparent;
    padding: 6px 12px; border-radius: 9999px; font-size: 13px; font-weight: 500;
    cursor: pointer; transition: background .12s, color .12s;
}
.aia-action-btn:hover { background: #006B3C; color: #fff; }
html.dark .aia-action-btn { border-color: #34d399; color: #34d399; }
html.dark .aia-action-btn:hover { background: #34d399; color: #0b1f17; }

/* --- Подтверждение --- */
.aia-approval { margin-top: 8px; padding: 10px; border-radius: 10px; background: #fff7ed; border: 1px solid #fdba74; }
html.dark .aia-approval { background: #2a1f12; border-color: #7c4a1d; }
.aia-approval-msg { font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.aia-approval-tool code { font-size: 12px; font-weight: 600; }
.aia-approval-tool pre { margin: 4px 0 8px; font-size: 11.5px; max-height: 140px; overflow: auto; background: rgba(0, 0, 0, .05); padding: 6px; border-radius: 6px; }
.aia-approval-actions { display: flex; gap: 8px; }
.aia-btn { flex: 1; padding: 7px 10px; border-radius: 8px; border: none; cursor: pointer; font-size: 13px; font-weight: 600; }
.aia-btn-approve { background: #16a34a; color: #fff; }
.aia-btn-reject { background: transparent; color: inherit; border: 1px solid rgba(0, 0, 0, .2); }
html.dark .aia-btn-reject { border-color: rgba(255, 255, 255, .25); }

/* --- Ввод --- */
.aia-input {
    display: flex; align-items: flex-end; gap: 8px;
    padding: 12px 14px;
    border-top: 1px solid rgba(0, 0, 0, .08);
}
html.dark .aia-input { border-top-color: rgba(255, 255, 255, .08); }
.aia-input textarea {
    flex: 1; resize: none; max-height: 140px;
    padding: 9px 12px; border-radius: 10px; font-size: 14px;
    border: 1px solid rgba(0, 0, 0, .15); background: #fff; color: inherit;
    font-family: inherit; line-height: 1.4; outline: none;
}
.aia-input textarea:focus { border-color: #16a34a; }
html.dark .aia-input textarea { background: #27272a; border-color: rgba(255, 255, 255, .15); }
.aia-send {
    display: flex; align-items: center; justify-content: center;
    width: 40px; height: 40px; border-radius: 10px; border: none; cursor: pointer;
    background: #006B3C; color: #fff; flex-shrink: 0;
}
.aia-send:disabled { opacity: .45; cursor: default; }

/* --- Индикатор печати --- */
.aia-typing { display: inline-flex; gap: 4px; align-items: center; }
.aia-typing i { width: 6px; height: 6px; border-radius: 9999px; background: currentColor; opacity: .5; animation: aia-blink 1.2s infinite; }
.aia-typing i:nth-child(2) { animation-delay: .2s; }
.aia-typing i:nth-child(3) { animation-delay: .4s; }
@keyframes aia-blink { 0%, 60%, 100% { opacity: .25; } 30% { opacity: .9; } }
