/* ============================================================
   responsive.css — Tối ưu hóa giao diện cho mọi thiết bị
   Breakpoints:
     ≥1200px  : Desktop lớn
     900-1199px: Desktop nhỏ / Laptop
     768-899px : Tablet ngang
     600-767px : Tablet dọc / Phone lớn
     480-599px : Phone trung bình
     320-479px : Phone nhỏ
   ============================================================ */

/* ============================================================
   BASE: Đảm bảo html/body luôn cuộn được
   ============================================================ */
html {
  overflow-x: hidden;
}
body {
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
}

/* ============================================================
   APP CONTAINER: Bỏ fixed height — luôn cuộn tự nhiên
   ============================================================ */
.app-container {
  height: auto !important;
  min-height: calc(100vh - 90px);
  overflow: visible !important;
  display: flex;
  flex-direction: column;
}

/* ============================================================
   APP WORKSPACE: Chiều cao tự nhiên trên mọi screen
   ============================================================ */
.app-workspace {
  flex: 1;
  overflow: visible !important;
}

/* Left column panel: luôn cuộn được nội dung */
.app-left-column {
  overflow: visible;
}
.app-left-column .panel-body {
  overflow-y: auto;
  overflow-x: hidden;
}
.result-region {
  overflow: visible !important;
}
.result-region .panel-body {
  overflow: visible;
}


/* ============================================================
   DESKTOP LỚN (≥1400px): Tăng max-width workspace
   ============================================================ */
@media (min-width: 1400px) {
  .app-workspace {
    grid-template-columns: 400px 1fr;
  }
}

/* ============================================================
   DESKTOP NHỎ / LAPTOP (900px–1199px): Cột trái hẹp hơn
   ============================================================ */
@media (max-width: 1199px) and (min-width: 900px) {
  .app-workspace {
    grid-template-columns: 300px 1fr;
    padding: 12px;
    gap: 12px;
  }

  /* Leaderboard: zone-l nhỏ hơn */
  .lb5-card .lb5-zone-l { flex: 0 0 200px !important; }
  .lb5-trow .lb5-zone-l { flex: 0 0 185px !important; }
  .lb2-cb-id   { flex: 0 0 176px !important; }
  .lb2-cb-id-r { flex: 0 0 161px !important; }
}

/* ============================================================
   TABLET NGANG (768px–899px)
   ============================================================ */
@media (max-width: 899px) and (min-width: 768px) {
  /* Workspace: chuyển sang 1 cột */
  .app-workspace {
    display: flex !important;
    flex-direction: column !important;
    padding: 10px !important;
    gap: 10px !important;
    overflow: visible !important;
  }
  .app-right-column { display: contents; }
  .search-region   { order: 1; }
  .app-left-column { order: 2; }
  .result-region   { order: 3; }

  /* Panels: auto height */
  .app-panel {
    min-height: auto !important;
    height: auto !important;
    overflow: visible !important;
  }
  .app-left-column .panel-body,
  .result-region .panel-body { overflow: visible; }

  /* Leaderboard: zone-l gọn */
  .lb5-card .lb5-zone-l { flex: 0 0 190px !important; }
  .lb5-trow .lb5-zone-l { flex: 0 0 175px !important; }
  .lb2-cb-id   { flex: 0 0 166px !important; }
  .lb2-cb-id-r { flex: 0 0 151px !important; }

  /* Ticker wrap: cuộn thay vì fixed height */
  .lb2-ticker-wrap {
    height: 400px !important;
    overflow-y: auto !important;
  }
}

/* ============================================================
   TABLET DỌC / PHONE LỚN (600px–767px)
   ============================================================ */
@media (max-width: 767px) and (min-width: 600px) {
  /* Header */
  body { padding-top: 78px; }
  .dash-header-inner { padding: 0 10px; height: 54px; }
  .dash-ticker { top: 54px; height: 24px; }
  .app-container { margin-top: 78px !important; }

  /* Nav buttons */
  .dash-nav { gap: 4px; }
  .dash-nav-btn { padding: 5px 10px; font-size: .78rem; gap: 4px; }
  .nav-txt { display: inline; }

  /* Workspace */
  .app-workspace {
    display: flex !important;
    flex-direction: column !important;
    padding: 8px !important;
    gap: 8px !important;
  }
  .app-right-column { display: contents; }
  .search-region   { order: 1; }
  .app-left-column { order: 2; }
  .result-region   { order: 3; }

  /* Leaderboard: 1 cột dọc */
  .lb2-wrapper {
    display: flex !important;
    flex-direction: column !important;
    gap: 12px !important;
    height: auto !important;
  }
  .lb2-lop-section,
  .lb2-hs-section {
    width: 100% !important;
    height: auto !important;
    overflow: visible !important;
  }
  .lb2-lop-list {
    overflow-y: visible !important;
    height: auto !important;
    max-height: none !important;
  }
  .lb2-ticker-wrap {
    height: 360px !important;
    overflow-y: auto !important;
  }

  /* Cards: 2 dòng */
  .lb5-card, .lb5-trow {
    display: flex !important;
    flex-direction: row !important;
    flex-wrap: wrap !important;
    align-items: center !important;
    height: auto !important;
    min-height: 0 !important;
    padding: 8px 10px 7px !important;
    gap: 0 !important;
    overflow: visible !important;
  }
  .lb5-zone-l,
  .lb5-card .lb5-zone-l,
  .lb5-trow .lb5-zone-l {
    order: 1 !important;
    flex: 1 1 0 !important;
    min-width: 0 !important;
    max-width: calc(100% - 56px) !important;
    overflow: hidden !important;
    padding-left: 8px !important;
  }
  .lb5-zone-l .lb5-hsn   { max-width: none !important; }
  .lb5-zone-l .lb5-tname { max-width: none !important; }
  .lb5-zone-r {
    order: 2 !important;
    flex: 0 0 auto !important;
    min-width: 48px !important;
    padding-right: 4px !important;
  }
  .lb5-zone-m {
    order: 3 !important;
    flex: 0 0 100% !important;
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 4px !important;
    padding: 5px 8px 2px !important;
    overflow: visible !important;
  }
  .lb5-zone-m .lb5-pill {
    font-size: .62rem !important;
    padding: 2px 5px !important;
  }
  .lb2-cb-pills { display: none !important; }
  .lb2-cb-id, .lb2-cb-id-r { flex: 1 1 auto !important; }
  .lb2-cb-dtb { flex: 0 0 56px !important; }

  /* Score grid */
  .score-grid { grid-template-columns: 1fr 1fr; gap: 8px; }

  /* Stats */
  .vibrant-stats-grid { grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .stat-card { padding: 10px 8px; }

  /* Search form */
  .search-input-group { flex-wrap: wrap; }
  .vibrant-input {
    border-radius: 12px 12px 0 0;
    border-right: 1px solid #D1D5DB;
    border-bottom: none;
  }
  .btn-submit-search { width: 100%; border-radius: 0 0 12px 12px; padding: 11px; }
}

/* ============================================================
   PHONE TRUNG BÌNH (480px–599px)
   ============================================================ */
@media (max-width: 599px) and (min-width: 480px) {
  body { padding-top: 80px; }
  .dash-header-inner { padding: 0 8px; height: 56px; }
  .dash-ticker { top: 56px; height: 22px; }
  .app-container { margin-top: 80px !important; }

  /* Nav: chỉ icon */
  .nav-txt { display: none; }
  .dash-nav { gap: 3px; }
  .dash-nav-btn { padding: 5px 8px; font-size: 1rem; min-width: 34px; justify-content: center; }
  .pwa-header-install-btn { padding: 5px 8px; min-width: 34px; }

  /* Workspace */
  .app-workspace {
    display: flex !important;
    flex-direction: column !important;
    padding: 8px !important;
    gap: 8px !important;
  }
  .app-right-column { display: contents; }
  .search-region   { order: 1; }
  .app-left-column { order: 2; }
  .result-region   { order: 3; }

  /* Search form stacked */
  .search-input-group { flex-wrap: wrap; }
  .vibrant-input {
    border-radius: 12px 12px 0 0;
    border-right: 1px solid #D1D5DB;
    border-bottom: none;
    font-size: 14px;
  }
  .btn-submit-search { width: 100%; border-radius: 0 0 12px 12px; padding: 12px; }
  .btn-clear-search { right: 12px; top: 14px; transform: none; }
  .search-input-group .input-icon { top: 14px; transform: none; }

  /* Stats */
  .vibrant-stats-grid { grid-template-columns: repeat(3, 1fr); gap: 6px; }
  .stat-card { padding: 8px 6px; border-radius: 10px; }
  .sc-icon { font-size: 14px; margin-bottom: 3px; }
  .sc-num { font-size: 1rem; }
  .sc-lbl { font-size: .55rem; }

  /* Score grid */
  .score-grid { grid-template-columns: 1fr; gap: 6px; }

  /* AI panel header */
  .ai-panel-header { flex-direction: column; align-items: flex-start; gap: 6px; }
  .ai-student-ctx { max-width: 100%; display: block; }

  /* Leaderboard: 1 cột + 2 dòng cards */
  .lb2-wrapper {
    display: flex !important;
    flex-direction: column !important;
    gap: 12px !important;
    height: auto !important;
  }
  .lb2-lop-section, .lb2-hs-section {
    width: 100% !important; height: auto !important; overflow: visible !important;
  }
  .lb2-lop-list { overflow: visible !important; height: auto !important; max-height: none !important; }
  .lb2-ticker-wrap { height: 340px !important; overflow-y: auto !important; }

  .lb5-card, .lb5-trow {
    flex-wrap: wrap !important;
    height: auto !important;
    min-height: 0 !important;
    padding: 8px 8px 6px !important;
  }
  .lb5-zone-l,
  .lb5-card .lb5-zone-l,
  .lb5-trow .lb5-zone-l {
    order: 1 !important;
    flex: 1 1 0 !important;
    min-width: 0 !important;
    max-width: calc(100% - 52px) !important;
    padding-left: 6px !important;
  }
  .lb5-zone-r { order: 2 !important; flex: 0 0 auto !important; min-width: 44px !important; }
  .lb5-zone-m {
    order: 3 !important;
    flex: 0 0 100% !important;
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 3px !important;
    padding: 5px 6px 2px !important;
  }
  .lb5-zone-m .lb5-pill { font-size: .60rem !important; padding: 2px 4px !important; }
  .lb2-cb-pills { display: none !important; }
  .lb2-cb-id, .lb2-cb-id-r { flex: 1 1 auto !important; }
  .lb2-cb-dtb { flex: 0 0 52px !important; }
}

/* ============================================================
   PHONE NHỎ (320px–479px)
   ============================================================ */
@media (max-width: 479px) {
  body { padding-top: 78px; }

  /* Header: 2 dòng nếu cần */
  .dash-header-inner {
    padding: 4px 8px;
    height: auto;
    min-height: 52px;
    flex-wrap: nowrap;
    gap: 4px;
    overflow: hidden;
  }
  /* Logo */
  .dash-logo { flex: 1; min-width: 0; overflow: hidden; }
  .dash-logo-icon { width: 30px; height: 30px; font-size: 15px; flex-shrink: 0; }
  .dash-logo-ten {
    font-size: .70rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 130px;
  }
  .dash-logo-mo-ta { display: none; }

  /* Ticker */
  .dash-ticker { top: 52px; height: 22px; }
  .dash-ticker-label { padding: 0 8px; font-size: .58rem; }
  .dash-ticker-content { font-size: .62rem; }
  .app-container { margin-top: 76px !important; }

  /* Nav: icon only, super compact */
  .nav-txt { display: none !important; }
  .dash-nav { gap: 2px; flex-shrink: 0; }
  .dash-nav-btn {
    padding: 5px 6px;
    font-size: 0.9rem;
    min-width: 30px;
    border-radius: 7px;
    justify-content: center;
  }
  .pwa-header-install-btn { padding: 5px 6px; min-width: 30px; }
  /* Ẩn badge admin trên phone nhỏ */
  .dash-nav-admin-badge { display: none; }

  /* Workspace */
  .app-workspace { padding: 6px !important; gap: 6px !important; }

  /* Panel headers */
  .panel-header { padding: 10px 12px; font-size: 12px; }
  .search-region .panel-header span:not(.header-icon) { display: none; }

  /* Search stacked */
  .search-input-group { flex-wrap: wrap; }
  .vibrant-input {
    border-radius: 10px 10px 0 0;
    border-right: 1px solid #D1D5DB;
    border-bottom: none;
    font-size: 13px;
    padding: 10px 14px 10px 36px;
  }
  .btn-submit-search {
    width: 100%;
    border-radius: 0 0 10px 10px;
    padding: 11px;
    font-size: 14px;
  }
  .btn-clear-search { right: 10px; top: 12px; transform: none; }
  .search-input-group .input-icon { top: 12px; transform: none; left: 12px; }

  /* Stats */
  .vibrant-stats-grid { grid-template-columns: repeat(3, 1fr) !important; gap: 4px; }
  .stat-card { padding: 6px 4px; border-radius: 8px; }
  .sc-icon { font-size: 12px; margin-bottom: 2px; }
  .sc-num { font-size: .9rem; margin-bottom: 1px; }
  .sc-lbl { font-size: .50rem; letter-spacing: 0; }

  /* Score grid: 1 col */
  .score-grid { grid-template-columns: 1fr !important; }

  /* Student card */
  .st-avatar-pixel { width: 52px; height: 52px; }
  .st-name { font-size: .88rem; }
  .st-sbd { font-size: .72rem; }
  .st-info-row { font-size: 12px; padding: 6px 0; }

  /* AI panel header compact */
  .ai-panel-header { padding: 8px 12px; flex-direction: column; align-items: flex-start; gap: 5px; }
  .ai-student-ctx { max-width: 100%; display: block; font-size: 11px; }
  .btn-ai-refresh { padding: 4px 8px; font-size: .72rem; }

  /* Leaderboard */
  .lb2-wrapper {
    display: flex !important;
    flex-direction: column !important;
    gap: 10px !important;
    height: auto !important;
  }
  .lb2-lop-section, .lb2-hs-section {
    width: 100% !important; height: auto !important; overflow: visible !important;
  }
  .lb2-lop-list { overflow: visible !important; height: auto !important; max-height: none !important; }
  .lb2-ticker-wrap {
    height: 300px !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;
  }

  /* Cards: 2-line wrap */
  .lb5-card, .lb5-trow {
    display: flex !important;
    flex-direction: row !important;
    flex-wrap: wrap !important;
    align-items: center !important;
    height: auto !important;
    min-height: 0 !important;
    padding: 7px 8px 5px !important;
    gap: 0 !important;
    overflow: visible !important;
  }
  .lb5-zone-l,
  .lb5-card .lb5-zone-l,
  .lb5-trow .lb5-zone-l {
    order: 1 !important;
    flex: 1 1 0 !important;
    min-width: 0 !important;
    max-width: calc(100% - 48px) !important;
    overflow: hidden !important;
    padding-left: 6px !important;
  }
  /* Ẩn điểm TB học sinh đứng đầu lớp (quá chật) */
  .lb5-card .lb5-zone-l .lb5-sttb { display: none !important; }
  .lb5-zone-l .lb5-hsn { max-width: none !important; font-size: .72rem !important; }
  .lb5-zone-l .lb5-tname { max-width: none !important; font-size: .72rem !important; }
  .lb5-zone-l .lb5-cname { font-size: .82rem !important; }

  .lb5-zone-r {
    order: 2 !important;
    flex: 0 0 auto !important;
    min-width: 40px !important;
  }
  .lb5-zone-r .lb5-avg { font-size: .9rem !important; }
  .lb5-zone-r .lb5-arr { display: none !important; }

  .lb5-zone-m {
    order: 3 !important;
    flex: 0 0 100% !important;
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 3px !important;
    padding: 4px 6px 2px !important;
    overflow: visible !important;
  }
  .lb5-zone-m .lb5-pill {
    font-size: .58rem !important;
    padding: 1px 4px !important;
    gap: 2px !important;
  }
  .lb5-zone-m .lb5-pill strong { font-size: .60rem !important; }

  /* Col-bar labels */
  .lb2-cb-pills { display: none !important; }
  .lb2-cb-id, .lb2-cb-id-r { flex: 1 1 auto !important; font-size: .55rem !important; }
  .lb2-cb-rank { font-size: .55rem !important; flex: 0 0 20px !important; }
  .lb2-cb-dtb { flex: 0 0 44px !important; font-size: .55rem !important; }

  /* Info bar compact */
  .lb2-info-bar {
    padding: 6px 10px !important;
    gap: 6px !important;
  }
  .lb2-hdr-icon-wrap { width: 18px !important; height: 18px !important; }
  .lb2-hdr-icon { font-size: .8rem !important; }
  .lb2-info-bar .lb2-hdr-title { font-size: .72rem !important; }
  .lb2-hdr-badge { font-size: .60rem !important; padding: 1px 6px !important; }

  /* Pipeline image */
  .pipeline-wrap img { border-radius: 8px; }

  /* Modal */
  .modal-box { padding: 20px 14px; max-width: 100%; border-radius: 14px; }
  #modal-dangnhap > div {
    width: calc(100vw - 16px) !important;
    padding: 20px 14px !important;
    border-radius: 14px !important;
  }

  /* Chatbot bubble: đẩy lên để tránh nav bar mobile */
  #chatbot-bubble { right: 12px; bottom: 16px; width: 50px; height: 50px; font-size: 22px; }
  #chatbot-panel {
    right: 0; left: 0; bottom: 70px;
    width: 100%; max-width: 100%;
    border-radius: 18px 18px 0 0;
    height: calc(100vh - 80px);
    max-height: none !important;
  }
}

/* ============================================================
   PHONE RẤT NHỎ (≤360px): Samsung Galaxy A series, v.v.
   ============================================================ */
@media (max-width: 360px) {
  .dash-logo-ten { max-width: 100px; font-size: .65rem; }
  .dash-nav-btn { padding: 4px 5px; min-width: 28px; font-size: .85rem; }
  .sc-num { font-size: .85rem; }

  /* Ẩn avatar lớp trong zone-l (quá chật) */
  .lb5-zone-l .lb5-bsep,
  .lb5-zone-l .lb5-cr { display: none !important; }
  .lb5-zone-l .lb5-hsn { display: none !important; }
}

/* ============================================================
   FIX: Popup overlay trên mobile
   ============================================================ */
@media (max-width: 767px) {
  .lb2-popup {
    width: min(96vw, 400px) !important;
    max-height: 85vh !important;
  }
  .lb2-popup-stats {
    grid-template-columns: repeat(3, 1fr) !important;
    gap: 6px !important;
  }
}

/* ============================================================
   FIX: Admin page responsive
   ============================================================ */
@media (max-width: 768px) {
  .quanly-grid { grid-template-columns: 1fr !important; }
  .bang-hocsinh th:nth-child(n+4),
  .bang-hocsinh td:nth-child(n+4) { display: none; }
}
@media (max-width: 480px) {
  .bang-hocsinh th:nth-child(n+3),
  .bang-hocsinh td:nth-child(n+3) { display: none; }
}

/* ============================================================
   FIX: hocsinh.php responsive
   ============================================================ */
@media (max-width: 768px) {
  .hocsinh-grid { grid-template-columns: 1fr !important; gap: 16px; }
  .card { padding: 16px; }
  .diem-header { font-size: .82rem; }
}

/* ============================================================
   TOUCH IMPROVEMENTS: Vùng chạm rộng hơn
   ============================================================ */
@media (max-width: 767px) {
  .btn, .dash-nav-btn, button, a.lb5-trow {
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
  }
  /* Tăng vùng chạm cho các button nhỏ */
  .cb-header-btn { width: 36px; height: 36px; }
  .lb2-popup-close { width: 32px; height: 32px; }
}

/* ============================================================
   FIX: Scrollbar trên mobile (webkit)
   ============================================================ */
.lb2-ticker-wrap::-webkit-scrollbar,
.lb2-lop-list::-webkit-scrollbar,
.panel-body::-webkit-scrollbar {
  width: 3px;
}
.lb2-ticker-wrap::-webkit-scrollbar-track,
.panel-body::-webkit-scrollbar-track { background: transparent; }
.lb2-ticker-wrap::-webkit-scrollbar-thumb,
.panel-body::-webkit-scrollbar-thumb {
  background: #CBD5E1;
  border-radius: 3px;
}

/* ============================================================
   FIX: Ngăn tràn ngang toàn cục
   ============================================================ */
.lb2-wrapper, .lb5-card, .lb5-trow,
.app-workspace, .dash-header-inner,
.panel-body, .app-container,
.lb2-lop-section, .lb2-hs-section {
  max-width: 100%;
}

/* ============================================================
   FIX: Safe area cho iPhone X+ (notch)
   ============================================================ */
@supports (padding: max(0px)) {
  body {
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }
  #chatbot-bubble {
    bottom: max(16px, env(safe-area-inset-bottom));
    right: max(12px, env(safe-area-inset-right));
  }
  #chatbot-panel {
    padding-bottom: env(safe-area-inset-bottom);
  }
}
