/**
 * ArchByte chat widget
 * Spec: docs/CHAT-WIDGET-PLAN.md §3.2 e §5
 * Usa tokens de tokens.css — não introduzir hex novos.
 */

/* ============================================
   Launcher (botão flutuante — acima do .whatsapp-float)
   ============================================ */
.chat-launcher {
    position: fixed;
    /* whatsapp-float ocupa bottom: var(--space-6) + 56px. Empilhamos +12px. */
    bottom: calc(var(--space-6) + 56px + var(--space-3));
    right: var(--space-6);
    width: 56px;
    height: 56px;
    border: none;
    border-radius: var(--radius-full);
    background-color: var(--accent);
    color: var(--accent-text);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: var(--shadow-md), var(--glow-premium);
    z-index: 999;
    transition: transform var(--transition-base), box-shadow var(--transition-base);
}

.chat-launcher:hover,
.chat-launcher:focus-visible {
    transform: scale(1.06);
    box-shadow: var(--shadow-lg), var(--glow-premium-strong);
    outline: none;
}

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

.chat-launcher svg {
    width: 26px;
    height: 26px;
    fill: currentColor;
}

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

/* Quando o cookie banner estiver visível, sobe o launcher (mesma lógica do whatsapp-float) */
.cookie-banner.is-visible ~ .chat-launcher {
    bottom: calc(80px + 56px + var(--space-3));
}

@media (max-width: 768px) {
    .chat-launcher {
        bottom: calc(var(--space-4) + 50px + var(--space-2));
        right: var(--space-4);
        width: 50px;
        height: 50px;
    }

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

    .cookie-banner.is-visible ~ .chat-launcher {
        bottom: calc(120px + 50px + var(--space-2));
    }
}

/* ============================================
   Panel (drawer)
   ============================================ */
.chat-panel {
    position: fixed;
    bottom: calc(var(--space-6) + 56px + var(--space-3));
    right: var(--space-6);
    width: 360px;
    height: 540px;
    max-height: calc(100vh - 120px);
    background-color: var(--bg-surface-elevated);
    border: var(--border-width) solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg), var(--glow-premium);
    display: none;
    flex-direction: column;
    overflow: hidden;
    z-index: 1001;
    font-family: var(--font-body);
    color: var(--text-primary);
    animation: chat-panel-in var(--transition-base) ease-out;
}

.chat-panel.is-open {
    display: flex;
}

@keyframes chat-panel-in {
    from {
        opacity: 0;
        transform: translateY(12px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@media (prefers-reduced-motion: reduce) {
    .chat-panel {
        animation: none;
    }
    .chat-launcher {
        transition: none;
    }
}

@media (max-width: 640px) {
    .chat-panel {
        inset: 0;
        width: 100vw;
        height: 100vh;
        max-height: 100vh;
        border-radius: 0;
        border: none;
    }
}

/* Quando o cookie banner está visível em desktop, mantém o painel fora dele */
.cookie-banner.is-visible ~ .chat-panel {
    bottom: calc(80px + 56px + var(--space-3));
    max-height: calc(100vh - 200px);
}

@media (max-width: 640px) {
    .cookie-banner.is-visible ~ .chat-panel {
        bottom: 0;
        max-height: 100vh;
    }
}

/* ============================================
   Header
   ============================================ */
.chat-panel__header {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-4) var(--space-5);
    border-bottom: var(--border-width) solid var(--border);
    background-color: var(--bg-surface);
}

.chat-panel__title {
    flex: 1;
    margin: 0;
    font-family: var(--font-display);
    font-size: var(--text-base);
    font-weight: 600;
    color: var(--text-primary);
    letter-spacing: var(--tracking-tight);
}

.chat-panel__shortcut {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    padding: var(--space-1) var(--space-2);
    border-radius: var(--radius-sm);
    font-size: var(--text-xs);
    font-weight: 600;
    color: var(--text-secondary);
    text-decoration: none;
    border: var(--border-width) solid var(--border);
    transition: color var(--transition-fast), border-color var(--transition-fast);
}

.chat-panel__shortcut:hover,
.chat-panel__shortcut:focus-visible {
    color: var(--accent);
    border-color: var(--accent-border);
    outline: none;
}

.chat-panel__shortcut svg {
    width: 14px;
    height: 14px;
    fill: currentColor;
}

.chat-panel__close {
    width: 32px;
    height: 32px;
    border: none;
    background: transparent;
    color: var(--text-muted);
    border-radius: var(--radius-sm);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 22px;
    line-height: 1;
    transition: color var(--transition-fast), background-color var(--transition-fast);
}

.chat-panel__close:hover,
.chat-panel__close:focus-visible {
    color: var(--text-primary);
    background-color: var(--bg-surface-elevated);
    outline: none;
}

@media (max-width: 640px) {
    .chat-panel__close {
        width: 44px;
        height: 44px;
        font-size: 28px;
    }
}

/* ============================================
   Log (mensagens)
   ============================================ */
.chat-panel__log {
    flex: 1;
    overflow-y: auto;
    padding: var(--space-4) var(--space-5);
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
    background-color: var(--bg-surface-elevated);
}

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

.chat-panel__log::-webkit-scrollbar-thumb {
    background-color: var(--border);
    border-radius: var(--radius-full);
}

.chat-msg {
    max-width: 88%;
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-md);
    line-height: var(--leading-snug);
    font-size: var(--text-sm);
    white-space: pre-wrap;
    word-wrap: break-word;
}

.chat-msg--assistant {
    align-self: flex-start;
    background-color: var(--bg-surface);
    border: var(--border-width) solid var(--border);
    border-left: 3px solid var(--accent);
    color: var(--text-primary);
}

.chat-msg--user {
    align-self: flex-end;
    background-color: var(--accent);
    color: var(--accent-text);
    font-weight: 500;
}

.chat-msg--system {
    align-self: center;
    background: transparent;
    color: var(--text-muted);
    font-size: var(--text-xs);
    font-style: italic;
    text-align: center;
    max-width: 100%;
    padding: var(--space-1) var(--space-2);
}

.chat-msg--error {
    align-self: stretch;
    background-color: var(--bg-surface);
    border: var(--border-width) solid var(--border);
    border-left: 3px solid var(--text-muted);
    color: var(--text-secondary);
    font-size: var(--text-sm);
}

.chat-msg__typing::after {
    content: '…';
    display: inline-block;
    margin-left: 2px;
    animation: chat-typing 1.2s infinite;
}

@keyframes chat-typing {
    0%, 100% { opacity: 0.2; }
    50% { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
    .chat-msg__typing::after {
        animation: none;
        opacity: 1;
    }
}

/* ============================================
   Quick reply chips
   ============================================ */
.chat-panel__chips {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    padding: 0 var(--space-5) var(--space-3);
    background-color: var(--bg-surface-elevated);
}

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

.chat-chip {
    padding: var(--space-2) var(--space-3);
    border: var(--border-width) solid var(--border);
    border-radius: var(--radius-full);
    background-color: var(--bg-surface);
    color: var(--text-secondary);
    font-family: var(--font-body);
    font-size: var(--text-xs);
    cursor: pointer;
    transition: border-color var(--transition-fast), color var(--transition-fast), background-color var(--transition-fast);
}

.chat-chip:hover,
.chat-chip:focus-visible {
    border-color: var(--accent);
    color: var(--accent);
    outline: none;
}

/* ============================================
   Footer (form + handoff actions)
   ============================================ */
.chat-panel__form {
    display: flex;
    gap: var(--space-2);
    padding: var(--space-3) var(--space-4);
    border-top: var(--border-width) solid var(--border);
    background-color: var(--bg-surface);
}

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

.chat-panel__input {
    flex: 1;
    border: var(--border-width) solid var(--border);
    border-radius: var(--radius-md);
    background-color: var(--bg-surface-elevated);
    color: var(--text-primary);
    padding: var(--space-2) var(--space-3);
    font-family: var(--font-body);
    /* 16px previne zoom no iOS */
    font-size: 16px;
    line-height: var(--leading-snug);
    resize: none;
    max-height: 100px;
    transition: border-color var(--transition-fast);
}

.chat-panel__input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-ring);
}

.chat-panel__input::placeholder {
    color: var(--text-muted);
}

.chat-panel__send {
    flex-shrink: 0;
    border: none;
    border-radius: var(--radius-md);
    background-color: var(--accent);
    color: var(--accent-text);
    padding: 0 var(--space-4);
    font-family: var(--font-display);
    font-weight: 600;
    font-size: var(--text-sm);
    cursor: pointer;
    transition: background-color var(--transition-fast);
}

.chat-panel__send:hover:not(:disabled),
.chat-panel__send:focus-visible {
    background-color: var(--accent-hover);
    outline: none;
}

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

.chat-panel__send:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* ============================================
   Handoff actions (WhatsApp + form fallback)
   ============================================ */
.chat-panel__handoff {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    padding: var(--space-3) var(--space-4);
    border-top: var(--border-width) solid var(--border);
    background-color: var(--bg-surface);
}

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

.chat-handoff__btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-md);
    font-family: var(--font-display);
    font-weight: 600;
    font-size: var(--text-sm);
    text-decoration: none;
    border: var(--border-width-thick) solid transparent;
    cursor: pointer;
    transition: background-color var(--transition-fast), border-color var(--transition-fast), color var(--transition-fast);
}

.chat-handoff__btn--primary {
    background-color: var(--brand-whatsapp);
    color: #ffffff;
}

.chat-handoff__btn--primary:hover,
.chat-handoff__btn--primary:focus-visible {
    background-color: #1fb858;
    outline: none;
}

.chat-handoff__btn--secondary {
    background-color: transparent;
    color: var(--text-secondary);
    border-color: var(--border);
}

.chat-handoff__btn--secondary:hover,
.chat-handoff__btn--secondary:focus-visible {
    color: var(--accent);
    border-color: var(--accent-border);
    outline: none;
}

.chat-handoff__btn svg {
    width: 18px;
    height: 18px;
    fill: currentColor;
}

/* ============================================
   Acessibilidade — skip helpers
   ============================================ */
.chat-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;
}
