/* ============================================================
   Floating AI chat
   A quiet, editorial assistant that sits above the page.
   ============================================================ */

.sr-only {
    position: absolute !important;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* ------------------------------------------------------------
   Launcher
   ------------------------------------------------------------ */
.chat-launcher {
    position: fixed;
    right: max(24px, env(safe-area-inset-right));
    bottom: max(24px, env(safe-area-inset-bottom));
    z-index: 420;
    display: inline-flex;
    align-items: center;
    gap: 10px;
    height: 56px;
    padding: 0 20px 0 13px;
    border: 1px solid var(--accent);
    border-radius: 999px;
    background-color: var(--accent);
    color: var(--on-accent);
    font-family: var(--font-sans);
    font-size: 0.8125rem;
    font-weight: 500;
    letter-spacing: 0.01em;
    cursor: pointer;
    box-shadow:
        0 18px 40px -14px rgba(24, 24, 22, 0.4),
        0 2px 6px rgba(24, 24, 22, 0.1);
    transition:
        background-color 0.25s var(--ease),
        transform 0.25s var(--ease),
        box-shadow 0.25s var(--ease);
}

.chat-launcher:hover {
    background-color: var(--accent-hover);
    border-color: var(--accent-hover);
    transform: translateY(-2px);
    box-shadow:
        0 24px 46px -16px rgba(24, 24, 22, 0.45),
        0 3px 8px rgba(24, 24, 22, 0.12);
}

.chat-launcher:focus-visible {
    outline: 2px solid var(--ink);
    outline-offset: 4px;
}

.chat-launcher__icon {
    display: grid;
    place-items: center;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background-color: rgba(255, 255, 255, 0.16);
}

.chat-launcher__label {
    white-space: nowrap;
}

.chat-launcher__dot {
    position: relative;
    width: 6px;
    height: 6px;
    margin-left: 2px;
    border-radius: 50%;
    background-color: currentColor;
}

.chat-launcher__dot::after {
    content: "";
    position: absolute;
    inset: 0;
    border-radius: 50%;
    background-color: currentColor;
    opacity: 0.45;
    animation: chat-ping 2.8s var(--ease) infinite;
}

@keyframes chat-ping {
    0% { transform: scale(1); opacity: 0.45; }
    70%, 100% { transform: scale(2.8); opacity: 0; }
}

/* ------------------------------------------------------------
   Panel
   ------------------------------------------------------------ */
.chat-panel {
    position: fixed;
    right: max(24px, env(safe-area-inset-right));
    bottom: calc(max(24px, env(safe-area-inset-bottom)) + 68px);
    z-index: 421;
    display: flex;
    flex-direction: column;
    width: min(392px, calc(100vw - 48px));
    height: min(590px, calc(100dvh - 118px));
    overflow: hidden;
    border: 1px solid var(--line-strong);
    border-radius: 8px;
    background-color: var(--surface);
    box-shadow:
        0 34px 80px -28px rgba(24, 24, 22, 0.38),
        0 4px 14px rgba(24, 24, 22, 0.08);
    opacity: 0;
    visibility: hidden;
    transform: translateY(12px) scale(0.985);
    transform-origin: 100% 100%;
    pointer-events: none;
    transition:
        opacity 0.24s var(--ease),
        transform 0.24s var(--ease),
        visibility 0s linear 0.24s;
}

.chat-panel.is-open {
    opacity: 1;
    visibility: visible;
    transform: translateY(0) scale(1);
    pointer-events: auto;
    transition:
        opacity 0.3s var(--ease-out),
        transform 0.3s var(--ease-out),
        visibility 0s linear 0s;
}

.chat__header {
    display: flex;
    align-items: center;
    gap: 11px;
    padding: 13px 12px 13px 15px;
    border-bottom: 1px solid var(--line);
    background-color: var(--surface);
}

.chat__avatar {
    flex: none;
    width: 38px;
    height: 38px;
    overflow: hidden;
    border: 1px solid var(--line-strong);
    border-radius: 50%;
    background-color: var(--tint);
}

.chat__avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: 50% 30%;
}

.chat__identity {
    flex: 1;
    min-width: 0;
}

.chat__title {
    font-size: 0.875rem;
    font-weight: 600;
    line-height: 1.3;
    letter-spacing: -0.01em;
    color: var(--ink);
}

.chat__subtitle {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 2px;
    font-size: 0.625rem;
    font-weight: 500;
    line-height: 1.4;
    letter-spacing: 0.09em;
    text-transform: uppercase;
    color: var(--ink-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat__status-dot {
    flex: none;
    width: 5px;
    height: 5px;
    border-radius: 50%;
    background-color: var(--success);
}

.chat__close {
    flex: none;
    display: grid;
    place-items: center;
    width: 34px;
    height: 34px;
    border: 1px solid var(--line);
    border-radius: 50%;
    background-color: transparent;
    color: var(--ink-secondary);
    cursor: pointer;
    transition:
        background-color 0.2s var(--ease),
        border-color 0.2s var(--ease),
        color 0.2s var(--ease);
}

.chat__close:hover {
    border-color: var(--line-strong);
    background-color: var(--tint);
    color: var(--ink);
}

.chat__close:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.chat__disclosure {
    padding: 8px 15px;
    border-bottom: 1px solid var(--line);
    background-color: var(--tint);
    color: var(--ink-muted);
    font-size: 0.625rem;
    line-height: 1.5;
}

.chat.is-busy .chat__status-dot {
    background-color: var(--accent);
    animation: chat-blink 1.1s ease-in-out infinite;
}

@keyframes chat-blink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.25; }
}

/* ------------------------------------------------------------
   Conversation
   ------------------------------------------------------------ */
.chat__messages {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 16px 15px 14px;
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-width: thin;
    scrollbar-color: var(--line-strong) transparent;
}

.chat__messages::-webkit-scrollbar {
    width: 6px;
}

.chat__messages::-webkit-scrollbar-thumb {
    border-radius: 999px;
    background-color: var(--line-strong);
}

.chat-msg {
    display: flex;
    max-width: 90%;
}

.chat-msg--bot {
    align-self: flex-start;
}

.chat-msg--user {
    align-self: flex-end;
    justify-content: flex-end;
}

.chat-msg__bubble {
    padding: 9px 12px;
    font-size: 0.8125rem;
    line-height: 1.55;
    overflow-wrap: anywhere;
    white-space: pre-wrap;
    text-wrap: pretty;
}

.chat-msg--bot .chat-msg__bubble {
    border: 1px solid var(--line);
    border-radius: 3px 12px 12px 12px;
    background-color: var(--tint);
    color: var(--ink);
}

.chat-msg--user .chat-msg__bubble {
    border: 1px solid transparent;
    border-radius: 12px 12px 3px 12px;
    background-color: var(--ink);
    color: var(--paper);
}

.chat-msg__bubble a {
    color: var(--accent);
    text-decoration: underline;
    text-decoration-thickness: 1px;
    text-underline-offset: 2px;
    transition: color 0.2s var(--ease);
}

.chat-msg__bubble a:hover {
    color: var(--accent-hover);
}

.chat-msg__bubble a:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
    border-radius: 1px;
}

.chat-msg--user .chat-msg__bubble a {
    color: inherit;
    text-decoration-color: currentColor;
}

.chat-msg--user .chat-msg__bubble a:hover {
    color: inherit;
}

.chat-msg--user .chat-msg__bubble a:focus-visible {
    outline-color: var(--paper);
}

.chat-msg--notice {
    align-self: flex-start;
}

.chat-msg--notice .chat-msg__bubble {
    border: 1px solid var(--line-strong);
    border-left: 2px solid var(--error);
    border-radius: 3px 12px 12px 12px;
    background-color: var(--surface);
    color: var(--ink-secondary);
}

.chat-msg--typing .chat-msg__bubble {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 13px 14px;
}

.chat-typing__dot {
    width: 5px;
    height: 5px;
    border-radius: 50%;
    background-color: var(--ink-muted);
    animation: chat-typing 1.2s ease-in-out infinite;
}

.chat-typing__dot:nth-child(2) { animation-delay: 0.15s; }
.chat-typing__dot:nth-child(3) { animation-delay: 0.3s; }

@keyframes chat-typing {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.35; }
    30% { transform: translateY(-3px); opacity: 1; }
}

/* ------------------------------------------------------------
   Suggestions
   ------------------------------------------------------------ */
.chat__suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    width: 100%;
    padding: 2px 0 0;
}

.chat__suggestions[hidden] {
    display: none;
}

.chat__suggestions button {
    padding: 6px 11px;
    border: 1px solid var(--line-strong);
    border-radius: 999px;
    background-color: transparent;
    color: var(--ink-secondary);
    font-family: var(--font-sans);
    font-size: 0.6875rem;
    line-height: 1.4;
    cursor: pointer;
    transition:
        border-color 0.2s var(--ease),
        background-color 0.2s var(--ease),
        color 0.2s var(--ease);
}

.chat__suggestions button:hover {
    border-color: var(--ink);
    background-color: var(--surface);
    color: var(--ink);
}

.chat__suggestions button:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* ------------------------------------------------------------
   Composer
   ------------------------------------------------------------ */
.chat__form {
    padding: 10px 12px 11px;
    border-top: 1px solid var(--line);
    background-color: var(--surface);
}

.chat__input-row {
    display: flex;
    align-items: flex-end;
    gap: 8px;
}

.chat__input {
    flex: 1;
    min-height: 42px;
    max-height: 116px;
    padding: 10px 12px;
    border: 1px solid var(--line-strong);
    border-radius: 5px;
    background-color: var(--paper);
    color: var(--ink);
    font-family: var(--font-sans);
    font-size: 0.8125rem;
    line-height: 1.5;
    resize: none;
    overflow-y: auto;
    transition:
        border-color 0.2s var(--ease),
        background-color 0.2s var(--ease);
}

.chat__input::placeholder {
    color: var(--ink-muted);
    opacity: 1;
}

.chat__input:focus {
    border-color: var(--ink);
    outline: none;
}

.chat__input:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
}

.chat__send {
    flex: none;
    display: grid;
    place-items: center;
    width: 42px;
    height: 42px;
    border: 1px solid var(--accent);
    border-radius: 5px;
    background-color: var(--accent);
    color: var(--on-accent);
    cursor: pointer;
    transition:
        background-color 0.2s var(--ease),
        border-color 0.2s var(--ease),
        opacity 0.2s var(--ease);
}

.chat__send:hover:not(:disabled) {
    background-color: var(--accent-hover);
    border-color: var(--accent-hover);
}

.chat__send:focus-visible {
    outline: 2px solid var(--ink);
    outline-offset: 2px;
}

.chat__send:disabled {
    opacity: 0.38;
    cursor: not-allowed;
}

.chat__foot {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 10px;
    padding-top: 7px;
}

.chat__note,
.chat__cooldown {
    font-size: 0.5625rem;
    line-height: 1.5;
    letter-spacing: 0.04em;
    color: var(--ink-muted);
}

.chat__cooldown {
    flex: none;
    font-variant-numeric: tabular-nums;
    letter-spacing: 0.06em;
    text-transform: uppercase;
}

.chat__cooldown.is-waiting {
    color: var(--ink-secondary);
}

/* ------------------------------------------------------------
   Dark theme
   ------------------------------------------------------------ */
[data-theme="dark"] .chat-launcher {
    box-shadow:
        0 18px 40px -14px rgba(0, 0, 0, 0.7),
        0 2px 6px rgba(0, 0, 0, 0.35);
}

[data-theme="dark"] .chat-panel {
    box-shadow:
        0 34px 80px -28px rgba(0, 0, 0, 0.75),
        0 4px 14px rgba(0, 0, 0, 0.4);
}

[data-theme="dark"] .chat-msg--user .chat-msg__bubble {
    background-color: var(--ink);
    color: var(--paper);
}

[data-theme="dark"] .chat__input {
    background-color: var(--tint);
}

/* ------------------------------------------------------------
   Responsive
   ------------------------------------------------------------ */
/* Keep the floating chat out of the way while the mobile menu is open. */
body.menu-open .chat-launcher,
body.menu-open .chat-panel {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}

@media (max-width: 540px) {
    .chat-launcher {
        right: max(14px, env(safe-area-inset-right));
        bottom: max(14px, env(safe-area-inset-bottom));
        height: 50px;
        padding: 0 16px 0 10px;
        font-size: 0.75rem;
        gap: 8px;
    }

    .chat-launcher__icon {
        width: 30px;
        height: 30px;
    }

    .chat-panel {
        left: max(12px, env(safe-area-inset-left));
        right: max(12px, env(safe-area-inset-right));
        bottom: calc(max(14px, env(safe-area-inset-bottom)) + 60px);
        width: auto;
        height: min(620px, calc(100dvh - 86px));
        max-height: calc(100dvh - 86px);
        border-radius: 7px;
    }

    .chat__header {
        padding: 11px 10px 11px 13px;
    }

    .chat__disclosure {
        padding: 7px 13px;
        font-size: 0.59375rem;
    }

    .chat__messages {
        padding: 14px 13px 12px;
    }

    .chat__suggestions {
        padding: 2px 0 0;
    }

    .chat__form {
        padding: 9px 10px 10px;
    }

    .chat-msg {
        max-width: 92%;
    }

    .chat-msg__bubble,
    .chat__input {
        font-size: 0.8125rem;
    }
}

@media (max-width: 380px) {
    .chat-launcher__label {
        font-size: 0.6875rem;
    }

    .chat__subtitle {
        font-size: 0.5625rem;
        letter-spacing: 0.06em;
        white-space: normal;
        overflow: visible;
        text-overflow: clip;
        align-items: flex-start;
    }

    .chat__subtitle .chat__status-dot {
        margin-top: 0.35em;
    }

    .chat__disclosure {
        font-size: 0.5625rem;
    }

    .chat__note {
        letter-spacing: 0.02em;
    }
}

@media (max-height: 520px) {
    .chat-panel {
        top: 12px;
        bottom: 12px;
        height: auto;
        max-height: none;
    }

    .chat.is-open .chat-launcher {
        opacity: 0;
        pointer-events: none;
    }
}

/* ------------------------------------------------------------
   Reduced motion
   ------------------------------------------------------------ */
@media (prefers-reduced-motion: reduce) {
    .chat-launcher,
    .chat-panel,
    .chat__close,
    .chat__suggestions button,
    .chat__input,
    .chat__send {
        transition: none;
    }

    .chat-launcher:hover {
        transform: none;
    }

    .chat-launcher__dot::after,
    .chat.is-busy .chat__status-dot,
    .chat-typing__dot {
        animation: none;
    }

    .chat-typing__dot {
        opacity: 0.6;
    }
}
