/* TripleWin Assistant — chat widget
   Self-contained: every rule is namespaced under #tw-chat-toggle / #tw-chat-panel or .tw-* so the
   widget can be dropped onto any page without touching the host layout. */

:root {
    --tw-chat-brand: #e5ee80;
    --tw-chat-brand-dark: #d4dd5f;
    --tw-chat-ink: #1b1b1c;
    --tw-chat-surface: #fff;
    --tw-chat-canvas: #f8f9fa;
    --tw-chat-line: #e9ecef;
    --tw-chat-muted: #6c757d;
    --tw-chat-faint: #adb5bd;
    --tw-chat-danger: #d64545;
    --tw-chat-radius: 18px;
    --tw-chat-shadow: 0 12px 48px rgba(0, 0, 0, .18);
}

/* ---------------------------------------------------------------- launcher */

#tw-chat-toggle {
    position: fixed;
    bottom: 28px;
    right: 28px;
    z-index: 10000;
    width: 56px;
    height: 56px;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: var(--tw-chat-brand);
    color: var(--tw-chat-ink);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    cursor: pointer;
    box-shadow: 0 4px 16px rgba(0, 0, 0, .25);
    transition: transform .2s ease, box-shadow .2s ease;
}

#tw-chat-toggle:hover {
    transform: scale(1.08);
    box-shadow: 0 6px 20px rgba(0, 0, 0, .28);
}

#tw-chat-toggle:focus-visible {
    outline: 3px solid var(--tw-chat-ink);
    outline-offset: 3px;
}

/* Unread dot — only shown when a reply lands while the panel is closed. */
.tw-chat-unread {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: var(--tw-chat-danger);
    border: 2px solid var(--tw-chat-surface);
    display: none;
}

#tw-chat-toggle[data-unread="true"] .tw-chat-unread {
    display: block;
}

/* ------------------------------------------------------------------- panel */

#tw-chat-panel {
    position: fixed;
    bottom: 96px;
    right: 28px;
    z-index: 10001;
    width: 420px;
    max-width: calc(100vw - 32px);
    height: 620px;
    max-height: calc(100vh - 120px);
    background: var(--tw-chat-surface);
    border-radius: var(--tw-chat-radius);
    box-shadow: var(--tw-chat-shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    font-family: 'Outfit', 'DM Sans', sans-serif;
    transform-origin: bottom right;
    animation: tw-chat-panel-in .18s cubic-bezier(.4, 0, .2, 1);
}

#tw-chat-panel[hidden] {
    display: none;
}

@keyframes tw-chat-panel-in {
    from { opacity: 0; transform: translateY(12px) scale(.97); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

#tw-chat-header {
    background: var(--tw-chat-ink);
    color: var(--tw-chat-brand);
    padding: 14px 16px 14px 20px;
    font-weight: 600;
    font-size: 15px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}

.tw-chat-title {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
}

.tw-chat-title-text {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.tw-chat-header-actions {
    display: flex;
    align-items: center;
    gap: 2px;
}

.tw-chat-icon-btn {
    background: none;
    border: none;
    color: var(--tw-chat-brand);
    font-size: 18px;
    line-height: 1;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background .15s ease;
}

.tw-chat-icon-btn:hover {
    background: rgba(229, 238, 128, .16);
}

.tw-chat-icon-btn:focus-visible {
    outline: 2px solid var(--tw-chat-brand);
    outline-offset: -2px;
}

.tw-chat-icon-btn[hidden] {
    display: none;
}

/* ---------------------------------------------------------------- messages */

/* Holds the scroller plus the floating jump-to-latest pill, which must stay put while the
   transcript scrolls underneath it. */
.tw-chat-body {
    position: relative;
    flex: 1;
    min-height: 0;
    display: flex;
}

#tw-chat-messages {
    flex: 1;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 20px 16px;
    display: flex;
    flex-direction: column;
    gap: 14px;
    background: var(--tw-chat-canvas);
    scroll-behavior: smooth;
}

.tw-msg-row {
    display: flex;
    gap: 10px;
    align-items: flex-end;
    animation: tw-msg-in .18s ease-out;
}

@keyframes tw-msg-in {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: translateY(0); }
}

.tw-msg-row-bot,
.tw-msg-row-typing {
    align-self: flex-start;
}

.tw-msg-row-user {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.tw-msg-content {
    max-width: 80%;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.tw-avatar {
    width: 32px;
    height: 32px;
    min-width: 32px;
    border-radius: 50%;
    background: var(--tw-chat-brand);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: var(--tw-chat-ink);
}

.tw-msg {
    padding: 12px 16px;
    border-radius: 16px;
    font-size: 14px;
    line-height: 1.55;
    overflow-wrap: anywhere;
}

.tw-msg-bot {
    background: var(--tw-chat-surface);
    color: var(--tw-chat-ink);
    border: 1px solid var(--tw-chat-line);
    border-radius: 16px 16px 16px 4px;
}

.tw-msg-user {
    background: var(--tw-chat-ink);
    color: #f4f4f5;
    border-radius: 16px 16px 4px 16px;
}

.tw-msg-error {
    background: #fdf2f2;
    color: #8a2a2a;
    border: 1px solid #f5d2d2;
    border-radius: 16px 16px 16px 4px;
}

.tw-msg-time {
    font-size: 11px;
    color: #999;
    margin-top: 2px;
    padding: 0 4px;
}

.tw-msg-row-user .tw-msg-time {
    text-align: right;
}

/* Typing indicator */
.tw-typing-dots {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 14px 16px;
}

.tw-typing-dots span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--tw-chat-faint);
    animation: tw-typing 1.1s ease-in-out infinite;
}

.tw-typing-dots span:nth-child(2) { animation-delay: .15s; }
.tw-typing-dots span:nth-child(3) { animation-delay: .3s; }

@keyframes tw-typing {
    0%, 60%, 100% { transform: translateY(0); opacity: .45; }
    30% { transform: translateY(-4px); opacity: 1; }
}

/* ------------------------------------------------------------------- cards */

.tw-suggestion-card {
    background: var(--tw-chat-surface);
    border: 1px solid #e4e7eb;
    border-radius: 14px;
    padding: 14px;
    font-size: 13px;
    display: flex;
    gap: 14px;
    align-items: flex-start;
    animation: tw-msg-in .18s ease-out;
}

.tw-card-image {
    width: 100px;
    min-width: 100px;
    height: 110px;
    border-radius: 10px;
    background: linear-gradient(135deg, #c8e6c9 0%, #81d4fa 100%);
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #66bb6a;
    font-size: 28px;
}

.tw-card-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.tw-card-details {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 5px;
}

.tw-region-name {
    font-size: 16px;
    font-weight: 700;
    color: var(--tw-chat-ink);
    margin-bottom: 2px;
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.tw-card-detail-row {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    color: #444;
}

.tw-card-detail-row i {
    color: var(--tw-chat-muted);
    font-size: 14px;
    width: 18px;
    text-align: center;
}

.tw-card-detail-row strong {
    color: var(--tw-chat-ink);
}

.tw-weather-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 10px;
    border-radius: 20px;
    font-size: 11px;
    font-weight: 600;
}

.tw-weather-badge i { font-size: 12px; }
.tw-weather-good { background: #d4edda; color: #155724; }
.tw-weather-bad { background: #fff3cd; color: #856404; }

/* ----------------------------------------------------------- quick replies */

.tw-chat-suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 4px 0;
}

.tw-chat-suggestions-label {
    font-size: 11px;
    font-weight: 600;
    letter-spacing: .3px;
    text-transform: uppercase;
    color: var(--tw-chat-faint);
    width: 100%;
    margin-bottom: -2px;
}

.tw-suggestion-chip {
    background: var(--tw-chat-surface);
    border: 1.5px solid var(--tw-chat-brand);
    border-radius: 20px;
    padding: 8px 14px;
    font-size: 13px;
    font-family: inherit;
    color: var(--tw-chat-ink);
    cursor: pointer;
    transition: background .2s ease, border-color .2s ease, transform .12s ease;
    text-align: left;
    line-height: 1.4;
}

.tw-suggestion-chip:hover {
    background: var(--tw-chat-brand);
    border-color: var(--tw-chat-brand-dark);
}

.tw-suggestion-chip:active {
    transform: scale(.97);
}

.tw-suggestion-chip:focus-visible {
    outline: 2px solid var(--tw-chat-ink);
    outline-offset: 2px;
}

.tw-suggestion-chip[disabled] {
    opacity: .5;
    cursor: not-allowed;
}

.tw-suggestion-chip--retry {
    border-color: var(--tw-chat-danger);
    color: var(--tw-chat-danger);
}

.tw-suggestion-chip--retry:hover {
    background: #fdf2f2;
    border-color: var(--tw-chat-danger);
}

/* --------------------------------------------------------- jump-to-latest */

.tw-chat-jump {
    position: absolute;
    left: 50%;
    bottom: 12px;
    transform: translateX(-50%);
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 7px 14px;
    border: 1px solid var(--tw-chat-line);
    border-radius: 20px;
    background: var(--tw-chat-surface);
    color: var(--tw-chat-ink);
    font-family: inherit;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 4px 14px rgba(0, 0, 0, .12);
    z-index: 5;
}

.tw-chat-jump[hidden] {
    display: none;
}

/* ------------------------------------------------------------ form / input */

#tw-chat-form {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 14px;
    padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
    border-top: 1px solid #eee;
    background: var(--tw-chat-surface);
    position: relative;
}

#tw-chat-form.tw-recording .tw-input-wrapper {
    opacity: 0;
    pointer-events: none;
}

.tw-input-wrapper {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: center;
    gap: 8px;
    background: #f4f5f7;
    border-radius: 24px;
    padding: 10px 16px;
    border: 1px solid var(--tw-chat-line);
    transition: border-color .2s ease;
}

.tw-input-wrapper:focus-within {
    border-color: var(--tw-chat-brand);
}

.tw-input-icon {
    color: var(--tw-chat-faint);
    font-size: 16px;
}

#tw-chat-input {
    flex: 1;
    min-width: 0;
    border: none;
    outline: none;
    background: transparent;
    font-size: 13px;
    font-family: inherit;
    color: var(--tw-chat-ink);
}

#tw-chat-input::placeholder {
    color: var(--tw-chat-faint);
}

/* Appears only as the user approaches the server-side cap. */
.tw-char-count {
    font-size: 11px;
    font-variant-numeric: tabular-nums;
    color: var(--tw-chat-faint);
}

.tw-char-count[hidden] {
    display: none;
}

.tw-char-count--limit {
    color: var(--tw-chat-danger);
    font-weight: 600;
}

#tw-chat-mic,
#tw-chat-send {
    width: 42px;
    height: 42px;
    min-width: 42px;
    border-radius: 50%;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background .2s ease, transform .15s ease, color .2s ease;
}

#tw-chat-mic {
    width: 40px;
    height: 40px;
    min-width: 40px;
    background: linear-gradient(135deg, #f0f1f3 0%, #e4e6ea 100%);
    color: var(--tw-chat-muted);
    font-size: 17px;
    position: relative;
}

#tw-chat-mic:hover {
    background: linear-gradient(135deg, #e9ecef 0%, #dde0e4 100%);
    color: var(--tw-chat-ink);
    transform: scale(1.08);
}

#tw-chat-mic[hidden] {
    display: none;
}

#tw-chat-mic.tw-mic-active {
    background: linear-gradient(135deg, #ff5252 0%, #d32f2f 100%);
    color: #fff;
    box-shadow: 0 0 0 4px rgba(255, 82, 82, .2), 0 4px 12px rgba(255, 82, 82, .3);
    animation: tw-mic-glow 1.5s ease-in-out infinite;
    transform: scale(1.1);
}

@keyframes tw-mic-glow {
    0%, 100% { box-shadow: 0 0 0 4px rgba(255, 82, 82, .2), 0 4px 12px rgba(255, 82, 82, .3); }
    50% { box-shadow: 0 0 0 8px rgba(255, 82, 82, .1), 0 4px 20px rgba(255, 82, 82, .4); }
}

#tw-chat-send {
    background: var(--tw-chat-brand);
    color: var(--tw-chat-ink);
    font-size: 16px;
}

#tw-chat-send:hover:not(:disabled) {
    background: var(--tw-chat-brand-dark);
    transform: scale(1.05);
}

#tw-chat-send:disabled {
    opacity: .4;
    cursor: not-allowed;
    transform: none;
}

#tw-chat-mic:focus-visible,
#tw-chat-send:focus-visible,
.tw-chat-jump:focus-visible {
    outline: 2px solid var(--tw-chat-ink);
    outline-offset: 2px;
}

/* -------------------------------------------------------- recording overlay */

.tw-rec-overlay {
    position: absolute;
    left: 14px;
    right: 96px;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 0 16px;
    height: 42px;
    background: linear-gradient(135deg, #fafafa 0%, #f5f5f5 100%);
    border-radius: 21px;
    border: 1px solid #eee;
    box-shadow: 0 2px 8px rgba(0, 0, 0, .05);
    z-index: 10;
    animation: tw-rec-slide-in .2s cubic-bezier(.4, 0, .2, 1);
}

.tw-rec-overlay[hidden] {
    display: none;
}

@keyframes tw-rec-slide-in {
    from { opacity: 0; transform: translateY(-50%) scale(.95); }
    to { opacity: 1; transform: translateY(-50%) scale(1); }
}

.tw-rec-indicator {
    display: flex;
    align-items: center;
    gap: 8px;
}

.tw-rec-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #ff3b30;
    box-shadow: 0 0 6px rgba(255, 59, 48, .4);
    animation: tw-rec-blink 1s ease-in-out infinite;
}

@keyframes tw-rec-blink {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: .35; transform: scale(.85); }
}

.tw-rec-timer {
    font-size: 13px;
    font-weight: 600;
    color: #333;
    font-variant-numeric: tabular-nums;
    min-width: 32px;
    letter-spacing: .2px;
}

.tw-rec-waveform {
    display: flex;
    align-items: center;
    gap: 2.5px;
    flex: 1;
    justify-content: center;
    padding: 0 8px;
}

.tw-rec-waveform span {
    width: 2.5px;
    border-radius: 2px;
    background: #ff5252;
    opacity: .7;
    animation: tw-wave .7s ease-in-out infinite;
}

.tw-rec-waveform span:nth-child(1) { animation-delay: 0s; height: 8px; }
.tw-rec-waveform span:nth-child(2) { animation-delay: .08s; height: 14px; }
.tw-rec-waveform span:nth-child(3) { animation-delay: .16s; height: 10px; }
.tw-rec-waveform span:nth-child(4) { animation-delay: .24s; height: 16px; }
.tw-rec-waveform span:nth-child(5) { animation-delay: .32s; height: 7px; }

@keyframes tw-wave {
    0%, 100% { transform: scaleY(.4); }
    50% { transform: scaleY(1.3); }
}

/* --------------------------------------------------------------- signed out */

.tw-chat-signin {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 32px 24px;
    text-align: center;
    background: var(--tw-chat-canvas);
}

.tw-chat-signin-icon {
    font-size: 40px;
    color: var(--tw-chat-faint);
    margin-bottom: 16px;
}

.tw-chat-signin-title {
    font-size: 16px;
    font-weight: 600;
    color: var(--tw-chat-ink);
    margin: 0 0 8px;
}

.tw-chat-signin-text {
    font-size: 13px;
    color: var(--tw-chat-muted);
    margin: 0 0 20px;
    line-height: 1.5;
}

.tw-chat-signin-btn {
    display: inline-block;
    padding: 10px 28px;
    background: var(--tw-chat-brand);
    color: var(--tw-chat-ink);
    font-weight: 600;
    font-size: 14px;
    border-radius: 8px;
    text-decoration: none;
    transition: background .2s ease;
}

.tw-chat-signin-btn:hover {
    background: var(--tw-chat-brand-dark);
}

/* Visible only to screen readers — carries status announcements. */
.tw-sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* ------------------------------------------------------------------ mobile */

@media (max-width: 520px) {
    #tw-chat-panel {
        inset: 0;
        width: 100%;
        max-width: none;
        height: 100dvh;
        max-height: none;
        border-radius: 0;
    }

    #tw-chat-toggle {
        bottom: 20px;
        right: 20px;
    }

    .tw-msg-content {
        max-width: 86%;
    }

    .tw-card-image {
        width: 84px;
        min-width: 84px;
        height: 94px;
    }
}

/* --------------------------------------------------------- reduced motion */

@media (prefers-reduced-motion: reduce) {
    #tw-chat-panel,
    .tw-msg-row,
    .tw-suggestion-card,
    .tw-rec-overlay {
        animation: none;
    }

    #tw-chat-messages {
        scroll-behavior: auto;
    }

    .tw-typing-dots span,
    .tw-rec-dot,
    .tw-rec-waveform span,
    #tw-chat-mic.tw-mic-active {
        animation: none;
    }

    #tw-chat-toggle:hover,
    #tw-chat-send:hover:not(:disabled),
    #tw-chat-mic:hover {
        transform: none;
    }
}
