/* ============================================================
   Cloud5 — Global Mobile / Responsive Styles
   Подключается на всех страницах
   ============================================================ */

/* --- Safe area for notched phones --- */
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
body {
  overflow-x: hidden;
}

/* --- Tables: horizontal scroll wrapper --- */
.table-responsive,
.table-wrap,
.scroll-x {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

@media (max-width: 768px) {
  /* Generic containers */
  .container,
  .main-content,
  .content,
  .page-content {
    padding-left: 12px !important;
    padding-right: 12px !important;
  }

  body {
    padding-left: 0 !important;
    padding-right: 0 !important;
  }

  /* Cards / panels */
  .card,
  .panel,
  .box {
    margin-left: 0 !important;
    margin-right: 0 !important;
    border-radius: 10px !important;
  }

  /* Headers */
  .header,
  .topbar,
  .page-header {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 10px !important;
    padding: 12px 14px !important;
  }
  .header h1,
  .topbar h1,
  .page-header h1 {
    font-size: 18px !important;
  }
  .header-actions,
  .topbar-actions,
  .actions {
    width: 100%;
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 8px !important;
  }
  .header-actions .btn,
  .topbar .btn,
  .actions .btn {
    flex: 1 1 auto;
    min-width: 0;
    justify-content: center;
    font-size: 13px !important;
    padding: 8px 12px !important;
  }

  /* Buttons */
  .btn {
    min-height: 40px;
    touch-action: manipulation;
  }
  .btn-sm {
    min-height: 34px;
  }

  /* Forms */
  input[type="text"],
  input[type="password"],
  input[type="email"],
  input[type="number"],
  input[type="search"],
  input[type="date"],
  input[type="datetime-local"],
  select,
  textarea {
    font-size: 16px !important; /* prevent iOS zoom */
    max-width: 100%;
  }
  form {
    max-width: 100%;
  }

  /* Tables */
  table {
    font-size: 12px !important;
  }
  th, td {
    padding: 8px 6px !important;
  }
  /* Auto-wrap bare tables that aren't already in scroll containers */
  .card > table,
  .panel > table,
  .box > table,
  #topFiles table,
  #topFolders table,
  #topDownloaded table,
  #topViewed table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
  }

  /* Grids */
  .grid-4,
  .grid-3,
  .grid-2,
  .stats-grid,
  .pb-grid {
    grid-template-columns: 1fr 1fr !important;
    gap: 10px !important;
  }

  /* Tabs */
  .tabs,
  .tab-bar {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    flex-wrap: nowrap !important;
    white-space: nowrap;
  }
  .tab,
  .tabs .tab {
    flex-shrink: 0;
  }

  /* Modals */
  .modal-content,
  .modal .modal-content {
    width: 96vw !important;
    max-width: 96vw !important;
    max-height: 90vh !important;
    margin: 8px !important;
    border-radius: 12px !important;
  }

  /* File viewer */
  .fv-window {
    width: 100vw !important;
    height: 100vh !important;
    max-width: 100vw !important;
    max-height: 100vh !important;
    border-radius: 0 !important;
  }
  .fv-window.minimized {
    width: calc(100vw - 24px) !important;
    height: 48px !important;
    bottom: 12px !important;
    right: 12px !important;
    left: 12px !important;
  }

  /* Tree rows in statistics */
  .tree-row {
    grid-template-columns: 1fr 50px 70px !important;
    font-size: 12px !important;
    gap: 4px !important;
  }
  .tree-row > :nth-child(4),
  .tree-row > :nth-child(5),
  .tree-row > :nth-child(6) {
    display: none !important;
  }
  .tree-row.header > :nth-child(4),
  .tree-row.header > :nth-child(5),
  .tree-row.header > :nth-child(6) {
    display: none !important;
  }

  /* Context menus */
  .context-menu,
  #folderContextMenu,
  #fileContextMenu {
    max-width: calc(100vw - 16px) !important;
  }

  /* Upload queue */
  #uploadQueue {
    width: calc(100vw - 20px) !important;
    right: 10px !important;
    left: 10px !important;
    bottom: 10px !important;
  }

  /* Resources monitor cards */
  .cards,
  .resource-grid {
    grid-template-columns: 1fr !important;
  }

  /* Photo bot */
  .topbar {
    flex-direction: column !important;
    align-items: stretch !important;
  }

  /* News widget */
  #news-widget-root {
    max-width: calc(100% - 24px) !important;
  }

  /* Thumbnails grid */
  .thumbnails-grid,
  .thumbs,
  .file-grid {
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)) !important;
    gap: 8px !important;
  }

  /* Hide less important columns on very small screens handled below */
}

@media (max-width: 480px) {
  .grid-4,
  .grid-3,
  .grid-2,
  .stats-grid,
  .pb-grid {
    grid-template-columns: 1fr !important;
  }

  .stat-value {
    font-size: 20px !important;
  }

  h1 { font-size: 18px !important; }
  h2 { font-size: 16px !important; }

  .btn {
    font-size: 13px !important;
    padding: 8px 10px !important;
  }

  /* Statistics tabs scroll */
  .tabs {
    gap: 4px !important;
    padding-bottom: 4px;
  }
  .tab {
    padding: 8px 10px !important;
    font-size: 12px !important;
  }
}

/* ============================================================
   APP LAYOUT: sidebar + main (admin & user dashboard)
   ============================================================ */

/* Mobile top bar (hidden on desktop) */
.app-mobile-bar {
  display: none;
  position: sticky;
  top: 0;
  z-index: 200;
  background: #1e3a8a;
  color: #fff;
  padding: 10px 12px;
  padding-top: max(10px, env(safe-area-inset-top));
  align-items: center;
  gap: 10px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}
.app-mobile-bar .burger {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 8px;
  background: rgba(255,255,255,0.15);
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.app-mobile-bar .burger:active {
  background: rgba(255,255,255,0.3);
}
.app-mobile-bar .title {
  flex: 1;
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.app-mobile-bar .bar-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}
.app-mobile-bar .bar-actions a,
.app-mobile-bar .bar-actions button {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 8px;
  background: rgba(255,255,255,0.15);
  color: #fff;
  font-size: 16px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  cursor: pointer;
}

/* Overlay behind open sidebar */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 150;
  opacity: 0;
  transition: opacity 0.25s ease;
}
.sidebar-overlay.visible {
  display: block;
  opacity: 1;
}

@media (max-width: 850px) {
  .app-mobile-bar {
    display: flex;
  }

  .app-container {
    flex-direction: column !important;
    height: auto !important;
    min-height: 100vh !important;
  }

  .sidebar {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    bottom: 0 !important;
    width: min(320px, 85vw) !important;
    max-width: 320px !important;
    height: 100vh !important;
    height: 100dvh !important;
    z-index: 160 !important;
    transform: translateX(-105%);
    transition: transform 0.28s ease !important;
    box-shadow: 4px 0 24px rgba(0,0,0,0.25) !important;
    padding-top: env(safe-area-inset-top) !important;
  }
  .sidebar.mobile-open {
    transform: translateX(0) !important;
  }
  /* Don't use collapsed width on mobile */
  .sidebar.collapsed {
    width: min(320px, 85vw) !important;
  }
  .sidebar.collapsed .sidebar-header h3,
  .sidebar.collapsed .folder-item span:not(.icon):not(.lock-icon):not(.hidden-icon),
  .sidebar.collapsed .folder-checkbox,
  .sidebar.collapsed .arrow,
  .sidebar.collapsed .folder-name {
    display: initial !important;
  }
  .sidebar.collapsed .folder-item {
    justify-content: flex-start !important;
    padding: 6px 16px !important;
  }

  .main-content,
  .content-area {
    width: 100% !important;
    max-width: 100% !important;
    padding: 12px !important;
    margin: 0 !important;
  }

  /* Header actions stack */
  .header {
    flex-direction: column !important;
    align-items: stretch !important;
  }
  .header-actions {
    width: 100%;
  }
  .header-actions .btn {
    font-size: 12px !important;
    padding: 8px 10px !important;
  }
  /* Hide long text buttons labels on tiny screens - keep icons if present */
  .menu-dropdown .menu-panel {
    right: 0 !important;
    left: auto !important;
    min-width: 200px !important;
    max-width: calc(100vw - 24px) !important;
  }

  /* File table */
  .files-table,
  table.files {
    font-size: 12px !important;
  }

  /* Sort panel */
  .sort-panel,
  .sort-panel__row {
    flex-direction: column !important;
    align-items: stretch !important;
  }
  .sp-select {
    width: 100% !important;
    min-width: 0 !important;
  }
}

/* Touch-friendly folder items */
@media (max-width: 850px) {
  .folder-tree .folder-item {
    padding: 10px 16px !important;
    min-height: 44px;
  }
  .folder-menu-btn {
    display: inline-flex !important;
    width: 32px !important;
    height: 32px !important;
  }
}

/* Landscape phones */
@media (max-width: 900px) and (orientation: landscape) {
  .app-mobile-bar {
    padding: 6px 12px;
  }
  .sidebar {
    width: min(280px, 50vw) !important;
  }
}

/* Prevent body scroll when sidebar open */
body.sidebar-open {
  overflow: hidden !important;
  touch-action: none;
}

/* Shared pages already partly adapted — reinforce */
@media (max-width: 600px) {
  .shared-folder .header-inner,
  .header-inner {
    flex-direction: column !important;
    align-items: flex-start !important;
  }
}

/* Print: hide mobile chrome */
@media print {
  .app-mobile-bar,
  .sidebar-overlay,
  .sidebar {
    display: none !important;
  }
}
