/**
 * Mobile-only dashboard fixes — loaded after style.css.
 * Desktop (≥769px) is untouched.
 */

@media (max-width: 768px) {
  html,
  body {
    overflow-x: hidden;
    max-width: 100vw;
  }

  #app-container {
    height: 100dvh;
    min-height: 100dvh;
    max-height: 100dvh;
  }

  #app-container > .flex-1 {
    min-width: 0;
    width: 100%;
  }

  #main-view {
    padding: 12px max(12px, env(safe-area-inset-left)) 12px max(12px, env(safe-area-inset-right)) !important;
    -webkit-overflow-scrolling: touch;
    overflow-x: hidden;
  }

  #app-container header {
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
    min-height: 56px;
    height: auto;
  }

  #mobile-menu-btn {
    min-width: 44px;
    min-height: 44px;
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
  }

  #sidebar {
    width: min(88vw, 18rem);
    padding-bottom: env(safe-area-inset-bottom, 0);
  }

  #sidebar nav a,
  #sidebar button {
    min-height: 44px;
  }

  #page-title {
    font-size: 1rem !important;
    max-width: calc(100vw - 140px);
  }

  /* Tables: horizontal scroll only when needed */
  #main-view table {
    display: block;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  #main-view .overflow-x-auto,
  #main-view .table-responsive {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
  }

  #main-view .grid {
    min-width: 0;
  }

  /* HQ Dashboard */
  .hq-dashboard {
    padding-bottom: env(safe-area-inset-bottom, 12px);
    overflow-x: hidden;
  }

  .hq-hero {
    padding: 1.1rem 0.9rem;
    border-radius: 1.1rem;
  }

  .hq-hero .relative.z-10.flex {
    flex-direction: column;
    align-items: stretch;
    gap: 0.85rem;
  }

  .hq-hero h2 {
    font-size: 1.5rem !important;
    line-height: 1.3;
  }

  .hq-hero-logo {
    width: 48px;
    height: 48px;
  }

  .hq-hero .flex.flex-wrap.gap-3 {
    width: 100%;
    flex-direction: column;
    gap: 0.5rem !important;
  }

  .hq-stat-glass {
    flex: 1 1 100%;
    width: 100%;
    min-width: 0;
    min-height: 44px;
  }

  .hq-kpi-grid,
  .hq-dashboard .grid.grid-cols-2,
  .hq-dashboard .grid.md\:grid-cols-2,
  .hq-dashboard .grid.lg\:grid-cols-4 {
    grid-template-columns: 1fr !important;
    gap: 0.75rem !important;
  }

  .hq-kpi-card {
    padding: 0.9rem;
    min-height: 44px;
  }

  .hq-kpi-card h3 {
    font-size: 1.25rem !important;
  }

  .hq-kpi-icon {
    width: 2.5rem;
    height: 2.5rem;
    font-size: 0.95rem;
  }

  .hq-panel-luxe-header {
    flex-direction: column;
    align-items: flex-start !important;
    gap: 0.65rem;
  }

  .hq-modules-wrap {
    padding: 1rem;
  }

  .hq-module-card {
    padding: 0.9rem;
    min-height: 44px;
  }

  .hq-modules-grid,
  .hq-dashboard .hq-modules-wrap .grid {
    grid-template-columns: 1fr !important;
    gap: 0.75rem !important;
  }

  .hq-chart-wrap,
  .hq-dashboard canvas {
    max-width: 100% !important;
    height: auto !important;
  }

  /* Generic dashboard cards */
  #main-view .rounded-2xl,
  #main-view .rounded-xl {
    border-radius: 1rem;
  }

  #main-view .p-6,
  #main-view .p-8 {
    padding: 1rem !important;
  }

  #main-view .grid-cols-2,
  #main-view .grid-cols-3,
  #main-view .grid-cols-4 {
    grid-template-columns: 1fr !important;
  }

  #main-view .sm\:grid-cols-2 {
    grid-template-columns: 1fr !important;
  }

  #main-view button,
  #main-view .btn,
  #main-view a.inline-flex {
    min-height: 44px;
  }

  /* Credit balance widget — ensure button stays clickable on mobile */
  #credit-balance-widget {
    position: relative;
    z-index: 30;
  }

  #credit-topup-btn,
  a.credit-topup-btn,
  .credit-topup-btn {
    width: 100%;
    min-height: 48px;
    font-size: 0.95rem;
  }

  .help-guide-button {
    top: 60px;
    left: 12px;
    min-width: 86px;
    min-height: 44px;
    padding: 8px 10px;
    font-size: 10px;
  }

  .help-guide-panel {
    top: 112px;
    left: 12px;
    width: min(300px, calc(100vw - 24px));
    max-height: calc(100dvh - 140px);
  }
}

@media (max-width: 480px) {
  #main-view {
    padding: 10px !important;
  }

  .hq-hero h2 {
    font-size: 1.35rem !important;
  }

  .hq-kpi-card h3 {
    font-size: 1.15rem !important;
  }
}

@media (max-width: 414px) {
  #sidebar {
    width: min(92vw, 17rem);
  }
}

@media (max-width: 375px) {
  #page-title {
    font-size: 0.9rem !important;
  }
}

@media (max-width: 360px) {
  .hq-hero {
    padding: 0.9rem 0.75rem;
  }
}

@media (max-width: 320px) {
  #main-view {
    padding: 8px !important;
  }

  .hq-modules-wrap {
    padding: 0.75rem;
  }
}
