/* AI Assistant — floating button + panel + settings card.
   Uses CSS variables with fallbacks so it inherits the host app theme. */

:root {
    --ai-accent: var(--theme-default, #198754);
    --ai-bg: var(--bs-body-bg, #fff);
    --ai-fg: var(--bs-body-color, #212529);
    --ai-border: var(--bs-border-color, #dee2e6);
    --ai-muted: var(--bs-secondary-color, #6c757d);
}

/* ── Floating button ─────────────────────────────────────── */
.ai-fab {
    position: fixed;
    right: 22px;
    bottom: 22px;
    z-index: 1049;
    width: 52px;
    height: 52px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--ai-accent), #2fbf8f);
    color: #fff;
    font-size: 1.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 4px 14px rgba(0, 0, 0, .25);
    transition: transform .15s ease, opacity .2s ease;
    -webkit-user-select: none;
    user-select: none;
}
.ai-fab:hover { transform: scale(1.08); }
.ai-fab-hidden { opacity: 0; pointer-events: none; }

/* ── Panel ───────────────────────────────────────────────── */
.ai-panel {
    position: fixed;
    right: 22px;
    bottom: 22px;
    z-index: 1050;
    width: 400px;
    height: 560px;
    max-width: calc(100vw - 24px);
    max-height: calc(100vh - 24px);
    background: var(--ai-bg);
    color: var(--ai-fg);
    border: 1px solid var(--ai-border);
    border-radius: 14px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, .28);
    display: none;
    flex-direction: column;
    overflow: hidden;
    resize: both;
    min-width: 320px;
    min-height: 380px;
}
.ai-panel.open { display: flex; }

.ai-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 10px 12px;
    border-bottom: 1px solid var(--ai-border);
    cursor: move;
    background: linear-gradient(135deg, var(--ai-accent), #2fbf8f);
    color: #fff;
    touch-action: none;
}
.ai-panel-title { display: flex; align-items: center; gap: 8px; min-width: 0; }
.ai-panel-sub {
    font-size: .72rem;
    opacity: .85;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 250px;
}
.ai-sparkle { font-size: 1.15rem; }
.ai-panel-actions { display: flex; gap: 4px; }
.ai-icon-btn {
    background: rgba(255, 255, 255, .15);
    border: none;
    color: #fff;
    width: 28px;
    height: 28px;
    border-radius: 6px;
    cursor: pointer;
    font-size: .9rem;
    line-height: 1;
}
.ai-icon-btn:hover { background: rgba(255, 255, 255, .3); }

.ai-panel-body {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    scroll-behavior: smooth;
}

.ai-empty { color: var(--ai-muted); font-size: .88rem; text-align: center; margin: auto 8px; }

.ai-msg {
    max-width: 92%;
    padding: 8px 12px;
    border-radius: 12px;
    font-size: .875rem;
    line-height: 1.45;
    word-wrap: break-word;
    overflow-wrap: anywhere;
}
.ai-msg.user {
    align-self: flex-end;
    background: var(--ai-accent);
    color: #fff;
    border-bottom-right-radius: 4px;
    white-space: pre-wrap;
}
.ai-msg.assistant {
    align-self: flex-start;
    background: color-mix(in srgb, var(--ai-fg) 6%, var(--ai-bg));
    border: 1px solid var(--ai-border);
    border-bottom-left-radius: 4px;
}
.ai-msg.error {
    align-self: stretch;
    background: #fdecea;
    color: #b02a37;
    border: 1px solid #f5c2c7;
    font-size: .8rem;
}
.ai-msg.assistant p:last-child { margin-bottom: 0; }
.ai-msg.assistant pre {
    background: rgba(0, 0, 0, .78);
    color: #e6e6e6;
    padding: 8px 10px;
    border-radius: 8px;
    overflow-x: auto;
    font-size: .78rem;
}
.ai-msg.assistant table { border-collapse: collapse; margin: 6px 0; width: 100%; font-size: .8rem; }
.ai-msg.assistant th, .ai-msg.assistant td { border: 1px solid var(--ai-border); padding: 3px 8px; }

.ai-caret { animation: ai-blink 1s step-end infinite; }
@keyframes ai-blink { 50% { opacity: 0; } }

/* ── Suggestions ─────────────────────────────────────────── */
.ai-suggestions {
    display: flex;
    gap: 6px;
    padding: 6px 12px;
    flex-wrap: wrap;
    border-top: 1px solid var(--ai-border);
}
.ai-chip {
    border: 1px solid var(--ai-accent);
    color: var(--ai-accent);
    background: transparent;
    border-radius: 999px;
    font-size: .74rem;
    padding: 3px 10px;
    cursor: pointer;
    transition: background .12s;
}
.ai-chip:hover { background: color-mix(in srgb, var(--ai-accent) 12%, transparent); }

/* ── Model picker row ────────────────────────────────────── */
.ai-model-row {
    display: flex;
    gap: 6px;
    padding: 6px 12px 0;
    align-items: center;
}
.ai-model-select, .ai-model-custom {
    border: 1px solid var(--ai-border);
    border-radius: 8px;
    background: var(--ai-bg);
    color: var(--ai-muted);
    font-size: .74rem;
    padding: 3px 8px;
    max-width: 60%;
    outline: none;
}
.ai-model-custom { flex: 1; color: var(--ai-fg); }
.ai-model-select:focus, .ai-model-custom:focus { border-color: var(--ai-accent); }

/* ── Input row ───────────────────────────────────────────── */
.ai-panel-input {
    display: flex;
    gap: 8px;
    padding: 10px 12px;
    border-top: 1px solid var(--ai-border);
    align-items: flex-end;
}
.ai-panel-input textarea {
    flex: 1;
    resize: none;
    border: 1px solid var(--ai-border);
    border-radius: 10px;
    padding: 8px 10px;
    font-size: .875rem;
    background: var(--ai-bg);
    color: var(--ai-fg);
    max-height: 120px;
    outline: none;
}
.ai-panel-input textarea:focus { border-color: var(--ai-accent); }
.ai-send-btn {
    width: 38px;
    height: 38px;
    border-radius: 10px;
    border: none;
    background: var(--ai-accent);
    color: #fff;
    cursor: pointer;
    font-size: 1rem;
    flex-shrink: 0;
}
.ai-send-btn:disabled { opacity: .45; cursor: default; }
.ai-send-btn.stop { background: #b02a37; }

/* ── Settings card ───────────────────────────────────────── */
.ai-provider-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 8px;
}
.ai-provider-tile {
    text-align: left;
    border: 1px solid var(--ai-border);
    border-radius: 10px;
    background: var(--ai-bg);
    color: var(--ai-fg);
    padding: 8px 10px;
    cursor: pointer;
    transition: border-color .12s, background .12s;
}
.ai-provider-tile:hover { border-color: var(--ai-accent); }
.ai-provider-tile.selected {
    border: 2px solid var(--ai-accent);
    background: color-mix(in srgb, var(--ai-accent) 8%, var(--ai-bg));
}
.ai-provider-tile .name { font-weight: 600; font-size: .82rem; }
.ai-provider-tile .model {
    font-family: var(--bs-font-monospace, monospace);
    font-size: .72rem;
    color: var(--ai-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.ai-provider-tile .keyhint {
    font-family: var(--bs-font-monospace, monospace);
    font-size: .68rem;
    color: #198754;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* ── Mobile: bottom sheet ────────────────────────────────── */
@media (max-width: 576px) {
    .ai-panel {
        left: 0 !important;
        right: 0 !important;
        top: auto !important;
        bottom: 0 !important;
        width: 100% !important;
        max-width: 100%;
        height: 78vh !important;
        border-radius: 14px 14px 0 0;
        resize: none;
    }
    .ai-fab { right: 14px; bottom: 14px; }
}
