/* ============================================================
   FERALVOICE OS — MOBILE RESPONSIVE OVERRIDES
   Handles: overflow fixes, mobile sidebar drawer, responsive
   grids, tables, modals, and chat layouts.
   ============================================================ */

/* ---------- 1. MOBILE SIDEBAR DRAWER ---------- */
.mobile-sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  z-index: 49;
  opacity: 0;
  visibility: hidden;
  transition: all var(--t-base);
}
.mobile-menu-toggle {
  display: none;
}

@media (max-width: 1024px) {
  .mobile-menu-toggle {
    display: grid;
    place-items: center;
  }

  /* Hide sidebar by default on mobile, slide in when active */
  .app-shell {
    grid-template-columns: 1fr !important;
    grid-template-areas:
      "topbar"
      "content" !important;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 280px;
    max-width: 85vw;
    transform: translateX(-100%);
    transition: transform var(--t-slow);
    z-index: 50;
    box-shadow: var(--shadow-lg);
  }

  .app-shell.sidebar-open .sidebar {
    transform: translateX(0);
  }

  .app-shell.sidebar-open .mobile-sidebar-backdrop {
    display: block;
    opacity: 1;
    visibility: visible;
  }

  /* Hide the desktop collapse button on mobile */
  .sidebar-collapse { display: none; }

  /* Force full expanded sidebar styling on mobile regardless of desktop collapse or responsive layout.css rules */
  .app-shell .sidebar-item-label,
  .app-shell .sidebar-section-label,
  .app-shell .sidebar-item-badge,
  .app-shell .sidebar-footer-text,
  .app-shell.collapsed .sidebar-item-label,
  .app-shell.collapsed .sidebar-section-label,
  .app-shell.collapsed .sidebar-item-badge,
  .app-shell.collapsed .sidebar-footer-text {
    display: inline-block !important;
  }
  .app-shell .sidebar-item-label,
  .app-shell.collapsed .sidebar-item-label {
    display: block !important;
  }
  .app-shell .sidebar-item,
  .app-shell.collapsed .sidebar-item {
    justify-content: flex-start !important;
    padding: var(--space-2) var(--space-3) !important;
  }
  .app-shell .sidebar-header,
  .app-shell.collapsed .sidebar-header {
    padding: var(--space-4) var(--space-4) !important;
    justify-content: space-between !important;
  }
  .app-shell .sidebar-header .fv-logo span,
  .app-shell.collapsed .sidebar-header .fv-logo span {
    display: inline !important;
  }
  .app-shell .sidebar-footer,
  .app-shell.collapsed .sidebar-footer {
    padding: var(--space-3) !important;
  }
  .app-shell .sidebar-footer-user,
  .app-shell.collapsed .sidebar-footer-user {
    justify-content: flex-start !important;
  }
  .app-shell .sidebar-footer-user-info,
  .app-shell.collapsed .sidebar-footer-user-info {
    display: block !important;
  }
}

/* ---------- 2. TOPBAR ON MOBILE ---------- */
@media (max-width: 1024px) {
  .topbar {
    padding: 0 var(--space-3);
    gap: var(--space-2);
  }
  .topbar-search {
    display: none;
  }
  .breadcrumb {
    display: none;
  }
  .topbar-quick-add span {
    display: none;
  }
  .topbar-quick-add {
    width: 36px;
    height: 36px;
    padding: 0;
    justify-content: center;
  }
}

@media (max-width: 480px) {
  .topbar-divider { display: none; }
  .topbar-btn[data-tooltip="Help"] { display: none; }
}

/* ---------- 3. CONTENT AREA ---------- */
@media (max-width: 1024px) {
  .content-inner {
    padding: var(--space-4) var(--space-3);
  }
  .content-area::before { height: 140px; }
}

@media (max-width: 480px) {
  .content-inner {
    padding: var(--space-3) var(--space-2);
  }
}

/* ---------- 4. PAGE HEADER ---------- */
@media (max-width: 768px) {
  .page-header {
    flex-direction: column;
    align-items: stretch;
    gap: var(--space-3);
  }
  .page-actions {
    width: 100%;
    flex-wrap: wrap;
  }
  .page-actions .btn {
    flex: 1;
    min-width: 120px;
  }
  .page-actions .btn span {
    font-size: 12px;
  }
  .page-title {
    font-size: 20px;
  }
}

@media (max-width: 480px) {
  .page-actions .btn {
    width: 100%;
  }
}

/* ---------- 5. GRID LAYOUTS ---------- */
@media (max-width: 1280px) {
  .grid.responsive-4 { grid-template-columns: repeat(2, 1fr) !important; }
  .grid.responsive-3 { grid-template-columns: repeat(2, 1fr) !important; }
}

@media (max-width: 768px) {
  .grid.responsive-4 { grid-template-columns: 1fr !important; }
  .grid.responsive-3 { grid-template-columns: 1fr !important; }
  .grid.responsive-2 { grid-template-columns: 1fr !important; }
  .grid.responsive-2-1 { grid-template-columns: 1fr !important; }
  .grid.responsive-3-2 { grid-template-columns: 1fr !important; }
}

/* Default: make any inline grid responsive */
@media (max-width: 768px) {
  .grid[style*="grid-template-columns:2fr 1fr"],
  .grid[style*="grid-template-columns: 2fr 1fr"],
  .grid[style*="grid-template-columns:1fr 2fr"],
  .grid[style*="grid-template-columns: 1fr 2fr"],
  .grid[style*="grid-template-columns:1fr 1fr 1fr"],
  .grid[style*="grid-template-columns: 1fr 1fr 1fr"],
  .grid[style*="grid-template-columns:1fr 1fr"],
  .grid[style*="grid-template-columns: 1fr 1fr"],
  .grid[style*="grid-template-columns:repeat(3"],
  .grid[style*="grid-template-columns: repeat(3"],
  .grid[style*="grid-template-columns:repeat(4"],
  .grid[style*="grid-template-columns: repeat(4"] {
    grid-template-columns: 1fr !important;
  }
  .grid[style*="grid-template-columns:repeat(2"],
  .grid[style*="grid-template-columns: repeat(2"] {
    grid-template-columns: 1fr !important;
  }
}

@media (max-width: 480px) {
  .grid[style*="grid-template-columns"] {
    grid-template-columns: 1fr !important;
  }
}

/* ---------- 6. STAT CARDS ---------- */
@media (max-width: 1024px) {
  .stat-card {
    padding: var(--space-4);
  }
  .stat-card-value {
    font-size: 22px;
  }
  .stat-card-icon {
    width: 32px;
    height: 32px;
  }
}

/* ---------- 7. TABLES ---------- */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

@media (max-width: 768px) {
  .table {
    font-size: 12px;
  }
  .table thead th, .table tbody td {
    padding: var(--space-2) var(--space-3);
    white-space: nowrap;
  }
  /* Make tables scroll horizontally on mobile */
  .table-wrap {
    margin: 0 calc(-1 * var(--space-3));
    border-radius: 0;
    border-left: none;
    border-right: none;
  }
}

/* ---------- 8. CARDS ---------- */
@media (max-width: 768px) {
  .card-pad, .card-body { padding: var(--space-4); }
  .card-pad-lg, .card-header, .card-footer { padding: var(--space-3) var(--space-4); }
  .card-header h3, .card-header h4 { font-size: 15px; }
}

/* ---------- 9. MODALS ---------- */
@media (max-width: 768px) {
  .modal-backdrop {
    padding: var(--space-3);
  }
  .modal {
    max-height: 95vh;
    border-radius: var(--radius-lg);
  }
  .modal-header, .modal-body, .modal-footer {
    padding: var(--space-4);
  }
  .modal-lg, .modal-xl { max-width: 100%; }
  .modal-body .form-row,
  .modal-body .form-row-3 {
    grid-template-columns: 1fr !important;
  }
}

/* ---------- 10. DRAWER ---------- */
@media (max-width: 768px) {
  .drawer {
    width: 100%;
    max-width: 100%;
  }
  .drawer-header, .drawer-body, .drawer-footer {
    padding: var(--space-4);
  }
}

/* ---------- 11. KANBAN ---------- */
.kanban-board {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scroll-snap-type: x mandatory;
  padding-bottom: var(--space-3);
}

@media (max-width: 768px) {
  .kanban-column {
    flex: 0 0 280px;
    scroll-snap-align: start;
  }
}

/* ---------- 12. CHAT LAYOUT ---------- */
@media (max-width: 1024px) {
  .chat-layout {
    grid-template-columns: 1fr;
    height: calc(100vh - 64px - 32px);
  }
  .chat-sidebar {
    display: none;
  }
  .chat-sidebar.mobile-visible {
    display: flex;
    position: absolute;
    inset: 0;
    z-index: 10;
    background: var(--bg-surface);
  }
  .chat-messages {
    padding: var(--space-3);
  }
  .chat-message {
    max-width: 85%;
  }
}

@media (max-width: 480px) {
  .chat-message {
    max-width: 90%;
  }
  .chat-input input {
    font-size: 16px; /* prevents iOS zoom */
  }
}

/* ---------- 13. WIKI ---------- */
@media (max-width: 1024px) {
  .wiki-layout {
    grid-template-columns: 1fr;
  }
  .wiki-sidebar {
    order: 2;
  }
}

/* ---------- 14. CALENDAR ---------- */
@media (max-width: 768px) {
  .calendar-grid {
    min-width: 700px;
  }
  .calendar-cell {
    min-height: 80px;
  }
  .calendar-event {
    font-size: 10px;
  }
}

/* ---------- 15. FUNNEL ---------- */
@media (max-width: 768px) {
  .funnel-stage {
    width: 90%;
  }
  .funnel-stage-num {
    left: -20px;
    width: 20px;
    height: 20px;
    font-size: 10px;
  }
}

/* ---------- 16. FILE GRID ---------- */
@media (max-width: 1024px) {
  .file-grid {
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  }
}

@media (max-width: 480px) {
  .file-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: var(--space-2);
  }
  .file-card {
    padding: var(--space-2);
  }
  .file-card-icon {
    width: 40px;
    height: 40px;
  }
}

/* ---------- 17. FORMS ---------- */
@media (max-width: 768px) {
  .form-row, .form-row-3 {
    grid-template-columns: 1fr !important;
  }
  .input, .select, .textarea {
    font-size: 16px; /* prevents iOS zoom */
  }
}

/* ---------- 18. NOTIFICATIONS PANEL ---------- */
@media (max-width: 480px) {
  .notification-panel {
    position: fixed;
    top: 64px;
    right: 0;
    left: 0;
    width: 100%;
    max-width: 100%;
    border-radius: 0 0 var(--radius-lg) var(--radius-lg);
  }
}

/* ---------- 19. AVATARS & BADGES ---------- */
@media (max-width: 480px) {
  .avatar-group .avatar {
    margin-left: -10px;
  }
}

/* ---------- 20. SEARCH BOX ---------- */
@media (max-width: 1024px) {
  .search-box-kbd {
    display: none;
  }
}

/* ---------- 21. BUTTONS ---------- */
@media (max-width: 480px) {
  .btn-lg {
    height: 40px;
    font-size: 14px;
  }
  .btn {
    height: 34px;
    font-size: 12px;
  }
}

/* ---------- 22. PREVENT HORIZONTAL SCROLL ---------- */
html, body {
  overflow-x: hidden;
  max-width: 100%;
}

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

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

/* ---------- 23. PROGRESS BARS ---------- */
@media (max-width: 480px) {
  .progress {
    height: 5px;
  }
}

/* ---------- 24. SETTINGS TABS ---------- */
@media (max-width: 768px) {
  .settings-layout, .settings-with-tabs {
    grid-template-columns: 1fr !important;
  }
  .settings-tabs-vertical {
    flex-direction: row !important;
    overflow-x: auto;
    padding-bottom: var(--space-2);
  }
  .settings-tabs-vertical .tab {
    white-space: nowrap;
  }
}

/* ---------- 25. INTELLIGENCE KPI ---------- */
@media (max-width: 1024px) {
  .kpi-grid {
    grid-template-columns: repeat(2, 1fr) !important;
  }
}

@media (max-width: 600px) {
  .kpi-grid {
    grid-template-columns: 1fr !important;
  }
}

/* ---------- 26. CHART CONTAINERS ---------- */
.chart-container, canvas {
  max-width: 100%;
}

/* ---------- 27. TOUCH-FRIENDLY ---------- */
@media (hover: none) and (pointer: coarse) {
  .btn:hover, .card-hover:hover {
    transform: none;
  }
  .dropdown-item:hover {
    background: var(--bg-elevated);
  }
}

/* ---------- 28. PRINT-FRIENDLY ALWAYS ---------- */
.no-print { /* defined */ }

/* ---------- 29. UTILITY: HIDE ON MOBILE ---------- */
.hide-mobile { display: initial; }
@media (max-width: 768px) {
  .hide-mobile { display: none !important; }
}

.show-mobile { display: none !important; }
@media (max-width: 768px) {
  .show-mobile { display: initial !important; }
}

/* ---------- 30. SEGMENTED CONTROL ---------- */
@media (max-width: 480px) {
  .segmented {
    width: 100%;
    overflow-x: auto;
  }
  .segmented button {
    flex-shrink: 0;
    white-space: nowrap;
  }
}

/* ---------- 31. TABS ---------- */
@media (max-width: 768px) {
  .tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .tab {
    white-space: nowrap;
  }
}

/* ---------- 32. EMPTY STATES ---------- */
@media (max-width: 480px) {
  .empty-state {
    padding: var(--space-8) var(--space-4);
  }
}
