/* ==========================================================================
   Chat widget (SPIKE): www/js/chat_widget.js, included by
   backend/chat_widget.php on public pages only.
   Uses the tokens from main.css (colours, fonts, radii, shadows, --tap).
   ========================================================================== */

.chatw {
    --chatw-bottom: 16px;   /* set by the script to clear the map's controls */
    --chatw-gap: 16px;
    --chatw-size: 3.5rem;

    font-family: var(--font-body);
    color: var(--gray-900);
}

.chatw__sr {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    clip-path: inset(50%);
    white-space: nowrap;
}


/* ---- bubble ------------------------------------------------------------ */

.chatw__bubble {
    position: fixed;
    right: calc(var(--chatw-gap) + env(safe-area-inset-right));
    bottom: calc(var(--chatw-bottom) + env(safe-area-inset-bottom));
    z-index: 1100;   /* above Leaflet (1000) and the results sheet (900) */
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: var(--chatw-size);
    height: var(--chatw-size);
    border-radius: 50%;
    background: var(--teal);
    color: #fff;
    box-shadow: var(--shadow-lg);
    transition: bottom var(--transition), opacity var(--transition), transform var(--transition);
    -webkit-tap-highlight-color: transparent;
}

.chatw__bubble:active {
    transform: scale(0.96);
}

.chatw__bubble:focus-visible {
    outline: 3px solid var(--navy);
    outline-offset: 3px;
}

/* The results sheet is pulled up over most of the screen: step aside. */
.chatw--tucked .chatw__bubble {
    opacity: 0;
    pointer-events: none;
    transform: scale(0.6);
}


/* ---- panel ------------------------------------------------------------- */

.chatw__panel {
    position: fixed;
    right: calc(var(--chatw-gap) + env(safe-area-inset-right));
    bottom: calc(var(--chatw-gap) + env(safe-area-inset-bottom));
    z-index: 1100;
    display: flex;
    flex-direction: column;
    width: min(24rem, calc(100vw - 2 * var(--chatw-gap)));
    height: min(38rem, calc(100vh - 2 * var(--chatw-gap)));
    height: min(38rem, calc(100dvh - 2 * var(--chatw-gap)));
    overflow: hidden;
    border: 1px solid var(--gray-300);
    border-radius: var(--radius-xl);
    background: #fff;
    box-shadow: var(--shadow-lg);
}

.chatw__panel[hidden] {
    display: none;
}

.chatw__header {
    display: flex;
    flex: 0 0 auto;
    align-items: center;
    gap: var(--space-2);
    min-height: 3.5rem;
    padding: var(--space-2) var(--space-2) var(--space-2) var(--space-4);
    background: var(--teal);
    color: #fff;
}

.chatw__title {
    flex: 1 1 auto;
    min-width: 0;
    font-family: var(--font-head);
    font-size: var(--text-lg);
    font-weight: 700;
    line-height: 1.2;
}

.chatw__clear {
    min-height: var(--tap);
    padding: 0 var(--space-3);
    border-radius: var(--radius);
    color: #fff;
    font-size: var(--text-sm);
    font-weight: 600;
    text-decoration: underline;
    text-underline-offset: 3px;
}

.chatw__close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: var(--tap);
    height: var(--tap);
    border-radius: var(--radius);
    color: #fff;
    font-size: 1.75rem;
    line-height: 1;
}

.chatw__clear:focus-visible,
.chatw__close:focus-visible {
    outline-color: #fff;
    outline-offset: -3px;
}


/* ---- conversation ------------------------------------------------------ */

.chatw__log {
    display: flex;
    flex: 1 1 auto;
    flex-direction: column;
    gap: var(--space-3);
    min-height: 0;
    padding: var(--space-4);
    overflow-y: auto;
    overscroll-behavior: contain;
    background: var(--gray-100);
}

.chatw__log:focus-visible {
    outline-offset: -3px;
}

.chatw__messages {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
}

.chatw__msg {
    max-width: 88%;
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-lg);
    font-size: var(--text-md);
    line-height: 1.45;
    overflow-wrap: anywhere;
}

.chatw__msg--bot {
    align-self: flex-start;
    border: 1px solid var(--gray-200);
    border-bottom-left-radius: var(--space-1);
    background: #fff;
}

.chatw__msg--user {
    align-self: flex-end;
    border-bottom-right-radius: var(--space-1);
    background: var(--teal);
    color: #fff;
    white-space: pre-wrap;
}

.chatw__msg--error {
    align-self: flex-start;
    border: 1px solid var(--crimson);
    background: var(--crimson-lt);
    color: var(--crimson-dark);
}

.chatw__msg p + p,
.chatw__msg p + .chatw__list,
.chatw__msg .chatw__list + p {
    margin-top: var(--space-2);
}

.chatw__list {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    padding-left: 1.1rem;
    list-style: disc;
}

.chatw__list li::marker {
    color: var(--teal);
}

.chatw__link {
    color: var(--teal-dark);
    font-weight: 600;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.chatw__link:hover {
    color: var(--navy);
}


/* ---- presets (same look as the map's filter chips) --------------------- */

.chatw__presets {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
}

.chatw__preset {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: var(--tap);
    padding: 0 var(--space-4);
    border: 1px solid var(--gray-400);
    border-radius: var(--radius-pill);
    background: #fff;
    color: var(--gray-900);
    font-family: var(--font-body);
    font-size: var(--text-md);
    font-weight: 600;
    line-height: 1.2;
    -webkit-tap-highlight-color: transparent;
}

.chatw__preset:active {
    border-color: var(--teal);
    background: var(--teal-light);
}

.chatw__preset:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}


/* ---- ZIP prompt -------------------------------------------------------- */

.chatw__zip {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    padding: var(--space-3);
    border: 1px solid var(--gray-300);
    border-radius: var(--radius-lg);
    background: #fff;
}

.chatw__zip[hidden] {
    display: none;
}

.chatw__zip-label {
    font-family: var(--font-head);
    font-size: var(--text-sm);
    font-weight: 700;
}

.chatw__zip-row {
    display: flex;
    gap: var(--space-2);
}

.chatw__zip-input {
    flex: 1 1 auto;
    width: 6rem;
    min-width: 0;
    min-height: var(--tap);
    padding: 0 var(--space-3);
    border: 1px solid var(--gray-400);
    border-radius: var(--radius);
    font: inherit;
    font-size: var(--text-base);
    letter-spacing: 0.05em;
}

.chatw__zip-input[aria-invalid="true"] {
    border-color: var(--crimson);
}

.chatw__zip-go,
.chatw__zip-cancel {
    min-height: var(--tap);
    padding: 0 var(--space-4);
    border-radius: var(--radius);
    font-weight: 600;
}

.chatw__zip-go {
    background: var(--crimson);
    color: #fff;
}

.chatw__zip-go:disabled {
    opacity: 0.6;
}

.chatw__zip-cancel {
    color: var(--gray-700);
}

.chatw__zip-error {
    color: var(--crimson-dark);
    font-size: var(--text-sm);
}


/* ---- typing indicator -------------------------------------------------- */

.chatw__typing {
    display: inline-flex;
    gap: 0.3rem;
    align-items: center;
    width: max-content;
}

.chatw__typing[hidden] {
    display: none;
}

.chatw__dot {
    width: 0.5rem;
    height: 0.5rem;
    border-radius: 50%;
    background: var(--gray-400);
    animation: chatw-blink 1.2s infinite ease-in-out both;
}

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

@keyframes chatw-blink {
    0%, 80%, 100% { opacity: 0.3; transform: translateY(0); }
    40%           { opacity: 1;   transform: translateY(-3px); }
}


/* ---- input ------------------------------------------------------------- */

.chatw__form {
    display: flex;
    flex: 0 0 auto;
    align-items: flex-end;
    gap: var(--space-2);
    padding: var(--space-3) var(--space-3) var(--space-1);
    border-top: 1px solid var(--gray-200);
    background: #fff;
}

.chatw__input {
    flex: 1 1 auto;
    min-height: var(--tap);
    max-height: 7.5rem;
    padding: 0.6rem var(--space-3);
    border: 1px solid var(--gray-400);
    border-radius: var(--radius-lg);
    font: inherit;
    font-size: var(--text-base);   /* 16px: no zoom-on-focus on iOS */
    line-height: 1.4;
    resize: none;
}

.chatw__input:focus-visible,
.chatw__zip-input:focus-visible {
    border-color: var(--teal);
    outline: 2px solid var(--teal);
    outline-offset: 0;
}

.chatw__send {
    display: inline-flex;
    flex: 0 0 auto;
    align-items: center;
    justify-content: center;
    width: var(--tap);
    height: var(--tap);
    border-radius: var(--radius-lg);
    background: var(--crimson);
    color: #fff;
}

.chatw__send:disabled {
    opacity: 0.5;
    cursor: wait;
}

.chatw__foot {
    display: flex;
    flex: 0 0 auto;
    align-items: baseline;
    justify-content: space-between;
    gap: var(--space-3);
    padding: 0 var(--space-4) var(--space-3);
    background: #fff;
}

.chatw__note,
.chatw__count {
    color: var(--gray-500);
    font-size: 0.8125rem;
    line-height: 1.35;
}

.chatw__count {
    flex: 0 0 auto;
    font-variant-numeric: tabular-nums;
}


/* ---- hover (pointer devices only, like map.css) ------------------------- */

@media (hover: hover) {
    .chatw__bubble:hover { background: var(--teal-dark); }
    .chatw__clear:hover,
    .chatw__close:hover { background: rgba(255, 255, 255, 0.14); }
    .chatw__preset:hover:not(:disabled) { border-color: var(--teal); background: var(--teal-xlt); }
    .chatw__zip-go:hover:not(:disabled),
    .chatw__send:hover:not(:disabled) { background: var(--crimson-dark); }
}


/* ---- phones: full screen ----------------------------------------------- */

@media (max-width: 639.98px) {
    .chatw__panel {
        inset: 0;
        width: 100%;
        height: 100%;
        border: 0;
        border-radius: 0;
        padding-bottom: env(safe-area-inset-bottom);
    }

    /* The teal header runs up behind the notch. */
    .chatw__header {
        padding-top: calc(var(--space-2) + env(safe-area-inset-top));
    }

    .chatw__msg {
        max-width: 92%;
    }

    /* The page behind a full-screen chat must not scroll. */
    html.chatw-open,
    html.chatw-open body {
        overflow: hidden;
    }
}


@media (prefers-reduced-motion: reduce) {
    .chatw__bubble { transition: none; }
    .chatw__dot { animation: none; opacity: 0.6; }
}
