/* =====================================================
   STARLINK VMS — Mobile & Responsive Overrides
   Breakpoints:
     1024px  — Tablet (sidebar becomes drawer)
     768px   — Small tablet
     640px   — Large phone
     480px   — Small phone
     360px   — Very small phone

   RULES:
     • Zero page-level horizontal scrolling
     • Tables scroll internally only
     • Charts/components resize to fit viewport
     • Consistent left/right padding on all containers
   ===================================================== */

/* =====================================================
   0. GLOBAL OVERFLOW CONTROL
   ===================================================== */
html, body {
  overflow-x: hidden;
  width: 100%;
}

.app-shell {
  overflow-x: hidden;
  width: 100%;
}

.main-area {
  overflow-x: hidden;
  max-width: 100vw;
}

.page-content {
  overflow-x: hidden;
}

/* =====================================================
   1. LAYOUT & SHELL
   ===================================================== */

/* Sidebar becomes a full-height drawer on mobile */
@media (max-width: 1024px) {
  .sidebar {
    transform: translateX(-100%);
    width: 260px !important;
    z-index: 300;
    box-shadow: 4px 0 24px rgba(0,0,0,0.25);
    /* Push sidebar below the fixed app bar so the logo isn't hidden */
    top: var(--app-bar-height);
    height: calc(100vh - var(--app-bar-height));
  }
  .sidebar--mobile-open {
    transform: translateX(0);
  }
  /* HIDE collapse button on mobile — only hamburger controls the drawer */
  .sidebar__collapse {
    display: none !important;
  }
  /* HIDE the standard desktop header on mobile — replaced by mobile logo banner */
  .sidebar__header {
    display: none !important;
  }
  /* SHOW the mobile-only logo banner */
  .sidebar__mobile-logo {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 20px 18px 18px;
    border-bottom: 1px solid rgba(255,255,255,0.1);
    flex-shrink: 0;
  }
  .sidebar__mobile-logo__text {
    display: flex;
    flex-direction: column;
    gap: 2px;
  }
  .sidebar__mobile-logo__name {
    font-size: 18px;
    font-weight: 800;
    color: white;
    letter-spacing: 0.12em;
    line-height: 1;
  }
  .sidebar__mobile-logo__sub {
    font-size: 10px;
    font-weight: 500;
    color: rgba(255,255,255,0.45);
    letter-spacing: 0.04em;
    text-transform: uppercase;
  }
  .app-bar {
    position: fixed !important;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 900 !important;
  }
  .main-area {
    margin-left: 0 !important;
    padding-top: var(--app-bar-height) !important;
  }
  .app-bar__menu-btn {
    display: flex !important;
  }
  /* Footer starts at edge on mobile since sidebar is hidden */
  .design-footer {
    left: 0 !important;
    width: 100% !important;
  }
}


/* App bar tighter on small screen */
@media (max-width: 640px) {
  .app-bar {
    padding: 0 var(--space-3);
    height: 52px;
  }
  .main-area {
    padding-top: 52px !important;
  }
  /* Sidebar must also start at 52px to stay below the smaller app bar */
  .sidebar {
    top: 52px !important;
    height: calc(100vh - 52px) !important;
  }
  .app-bar__title {
    font-size: var(--font-size-base);
  }
  .app-bar__right .btn .btn__text { display: none; }
  .app-bar__right .btn::after { content: '+'; font-weight: 700; font-size: 18px; line-height: 1; }
  .app-bar__right .btn { width: 36px; height: 36px; min-width: 36px; padding: 0; display: flex; align-items: center; justify-content: center; }
  .user-info { display: none; }
  .page-content {
    padding: var(--space-4) var(--space-3);
    padding-bottom: 60px;
  }
  .page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-3);
    margin-bottom: var(--space-4);
  }
  .page-header__title {
    font-size: var(--font-size-xl);
  }
  .page-header__actions {
    width: 100%;
    flex-wrap: wrap;
    gap: var(--space-2);
  }
}

/* Very small screens — minimal chrome */
@media (max-width: 360px) {
  .app-bar {
    padding: 0 var(--space-2);
  }
  .app-bar__title {
    font-size: var(--font-size-sm);
    max-width: 120px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

/* Design footer — smaller text on tiny screens */
@media (max-width: 480px) {
  .design-footer {
    font-size: 9px;
    padding: 6px 0;
  }
}

/* =====================================================
   2. AUTH SCREENS
   ===================================================== */
@media (max-width: 768px) {
  .auth-split {
    grid-template-columns: 1fr;
  }
  .auth-brand {
    display: none;
  }
  .auth-form-panel {
    padding: var(--space-8) var(--space-5);
    min-height: 100vh;
    align-items: flex-start;
    padding-top: var(--space-12);
  }
  .auth-form-wrap {
    max-width: 100%;
  }
}

@media (max-width: 480px) {
  .auth-form-panel {
    padding: var(--space-6) var(--space-4);
    padding-top: var(--space-10);
  }
  .auth-heading {
    font-size: var(--font-size-xl);
  }
  .auth-subheading {
    font-size: var(--font-size-xs);
    margin-bottom: var(--space-6);
  }
  .auth-form-header {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-2);
    margin-bottom: var(--space-6);
  }
}

@media (max-width: 400px) {
  .form-label-row {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-1);
  }
}

/* =====================================================
   3. DASHBOARD
   ===================================================== */
@media (max-width: 1024px) {
  .dashboard-main {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 640px) {
  .kpi-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: var(--space-3);
  }
  .chart-section {
    padding: var(--space-4);
  }
  .chart-section__header {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-2);
  }
  .bar-chart__label { font-size: 8px; }
  .recent-checkins__header {
    padding: var(--space-4);
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-2);
  }
  .arrivals-panel__list {
    max-height: 280px;
  }
  .table-filters {
    padding: var(--space-3) var(--space-4);
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
  }
}

@media (max-width: 480px) {
  .kpi-grid {
    grid-template-columns: 1fr;
  }
  .kpi-card__value {
    font-size: var(--font-size-3xl);
  }
  /* Make dashboard tables scrollable internally */
  .recent-checkins {
    overflow-x: auto;
  }
  .recent-table {
    min-width: 540px;
  }
}

/* =====================================================
   4. VISITORS MODULE
   ===================================================== */

/* Make visitor table scroll horizontally INSIDE its container only */
@media (max-width: 768px) {
  /* .visitor-log-wrap stays as the card shell — DO NOT set overflow-x here */
  /* Scroll is scoped to #log-table-wrap via visitors.css */
  #log-table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  #log-table-wrap table {
    min-width: 760px;
  }
}

@media (max-width: 640px) {
  /* Visitor card stacks vertically */
  .visitor-card {
    flex-direction: column;
    text-align: center;
    padding: var(--space-4);
    gap: var(--space-3);
  }
  .visitor-card__meta {
    justify-content: center;
  }
  /* Visitor detail — single column */
  .visitor-detail-layout {
    grid-template-columns: 1fr;
  }
  /* Remove sticky positioning on right panel on mobile */
  .detail-actions-card {
    position: static;
  }
  /* Form page */
  .visitor-form-page {
    max-width: 100%;
  }
  .visitor-form-card {
    padding: var(--space-4);
    border-radius: var(--radius-md);
    margin-bottom: 130px;
  }
  /* Sticky form footer full-width and stacked */
  .visitor-form-footer {
    left: 0 !important;
    right: 0;
    padding: var(--space-3) var(--space-4);
    flex-direction: column-reverse;
    gap: var(--space-2);
    height: auto;
  }
  .visitor-form-footer .btn {
    width: 100%;
    justify-content: center;
  }
  /* Info grid single column */
  .info-grid {
    grid-template-columns: 1fr;
    gap: var(--space-2);
  }
  /* Page controls */
  .page-subheader {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-3);
  }
  .page-subheader__right {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
  }
  .page-subheader__right .btn {
    flex: 1;
    min-width: 120px;
    justify-content: center;
  }
}

@media (max-width: 480px) {
  /* Visitor type control scrolls internally */
  .visitor-type-control {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .visitor-type-control label {
    white-space: nowrap;
    padding: 8px 10px;
    font-size: var(--font-size-xs);
  }
}

/* =====================================================
   5. REPORTS MODULE — FIX: NO HORIZONTAL SCROLLING
   ===================================================== */

/* Report header stacks on mobile */
@media (max-width: 640px) {
  .rpt-header {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-3);
  }
  .rpt-header > div {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
  }
  .rpt-header > div .btn {
    flex: 1;
    min-width: 0;
    justify-content: center;
  }
}

/* Report toolbar — scroll internally, not the page */
@media (max-width: 640px) {
  .rpt-toolbar {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    flex-wrap: nowrap;
    gap: var(--space-2);
  }
  .rpt-period-btn {
    flex-shrink: 0;
    padding: 6px 12px;
    font-size: var(--font-size-xs);
  }
  .rpt-toolbar .filter-select {
    flex-shrink: 0;
  }
}

/* Report charts — single column stacking */
@media (max-width: 768px) {
  .rpt-charts {
    grid-template-columns: 1fr !important;
  }
}

/* Report cards — fit inside the viewport, charts resize */
@media (max-width: 640px) {
  .rpt-card {
    padding: var(--space-4);
    overflow: hidden;
  }
  .rpt-card__header {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-2);
    margin-bottom: var(--space-4);
  }
  /* Trend bar chart — resize to fit, not scroll */
  .trend-chart {
    gap: 4px;
    height: 150px;
    padding-bottom: 20px;
  }
  .trend-bar__label {
    font-size: 8px;
  }
  /* Donut chart — centered, fits screen */
  .donut-wrap {
    padding: 0;
  }
  .donut {
    width: 120px !important;
    height: 120px !important;
  }
  .donut__svg {
    width: 120px !important;
    height: 120px !important;
  }
}

/* Report KPIs */
@media (max-width: 640px) {
  .rpt-kpi-row {
    grid-template-columns: 1fr;
  }
  .rpt-kpi {
    padding: var(--space-4);
  }
}

@media (max-width: 640px) {
  /* Report table — scroll inside container only */
  .rpt-table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .rpt-table {
    min-width: 500px;
  }
  .rpt-table-header {
    padding: var(--space-4);
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-2);
  }
}

/* Compliance rows on mobile */
@media (max-width: 480px) {
  .compliance-row-rpt {
    padding: var(--space-3) var(--space-4);
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-2);
  }
  .compliance-row-rpt > div:last-child {
    text-align: left !important;
  }
}

/* =====================================================
   6. SETTINGS MODULE — NO HORIZONTAL SCROLLING ON CONTENT
   ===================================================== */
@media (max-width: 900px) {
  .settings-layout {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 640px) {
  /* Settings tabs — horizontal scroll ALLOWED on tabs only */
  .settings-tabs {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    flex-wrap: nowrap;
  }
  .settings-tab {
    white-space: nowrap;
    padding: 8px 14px;
    font-size: var(--font-size-xs);
    flex-shrink: 0;
  }
  /* Settings card — proper padding, no overflow */
  .settings-card {
    overflow: hidden;
  }
  .settings-card__header,
  .settings-card__body,
  .settings-card__footer {
    padding: var(--space-4);
  }
  /* Footer buttons — stack and go full width */
  .settings-card__footer {
    flex-direction: column-reverse;
    gap: var(--space-2);
  }
  .settings-card__footer .btn {
    width: 100%;
    justify-content: center;
  }
  /* Setting rows — stack label and control */
  .setting-row {
    flex-direction: column;
    gap: var(--space-2);
  }
  .setting-row__control {
    align-self: flex-start;
    width: 100%;
  }
  /* Make all inputs inside settings full width */
  .setting-row__control .form-input {
    width: 100% !important;
    max-width: 100% !important;
  }
  .setting-row__control select.form-input {
    width: 100% !important;
  }
  /* Users table — scroll inside container only */
  .settings-card .users-table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
  }
  /* Settings header with button stacks */
  .settings-card__header {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-3);
  }
  .settings-card__header .btn {
    width: 100%;
    justify-content: center;
  }
  /* Color swatches — wrap properly */
  .color-swatches {
    gap: 8px;
    flex-wrap: wrap;
  }
  .color-swatch {
    width: 32px;
    height: 32px;
  }
  /* Logo uploader — smaller padding */
  .logo-uploader {
    padding: var(--space-4);
  }
  /* Doc items — stack actions */
  .doc-item {
    flex-wrap: wrap;
    gap: var(--space-2);
  }
  .doc-item__actions {
    width: 100%;
    justify-content: flex-start;
  }
  /* Invite form grid — single column */
  .settings-invite-grid {
    grid-template-columns: 1fr !important;
  }
}

@media (max-width: 480px) {
  .settings-header {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-3);
  }
  .settings-header h1 {
    font-size: var(--font-size-xl);
  }
}

/* =====================================================
   7. PROFILE MODULE
   ===================================================== */
@media (max-width: 900px) {
  .profile-layout {
    grid-template-columns: 1fr;
  }
  .profile-id-card {
    position: static;
  }
}

@media (max-width: 640px) {
  .profile-card__header {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-2);
    padding: var(--space-4);
  }
  .profile-card__body {
    padding: var(--space-4);
  }
  .profile-card__footer {
    flex-direction: column;
    padding: var(--space-4);
  }
  .profile-card__footer .btn {
    width: 100%;
    justify-content: center;
  }
  .notif-pref-row {
    flex-direction: column;
    gap: var(--space-2);
    align-items: flex-start;
  }
  /* Profile form grid — MUST stack to single column */
  .profile-form-grid {
    grid-template-columns: 1fr !important;
  }
}

@media (max-width: 480px) {
  .profile-stat-row {
    flex-direction: row;
    justify-content: space-around;
    padding-top: var(--space-3);
  }
  .profile-stat__val {
    font-size: var(--font-size-lg);
  }
  .profile-avatar {
    width: 80px;
    height: 80px;
    font-size: 28px;
  }
}

/* =====================================================
   8. NOTIFICATIONS & DROPDOWNS
   ===================================================== */
@media (max-width: 480px) {
  /* Notification panel — full width on very small screens */
  .notif-panel {
    position: fixed;
    top: 52px;
    left: 0;
    right: 0;
    width: 100%;
    border-radius: 0;
    max-height: calc(100vh - 52px);
    overflow-y: auto;
  }
  /* User dropdown full width */
  .user-dropdown {
    position: fixed;
    top: 52px;
    right: 0;
    left: 0;
    width: 100% !important;
    border-radius: 0;
  }
}

/* =====================================================
   9. COMPONENTS (Badges, Tables, Forms)
   ===================================================== */
@media (max-width: 640px) {
  /* Stack form rows */
  .form-group--row {
    flex-direction: column;
    align-items: flex-start;
  }
  /* Badge truncation */
  .badge {
    max-width: 120px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  /* Buttons full width in footers */
  .modal-footer .btn,
  .card-footer .btn {
    flex: 1;
    justify-content: center;
    min-width: 0;
  }
  /* Toast width */
  #toast-container {
    left: var(--space-3);
    right: var(--space-3);
    bottom: 60px; /* above design footer */
  }
  .toast {
    width: 100%;
  }
  /* Filter bar — stack vertically on mobile */
  .filter-bar {
    flex-direction: column;
    align-items: stretch;
  }
  .filter-search {
    min-width: 0;
    width: 100%;
  }
  .filter-select {
    width: 100%;
  }
}

/* =====================================================
   10. HELPERS — Touch targets & accessibility
   ===================================================== */
@media (max-width: 768px) {
  /* Ensure minimum 44px touch targets */
  .btn {
    min-height: 44px;
  }
  .btn--sm {
    min-height: 36px;
  }
  .nav-item {
    min-height: 44px;
    padding: var(--space-3);
  }
  /* Remove hover effects on touch — prevents sticky hover states */
  .nav-item:hover {
    background: transparent;
  }
  .nav-item.active {
    background: var(--color-navy-mid);
  }
  /* Inputs larger for touch */
  .form-input {
    height: 44px;
    font-size: 16px; /* Prevents iOS zoom on focus */
  }
  select.form-input {
    font-size: 16px;
  }
}

/* =====================================================
   11. UNIVERSAL TABLE CONTAINMENT
   All tables must scroll inside their container only,
   never causing page-level horizontal scroll.
   ===================================================== */
@media (max-width: 768px) {
  .data-table-wrap,
  .rpt-table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
  }
  /* visitor log table — scoped via #log-table-wrap, NOT .visitor-log-wrap */
  #log-table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
  }
}

/* =====================================================
   12. FALLBACK INLINE STYLE OVERRIDES
   These target elements with inline styles that prevent 
   media query overrides. Uses attribute selectors + !important
   to ensure mobile-first layout.
   ===================================================== */
@media (max-width: 1024px) {
  /* Profile form grid — override inline grid-template-columns */
  .profile-card__body div[style*="grid-template-columns"],
  .profile-card__body .profile-form-grid {
    grid-template-columns: 1fr !important;
  }
  /* Settings invite form grid — override inline grid */
  .settings-card__body div[style*="grid-template-columns"],
  .settings-card__body .settings-invite-grid {
    grid-template-columns: 1fr !important;
  }

  /* Settings card footer — stack buttons vertically */
  .settings-card__footer {
    flex-direction: column-reverse !important;
    gap: var(--space-2) !important;
    padding: var(--space-3) var(--space-4) !important;
  }
  .settings-card__footer .btn {
    width: 100% !important;
    justify-content: center !important;
  }

  /* Settings card body — tighter padding on mobile */
  .settings-card__body {
    padding: var(--space-4) !important;
  }

  /* Settings card header — tighter padding on mobile */
  .settings-card__header {
    padding: var(--space-4) !important;
  }

  /* Report header & card — contain everything inside viewport */
  .rpt-header {
    flex-direction: column !important;
    align-items: flex-start !important;
  }
  .rpt-header > div {
    width: 100% !important;
    display: flex !important;
    flex-wrap: wrap !important;
  }
  .rpt-header > div .btn {
    flex: 1 !important;
    min-width: 0 !important;
  }
  .rpt-card {
    padding: var(--space-4) !important;
  }

  /* All form inputs must respect container bounds */
  .form-input {
    max-width: 100% !important;
    box-sizing: border-box !important;
  }
}
