/* =======================================
   1. Variables & Reset
======================================= */
:root {
    --primary: #164397;
    --secondary: #065DFF;
    --accent: #00B462; /* سبز */
    --csv-color: #e08e0b; /* نارنجی */
    --pdf-color: #dc3545;
    --text-dark: #333;
    --text-white: #fff;
    --text-placeholder: #999;
    --bg-input: #fff;
    --radius: 10px;
    --shadow-header: 0 4px 15px rgba(0, 0, 0, 0.05);
    --shadow-dropdown: 0 10px 40px rgba(0, 0, 0, 0.12);
}

.tejaratno-branches-wrapper, .tejaratno-medicals-wrapper {
    font-family: inherit;
    direction: rtl;
    background: transparent !important;
    padding: 10px 0;
    margin: 20px auto;
    max-width: 100%;
    box-sizing: border-box;
}

*, *::before, *::after { box-sizing: border-box; }

/* =======================================
   2. Grid Layout
======================================= */
.modern-filter-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 15px;
    margin-bottom: 25px;
    align-items: start;
}
.search-box-wrapper { grid-column: 1 / -1; position: relative; }
.modern-filter-grid select { display: block; position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); border: 0; opacity: 0; z-index: -100; }

/* =======================================
   3. Inputs & Select2
======================================= */
.modern-filter-grid input[type="text"],
.select2-container--default .select2-selection--single {
    height: 50px !important;
    min-height: 50px !important;
    background-color: var(--bg-input) !important;
    border: 1px solid #e0e0e0 !important;
    border-radius: var(--radius) !important;
    color: var(--text-dark) !important;
    font-size: 14px;
    width: 100%;
    outline: none;
    box-shadow: none !important;
    display: flex !important;
    align-items: center !important;
    transition: border-color 0.3s ease;
    position: relative;
    box-sizing: border-box !important;
}

.modern-filter-grid input[type="text"] {
    padding: 0 15px !important;
    margin: 0 !important;
    line-height: normal !important;
}

.modern-filter-grid input[type="text"]:focus,
.select2-container--default.select2-container--open .select2-selection--single {
    border-color: var(--secondary) !important;
}

/* Select2 Internals */
.select2-container { width: 100% !important; }
.select2-container .select2-selection--single .select2-selection__rendered {
    line-height: 48px !important; padding-right: 15px !important; padding-left: 40px !important; color: #444 !important; width: 100%; display: block;
}
.select2-container--default .select2-selection--single .select2-selection__arrow {
    height: 48px !important; width: 30px !important; position: absolute !important; top: 1px !important; left: 1px !important; right: auto !important; background: transparent;
}
.select2-container--default .select2-selection--single .select2-selection__arrow b {
    border-color: #888 transparent transparent transparent !important; border-width: 5px 4px 0 4px !important; margin-top: -2px !important; left: 50% !important; top: 50% !important;
}
.select2-container--default .select2-selection--single .select2-selection__clear {
    position: absolute !important; left: 30px !important; top: 50% !important; transform: translateY(-50%);
    width: 20px; height: 20px; line-height: 20px; font-size: 18px; color: #bbb !important; font-weight: bold; z-index: 10; cursor: pointer; margin: 0 !important;
}
.select2-container--default .select2-selection--single .select2-selection__placeholder { color: var(--text-placeholder) !important; }

/* Dropdown */
.select2-dropdown { border: 1px solid #e0e0e0 !important; border-radius: var(--radius) !important; box-shadow: var(--shadow-dropdown) !important; overflow: hidden; z-index: 99999; background: #fff; padding: 0 !important; margin-top: -19px !important; }
.select2-search--dropdown { padding: 8px !important; }
.select2-search--dropdown .select2-search__field { border: 1px solid #eee !important; border-radius: 8px !important; padding: 8px 10px !important; outline: none !important; }
.select2-search--dropdown .select2-search__field:focus { border-color: var(--secondary) !important; }
.select2-results__options { max-height: 250px !important; }
.select2-results__option { padding: 10px 15px !important; font-size: 13px !important; color: #555; border-bottom: 1px solid #f9f9f9; }
.select2-results__option:last-child { border-bottom: none; }
.select2-container--default .select2-results__option--highlighted[aria-selected] { background-color: #f0f7ff !important; color: var(--secondary) !important; }
.select2-container--default .select2-results__option[aria-selected="true"] { background-color: #e6f2ff !important; color: var(--primary) !important; font-weight: bold; }

/* =======================================
   4. Search & Clear
======================================= */
.search-input { padding-left: 45px !important; }
.clear-search-btn {
    position: absolute; left: 12px; top: 50%; transform: translateY(-50%); width: 26px; height: 26px;
    line-height: 26px; text-align: center; font-size: 20px; color: #bbb; cursor: pointer; background: transparent;
    border-radius: 50%; transition: all 0.2s; z-index: 10; display: none;
}
.clear-search-btn:hover { background: #ffeeee; color: #ff4d4d; }

/* =======================================
   5. Table Styles
======================================= */
.branches-table-responsive, .medicals-table-responsive { width: 100%; overflow-x: auto; margin-top: 15px; }
.branches-table, .medicals-table { width: 100%; border-collapse: separate; border-spacing: 0 8px; min-width: 900px; }

/* Header */
.branches-table thead th, .medicals-table thead th {
    background: #164397 !important; color: #ffffff !important; padding: 18px 15px;
    font-weight: 500; font-size: 15px; text-align: center; border: none; white-space: nowrap; box-shadow: var(--shadow-header);
}
.branches-table thead th:first-child, .medicals-table thead th:first-child { border-radius: 0 var(--radius) var(--radius) 0; }
.branches-table thead th:last-child, .medicals-table thead th:last-child { border-radius: var(--radius) 0 0 var(--radius); }

/* Body */
.branches-table tbody tr, .medicals-table tbody tr { background: #fff; box-shadow: 0 2px 8px rgba(0,0,0,0.02); transition: none; }
.branches-table tbody tr:hover, .medicals-table tbody tr:hover { background-color: #fff !important; transform: none !important; box-shadow: 0 2px 8px rgba(0,0,0,0.02) !important; }

.branches-table td, .medicals-table td { padding: 16px 12px; font-size: 13px; color: #555; text-align: center; border: none; vertical-align: middle; }
.branches-table td:first-child, .medicals-table td:first-child { border-radius: 0 var(--radius) var(--radius) 0; }
.branches-table td:last-child, .medicals-table td:last-child { border-radius: var(--radius) 0 0 var(--radius); }

/* Links & Buttons */
.phone-link { color: var(--text-dark); font-weight: 700; text-decoration: none; font-family: inherit; direction: ltr; display: inline-block; }
.phone-link:hover { color: var(--secondary); }

.action-btn { display: inline-flex; align-items: center; justify-content: center; height: 36px; padding: 0 15px; border-radius: 8px; text-decoration: none; font-size: 12px; border: none; transition: background-color 0.2s; color: #fff !important; margin: 2px; cursor: pointer; }
.btn-map { background: var(--secondary); }
.btn-map:hover { background: var(--primary); transform: none !important; }
.btn-details { background: var(--accent); }
.btn-details:hover { background: #009652; transform: none !important; }
.action-btn.disabled { background: #e0e0e0 !important; color: #999 !important; cursor: default; pointer-events: none; box-shadow: none; transform: none; }

.map-mobile { display: none !important; }
.map-desktop { display: inline-flex !important; }
.status-icon svg { width: 24px; height: 24px; display: inline-block; vertical-align: middle; }

/* =======================================
   6. Export Buttons & Results Count (FIXED)
======================================= */
.export-buttons-wrapper {
    display: flex;
    align-items: center;
    justify-content: space-between; /* Count right, Buttons left */
    margin-bottom: 20px;
    flex-wrap: wrap;
    gap: 15px;
}

/* گروه دکمه‌ها برای چسبیدن به چپ */
.download-btn-group {
    display: flex;
    gap: 10px;
    margin-right: auto; /* در حالت راست‌چین، این یعنی برو به چپ‌ترین حالت ممکن */
}

.download-btn {
    background: #fff; border: 1px solid #ccc; border-radius: var(--radius); padding: 0 20px; height: 40px;
    display: flex; align-items: center; justify-content: center; gap: 8px; font-weight: 600; cursor: pointer;
    font-size: 13px; min-width: 100px; transition: all 0.3s; text-decoration: none !important;
}
.btn-excel { border-color: var(--accent); color: var(--accent); }
.btn-excel:hover { background: var(--accent); color: #fff; }
.btn-csv { border-color: var(--csv-color); color: var(--csv-color); }
.btn-csv:hover { background: var(--csv-color); color: #fff; }
.download-btn:disabled { opacity: 0.7; cursor: not-allowed; pointer-events: none; }

/* تعداد نتایج */
.results-count {
    font-size: 14px; color: var(--primary); font-weight: 700; background: #eef4ff; padding: 8px 15px; border-radius: var(--radius); display: none;
}
.medicals-count-visible { display: inline-block !important; }

.spinner-loader { width: 16px; height: 16px; border: 2px solid currentColor; border-bottom-color: transparent; border-radius: 50%; display: inline-block; box-sizing: border-box; animation: rotation 1s linear infinite; }
@keyframes rotation { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }

/* =======================================
   7. Details Modal
======================================= */
.tejaratno-modal-overlay { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.6); z-index: 999999; display: none; align-items: center; justify-content: center; backdrop-filter: blur(2px); }
.tejaratno-modal-content { background: #fff; width: 90%; max-width: 500px; border-radius: 12px; padding: 25px; position: relative; box-shadow: 0 10px 40px rgba(0,0,0,0.2); animation: slideUp 0.3s ease; }
@keyframes slideUp { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
.tejaratno-modal-close { position: absolute; left: 15px; top: 15px; font-size: 24px; color: #999; cursor: pointer; line-height: 1; }
.tejaratno-modal-close:hover { color: #333; }
.tejaratno-modal-title { font-size: 18px; font-weight: bold; margin-bottom: 15px; color: var(--primary); padding-bottom: 10px; border-bottom: 1px solid #eee; }
.tejaratno-modal-body { font-size: 14px; line-height: 1.8; color: #444; max-height: 60vh; overflow-y: auto; text-align: justify; }

/* =======================================
   8. Pagination
======================================= */
.pagination-modern { display: flex; justify-content: center; align-items: center; gap: 6px; margin-top: 30px; clear: both; }
.pagination-modern .page-link { display: flex; align-items: center; justify-content: center; width: 40px; height: 40px; background: #fff; border: 1px solid #eee; border-radius: 8px; color: #777; text-decoration: none; transition: all 0.2s; }
.pagination-modern .page-link:hover, .pagination-modern .page-link.active { background: var(--secondary); border-color: var(--secondary); color: #fff; box-shadow: 0 4px 10px rgba(6, 93, 255, 0.2); }

@media (max-width: 992px) { .modern-filter-grid { grid-template-columns: 1fr 1fr; } .search-box-wrapper { grid-column: span 2; } }

/* =======================================
   9. MOBILE VIEW
======================================= */
@media (max-width: 600px) {
    .map-mobile { display: inline-flex !important; }
    .map-desktop { display: none !important; }

    .tejaratno-branches-wrapper, .tejaratno-medicals-wrapper { background: transparent !important; padding: 0 !important; box-shadow: none !important; }
    .branches-table-responsive, .medicals-table-responsive { overflow-x: hidden; }
    .branches-table, .medicals-table { min-width: 0 !important; width: 100% !important; display: block; border: none; }
    .modern-filter-grid { grid-template-columns: 1fr !important; display: grid; } 
    .search-box-wrapper { grid-column: 1 / -1 !important; }
    
    .branches-table thead, .medicals-table thead { display: none; }
    .branches-table tbody, .medicals-table tbody { display: block; width: 100%; }
    
    .branches-table tbody tr, .medicals-table tbody tr {
        display: flex; flex-direction: column; margin-bottom: 15px; padding: 15px; 
        border: 1px solid #e1e5ea; border-radius: 12px; background: #fff !important;
        box-shadow: 0 4px 10px rgba(0,0,0,0.04); width: 100%; box-sizing: border-box;
        transition: none !important; transform: none !important;
    }
    
    .branches-table td, .medicals-table td {
        display: flex; justify-content: space-between; align-items: center; text-align: left; 
        border: none; border-bottom: 1px dashed #f0f0f0; padding: 12px 0; width: 100%;
        white-space: normal !important; word-wrap: break-word; background: transparent !important;
    }
    .branches-table td:last-child, .medicals-table td:last-child { border: none; padding-top: 15px; }
    .branches-table td::before, .medicals-table td::before {
        content: attr(data-label); font-weight: 700; color: #999; font-size: 12px; flex-shrink: 0; margin-left: 15px; text-align: right;
    }
    .map-btn, .btn-details { width: auto !important; min-width: 80px; }
    
    /* فیکس دکمه‌ها در موبایل */
    .export-buttons-wrapper { flex-direction: column; align-items: stretch; gap: 15px; }
    .download-btn-group { margin-right: 0; display: flex; gap: 10px; }
    .download-btn { flex: 1; font-size: 12px; padding: 0 10px; }
    .results-count { text-align: center; margin-right: 0; width: 100%; order: -1; }
}

.select2-hidden-accessible { border: 0 !important; clip: rect(0 0 0 0) !important; height: 1px !important; width: 1px !important; margin: -1px !important; overflow: hidden !important; padding: 0 !important; position: absolute !important; }

/* =======================================
   10. PRINT
======================================= */
@media print {
    @page { size: landscape; margin: 1cm; }
    body { visibility: hidden !important; background: #fff !important; }
    
    .tejaratno-branches-wrapper, .tejaratno-medicals-wrapper {
        visibility: visible !important; position: absolute !important; left: 0 !important; top: 0 !important;
        width: 100% !important; margin: 0 !important; padding: 0 !important;
        background: #fff !important; border: none !important; box-shadow: none !important;
    }
    .tejaratno-branches-wrapper *, .tejaratno-medicals-wrapper * { visibility: visible !important; }

    .modern-filter-grid, .export-buttons-wrapper, .pagination-modern, .map-btn, .btn-details { display: none !important; }

    .branches-table-responsive, .medicals-table-responsive { overflow: visible !important; display: block !important; }
    .branches-table, .medicals-table {
        width: 100% !important; border-collapse: collapse !important; border: 1px solid #000 !important;
    }
    
    .branches-table th, .medicals-table th {
        background-color: #eee !important; color: #000 !important;
        border: 1px solid #000 !important; padding: 6px !important; font-size: 10pt !important;
        box-shadow: none !important;
    }
    .branches-table td, .medicals-table td {
        border: 1px solid #000 !important; color: #000 !important;
        padding: 6px !important; font-size: 10pt !important;
    }

    /* مخفی کردن ستون‌های عملیاتی */
    .branches-table th:nth-last-child(1), .branches-table td:nth-last-child(1),
    .branches-table th:nth-last-child(2), .branches-table td:nth-last-child(2),
    .medicals-table th:nth-last-child(1), .medicals-table td:nth-last-child(1),
    .medicals-table th:nth-last-child(2), .medicals-table td:nth-last-child(2) { display: none !important; }

    a { text-decoration: none !important; color: #000 !important; pointer-events: none !important; }
    a[href]:after { content: none !important; }
    .tejaratno-modal-overlay { display: none !important; }
}