/* ============================================
   CHAT FULLSCREEN - iOS STYLE KEYBOARD
   ============================================ */

/* Desativa transições durante carregamento inicial */
#chatMessages.no-transition,
#chatMessages.no-transition * {
    transition: none !important;
}

/* Prevent iOS text resize on orientation change */
#view-chat,
#view-chat *,
#chatMessages,
#chatMessages * {
    -webkit-text-size-adjust: 100% !important;
    -moz-text-size-adjust: 100% !important;
    -ms-text-size-adjust: 100% !important;
    text-size-adjust: 100% !important;
}

/* View Chat - Ocupa tela inteira */
#view-chat {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    width: 100vw !important;
    height: 100vh !important;
    background: transparent !important;
    display: none;
    flex-direction: column;
    z-index: 50 !important;
    margin: 0 !important;
    padding: 0 !important;
    max-width: none !important;
    overflow: hidden;
}

#view-chat.active-view {
    display: flex !important;
}

/* ============================================
   MENSAGENS
   ============================================ */

#chatMessages {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0; /* Vai até ao fundo do ecrã por defeito */
    overflow-y: auto;
    overflow-x: hidden;
    padding: 16px;
    padding-top: 117.5px; /* Espaço para o header */
    padding-bottom: calc(75px + env(safe-area-inset-bottom, 0px)); /* Espaço para a dock */
    display: none; /* Escondido por defeito */
    flex-direction: column; /* Normal: antigas em cima, recentes em baixo */
    gap: 11px;
    background: transparent;
    transition: bottom 0.3s cubic-bezier(0.25, 0.1, 0.25, 1), padding-bottom 0.3s cubic-bezier(0.25, 0.1, 0.25, 1);
    z-index: 100;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-y: contain;
    will-change: bottom, padding-bottom;
}

/* Mensagens alinhadas ao topo (comportamento padrão de chat) */

/* Mostra mensagens apenas quando chat está ativo */
body:has(#view-chat.active-view) #chatMessages {
    display: flex;
}

/* Quando teclado está aberto, mensagens sobem */
body:has(#chatKeyboard.open) #chatMessages {
    bottom: calc(270px + env(safe-area-inset-bottom, 0px)); /* Altura do teclado + safe area */
    padding-bottom: 24px !important; /* Espaço entre mensagens e teclado */
}

/* Durante animação de escrita do AI - mais espaço para ver a mensagem */
#chatMessages.ai-typing {
    padding-bottom: 55px !important;
    transition: padding-bottom 0.3s ease;
}

/* Quando search mode está ativo - teclado sem input bar, menos altura */
body.search-mode:has(#chatKeyboard.open.global-mode) #chatMessages {
    bottom: calc(213px + env(safe-area-inset-bottom, 0px)); /* Altura menor sem input bar */
}

#chatMessages::-webkit-scrollbar {
    width: 3px;
}

#chatMessages::-webkit-scrollbar-thumb {
    background: rgba(255,255,255,0.2);
    border-radius: 2px;
}

#scrollToBottomBtn {
    position: fixed;
    right: 16px;
    bottom: calc(100px + env(safe-area-inset-bottom, 0px));
    width: 29px;
    height: 29px;
    border-radius: 50%;
    background: color-mix(in srgb, var(--glass-bg) 90%, rgba(80, 80, 85, 0.8));
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid rgba(255, 255, 255, 0.3);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 500;
    opacity: 0;
    visibility: hidden;
    transform: scale(0.8) translateY(10px);
    transition: opacity 0.2s ease, visibility 0.2s ease, transform 0.2s ease, bottom 0.3s ease;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.4);
    -webkit-tap-highlight-color: transparent;
}

#scrollToBottomBtn.visible {
    opacity: 1;
    visibility: visible;
    transform: scale(1) translateY(0);
}

#scrollToBottomBtn:active {
    transform: scale(0.9);
}

#scrollToBottomBtn svg {
    width: 16px;
    height: 16px;
    fill: none;
    stroke: rgba(255, 255, 255, 0.5);
    stroke-width: 2.5;
    stroke-linecap: round;
    stroke-linejoin: round;
    margin-top: 1px;
}

/* Quando teclado está aberto, botão sobe */
body:has(#chatKeyboard.open) #scrollToBottomBtn {
    bottom: calc(296px + env(safe-area-inset-bottom, 0px));
}

/* Esconde quando chat não está ativo */
body:not(:has(#view-chat.active-view)) #scrollToBottomBtn {
    display: none;
}

/* Mini Keyboard Button - abre teclado quando no fundo */
#miniKeyboardBtn {
    position: fixed;
    right: 16px;
    bottom: calc(88px + env(safe-area-inset-bottom, 0px));
    width: 70px;
    height: 32px;
    padding: 6px 5px;
    border-radius: 8px;
    background: color-mix(in srgb, var(--glass-bg) 90%, rgba(80, 80, 85, 0.8));
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid rgba(255, 255, 255, 0.3);
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 2px;
    cursor: pointer;
    z-index: 500;
    opacity: 0;
    visibility: hidden;
    transform: scale(0.9) translateY(5px);
    transition: opacity 0.2s ease, visibility 0.2s ease, transform 0.2s ease, bottom 0.3s ease;
    -webkit-tap-highlight-color: transparent;
    box-sizing: border-box;
}

#miniKeyboardBtn.visible {
    opacity: 1;
    visibility: visible;
    transform: scale(1) translateY(0);
}

#miniKeyboardBtn:active {
    transform: scale(0.95);
    background: rgba(50, 50, 55, 0.8);
}

/* Mini keyboard rows */
.mini-kb-row {
    display: flex;
    justify-content: center;
    gap: 2px;
    width: 100%;
}

.mini-kb-row span {
    width: 5px;
    height: 4px;
    background: rgba(255, 255, 255, 0.25);
    border-radius: 1px;
}

.mini-kb-row.mini-kb-space {
    margin-top: 1px;
}

.mini-kb-row .space-bar {
    width: 36px;
    height: 4px;
    background: rgba(255, 255, 255, 0.25);
    border-radius: 2px;
}

/* Esconde quando chat não está ativo */
body:not(:has(#view-chat.active-view)) #miniKeyboardBtn {
    display: none;
}

/* Quando teclado está aberto, mini teclado fica acima do input bar */
body:has(#chatKeyboard.open) #miniKeyboardBtn {
    bottom: calc(295px + env(safe-area-inset-bottom, 0px));
}

.msg {
    max-width: 69%;
    padding: 10px 14px;
    border-radius: 18px;
    font-size: 15px !important;
    line-height: 1.35;
    -webkit-text-size-adjust: 100% !important;
    text-size-adjust: 100% !important;
    word-wrap: break-word;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
    transition: transform 0.3s ease, box-shadow 0.3s ease, width 0.05s ease-out;
}

/* Animação de typewriter - cursor piscante no final */
.msg.bot.typing {
    position: relative;
}

.msg.bot.typing::after {
    content: '|';
    animation: cursorBlink 0.8s ease-in-out infinite;
    opacity: 1;
    margin-left: 1px;
}

.msg:active {
    transform: scale(0.98);
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
}

.msg.user {
    background: transparent;
    color: var(--msg-user-text, #fff);
    align-self: flex-end;
    border-bottom-right-radius: 4px;
}

.msg.bot {
    background: transparent;
    color: var(--msg-ai-text, #fff);
    align-self: flex-start;
    border-bottom-left-radius: 4px;
}

/* Chat message backgrounds when toggle is ON */
body.chat-msg-background .msg.user {
    background: var(--msg-user-bg, rgba(0, 122, 255, 0.15));
}

body.chat-msg-background .msg.bot {
    background: var(--msg-ai-bg, rgba(255, 255, 255, 0.08));
}

/* Fog effect: 50% transparency + blur on message backgrounds */
body.fog-active.chat-msg-background .msg.user,
body.fog-active.chat-msg-background .msg.bot {
    background: color-mix(in srgb, var(--msg-user-bg, rgba(0, 122, 255, 0.15)) 50%, transparent);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
}

body.fog-active.chat-msg-background .msg.bot {
    background: color-mix(in srgb, var(--msg-ai-bg, rgba(255, 255, 255, 0.08)) 50%, transparent);
}

/* Indicador de Tokens (aparece no canto inferior direito da mensagem do bot) */
.token-indicator {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    font-size: 10px;
    color: rgba(255, 255, 255, 0.4);
    margin-top: 6px;
    padding-top: 4px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    font-family: 'SF Mono', 'Monaco', 'Consolas', monospace;
    cursor: help;
}

.token-indicator .token-icon {
    font-size: 8px;
    opacity: 0.6;
}

.msg-text {
    display: block;
}

/* Typing Indicator (três pontinhos animados) */
.msg.typing-indicator {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 12px 16px;
    width: auto;
    min-width: auto;
}

.msg.typing-indicator span {
    width: 8px;
    height: 8px;
    background: rgba(255, 255, 255, 0.5);
    border-radius: 50%;
    animation: typingBounce 1.4s ease-in-out infinite;
}

.msg.typing-indicator span:nth-child(1) {
    animation-delay: 0s;
}

.msg.typing-indicator span:nth-child(2) {
    animation-delay: 0.2s;
}

.msg.typing-indicator span:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typingBounce {
    0%, 60%, 100% {
        transform: translateY(0);
        opacity: 0.5;
    }
    30% {
        transform: translateY(-6px);
        opacity: 1;
    }
}

/* Transição suave de typing para mensagem */
.msg.typing-transform {
    transition: width 0.3s ease, min-width 0.3s ease;
    display: block !important;
    padding: 10px 14px !important;
}

@keyframes cursorBlink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0; }
}

/* ============================================
   TECLADO iOS STYLE
   ============================================ */

#chatKeyboard {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    background: var(--glass-bg);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-top: 1px solid var(--nav-border-color);
    z-index: 99999;
    transform: translateY(100%);
    transition: transform 0.3s ease;
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    -webkit-touch-callout: none;
    /* Disable pull-to-refresh and scroll interference */
    overscroll-behavior: none;
    -webkit-overflow-scrolling: auto;
    touch-action: none;
    /* Safe area para dispositivos iOS */
    padding-bottom: env(safe-area-inset-bottom, 20px);
}

/* Mostra teclado quando tem classe open */
#chatKeyboard.open {
    transform: translateY(0);
}

/* Input Bar */
#inputBar {
    display: flex;
    align-items: center;
    padding: 8px 16px !important;
    gap: 10px;
    background: transparent;
    border: none;
    position: relative;
    z-index: 1;
}

/* Arrow Down - Setas para fechar teclado */
.arrow-down {
    min-width: 42px !important;
    max-width: 50px !important;
    height: 42px !important;
    border-radius: 8px !important;
    background: linear-gradient(180deg, #c75050 0%, #a04040 100%) !important;
    color: #fff !important;
    font-size: 14px !important;
    font-weight: bold;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
}

.arrow-down svg {
    width: 22px !important;
    height: 22px !important;
    margin: 0 !important;
    stroke: var(--text) !important;
}

/* Input Wrapper - Visual input display */
#chatInputWrapper {
    flex: 1;
    min-height: 20px;
    max-height: 120px;
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid var(--nav-border-color);
    border-radius: 18px;
    padding: 7px 14px;
    color: var(--text, #fff);
    font-size: 16px;
    overflow: hidden; /* Esconde overflow mas não corta o placeholder */
    word-wrap: break-word;
    word-break: break-word;
    line-height: 20px;
    /* Permite scroll vertical com swipe */
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
    overscroll-behavior: contain;
    /* Scrollbar minimalista */
    scrollbar-width: thin;
    scrollbar-color: rgba(255, 255, 255, 0.3) transparent;
}

#chatInputWrapper::-webkit-scrollbar {
    width: 3px;
}

#chatInputWrapper::-webkit-scrollbar-track {
    background: transparent;
}

#chatInputWrapper::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.3);
    border-radius: 2px;
}

#chatInputText {
    color: var(--text, #fff);
    font-size: 16px;
    line-height: 20px;
    font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', sans-serif;
    display: block;
    position: relative;
    width: 100%;
}

/* Conteúdo do texto */
.text-content {
    display: inline;
    white-space: pre-wrap;
    word-wrap: break-word;
    word-break: break-word;
    position: relative;
}

/* Quando vazio (placeholder visível), não quebra palavras */
.text-content.empty {
    white-space: nowrap;
    word-break: normal;
    word-wrap: normal;
}

/* Cursor piscante - posição absoluta para não afetar layout */
.text-cursor {
    position: absolute;
    width: 2px;
    height: 18px;
    background: #0b84fe;
    animation: blink-caret 1s step-end infinite;
    pointer-events: none;
}

/* Placeholder - quando texto vazio */
.text-content.empty::before {
    content: attr(data-placeholder);
    color: #8e8e93;
    position: relative;
    white-space: nowrap;
    display: inline;
}

#chatInputText[data-placeholder] .text-content.empty::before {
    content: attr(data-placeholder);
}

@keyframes blink-caret {
    0%, 50% { opacity: 1; }
    50.01%, 100% { opacity: 0; }
}

/* Hidden real input - Bloqueia teclado nativo iOS/Android */
#chatInput {
    position: absolute;
    opacity: 0;
    pointer-events: none;
    /* Bloquear teclado nativo */
    -webkit-user-select: none;
    user-select: none;
    caret-color: transparent;
    touch-action: none;
}

/* Textarea alternativo (chat-section.html) - Bloqueia teclado nativo */
textarea.chat-input {
    -webkit-user-select: none;
    user-select: none;
    caret-color: transparent;
    touch-action: none;
}

#btnSend {
    width: 42px;
    height: 42px;
    min-width: 42px;
    border-radius: 50%;
    border: none;
    background: linear-gradient(135deg, #c45c26 0%, #a04520 50%, #8b3a1a 100%);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    flex-shrink: 0;
    position: relative;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

/* Mostra o ícone SVG */
#btnSend svg {
    width: 18px;
    height: 18px;
    fill: rgba(0, 0, 0, 0.8);
    stroke: rgba(0, 0, 0, 0.8);
    position: relative;
    z-index: 1;
}

#btnSend:hover {
    transform: scale(1.05);
    background: linear-gradient(135deg, #d4692e 0%, #b35025 50%, #9b4220 100%);
}

#btnSend:active {
    transform: scale(0.95);
}

/* ============================================
   KB-SEND-BTN - Same vibrant style as #btnSend
   ============================================ */

.kb-send-btn {
    width: 42px;
    height: 42px;
    min-width: 42px;
    border-radius: 50%;
    border: none;
    background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 25%, #f79f1f 50%, #ff6b6b 75%, #ee5a24 100%);
    background-size: 300% 300%;
    animation: gradientShift 3s ease infinite;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    flex-shrink: 0;
    position: relative;
    overflow: hidden;
    box-shadow:
        0 4px 15px rgba(238, 90, 36, 0.5),
        0 8px 25px rgba(247, 159, 31, 0.3),
        inset 0 2px 4px rgba(255, 255, 255, 0.3);
}

/* Esconde o ícone SVG dentro de kb-send-btn */
.kb-send-btn svg {
    display: none;
}

/* Brilho pulsante rotativo */
.kb-send-btn::before {
    content: '';
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: conic-gradient(
        from 0deg,
        transparent 0deg,
        rgba(255, 255, 255, 0.4) 60deg,
        transparent 120deg
    );
    animation: rotateStar 2s linear infinite;
    pointer-events: none;
}

/* Glow interno */
.kb-send-btn::after {
    content: '';
    position: absolute;
    inset: 3px;
    border-radius: 50%;
    background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 50%, #f79f1f 100%);
    background-size: 200% 200%;
    animation: gradientShift 3s ease infinite;
}

.kb-send-btn:hover {
    transform: scale(1.08);
    box-shadow:
        0 6px 20px rgba(238, 90, 36, 0.6),
        0 12px 35px rgba(247, 159, 31, 0.4),
        inset 0 2px 4px rgba(255, 255, 255, 0.4);
}

.kb-send-btn:active {
    transform: scale(0.95);
    box-shadow:
        0 2px 10px rgba(238, 90, 36, 0.5),
        0 4px 15px rgba(247, 159, 31, 0.3),
        inset 0 2px 4px rgba(255, 255, 255, 0.2);
}

/* Keyboard Container */
#keyboard {
    background: transparent;
    padding: 6px 3px 8px;
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    -webkit-touch-callout: none;
    /* Disable all scroll/refresh behaviors */
    overscroll-behavior: none;
    touch-action: none;
    overflow: visible; /* Permite popup sair dos limites */
    position: relative;
    z-index: 5;
}

/* Layout */
.kb-layout {
    display: block;
}

.kb-layout.hidden {
    display: none;
}

/* Rows */
.kb-row {
    display: flex;
    justify-content: center;
    gap: 6px;
    margin-bottom: 10px;
}

.kb-row:last-child {
    margin-bottom: 0;
}

/* Keys - High Contrast Style */
.key {
    background: rgba(60, 60, 65, 0.95);
    box-shadow:
        0 1px 0 rgba(0, 0, 0, 0.8),
        0 3px 6px rgba(0, 0, 0, 0.5),
        0 6px 12px rgba(0, 0, 0, 0.3),
        inset 0 1px 0 rgba(255, 255, 255, 0.15);
    border: 1px solid rgba(255, 255, 255, 0.1);
    height: 42px;
    line-height: 42px;
    font-size: 22px;
    border-radius: 5px;
    min-width: 32px;
    flex: 1;
    max-width: 36px;
    text-align: center;
    display: inline-block;
    font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', sans-serif;
    font-weight: 400;
    color: var(--text, #fff);
    cursor: pointer;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    position: relative;
    touch-action: manipulation;
}

/* Extended touch area - invisible hit zone around each key */
.key::before {
    content: '';
    position: absolute;
    top: -8px;
    left: -4px;
    right: -4px;
    bottom: -8px;
    z-index: 1;
}

.key:active {
    background: rgba(80, 80, 85, 0.95);
}

/* ============================================
   KEY PRESS ANIMATION - iOS Style Stretch
   Usa ::before para esticar - idêntico ao documento
   ============================================ */

/* Key base - prepara para o efeito de stretch */
.key[data-key] {
    transition: none;
}

.key[data-key].touching {
    z-index: 100;
}

/* Pseudo-elemento que ESTICA para cima */
.key[data-key]::after {
    content: attr(data-key);
    position: absolute;
    left: 50%;
    bottom: 0;
    transform: translateX(-50%);
    width: 100%;
    min-width: 36px;
    height: 42px;
    background: #3a3a3c;
    color: var(--text, #fff);
    font-size: 22px;
    font-weight: 400;
    font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', sans-serif;
    border-radius: 5px;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    pointer-events: none;
    opacity: 0;
    text-transform: uppercase;
    transition:
        height 0.1s cubic-bezier(0.2, 0, 0, 1),
        background 0.1s ease,
        border-radius 0.1s ease,
        font-size 0.1s ease,
        opacity 0.05s ease;
    box-shadow:
        0 0 0 0.5px rgba(0, 0, 0, 0.2),
        0 -2px 8px rgba(0, 0, 0, 0.15),
        0 -4px 16px rgba(0, 0, 0, 0.1);
}

/* Quando toca - ESTICA para cima */
.key[data-key].touching::after {
    opacity: 1;
    height: 95px;
    /* Mesma cor do teclado/teclas */
    background: rgba(60, 60, 65, 0.98);
    /* Mesma cor da letra do teclado */
    color: var(--text, #fff);
    font-size: 32px;
    border-radius: 8px 8px 5px 5px;
    /* Letra no topo da bolha */
    align-items: flex-start;
    padding-top: 6px;
}

/* Key pressed class para compatibilidade */
.key.key-pressed {
    z-index: 100;
}

/* Fallback: popup element (se JS criar) - escondido */
.key-popup {
    display: none;
}


/* Function Keys (shift, backspace, 123, ABC, enter) */
.fn-key {
    background: rgba(45, 45, 50, 0.95);
    box-shadow:
        0 1px 0 rgba(0, 0, 0, 0.8),
        0 3px 6px rgba(0, 0, 0, 0.5),
        0 6px 12px rgba(0, 0, 0, 0.3),
        inset 0 1px 0 rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.08);
    color: var(--text, #fff);
    font-size: 16px;
    font-weight: 500;
    min-width: 42px;
    max-width: 50px;
}

.fn-key:active {
    background: rgba(65, 65, 70, 0.95);
}

.fn-key[data-action="shift"].active {
    background: rgba(75, 75, 80, 0.95);
    color: var(--text, #fff);
}

/* Space Key - Extended bottom touch area */
.space-key {
    font-size: 16px;
    font-weight: 400;
    letter-spacing: 0.5px;
}

/* Space key has larger touch area, especially below */
.space-key::before {
    top: -10px;
    left: -6px;
    right: -6px;
    bottom: -25px; /* Extra touch area below the space bar */
}

/* Enter Key - Nova linha */
.enter-key {
    background: rgba(50, 50, 55, 0.95) !important;
    font-size: 20px !important;
    font-weight: 400;
}

.enter-key:active {
    background: rgba(70, 70, 75, 0.95) !important;
}

/* Last row - all buttons together */
.kb-row:last-child {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding-left: 0;
    padding-right: 0;
}

/* 123/ABC button slightly larger */
.kb-row:last-child .fn-key[data-action="numbers"],
.kb-row:last-child .fn-key[data-action="letters"] {
    min-width: 52px;
    max-width: 60px;
}

/* Space key in last row - expanded to replace enter button */
.kb-row:last-child .space-key {
    width: 440px !important;
    flex: none;
}

/* Numbers/Symbols layout - wider keys on third row */
#numbersLayout .kb-row:nth-child(3) .key,
#symbolsLayout .kb-row:nth-child(3) .key {
    flex: 1;
    max-width: 42px;
}

#numbersLayout .kb-row:nth-child(3) .fn-key,
#symbolsLayout .kb-row:nth-child(3) .fn-key {
    max-width: 50px;
}


/* ============================================
   RESPONSIVE
   ============================================ */

@media (max-width: 480px) {
    .msg {
        max-width: 79%;
        font-size: 14px;
    }

    #chatMessages {
        padding: 12px;
        padding-top: 107.5px; /* Espaço para o header em mobile */
        bottom: 0; /* Vai até ao fundo */
        padding-bottom: calc(65px + env(safe-area-inset-bottom, 0px)); /* Espaço para a dock */
    }

    body:has(#chatKeyboard.open) #chatMessages {
        bottom: calc(248px + env(safe-area-inset-bottom, 0px)); /* Altura do teclado em mobile + safe area */
        padding-bottom: 33px; /* Espaço entre última mensagem e teclado */
    }

    #inputBar {
        padding: 6px 8px;
    }

    #chatInput {
        height: 34px;
        font-size: 15px;
    }

    #btnSend {
        width: 34px;
        height: 34px;
        min-width: 34px;
    }

    #keyboard {
        padding: 4px 2px 6px;
    }

    .kb-row {
        gap: 5px;
        margin-bottom: 8px;
    }

    .key {
        height: 40px;
        line-height: 40px;
        font-size: 20px;
        max-width: 32px;
        border-radius: 5px;
    }

    .fn-key {
        font-size: 14px;
        max-width: 45px;
    }

    .space-key {
        min-width: 185px;
        font-size: 14px;
    }
}

/* ============================================
   GLOBAL KEYBOARD - Input ativo highlight
   ============================================ */
.global-kb-active {
    /* Sem contorno visual - apenas marcação interna */
    outline: none !important;
    box-shadow: none !important;
}

/* Garantir que inputs bloqueados ainda parecem editáveis */
input[readonly].global-kb-active,
textarea[readonly].global-kb-active {
    cursor: text;
    background-color: inherit;
    opacity: 1;
    outline: none !important;
    box-shadow: none !important;
    /* Prevenir seleção de texto ao usar teclado custom */
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
}

/* ============================================
   GLOBAL KEYBOARD MODE - Sem input bar
   Quando teclado está em modo global (fora do chat)
   ============================================ */

/* Esconder input bar no modo global */
#chatKeyboard.global-mode #inputBar {
    display: none !important;
}

/* Teclado sempre à frente de tudo - z-index 99999 definido no #chatKeyboard base */

/* Linha horizontal no topo do teclado no modo global */
#chatKeyboard.global-mode::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 1px;
    background: rgba(255, 255, 255, 0.15);
}

/* Ajustar padding do teclado no modo global */
#chatKeyboard.global-mode #keyboard {
    padding-top: 12px;
}

/* ============================================
   TOKEN COUNTER - Floating Mini Box
   ============================================ */

.token-counter {
    position: fixed;
    top: 80px;
    right: 16px;
    width: 160px;
    background: rgba(20, 20, 25, 0.95);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-radius: 12px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
    z-index: 1000;
    opacity: 0;
    visibility: hidden;
    transform: scale(0.9) translateY(-10px);
    transition: opacity 0.25s ease, visibility 0.25s ease, transform 0.25s ease;
    font-family: 'SF Mono', 'Monaco', 'Consolas', monospace;
    overflow: hidden;
}

.token-counter.visible {
    opacity: 1;
    visibility: visible;
    transform: scale(1) translateY(0);
}

/* Esconde quando chat não está ativo */
body:not(:has(#view-chat.active-view)) .token-counter {
    display: none;
}

.token-counter-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 12px;
    background: rgba(255, 255, 255, 0.05);
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.token-counter-title {
    font-size: 11px;
    font-weight: 600;
    color: rgba(255, 255, 255, 0.8);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.token-counter-close {
    background: none;
    border: none;
    color: rgba(255, 255, 255, 0.5);
    font-size: 18px;
    cursor: pointer;
    padding: 0;
    line-height: 1;
    transition: color 0.2s ease;
}

.token-counter-close:hover {
    color: rgba(255, 255, 255, 0.9);
}

.token-counter-body {
    padding: 10px 12px;
}

.token-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 6px;
}

.token-row:last-child {
    margin-bottom: 0;
}

.token-label {
    font-size: 10px;
    color: rgba(255, 255, 255, 0.5);
}

.token-value {
    font-size: 11px;
    color: rgba(255, 255, 255, 0.9);
    font-weight: 500;
}

.token-row.total .token-label,
.token-row.total .token-value {
    color: #4CAF50;
    font-weight: 600;
}

.token-divider {
    height: 1px;
    background: rgba(255, 255, 255, 0.1);
    margin: 8px 0;
}

.token-row.cost .token-value {
    color: #FFB74D;
}

.token-row.session .token-value {
    color: #FF7043;
}

/* Animação de update */
.token-value.updating {
    animation: tokenPulse 0.3s ease;
}

@keyframes tokenPulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.15); color: #4CAF50; }
    100% { transform: scale(1); }
}

/* Quando teclado está aberto, move para cima */
body:has(#chatKeyboard.open) .token-counter {
    top: 20px;
}

/* Esconde o botão do mini teclado quando toggle está desativado */
body.use-native-keyboard #miniKeyboardBtn {
    display: none !important;
}

/* ============================================
   CHAT GRAYSCALE MODE (Long press no botão do chat)
   ============================================ */

body.chat-grayscale-mode #chatMessages {
    filter: grayscale(1);
    background: var(--bg-color) !important;
}

body.chat-grayscale-mode #tokenCounter,
body.chat-grayscale-mode #scrollToBottomBtn,
body.chat-grayscale-mode #miniKeyboardBtn {
    filter: grayscale(1);
}


/* ============================================
   CONVERSATION SEPARATOR (Reset Memory)
   ============================================ */

.conversation-separator {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: 20px 0;
    margin: 16px -12px; /* Compensa o padding do container */
    width: calc(100% + 24px); /* Estende para compensar margens negativas */
    box-sizing: border-box;
}

@media (min-width: 481px) {
    .conversation-separator {
        margin: 16px -16px;
        width: calc(100% + 32px);
    }
}

.conversation-separator .separator-line {
    flex: 1;
    height: 1px;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
}

.conversation-separator .separator-text {
    font-size: 11px;
    color: rgba(255, 255, 255, 0.5);
    text-transform: uppercase;
    letter-spacing: 1px;
    padding: 4px 12px;
    background: rgba(255, 255, 255, 0.08);
    border-radius: 12px;
    white-space: nowrap;
}

/* Separator clickable state - cancel new conversation */
.conversation-separator {
    cursor: pointer;
    transition: opacity 0.2s ease;
}

.conversation-separator:active {
    opacity: 0.5;
}

.conversation-separator .separator-text {
    transition: background 0.2s ease, color 0.2s ease;
}

.conversation-separator:active .separator-text {
    background: rgba(255, 100, 100, 0.2);
    color: rgba(255, 150, 150, 0.8);
}
