/**
 * Адаптивный дизайн для мобильных устройств
 * Mobile-first responsive styles для Ninox
 */

/* === Брейкпоинты ===
   Mobile: < 768px
   Tablet: 768px - 1024px
   Desktop: > 1024px
*/

/* === Mobile стили (по умолчанию) ===
@media (max-width: 768px) {
  :root {
    --sidebar-width: 100%;
    --toolbar-h: 50px;
  }
}
*/

/* === Mobile адаптивность === */
@media (max-width: 768px) {
  /* App Shell */
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }

  /* Sidebar становится верхним меню */
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: auto;
    max-height: 100vh;
    overflow: hidden;
    z-index: 1000;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    padding: 0;
    display: flex;
    flex-direction: column;
  }

  .sidebar-scroll {
    padding: 10px;
  }

  .sidebar-dock {
    padding: 0 10px 10px;
  }

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

  /* Sidebar overlay */
  .sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 999;
  }

  .sidebar-overlay.active {
    display: block;
  }

  /* Mobile menu button */
  .mobile-menu-button {
    display: block !important;
    position: fixed;
    top: max(10px, env(safe-area-inset-top, 0px));
    left: max(10px, env(safe-area-inset-left, 0px));
    z-index: 1001;
    background: var(--accent);
    color: white;
    border: none;
    padding: 10px;
    border-radius: 8px;
    cursor: pointer;
  }

  /* Workspace занимает весь экран */
  .workspace {
    grid-column: 1 / -1;
    padding-top: max(52px, env(safe-area-inset-top, 0px));
  }

  /* Toolbar: колонка, поиск на всю ширину (базовый layout — flex в styles.css) */
  .workspace-toolbar {
    flex-direction: column;
    align-items: stretch;
    padding: 8px;
    gap: 8px;
  }

  .toolbar-group {
    flex-wrap: wrap;
    justify-content: flex-start;
  }

  .toolbar-group.align-end {
    margin-left: 0;
    justify-content: flex-start;
  }

  .toolbar-search {
    order: 3;
    flex: 1 1 auto;
    max-width: none;
    width: 100%;
  }

  .toolbar-search input {
    width: 100%;
  }

  /* Скрываем второстепенные кнопки на мобильных */
  .toolbar-icon-button:not(.toolbar-nav-button) {
    padding: 6px;
  }

  /* Detail panel на весь экран */
  .detail-panel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    width: 100% !important;
    z-index: 1002;
    transform: translateY(100%);
    transition: transform 0.3s ease;
  }

  .detail-panel.open {
    transform: translateY(0);
  }

  .detail-panel-head {
    border-radius: 0;
  }

  /* Dialogs на весь экран */
  .dialog-shell {
    border: none;
    border-radius: 0;
    margin: 0;
    padding: 0;
    width: 100% !important;
    max-width: 100% !important;
    height: 100% !important;
    max-height: 100% !important;
  }

  .dialog-card {
    border-radius: 0;
    padding: 15px;
  }

  /* Grid и таблицы */
  .record-grid-table {
    font-size: 12px;
    overflow-x: auto;
  }

  .record-grid-table th,
  .record-grid-table td {
    padding: 8px 4px;
    white-space: nowrap;
  }

  /* Карточки вместо таблицы на очень маленьких экранах */
  @media (max-width: 480px) {
    .record-grid-table thead {
      display: none;
    }

    .record-grid-table tbody {
      display: block;
    }

    .record-grid-table tr {
      display: block;
      margin-bottom: 15px;
      padding: 10px;
      background: var(--card-bg);
      border-radius: 8px;
      box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    }

    .record-grid-table td {
      display: block;
      text-align: right;
      padding: 5px 10px;
      border-bottom: 1px solid var(--border-color);
    }

    .record-grid-table td::before {
      content: attr(data-label);
      float: left;
      font-weight: bold;
      text-transform: uppercase;
      font-size: 11px;
      color: var(--muted);
    }
  }

  /* Summary strip */
  .summary-strip {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .summary-pill {
    display: inline-flex;
    flex-shrink: 0;
    margin-right: 8px;
  }

  /* Mode buttons */
  .mode-switch {
    display: flex;
    justify-content: space-around;
  }

  .mode-button span:not(.nav-icon) {
    display: none;
  }

  .mode-button {
    padding: 10px;
  }

  /* Sidebar sections */
  .sidebar-section {
    margin-bottom: 15px;
  }

  .table-list {
    display: grid;
    grid-template-columns: 1fr;
    gap: 8px;
  }

  /* Кнопки действий */
  .sidebar-footer-actions {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 5px;
  }

  .sidebar-footer-button {
    font-size: 11px;
    padding: 8px 4px;
  }

  /* Workspace actions */
  .workspace-actions {
    flex-wrap: wrap;
    gap: 8px;
  }

  .workspace-actions .filter-box {
    flex-basis: calc(50% - 4px);
  }

  .workspace-actions .primary-button {
    flex-basis: 100%;
    order: 3;
  }

  /* Report blocks */
  .report-block-card {
    margin-bottom: 10px;
  }

  .report-kpi-grid {
    grid-template-columns: 1fr;
  }

  /* Field draft list */
  .field-draft-list {
    max-height: 400px;
    overflow-y: auto;
  }

  /* Block grid в отчётах */
  .block-grid {
    grid-template-columns: 1fr;
  }

  /* Report builder options */
  .report-block-options {
    grid-template-columns: 1fr;
  }

  /* Touch-friendly элементы */
  button,
  input,
  select,
  textarea,
  .table-button,
  .nav-entry {
    min-height: 44px; /* Минимальный размер для touch */
  }

  /* Увеличиваем tap targets */
  .toolbar-icon-button,
  .sidebar-add-button,
  .ghost-button {
    min-width: 44px;
    min-height: 44px;
  }

  /* Скрываем hover эффекты на touch устройствах */
  @media (hover: none) {
    .table-button:hover,
    .nav-entry:hover,
    .record-grid-table tr:hover td {
      background: transparent;
    }

    .table-button:active,
    .nav-entry:active {
      background: var(--active-bg);
    }
  }
}

/* === Tablet адаптивность (768px - 1024px) === */
@media (min-width: 769px) and (max-width: 1024px) {
  :root {
    --sidebar-width: 220px;
  }

  .app-shell {
    grid-template-columns: var(--sidebar-width) 1fr;
  }

  .sidebar {
    transform: none !important;
    position: relative;
  }

  .mobile-menu-button {
    display: none !important;
  }

  .workspace-toolbar {
    padding: 10px;
  }

  .toolbar-search {
    max-width: 300px;
  }

  .detail-panel {
    width: 400px !important;
  }

  .record-grid-table {
    font-size: 13px;
  }

  .report-kpi-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* === Desktop оптимизация (> 1024px) === */
@media (min-width: 1025px) {
  .mobile-menu-button {
    display: none !important;
  }

  .sidebar-overlay {
    display: none !important;
  }
}

/* === Пейзажная ориентация на мобильных === */
@media (max-width: 768px) and (orientation: landscape) {
  .sidebar {
    max-height: 100vh;
  }

  .dialog-shell {
    max-height: 90vh;
  }

  .detail-panel {
    max-height: 90vh;
  }
}

/* === Плотность пикселей === */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
  /* Retina дисплеи */
  .record-grid-table,
  .table-button,
  .nav-entry {
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
  }
}

/* === Предпочтение уменьшенного движения === */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* === Тёмная тема для мобильных === */
@media (max-width: 768px) {
  :root[data-theme="dark"] {
    --sidebar-bg: #0a0a14;
    --toolbar-bg: #1a1a2e;
    --card-bg: #1e1e2e;
  }
}

/* === Печать === */
@media print {
  .sidebar,
  .workspace-toolbar,
  .workspace-view-tabs,
  .detail-panel,
  .mobile-menu-button,
  .sidebar-overlay {
    display: none !important;
  }

  .app-shell {
    display: block;
  }

  .workspace {
    padding: 0;
  }

  .record-grid-table {
    font-size: 10px;
    border: 1px solid #000;
  }

  .record-grid-table th,
  .record-grid-table td {
    border: 1px solid #000;
    padding: 4px;
  }

  body {
    background: white;
    color: black;
  }
}
