/* ================================================
   MOBILE-FIRST RESPONSIVE OVERRIDES
   منصة متابعة طلاب الدراسات العليا
   v2.5.0 — يُحمّل بعد style.css و saas-overrides.css
   ================================================ */

/* --- Safe areas (iPhone notch / Android gesture bar) --- */
:root {
    --nav-height: 64px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    --touch-min: 44px;
}

/* --- Global mobile performance & touch --- */
@media (max-width: 991px) {
    html {
        -webkit-text-size-adjust: 100%;
        text-size-adjust: 100%;
    }

    body {
        background-attachment: scroll !important;
        padding-bottom: var(--safe-bottom);
    }

    /* Prevent iOS zoom on input focus */
    input[type="text"],
    input[type="number"],
    input[type="email"],
    input[type="password"],
    input[type="tel"],
    input[type="date"],
    input[type="url"],
    input[type="search"],
    select,
    textarea {
        font-size: 16px !important;
    }

    button,
    .btn-primary,
    .btn-secondary,
    .nav-link,
    a.portal-card,
    .portal-card {
        -webkit-tap-highlight-color: rgba(108, 76, 241, 0.15);
        touch-action: manipulation;
    }

    /* Reduce heavy animations on mobile */
    .hero-section::before {
        animation: none !important;
    }

    .scroll-reveal {
        opacity: 1 !important;
        transform: none !important;
        transition: none !important;
    }

    .premium-card:hover,
    .portal-card:hover,
    .stat-item:hover {
        transform: none;
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

/* --- Navigation (unified mobile menu) --- */
@media (max-width: 991px) {
    .nav-premium {
        height: var(--nav-height) !important;
        min-height: var(--nav-height);
        padding: 0 16px !important;
        padding-top: var(--safe-top) !important;
        flex-direction: row !important;
        align-items: center !important;
        gap: 0 !important;
        background: rgba(255, 255, 255, 0.95) !important;
        backdrop-filter: blur(16px);
        -webkit-backdrop-filter: blur(16px);
        border-bottom: 1px solid rgba(108, 76, 241, 0.1);
        box-shadow: 0 2px 12px rgba(31, 31, 40, 0.06);
    }

    .nav-premium.scrolled {
        height: var(--nav-height) !important;
    }

    .nav-actions {
        margin-right: auto;
        gap: 8px;
    }

    #visit-counter {
        display: none !important;
    }

    .mobile-toggle {
        display: flex !important;
        align-items: center;
        justify-content: center;
        width: var(--touch-min);
        height: var(--touch-min);
        min-width: var(--touch-min);
        min-height: var(--touch-min);
        border-radius: 10px;
        background: rgba(108, 76, 241, 0.08);
        color: var(--primary) !important;
        border: 1px solid rgba(108, 76, 241, 0.15);
        padding: 0 !important;
        z-index: 1200;
    }

    .mobile-toggle i {
        width: 22px !important;
        height: 22px !important;
        color: var(--primary) !important;
        stroke: var(--primary) !important;
    }

    /* Overlay backdrop */
    .nav-mobile-overlay {
        position: fixed;
        inset: 0;
        background: rgba(15, 23, 42, 0.45);
        z-index: 1090;
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.25s ease, visibility 0.25s ease;
        -webkit-backdrop-filter: blur(2px);
        backdrop-filter: blur(2px);
    }

    .nav-mobile-overlay.active {
        opacity: 1;
        visibility: visible;
    }

    .nav-links {
        position: fixed !important;
        top: calc(var(--nav-height) + var(--safe-top)) !important;
        right: 0 !important;
        left: 0 !important;
        width: 100% !important;
        height: auto !important;
        max-height: calc(100vh - var(--nav-height) - var(--safe-top));
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        display: flex !important;
        flex-direction: column !important;
        gap: 0 !important;
        padding: 8px 12px 20px !important;
        margin: 0 !important;
        background: #ffffff !important;
        box-shadow: 0 12px 40px rgba(31, 31, 40, 0.12);
        border-top: 1px solid rgba(108, 76, 241, 0.1);
        z-index: 1150;
        transform: translateY(-110%);
        opacity: 0;
        visibility: hidden;
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1),
                    opacity 0.25s ease,
                    visibility 0.25s ease;
        pointer-events: none;
    }

    .nav-links.active {
        transform: translateY(0) !important;
        opacity: 1 !important;
        visibility: visible !important;
        pointer-events: auto;
    }

    .nav-link {
        color: var(--text-main) !important;
        font-size: 1.05rem !important;
        font-weight: 600;
        width: 100% !important;
        text-align: center;
        padding: 14px 16px !important;
        min-height: var(--touch-min);
        display: flex;
        align-items: center;
        justify-content: center;
        border-bottom: 1px solid rgba(108, 76, 241, 0.08);
        border-radius: 10px;
    }

    .nav-link:last-child {
        border-bottom: none;
    }

    body.nav-menu-open {
        overflow: hidden;
        position: fixed;
        width: 100%;
    }
}

/* --- Hero & Search --- */
@media (max-width: 768px) {
    .hero-section {
        min-height: auto !important;
        padding: calc(var(--nav-height) + var(--safe-top) + 24px) 16px 48px !important;
    }

    .hero-title {
        font-size: clamp(1.5rem, 5.5vw, 2rem) !important;
        line-height: 1.35 !important;
        margin-bottom: 12px;
    }

    .hero-subtitle {
        font-size: 0.95rem !important;
        margin-bottom: 24px;
        padding: 0 4px;
    }

    .search-box {
        flex-direction: column !important;
        border-radius: 20px !important;
        padding: 12px !important;
        gap: 10px;
        max-width: 100%;
    }

    .input-wrapper {
        padding: 0 16px !important;
        width: 100%;
    }

    .input-wrapper input {
        padding: 14px 0 !important;
        font-size: 16px !important;
    }

    /* Full-width search button with visible text on mobile */
    .search-box .btn-primary,
    #search-btn {
        width: 100% !important;
        height: var(--touch-min) !important;
        min-height: var(--touch-min);
        border-radius: 12px !important;
        text-indent: 0 !important;
        font-size: 1rem !important;
        font-weight: 700;
    }

    #search-btn::after {
        display: none !important;
    }

    #search-btn::before {
        content: 'بحث الآن';
    }

    .hero-quick-actions {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 10px !important;
        margin-top: 20px !important;
        padding: 0 4px;
    }

    .btn-hero-marketing,
    .btn-hero-student,
    .btn-hero-supervisor,
    .btn-hero-activities {
        width: 100% !important;
        justify-content: center !important;
        min-height: var(--touch-min);
        padding: 12px 20px !important;
    }

    .academic-divider .line {
        width: 60px;
    }
}

@media (max-width: 480px) {
    .hero-section {
        padding-bottom: 32px !important;
    }

    .search-container {
        padding: 0;
    }
}

/* --- Quick portals & cards --- */
@media (max-width: 768px) {
    .quick-portals {
        margin: 16px auto 24px !important;
        padding: 0 16px;
    }

    .portal-grid {
        grid-template-columns: 1fr !important;
        gap: 12px;
    }

    .portal-card {
        padding: 18px !important;
        min-height: var(--touch-min);
    }

    .premium-grid,
    .download-grid {
        grid-template-columns: 1fr !important;
        gap: 16px !important;
    }

    .container {
        padding: 0 16px !important;
    }

    .main-layout-grid {
        gap: 24px;
    }

    .content-section {
        padding: 32px 0 !important;
    }

    .section-subtitle h3 {
        font-size: 1.25rem !important;
    }
}

/* --- Stats --- */
@media (max-width: 768px) {
    .stats-card-group {
        margin-bottom: 32px !important;
    }

    .stats-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 12px !important;
    }

    .stat-item {
        padding: 16px 12px !important;
    }

    .stat-number,
    .stat-value {
        font-size: 1.6rem !important;
    }

    .stat-label,
    .stat-title {
        font-size: 0.8rem !important;
    }
}

@media (max-width: 400px) {
    .stats-grid {
        grid-template-columns: 1fr !important;
    }
}

/* --- Roadmap --- */
@media (max-width: 768px) {
    .roadmap-arrows-container {
        gap: 12px !important;
    }

    .roadmap-arrow-step {
        padding: 18px 14px !important;
    }

    .roadmap-arrow-step h4 {
        font-size: 0.9rem !important;
    }

    .roadmap-arrow-step p {
        font-size: 0.8rem !important;
    }
}

/* --- Modals --- */
@media (max-width: 768px) {
    .modal {
        padding: 12px !important;
        padding-top: calc(12px + var(--safe-top)) !important;
        padding-bottom: calc(12px + var(--safe-bottom)) !important;
        align-items: flex-end !important;
    }

    .modal-content,
    .details-modal-content,
    .archive-search-modal-content {
        width: 100% !important;
        max-width: 100% !important;
        max-height: calc(92vh - var(--safe-bottom));
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        border-radius: 20px 20px 12px 12px !important;
        padding: 24px 18px !important;
        margin: 0 !important;
    }

    .close-modal {
        width: var(--touch-min) !important;
        height: var(--touch-min) !important;
        top: 12px !important;
        left: 12px !important;
    }

    /* Thesis discussion details — scroll body on mobile (hero stays fixed) */
    #thesis-discussion-details-modal.modal {
        align-items: stretch !important;
        padding: 0 !important;
        padding-top: var(--safe-top) !important;
        padding-bottom: var(--safe-bottom) !important;
    }

    #thesis-discussion-details-modal .td-modal-shell {
        display: flex !important;
        flex-direction: column !important;
        max-height: 100% !important;
        height: calc(100dvh - var(--safe-top) - var(--safe-bottom)) !important;
        overflow: hidden !important;
        border-radius: 0 !important;
        margin: 0 !important;
        padding: 0 !important;
    }

    #thesis-discussion-details-modal .td-modal-scroll-body {
        flex: 1 1 auto;
        min-height: 0;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
        touch-action: pan-y;
        padding: 20px 16px 28px !important;
    }

    #thesis-discussion-details-modal .thesis-discussion-hero {
        flex-shrink: 0;
    }
}

/* --- Student result & timeline --- */
@media (max-width: 768px) {
    #student-result-page {
        padding: calc(var(--nav-height) + 16px) 16px 32px !important;
    }

    .result-header {
        flex-direction: column !important;
        gap: 16px !important;
        text-align: center;
    }

    .back-btn {
        width: 100% !important;
        min-height: var(--touch-min);
        justify-content: center !important;
    }

    .timeline {
        flex-direction: column !important;
        gap: 20px;
        padding: 10px 0;
    }

    .timeline::before {
        top: 0;
        bottom: 0;
        left: 20px;
        right: auto;
        width: 4px;
        height: auto;
    }

    .milestone {
        width: 100% !important;
        flex-direction: row !important;
        justify-content: flex-start;
        gap: 16px;
        padding-right: 0;
        padding-left: 48px;
    }
}

/* --- Tables (horizontal scroll) — الحل الجذري للجوال --- */
.table-responsive {
    display: block;
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    overflow-y: visible;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
    touch-action: pan-x pan-y;
    scrollbar-width: thin;
}

.table-responsive::-webkit-scrollbar {
    height: 6px;
}

.table-responsive::-webkit-scrollbar-thumb {
    background: rgba(108, 76, 241, 0.35);
    border-radius: 999px;
}

.table-section,
.table-container {
    overflow: visible !important;
}

.table-section > .table-responsive,
.table-container > .table-responsive {
    margin: 0 -2px;
    padding-bottom: 4px;
}

@media (max-width: 991px) {
    .table-responsive {
        overflow-x: scroll !important;
        scroll-snap-type: x proximity;
    }

    .table-scroll-hint {
        display: block;
        text-align: center;
        font-size: 0.75rem;
        color: var(--text-muted);
        padding: 8px 0 4px;
        opacity: 0.85;
        user-select: none;
    }

    .data-table,
    .admin-table,
    .display,
    .table-responsive > table {
        min-width: 720px;
        width: max-content;
    }

    .dataTables_wrapper {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
    }

    /* أزرار وعناصر تفاعلية — لا تُعطّل اللمس */
    button,
    .btn-icon,
    [data-platform-action],
    .table-actions button {
        pointer-events: auto !important;
        touch-action: manipulation;
        cursor: pointer;
    }

    button svg,
    button i,
    [data-platform-action] svg,
    [data-platform-action] i {
        pointer-events: none;
    }
}

/* --- Forms & admin --- */
@media (max-width: 768px) {
    .form-row-2,
    .form-row-3,
    .dashboard-header {
        flex-direction: column !important;
        gap: 12px !important;
    }

    .form-row-2 > *,
    .form-row-3 > *,
    .header-actions button,
    .header-actions .btn-secondary,
    .header-actions .btn-primary,
    .btn-search-main,
    .management-actions-row button {
        width: 100% !important;
    }

    .admin-layout {
        flex-direction: column !important;
    }

    .admin-sidebar {
        width: 100% !important;
        position: relative !important;
    }

    .admin-main {
        padding: 16px 12px !important;
    }

    .dashboard-grid,
    .charts-row,
    .admin-quick-menu {
        grid-template-columns: 1fr !important;
    }

    .chart-card canvas {
        max-height: 260px;
    }

    .search-actions-centered {
        flex-direction: column;
    }

    .search-actions-centered button {
        width: 100%;
    }

    .results-grid-premium {
        grid-template-columns: 1fr !important;
    }

    .result-item-card {
        flex-direction: column;
        align-items: stretch !important;
        gap: 12px;
    }

    .btn-recall-sm {
        width: 100%;
        justify-content: center;
        min-height: var(--touch-min);
    }
}

/* --- Footer --- */
@media (max-width: 768px) {
    .main-footer {
        padding: 48px 16px 32px !important;
        margin-top: 48px;
        padding-bottom: calc(32px + var(--safe-bottom)) !important;
    }

    .footer-grid {
        grid-template-columns: 1fr !important;
        gap: 32px !important;
        text-align: center;
    }

    .footer-logo {
        justify-content: center;
    }

    .social-links-footer {
        justify-content: center;
    }

    .footer-col-links h4::after,
    .footer-col-contact h4::after {
        right: 50%;
        transform: translateX(50%);
    }
}

/* --- Notifications --- */
@media (max-width: 640px) {
    .notification-container {
        top: calc(var(--nav-height) + var(--safe-top) + 8px) !important;
        right: 12px !important;
        left: 12px !important;
    }
}

/* --- Full-page views --- */
@media (max-width: 768px) {
    .page-main-title {
        font-size: 1.5rem !important;
    }

    .filter-group-premium,
    .management-actions-row {
        flex-direction: column !important;
        gap: 10px;
    }

    .roadmap-main-header {
        flex-direction: column;
        align-items: flex-start !important;
        padding: 16px !important;
    }

    .thesis-tracking-grid {
        grid-template-columns: 1fr !important;
    }

    .tracking-stage-card.active {
        transform: none !important;
    }
}

/* --- Images lazy-load placeholder --- */
img[loading="lazy"] {
    background: #f1f5f9;
}

img[loading="lazy"]:not([src]),
img.lazy-loading {
    min-height: 120px;
}

/* --- Thesis discussion modal (all screens) --- */
#thesis-discussion-details-modal .td-modal-shell {
    display: flex;
    flex-direction: column;
    max-height: calc(92vh - var(--safe-bottom, 0px));
    overflow: hidden;
    padding: 0 !important;
}

#thesis-discussion-details-modal .td-modal-scroll-body {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}

/* --- Very small phones (320px) --- */
@media (max-width: 360px) {
    .nav-premium {
        padding: 0 12px !important;
    }

    .hero-title {
        font-size: 1.35rem !important;
    }

    .portal-info h3 {
        font-size: 1rem !important;
    }
}
