/* FILE: static/css/mobile-responsive.css */
/* ═══════════════════════════════════════════════════════════
   Hakan Paylar CRM — Responsive Fixes
   بهینه‌سازی کامل برای موبایل و تبلت
   ═══════════════════════════════════════════════════════════ */

/* ─── پایه: جلوگیری از overflow افقی ─── */
html, body {
    overflow-x: hidden;
    max-width: 100vw;
}

.main-content,
.content,
.crm-page-card,
.page-actions-row {
    max-width: 100%;
    overflow-x: hidden;
}

/* ═══════════════════════════════════════════════════════════
   ۱) TABLES — جدول‌های قابل اسکرول
   ═══════════════════════════════════════════════════════════ */

.table-responsive {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    position: relative;
    padding-bottom: 4px;
}

.table-responsive::-webkit-scrollbar {
    height: 6px;
}
.table-responsive::-webkit-scrollbar-track {
    background: #f1f5f9;
    border-radius: 3px;
}
.table-responsive::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 3px;
}
.table-responsive::-webkit-scrollbar-thumb:hover {
    background: #94a3b8;
}

/* راهنمای بصری: قبل و بعد از جدول سایه */
.table-responsive::before,
.table-responsive::after {
    content: '';
    position: absolute;
    top: 0;
    bottom: 0;
    width: 20px;
    pointer-events: none;
    z-index: 2;
    opacity: 0;
    transition: opacity 0.2s ease;
}
.table-responsive::before {
    right: 0;
    background: linear-gradient(to left, rgba(0,0,0,0.08), transparent);
}
.table-responsive::after {
    left: 0;
    background: linear-gradient(to right, rgba(0,0,0,0.08), transparent);
}
.table-responsive.has-scroll-left::before { opacity: 1; }
.table-responsive.has-scroll-right::after { opacity: 1; }

/* روی موبایل جدول‌ها فشرده‌تر */
@media (max-width: 767px) {
    .table > :not(caption) > * > * {
        padding: 8px 6px;
        font-size: 13px;
        white-space: nowrap;
    }
    .table thead th {
        font-size: 12px;
        font-weight: 600;
    }
}

/* ═══════════════════════════════════════════════════════════
   ۲) KPI GRID — کارت‌های آماری
   ═══════════════════════════════════════════════════════════ */

.kpi-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 12px;
}

@media (max-width: 991px) {
    .kpi-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 10px;
    }
}

@media (max-width: 575px) {
    .kpi-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 8px;
    }
    .kpi-card {
        padding: 12px 10px;
        flex-direction: column;
        align-items: flex-start;
        gap: 6px;
    }
    .kpi-card .kpi-icon {
        width: 32px;
        height: 32px;
        font-size: 14px;
    }
    .kpi-card .kpi-info span {
        font-size: 11px;
    }
    .kpi-card .kpi-info h3 {
        font-size: 18px;
        margin: 2px 0 0;
    }
    .kpi-card .kpi-info small {
        font-size: 10px;
    }
}

/* ═══════════════════════════════════════════════════════════
   ۳) PAGE ACTIONS — ردیف دکمه‌های بالای صفحه
   ═══════════════════════════════════════════════════════════ */

.page-actions-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 12px;
}

@media (max-width: 767px) {
    .page-actions-row {
        flex-direction: column;
        align-items: stretch;
    }
    .page-actions-row > * {
        width: 100%;
    }
    .page-actions-row .btn {
        width: 100%;
        justify-content: center;
    }
    .page-actions-row h2 {
        font-size: 20px;
        margin: 4px 0;
    }
    .page-actions-row p {
        font-size: 13px;
    }
}

/* ═══════════════════════════════════════════════════════════
   ۴) CHARTS — نمودارهای Apex
   ═══════════════════════════════════════════════════════════ */

.apexcharts-canvas,
.apexcharts-svg {
    max-width: 100% !important;
}

@media (max-width: 767px) {
    /* نمودارهای کوچیک‌تر با padding کم */
    .crm-page-card .apexcharts-canvas {
        margin: 0 -8px;
    }
    /* فونت لِیبل‌ها کوچیک‌تر */
    .apexcharts-text {
        font-size: 10px !important;
    }
    .apexcharts-legend {
        font-size: 11px !important;
    }
}

/* ═══════════════════════════════════════════════════════════
   ۵) TOPBAR — نوار بالا
   ═══════════════════════════════════════════════════════════ */

@media (max-width: 767px) {
    .topbar {
        padding: 8px 12px;
        gap: 8px;
    }
    .page-heading-kicker {
        font-size: 9px;
    }
    .page-title {
        font-size: 15px;
    }
    .user-menu-copy {
        display: none !important;
    }
    .user-menu-trigger {
        padding: 4px;
    }
    .notification-btn {
        padding: 6px;
    }
}

/* ═══════════════════════════════════════════════════════════
   ۶) CARDS — کارت‌های عمومی
   ═══════════════════════════════════════════════════════════ */

@media (max-width: 767px) {
    .crm-page-card {
        padding: 14px 12px;
        border-radius: 10px;
        margin-bottom: 12px;
    }
    .crm-page-card h5 {
        font-size: 15px;
        margin-bottom: 10px;
    }
    .crm-page-card h4 {
        font-size: 17px;
    }
}

/* ═══════════════════════════════════════════════════════════
   ۷) FORMS — فرم‌ها روی موبایل
   ═══════════════════════════════════════════════════════════ */

@media (max-width: 767px) {
    .form-control,
    .form-select {
        font-size: 16px !important; /* ← جلوگیری از zoom خودکار iOS */
        padding: 10px 12px;
        min-height: 42px;
    }
    .form-label {
        font-size: 13px;
        font-weight: 500;
    }
    /* دکمه‌های فرم full-width */
    form .btn:not(.btn-sm):not(.btn-close) {
        width: 100%;
        margin-bottom: 8px;
        padding: 12px;
    }
    /* کنار هم قرار گرفتن دکمه‌های عملیاتی */
    form .d-flex.gap-2 > .btn {
        width: auto;
        flex: 1;
    }
    textarea.form-control {
        min-height: 100px;
    }
}

/* ═══════════════════════════════════════════════════════════
   ۸) MODALS — مودال‌ها
   ═══════════════════════════════════════════════════════════ */

@media (max-width: 575px) {
    .modal-dialog {
        margin: 8px;
        max-width: calc(100% - 16px);
    }
    .modal-body {
        padding: 14px 12px;
    }
    .modal-header,
    .modal-footer {
        padding: 12px 14px;
    }
    .modal-footer .btn {
        flex: 1;
    }
}

/* ═══════════════════════════════════════════════════════════
   ۹) TABS / SUB-NAV — تب‌های افقی قابل اسکرول
   ═══════════════════════════════════════════════════════════ */

.nav-tabs,
.tabs-scroll {
    overflow-x: auto;
    overflow-y: hidden;
    flex-wrap: nowrap;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 2px;
}
.nav-tabs::-webkit-scrollbar,
.tabs-scroll::-webkit-scrollbar {
    display: none;
}
.nav-tabs .nav-link,
.tabs-scroll .nav-link {
    white-space: nowrap;
    flex-shrink: 0;
}

/* ═══════════════════════════════════════════════════════════
   ۱۰) SIDEBAR — رفع مشکلات موبایل (نسخه‌ی قطعی)
   ═══════════════════════════════════════════════════════════ */

/* اسکرول مستقل برای خود sidebar-nav */
.sidebar {
    display: flex;
    flex-direction: column;
}

.sidebar-nav {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    padding-bottom: 12px;
    overscroll-behavior: contain;
}

.sidebar-nav::-webkit-scrollbar {
    width: 4px;
}
.sidebar-nav::-webkit-scrollbar-track {
    background: transparent;
}
.sidebar-nav::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.15);
    border-radius: 2px;
}

/* ─── روی موبایل: سایدبار به‌طور پیش‌فرض مخفی باشه ─── */
@media (max-width: 991px) {
    .sidebar {
        position: fixed !important;
        top: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        width: 280px !important;
        max-width: 85vw !important;
        height: 100vh !important;
        height: 100dvh !important;
        max-height: 100dvh !important;
        z-index: 1050 !important;
        overflow-y: auto;
        overscroll-behavior: contain;

        /* حالت پیش‌فرض: مخفی */
        transform: translateX(100%);
        visibility: hidden;
        opacity: 0;

        /* انیمیشن نرم */
        transition:
            transform 0.28s cubic-bezier(0.4, 0, 0.2, 1),
            opacity 0.2s ease,
            visibility 0.28s ease;
    }

    /* حالت باز */
    .sidebar.sidebar-open {
        transform: translateX(0) !important;
        visibility: visible !important;
        opacity: 1 !important;
    }

    .sidebar-nav {
        padding-bottom: 40px;
    }

    /* Backdrop */
    .sidebar-backdrop {
        position: fixed !important;
        inset: 0 !important;
        background: rgba(0, 0, 0, 0.45) !important;
        z-index: 1040 !important;
        opacity: 0;
        visibility: hidden;
        pointer-events: none;
        transition: opacity 0.25s ease, visibility 0.25s ease;
    }
    .sidebar-backdrop.show {
        opacity: 1 !important;
        visibility: visible !important;
        pointer-events: auto !important;
    }

    /* وقتی سایدبار بازه، body اسکرول نشه */
    body.sidebar-open {
        overflow: hidden !important;
    }
}

/* ═══════════════════════════════════════════════════════════
   ۱۱) UTILITY — کلاس‌های کمکی
   ═══════════════════════════════════════════════════════════ */

@media (max-width: 767px) {
    /* جداکننده‌های عمودی روی موبایل تبدیل به افقی */
    .vr {
        display: none;
    }

    /* همیشه اسکرول افقی برای ردیف‌های flex بلند */
    .d-flex.flex-wrap {
        row-gap: 8px;
    }

    /* Padding عمومی content */
    .content {
        padding: 12px 10px 80px 10px; /* ← پدینگ پایین برای bottom-nav */
    }

    /* h1..h6 کوچیک‌تر */
    h1 { font-size: 22px; }
    h2 { font-size: 20px; }
    h3 { font-size: 18px; }
    h4 { font-size: 16px; }
    h5 { font-size: 15px; }
    h6 { font-size: 14px; }

    /* Alert فشرده‌تر */
    .alert {
        padding: 10px 12px;
        font-size: 13px;
    }

    /* Badges کوچیک‌تر */
    .badge {
        font-size: 11px;
        padding: 4px 8px;
    }

    /* Buttons */
    .btn {
        font-size: 14px;
        padding: 8px 14px;
    }
    .btn-sm {
        font-size: 12px;
        padding: 5px 10px;
    }
}

/* ═══════════════════════════════════════════════════════════
   ۱۲) TABLE CELLS — جلوگیری از شکستن متن
   ═══════════════════════════════════════════════════════════ */

@media (max-width: 767px) {
    .table td,
    .table th {
        max-width: 180px;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    /* ستون اول (کد) و عملیات همیشه دیده بشن */
    .table td:first-child,
    .table th:first-child,
    .table td:last-child,
    .table th:last-child {
        position: sticky;
        background: inherit;
        z-index: 1;
    }
    .table td:first-child,
    .table th:first-child {
        right: 0;
        background: #fff;
        box-shadow: -2px 0 4px rgba(0,0,0,0.03);
    }
    .table td:last-child,
    .table th:last-child {
        left: 0;
        background: #fff;
        box-shadow: 2px 0 4px rgba(0,0,0,0.03);
    }
    /* headercells رنگ پس‌زمینه می‌گیرن */
    .table thead th:first-child,
    .table thead th:last-child {
        background: #f8fafc;
    }
}

/* ═══════════════════════════════════════════════════════════
   ۱۳) HIDE / SHOW کمک‌کننده
   ═══════════════════════════════════════════════════════════ */

@media (max-width: 767px) {
    .hide-mobile { display: none !important; }
    .text-end { text-align: right !important; }
}

@media (min-width: 768px) {
    .show-only-mobile { display: none !important; }
}

/* ═══════════════════════════════════════════════════════════
   ۱۴) SCROLLBAR — زیباتر
   ═══════════════════════════════════════════════════════════ */

* {
    scrollbar-width: thin;
    scrollbar-color: #cbd5e1 transparent;
}

/* ═══════════════════════════════════════════════════════════
   ۱۵) TOUCH TARGETS — حداقل ۴۴px برای انگشت
   ═══════════════════════════════════════════════════════════ */

@media (max-width: 767px) {
    .nav-link,
    .btn,
    .form-check-input,
    .icon-btn,
    button {
        min-height: 40px;
    }
    .form-check-input {
        min-height: unset;
        width: 20px;
        height: 20px;
    }
}

/* ═══════════════════════════════════════════════════════════
   ۱۶) PRINT — چاپ
   ═══════════════════════════════════════════════════════════ */

@media print {
    .sidebar,
    .topbar,
    .bottom-nav,
    .no-print {
        display: none !important;
    }
    .main-content,
    .content {
        padding: 0 !important;
        margin: 0 !important;
    }
}