/* Ask EMS (AI assistant) and AI settings. Uses the variables from app.css. */

/* ---------- floating button ---------- */
.ai-fab {
    position: fixed; right: 1.5rem; bottom: 1.5rem; z-index: 60;
    display: inline-flex; align-items: center; gap: .4rem;
    padding: .5rem .95rem .5rem .8rem; border: 0; border-radius: 999px;
    background: linear-gradient(135deg, var(--blue-500), var(--navy-900));
    color: #fff; font: inherit; font-size: .84rem; font-weight: 500; letter-spacing: .01em; cursor: pointer;
    box-shadow: 0 6px 18px rgba(11, 61, 145, .28), 0 1px 3px rgba(11, 61, 145, .2);
    transition: transform .18s ease, box-shadow .18s ease;
    animation: ai-fab-in .4s cubic-bezier(.2, .9, .3, 1.2) both;
}
.ai-fab:hover { transform: translateY(-1px); box-shadow: 0 9px 24px rgba(11, 61, 145, .34); }
.ai-fab:focus-visible { outline: 3px solid rgba(56, 189, 248, .6); outline-offset: 3px; }
.ai-fab .nav-icon { color: #bfe6ff; }
@keyframes ai-fab-in { from { opacity: 0; transform: translateY(12px) scale(.9); } to { opacity: 1; transform: none; } }

/* ---------- side panel ---------- */
.ai-panel {
    position: fixed; top: 0; right: 0; bottom: 0; z-index: 70;
    width: min(460px, 100vw);
    background: var(--surface); border-left: 1px solid var(--border);
    box-shadow: -18px 0 40px rgba(15, 23, 42, .14);
    transform: translateX(105%); visibility: hidden;
    transition: transform .28s cubic-bezier(.2, .8, .2, 1), visibility 0s linear .28s;
    display: flex;
}
.ai-panel.is-open { transform: none; visibility: visible; transition: transform .28s cubic-bezier(.2, .8, .2, 1), visibility 0s; }
.ai-panel-backdrop { display: none; }
@media (max-width: 760px) {
    .ai-panel-backdrop.is-open { display: block; position: fixed; inset: 0; z-index: 65; background: rgba(10, 37, 64, .35); }
    .ai-fab { right: 1rem; bottom: 1rem; }
}

/* ---------- full page ---------- */
.ai-page { height: calc(100vh - 150px); min-height: 520px; }
.ai-page .ai-chat { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); overflow: hidden; }

/* ---------- chat layout ---------- */
.ai-chat { display: flex; width: 100%; height: 100%; min-height: 0; }
.ai-main { flex: 1; min-width: 0; display: flex; flex-direction: column; min-height: 0; }

.ai-head {
    display: flex; align-items: center; justify-content: space-between; gap: .75rem;
    padding: .8rem 1rem; border-bottom: 1px solid var(--border);
    background: linear-gradient(180deg, #f7faff, #fff);
}
.ai-head-title { display: flex; align-items: center; gap: .7rem; min-width: 0; }
.ai-head-title strong { display: block; font-size: 1rem; color: var(--navy-900); }
.ai-head-title small { display: flex; align-items: center; gap: .35rem; color: var(--muted); font-size: .78rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ai-head-actions { display: flex; gap: .15rem; }
.ai-logo {
    display: inline-grid; place-items: center; width: 36px; height: 36px; border-radius: 11px; flex: none;
    background: linear-gradient(135deg, var(--blue-500), var(--navy-900)); color: #fff;
    box-shadow: 0 4px 12px rgba(11, 61, 145, .3);
}
.ai-dot { width: 7px; height: 7px; border-radius: 50%; background: #1baf7a; box-shadow: 0 0 0 3px rgba(27, 175, 122, .18); flex: none; }

.ai-messages { flex: 1; min-height: 0; overflow-y: auto; padding: 1.1rem 1rem 1.5rem; scroll-behavior: smooth; background: #fbfcfe; }

/* empty state */
.ai-empty { display: flex; flex-direction: column; align-items: center; text-align: center; gap: .6rem; padding: 1.5rem .5rem; animation: ai-rise .35s ease both; }
.ai-empty h2 { margin: .35rem 0 0; font-size: 1.15rem; color: var(--navy-900); }
.ai-empty p { margin: 0; max-width: 52ch; color: var(--muted); font-size: .92rem; }
.ai-empty-icon {
    display: inline-grid; place-items: center; width: 60px; height: 60px; border-radius: 18px;
    background: radial-gradient(circle at 30% 25%, #e7f3ff, #cfe3fb); color: var(--blue-600);
    box-shadow: inset 0 0 0 1px rgba(21, 96, 189, .12);
}
.ai-suggestions { display: flex; flex-wrap: wrap; justify-content: center; gap: .5rem; margin-top: .75rem; }
.ai-chip {
    padding: .5rem .8rem; border: 1px solid #d5e2f3; border-radius: 12px; background: #fff;
    color: var(--blue-800); font: inherit; font-size: .85rem; text-align: start; cursor: pointer; max-width: 100%;
    transition: background .15s, border-color .15s, transform .15s;
}
.ai-chip:hover { background: #eef5ff; border-color: #a9c7ee; transform: translateY(-1px); }
.ai-notice { display: flex; justify-content: center; padding: 2rem 1rem; color: var(--muted); }
.ai-notice-error { color: var(--danger); }

/* messages */
.ai-row { display: flex; gap: .55rem; margin: 0 0 1rem; animation: ai-rise .25s ease both; }
.ai-row-user { justify-content: flex-end; }
.ai-avatar {
    display: inline-grid; place-items: center; width: 28px; height: 28px; border-radius: 9px; flex: none; margin-top: 2px;
    background: linear-gradient(135deg, var(--blue-500), var(--navy-900)); color: #fff;
}
.ai-bubble { max-width: 88%; min-width: 0; border-radius: 16px; line-height: 1.55; font-size: .93rem; overflow-wrap: anywhere; }
.ai-bubble-user {
    padding: .6rem .9rem; white-space: pre-wrap;
    background: linear-gradient(180deg, var(--blue-500), var(--blue-600)); color: #fff;
    border-bottom-right-radius: 5px; box-shadow: 0 2px 8px rgba(21, 96, 189, .25);
}
.ai-bubble-bot { flex: 1; padding: .7rem .95rem; background: #fff; border: 1px solid var(--border); border-top-left-radius: 5px; box-shadow: 0 1px 2px rgba(15, 23, 42, .04); }
.ai-bubble-bot.is-error { background: #fff7f6; border-color: #f4c7c3; color: var(--danger); }

.ai-working { display: flex; align-items: center; gap: .6rem; color: var(--muted); font-size: .88rem; }
.ai-typing { display: inline-flex; gap: 3px; }
.ai-typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--blue-500); opacity: .35; animation: ai-blink 1.1s infinite ease-in-out; }
.ai-typing i:nth-child(2) { animation-delay: .15s; }
.ai-typing i:nth-child(3) { animation-delay: .3s; }
.ai-steps { list-style: none; margin: .55rem 0 0; padding: 0; font-size: .8rem; color: var(--muted); }
.ai-steps li { display: flex; align-items: center; gap: .4rem; padding: .1rem 0; animation: ai-rise .2s ease both; }
.ai-steps li::before { content: ""; width: 14px; height: 14px; flex: none; border-radius: 50%; background: #dcf5e8 url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2314633f' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 12l4 4 8-8'/%3E%3C/svg%3E") center / 10px no-repeat; }
.ai-steps li:last-child::before { background: #e3eefc; animation: ai-blink 1.1s infinite ease-in-out; }

/* markdown inside answers */
.ai-md > :first-child { margin-top: 0; }
.ai-md > :last-child { margin-bottom: 0; }
.ai-md p { margin: 0 0 .6rem; }
.ai-md h3, .ai-md h4, .ai-md h5 { margin: .8rem 0 .35rem; font-size: .95rem; color: var(--navy-900); }
.ai-md ul, .ai-md ol { margin: .2rem 0 .7rem; padding-inline-start: 1.25rem; }
.ai-md li { margin: .15rem 0; }
.ai-md li.md-sub { margin-inline-start: 1rem; }
.ai-md strong { color: var(--navy-900); font-weight: 650; }
.ai-md code { padding: .05rem .3rem; border-radius: 5px; background: #eef2f7; font-size: .85em; }
.ai-md pre { padding: .6rem .75rem; border-radius: 8px; background: #0f172a; color: #e2e8f0; overflow-x: auto; font-size: .82rem; }
.ai-md pre code { background: none; padding: 0; }
.ai-md hr { border: 0; border-top: 1px solid var(--border); margin: .8rem 0; }
.ai-md .md-table { overflow-x: auto; margin: .3rem 0 .7rem; border: 1px solid var(--border); border-radius: 10px; }
.ai-md table { width: 100%; border-collapse: collapse; font-size: .84rem; }
.ai-md th, .ai-md td { padding: .4rem .6rem; border-bottom: 1px solid var(--border); text-align: start; white-space: nowrap; }
.ai-md th { background: #f6f9fd; color: #334155; font-weight: 600; }
.ai-md tbody tr:last-child td { border-bottom: 0; }
.ai-md tbody tr:hover td { background: #fafcff; }

.ai-chart { margin: .75rem 0 0; padding: .6rem .7rem .3rem; border: 1px solid var(--border); border-radius: 12px; background: #fcfdff; animation: ai-rise .3s ease both; }
.ai-chart figcaption { font-size: .8rem; font-weight: 600; color: #334155; margin-bottom: .2rem; }

.ai-foot { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .5rem; margin-top: .7rem; padding-top: .55rem; border-top: 1px dashed var(--border); }
.ai-sources { display: flex; flex-wrap: wrap; align-items: center; gap: .35rem; font-size: .75rem; color: var(--muted); }
.ai-source { display: inline-flex; padding: .15rem .5rem; border-radius: 999px; background: #eef5ff; color: var(--blue-800); text-decoration: none; font-weight: 600; }
a.ai-source:hover { background: #dceafe; }
.ai-copy { display: inline-flex; align-items: center; gap: .3rem; margin-inline-start: auto; padding: .2rem .45rem; border: 0; border-radius: 7px; background: none; color: var(--muted); font: inherit; font-size: .75rem; cursor: pointer; }
.ai-copy:hover { background: #eef2f7; color: var(--text); }

/* composer */
.ai-compose { padding: .7rem .9rem .8rem; border-top: 1px solid var(--border); background: #fff; }
.ai-input {
    display: flex; align-items: flex-end; gap: .5rem; padding: .45rem .45rem .45rem .8rem;
    border: 1px solid #cbd5e1; border-radius: 14px; background: #fff; transition: border-color .15s, box-shadow .15s;
}
.ai-input:focus-within { border-color: var(--blue-500); box-shadow: 0 0 0 3px rgba(29, 120, 216, .16); }
.ai-input textarea {
    flex: 1; min-height: 24px; max-height: 180px; padding: .3rem 0; border: 0; outline: 0; resize: none;
    font: inherit; font-size: .93rem; line-height: 1.45; background: transparent; color: var(--text);
}
.ai-send {
    display: inline-grid; place-items: center; width: 38px; height: 38px; flex: none; border: 0; border-radius: 11px;
    background: linear-gradient(180deg, var(--blue-500), var(--blue-600)); color: #fff; cursor: pointer; transition: opacity .15s, transform .15s;
}
.ai-send:hover:not([disabled]) { transform: translateY(-1px); }
.ai-send[disabled] { opacity: .4; cursor: default; }
.ai-send.is-stop { background: #334155; }
.ai-compose-note { margin-top: .4rem; font-size: .72rem; color: var(--muted); text-align: center; }

/* history (full page) */
.ai-history { width: 270px; flex: none; display: flex; flex-direction: column; gap: .5rem; padding: .9rem; border-right: 1px solid var(--border); background: #f7f9fc; overflow-y: auto; }
.ai-history-title { margin-top: .6rem; font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.ai-history-empty { font-size: .85rem; color: var(--muted); }
.ai-history ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.ai-history li { display: flex; align-items: center; border-radius: 9px; }
.ai-history li:hover, .ai-history li.active { background: #e8f0fb; }
.ai-history-item { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: flex-start; gap: .1rem; padding: .45rem .55rem; border: 0; background: none; font: inherit; text-align: start; cursor: pointer; color: var(--text); }
.ai-history-text { width: 100%; font-size: .85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ai-history-date { font-size: .7rem; color: var(--muted); }
.ai-history-delete { width: 30px; height: 30px; opacity: 0; color: var(--muted); }
.ai-history li:hover .ai-history-delete { opacity: 1; }
@media (max-width: 860px) { .ai-history { display: none; } .ai-page { height: calc(100vh - 120px); } }

@keyframes ai-rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@keyframes ai-blink { 0%, 80%, 100% { opacity: .3; } 40% { opacity: 1; } }

@media (prefers-reduced-motion: reduce) {
    .ai-fab, .ai-row, .ai-empty, .ai-chart, .ai-steps li { animation: none; }
    .ai-panel, .ai-panel.is-open { transition: none; }
    .ai-messages { scroll-behavior: auto; }
}

/* ---------- AI settings (admin) ---------- */
.btn-sm { padding: .4rem .7rem; font-size: .82rem; border-radius: 8px; }
.ai-actions { display: flex; flex-wrap: wrap; align-items: center; gap: .5rem; margin-top: 1rem; }
.ai-actions .spacer { flex: 1; }
.ai-conn-list { display: flex; flex-direction: column; gap: .75rem; }
.ai-conn { transition: border-color .15s, box-shadow .15s; }
.ai-conn.is-active { border-color: #9fd7bd; box-shadow: 0 0 0 3px rgba(27, 175, 122, .1); }
.ai-conn.selected { border-color: var(--blue-500); }
.ai-conn-head { display: flex; align-items: center; justify-content: space-between; gap: .5rem; }
.ai-conn-head strong { margin-inline-end: .4rem; }
.ai-conn-preset { font-size: .75rem; color: var(--muted); }
.ai-conn-meta { display: grid; grid-template-columns: 80px minmax(0, 1fr); gap: .25rem .75rem; margin: .7rem 0; font-size: .85rem; }
.ai-conn-meta dt { color: var(--muted); }
.ai-conn-meta dd { margin: 0; min-width: 0; }
.ai-ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ai-conn-actions { display: flex; flex-wrap: wrap; gap: .4rem; }
.ai-conn-actions .btn-danger { margin-inline-start: auto; }
.ai-admin-empty { margin-bottom: .75rem; }
.ai-key-row { display: flex; gap: .5rem; align-items: center; }
.ai-key-row input { flex: 1; min-width: 0; }
.ai-advanced { margin-top: .9rem; padding: .6rem .8rem; border: 1px solid var(--border); border-radius: 10px; background: #fafcff; }
.ai-advanced summary { cursor: pointer; font-weight: 600; font-size: .88rem; color: #334155; }
.ai-advanced[open] summary { margin-bottom: .4rem; }
.ai-advanced label, .ai-options label:not(.check) { display: block; margin-top: .6rem; }
.ai-advanced input, .ai-advanced select, .ai-options input[type=number], .ai-options textarea { width: 100%; }
.ai-options textarea, .admin-form textarea {
    padding: .6rem .75rem; border: 1px solid #cbd5e1; border-radius: 10px; font: inherit; font-size: .92rem; resize: vertical;
}
.ai-options textarea:focus { outline: none; border-color: var(--blue-500); box-shadow: 0 0 0 3px rgba(29, 120, 216, .18); }
.ai-two { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }
.ai-three { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: .75rem 1.25rem; margin-top: .5rem; }
.ai-test { display: flex; flex-direction: column; gap: .15rem; margin-top: 1rem; padding: .65rem .85rem; border-radius: 10px; font-size: .88rem; }
.ai-test.ok { background: #ecfbf3; border: 1px solid #a7e3c4; color: #14633f; }
.ai-test.warn { background: #fff8e6; border: 1px solid #f5d38a; color: #7a4d00; }
.ai-test.fail { background: #fef3f2; border: 1px solid #fecdca; color: var(--danger); }
.ai-test small { color: inherit; opacity: .8; }
.ai-usage-head { display: flex; justify-content: flex-end; margin-bottom: .75rem; }
.ai-usage-days { width: auto; }
.ai-usage-kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: .75rem; margin-bottom: .9rem; }
.ai-usage-kpis .card { display: flex; flex-direction: column; gap: .2rem; }
.ai-usage-kpis span { color: var(--muted); font-size: .82rem; font-weight: 600; }
.ai-usage-kpis strong { font-size: 1.6rem; color: var(--navy-900); letter-spacing: -.02em; }
.ai-usage-kpis + .card, .chart-card + .card { margin-top: .9rem; }
