/* ── Tour chat ─────────────────────────────────────────────────────────────
   The Chats tab of the guide and visitor pages (see shared/tour-chat.js).
   The tab takes the whole space between the header and the bottom bar and
   does its own scrolling, so the composer always stays above the bottom bar,
   including when the phone keyboard is open. */
:root {
    --chat-accent: #6685EC;
    --chat-accent-soft: #EEF2FD;
    --chat-surface: #F7F8FA;
    --chat-card: #FFFFFF;
    --chat-border: #E6E8EE;
    --chat-text: #1F2937;
    --chat-muted: #6B7280;
    --chat-subtle: #9CA3AF;
    --chat-danger: #E14B4E;
    --chat-radius: 14px;
}

/* The chat scrolls inside itself; the page area around it must not. */
body.chat-tab-active .main-scrollable-area {
    overflow: hidden;
}

#chats-tab.tab-content-area {
    padding: 0;
    min-height: 0;
    background: var(--chat-surface);
}

/* No `display` here: the root is also a .tab-content-area, which decides
   whether the tab is shown at all. */
.tour-chat {
    flex-direction: column;
    flex: 1;
    min-height: 0;
    color: var(--chat-text);
}

.tour-chat .chat-view {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
}

.tour-chat .chat-view[hidden] {
    display: none;
}

.tour-chat button {
    font-family: inherit;
    -webkit-tap-highlight-color: transparent;
}

.tour-chat svg {
    display: block;
}

/* ── List of chats ─────────────────────────────────────────────────────── */
.chat-list {
    flex: 1;
    overflow-y: auto;
    padding: 12px 12px 96px;
    -webkit-overflow-scrolling: touch;
}

.chat-row {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    padding: 12px 8px;
    border: none;
    border-bottom: 1px solid var(--chat-border);
    background: transparent;
    text-align: left;
    cursor: pointer;
    color: inherit;
}

.chat-row.is-pinned {
    background: var(--chat-accent-soft);
    border-bottom: none;
    border-radius: var(--chat-radius);
    margin-bottom: 4px;
}

.chat-row:active {
    background: rgba(102, 133, 236, 0.08);
}

.chat-row-body {
    flex: 1;
    min-width: 0;
}

.chat-row-title {
    font-size: 16px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-row-preview {
    margin-top: 2px;
    font-size: 14px;
    color: var(--chat-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-row.is-unread .chat-row-preview {
    color: var(--chat-text);
    font-weight: 700;
}

.chat-row-meta {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
    min-width: 40px;
}

.chat-row-time {
    font-size: 12px;
    color: var(--chat-subtle);
}

.chat-row.is-unread .chat-row-time {
    color: var(--chat-accent);
}

.chat-unread-dot {
    width: 9px;
    height: 9px;
    border-radius: 50%;
    background: var(--chat-accent);
}

.chat-unread-dot[hidden] {
    display: none;
}

.chat-row-chevron {
    width: 18px;
    height: 18px;
    color: var(--chat-subtle);
    flex-shrink: 0;
}

.chat-row.is-offline .chat-avatar {
    opacity: 0.5;
}

/* ── Avatars ───────────────────────────────────────────────────────────── */
.chat-avatar {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.chat-avatar svg {
    width: 22px;
    height: 22px;
}

.chat-avatar--broadcast {
    background: var(--chat-accent);
    color: #FFFFFF;
}

.chat-avatar--broadcast svg {
    width: 24px;
    height: 24px;
}

.chat-avatar--guide {
    background: var(--chat-accent-soft);
    color: var(--chat-accent);
}

.chat-empty {
    padding: 24px 12px;
    text-align: center;
    color: var(--chat-subtle);
    font-size: 14px;
}

/* ── Conversation header ───────────────────────────────────────────────── */
.chat-thread-header {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: var(--chat-card);
    border-bottom: 1px solid var(--chat-border);
    flex-shrink: 0;
}

.chat-back-btn {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    justify-self: start;
    padding: 6px 4px;
    border: none;
    background: none;
    color: var(--chat-accent);
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
}

.chat-back-btn svg {
    width: 18px;
    height: 18px;
}

.chat-thread-title {
    font-size: 16px;
    font-weight: 700;
    text-align: center;
    max-width: 55vw;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* ── Quick notifications (guide) ───────────────────────────────────────── */
.chat-quick-panel {
    margin: 8px 12px 0;
    padding: 4px 12px 12px;
    background: var(--chat-accent-soft);
    border-radius: var(--chat-radius);
    flex-shrink: 0;
}

.chat-quick-panel[hidden] {
    display: none;
}

.chat-quick-toggle {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    padding: 8px 0;
    border: none;
    background: none;
    color: var(--chat-muted);
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
}

.chat-quick-chevron {
    width: 18px;
    height: 18px;
    transition: transform 0.2s ease;
}

.chat-quick-panel.is-collapsed {
    padding-bottom: 4px;
}

.chat-quick-panel.is-collapsed .chat-quick-chevron {
    transform: rotate(180deg);
}

.chat-quick-panel.is-collapsed .chat-quick-chips {
    display: none;
}

.chat-quick-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    max-height: 30vh;
    overflow-y: auto;
}

.chat-quick-chip {
    padding: 8px 12px;
    border: 1px solid var(--chat-border);
    border-radius: 999px;
    background: var(--chat-card);
    color: var(--chat-accent);
    font-size: 13px;
    line-height: 1.2;
    text-align: left;
    cursor: pointer;
    box-shadow: 0 1px 2px rgba(16, 24, 40, 0.06);
}

.chat-quick-chip:active {
    background: var(--chat-accent-soft);
}

/* ── Messages ──────────────────────────────────────────────────────────── */
.chat-messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 14px;
    -webkit-overflow-scrolling: touch;
}

.chat-message {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    max-width: 100%;
}

.chat-message.is-other {
    justify-content: flex-start;
}

.chat-message.is-own {
    flex-direction: row-reverse;
}

.chat-message .chat-avatar {
    width: 38px;
    height: 38px;
    margin-bottom: 2px;
}

.chat-message .chat-avatar svg {
    width: 19px;
    height: 19px;
}

.chat-message-column {
    display: flex;
    flex-direction: column;
    min-width: 0;
    max-width: 78%;
}

.chat-message.is-own .chat-message-column {
    align-items: flex-end;
}

.chat-message.is-other .chat-message-column {
    align-items: flex-start;
}

.chat-message-meta {
    display: flex;
    gap: 24px;
    margin: 0 6px 4px;
    font-size: 12px;
    color: var(--chat-muted);
}

.chat-bubble {
    padding: 10px 14px;
    border-radius: var(--chat-radius);
    font-size: 15px;
    line-height: 1.35;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    background: var(--chat-card);
    color: var(--chat-text);
    box-shadow: 0 1px 3px rgba(16, 24, 40, 0.1);
}

/* Whatever the guide writes is blue, on both sides of the conversation. */
.chat-message.from-guide .chat-bubble {
    background: var(--chat-accent);
    color: #FFFFFF;
    box-shadow: none;
}

.chat-original-toggle {
    margin: 4px 6px 0;
    padding: 0;
    border: none;
    background: none;
    color: var(--chat-accent);
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
}

.chat-message.is-pending .chat-bubble {
    opacity: 0.6;
}

.chat-sending {
    margin: 4px 6px 0;
    font-size: 11px;
    color: var(--chat-subtle);
}

.chat-retry {
    margin: 4px 6px 0;
    padding: 0;
    border: none;
    background: none;
    color: var(--chat-danger);
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
}

.chat-message.is-failed .chat-bubble {
    opacity: 1;
    outline: 1px solid var(--chat-danger);
}

/* ── Composer ──────────────────────────────────────────────────────────── */
.chat-composer {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    background: var(--chat-card);
    border-top: 1px solid var(--chat-border);
    flex-shrink: 0;
}

.chat-composer[hidden],
.chat-readonly-note[hidden] {
    display: none;
}

.tour-chat .chat-composer-input {
    flex: 1;
    min-width: 0;
    height: 44px;
    padding: 0 16px;
    border: 1px solid #D5D9E0;
    border-radius: 999px;
    background: var(--chat-card);
    color: var(--chat-text);
    /* 16px keeps iOS from zooming into the field on focus. */
    font-size: 16px;
    font-family: inherit;
    outline: none;
}

.tour-chat .chat-composer-input:focus {
    border-color: var(--chat-accent);
}

.chat-send-btn {
    width: 44px;
    height: 44px;
    border: none;
    border-radius: 50%;
    background: var(--chat-accent);
    color: #FFFFFF;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    cursor: pointer;
}

.chat-send-btn svg {
    width: 20px;
    height: 20px;
}

.chat-send-btn:active {
    transform: scale(0.95);
}

.chat-readonly-note {
    padding: 12px;
    background: var(--chat-card);
    border-top: 1px solid var(--chat-border);
    color: var(--chat-muted);
    font-size: 13px;
    flex-shrink: 0;
}

/* ── Rename dialog (visitor) ───────────────────────────────────────────── */
.chat-dialog-layer {
    position: fixed;
    inset: 0;
    z-index: 3000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    background: rgba(16, 24, 40, 0.45);
}

.chat-dialog-layer[hidden] {
    display: none;
}

.chat-dialog {
    width: 100%;
    max-width: 360px;
    padding: 24px 20px 20px;
    border-radius: 20px;
    background: var(--chat-card);
    box-shadow: 0 12px 32px rgba(16, 24, 40, 0.2);
    box-sizing: border-box;
}

.chat-dialog-title {
    margin: 0 0 6px;
    font-size: 19px;
    font-weight: 700;
    color: var(--chat-text);
}

.chat-dialog-hint {
    margin: 0 0 16px;
    font-size: 14px;
    color: var(--chat-muted);
}

.chat-dialog .chat-dialog-input {
    width: 100%;
    height: 46px;
    padding: 0 14px;
    border: 1px solid #D5D9E0;
    border-radius: 12px;
    font-size: 16px;
    background: var(--chat-card);
    color: var(--chat-text);
    font-family: inherit;
    box-sizing: border-box;
    outline: none;
}

.chat-dialog .chat-dialog-input:focus {
    border-color: var(--chat-accent);
}

.chat-dialog-error {
    margin-top: 8px;
    font-size: 13px;
    color: var(--chat-danger);
}

.chat-dialog-error[hidden] {
    display: none;
}

.chat-dialog-actions {
    display: flex;
    gap: 10px;
    margin-top: 20px;
}

.chat-dialog-btn {
    flex: 1;
    height: 46px;
    border-radius: 12px;
    font-size: 16px;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
}

.chat-dialog-btn.is-secondary {
    border: 1px solid var(--chat-border);
    background: var(--chat-card);
    color: var(--chat-text);
}

.chat-dialog-btn.is-primary {
    border: none;
    background: var(--chat-accent);
    color: #FFFFFF;
}

.chat-dialog-btn:disabled {
    opacity: 0.6;
}

/* ── Bottom bar badge and header name chip ─────────────────────────────── */
.tab-icon-with-badge {
    position: relative;
    display: inline-block;
    line-height: 0;
}

.tab-unread-badge {
    position: absolute;
    top: -3px;
    right: -5px;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: #E0574F;
    border: 2px solid #FFFFFF;
}

.tab-unread-badge[hidden] {
    display: none;
}

.nav-visitor-name {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    max-width: 55vw;
    padding: 6px 0 6px 8px;
    border: none;
    background: none;
    color: #6B7280;
    font-family: inherit;
    font-size: 18px;
    font-weight: 700;
    cursor: pointer;
}

.nav-visitor-name[hidden] {
    display: none;
}

.nav-visitor-name-text {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.nav-visitor-name svg {
    width: 20px;
    height: 20px;
    flex-shrink: 0;
}

/* Wider screens (tablets, desktop preview): keep the bubbles readable. */
@media (min-width: 600px) {
    .chat-message-column {
        max-width: 70%;
    }
}

/* Guide page: while the chat is open the microphone bar, its waveform and the
   lock button step aside so the message box sits right above the bottom bar.
   The microphone itself keeps running. */
body.chat-tab-active .footer-with-mic,
body.chat-tab-active #speakerWave,
body.chat-tab-active #lock-float-container {
    display: none !important;
}
