/* ============================================
   CHAT WIDGET - ENHANCED STYLING
   ============================================ */

/* Chat Widget Container */
.chat-widget {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 10002 !important; /* Higher than mobile nav (9999) - using !important to ensure it's above */
    font-family: 'Poppins', -apple-system, BlinkMacSystemFont, 'Segoe UI',
        Roboto, 'Helvetica Neue', Arial, sans-serif;
}

/* Chat Toggle Button */
.chat-toggle-btn {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    background: var(
        --gradient-primary,
        linear-gradient(135deg, #62411e 0%, #442d15 100%)
    );
    border: none;
    color: var(--text-on-primary, #ffffff);
    font-size: 26px;
    cursor: pointer;
    box-shadow: var(--shadow-lg, 0 8px 30px rgba(98, 65, 30, 0.12));
    transition: var(--transition, all 0.3s cubic-bezier(0.4, 0, 0.2, 1));
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10004; /* Button should be above window when closed */
}

.chat-toggle-btn:hover {
    transform: translateY(-4px) scale(1.05);
    box-shadow: var(--shadow-xl, 0 12px 40px rgba(98, 65, 30, 0.15));
    background: var(
        --gradient-light,
        linear-gradient(135deg, #8b6337 0%, #62411e 100%)
    );
}

.chat-toggle-btn:active {
    transform: translateY(-2px) scale(1.02);
}

.chat-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    background: #dc3545;
    color: white;
    border-radius: 50%;
    width: 24px;
    height: 24px;
    font-size: 11px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid white;
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%,
    100% {
        transform: scale(1);
    }

    50% {
        transform: scale(1.1);
    }
}

/* Chat Window */
.chat-window {
    position: absolute;
    bottom: 84px; /* Sejajar dengan tombol (64px button + 20px gap) */
    right: 0;
    width: 400px;
    max-width: calc(100vw - 40px);
    height: 650px;
    max-height: calc(100vh - 120px);
    background: white;
    border-radius: 16px;
    box-shadow: 0 12px 48px rgba(0, 0, 0, 0.15);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: slideUp 0.3s ease-out;
    z-index: 10003; /* Ensure window is above button */
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Chat Header */
.chat-header {
    background: var(
        --gradient-primary,
        linear-gradient(135deg, #62411e 0%, #442d15 100%)
    );
    color: var(--text-on-primary, #ffffff);
    padding: 20px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    flex-shrink: 0; /* Prevent header from shrinking */
}

.chat-header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.chat-header-info {
    display: flex;
    align-items: center;
    gap: 12px;
    flex: 1;
}

.chat-header-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    backdrop-filter: blur(10px);
}

.chat-header-text {
    flex: 1;
}

.chat-title {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
    line-height: 1.3;
}

.chat-subtitle {
    margin: 4px 0 0 0;
    font-size: 12px;
    opacity: 0.9;
    font-weight: 400;
}

.chat-close-btn {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: white;
    font-size: 18px;
    cursor: pointer;
    padding: 8px;
    border-radius: 8px;
    transition: all 0.2s;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.chat-close-btn:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: rotate(90deg);
}

/* Chat Body */
.chat-body {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 20px;
    background: #f8f9fa;
    scroll-behavior: smooth;
    min-height: 0; /* Important for flex children */
    display: flex;
    flex-direction: column;
    align-items: stretch; /* Ensure body doesn't push content up */
    justify-content: flex-start;
}

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

.chat-body::-webkit-scrollbar-track {
    background: #f1f1f1;
}

.chat-body::-webkit-scrollbar-thumb {
    background: #c1c1c1;
    border-radius: 3px;
}

.chat-body::-webkit-scrollbar-thumb:hover {
    background: #a8a8a8;
}

/* Initial Form */
.chat-initial-form {
    padding: 0;
    width: 100%;
}

.chat-form-header {
    margin-bottom: 20px;
    margin-top: 0;
    text-align: center;
    padding-top: 0;
    padding-bottom: 16px;
    border-bottom: 2px solid #e9ecef;
}

.chat-form-icon {
    width: 56px;
    height: 56px;
    margin: 0 auto 10px;
    border-radius: 50%;
    background: var(
        --gradient-primary,
        linear-gradient(135deg, #62411e 0%, #442d15 100%)
    );
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-on-primary, #ffffff);
    font-size: 24px;
    box-shadow: var(--shadow, 0 2px 10px rgba(98, 65, 30, 0.08));
}

.chat-form-header h6 {
    margin: 0 0 6px 0;
    color: #333;
    font-weight: 600;
    font-size: 15px;
    line-height: 1.4;
}

.chat-form-header p {
    margin: 0;
    font-size: 12px;
    color: #6c757d;
    line-height: 1.4;
}

.chat-initial-form .form-group {
    margin-bottom: 16px;
}

.chat-initial-form .form-label {
    font-size: 14px;
    font-weight: 500;
    margin-bottom: 8px;
    color: #333;
    display: flex;
    align-items: center;
    gap: 6px;
}

.chat-initial-form .form-label i {
    color: var(--primary, #62411e);
    font-size: 14px;
}

.chat-initial-form .form-control {
    font-size: 14px;
    padding: 12px 16px;
    border: 2px solid #e0e0e0;
    border-radius: 8px;
    transition: all 0.3s;
    background: white;
}

.chat-initial-form .form-control:focus {
    border-color: var(--primary, #62411e);
    box-shadow: 0 0 0 3px rgba(98, 65, 30, 0.1);
    outline: none;
}

.chat-initial-form .form-control::placeholder {
    color: #adb5bd;
}

.chat-initial-form .form-text {
    font-size: 12px;
    margin-top: 4px;
    display: block;
}

.chat-initial-form .invalid-feedback {
    display: none;
    font-size: 12px;
    color: #dc3545;
    margin-top: 4px;
}

.chat-initial-form .form-control.is-invalid {
    border-color: #dc3545;
}

.chat-initial-form .form-control.is-invalid ~ .invalid-feedback {
    display: block;
}

.btn-chat-submit {
    background: var(
        --gradient-primary,
        linear-gradient(135deg, #62411e 0%, #442d15 100%)
    );
    border: none;
    padding: 14px 24px;
    font-weight: var(--font-extra-bold, 800);
    font-size: 15px;
    border-radius: 8px;
    margin-top: 8px;
    transition: var(--transition, all 0.3s cubic-bezier(0.4, 0, 0.2, 1));
    box-shadow: var(--shadow, 0 2px 10px rgba(98, 65, 30, 0.08));
    text-transform: none;
    letter-spacing: 0.3px;
    color: var(--text-on-primary, #ffffff);
    font-family: var(--font-primary, 'Poppins', sans-serif);
    width: 100%;
}

.btn-chat-submit:hover {
    background: var(
        --gradient-light,
        linear-gradient(135deg, #8b6337 0%, #62411e 100%)
    );
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg, 0 8px 30px rgba(98, 65, 30, 0.12));
}

.btn-chat-submit:active {
    transform: translateY(0);
}

.btn-chat-submit:disabled {
    opacity: 0.7;
    cursor: not-allowed;
    transform: none;
}

/* Loading State */
.chat-loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    gap: 16px;
}

.chat-loading-spinner {
    margin-bottom: 8px;
}

.chat-loading-text {
    color: #6c757d;
    font-size: 14px;
    margin: 0;
}

/* Empty State */
.chat-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    gap: 16px;
    padding: 40px 20px;
}

.chat-empty-icon {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background: #f0f0f0;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #adb5bd;
    font-size: 36px;
}

.chat-empty-text {
    color: #6c757d;
    font-size: 14px;
    margin: 0;
}

/* Messages Container */
.chat-messages {
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding-bottom: 8px;
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
}

.chat-message {
    display: flex;
    margin-bottom: 4px;
    animation: fadeInMessage 0.3s ease-out;
}

@keyframes fadeInMessage {
    from {
        opacity: 0;
        transform: translateY(8px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.chat-message.user,
.chat-message.guest {
    justify-content: flex-end;
}

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

.chat-message-bubble {
    max-width: 75%;
    padding: 12px 16px;
    border-radius: 16px;
    word-wrap: break-word;
    position: relative;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.chat-message.user .chat-message-bubble,
.chat-message.guest .chat-message-bubble {
    background: var(
        --gradient-primary,
        linear-gradient(135deg, #62411e 0%, #442d15 100%)
    );
    color: var(--text-on-primary, #ffffff);
    border-bottom-right-radius: 4px;
}

.chat-message.admin .chat-message-bubble {
    background: white;
    color: #333;
    border: 1px solid #e0e0e0;
    border-bottom-left-radius: 4px;
}

.chat-message-text {
    font-size: 14px;
    line-height: 1.5;
    word-break: break-word;
}

.chat-message-time {
    font-size: 11px;
    margin-top: 6px;
    opacity: 0.8;
    display: block;
}

.chat-message-status {
    font-size: 10px;
    margin-top: 4px;
    opacity: 0.7;
    display: block;
    text-align: right;
}

/* Chat Footer */
.chat-footer {
    padding: 16px 20px;
    background: white;
    border-top: 1px solid #e9ecef;
    box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.05);
    flex-shrink: 0; /* Prevent footer from shrinking */
}

.chat-input-wrapper {
    display: flex;
    gap: 12px;
    align-items: center;
}

.chat-message-input {
    flex: 1;
    border: 2px solid #e0e0e0;
    border-radius: 24px;
    padding: 12px 20px;
    font-size: 14px;
    outline: none;
    transition: all 0.3s;
    background: #f8f9fa;
    font-family: inherit;
}

.chat-message-input:focus {
    border-color: var(--primary, #62411e);
    background: white;
    box-shadow: 0 0 0 3px rgba(98, 65, 30, 0.1);
}

.chat-message-input::placeholder {
    color: #adb5bd;
}

.chat-send-btn {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(
        --gradient-primary,
        linear-gradient(135deg, #62411e 0%, #442d15 100%)
    );
    border: none;
    color: var(--text-on-primary, #ffffff);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition, all 0.3s cubic-bezier(0.4, 0, 0.2, 1));
    font-size: 16px;
    box-shadow: var(--shadow, 0 2px 10px rgba(98, 65, 30, 0.08));
    flex-shrink: 0;
}

.chat-send-btn:hover:not(:disabled) {
    transform: translateY(-2px) scale(1.05);
    box-shadow: var(--shadow-lg, 0 8px 30px rgba(98, 65, 30, 0.12));
    background: var(
        --gradient-light,
        linear-gradient(135deg, #8b6337 0%, #62411e 100%)
    );
}

.chat-send-btn:active:not(:disabled) {
    transform: translateY(0) scale(1);
}

.chat-send-btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
}

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

/* Tablet (992px and below) */
@media (max-width: 992px) and (min-width: 769px) {
    .chat-window {
        width: 380px;
        height: 600px;
        max-height: calc(100vh - 100px);
        bottom: 80px; /* Sejajar dengan tombol (60px button + 20px gap) */
    }

    .chat-toggle-btn {
        width: 60px;
        height: 60px;
        font-size: 24px;
    }

    .chat-body {
        padding: 18px;
    }

    .chat-message-bubble {
        max-width: 80%;
    }
}

/* Mobile */
@media (max-width: 768px) {
    .chat-widget {
        /* Position above mobile bottom navigation bar */
        /* Menghitung tinggi nav bar (sekitar 70px) + gap (6px) + safe-area-inset-bottom */
        bottom: calc(
            76px + env(safe-area-inset-bottom, 0px)
        ); /* Tepat di atas mobile nav bar dengan gap yang lebih rapi */
        right: 16px;
        z-index: 10002 !important; /* Higher than mobile nav (9999) - using !important */
    }

    .chat-toggle-btn {
        width: 56px;
        height: 56px;
        font-size: 22px;
        z-index: 10004; /* Button should be above window when closed */
    }

    .chat-window {
        width: calc(100vw - 24px);
        height: calc(
            100vh - 180px
        ); /* Adjusted untuk proporsi yang lebih baik */
        max-height: calc(100vh - 180px);
        /* Disesuaikan dengan posisi tombol baru (56px button + 6px gap) + safe-area-inset-bottom */
        bottom: calc(62px + env(safe-area-inset-bottom, 0px));
        right: 12px;
        border-radius: 16px 16px 0 0;
        z-index: 10003; /* Window above button */
    }

    .chat-header {
        padding: 14px 16px;
        flex-shrink: 0;
    }

    .chat-header-avatar {
        width: 40px;
        height: 40px;
        font-size: 18px;
    }

    .chat-title {
        font-size: 16px;
    }

    .chat-subtitle {
        font-size: 11px;
    }

    .chat-body {
        padding: 14px 14px 18px; /* Disesuaikan untuk spacing yang lebih baik */
        min-height: 0;
        /* Optimized padding untuk mobile - lebih proporsional */
    }

    .chat-initial-form {
        padding: 0;
        margin-top: 0;
    }

    .chat-form-header {
        margin-bottom: 16px;
        margin-top: 0;
        padding-top: 0;
        padding-bottom: 12px;
    }

    .chat-form-icon {
        width: 48px;
        height: 48px;
        font-size: 20px;
        margin-bottom: 8px;
    }

    .chat-form-header h6 {
        font-size: 14px;
        margin-bottom: 4px;
    }

    .chat-form-header p {
        font-size: 11px;
    }

    .chat-initial-form .form-group {
        margin-bottom: 14px;
    }

    .chat-initial-form .form-label {
        font-size: 13px;
        margin-bottom: 6px;
    }

    .chat-initial-form .form-control {
        padding: 10px 12px;
        font-size: 14px;
    }

    .chat-message-bubble {
        max-width: 85%;
        padding: 10px 14px;
        font-size: 14px;
    }

    .chat-messages {
        gap: 10px;
    }

    .chat-footer {
        padding: 12px 16px;
    }

    .chat-message-input {
        padding: 10px 16px;
        font-size: 14px;
    }

    .chat-empty {
        padding: 40px 20px;
    }

    .chat-send-btn {
        width: 40px;
        height: 40px;
        font-size: 14px;
    }

    .chat-initial-form .form-control {
        padding: 10px 14px;
        font-size: 14px;
        width: 100%; /* Memastikan input full width di mobile */
        box-sizing: border-box; /* Memastikan padding tidak menambah lebar */
    }

    .btn-chat-submit {
        padding: 12px 16px; /* Sedikit ditingkatkan untuk touch target yang lebih baik */
        font-size: 14px;
        margin-top: 6px;
        width: 100%; /* Memastikan button full width */
        box-sizing: border-box;
    }

    /* Memastikan form elements responsif */
    .chat-initial-form .form-group {
        width: 100%;
        box-sizing: border-box;
    }

    .chat-initial-form select.form-control {
        width: 100%;
        box-sizing: border-box;
    }

    .chat-initial-form textarea.form-control {
        width: 100%;
        box-sizing: border-box;
        resize: vertical; /* Hanya resize vertikal */
    }
}

/* Small Mobile */
@media (max-width: 480px) {
    .chat-widget {
        /* Position above mobile bottom navigation bar */
        /* Menghitung tinggi nav bar (sekitar 68px) + gap (6px) + safe-area-inset-bottom */
        bottom: calc(
            74px + env(safe-area-inset-bottom, 0px)
        ); /* Tepat di atas mobile nav bar dengan gap yang lebih rapi */
        right: 12px;
        z-index: 10002 !important; /* Higher than mobile nav - using !important */
    }

    .chat-toggle-btn {
        width: 52px;
        height: 52px;
        font-size: 20px;
        z-index: 10004; /* Button should be above window when closed */
    }

    .chat-window {
        width: calc(100vw - 20px);
        height: calc(
            100vh - 170px
        ); /* Adjusted untuk proporsi yang lebih baik */
        max-height: calc(100vh - 170px);
        /* Disesuaikan dengan posisi tombol baru (52px button + 6px gap) + safe-area-inset-bottom */
        bottom: calc(60px + env(safe-area-inset-bottom, 0px));
        right: 10px;
        z-index: 10003; /* Window above button */
    }

    .chat-header {
        padding: 12px 14px;
        flex-shrink: 0;
    }

    .chat-body {
        padding: 12px 12px 16px; /* Disesuaikan untuk spacing yang lebih baik */
        min-height: 0;
        /* Optimized padding untuk small mobile - lebih proporsional */
    }

    .chat-initial-form {
        padding: 0;
        margin-top: 0;
    }

    .chat-form-header {
        margin-bottom: 14px;
        margin-top: 0;
        padding-top: 0;
        padding-bottom: 10px;
    }

    .chat-form-icon {
        width: 44px;
        height: 44px;
        font-size: 18px;
        margin-bottom: 6px;
    }

    .chat-form-header h6 {
        font-size: 13px;
        margin-bottom: 3px;
    }

    .chat-form-header p {
        font-size: 10px;
    }

    .chat-initial-form .form-group {
        margin-bottom: 12px;
    }

    .chat-initial-form .form-label {
        font-size: 12px;
        margin-bottom: 5px;
    }

    .chat-initial-form .form-control {
        padding: 8px 10px;
        font-size: 13px;
    }

    .chat-initial-form .form-text {
        font-size: 11px;
    }

    .chat-message-bubble {
        max-width: 90%;
        padding: 10px 12px;
        font-size: 13px;
    }

    .chat-messages {
        gap: 8px;
    }

    .chat-empty {
        padding: 30px 16px;
    }

    .chat-empty-icon {
        font-size: 36px;
        width: 60px;
        height: 60px;
    }

    .chat-empty-text {
        font-size: 13px;
    }

    .chat-footer {
        padding: 10px 14px;
    }

    /* Memastikan form elements responsif di small mobile */
    .chat-initial-form .form-control {
        width: 100%;
        box-sizing: border-box;
    }

    .chat-initial-form select.form-control,
    .chat-initial-form textarea.form-control {
        width: 100%;
        box-sizing: border-box;
    }

    .btn-chat-submit {
        width: 100%;
        box-sizing: border-box;
    }
}

/* Landscape Mobile */
@media (max-width: 768px) and (orientation: landscape) {
    .chat-window {
        height: calc(100vh - 60px);
        max-height: calc(100vh - 60px);
    }
}

/* Print - Hide Chat */
@media print {
    .chat-widget {
        display: none !important;
    }
}

/* High Contrast Mode Support */
@media (prefers-contrast: high) {
    .chat-toggle-btn,
    .chat-header,
    .btn-chat-submit,
    .chat-send-btn {
        border: 2px solid currentColor;
    }
}

/* Reduced Motion Support */
@media (prefers-reduced-motion: reduce) {
    .chat-toggle-btn,
    .chat-send-btn,
    .chat-close-btn,
    .chat-message,
    .chat-window {
        animation: none;
        transition: none;
    }
}
