/**
 * @fileoverview Reusable UI Components for Chahua Web Store
 * @description Common components like modals, buttons, forms, and interactive elements used across pages
 * @dependencies 
 *   - global.css (CSS variables and base styles)
 * @components
 *   - Modal dialogs with animations
 *   - Form inputs and validation styles
 *   - Button variants (primary, secondary, danger)
 *   - Loading spinners and progress indicators
 *   - Toast notifications and alerts
 *   - Card components with hover effects
 * @animations
 *   - fadeIn: Modal background fade (0.3s)
 *   - slideIn: Modal content slide animation (0.3s)
 *   - Button hover transformations
 * @performance 
 *   - Modular CSS for efficient loading
 *   - Hardware-accelerated animations
 *   - Optimized for reusability
 * @calledBy All HTML pages requiring common UI components
 * @example
 * // Modal usage:
 * <div class="modal" id="myModal">
 *   <div class="modal-content">
 *     <div class="modal-header">
 *       <h3>Title</h3>
 *       <button class="modal-close">×</button>
 *     </div>
 *   </div>
 * </div>
 */

/* components.css - UI Components ที่ใช้ซ้ำๆ */

/* Modals */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    z-index: 1000;
    animation: fadeIn 0.3s ease;
    align-items: center;
    justify-content: center;
}

.modal.show {
    display: flex;
    position: relative;
    width: 100%;
    min-height: 100vh;
    background: var(--bg-primary);
}

.modal-content {
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    border-radius: 16px;
    padding: 2rem;
    width: 90%;
    max-width: 420px;
    animation: slideIn 0.3s ease;
    margin: auto;
    box-shadow: var(--shadow-xl);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
}

.modal-header h3 {
    color: var(--accent-blue);
    font-size: 1.75rem;
}

.modal-close {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 1.5rem;
    cursor: pointer;
    transition: color 0.3s ease;
}

.modal-close:hover {
    color: var(--accent-blue);
}

.modal-footer {
    text-align: center;
    margin-top: 1.5rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border-color);
}

.modal-footer p {
    color: var(--text-secondary);
}

.modal-footer a {
    color: var(--accent-blue);
    text-decoration: none;
    font-weight: 500;
}

.modal-footer a:hover {
    text-decoration: underline;
}

/* Forms */
.auth-form {
    display: flex;
    flex-direction: column;
    gap: 1.2rem;
}

.form-group {
    display: flex;
    flex-direction: column;
}

.form-group label {
    color: var(--text-primary);
    margin-bottom: 0.5rem;
    font-weight: 500;
}

.form-group input {
    padding: 0.9rem;
    border: 2px solid var(--border-color);
    border-radius: 8px;
    background: var(--bg-primary);
    color: var(--text-primary);
    font-size: 1rem;
    transition: all 0.3s ease;
}

.form-group input:focus {
    outline: none;
    border-color: var(--accent-blue);
    box-shadow: 0 0 0 4px rgba(0, 170, 255, 0.15);
}

/* Buttons */
.btn-submit {
    padding: 1rem 2rem;
    background: linear-gradient(135deg, var(--accent-blue), var(--accent-light));
    color: white;
    border: 1px solid var(--glass-border);
    border-radius: 12px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    margin-top: 1rem;
    box-shadow: var(--shadow-md);
    position: relative;
    overflow: hidden;
}

.btn-submit::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
    transition: left 0.5s ease;
}

.btn-submit:hover::before {
    left: 100%;
}

.btn-submit:hover:not(:disabled) {
    background: linear-gradient(135deg, var(--accent-blue-hover), var(--accent-blue));
    transform: translateY(-2px);
    box-shadow: var(--shadow-glow);
}

.btn-submit:disabled {
    background: linear-gradient(135deg, #555, #666);
    cursor: not-allowed;
    box-shadow: var(--shadow-sm);
}

.social-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    width: 100%;
    padding: 0.8rem 1.2rem;
    margin-bottom: 0.8rem;
    border: 1px solid var(--glass-border);
    border-radius: 12px;
    background: var(--glass-bg);
    color: var(--text-primary);
    cursor: pointer;
    font-size: 0.95rem;
    font-weight: 500;
    transition: all 0.3s ease;
    box-shadow: var(--shadow-sm);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    position: relative;
    overflow: hidden;
}

.social-btn::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(14, 165, 233, 0.1), transparent);
    transition: left 0.5s ease;
}

.social-btn:hover::before {
    left: 100%;
}

.social-btn:hover {
    border-color: var(--accent-blue);
    background: var(--glass-bg);
    box-shadow: var(--shadow-glow);
    transform: translateY(-1px);
}

/* GitHub Button Specific Styling */
.github-btn {
    border-color: #333;
    background: #24292e;
    color: #fff;
}

.github-btn:hover {
    border-color: #000;
    background: #1b1f23;
    color: #fff;
}

.github-btn .social-icon {
    fill: #fff;
}

/* Twitter/X Button Specific Styling */
.twitter-btn {
    border-color: #1da1f2;
    background: #000;
    color: #fff;
}

.twitter-btn:hover {
    border-color: #1a8cd8;
    background: #333;
    color: #fff;
}

.twitter-btn .social-icon {
    fill: #fff;
}

.social-icon {
    width: 20px;
    height: 20px;
}

/* Divider */
.divider {
    text-align: center;
    margin: 1.5rem 0;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 1rem;
}

.divider::before,
.divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--border-color);
}

/* AI Chat Widget */
.cha-chat-widget.ai-powered {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 9999;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.cha-chat-toggle {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(135deg, #00aaff, #0088cc);
    color: white;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    box-shadow: 0 4px 20px rgba(0, 170, 255, 0.3);
    transition: all 0.3s ease;
    animation: pulse 2s infinite;
}

.cha-chat-toggle:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 25px rgba(0, 170, 255, 0.4);
}

.cha-chat-toggle.open {
    background: linear-gradient(135deg, #ff6b6b, #ff5252);
}

@keyframes pulse {
    0% { box-shadow: 0 4px 20px rgba(0, 170, 255, 0.3); }
    50% { box-shadow: 0 4px 30px rgba(0, 170, 255, 0.6); }
    100% { box-shadow: 0 4px 20px rgba(0, 170, 255, 0.3); }
}

.cha-chat-box {
    position: absolute;
    bottom: 80px;
    right: 0;
    width: 380px;
    height: 550px;
    background: #1e1e1e;
    border-radius: 15px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
    display: none;
    flex-direction: column;
    overflow: hidden;
    border: 1px solid rgba(0, 170, 255, 0.2);
    max-height: 80vh;
}

.cha-chat-box.open {
    display: flex;
    animation: slideUp 0.3s ease-out;
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.cha-chat-header {
    background: linear-gradient(135deg, #2c2c2c, #1a1a1a);
    color: white;
    padding: 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid rgba(0, 170, 255, 0.2);
}

.cha-chat-title {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.ai-indicator {
    font-weight: bold;
    color: #00aaff;
    font-size: 14px;
}

.powered-by {
    font-size: 10px;
    color: #888;
}

.cha-chat-status {
    font-size: 11px;
    color: #00aaff;
    background: rgba(0, 170, 255, 0.1);
    padding: 4px 8px;
    border-radius: 10px;
    margin-top: 4px;
}

.cha-chat-controls {
    display: flex;
    gap: 8px;
}

.cha-chat-control-btn {
    background: rgba(255, 255, 255, 0.1);
    border: none;
    border-radius: 50%;
    width: 30px;
    height: 30px;
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
}

.cha-chat-control-btn:hover {
    background: rgba(0, 170, 255, 0.3);
    transform: scale(1.1);
}

.cha-chat-body {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: #2a2a2a;
    overflow: hidden;
    position: relative;
}

.cha-chat-messages {
    flex: 1;
    padding: 15px;
    overflow-y: auto;
    overflow-x: hidden;
    display: flex;
    flex-direction: column;
    gap: 12px;
    height: 0;
    min-height: 200px;
    max-height: none;
    position: relative;
    scroll-behavior: smooth;
}

.cha-chat-messages::-webkit-scrollbar {
    width: 8px;
}

.cha-chat-messages::-webkit-scrollbar-track {
    background: #1a1a1a;
    border-radius: 4px;
}

.cha-chat-messages::-webkit-scrollbar-thumb {
    background: #00aaff;
    border-radius: 4px;
    border: 1px solid #1a1a1a;
}

.cha-chat-messages::-webkit-scrollbar-thumb:hover {
    background: #0088cc;
}

.cha-chat-message {
    display: flex;
    flex-direction: column;
    gap: 4px;
    max-width: 80%;
    animation: messageSlide 0.3s ease-out;
}

@keyframes messageSlide {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.cha-chat-message.cha-user {
    align-self: flex-end;
}

.cha-chat-message.cha-bot {
    align-self: flex-start;
}

.cha-message-content {
    padding: 12px 16px;
    border-radius: 18px;
    font-size: 14px;
    line-height: 1.4;
}

.cha-user .cha-message-content {
    background: linear-gradient(135deg, #00aaff, #0088cc);
    color: white;
}

.cha-bot .cha-message-content {
    background: #3a3a3a;
    color: #e0e0e0;
    border: 1px solid rgba(0, 170, 255, 0.2);
}

.cha-message-time {
    font-size: 10px;
    color: #888;
    text-align: right;
}

.cha-user .cha-message-time {
    text-align: right;
}

.cha-bot .cha-message-time {
    text-align: left;
}

.cha-message-avatar {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: #00aaff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    margin-bottom: 4px;
}

.ai-thinking {
    display: flex;
    align-items: center;
    padding: 15px;
    background: rgba(0, 170, 255, 0.05);
    border-radius: 15px;
    margin: 10px;
}

.thinking-animation {
    display: flex;
    margin-right: 10px;
}

.thinking-animation span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #00aaff;
    display: inline-block;
    margin: 0 2px;
    animation: thinking 1.4s infinite ease-in-out;
}

.thinking-animation span:nth-child(1) { animation-delay: -0.32s; }
.thinking-animation span:nth-child(2) { animation-delay: -0.16s; }

@keyframes thinking {
    0%, 80%, 100% { 
        transform: scale(0.8);
        opacity: 0.5;
    } 
    40% { 
        transform: scale(1);
        opacity: 1;
    }
}

.thinking-text {
    color: #00aaff;
    font-size: 12px;
}

.cha-chat-input-area {
    padding: 15px;
    background: #1a1a1a;
    border-top: 1px solid rgba(0, 170, 255, 0.2);
    display: flex;
    gap: 10px;
    align-items: center;
}

.cha-chat-input {
    flex: 1;
    background: #2a2a2a;
    border: 1px solid rgba(0, 170, 255, 0.3);
    border-radius: 20px;
    padding: 12px 16px;
    color: white;
    font-size: 14px;
    outline: none;
    transition: all 0.3s ease;
}

.cha-chat-input:focus {
    border-color: #00aaff;
    box-shadow: 0 0 10px rgba(0, 170, 255, 0.2);
}

.cha-chat-input::placeholder {
    color: #888;
}

.cha-chat-send {
    background: linear-gradient(135deg, #00aaff, #0088cc);
    border: none;
    border-radius: 50%;
    width: 40px;
    height: 40px;
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
}

.cha-chat-send:hover {
    transform: scale(1.1);
    box-shadow: 0 4px 15px rgba(0, 170, 255, 0.3);
}

.ai-suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 12px;
    background: rgba(0, 170, 255, 0.02);
    border-top: 1px solid rgba(0, 170, 255, 0.1);
}

.suggestion-btn {
    background: rgba(0, 170, 255, 0.1);
    border: 1px solid rgba(0, 170, 255, 0.3);
    color: #00aaff;
    padding: 6px 12px;
    border-radius: 20px;
    font-size: 12px;
    cursor: pointer;
    transition: all 0.3s ease;
    white-space: nowrap;
}

.suggestion-btn:hover {
    background: rgba(0, 170, 255, 0.2);
    transform: translateY(-1px);
    box-shadow: 0 2px 8px rgba(0, 170, 255, 0.2);
}

.cha-chat-widget.cha-dark-theme .cha-chat-box {
    background: #0a0a0a;
}

.cha-chat-widget.cha-dark-theme .cha-chat-body {
    background: #111;
}

.cha-chat-widget.cha-dark-theme .cha-chat-input {
    background: #1a1a1a;
}

.cha-chat-widget .sign img,
.line-avatar img,
.line-message-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 50%;
    border: 2px solid #00FF88;
    background: #0D1E2A;
}

.cha-chat-toggle:hover .sign img {
    transform: scale(1.1);
    transition: transform 0.3s ease;
}

@keyframes logoGlow {
    0% { box-shadow: 0 0 5px #00FF88; }
    50% { box-shadow: 0 0 20px #00FF88, 0 0 30px #00FF88; }
    100% { box-shadow: 0 0 5px #00FF88; }
}

.ai-loading .line-avatar img {
    animation: logoGlow 2s infinite;
}

@media (max-width: 480px) {
    .cha-chat-box {
        width: calc(100vw - 40px);
        height: calc(100vh - 120px);
        max-height: calc(100vh - 120px);
        right: 20px;
        bottom: 80px;
    }
    
    .cha-chat-messages {
        min-height: 150px;
    }
    
    .cha-chat-toggle {
        width: 50px;
        height: 50px;
        font-size: 20px;
    }
}

/* Modal Animations */
@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes slideIn {
    from { 
        opacity: 0;
        transform: translate(-50%, -60%);
    }
    to { 
        opacity: 1;
        transform: translate(-50%, -50%);
    }
}

/* ✅ Chat Widget Protection - ป้องกันการทับซ้อนจากสคริปต์อื่น */
.cha-chat-widget {
    position: fixed !important;
    bottom: 20px !important;
    right: 20px !important;
    z-index: 9999 !important;
    pointer-events: auto !important;
    visibility: visible !important;
    opacity: 1 !important;
}

.cha-chat-widget.mobile-hidden {
    display: none !important;
}

/* ✅ ป้องกัน interference จาก JavaScript อื่น */
.cha-chat-widget * {
    box-sizing: border-box !important;
}

.cha-chat-toggle {
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
}

.cha-chat-box {
    background: #1e1e1e !important;
    border-radius: 15px !important;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3) !important;
}

.cha-chat-box.open {
    display: flex !important;
}
