/* ===== AI CHAT WIDGET ===== */
:root {
    --chat-accent: var(--first-color);
    --chat-accent-alt: var(--first-color-alt);
    --chat-panel: rgba(255, 255, 255, 0.94);
    --chat-panel-solid: #ffffff;
    --chat-border: rgba(15, 23, 42, 0.12);
    --chat-border-strong: rgba(37, 99, 235, 0.22);
    --chat-text: var(--title-color);
    --chat-muted: var(--text-color-light);
    --chat-bot: rgba(248, 250, 252, 0.92);
    --chat-user: linear-gradient(135deg, #2563eb, #1d4ed8);
    --chat-code-inline-bg: rgba(37, 99, 235, 0.08);
    --chat-code-inline-border: rgba(37, 99, 235, 0.18);
    --chat-code-block-bg: #0f172a;
    --chat-code-block-text: #e2e8f0;
    --chat-code-scroll-track: rgba(148, 163, 184, 0.18);
    --chat-code-scroll-thumb: rgba(37, 99, 235, 0.72);
    --chat-shadow: 0 24px 70px rgba(15, 23, 42, 0.22);
    --chat-fab-size: 3.55rem;
    --chat-panel-top: max(5.75rem, env(safe-area-inset-top));
    --chat-panel-bottom: calc(max(1.25rem, env(safe-area-inset-bottom)) + var(--chat-fab-size) + 0.85rem);
    --floating-control-bottom: max(1.25rem, env(safe-area-inset-bottom));
    --floating-control-side: max(1.25rem, env(safe-area-inset-left));
}

.dark-theme {
    --chat-panel: rgba(15, 23, 42, 0.92);
    --chat-panel-solid: #101826;
    --chat-border: rgba(148, 163, 184, 0.16);
    --chat-border-strong: rgba(59, 130, 246, 0.34);
    --chat-bot: rgba(15, 23, 42, 0.82);
    --chat-code-inline-bg: rgba(59, 130, 246, 0.12);
    --chat-code-inline-border: rgba(59, 130, 246, 0.22);
    --chat-code-block-bg: rgba(2, 6, 23, 0.94);
    --chat-code-block-text: #dbeafe;
    --chat-code-scroll-track: rgba(148, 163, 184, 0.16);
    --chat-code-scroll-thumb: rgba(59, 130, 246, 0.78);
    --chat-shadow: 0 28px 80px rgba(0, 0, 0, 0.46);
}

@media (prefers-color-scheme: dark) {
    :root:not(.light-theme) {
        --chat-panel: rgba(15, 23, 42, 0.92);
        --chat-panel-solid: #101826;
        --chat-border: rgba(148, 163, 184, 0.16);
        --chat-border-strong: rgba(59, 130, 246, 0.34);
        --chat-bot: rgba(15, 23, 42, 0.82);
        --chat-code-inline-bg: rgba(59, 130, 246, 0.12);
        --chat-code-inline-border: rgba(59, 130, 246, 0.22);
        --chat-code-block-bg: rgba(2, 6, 23, 0.94);
        --chat-code-block-text: #dbeafe;
        --chat-code-scroll-track: rgba(148, 163, 184, 0.16);
        --chat-code-scroll-thumb: rgba(59, 130, 246, 0.78);
        --chat-shadow: 0 28px 80px rgba(0, 0, 0, 0.46);
    }
}

/* Existing floating navigation/scroll controls move left so chat owns the right side. */
.scrollup,
.scrolldown {
    left: var(--floating-control-side);
    right: auto;
}

.scrollup.show-scroll {
    bottom: calc(var(--floating-control-bottom) + 3.75rem);
}

.scrolldown.show-scroll {
    bottom: var(--floating-control-bottom);
}

.chat-widget {
    position: fixed;
    right: max(1.25rem, env(safe-area-inset-right));
    bottom: max(1.25rem, env(safe-area-inset-bottom));
    z-index: 900;
    font-family: var(--body-font);
    color: var(--chat-text);
}

body.is-loading .chat-widget {
    visibility: hidden;
}

.chat-widget__fab {
    position: fixed;
    right: max(1.25rem, env(safe-area-inset-right));
    bottom: max(1.25rem, env(safe-area-inset-bottom));
    width: var(--chat-fab-size);
    height: var(--chat-fab-size);
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, var(--chat-accent), var(--chat-accent-alt));
    color: #ffffff;
    box-shadow: 0 18px 38px rgba(37, 99, 235, 0.36), 0 0 0 10px rgba(37, 99, 235, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.28);
    transition: transform 0.25s ease, box-shadow 0.25s ease;
    z-index: 2;
}

.chat-widget__fab:hover {
    transform: translateY(-3px);
    box-shadow: 0 22px 45px rgba(37, 99, 235, 0.42), 0 0 0 12px rgba(37, 99, 235, 0.10);
}

.chat-widget__fab i {
    font-size: 1.42rem;
}

.chat-widget__fab-icon--close {
    display: none;
}

.chat-widget[data-open="true"] .chat-widget__fab-icon--bot {
    display: none;
}

.chat-widget[data-open="true"] .chat-widget__fab-icon--close {
    display: inline-block;
}

.chat-widget__status-dot {
    position: absolute;
    right: 0.28rem;
    bottom: 0.28rem;
    width: 0.78rem;
    height: 0.78rem;
    border-radius: 999px;
    background: #22c55e;
    border: 3px solid var(--chat-panel-solid);
}

.chat-widget[data-open="true"] .chat-widget__status-dot {
    display: none;
}

.chat-widget__panel {
    position: fixed;
    top: auto;
    right: max(1.25rem, env(safe-area-inset-right));
    bottom: var(--chat-panel-bottom);
    width: min(24.5rem, calc(100vw - 2.5rem));
    height: min(44rem, calc(100vh - var(--chat-panel-top) - var(--chat-panel-bottom)));
    height: min(44rem, calc(100dvh - var(--chat-panel-top) - var(--chat-panel-bottom)));
    display: grid;
    grid-template-rows: auto 1fr auto;
    overflow: hidden;
    background: var(--chat-panel);
    border: 1px solid var(--chat-border);
    border-radius: 1.1rem;
    box-shadow: var(--chat-shadow);
    backdrop-filter: blur(18px);
    -webkit-backdrop-filter: blur(18px);
    opacity: 0;
    pointer-events: none;
    transform: translateY(1rem) scale(0.98);
    transform-origin: bottom right;
    transition: opacity 0.22s ease, transform 0.22s ease, visibility 0.22s ease;
    visibility: hidden;
}

.chat-widget[data-open="true"] .chat-widget__panel {
    opacity: 1;
    pointer-events: auto;
    transform: translateY(0) scale(1);
    visibility: visible;
}

.chat-widget__sheet-handle {
    display: none;
}

.chat-widget__header {
    display: grid;
    grid-template-columns: auto 1fr auto auto auto;
    gap: 0.65rem;
    align-items: center;
    padding: 1rem;
    border-bottom: 1px solid var(--chat-border);
    background: linear-gradient(180deg, rgba(255, 255, 255, 0.12), rgba(255, 255, 255, 0));
}

.chat-widget__avatar {
    position: relative;
    width: 2.5rem;
    height: 2.5rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 0.8rem;
    color: #ffffff;
    background: linear-gradient(135deg, var(--chat-accent), var(--chat-accent-alt));
    box-shadow: 0 12px 24px rgba(37, 99, 235, 0.24);
}

.chat-widget__identity {
    min-width: 0;
}

.chat-widget__identity h2 {
    margin: 0;
    font-size: 0.95rem;
    color: var(--chat-text);
}

.chat-widget__identity p {
    margin: 0.15rem 0 0;
    font-size: 0.72rem;
    line-height: 1.35;
    color: var(--chat-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-widget__online-dot {
    display: inline-block;
    width: 0.5rem;
    height: 0.5rem;
    margin-right: 0.3rem;
    border-radius: 50%;
    background: #22c55e;
    box-shadow: 0 0 10px rgba(34, 197, 94, 0.55);
}

.chat-widget__icon-button,
.chat-widget__icon-link {
    width: 2rem;
    height: 2rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 0.6rem;
    background: transparent;
    color: var(--chat-muted);
    transition: background-color 0.2s ease, color 0.2s ease;
}

.chat-widget__icon-button:hover,
.chat-widget__icon-link:hover {
    background: rgba(37, 99, 235, 0.1);
    color: var(--chat-accent);
}

.chat-widget__messages {
    overflow-y: auto;
    padding: 1rem;
    scroll-behavior: smooth;
    touch-action: pan-y;
    scrollbar-width: none;
    -ms-overflow-style: none;
}

.chat-widget__messages::-webkit-scrollbar {
    display: none;
}

.chat-widget__intro {
    padding: 0.85rem 0.95rem;
    border: 1px solid var(--chat-border);
    border-radius: 0.8rem;
    background: var(--chat-bot);
    color: var(--text-color);
    font-size: 0.85rem;
    line-height: 1.55;
}

.chat-widget__intro-title {
    margin: 0 0 0.35rem;
    color: var(--chat-text);
    font-weight: var(--font-semi-bold);
}

.chat-widget__chips {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.55rem;
    margin-top: 0.8rem;
}

.chat-widget__chip {
    min-height: 2.75rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.45rem;
    padding: 0.55rem 0.7rem;
    border: 1px solid var(--chat-border-strong);
    border-radius: 0.55rem;
    background: rgba(37, 99, 235, 0.06);
    color: var(--chat-accent);
    font-size: 0.72rem;
    font-weight: var(--font-semi-bold);
    line-height: 1.2;
}

.chat-widget__chip:hover {
    background: rgba(37, 99, 235, 0.12);
}

.chat-widget__chip:disabled {
    opacity: 0.55;
    cursor: not-allowed;
}

.chat-widget__message-row {
    display: flex;
    gap: 0.55rem;
    margin-top: 0.9rem;
    align-items: flex-end;
}

.chat-widget__message-row--user {
    justify-content: flex-end;
}

.chat-widget__message-row--assistant {
    justify-content: flex-start;
}

.chat-widget__mini-avatar {
    width: 1.8rem;
    height: 1.8rem;
    flex: 0 0 1.8rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 0.55rem;
    background: rgba(37, 99, 235, 0.1);
    color: var(--chat-accent);
    border: 1px solid var(--chat-border-strong);
}

.chat-widget__bubble {
    max-width: min(78%, 18rem);
    padding: 0.75rem 0.85rem;
    border-radius: 0.9rem;
    font-size: 0.82rem;
    line-height: 1.55;
    overflow-wrap: anywhere;
}

.chat-widget__message-row--assistant .chat-widget__bubble {
    max-width: calc(100% - 2.35rem);
    background: var(--chat-bot);
    border: 1px solid var(--chat-border);
    color: var(--text-color);
    border-bottom-left-radius: 0.3rem;
}

.chat-widget__message-row--user .chat-widget__bubble {
    background: var(--chat-user);
    color: #ffffff;
    border-bottom-right-radius: 0.3rem;
    box-shadow: 0 12px 28px rgba(37, 99, 235, 0.24);
}

.chat-widget__plain-text {
    white-space: pre-wrap;
}

.chat-widget__bubble a {
    color: inherit;
    text-decoration: underline;
    text-underline-offset: 0.15em;
}

.chat-widget__message-row--assistant .chat-widget__bubble a {
    color: var(--chat-accent);
}

.chat-widget__markdown {
    min-width: 0;
}

.chat-widget__markdown > :first-child {
    margin-top: 0;
}

.chat-widget__markdown > :last-child {
    margin-bottom: 0;
}

.chat-widget__markdown p,
.chat-widget__markdown ul,
.chat-widget__markdown ol,
.chat-widget__markdown blockquote,
.chat-widget__markdown pre,
.chat-widget__markdown table {
    margin: 0.55em 0;
}

.chat-widget__markdown h1,
.chat-widget__markdown h2,
.chat-widget__markdown h3,
.chat-widget__markdown h4 {
    margin: 0.8em 0 0.4em;
    color: var(--chat-text);
    font-weight: var(--font-semi-bold);
    line-height: 1.25;
}

.chat-widget__markdown h1 {
    font-size: 1.12em;
}

.chat-widget__markdown h2 {
    font-size: 1.06em;
}

.chat-widget__markdown h3,
.chat-widget__markdown h4 {
    font-size: 1em;
}

.chat-widget__markdown strong {
    color: var(--chat-text);
    font-weight: var(--font-semi-bold);
}

.chat-widget__markdown ul,
.chat-widget__markdown ol {
    padding-left: 1.15rem;
}

.chat-widget__markdown li {
    margin: 0.25em 0;
}

.chat-widget__markdown li::marker {
    color: var(--chat-accent);
}

.chat-widget__markdown blockquote {
    padding-left: 0.75rem;
    border-left: 3px solid var(--chat-accent);
    color: var(--chat-muted);
}

.chat-widget__markdown code {
    padding: 0.12rem 0.28rem;
    border: 1px solid var(--chat-code-inline-border);
    border-radius: 0.35rem;
    background: var(--chat-code-inline-bg);
    color: var(--chat-accent);
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
    font-size: 0.92em;
}

.chat-widget__markdown pre {
    max-width: 100%;
    overflow-x: auto;
    padding: 0.85rem;
    border: 1px solid var(--chat-border);
    border-radius: 0.7rem;
    background: var(--chat-code-block-bg);
    color: var(--chat-code-block-text);
    scrollbar-color: var(--chat-code-scroll-thumb) var(--chat-code-scroll-track);
    scrollbar-width: thin;
}

.chat-widget__markdown table::-webkit-scrollbar {
    display: none;
}

.chat-widget__markdown pre::-webkit-scrollbar {
    height: 0.42rem;
}

.chat-widget__markdown pre::-webkit-scrollbar-track {
    margin: 0 0.7rem;
    border-radius: 999px;
    background: var(--chat-code-scroll-track);
}

.chat-widget__markdown pre::-webkit-scrollbar-thumb {
    border-radius: 999px;
    background: var(--chat-code-scroll-thumb);
}

.chat-widget__markdown pre::-webkit-scrollbar-thumb:hover {
    background: var(--chat-accent);
}

.chat-widget__markdown pre code {
    display: block;
    min-width: max-content;
    padding: 0;
    border: 0;
    border-radius: 0;
    background: transparent;
    color: inherit;
    white-space: pre;
}

.chat-widget__markdown table {
    display: block;
    max-width: 100%;
    overflow-x: auto;
    border-collapse: collapse;
    font-size: 0.92em;
    scrollbar-width: none;
    -ms-overflow-style: none;
}

.chat-widget__markdown th,
.chat-widget__markdown td {
    padding: 0.45rem 0.6rem;
    border: 1px solid var(--chat-border);
    text-align: left;
    white-space: nowrap;
}

.chat-widget__markdown th {
    color: var(--chat-accent);
    background: rgba(37, 99, 235, 0.08);
    font-weight: var(--font-semi-bold);
}

.chat-widget__markdown hr {
    margin: 0.8rem 0;
    border: 0;
    border-top: 1px solid var(--chat-border);
}

.chat-widget__time {
    display: block;
    margin-top: 0.35rem;
    font-size: 0.65rem;
    color: currentColor;
    opacity: 0.62;
    text-align: right;
}

.chat-widget__typing {
    display: inline-flex;
    gap: 0.32rem;
    align-items: center;
    min-width: 3.4rem;
}

.chat-widget__typing span {
    width: 0.42rem;
    height: 0.42rem;
    border-radius: 50%;
    background: var(--chat-accent);
    animation: chatTyping 1s infinite ease-in-out;
}

.chat-widget__typing span:nth-child(2) {
    animation-delay: 0.15s;
}

.chat-widget__typing span:nth-child(3) {
    animation-delay: 0.3s;
}

.chat-widget__composer {
    padding: 0.85rem;
    border-top: 1px solid var(--chat-border);
    background: var(--chat-panel);
}

.chat-widget__input-label {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
}

.chat-widget__input-shell {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 0.65rem;
    align-items: end;
    padding: 0.5rem;
    border: 1px solid var(--chat-border);
    border-radius: 0.85rem;
    background: rgba(255, 255, 255, 0.72);
}

.dark-theme .chat-widget__input-shell {
    background: rgba(2, 6, 23, 0.28);
}

@media (prefers-color-scheme: dark) {
    :root:not(.light-theme) .chat-widget__input-shell {
        background: rgba(2, 6, 23, 0.28);
    }
}

.chat-widget__input-shell textarea {
    width: 100%;
    min-height: 2rem;
    max-height: 7.5rem;
    resize: none;
    overflow-y: auto;
    scrollbar-width: none;
    -ms-overflow-style: none;
    border: 0;
    outline: 0;
    background: transparent;
    color: var(--chat-text);
    line-height: 1.45;
    padding: 0.28rem 0;
}

.chat-widget__input-shell textarea::-webkit-scrollbar {
    display: none;
}

.chat-widget__input-shell textarea::placeholder {
    color: var(--chat-muted);
}

.chat-widget__send {
    width: 2.25rem;
    height: 2.25rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 0.7rem;
    background: var(--chat-accent);
    color: #ffffff;
    transition: background-color 0.2s ease, transform 0.2s ease, opacity 0.2s ease;
}

.chat-widget__send:hover {
    background: var(--chat-accent-alt);
    transform: translateY(-1px);
}

.chat-widget__send:disabled {
    opacity: 0.48;
    cursor: not-allowed;
    transform: none;
}

body.chat-widget-open {
    --chat-open: 1;
}

@keyframes chatTyping {
    0%, 80%, 100% {
        opacity: 0.35;
        transform: translateY(0);
    }
    40% {
        opacity: 1;
        transform: translateY(-0.2rem);
    }
}

@media screen and (min-width: 1180px) {
    .chat-widget__panel {
        width: 25.5rem;
    }
}

@media screen and (min-width: 768px) {
    :root {
        --chat-fab-size: 3.25rem;
    }

    .chat-widget__fab i {
        font-size: 1.28rem;
    }

    .chat-widget__status-dot {
        right: 0.24rem;
        bottom: 0.24rem;
        width: 0.7rem;
        height: 0.7rem;
    }

    .chat-widget__identity h2 {
        font-size: 0.88rem;
    }

    .chat-widget__identity p {
        font-size: 0.68rem;
    }

    .chat-widget__intro {
        font-size: 0.78rem;
        line-height: 1.5;
    }

    .chat-widget__chip {
        min-height: 2.55rem;
        font-size: 0.68rem;
    }

    .chat-widget__bubble {
        padding: 0.68rem 0.78rem;
        font-size: 0.76rem;
        line-height: 1.5;
    }

    .chat-widget__time {
        font-size: 0.6rem;
    }

    .chat-widget__input-shell textarea {
        font-size: 0.8rem;
    }
}

@media screen and (max-width: 767px) {
    :root {
        --chat-fab-size: 3.45rem;
        --floating-control-bottom: max(1rem, env(safe-area-inset-bottom));
        --floating-control-side: max(1rem, env(safe-area-inset-left));
    }

    body.chat-widget-open {
        overflow: hidden;
        touch-action: none;
    }

    .chat-widget {
        right: max(1rem, env(safe-area-inset-right));
        bottom: max(1rem, env(safe-area-inset-bottom));
    }

    .chat-widget__fab {
        right: max(1rem, env(safe-area-inset-right));
        bottom: max(1rem, env(safe-area-inset-bottom));
    }

    .chat-widget[data-open="true"] .chat-widget__fab {
        transform: scale(0.88);
        box-shadow: 0 14px 30px rgba(37, 99, 235, 0.34);
    }

    .chat-widget__panel {
        top: auto;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        height: var(--chat-mobile-sheet-height, min(78dvh, calc(100dvh - 5.25rem)));
        max-height: calc(100dvh - 0.75rem);
        min-height: min(28rem, calc(100dvh - 5.25rem));
        border-radius: 1.25rem 1.25rem 0 0;
        transform: translateY(100%);
        transform-origin: bottom center;
    }

    .chat-widget[data-open="true"] .chat-widget__panel {
        transform: translateY(0);
    }

    .chat-widget__sheet-handle {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100%;
        height: 2rem;
        margin: 0;
        cursor: grab;
        touch-action: none;
    }

    .chat-widget__sheet-handle::before {
        content: "";
        display: block;
        width: 2.8rem;
        height: 0.28rem;
        border-radius: 999px;
        background: rgba(100, 116, 139, 0.35);
    }

    .chat-widget--resizing .chat-widget__sheet-handle {
        cursor: grabbing;
    }

    .chat-widget__header {
        grid-template-columns: auto 1fr auto auto auto;
        padding: 0.25rem 1rem 0.85rem;
    }

    .chat-widget__identity h2 {
        font-size: 0.92rem;
    }

    .chat-widget__identity p {
        max-width: 11rem;
    }

    .chat-widget__messages {
        padding: 0.85rem;
    }

    .chat-widget__chips {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .chat-widget__chip {
        min-height: 3rem;
        font-size: 0.72rem;
    }

    .chat-widget__bubble {
        max-width: 82%;
        font-size: 0.83rem;
    }

    .chat-widget__composer {
        padding: 0.75rem 0.75rem calc(0.75rem + env(safe-area-inset-bottom));
    }

    .chat-widget[data-open="true"] .chat-widget__composer {
        padding-right: calc(4.25rem + env(safe-area-inset-right));
    }
}

@media screen and (max-width: 390px) {
    .chat-widget__chips {
        grid-template-columns: 1fr;
    }

    .chat-widget__identity p {
        max-width: 8.5rem;
    }
}
