/* =============================================================================
   call_screen.css — Tela de chamada de voz responsiva (iPhone/Safari incluído)
   -----------------------------------------------------------------------------
   • usa 100dvh + visualViewport (--call-vh) para não ficar atrás da barra do Safari
   • respeita as safe areas (notch e barra inferior do iPhone)
   • rodapé e controles fazem parte do fluxo flex (não ficam por baixo do browser)
   • mini chat moderno, com bolhas, avatares, estados vazios e animações suaves
   • botão do microfone sempre no mesmo lugar: cinza quando ocupado, vermelho
     quando está pronto a ouvir
   ============================================================================= */

#background-calls {
    inset: 0;
    width: 100%;
    height: 0%;
    display: none;
    align-items: center;
    justify-content: center;
    position: fixed;
    z-index: 6000;
    overflow: hidden;
    box-sizing: border-box;
    padding: calc(6px + env(safe-area-inset-top, 0px)) 10px 6px;
    background: rgba(1, 1, 1, .72);
    -webkit-backdrop-filter: blur(35px);
    backdrop-filter: blur(35px);
    -webkit-overflow-scrolling: touch;
    touch-action: manipulation;
}

#background-calls.is-open {
    display: flex !important;
    height: 100% !important;
    height: var(--call-vh, 100dvh) !important;
    min-height: 100%;
    min-height: var(--call-vh, 100dvh);
    max-height: 100%;
    max-height: var(--call-vh, 100dvh);
}

body.call-active {
    overflow: hidden !important;
    overscroll-behavior: none;
}

#call-screen {
    position: relative;
    width: 100%;
    max-width: 430px;
    height: 100%;
    max-height: 100%;
    min-height: 0;
    margin: 0 auto;
    padding: 2px 4px 0;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    text-align: center;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
    scrollbar-width: none;
}
#call-screen::-webkit-scrollbar { display: none; }

/* ── Topo ────────────────────────────────────────────────────────────────── */
.call-hero {
    flex: 0 0 auto;
    width: 100%;
    margin-top: 2px !important;
    position: relative;
    z-index: 2;
}

.call-avatar {
    width: clamp(74px, 18vw, 102px);
    height: clamp(74px, 18vw, 102px);
    margin: 2px auto 0;
    display: block;
    border: 3px solid rgba(255, 255, 255, .16);
    border-radius: 50%;
    background-color: #24262b;
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    box-shadow:
        0 0 0 7px rgba(246, 117, 3, .10),
        0 18px 44px rgba(0, 0, 0, .48);
    cursor: default;
    -webkit-tap-highlight-color: transparent;
}

.call-log {
    font-size: clamp(17px, 4.6vw, 21px) !important;
    line-height: 1.2;
    font-weight: 800 !important;
    letter-spacing: -.02em;
    margin: 11px 10px 3px !important;
    color: #fff !important;
    text-shadow: 0 2px 14px rgba(0, 0, 0, .3);
}

.call-timer {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 68px;
    padding: 3px 10px;
    border-radius: 999px;
    background: rgba(255, 255, 255, .07);
    border: 1px solid rgba(255, 255, 255, .10);
    font-size: 12px !important;
    font-weight: 700 !important;
    color: #d8dce5 !important;
    font-variant-numeric: tabular-nums;
    letter-spacing: .04em;
}

#call_lang_switch_btn {
    margin: 6px auto 0 !important;
    max-width: 100%;
    min-height: 28px;
    padding: 4px 11px 4px 8px !important;
    gap: 6px;
    white-space: nowrap;
    overflow: hidden;
}
#call_lang_switch_name {
    max-width: 130px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Microfone: ocupa sempre o mesmo espaço, só muda de estado. */
#reactiveMic {
    display: flex !important;
    align-items: center;
    justify-content: center;
    width: clamp(48px, 13vw, 56px);
    height: clamp(48px, 13vw, 56px);
    margin: 7px auto 5px !important;
    padding: 0;
    flex: 0 0 auto;
    border: 0;
    border-radius: 50%;
    background-color: #3f4650;
    background-image: url('../img/icons/microphone_60px.png');
    background-position: center;
    background-size: 42% 42%;
    background-repeat: no-repeat;
    box-shadow: 0 8px 22px rgba(0, 0, 0, .30);
    cursor: default;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transform: scale(.94);
    transition:
        opacity .18s ease,
        visibility .18s ease,
        transform .18s ease,
        background-color .18s ease,
        box-shadow .18s ease;
    -webkit-tap-highlight-color: transparent;
}
#reactiveMic.is-visible {
    opacity: 1;
    visibility: visible;
    transform: scale(1);
}
#reactiveMic.is-listening,
#reactiveMic.is-processing {
    background-color: #4b5563;
    box-shadow: 0 8px 22px rgba(0, 0, 0, .28);
    cursor: default;
    pointer-events: none;
}
#reactiveMic.is-available {
    background-color: #dc2626;
    cursor: pointer;
    pointer-events: auto;
    animation: callMicReady 2.2s ease-out infinite;
}
#reactiveMic.is-available:hover { transform: scale(1.04); }
#reactiveMic.is-available:active { transform: scale(.97); }
#reactiveMic:disabled { opacity: 1; }

@keyframes callMicReady {
    0%   { box-shadow: 0 0 0 0 rgba(220, 38, 38, .55), 0 8px 22px rgba(0, 0, 0, .3); }
    55%  { box-shadow: 0 0 0 15px rgba(220, 38, 38, 0), 0 10px 26px rgba(0, 0, 0, .34); }
    100% { box-shadow: 0 0 0 0 rgba(220, 38, 38, 0), 0 8px 22px rgba(0, 0, 0, .3); }
}

#feedback-call-phases {
    min-height: 16px;
    max-width: 92%;
    margin: 1px auto 0;
    padding: 0 8px;
    font-size: 10.5px;
    font-weight: 600;
    line-height: 1.35;
    color: rgba(255, 255, 255, .66);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* ── Mini chat ───────────────────────────────────────────────────────────── */
.hib-chat {
    flex: 1 1 auto;
    min-height: 0;
    max-height: none !important;
    width: 100%;
    max-width: 390px;
    margin: 10px auto 8px !important;
    padding: 13px 12px !important;
    display: none;
    flex-direction: column;
    gap: 10px !important;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    scrollbar-color: rgba(246, 117, 3, .45) transparent;
    box-sizing: border-box;
    border: 1px solid rgba(255, 255, 255, .09);
    border-radius: 22px !important;
    background:
        linear-gradient(180deg, rgba(31, 33, 39, .80), rgba(18, 20, 26, .82)) !important;
    -webkit-backdrop-filter: blur(18px);
    backdrop-filter: blur(18px);
    box-shadow:
        0 18px 50px rgba(0, 0, 0, .28),
        inset 0 1px 0 rgba(255, 255, 255, .05);
}
.hib-chat::-webkit-scrollbar { width: 3px; }
.hib-chat::-webkit-scrollbar-thumb { background: rgba(246, 117, 3, .42); border-radius: 99px; }

.hib-chat:empty::before {
    content: "A conversa aparece aqui.\A Fale ou escreva para começar.";
    white-space: pre-line;
    margin: auto;
    padding: 24px 18px;
    max-width: 250px;
    color: rgba(255, 255, 255, .38);
    font-size: 11.5px;
    font-weight: 600;
    line-height: 1.65;
    text-align: center;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='34' height='34' viewBox='0 0 24 24' fill='none' stroke='%23f67503' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round' opacity='.5'%3E%3Cpath d='M21 15a4 4 0 0 1-4 4H8l-5 3V7a4 4 0 0 1 4-4h10a4 4 0 0 1 4 4z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: center top;
    padding-top: 54px;
}

.hib-msg {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    width: 100%;
    animation: hibMsgIn .22s ease-out both;
}
.hib-msg--ai { justify-content: flex-start; }
.hib-msg--user { justify-content: flex-end; }

.hib-msg-avatar {
    width: 30px;
    height: 30px;
    flex: 0 0 30px;
    border-radius: 50%;
    background-color: #2f3238;
    background-size: cover;
    background-position: center;
    border: 1px solid rgba(255, 255, 255, .10);
    box-shadow: 0 4px 12px rgba(0, 0, 0, .24);
}

.hib-msg-body {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    max-width: calc(100% - 42px);
    min-width: 0;
}
.hib-msg--user .hib-msg-body { align-items: flex-end; }

.hib-bubble {
    max-width: 100%;
    padding: 9px 12px;
    border-radius: 16px;
    font-size: 12.5px;
    line-height: 1.45;
    text-align: left;
    overflow-wrap: anywhere;
    word-break: break-word;
    white-space: pre-wrap;
    box-shadow: 0 5px 16px rgba(0, 0, 0, .18);
}
.hib-bubble--ai {
    color: #e8eaf0;
    background: linear-gradient(145deg, rgba(58, 62, 72, .92), rgba(38, 41, 49, .92));
    border: 1px solid rgba(255, 255, 255, .07);
    border-bottom-left-radius: 6px;
}
.hib-bubble--user {
    color: #fff;
    background: linear-gradient(135deg, #f67503, #ff9a44);
    border: 1px solid rgba(255, 255, 255, .12);
    border-bottom-right-radius: 6px;
    box-shadow: 0 7px 20px rgba(246, 117, 3, .28);
}
.hib-bubble b, .hib-bubble strong { font-weight: 800; }

.hib-msg-time {
    margin-top: 3px;
    font-size: 8.5px;
    line-height: 1;
    color: rgba(255, 255, 255, .35);
    font-weight: 600;
}
.hib-msg--user .hib-msg-time { text-align: right; }

.hib-typing-row {
    display: flex;
    align-items: center;
    gap: 8px;
    animation: hibMsgIn .18s ease-out both;
}
.hib-typing-dots {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 10px 13px;
    border-radius: 16px 16px 16px 6px;
    background: rgba(58, 62, 72, .9);
    border: 1px solid rgba(255, 255, 255, .07);
}
.hib-typing-dots span {
    width: 5px;
    height: 5px;
    border-radius: 50%;
    background: #ffb37a;
    animation: hibTyping 1.1s ease-in-out infinite;
}
.hib-typing-dots span:nth-child(2) { animation-delay: .15s; }
.hib-typing-dots span:nth-child(3) { animation-delay: .3s; }

@keyframes hibMsgIn {
    from { opacity: 0; transform: translateY(5px) scale(.985); }
    to   { opacity: 1; transform: none; }
}
@keyframes hibTyping {
    0%, 60%, 100% { transform: translateY(0); opacity: .45; }
    30% { transform: translateY(-3px); opacity: 1; }
}

/* ── Rodapé e controles ──────────────────────────────────────────────────── */
.call-footer {
    position: relative !important;
    bottom: auto !important;
    left: auto !important;
    right: auto !important;
    flex: 0 0 auto;
    width: 100%;
    max-height: none !important;
    margin: auto auto 0 !important;
    padding: 6px 2px calc(40px + env(safe-area-inset-bottom, 0px)) !important;
    gap: 8px !important;
    box-sizing: border-box;
    z-index: 3;
}

.hib-input-area {
    width: 100% !important;
    max-width: 390px !important;
    min-height: 44px;
    margin: 0 auto !important;
    padding: 5px 6px 5px 14px !important;
    border-radius: 16px !important;
    border: 1px solid rgba(255, 255, 255, .12) !important;
    background: rgba(24, 26, 31, .94) !important;
    box-shadow: 0 8px 22px rgba(0, 0, 0, .24);
    box-sizing: border-box;
}
#hib_chat_input {
    min-width: 0;
    font-size: 16px !important;
    line-height: 1.35;
}
#hib_chat_input::placeholder { color: rgba(255, 255, 255, .36); }
#hib_send_msg_btn {
    width: 34px !important;
    height: 34px !important;
    flex: 0 0 34px;
    background: linear-gradient(135deg, #f67503, #ff9a44) !important;
    box-shadow: 0 5px 14px rgba(246, 117, 3, .30);
    transition: transform .16s ease;
}
#hib_send_msg_btn:active { transform: scale(.94); }

#call_screen_buttons_container {
    width: 100%;
    margin: 0 auto !important;
    padding: 0 2px;
    box-sizing: border-box;
    position: relative;
    z-index: 2;
}
#call_controls_panel {
    display: grid !important;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    align-items: center;
    justify-items: center;
    gap: 7px !important;
    width: 100% !important;
    max-width: 380px;
    margin: 0 auto !important;
    padding: 9px 10px !important;
    border-radius: 26px !important;
    background: linear-gradient(180deg, rgba(42, 45, 53, .86), rgba(24, 26, 32, .92)) !important;
    border: 1px solid rgba(255, 255, 255, .12) !important;
    box-shadow:
        0 14px 38px rgba(0, 0, 0, .38),
        inset 0 1px 0 rgba(255, 255, 255, .06);
    -webkit-backdrop-filter: blur(22px);
    backdrop-filter: blur(22px);
    box-sizing: border-box;
}

.call-control {
    width: 45px !important;
    height: 45px !important;
    max-width: 50px !important;
    min-width: 0;
    margin: 0 auto;
    border: 0;
    border-radius: 50% !important;
    background-color: #2d3138 !important;
    background-size: 48% 48%;
    background-position: center;
    background-repeat: no-repeat;
    box-shadow:
        0 4px 12px rgba(0, 0, 0, .28),
        inset 0 1px 0 rgba(255, 255, 255, .06);
    outline: 1px solid rgba(255, 255, 255, .07);
    outline-offset: -1px;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: transform .14s ease, filter .14s ease, opacity .14s ease;
}
.call-control:hover { filter: brightness(1.14); }
.call-control:active { transform: scale(.94); }
.call-control--mode { background-color: #dc2626 !important; background-size: 76% 76% !important; }
.call-control--chat { background-color: #f67503 !important; }
.call-control--end { background-color: #dc2626 !important; background-size: 76% 76% !important; }

.call-wave {
    height: 34px !important;
    bottom: calc(2px + env(safe-area-inset-bottom, 0px)) !important;
    left: 0;
    opacity: .35;
    z-index: 0;
}

/* ── Seletor/overlay de idioma ───────────────────────────────────────────── */
#call_lang_chooser {
    width: min(100%, 360px);
    max-height: calc(var(--call-vh, 100dvh) - 220px);
    margin: 10px auto 0;
    padding-bottom: 4px;
}
#call_lang_chooser .call-lang-grid { max-height: min(40vh, 310px); }
#call_lang_switcher {
    padding-top: calc(14px + env(safe-area-inset-top, 0px)) !important;
    padding-bottom: calc(14px + env(safe-area-inset-bottom, 0px)) !important;
}
#call_lang_switcher .switcher-card {
    max-height: calc(var(--call-vh, 100dvh) - 60px);
    overflow-y: auto;
}

/* ── Barra de chamada recebida ───────────────────────────────────────────── */
#call_alert {
    top: 0 !important;
    left: 0 !important;
    width: 100% !important;
    height: auto !important;
    min-height: 72px;
    padding: calc(8px + env(safe-area-inset-top, 0px)) 14px 10px !important;
    border-radius: 0 0 18px 18px !important;
    box-sizing: border-box;
}

/* ── Ecrãs pequenos/curtos ───────────────────────────────────────────────── */
@media (max-width: 370px) {
    .call-avatar { width: 68px; height: 68px; }
    #call_screen_buttons_container { padding: 0; }
    #call_controls_panel { gap: 5px !important; padding: 8px 8px !important; }
    .call-control { width: 42px !important; height: 42px !important; max-width: 44px !important; }
    .call-log { font-size: 16px !important; }
    #reactiveMic { width: 46px; height: 46px; }
}

@media (max-height: 700px) {
    .call-avatar { width: 72px; height: 72px; }
    .call-log { margin-top: 8px !important; font-size: 17px !important; }
    #reactiveMic { width: 46px; height: 46px; margin: 5px auto 4px !important; }
    .hib-chat { margin-top: 7px !important; margin-bottom: 6px !important; padding: 10px !important; gap: 8px !important; }
    .call-footer { gap: 6px !important; padding-bottom: calc(24px + env(safe-area-inset-bottom, 0px)) !important; }
    .call-wave { height: 24px !important; }
    #call_lang_chooser { max-height: calc(var(--call-vh, 100dvh) - 150px); }
}

@media (max-height: 560px) and (orientation: portrait) {
    .call-avatar { width: 56px; height: 56px; }
    .call-log { font-size: 15px !important; margin-top: 5px !important; }
    .call-timer { font-size: 10.5px !important; }
    #call_lang_switch_btn { display: none; }
    #feedback-call-phases { font-size: 9.5px; }
    #reactiveMic { width: 42px; height: 42px; margin: 3px auto 2px !important; }
    .call-control { width: 40px !important; height: 40px !important; max-width: 40px !important; }
    #call_controls_panel { padding: 7px 7px !important; gap: 4px !important; }
    .hib-chat { min-height: 0; }
}

@media (orientation: landscape) and (max-height: 540px) {
    #background-calls { padding-top: calc(3px + env(safe-area-inset-top, 0px)); padding-bottom: calc(3px + env(safe-area-inset-bottom, 0px)); }
    #call-screen { max-width: 760px; overflow-y: auto; }
    .call-hero { display: grid; grid-template-columns: 58px 1fr; grid-template-rows: auto auto; column-gap: 12px; align-items: center; text-align: left; }
    .call-avatar { grid-row: 1 / 3; width: 52px; height: 52px; margin: 0; }
    .call-log { margin: 0 !important; text-align: left; font-size: 15px !important; }
    .call-timer { margin: 1px 0 0; text-align: left; }
    .call-lang-title, .call-lang-subtitle { display: none; }
    #call_lang_chooser { max-height: calc(var(--call-vh, 100dvh) - 82px); margin-top: 2px; }
    #call_lang_chooser .call-lang-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); max-height: calc(var(--call-vh, 100dvh) - 138px); }
    #reactiveMic { width: 38px; height: 38px; margin: 2px auto !important; }
    .hib-chat { max-height: 120px; margin: 4px auto !important; }
    .call-footer { padding-top: 2px !important; }
    #call_controls_panel { max-width: 400px; padding: 6px 8px !important; }
    .call-control { width: 38px !important; height: 38px !important; max-width: 38px !important; }
}

@media (prefers-reduced-motion: reduce) {
    #reactiveMic, .hib-msg, .hib-typing-row, .hib-typing-dots span {
        animation: none !important;
        transition: none !important;
    }
}
