/* ============================================================================
 * KODER OS 4.0 – COMPLETE MOBILE EXPERIENCE (<= 900px / <= 768px)
 * Floating Liquid Glass Bottom Nav, Card-Transformed Data Tables, Fullscreen
 * Mobile Modals, 1-Col Form Grids, Mail Mobile Stack, Document Studio Mobile
 * ============================================================================ */

/* Hidden on Desktop */
.m-topbar,
.m-bottom-nav,
.m-sheet-overlay,
.m-table-mode-bar {
  display: none;
}

body.theme-dark .m-topbar,
body.theme-dark .m-bottom-nav {
  background: var(--ig-glass-bg);
  border-color: var(--ig-glass-border);
  box-shadow: 0 12px 34px rgba(0, 0, 0, .34);
}
body.theme-dark .m-touch-btn {
  color: var(--text-main);
  background: var(--bg-surface);
  border-color: var(--border);
}
body.theme-dark .m-nav-item.active { color: var(--text-main); }
body.theme-dark .m-nav-center-btn {
  color: #181A1B;
  background: var(--primary);
  border-color: var(--primary-border);
  box-shadow: 0 8px 20px rgba(0, 0, 0, .32);
}
body.theme-dark .m-sheet-overlay { background: rgba(8, 9, 10, .72); }

@media (max-width: 900px) {
  body {
    flex-direction: column !important;
  }

  /* Hide Desktop Dual-Navigation & Desktop Tabs on Smartphones */
  #koderWorkspaceRail,
  #koderWorkspacePanel,
  #koderSubpanelResizer,
  .ig-topbar {
    display: none !important;
  }

  /* Minimalist Mobile Topbar */
  .m-topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
    min-height: calc(52px + env(safe-area-inset-top));
    background: var(--ig-glass-bg, rgba(12, 16, 24, 0.88));
    backdrop-filter: blur(18px);
    -webkit-backdrop-filter: blur(18px);
    border-bottom: 1px solid var(--border);
    z-index: 80;
    flex-shrink: 0;
  }
  .m-topbar-left {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
  }
  .m-topbar-title {
    font-size: 16px;
    font-weight: 700;
    color: var(--text-main);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .m-topbar-actions {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
  }
  .m-touch-btn {
    min-width: 44px;
    min-height: 44px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--bg-surface);
    color: var(--text-main);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    position: relative;
  }
  .m-touch-btn:active {
    transform: scale(0.95);
  }
  .m-task-count {
    position: absolute;
    top: 5px;
    right: 5px;
    min-width: 16px;
    height: 16px;
    padding: 0 4px;
    border-radius: 999px;
    background: var(--primary);
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  /* Sublinks Bar: Smooth horizontal swipeable pills */
  #tabSublinksBar {
    display: flex !important;
    flex-wrap: nowrap !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    gap: 6px !important;
    padding: 8px 12px !important;
    scrollbar-width: none;
  }
  #tabSublinksBar::-webkit-scrollbar { display: none; }
  #tabSublinksBar > * { flex-shrink: 0 !important; }

  /* Safe Content Area above Floating Bottom Nav */
  .content-body {
    padding: 10px 10px calc(94px + env(safe-area-inset-bottom)) !important;
  }

  .panel, .card {
    padding: 12px !important;
    border-radius: 12px !important;
  }
  .panel-header, .panel-header > div, .section-header {
    flex-wrap: wrap !important;
    max-width: 100% !important;
    gap: 8px !important;
  }
  .panel-header input, .panel-header select {
    max-width: 100% !important;
    min-width: 0 !important;
    flex: 1 1 160px;
  }

  /* Prevent iOS Safari Auto-Zoom on Inputs & Ensure 44px Touch Targets */
  input:not([type="checkbox"]):not([type="radio"]):not([type="color"]),
  select,
  textarea,
  .form-input,
  .form-select {
    font-size: 16px !important;
    min-height: 44px !important;
    width: 100%;
    box-sizing: border-box;
  }
  .btn {
    min-height: 42px;
  }
  .btn-sm {
    min-height: 38px;
  }
  .btn-xs {
    min-height: 34px;
    padding: 4px 10px;
  }

  /* Bento KPI Grid on Mobile (2 columns) */
  #view-dashboard .kpi-grid,
  .kpi-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 8px !important;
  }
  .kpi-card {
    padding: 10px !important;
    min-height: 70px !important;
    grid-template-columns: 32px minmax(0, 1fr) !important;
    column-gap: 8px !important;
  }
  .kpi-card .kpi-icon {
    width: 32px !important;
    height: 32px !important;
  }
  .kpi-value {
    font-size: 16px !important;
  }

  /* Floating Liquid Glass Bottom Navigation */
  .m-bottom-nav {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    align-items: center;
    position: fixed;
    left: 10px;
    right: 10px;
    bottom: max(10px, env(safe-area-inset-bottom));
    height: 62px;
    padding: 0 6px;
    border-radius: 20px;
    background: var(--ig-glass-bg, rgba(12, 16, 24, 0.88));
    backdrop-filter: blur(22px);
    -webkit-backdrop-filter: blur(22px);
    border: 1px solid var(--ig-glass-border, rgba(255, 255, 255, 0.14));
    box-shadow: 0 12px 34px rgba(0, 0, 0, 0.28);
    z-index: 95;
  }
  .m-nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    height: 50px;
    border: none;
    background: transparent;
    color: var(--text-muted);
    font-size: 10px;
    font-weight: 600;
    cursor: pointer;
    position: relative;
    border-radius: 12px;
  }
  .m-nav-item.active {
    color: var(--primary);
  }
  .m-nav-center-btn {
    width: 48px;
    height: 48px;
    margin: -18px auto 0;
    border-radius: 16px;
    border: 1px solid rgba(255, 255, 255, 0.22);
    background: linear-gradient(135deg, var(--primary) 0%, #1D4ED8 100%);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 8px 20px rgba(37, 99, 235, 0.45);
    cursor: pointer;
  }
  .m-nav-center-btn:active {
    transform: scale(0.94);
  }

  /* Bottom Sheet Overlay & Container */
  .m-sheet-overlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 10100;
    background: rgba(8, 11, 17, 0.58);
    backdrop-filter: blur(6px);
    align-items: flex-end;
    justify-content: center;
  }
  .m-sheet-overlay.open {
    display: flex;
  }
  .m-sheet {
    width: 100%;
    max-width: 560px;
    max-height: 84vh;
    background: var(--bg-card);
    color: var(--text-main);
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
    border: 1px solid var(--border);
    padding: 10px 14px calc(18px + env(safe-area-inset-bottom));
    display: flex;
    flex-direction: column;
    gap: 10px;
    overflow-y: auto;
    animation: mSheetUp 0.18s cubic-bezier(0.22, 1, 0.36, 1);
  }
  @keyframes mSheetUp {
    from { transform: translateY(40px); opacity: 0.4; }
    to { transform: translateY(0); opacity: 1; }
  }
  .m-sheet-handle {
    width: 40px;
    height: 4px;
    border-radius: 999px;
    background: var(--border);
    margin: 0 auto 4px;
  }
  .m-action-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
  }
  .m-action-tile {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-height: 76px;
    padding: 8px 6px;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: var(--bg-surface);
    color: var(--text-main);
    font-size: 11px;
    font-weight: 600;
    text-align: center;
    cursor: pointer;
  }
  .m-action-tile:active {
    background: var(--primary-subtle);
    border-color: var(--primary);
  }
}

/* ============================================================================
 * SMARTPHONE FULL PAGE, FORM, MODAL, MAIL, DOCUMENT STUDIO & TABLE CARDS (<=768px)
 * ============================================================================ */
@media (max-width: 768px) {
  /* Collapse ALL multi-column inline grids inside views/forms to 1 clean column */
  .view-section div[style*="grid-template-columns"]:not(.kpi-grid):not(.m-action-grid),
  .modal-body div[style*="grid-template-columns"]:not(.m-action-grid),
  .shop-product-grid,
  .form-grid,
  .dashboard-lower-grid {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  /* Native Full-Width Bottom-Sheet Modals on Smartphones */
  .modal-overlay {
    padding: 0 !important;
    align-items: flex-end !important;
  }
  .modal-content {
    width: 100% !important;
    max-width: 100% !important;
    max-height: 92dvh !important;
    border-radius: 18px 18px 0 0 !important;
    margin: 0 !important;
    display: flex;
    flex-direction: column;
  }
  .modal-body {
    overflow-y: auto !important;
    padding: 14px 14px calc(24px + env(safe-area-inset-bottom)) !important;
  }

  /* Mobile Card Transformation for ALL Data Tables */
  .m-table-mode-bar {
    display: flex;
    justify-content: flex-end;
    margin-bottom: 6px;
  }
  table.data-table:not(.mobile-raw-table) {
    border: none !important;
    background: transparent !important;
  }
  table.data-table:not(.mobile-raw-table) thead {
    display: none !important;
  }
  table.data-table:not(.mobile-raw-table),
  table.data-table:not(.mobile-raw-table) tbody,
  table.data-table:not(.mobile-raw-table) tr,
  table.data-table:not(.mobile-raw-table) td {
    display: block;
    width: 100%;
    box-sizing: border-box;
  }
  table.data-table:not(.mobile-raw-table) tbody tr {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 10px 12px;
    margin-bottom: 10px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
  }
  table.data-table:not(.mobile-raw-table) tbody td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    height: auto !important;
    min-height: 30px;
    padding: 5px 0 !important;
    border: none !important;
    border-bottom: 1px solid var(--border-subtle) !important;
    text-align: right;
    font-size: 13px;
    word-break: break-word;
  }
  table.data-table:not(.mobile-raw-table) tbody td:last-child {
    border-bottom: none !important;
    justify-content: flex-start;
    flex-wrap: wrap;
    gap: 6px;
    padding-top: 8px !important;
    text-align: left;
  }
  table.data-table:not(.mobile-raw-table) tbody td::before {
    content: attr(data-label);
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    color: var(--text-muted);
    text-align: left;
    flex-shrink: 0;
  }
  table.data-table:not(.mobile-raw-table) tbody td[colspan]::before {
    display: none;
  }
  table.data-table:not(.mobile-raw-table) tbody td[colspan] {
    justify-content: center;
    text-align: center;
  }

  /* Mail 2.0 Mobile Stack */
  .mail-v2-shell {
    flex-direction: column !important;
    height: auto !important;
  }
  .mail-v2-shell > * {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
  }
  .mail-resizer {
    display: none !important;
  }

  /* Document studio: category chips and a deliberate full-screen preview */
  .doc-center-shell { grid-template-columns: minmax(0, 1fr) !important; }
  .doc-col-cats {
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 5px;
  }
  .doc-col-cats > .doc-section-label { grid-column: 1 / -1; }
  .doc-col-preview.is-open { inset: 1vh 1vw; }
  .doc-template-list-wrap .data-table { min-width: 680px; }
}
