/* AI assistant: floating button + panel.
   Desktop: a side panel; the page shifts left so the form stays usable.
   Phone: a bottom sheet above the bottom navigation. */

:root {
    --ai-width: 400px;
}

/* ---- Floating button ---- */
.ai-fab {
    position: fixed;
    right: 20px;
    bottom: 24px;
    z-index: 1040;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    height: 48px;
    padding: 0 18px 0 16px;
    border: 0;
    border-radius: 999px;
    background: var(--primary);
    color: #fff;
    font-weight: 600;
    box-shadow: 0 8px 20px rgb(79 70 229 / 0.35);
    transition: transform 0.15s ease, background-color 0.15s ease;
}

.ai-fab:hover {
    background: var(--primary-hover);
    transform: translateY(-1px);
}

.ai-fab i {
    font-size: 1.15rem;
}

body.ai-open .ai-fab {
    display: none;
}

/* ---- Panel ---- */
.ai-panel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: var(--ai-width);
    z-index: 1045;
    display: flex;
    flex-direction: column;
    background: var(--bg-card);
    border-left: 1px solid var(--border-color);
    box-shadow: -8px 0 24px rgb(15 23 42 / 0.08);
    transform: translateX(100%);
    visibility: hidden;
    transition: transform 0.2s ease, visibility 0s linear 0.2s;
}

.ai-panel.open {
    transform: none;
    visibility: visible;
    transition: transform 0.2s ease;
}

@media (min-width: 992px) {
    body.ai-open {
        padding-right: var(--ai-width);
    }
}

@media (max-width: 991px) {

    /* Room for the floating button above the bottom nav, so it never covers
       the last thing on a page. */
    body {
        padding-bottom: calc(140px + var(--safe-bottom));
    }

    .ai-fab {
        bottom: calc(65px + var(--safe-bottom) + 14px);
        width: 52px;
        height: 52px;
        padding: 0;
        justify-content: center;
    }

    .ai-fab-label {
        display: none;
    }

    .ai-panel {
        top: auto;
        left: 0;
        width: auto;
        height: min(88dvh, calc(100dvh - var(--safe-top) - 24px));
        z-index: 1060;
        border-left: 0;
        border-radius: 18px 18px 0 0;
        box-shadow: 0 -8px 30px rgb(15 23 42 / 0.18);
        transform: translateY(100%);
        padding-bottom: var(--safe-bottom);
    }
}

/* Beside a page's own floating button (the dashboard "+", shown below 768px:
   a 56px circle, 24px in from the right, its bottom edge 94px up). Same
   centre line, 12px to its left. */
@media (max-width: 767px) {
    .ai-fab.ai-fab-beside {
        right: calc(24px + 56px + 12px);
        bottom: calc(94px + (56px - 52px) / 2);
    }
}

/* ---- Header ---- */
.ai-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 14px 10px 18px;
    border-bottom: 1px solid var(--border-color);
}

.ai-title {
    font-weight: 700;
    font-size: 1rem;
}

.ai-title i {
    color: var(--primary);
}

.ai-head-actions {
    display: flex;
    gap: 4px;
}

.ai-icon-btn {
    width: 36px;
    height: 36px;
    border: 0;
    border-radius: 10px;
    background: transparent;
    color: var(--text-muted);
}

.ai-icon-btn:hover {
    background: var(--bg-main);
    color: var(--text-main);
}

/* ---- About / case ---- */
.ai-about {
    padding: 10px 18px 12px;
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-main);
}

.ai-about-row {
    display: flex;
    align-items: baseline;
    gap: 8px;
    margin-bottom: 8px;
    min-width: 0;
}

.ai-about-label {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.ai-about-name {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.ai-link-btn {
    border: 0;
    background: none;
    padding: 0;
    color: var(--primary);
    font-size: 0.85rem;
    font-weight: 600;
}

.ai-case {
    border-radius: 10px;
}

/* ---- Patient picker ---- */
.ai-picker {
    padding: 14px 18px;
    border-bottom: 1px solid var(--border-color);
    overflow-y: auto;
    max-height: 60%;
}

.ai-picker-title {
    display: block;
    font-weight: 600;
    margin-bottom: 8px;
}

.ai-picker-results {
    margin-top: 10px;
}

.ai-picker-heading {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    margin: 4px 0 6px;
}

.ai-picker-item {
    display: flex;
    justify-content: space-between;
    width: 100%;
    padding: 9px 10px;
    border: 0;
    border-radius: 10px;
    background: transparent;
    text-align: left;
}

.ai-picker-item:hover {
    background: var(--primary-light);
}

.ai-picker-time,
.ai-picker-empty {
    color: var(--text-muted);
    font-size: 0.85rem;
}

/* ---- Notices ---- */
.ai-notice {
    margin: 10px 14px 0;
    padding: 9px 12px;
    border-radius: 10px;
    background: #fff7ed;
    color: #9a3412;
    font-size: 0.85rem;
}

/* ---- Transcript ---- */
.ai-messages {
    flex: 1;
    overflow-y: auto;
    padding: 14px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    overscroll-behavior: contain;
}

.ai-msg-user {
    align-self: flex-end;
    max-width: 85%;
    padding: 8px 12px;
    border-radius: 14px 14px 4px 14px;
    background: var(--primary-light);
    color: var(--text-main);
    font-size: 0.9rem;
    white-space: pre-wrap;
}

.ai-msg-ai {
    font-size: 0.92rem;
    line-height: 1.55;
}

.ai-answer> :first-child {
    margin-top: 0;
}

.ai-answer> :last-child {
    margin-bottom: 0;
}

.ai-answer h1,
.ai-answer h2,
.ai-answer h3,
.ai-answer h4 {
    font-size: 0.95rem;
    font-weight: 700;
    margin: 14px 0 6px;
}

.ai-answer p {
    margin: 0 0 8px;
}

.ai-answer ul,
.ai-answer ol {
    margin: 0 0 8px;
    padding-inline-start: 1.2rem;
}

.ai-answer li {
    margin-bottom: 3px;
}

.ai-answer table {
    width: 100%;
    font-size: 0.85rem;
    margin-bottom: 8px;
    border-collapse: collapse;
}

.ai-answer th,
.ai-answer td {
    padding: 4px 6px;
    border-bottom: 1px solid var(--border-color);
    text-align: start;
}

.ai-error {
    color: #b91c1c;
    font-size: 0.88rem;
    margin-top: 6px;
}

.ai-tools {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 8px;
}

.ai-tool {
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--bg-card);
    padding: 3px 10px;
    font-size: 0.8rem;
    color: var(--text-muted);
}

.ai-tool:hover {
    color: var(--primary);
    border-color: var(--primary);
}

.ai-thinking {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    padding: 6px 0;
}

.ai-dots {
    display: inline-flex;
    gap: 4px;
}

.ai-thinking-label {
    font-size: 0.85rem;
    color: var(--text-muted);
}

.ai-thinking i {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--secondary);
    opacity: 0.35;
    animation: ai-pulse 1.2s infinite ease-in-out;
}

.ai-thinking i:nth-child(2) {
    animation-delay: 0.15s;
}

.ai-thinking i:nth-child(3) {
    animation-delay: 0.3s;
}

@keyframes ai-pulse {

    0%,
    80%,
    100% {
        opacity: 0.35;
        transform: none;
    }

    40% {
        opacity: 1;
        transform: translateY(-3px);
    }
}

@media (prefers-reduced-motion: reduce) {

    .ai-panel,
    .ai-fab,
    .ai-thinking i {
        transition: none;
        animation: none;
    }
}

/* ---- Quick actions ---- */
.ai-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 8px 14px 0;
}

.ai-chips:empty {
    display: none;
}

.ai-chip {
    border: 1px solid #c7d2fe;
    border-radius: 999px;
    background: var(--primary-light);
    color: var(--primary);
    padding: 5px 12px;
    font-size: 0.82rem;
    font-weight: 600;
    white-space: nowrap;
}

@media (hover: hover) {
    .ai-chip:hover:not(:disabled) {
        background: var(--primary);
        color: #fff;
    }
}

.ai-chip:active:not(:disabled) {
    background: var(--primary);
    color: #fff;
}

.ai-chip:disabled {
    opacity: 0.5;
}

@media (max-width: 991px) {
    .ai-chips {
        flex-wrap: nowrap;
        overflow-x: auto;
        padding-bottom: 2px;
        scrollbar-width: none;
    }
}

/* ---- Compose ---- */
.ai-compose {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    margin: 10px 14px 0;
    padding: 6px 6px 6px 12px;
    border: 1px solid var(--border-color);
    border-radius: 14px;
    background: var(--bg-card);
}

.ai-compose:focus-within {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-light);
}

.ai-compose textarea {
    flex: 1;
    min-width: 0;
    border: 0;
    outline: 0;
    resize: none;
    padding: 6px 0;
    max-height: 140px;
    background: transparent;
    font-size: 16px;
    /* 16px stops iOS zooming into the field */
}

.ai-send {
    flex: none;
    width: 36px;
    height: 36px;
    border: 0;
    border-radius: 10px;
    background: var(--primary);
    color: #fff;
}

.ai-stop {
    background: var(--secondary);
}

.ai-foot {
    margin: 6px 14px 10px;
    font-size: 0.72rem;
    color: var(--text-muted);
    text-align: center;
}

@media print {

    .ai-fab,
    .ai-panel {
        display: none !important;
    }

    body.ai-open {
        padding-right: 0;
    }
}
