/* ============================================================
   TerraBlaze Adventure — site assistant

   Everything here is namespaced under .tb-chat* so it can drop
   onto any page without touching the rest of the cascade. It
   reuses the site's tokens (--primary, --accent, the shadow and
   easing scale) rather than introducing a second palette, with
   literal fallbacks so the widget still looks right if it is
   ever loaded ahead of styles.css.
   ============================================================ */

.tb-chat {
    --tbc-navy: var(--primary, #17140f);
    --tbc-navy-dark: var(--primary-dark, #0c0a07);
    --tbc-navy-light: var(--primary-light, #3f4a37);
    --tbc-gold: var(--accent, #b5502f);
    --tbc-gold-soft: var(--accent-soft, #e8d2c2);
    --tbc-surface: var(--bg-white, #ffffff);
    --tbc-canvas: var(--bg-light, #f7f5f0);
    --tbc-ink: var(--text-dark, #242220);
    --tbc-muted: var(--text-muted, #6f6a62);
    --tbc-line: var(--border-color, #e7e2d8);
    --tbc-ease: var(--ease-premium, cubic-bezier(0.22, 1, 0.36, 1));
    --tbc-radius: var(--border-radius-lg, 20px);

    position: fixed;
    right: 1.5rem;
    bottom: 1.5rem;
    z-index: 1400;
    font-family: var(--font-primary, 'Archivo', sans-serif);
    line-height: 1.55;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 0.75rem;
    /* The bar itself must not block clicks on the page behind it. */
    pointer-events: none;
}

.tb-chat > * {
    pointer-events: auto;
}

/* ─── Launcher ───────────────────────────────────────────────── */

.tb-chat-launcher {
    position: relative;
    width: 62px;
    height: 62px;
    flex: 0 0 auto;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    padding: 0;
    background: var(--tbc-navy);
    color: #fff;
    display: grid;
    place-items: center;
    box-shadow: none;
    transition: transform 0.35s var(--tbc-ease), box-shadow 0.35s var(--tbc-ease);
    -webkit-tap-highlight-color: transparent;
}

/* Gold hairline ring, echoing the accent border used on premium cards. */
.tb-chat-launcher::after {
    content: '';
    position: absolute;
    inset: -3px;
    border-radius: 50%;
    border: 1px solid rgba(181, 80, 47, 0.55);
    pointer-events: none;
}

.tb-chat-launcher:hover,
.tb-chat-launcher:focus-visible {
    transform: translateY(-3px) scale(1.04);
    box-shadow: 0 0 0 4px rgba(181, 80, 47, 0.18);
}

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

.tb-chat-launcher:active {
    transform: translateY(-1px) scale(0.98);
}

.tb-chat-launcher-icon {
    width: 27px;
    height: 27px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.7;
    stroke-linecap: round;
    stroke-linejoin: round;
    transition: opacity 0.25s var(--tbc-ease), transform 0.35s var(--tbc-ease);
}

.tb-chat-launcher-icon--close {
    position: absolute;
    opacity: 0;
    transform: rotate(-45deg) scale(0.7);
}

.tb-chat.is-open .tb-chat-launcher-icon--chat {
    opacity: 0;
    transform: rotate(45deg) scale(0.7);
}

.tb-chat.is-open .tb-chat-launcher-icon--close {
    opacity: 1;
    transform: none;
}

/* Slow gold halo, so the button reads as live without nagging. */
.tb-chat-launcher-pulse {
    position: absolute;
    inset: 0;
    border-radius: 50%;
    border: 2px solid rgba(181, 80, 47, 0.5);
    animation: tbChatPulse 2.8s var(--tbc-ease) infinite;
    pointer-events: none;
}

.tb-chat.is-open .tb-chat-launcher-pulse,
.tb-chat.has-opened .tb-chat-launcher-pulse {
    display: none;
}

@keyframes tbChatPulse {
    0% { transform: scale(1); opacity: 0.75; }
    70% { transform: scale(1.45); opacity: 0; }
    100% { transform: scale(1.45); opacity: 0; }
}

/* Unread dot on the launcher while the teaser is waiting. */
.tb-chat-badge {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 13px;
    height: 13px;
    border-radius: 50%;
    background: var(--tbc-gold);
    border: 2px solid #fff;
    opacity: 0;
    transform: scale(0.4);
    transition: opacity 0.3s var(--tbc-ease), transform 0.3s var(--tbc-ease);
}

.tb-chat.has-teaser .tb-chat-badge {
    opacity: 1;
    transform: none;
}

/* ─── Teaser bubble ──────────────────────────────────────────── */

/* The bubble is a plain box holding two buttons — open and dismiss. A button
   nested inside another button is not valid, and screen readers handle the
   pair far better than one control with a second hidden inside it. */
.tb-chat-teaser {
    position: relative;
    max-width: min(19rem, calc(100vw - 3.5rem));
    background: var(--tbc-surface);
    border: 1px solid var(--tbc-line);
    border-radius: 2px;
    box-shadow: var(--shadow-premium, 0 18px 36px -14px rgba(23, 20, 15, 0.18));
    color: var(--tbc-ink);
    opacity: 0;
    visibility: hidden;
    transform: translateY(12px) scale(0.94);
    transform-origin: bottom right;
    transition: opacity 0.45s var(--tbc-ease), transform 0.45s var(--tbc-ease), visibility 0.45s;
}

.tb-chat-teaser-open {
    display: flex;
    width: 100%;
    align-items: flex-start;
    gap: 0.7rem;
    padding: 0.85rem 2.1rem 0.85rem 0.9rem;
    border: none;
    border-radius: inherit;
    background: none;
    color: inherit;
    font: inherit;
    text-align: left;
    cursor: pointer;
}

.tb-chat-teaser-open:focus-visible {
    outline: 2px solid var(--tbc-gold);
    outline-offset: -2px;
}

.tb-chat.has-teaser .tb-chat-teaser {
    opacity: 1;
    visibility: visible;
    transform: none;
}

.tb-chat-teaser-avatar {
    flex: 0 0 auto;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    display: grid;
    place-items: center;
    background: var(--tbc-gold);
    color: #fff;
}

.tb-chat-teaser-avatar svg {
    width: 19px;
    height: 19px;
    fill: currentColor;
}

.tb-chat-teaser-text {
    font-size: 0.9rem;
    font-weight: 500;
}

.tb-chat-teaser-text small {
    display: block;
    font-size: 0.76rem;
    font-weight: 400;
    color: var(--tbc-muted);
    margin-top: 0.1rem;
}

.tb-chat-teaser-close {
    position: absolute;
    top: 5px;
    right: 5px;
    width: 24px;
    height: 24px;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: var(--tbc-muted);
    cursor: pointer;
    display: grid;
    place-items: center;
    font-size: 1.05rem;
    line-height: 1;
    transition: background var(--transition-fast, 0.2s), color var(--transition-fast, 0.2s);
}

.tb-chat-teaser-close:hover {
    background: var(--tbc-canvas);
    color: var(--tbc-ink);
}

/* ─── Panel ──────────────────────────────────────────────────── */

.tb-chat-panel {
    position: absolute;
    right: 0;
    bottom: calc(62px + 0.9rem);
    width: min(23.75rem, calc(100vw - 3rem));
    height: min(37.5rem, calc(100vh - 8rem));
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--tbc-canvas);
    border: 1px solid var(--tbc-line);
    border-radius: var(--tbc-radius);
    box-shadow: none;
    opacity: 0;
    /* visibility, not [hidden]: an author `display: flex` beats the UA's
       `[hidden] { display: none }`, so the attribute would do nothing here.
       visibility:hidden still drops the panel out of the tab order and the
       accessibility tree, and unlike display it animates. */
    visibility: hidden;
    pointer-events: none;
    transform: translateY(16px) scale(0.96);
    transform-origin: bottom right;
    transition: opacity 0.35s var(--tbc-ease), transform 0.35s var(--tbc-ease), visibility 0.35s;
}

.tb-chat.is-open .tb-chat-panel {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
    transform: none;
}

/* Header */

.tb-chat-head {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.95rem 1rem;
    background: var(--tbc-navy);
    color: #fff;
    border-bottom: 1px solid rgba(181, 80, 47, 0.45);
}

.tb-chat-head-avatar {
    flex: 0 0 auto;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: grid;
    place-items: center;
    background: var(--tbc-gold);
    box-shadow: none;
}

.tb-chat-head-avatar svg {
    width: 22px;
    height: 22px;
    fill: #fff;
}

.tb-chat-head-id {
    flex: 1 1 auto;
    min-width: 0;
}

.tb-chat-head-title {
    font-size: 0.95rem;
    font-weight: 600;
    letter-spacing: var(--track-snug, -0.01em);
}

.tb-chat-head-status {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    font-size: 0.74rem;
    color: rgba(255, 255, 255, 0.72);
}

.tb-chat-head-status::before {
    content: '';
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #59c07f;
    box-shadow: 0 0 0 3px rgba(89, 192, 127, 0.22);
}

.tb-chat-head-btn {
    flex: 0 0 auto;
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.12);
    color: #fff;
    cursor: pointer;
    display: grid;
    place-items: center;
    transition: background var(--transition-fast, 0.2s), transform var(--transition-fast, 0.2s);
}

.tb-chat-head-btn:hover {
    background: rgba(255, 255, 255, 0.24);
    transform: rotate(90deg);
}

.tb-chat-head-btn svg {
    width: 15px;
    height: 15px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
}

/* Message log */

.tb-chat-log {
    flex: 1 1 auto;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 1.1rem 1rem 0.5rem;
    display: flex;
    flex-direction: column;
    gap: 0.85rem;
    scrollbar-width: thin;
    scrollbar-color: #cfc8bb transparent;
}

.tb-chat-log::-webkit-scrollbar {
    width: 6px;
}

.tb-chat-log::-webkit-scrollbar-thumb {
    background: #cfc8bb;
    border-radius: 3px;
}

.tb-chat-msg {
    max-width: 88%;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    animation: tbChatIn 0.4s var(--tbc-ease) both;
}

@keyframes tbChatIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: none; }
}

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

.tb-chat-msg--user {
    align-self: flex-end;
    align-items: flex-end;
}

.tb-chat-bubble {
    padding: 0.7rem 0.9rem;
    font-size: 0.875rem;
    border-radius: 2px;
    word-wrap: break-word;
    overflow-wrap: anywhere;
}

.tb-chat-msg--bot .tb-chat-bubble {
    background: var(--tbc-surface);
    border: 1px solid var(--tbc-line);
    color: var(--tbc-ink);
    box-shadow: var(--shadow-sm, 0 1px 2px rgba(23, 20, 15, 0.07));
}

.tb-chat-msg--user .tb-chat-bubble {
    background: var(--tbc-navy);
    color: #fff;
}

.tb-chat-bubble p + p {
    margin-top: 0.55rem;
}

.tb-chat-bubble strong {
    font-weight: 600;
}

.tb-chat-bubble a {
    color: var(--tbc-gold);
    font-weight: 600;
    text-decoration: underline;
    text-underline-offset: 2px;
}

/* Fact strip inside an answer — mirrors the dt-facts pattern. */
.tb-chat-facts {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
    margin-top: 0.6rem;
}

.tb-chat-fact {
    display: inline-flex;
    align-items: baseline;
    gap: 0.3rem;
    padding: 0.24rem 0.55rem;
    border-radius: 2px;
    background: var(--tbc-canvas);
    border: 1px solid var(--tbc-line);
    font-size: 0.73rem;
}

.tb-chat-fact b {
    color: var(--tbc-muted);
    font-weight: 500;
}

/* Result cards for listings */
.tb-chat-cards {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.tb-chat-card {
    display: block;
    padding: 0.6rem 0.75rem;
    background: var(--tbc-surface);
    border: 1px solid var(--tbc-line);
    border-left: 3px solid var(--tbc-gold);
    border-radius: 2px;
    color: var(--tbc-ink);
    transition: transform var(--transition-fast, 0.2s), box-shadow var(--transition-fast, 0.2s), border-color var(--transition-fast, 0.2s);
}

.tb-chat-card:hover {
    transform: translateX(3px);
    border-color: var(--tbc-gold);
    box-shadow: var(--shadow-md, 0 3px 8px -2px rgba(23, 20, 15, 0.12));
}

.tb-chat-card-title {
    display: block;
    font-size: 0.83rem;
    font-weight: 600;
    letter-spacing: var(--track-snug, -0.01em);
}

.tb-chat-card-meta {
    display: block;
    font-size: 0.72rem;
    color: var(--tbc-muted);
    margin-top: 0.12rem;
}

/* Inline link buttons under an answer */
.tb-chat-links {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
}

.tb-chat-link {
    display: inline-flex;
    align-items: center;
    gap: 0.32rem;
    padding: 0.4rem 0.75rem;
    border-radius: 2px;
    font-size: 0.77rem;
    font-weight: 600;
    background: var(--tbc-gold);
    color: #fff;
    box-shadow: var(--shadow-gold, 0 12px 28px -12px rgba(181, 80, 47, 0.38));
    transition: transform var(--transition-fast, 0.2s), background var(--transition-fast, 0.2s), box-shadow var(--transition-fast, 0.2s);
}

.tb-chat-link:hover {
    background: var(--accent-hover, #943f22);
    transform: translateY(-2px);
    box-shadow: none;
}

.tb-chat-link svg {
    width: 13px;
    height: 13px;
    fill: currentColor;
}

.tb-chat-link--ghost {
    background: var(--tbc-surface);
    color: var(--tbc-navy);
    border: 1px solid var(--tbc-line);
    box-shadow: none;
}

.tb-chat-link--ghost:hover {
    border-color: var(--tbc-gold);
    box-shadow: var(--shadow-sm, 0 1px 2px rgba(23, 20, 15, 0.07));
}

/* Suggestion chips */

.tb-chat-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
}

.tb-chat-chip {
    padding: 0.42rem 0.8rem;
    border-radius: 2px;
    border: 1px solid var(--tbc-line);
    background: var(--tbc-surface);
    color: var(--tbc-navy);
    font: inherit;
    font-size: 0.78rem;
    font-weight: 500;
    cursor: pointer;
    transition: background var(--transition-fast, 0.2s), color var(--transition-fast, 0.2s),
        border-color var(--transition-fast, 0.2s), transform var(--transition-fast, 0.2s);
}

.tb-chat-chip:hover,
.tb-chat-chip:focus-visible {
    background: var(--tbc-navy);
    border-color: var(--tbc-navy);
    color: #fff;
    transform: translateY(-1px);
}

.tb-chat-chip:focus-visible {
    outline: 2px solid var(--tbc-gold);
    outline-offset: 2px;
}

/* Typing indicator */

.tb-chat-typing {
    display: flex;
    gap: 4px;
    padding: 0.85rem 0.9rem;
}

.tb-chat-typing span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--tbc-muted);
    animation: tbChatDot 1.2s var(--tbc-ease) infinite;
}

.tb-chat-typing span:nth-child(2) { animation-delay: 0.16s; }
.tb-chat-typing span:nth-child(3) { animation-delay: 0.32s; }

@keyframes tbChatDot {
    0%, 60%, 100% { opacity: 0.28; transform: translateY(0); }
    30% { opacity: 1; transform: translateY(-4px); }
}

/* Composer */

.tb-chat-foot {
    flex: 0 0 auto;
    padding: 0.7rem 0.8rem calc(0.7rem + env(safe-area-inset-bottom, 0px));
    background: var(--tbc-surface);
    border-top: 1px solid var(--tbc-line);
}

.tb-chat-form {
    display: flex;
    align-items: flex-end;
    gap: 0.5rem;
}

.tb-chat-input {
    flex: 1 1 auto;
    min-width: 0;
    resize: none;
    max-height: 6.5rem;
    padding: 0.6rem 0.85rem;
    border: 1px solid var(--tbc-line);
    border-radius: 2px;
    background: var(--tbc-canvas);
    color: var(--tbc-ink);
    font-size: 0.86rem;
    line-height: 1.45;
    transition: border-color var(--transition-fast, 0.2s), box-shadow var(--transition-fast, 0.2s);
}

.tb-chat-input:focus {
    outline: none;
    border-color: var(--tbc-gold);
    box-shadow: 0 0 0 3px rgba(181, 80, 47, 0.16);
}

.tb-chat-input::placeholder {
    color: var(--tbc-muted);
}

.tb-chat-send {
    flex: 0 0 auto;
    width: 40px;
    height: 40px;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    display: grid;
    place-items: center;
    background: var(--tbc-gold);
    color: #fff;
    box-shadow: var(--shadow-gold, 0 12px 28px -12px rgba(181, 80, 47, 0.38));
    transition: transform var(--transition-fast, 0.2s), background var(--transition-fast, 0.2s), opacity var(--transition-fast, 0.2s);
}

.tb-chat-send:hover:not(:disabled) {
    background: var(--accent-hover, #943f22);
    transform: scale(1.06);
}

.tb-chat-send:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.tb-chat-send svg {
    width: 17px;
    height: 17px;
    fill: currentColor;
}

.tb-chat-note {
    margin-top: 0.45rem;
    text-align: center;
    font-size: 0.66rem;
    color: var(--tbc-muted);
}

.tb-chat-note a {
    color: var(--tbc-gold);
    font-weight: 600;
}

/* Screen-reader-only live region */
.tb-chat-sr {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
}

/* ─── Yielding to the mobile nav drawer ──────────────────────── */
/* The drawer is fixed at right: 0 inside the header's stacking context, so
   the widget would otherwise float on top of the open menu. The script
   mirrors the hamburger's aria-expanded onto this class. */

.tb-chat.is-nav-open {
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s var(--tbc-ease);
}

/* ─── Page CTA that already owns the bottom-right corner ─────── */
/* Detail pages carry a fixed "Plan This Trek" pill there. The script
   flags the document so the pill steps aside instead of sitting
   underneath the launcher. */

.tb-chat-shift-cta .sticky-quote-btn {
    right: 6.5rem;
}

/* ─── Responsive ─────────────────────────────────────────────── */

@media (max-width: 640px) {
    .tb-chat {
        right: 1rem;
        bottom: 1rem;
    }

    .tb-chat-launcher {
        width: 56px;
        height: 56px;
    }

    .tb-chat-launcher-icon {
        width: 24px;
        height: 24px;
    }

    /* Stacked rather than side by side — there is no width for both. */
    .tb-chat-shift-cta .sticky-quote-btn {
        right: 1rem;
        bottom: 5.25rem;
        padding: 0.7rem 1.3rem;
        font-size: 0.9rem;
    }

    /* Full-screen sheet: a 380px panel on a phone leaves no room to read. */
    .tb-chat-panel {
        position: fixed;
        inset: 0;
        width: 100%;
        height: 100vh;
        height: 100dvh;
        max-height: none;
        border: none;
        border-radius: 0;
        transform: translateY(100%);
        transform-origin: center bottom;
    }

    .tb-chat.is-open .tb-chat-panel {
        transform: none;
    }

    .tb-chat-head {
        padding-top: calc(0.95rem + env(safe-area-inset-top, 0px));
    }

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

    /* The launcher would float over the full-screen sheet. */
    .tb-chat.is-open .tb-chat-launcher,
    .tb-chat.is-open .tb-chat-teaser {
        display: none;
    }
}

/* The teaser competes with a small viewport's content; keep it short. */
@media (max-width: 380px) {
    .tb-chat-teaser {
        max-width: calc(100vw - 2.5rem);
    }
}

/* ─── Reduced motion ─────────────────────────────────────────── */

@media (prefers-reduced-motion: reduce) {
    .tb-chat *,
    .tb-chat *::before,
    .tb-chat *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }

    .tb-chat-launcher-pulse {
        display: none;
    }
}
