:root {
    --chat-bg: #ece5dd;
    --header-green: #075e54;
    --primary-green: #128c7e;
    --text-dark: #303030;
    --text-white: #ffffff;
    --msg-bot: #ffffff;
    --msg-user: #d9fdd3;
    --font-jakarta: 'Plus Jakarta Sans', sans-serif;
    /* Safe area insets for notched phones (iPhone X+, etc.) */
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    /* Prevent blue highlight on tap (Android/iOS) */
    -webkit-tap-highlight-color: transparent;
}

html {
    /* Prevent font-size boosting on orientation change */
    -webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    background-color: #0f172a;
    font-family: var(--font-jakarta);
    min-height: 100vh;
    /* Fix iOS 100vh bug — use dynamic viewport height */
    min-height: 100dvh;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0;
    margin: 0;
    /* Prevent overscroll bounce on iOS */
    overscroll-behavior: none;
    overflow: hidden;
    /* Smoother font rendering on mobile */
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ==============================
   Chat Container Frame
   ============================== */
.chat-wrapper {
    background-color: var(--chat-bg);
    width: 100%;
    max-width: 450px;
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 12px 30px rgba(0, 0, 0, 0.4);
    position: relative;
    overflow: hidden;
    /* GPU-accelerated compositing for smoother scrolling */
    will-change: transform;
    transform: translateZ(0);
}

/* Desktop: card-like appearance */
@media (min-width: 451px) {
    .chat-wrapper {
        border-radius: 24px;
        height: 90vh;
        max-height: 800px;
    }
}

/* ==============================
   Chat Header
   ============================== */
.chat-header {
    background-color: var(--header-green);
    color: var(--text-white);
    padding: 12px 16px;
    /* Safe area for notched phones */
    padding-top: calc(12px + var(--safe-top));
    padding-left: calc(16px + var(--safe-left));
    padding-right: calc(16px + var(--safe-right));
    display: flex;
    align-items: center;
    border-bottom: 1px solid rgba(0, 0, 0, 0.08);
    /* Prevent header from shrinking */
    flex-shrink: 0;
    min-height: 60px;
    z-index: 10;
}

.chat-user {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
}

.avatar {
    width: 44px;
    height: 44px;
    min-width: 44px;
    min-height: 44px;
    background-color: rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 22px;
    overflow: hidden;
    /* Crisp rendering for photo */
    image-rendering: auto;
}

.user-meta {
    display: flex;
    flex-direction: column;
    min-width: 0; /* Allow text truncation */
}

.user-name {
    font-weight: 700;
    font-size: 15px;
    /* Prevent long names from breaking layout */
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.user-status {
    font-size: 11px;
    opacity: 0.85;
    font-weight: 500;
}

/* ==============================
   Message Field (scrollable area)
   ============================== */
.chat-field {
    flex: 1 1 0%;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 16px 12px;
    padding-left: calc(12px + var(--safe-left));
    padding-right: calc(12px + var(--safe-right));
    display: flex;
    flex-direction: column;
    gap: 10px;
    scroll-behavior: smooth;
    /* Momentum scrolling on iOS */
    -webkit-overflow-scrolling: touch;
    /* Prevent pull-to-refresh */
    overscroll-behavior-y: contain;
    /* GPU acceleration for smooth scroll */
    transform: translateZ(0);
}

/* Hide scrollbar on mobile for cleaner look */
.chat-field::-webkit-scrollbar {
    width: 4px;
}

.chat-field::-webkit-scrollbar-thumb {
    background-color: rgba(0, 0, 0, 0.1);
    border-radius: 4px;
}

/* Tiny screens: hide scrollbar entirely */
@media (max-width: 380px) {
    .chat-field::-webkit-scrollbar {
        width: 0;
        display: none;
    }
    .chat-field {
        scrollbar-width: none;
    }
}

/* ==============================
   Speech Bubbles
   ============================== */
.bubble {
    max-width: 85%;
    padding: 10px 14px;
    border-radius: 14px;
    font-size: 14px;
    line-height: 1.55;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
    animation: bubblePop 0.3s cubic-bezier(0.1, 0.8, 0.3, 1) forwards;
    /* Prevent text from overflowing */
    word-wrap: break-word;
    overflow-wrap: break-word;
    hyphens: auto;
}

@keyframes bubblePop {
    0% { transform: scale(0.92); opacity: 0; }
    100% { transform: scale(1); opacity: 1; }
}

.bot-msg {
    background-color: var(--msg-bot);
    color: var(--text-dark);
    align-self: flex-start;
    border-top-left-radius: 4px;
}

.user-msg {
    background-color: var(--msg-user);
    color: var(--text-dark);
    align-self: flex-end;
    border-top-right-radius: 4px;
}

/* Small phones: slightly smaller bubbles */
@media (max-width: 360px) {
    .bubble {
        max-width: 90%;
        padding: 9px 12px;
        font-size: 13.5px;
    }
}

/* Larger phones */
@media (min-width: 390px) {
    .bubble {
        max-width: 82%;
        padding: 12px 16px;
        font-size: 14.5px;
    }
}

/* ==============================
   Chat Footer / Input Actions
   ============================== */
.chat-footer {
    background-color: #f0f0f0;
    border-top: 1px solid rgba(0, 0, 0, 0.08);
    padding: 12px 16px;
    padding-left: calc(16px + var(--safe-left));
    padding-right: calc(16px + var(--safe-right));
    padding-bottom: calc(12px + var(--safe-bottom));
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-height: 70px;
    justify-content: center;
    /* Prevent footer from shrinking */
    flex-shrink: 0;
    z-index: 10;
}

/* ==============================
   Option Buttons (touch-optimized)
   ============================== */
.chat-btn {
    background-color: #ffffff;
    border: 1.5px solid rgba(0, 0, 0, 0.08);
    color: #075e54;
    /* Min 48px touch target (Google recommendation) */
    padding: 14px 20px;
    min-height: 48px;
    border-radius: 14px;
    font-family: var(--font-jakarta);
    font-size: 15px;
    font-weight: 700;
    cursor: pointer;
    text-align: center;
    transition: background-color 0.15s ease, transform 0.1s ease, border-color 0.15s ease;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.03);
    /* Prevent text selection on long press */
    -webkit-user-select: none;
    user-select: none;
    /* Touch action optimization */
    touch-action: manipulation;
}

/* Hover only on devices that support it (not touch) */
@media (hover: hover) and (pointer: fine) {
    .chat-btn:hover {
        background-color: #f7f9fa;
        border-color: #075e54;
        transform: translateY(-1px);
    }
}

.chat-btn:active {
    transform: scale(0.97);
    background-color: #eef5f4;
    border-color: #075e54;
}

/* Small phones */
@media (max-width: 360px) {
    .chat-btn {
        padding: 12px 16px;
        font-size: 14px;
        border-radius: 12px;
    }
}

/* ==============================
   Chatbot Typing Loader
   ============================== */
.typing-indicator {
    background-color: var(--msg-bot);
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 12px 18px;
    align-self: flex-start;
    border-radius: 14px;
    border-top-left-radius: 4px;
    width: 60px;
    height: 38px;
    box-sizing: border-box;
}

.typing-indicator span {
    width: 6px;
    height: 6px;
    background-color: rgba(0, 0, 0, 0.4);
    border-radius: 50%;
    display: inline-block;
    animation: typingBounce 1.2s infinite ease-in-out;
}

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

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

@keyframes typingBounce {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-5px); }
}

/* ==============================
   CTA Button (Telegram link)
   ============================== */
.btn-cta-chat {
    background-color: #0088cc;
    color: #ffffff;
    width: 100%;
    /* Min 56px for prominent CTA tap target */
    min-height: 56px;
    border-radius: 14px;
    font-weight: 800;
    font-size: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    text-decoration: none;
    margin-top: 14px;
    box-shadow: 0 4px 12px rgba(0, 136, 204, 0.25);
    transition: background-color 0.2s ease, transform 0.15s ease;
    align-self: center;
    /* Prevent text selection on long press */
    -webkit-user-select: none;
    user-select: none;
    touch-action: manipulation;
}

@media (hover: hover) and (pointer: fine) {
    .btn-cta-chat:hover {
        background-color: #0099e0;
    }
}

.btn-cta-chat:active {
    transform: scale(0.97);
    background-color: #007ab8;
}

.tg-mini-svg {
    width: 22px;
    height: 22px;
    flex-shrink: 0;
}

.btn-pulse-chat {
    animation: chatPulse 2.2s infinite;
}

@keyframes chatPulse {
    0% { transform: scale(1); box-shadow: 0 4px 12px rgba(0, 136, 204, 0.25); }
    50% { transform: scale(1.02); box-shadow: 0 6px 18px rgba(0, 136, 204, 0.5); }
    100% { transform: scale(1); box-shadow: 0 4px 12px rgba(0, 136, 204, 0.25); }
}

.chat-note {
    text-align: center;
    font-size: 11px;
    color: rgba(0, 0, 0, 0.4);
    font-weight: 600;
    margin-top: 8px;
    align-self: center;
}

/* ==============================
   MOBILE-SPECIFIC BREAKPOINTS
   ============================== */

/* Very small phones (iPhone SE, Galaxy A01, etc.) */
@media (max-width: 340px) {
    .chat-header {
        padding: 10px 10px;
        padding-top: calc(10px + var(--safe-top));
        min-height: 52px;
    }
    .avatar {
        width: 38px;
        height: 38px;
        min-width: 38px;
        min-height: 38px;
    }
    .user-name { font-size: 13.5px; }
    .user-status { font-size: 10px; }
    .chat-field {
        padding: 12px 8px;
        gap: 8px;
    }
    .chat-footer {
        padding: 10px 10px;
        padding-bottom: calc(10px + var(--safe-bottom));
        min-height: 60px;
        gap: 6px;
    }
    .btn-cta-chat {
        min-height: 50px;
        font-size: 14.5px;
        border-radius: 12px;
    }
}

/* Standard phones (375px — iPhone 6/7/8/12 mini) */
@media (min-width: 341px) and (max-width: 414px) {
    .chat-field {
        padding: 16px 14px;
    }
}

/* Large phones (iPhone Pro Max, Galaxy S Ultra, etc.) */
@media (min-width: 415px) and (max-width: 450px) {
    .chat-field {
        padding: 20px 16px;
        gap: 12px;
    }
}

/* Landscape mobile — keep usable */
@media (max-height: 500px) and (orientation: landscape) {
    .chat-header {
        padding-top: 8px;
        padding-bottom: 8px;
        min-height: 48px;
    }
    .avatar {
        width: 36px;
        height: 36px;
        min-width: 36px;
        min-height: 36px;
    }
    .chat-field {
        padding: 8px 12px;
        gap: 6px;
    }
    .bubble {
        padding: 8px 12px;
        font-size: 13px;
    }
    .chat-footer {
        padding: 8px 12px;
        min-height: 50px;
    }
    .chat-btn {
        padding: 10px 16px;
        min-height: 42px;
        font-size: 13.5px;
    }
    .btn-cta-chat {
        min-height: 46px;
        font-size: 14px;
    }
}

/* ==============================
   PERFORMANCE & ACCESSIBILITY
   ============================== */

/* Reduce animations for users who prefer reduced motion */
@media (prefers-reduced-motion: reduce) {
    .bubble {
        animation: none;
    }
    .btn-pulse-chat {
        animation: none;
    }
    .typing-indicator span {
        animation: none;
    }
}

/* Dark mode support for system-level dark theme */
@media (prefers-color-scheme: dark) {
    body {
        background-color: #0a0e1a;
    }
}

/* Focus-visible for keyboard navigation (accessibility) */
.chat-btn:focus-visible,
.btn-cta-chat:focus-visible {
    outline: 2px solid #128c7e;
    outline-offset: 2px;
}
