/**
 * Frontend Styles (Mobile-First Version with Vertical Cards Layout)
 * File: assets/css/frontend.css
 * Version: 3.0 - Vertical Cards for Mobile (UI/UX Optimized)
 */

/* =========================================
   0. متغیرهای CSS و Reset
   ========================================= */
:root {
    --primary-color: #667eea;
    --primary-dark: #5568d3;
    --secondary-color: #764ba2;
    --success-color: #4caf50;
    --error-color: #e74c3c;
    --text-dark: #2c3e50;
    --text-medium: #34495e;
    --text-light: #7f8c8d;
    --bg-light: #f8f9fa;
    --border-color: #e0e0e0;
    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.06);
    --shadow-md: 0 5px 20px rgba(0, 0, 0, 0.08);
    --shadow-lg: 0 10px 50px rgba(0, 0, 0, 0.1);
    --radius-sm: 6px;
    --radius-md: 8px;
    --radius-lg: 12px;
    --radius-xl: 16px;
}

* {
    box-sizing: border-box;
}

/* =========================================
   1. استایل‌های فرم ورودی و عمومی
   ========================================= */

.afp-form-container {
    max-width: 700px;
    margin: 20px auto;
    background: #ffffff;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
    padding: 24px;
    width: calc(100% - 32px);
}

.afp-form-header {
    text-align: center;
    margin-bottom: 32px;
    padding-bottom: 24px;
    border-bottom: 2px solid #f0f0f0;
}

.afp-form-header h2 {
    color: var(--text-dark);
    font-size: 24px;
    margin-bottom: 12px;
    font-weight: 700;
    line-height: 1.3;
}

.afp-form-header p {
    color: var(--text-light);
    font-size: 15px;
    line-height: 1.6;
    margin: 0;
}

.afp-form-row {
    display: grid;
    grid-template-columns: 1fr;
    gap: 16px;
    margin-bottom: 20px;
}

.afp-form-group {
    margin-bottom: 20px;
}

.afp-form-group-half {
    margin-bottom: 0;
}

.afp-form-group label {
    display: block;
    font-weight: 600;
    color: var(--text-medium);
    margin-bottom: 8px;
    font-size: 14px;
    line-height: 1.4;
}

.afp-form-group .required {
    color: var(--error-color);
    margin-right: 4px;
    font-weight: 700;
}

.afp-form-group .description {
    font-size: 12px;
    color: #666;
    margin-top: 6px;
    font-style: italic;
    line-height: 1.5;
}

.afp-form-group input[type="text"],
.afp-form-group input[type="number"],
.afp-form-group input[type="email"],
.afp-form-group input[type="password"],
.afp-form-group select,
.afp-form-group textarea {
    width: 100%;
    padding: 12px 15px;
    border: 2px solid var(--border-color);
    border-radius: var(--radius-md);
    font-size: 16px;
    transition: all 0.2s ease;
    background: #f9f9f9;
    font-family: inherit;
    color: var(--text-dark);
}

.afp-form-group select {
    appearance: none;
    -webkit-appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3E%3Cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3E%3C/svg%3E");
    background-position: left 12px center;
    background-repeat: no-repeat;
    background-size: 20px;
    padding-right: 40px;
}

.afp-form-group input:focus,
.afp-form-group select:focus,
.afp-form-group textarea:focus {
    outline: none;
    border-color: #3498db;
    background: #ffffff;
    box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
}

.afp-form-group input.error,
.afp-form-group select.error,
.afp-form-group textarea.error {
    border-color: var(--error-color);
    background: #fff5f5;
}

.afp-form-group textarea {
    resize: vertical;
    min-height: 80px;
}

.afp-supplements-field {
    background: linear-gradient(135deg, #e8f5e9 0%, #c8e6c9 100%);
    padding: 18px;
    border-radius: var(--radius-md);
    margin: 16px 0;
    border: 2px solid var(--success-color);
}

.afp-checkbox-label {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    cursor: pointer;
    margin: 0;
    padding: 8px;
    border-radius: var(--radius-sm);
    transition: background 0.2s;
}

.afp-checkbox-label:hover {
    background: rgba(76, 175, 80, 0.1);
}

.afp-checkbox-label input[type="checkbox"] {
    width: 22px;
    height: 22px;
    min-width: 22px;
    cursor: pointer;
    margin-top: 2px;
    accent-color: var(--success-color);
}

.afp-checkbox-text {
    flex: 1;
    line-height: 1.5;
}

.afp-checkbox-text strong {
    color: #2e7d32;
    font-size: 15px;
    display: block;
    margin-bottom: 4px;
}

.afp-checkbox-text small {
    color: #555;
    line-height: 1.6;
    font-size: 13px;
}

.afp-custom-fields {
    background: var(--bg-light);
    padding: 20px;
    border-radius: var(--radius-md);
    margin: 24px 0;
}

.afp-custom-fields h3 {
    color: var(--text-dark);
    font-size: 18px;
    margin-bottom: 16px;
    font-weight: 600;
}

.afp-form-actions {
    margin-top: 32px;
    text-align: center;
}

.afp-submit-btn {
    background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
    color: #ffffff;
    border: none;
    padding: 15px 40px;
    font-size: 16px;
    font-weight: 600;
    border-radius: 50px;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
    position: relative;
    width: 100%;
    max-width: 100%;
    display: inline-flex;
    justify-content: center;
    align-items: center;
    gap: 8px;
    touch-action: manipulation;
}

.afp-submit-btn:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.5);
}

.afp-submit-btn:active:not(:disabled) {
    transform: translateY(0);
}

.afp-submit-btn:disabled {
    opacity: 0.7;
    cursor: not-allowed;
}

.afp-btn-loader {
    display: inline-block;
}

.afp-btn-loader .spinner {
    display: inline-block;
    width: 18px;
    height: 18px;
    border: 3px solid rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    border-top-color: #ffffff;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

#afp-progress-container {
    margin-top: 24px;
    padding: 20px;
    background: var(--bg-light);
    border-radius: var(--radius-md);
    border: 1px solid var(--border-color);
}

.afp-progress-bar {
    height: 10px;
    background: #e0e0e0;
    border-radius: 10px;
    overflow: hidden;
    margin-bottom: 12px;
}

.afp-progress-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--primary-color) 0%, var(--secondary-color) 100%);
    width: 0;
    transition: width 0.5s ease;
    border-radius: 10px;
    position: relative;
}

.afp-progress-fill::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent);
    animation: shimmer 2s infinite;
}

@keyframes shimmer {
    0% { transform: translateX(-100%); }
    100% { transform: translateX(100%); }
}

.afp-progress-text {
    text-align: center;
    color: #555;
    font-weight: 600;
    font-size: 14px;
}

.afp-alert {
    padding: 14px 18px;
    border-radius: var(--radius-md);
    margin-top: 16px;
    font-size: 14px;
    line-height: 1.5;
}

.afp-alert-success {
    background: #d4edda;
    color: #155724;
    border: 1px solid #c3e6cb;
}

.afp-alert-error {
    background: #f8d7da;
    color: #721c24;
    border: 1px solid #f5c6cb;
}

/* =========================================
   2. استایل‌های پاپ‌آپ (Modal) ورود/ثبت‌نام
   ========================================= */

.afp-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(5px);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 99999;
    padding: 16px;
    overflow-y: auto;
}

.afp-modal-content {
    background: #ffffff;
    border-radius: var(--radius-xl);
    max-width: 450px;
    width: 100%;
    box-shadow: var(--shadow-lg);
    animation: modalSlideIn 0.4s ease-out;
    max-height: calc(100vh - 32px);
    overflow-y: auto;
    position: relative;
    margin: auto;
}

@keyframes modalSlideIn {
    from { opacity: 0; transform: translateY(-30px); }
    to { opacity: 1; transform: translateY(0); }
}

.afp-modal-header {
    background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
    color: white;
    padding: 28px 24px;
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    text-align: center;
}

.afp-modal-header h2 {
    margin: 0 0 8px 0;
    font-size: 24px;
    font-weight: 700;
    color: white;
}

.afp-modal-header p {
    margin: 0;
    font-size: 14px;
    opacity: 0.95;
    color: rgba(255,255,255,0.9);
}

.afp-auth-tabs {
    display: flex;
    background: var(--bg-light);
    padding: 5px;
    margin: 0;
    border-bottom: 1px solid var(--border-color);
}

.afp-auth-tab {
    flex: 1;
    padding: 12px;
    background: transparent;
    border: none;
    font-size: 15px;
    font-weight: 600;
    color: var(--text-light);
    cursor: pointer;
    transition: all 0.2s ease;
    border-radius: var(--radius-md);
}

.afp-auth-tab:hover { 
    color: var(--primary-color); 
}

.afp-auth-tab.active {
    background: #ffffff;
    color: var(--primary-color);
    box-shadow: 0 2px 8px rgba(102, 126, 234, 0.15);
}

.afp-auth-form {
    display: none;
    padding: 24px;
}

.afp-auth-form.active { 
    display: block; 
}

.afp-auth-form .afp-form-group { 
    margin-bottom: 18px; 
}

.afp-auth-form .afp-input {
    width: 100%;
    padding: 12px 15px;
    border: 2px solid var(--border-color);
    border-radius: var(--radius-md);
    font-size: 16px;
    transition: all 0.2s ease;
    font-family: inherit;
}

.afp-auth-form .afp-input:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.afp-checkbox-group { 
    display: flex; 
    align-items: center; 
}

.afp-checkbox-group label { 
    display: flex; 
    align-items: center; 
    gap: 8px; 
    cursor: pointer; 
    font-weight: normal; 
    margin: 0; 
}

.afp-checkbox-group input[type="checkbox"] { 
    width: 18px; 
    height: 18px; 
    cursor: pointer; 
    accent-color: var(--primary-color);
}

.afp-auth-footer {
    margin-top: 16px;
    text-align: center;
    font-size: 13px;
}

.afp-auth-footer a { 
    color: var(--primary-color); 
    text-decoration: none; 
    font-weight: 600; 
}

#afp-register-message {
    margin: 15px 0;
    padding: 12px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    display: none;
}

#afp-register-message.success { 
    background: #d4edda; 
    color: #155724; 
    border: 1px solid #c3e6cb; 
    display: block; 
}

#afp-register-message.error { 
    background: #f8d7da; 
    color: #721c24; 
    border: 1px solid #f5c6cb; 
    display: block; 
}

/* =========================================
   3. استایل‌های باکس آنالیز و چارت
   ========================================= */

#afp-analysis-box {
    background: linear-gradient(135deg, #2c3e50 0%, #3498db 100%);
    color: white;
    border-radius: var(--radius-lg);
    padding: 20px;
    margin-bottom: 24px;
    box-shadow: 0 8px 25px rgba(52, 152, 219, 0.3);
    display: none;
    animation: fadeIn 0.5s ease-out;
}

@keyframes fadeIn { 
    from { opacity: 0; transform: translateY(10px); } 
    to { opacity: 1; transform: translateY(0); } 
}

#afp-analysis-box h3 {
    color: white;
    margin: 0 0 16px 0;
    font-size: 18px;
    border-bottom: 1px solid rgba(255,255,255,0.2);
    padding-bottom: 10px;
}

.afp-analysis-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
}

.afp-analysis-item {
    background: rgba(255,255,255,0.15);
    backdrop-filter: blur(10px);
    padding: 14px;
    border-radius: var(--radius-md);
    text-align: center;
    border: 1px solid rgba(255,255,255,0.2);
}

.afp-analysis-value {
    font-size: 22px;
    font-weight: 700;
    display: block;
    margin-bottom: 4px;
}

.afp-analysis-label {
    font-size: 12px;
    opacity: 0.8;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.afp-macro-chart-container {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-wrap: wrap;
    background: #fff;
    padding: 24px;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    margin-bottom: 24px;
    border: 1px solid #eee;
    gap: 24px;
}

.afp-pie-chart {
    width: 140px;
    height: 140px;
    border-radius: 50%;
    position: relative;
    flex-shrink: 0;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.afp-chart-legend {
    flex: 1;
    min-width: 180px;
}

.afp-legend-item {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
    font-size: 14px;
    color: #555;
    padding: 6px;
    border-radius: var(--radius-sm);
    transition: background 0.2s;
}

.afp-legend-item:hover {
    background: var(--bg-light);
}

.afp-legend-color {
    width: 14px;
    height: 14px;
    border-radius: 3px;
    margin-left: 10px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

/* =========================================
   4. استایل‌های نمایش برنامه (Desktop Default)
   ========================================= */

.afp-plan-view-container {
    max-width: 1000px;
    margin: 20px auto;
    padding: 0 16px;
}

.afp-plan-wrapper { 
    background: #fff; 
    padding: 20px; 
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
}

/* Base Table Styles (Desktop) */
.afp-table-wrapper {
    width: 100%;
    overflow-x: auto;
    margin: 20px 0;
    border-radius: var(--radius-md);
    border: 1px solid #eee;
}

.afp-plan-content table, 
table.afp-plan-table {
    width: 100%;
    border-collapse: collapse;
    margin: 0;
    font-size: 14px;
    background: #fff;
}

.afp-plan-content table th, 
table.afp-plan-table th {
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    color: #ffffff;
    text-align: center;
    padding: 14px 12px;
    font-weight: 600;
    border: 1px solid #eee;
    white-space: nowrap;
    font-size: 13px;
    text-transform: uppercase;
}

.afp-plan-content table td, 
table.afp-plan-table td {
    padding: 12px;
    border: 1px solid #eee;
    color: #333;
    text-align: center;
    line-height: 1.5;
}

.afp-plan-content table tr:nth-child(even), 
table.afp-plan-table tr:nth-child(even) { 
    background-color: var(--bg-light); 
}

.afp-plan-content table tr:hover, 
table.afp-plan-table tr:hover { 
    background-color: #f1f4f9; 
}

.afp-plan-content h2 { 
    color: var(--text-dark); 
    border-bottom: 3px solid var(--primary-color); 
    padding-bottom: 8px; 
    margin-top: 40px; 
    margin-bottom: 20px; 
    font-size: 22px;
    display: inline-block;
}

.afp-plan-content h3 { 
    background: linear-gradient(to right, #e0f2fe, #f0f9ff); 
    padding: 12px 16px; 
    border-radius: var(--radius-md); 
    color: #0369a1; 
    margin-top: 24px; 
    margin-bottom: 16px; 
    font-size: 16px;
    font-weight: 600;
    border-right: 4px solid #0284c7;
}

.afp-plan-content ul { 
    list-style: none; 
    padding: 0; 
    margin: 16px 0; 
}

.afp-plan-content ul li { 
    padding: 10px 15px; 
    border-bottom: 1px dashed #eee; 
    background: #fff;
    line-height: 1.6;
}

.afp-plan-content ul li:last-child { 
    border-bottom: none; 
}

.afp-plan-actions { 
    margin-top: 32px; 
    text-align: center; 
    display: flex; 
    flex-wrap: wrap;
    justify-content: center; 
    gap: 12px; 
}

.afp-btn { 
    display: inline-flex; 
    align-items: center; 
    justify-content: center;
    gap: 8px; 
    padding: 12px 24px; 
    border-radius: var(--radius-md); 
    text-decoration: none; 
    font-weight: 600; 
    cursor: pointer;
    font-size: 14px;
    transition: all 0.2s ease;
    min-width: 140px;
    touch-action: manipulation;
}

.afp-btn-print { 
    background: var(--primary-color); 
    color: white; 
    border: none;
    box-shadow: var(--shadow-sm);
}

.afp-btn-print:hover {
    background: var(--primary-dark);
    transform: translateY(-2px);
}

.afp-btn-back { 
    background: #ecf0f1; 
    color: #333;
    border: 2px solid var(--border-color);
}

.afp-btn-back:hover {
    background: #fff;
    border-color: var(--primary-color);
}

/* =========================================
   5. CRITICAL FIX - نمایش کارتی جداول در موبایل (Vertical Stacked)
   ========================================= */

@media (max-width: 768px) {
    /* 5.1. تنظیمات کانتینر اصلی */
    body {
        overflow-x: hidden;
    }

    .afp-plan-view-container {
        margin: 10px auto;
        padding: 0;
        width: 100%;
    }

    .afp-plan-wrapper {
        padding: 12px;
        border-radius: 0;
        box-shadow: none;
        background: transparent;
    }

    .afp-table-wrapper {
        border: none;
        overflow: visible;
        margin: 0;
    }

    /* 5.2. تبدیل جدول به کارت (Card View Transformation) */
    
    /* اجبار به نمایش بلوکی برای تمام اجزای جدول */
    .afp-plan-content table, 
    .afp-plan-content thead, 
    .afp-plan-content tbody, 
    .afp-plan-content th, 
    .afp-plan-content td, 
    .afp-plan-content tr,
    table.afp-plan-table,
    table.afp-plan-table tbody,
    table.afp-plan-table tr,
    table.afp-plan-table td { 
        display: block; 
    }

    /* مخفی کردن کامل هدر جدول (چون در حالت کارتی جاگیر است) */
    .afp-plan-content table thead,
    table.afp-plan-table thead { 
        position: absolute;
        top: -9999px;
        left: -9999px;
    }

    /* استایل هر ردیف به عنوان یک کارت */
    .afp-plan-content table tr,
    table.afp-plan-table tr { 
        background: #ffffff;
        border: 2px solid #e2e8f0;
        border-radius: var(--radius-md);
        margin-bottom: 16px; /* فاصله بین کارت‌ها */
        padding: 8px 16px;
        box-shadow: 0 4px 6px rgba(0,0,0,0.02);
        position: relative;
    }

    /* حذف پس‌زمینه رنگی ردیف‌های زوج برای یکدست شدن کارت‌ها */
    .afp-plan-content table tr:nth-child(even), 
    table.afp-plan-table tr:nth-child(even) {
        background-color: #ffffff;
    }

    /* استایل سلول‌ها (داده‌های درون کارت) */
    .afp-plan-content table td,
    table.afp-plan-table td { 
        /* نمایش به صورت عمودی و تمیز */
        border: none;
        border-bottom: 1px solid #f1f1f1;
        position: relative;
        padding: 12px 0;
        text-align: right; /* راست چین برای فارسی */
        min-height: 40px;
        display: flex;
        align-items: center;
        justify-content: flex-start;
        font-size: 15px;
        color: var(--text-dark);
        width: 100%;
        white-space: normal; /* اجازه شکستن متن */
    }

    /* حذف خط زیرین آخرین آیتم در کارت */
    .afp-plan-content table td:last-child,
    table.afp-plan-table td:last-child { 
        border-bottom: none;
    }

    /* بهبود نمایش ردیف‌های خاص (مثل هدرهای وسط جدول یا سوپرست‌ها) */
    .afp-plan-content table td[colspan],
    table.afp-plan-table td[colspan] {
        background: #f8fafc;
        color: var(--primary-color);
        font-weight: 700;
        justify-content: center;
        text-align: center;
        border-radius: var(--radius-sm);
        margin: 4px 0;
        border-bottom: none;
    }

    /* 5.3. سایر اصلاحات موبایل */
    .afp-form-container { 
        padding: 20px 16px; 
        margin: 16px; 
        width: calc(100% - 32px);
    }

    .afp-form-header h2 { font-size: 20px; }
    .afp-form-row { grid-template-columns: 1fr; gap: 0; }
    
    .afp-analysis-grid { grid-template-columns: 1fr 1fr; }
    .afp-macro-chart-container { flex-direction: column; }
    
    .afp-plan-content h2 { font-size: 18px; margin-top: 24px; }
    .afp-plan-content h3 { font-size: 15px; padding: 10px; }
    
    .afp-plan-actions { flex-direction: column; width: 100%; }
    .afp-btn { width: 100%; }
}

/* =========================================
   6. Responsive - تبلت و دسکتاپ
   ========================================= */

@media (min-width: 769px) {
    .afp-form-container {
        padding: 32px;
        margin: 40px auto;
    }

    .afp-form-row {
        grid-template-columns: 1fr 1fr;
        gap: 16px;
    }

    .afp-form-header h2 {
        font-size: 28px;
    }

    .afp-submit-btn {
        width: auto;
        min-width: 250px;
        max-width: 400px;
    }

    .afp-analysis-grid {
        grid-template-columns: repeat(4, 1fr);
        gap: 15px;
    }

    .afp-plan-wrapper {
        padding: 32px;
    }
}

@media (min-width: 1024px) {
    .afp-form-container {
        padding: 40px;
    }

    .afp-plan-view-container {
        padding: 0 20px;
    }
}

/* =========================================
   7. پشتیبانی RTL
   ========================================= */

html[dir="rtl"] .afp-form-group .required { 
    margin-right: 0; 
    margin-left: 4px; 
}

html[dir="rtl"] .afp-legend-color {
    margin-left: 0;
    margin-right: 10px;
}

html[dir="rtl"] .afp-form-group select {
    background-position: right 12px center;
    padding-right: 15px;
    padding-left: 40px;
}

html[dir="rtl"] .afp-plan-content h3 {
    border-right: none;
    border-left: 4px solid #0284c7;
}

/* =========================================
   8. بهبودهای دسترسی و عملکرد
   ========================================= */

.afp-submit-btn:focus-visible,
.afp-btn:focus-visible,
.afp-form-group input:focus-visible,
.afp-form-group select:focus-visible,
.afp-form-group textarea:focus-visible,
.afp-auth-tab:focus-visible {
    outline: 3px solid var(--primary-color);
    outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

.afp-submit-btn:active,
.afp-btn:active,
.afp-checkbox-label:active,
.afp-auth-tab:active {
    opacity: 0.8;
}

.afp-error-message {
    color: var(--error-color);
    font-size: 13px;
    margin-top: 6px;
    display: flex;
    align-items: center;
    gap: 4px;
}

.afp-success-message {
    color: var(--success-color);
    background: #f0fdf4;
    padding: 12px 16px;
    border-radius: var(--radius-sm);
    border-right: 4px solid var(--success-color);
    margin: 16px 0;
}