/* ============================================================
   COACHING PLATFORM PRO — RESPONSIVE SYSTEM v3.0
   Strategy: Compact + scroll tables, never break grids
   ============================================================ */

/* --- Base --- */
*, *::before, *::after { box-sizing: border-box; }
html { overflow-x: hidden; scroll-behavior: smooth; }
body, .app-shell { overflow-x: hidden; max-width: 100vw; }
img, video, iframe, canvas { max-width: 100%; height: auto; }
pre, code { white-space: pre-wrap; word-break: break-word; }

/* ============================================================
   SHELL LAYOUT
   ============================================================ */
.shell-content {
    margin-left: 0;
    margin-top: 60px;
    padding: 12px;
    padding-bottom: calc(72px + 12px + env(safe-area-inset-bottom, 0px));
    min-height: calc(100vh - 60px);
    box-sizing: border-box;
}

/* Sidebar: hidden on mobile */
.admin-sidebar, .coach-sidebar, .coachee-sidebar {
    transform: translateX(-100%);
    transition: transform 0.3s cubic-bezier(0.4,0,0.2,1);
}
.admin-sidebar.open, .coach-sidebar.open, .coachee-sidebar.open {
    transform: translateX(0);
    box-shadow: 4px 0 32px rgba(0,0,0,0.18);
    z-index: 200;
}
.sidebar-overlay {
    position: fixed; inset: 0;
    background: rgba(15,23,42,0.5);
    z-index: 199; display: none; opacity: 0;
    transition: opacity 0.3s; backdrop-filter: blur(2px);
}
.sidebar-overlay.show { display: block; opacity: 1; }

/* ============================================================
   DESKTOP (>= 1024px)
   ============================================================ */
@media (min-width: 1024px) {
    body.role-admin .admin-sidebar,
    body.role-coach .coach-sidebar,
    body.role-coachee .coachee-sidebar { transform: translateX(0); }

    body.role-admin .shell-header,
    body.role-coach .shell-header,
    body.role-coachee .shell-header {
        left: 260px !important; right: 0 !important;
        max-width: none !important; transform: none !important;
    }
    body.role-admin .shell-content,
    body.role-coach .shell-content,
    body.role-coachee .shell-content {
        margin-left: 260px !important;
        margin-right: 0 !important;
        max-width: none !important;
        width: auto !important;
        padding: 28px 36px;
        padding-bottom: 36px;
        margin-top: 60px !important;
    }
    body.role-admin .bottom-nav-app,
    body.role-coach .bottom-nav-app,
    body.role-coachee .bottom-nav-app { display: none !important; }

    body.role-admin .sidebar-overlay,
    body.role-coach .sidebar-overlay,
    body.role-coachee .sidebar-overlay { display: none !important; }

    body.role-admin #sidebarToggle,
    body.role-coach #coachSidebarToggle,
    body.role-coachee #coacheeSidebarToggle { display: none !important; }

    body.role-admin .admin-header-title,
    body.role-coach .admin-header-title { flex: 1; display: block !important; }
    body.role-admin .admin-header-logo { display: none !important; }
}

/* ============================================================
   MOBILE (< 1024px)
   ============================================================ */
@media (max-width: 1023px) {
    body.role-admin .shell-header,
    body.role-coach .shell-header,
    body.role-coachee .shell-header { left: 0; right: 0; }

    body.role-admin #sidebarToggle,
    body.role-coach #coachSidebarToggle,
    body.role-coachee #coacheeSidebarToggle { display: flex !important; }

    body.role-admin .admin-header-logo { display: flex !important; flex: 1; }
    body.role-admin .admin-header-title { display: none !important; }

    body.role-admin .shell-content,
    body.role-coach .shell-content,
    body.role-coachee .shell-content {
        margin-left: 0; max-width: 100%;
        padding: 12px;
        padding-bottom: calc(72px + 12px + env(safe-area-inset-bottom, 0px));
        margin-top: 60px;
    }
    body.role-admin .bottom-nav-app,
    body.role-coach .bottom-nav-app,
    body.role-coachee .bottom-nav-app { display: flex !important; }
}

/* ============================================================
   SAFE AREA
   ============================================================ */
.bottom-nav-app {
    padding-bottom: max(env(safe-area-inset-bottom, 0px), 8px);
}
@media (min-width: 1024px) {
    .shell-content { padding-bottom: 36px; }
}

/* ============================================================
   FORMS — prevent iOS zoom
   ============================================================ */
@media (max-width: 768px) {
    input[type="text"], input[type="email"], input[type="password"],
    input[type="number"], input[type="date"], input[type="time"],
    input[type="search"], input[type="tel"], input[type="url"],
    textarea, select, .form-control { font-size: 16px !important; }
}

/* ============================================================
   TABLES — Horizontal scroll (compact, professional)
   ============================================================ */
.table-responsive {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: 12px;
}

/* Scroll wrapper with right shadow hint */
.table-scroll-wrap {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
    position: relative;
}
.table-scroll-wrap::after {
    content: '';
    position: absolute;
    top: 0; right: 0; bottom: 0;
    width: 28px;
    background: linear-gradient(to right, transparent, rgba(255,255,255,0.9));
    pointer-events: none;
    border-radius: 0 12px 12px 0;
}

/* Compact table on mobile */
@media (max-width: 768px) {
    .table th, .table td {
        font-size: 0.78rem !important;
        padding: 8px 10px !important;
        white-space: nowrap !important;
    }

    /* Card tables inside .card: compact */
    .card table th,
    .card table td {
        font-size: 0.76rem !important;
        padding: 8px 10px !important;
        white-space: nowrap !important;
    }

    /* Make sure table wrapper scrolls */
    .card > div[style*="overflow-x"],
    .card > .table-responsive {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch !important;
        max-width: 100% !important;
    }

    /* Scroll any table container that doesn't have overflow set */
    .card table {
        min-width: 520px; /* Force scroll on narrow screens */
    }
}

/* ============================================================
   STAT GRID — 2 col on mobile, 4 col on tablet+
   ============================================================ */
.stat-grid {
    display: grid !important;
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 10px;
    margin-bottom: 16px;
}
@media (min-width: 640px) {
    .stat-grid { grid-template-columns: repeat(4, 1fr) !important; }
}
@media (max-width: 360px) {
    .stat-grid { grid-template-columns: 1fr !important; }
}

/* ============================================================
   QUICK ACTIONS — Always grid, never single column
   ============================================================ */
.quick-actions {
    display: grid !important;
    grid-template-columns: repeat(4, 1fr) !important;
    gap: 8px;
}
@media (min-width: 640px) {
    .quick-actions { grid-template-columns: repeat(6, 1fr) !important; }
}

/* Quick action items: compact on mobile */
@media (max-width: 480px) {
    .quick-action-item .qa-icon {
        width: 36px !important; height: 36px !important;
        font-size: 1rem !important;
    }
    .quick-action-item .qa-label {
        font-size: 0.58rem !important;
        line-height: 1.2 !important;
    }
}

/* ============================================================
   PAGE HEADER — Stack title + button on mobile
   ============================================================ */
@media (max-width: 600px) {
    div.page-header {
        flex-wrap: wrap !important;
        gap: 10px !important;
    }
    div.page-header h1 { font-size: 1.1rem !important; }
    div.page-header p { font-size: 0.8rem !important; }
}

/* ============================================================
   FILTER FORMS — Stack inputs on mobile
   ============================================================ */
@media (max-width: 600px) {
    /* Only target actual filter form rows, not all flex divs */
    form[method="GET"] > div[style*="display:flex"],
    form[method="GET"] > div[style*="display: flex"] {
        flex-wrap: wrap !important;
        gap: 8px !important;
    }
    form[method="GET"] input[type="text"],
    form[method="GET"] select {
        flex: 1 1 100% !important;
        min-width: 0 !important;
    }
}

/* ============================================================
   CARDS — Mobile padding
   ============================================================ */
@media (max-width: 480px) {
    .card { border-radius: 12px !important; }
    .card-body { padding: 12px !important; }
    .card-header { padding: 12px 14px !important; }
}

/* ============================================================
   STAT CARDS — Compact on mobile
   ============================================================ */
@media (max-width: 480px) {
    .stat-card { padding: 10px 12px !important; gap: 8px !important; }
    .stat-icon { width: 36px !important; height: 36px !important; font-size: 1rem !important; }
    .stat-value { font-size: 1.2rem !important; }
    .stat-label { font-size: 0.72rem !important; }
}

/* ============================================================
   MODAL — Bottom sheet on mobile
   ============================================================ */
@media (max-width: 600px) {
    .modal-overlay {
        padding: 0 !important;
        align-items: flex-end !important;
    }
    .modal-box {
        max-width: 100% !important;
        border-radius: 20px 20px 0 0 !important;
        max-height: 92vh !important;
    }
}

/* ============================================================
   BOOKING / ENGAGEMENT
   ============================================================ */
@media (max-width: 640px) {
    #bookingSteps .step-circle {
        width: 26px !important; height: 26px !important;
        font-size: 0.72rem !important;
    }
    #bookingSteps .step-line { width: 20px !important; }
    .booking-pkg-grid { grid-template-columns: 1fr !important; }
}

/* ============================================================
   COACH DIRECTORY
   ============================================================ */
@media (max-width: 600px) {
    .coach-card-grid { grid-template-columns: 1fr !important; }
    .coach-card-cta { flex-direction: column !important; }
    .coach-card-cta a { text-align: center !important; }
    .coach-dir-filters {
        flex-direction: column !important;
        gap: 8px !important;
    }
    .coach-dir-filters input,
    .coach-dir-filters select { width: 100% !important; }
}
@media (min-width: 601px) and (max-width: 1023px) {
    .coach-card-grid { grid-template-columns: repeat(2, 1fr) !important; }
}

/* ============================================================
   PROFILE
   ============================================================ */
@media (max-width: 640px) {
    .coach-profile-hero {
        flex-direction: column !important;
        text-align: center !important;
        align-items: center !important;
    }
    .coach-profile-hero .avatar-wrap { margin: 0 auto 12px !important; }
    .profile-stat-row { grid-template-columns: repeat(2, 1fr) !important; }
    .review-grid { grid-template-columns: 1fr !important; }
}

/* ============================================================
   SETTINGS TABS — Horizontal scroll
   ============================================================ */
@media (max-width: 640px) {
    .settings-tabs,
    div[style*="border-bottom:2px solid"][style*="display:flex"],
    div[style*="border-bottom: 2px solid"][style*="display:flex"] {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch !important;
        flex-wrap: nowrap !important;
        padding-bottom: 1px !important;
    }
    .settings-tabs a,
    .settings-tabs button { flex-shrink: 0 !important; }
}

/* ============================================================
   SESSION CARDS
   ============================================================ */
@media (max-width: 600px) {
    .session-card { padding: 10px 12px !important; gap: 8px !important; }
    .session-card-time { min-width: 38px !important; }
    .dashboard-grid { grid-template-columns: 1fr !important; }
}

/* ============================================================
   TOUCH TARGETS
   ============================================================ */
@media (pointer: coarse) {
    .btn, .sidebar-nav-item, .bottom-nav-app .nav-item,
    .shell-icon-btn, .shell-avatar,
    .dropdown-item { min-height: 44px; }
    .sidebar-nav-item { padding: 11px 13px !important; }
    input[type="checkbox"],
    input[type="radio"] { width: 20px !important; height: 20px !important; }
}

/* ============================================================
   HOVER — Disable on touch
   ============================================================ */
@media (hover: none) {
    .app-card:hover { box-shadow: 0 2px 8px rgba(0,0,0,0.05) !important; transform: none !important; }
    .stat-card:hover { transform: none !important; }
    .session-card:hover { transform: none !important; }
    .sidebar-nav-item:hover { transform: none !important; }
    .quick-action-item:hover { transform: none !important; }
    .plan-card:hover { transform: none !important; }
}

/* ============================================================
   ACTIVE FEEDBACK
   ============================================================ */
@media (pointer: coarse) {
    .btn:active { transform: scale(0.97) !important; }
    .quick-action-item:active { transform: scale(0.95) !important; }
    .bottom-nav-app .nav-item:active .nav-icon-wrap { transform: scale(0.9) !important; }
}

/* ============================================================
   TYPOGRAPHY — Small screens
   ============================================================ */
@media (max-width: 480px) {
    .page-header h1 { font-size: 1.1rem !important; }
    .page-header p { font-size: 0.8rem !important; }
    .card { border-radius: 12px !important; }
}

/* ============================================================
   INLINE GRIDS — Platform-wide mobile safety net
   Nhiều trang dùng grid 2 cột viết thẳng trong style="" mà không có
   media query → kẹt 2 cột trên điện thoại. Quy tắc dưới gộp chúng về
   1 cột trên màn hình nhỏ, áp dụng toàn nền tảng (không cần sửa từng file).
   Thêm class "keep-cols" vào phần tử nào muốn giữ nguyên số cột.
   ============================================================ */
@media (max-width: 560px) {
    [style*="grid-template-columns:1fr 1fr"]:not(.keep-cols):not(.stat-grid):not(.quick-actions),
    [style*="grid-template-columns: 1fr 1fr"]:not(.keep-cols):not(.stat-grid):not(.quick-actions) {
        grid-template-columns: 1fr !important;
    }
}
/* Lưới nhiều cột cố định (3–4 cột) → tự co theo bề ngang trên màn hình nhỏ,
   phần tử lẻ tự giãn đầy (không để trống bên phải). */
@media (max-width: 560px) {
    [style*="grid-template-columns:repeat(3,1fr)"]:not(.keep-cols):not(.stat-grid):not(.quick-actions),
    [style*="grid-template-columns:repeat(4,1fr)"]:not(.keep-cols):not(.stat-grid):not(.quick-actions),
    [style*="grid-template-columns: repeat(3, 1fr)"]:not(.keep-cols):not(.stat-grid):not(.quick-actions),
    [style*="grid-template-columns: repeat(4, 1fr)"]:not(.keep-cols):not(.stat-grid):not(.quick-actions) {
        grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)) !important;
    }
}

/* ============================================================
   OVERFLOW PREVENTION
   ============================================================ */
body, .app-shell { overflow-x: hidden; max-width: 100vw; }

/* ============================================================
   PRINT
   ============================================================ */
@media print {
    .shell-header, .admin-sidebar, .coach-sidebar,
    .coachee-sidebar, .bottom-nav-app, .sidebar-overlay,
    .btn, nav { display: none !important; }
    .shell-content { margin: 0 !important; padding: 0 !important; }
    body { background: white !important; }
}

/* ============================================================
   ADMIN/USERS — Dual Layout (Desktop table / Mobile cards)
   ============================================================ */

/* Desktop: show table, hide mobile list */
.desktop-user-table { display: block; }
.mobile-user-list   { display: none; }

/* Mobile: hide table, show compact cards */
@media (max-width: 640px) {
    .desktop-user-table { display: none !important; }
    .mobile-user-list   { display: block !important; }
}

/* Mobile User Card Row */
.muc-row {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    border-bottom: 1px solid #F1F5F9;
    transition: background 0.15s;
}
.muc-row:hover { background: #F8FAFF; }
.muc-row:last-child { border-bottom: none; }

/* Avatar circle */
.muc-avatar {
    width: 40px; height: 40px;
    border-radius: 50%;
    color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-weight: 700; font-size: 0.85rem;
    flex-shrink: 0;
}

/* Info block */
.muc-info { flex: 1; min-width: 0; }

.muc-name {
    font-weight: 700;
    font-size: 0.85rem;
    color: #1E293B;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.muc-me {
    font-size: 0.62rem;
    background: #EEF2FF; color: #4F46E5;
    padding: 1px 5px; border-radius: 8px;
    font-weight: 600; vertical-align: middle;
}
.muc-email {
    font-size: 0.72rem;
    color: #94A3B8;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    margin-top: 1px;
}
.muc-badges {
    display: flex; gap: 4px; margin-top: 4px; flex-wrap: wrap;
}
.muc-role, .muc-status {
    font-size: 0.62rem; font-weight: 700;
    padding: 2px 7px; border-radius: 20px;
    white-space: nowrap;
}

/* Action buttons */
.muc-actions {
    display: flex; flex-direction: column; gap: 4px;
    flex-shrink: 0;
}
.muc-btn {
    display: flex; align-items: center; justify-content: center;
    width: 32px; height: 32px;
    border-radius: 8px;
    background: #F1F5F9;
    border: none; cursor: pointer;
    font-size: 0.9rem;
    text-decoration: none;
    transition: background 0.15s;
    color: inherit;
}
.muc-btn:hover { background: #E0E7FF; }
.muc-btn:active { transform: scale(0.92); }
