/* ============================================
 * Mobile Native-Style Layout Overrides
 * Applies strictly at max-width: 768px.
 * Tablet and desktop (769px+) are never affected.
 * ============================================ */

@media (max-width: 768px) {
  html, body {
    overflow-x: hidden !important;
    max-width: 100vw !important;
  }

  /* --------------------------------------------
   * 1. Navigation & Header
   * Hide hamburger button as main nav; use bottom bar + More tab
   * -------------------------------------------- */
  #mobile-menu-btn {
    display: none !important;
  }

  .mobile-header {
    height: 56px;
    padding: 0 1rem;
    position: sticky;
    top: 0;
    z-index: 90;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
  }

  .main-content {
    margin-left: 0 !important;
    padding: 0.75rem 1rem calc(72px + env(safe-area-inset-bottom, 0px)) 1rem !important;
    -webkit-overflow-scrolling: touch;
  }

  /* --------------------------------------------
   * 2. Bottom Tab Bar
   * Fixed 5-tab bar (Dashboard, Notes, Tasks, Goals, More)
   * -------------------------------------------- */
  .mobile-tab-bar {
    display: flex !important;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    height: calc(56px + env(safe-area-inset-bottom, 0px));
    padding-bottom: env(safe-area-inset-bottom, 0px);
    background: var(--bg-secondary);
    border-top: 1px solid var(--border);
    z-index: 95;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.06);
    touch-action: manipulation;
    transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.2s ease;
  }

  .mobile-tab-bar.keyboard-open {
    transform: translateY(100%);
    opacity: 0;
    pointer-events: none;
  }

  .mobile-tab-bar .tab-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 48px;
    min-width: 44px;
    padding: 0.35rem 0.2rem;
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    font-family: inherit;
    font-size: 0.7rem;
    font-weight: 500;
    gap: 0.2rem;
    text-decoration: none;
    touch-action: manipulation;
    transition: color 0.15s ease;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
  }

  .mobile-tab-bar .tab-item i,
  .mobile-tab-bar .tab-item svg {
    width: 20px;
    height: 20px;
    stroke-width: 2;
    transition: transform 0.15s ease;
  }

  .mobile-tab-bar .tab-item.active {
    color: var(--accent-primary);
    font-weight: 600;
  }

  .mobile-tab-bar .tab-item.active i,
  .mobile-tab-bar .tab-item.active svg {
    transform: scale(1.1);
  }

  /* --------------------------------------------
   * 3. Floating Action Button (FAB)
   * Bottom right (bottom left in RTL) for current section action
   * -------------------------------------------- */
  .mobile-fab {
    display: flex !important;
    align-items: center;
    justify-content: center;
    position: fixed;
    bottom: calc(56px + env(safe-area-inset-bottom, 0px) + 16px);
    right: 16px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--accent-primary);
    color: var(--accent-contrast, #ffffff);
    border: none;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.28);
    cursor: pointer;
    z-index: 92;
    touch-action: manipulation;
    transition: transform 0.2s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.2s ease;
    -webkit-tap-highlight-color: transparent;
  }

  .mobile-fab i,
  .mobile-fab svg {
    width: 24px;
    height: 24px;
    stroke-width: 2.2;
  }

  .mobile-fab:active {
    transform: scale(0.92);
  }

  .mobile-fab.hidden {
    opacity: 0 !important;
    pointer-events: none !important;
    transform: scale(0.7) !important;
  }

  /* --------------------------------------------
   * 4. Compact Stat Cards (2-column grid on dashboard only)
   * -------------------------------------------- */
  #section-dashboard .card-grid,
  .card-grid:has(.stat-card) {
    display: grid !important;
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 0.75rem !important;
    margin-bottom: 1.25rem !important;
  }

  /* Goals cards full-width 1-column on mobile */
  #section-goals .card-grid {
    display: flex !important;
    flex-direction: column !important;
    gap: 0.875rem !important;
    width: 100% !important;
    margin-bottom: 1.25rem !important;
  }

  .card-grid .stat-card {
    padding: 0.875rem !important;
    gap: 0.625rem !important;
    min-height: 44px;
  }

  .card-grid .stat-card h3 {
    font-size: 1.25rem !important;
    line-height: 1.2;
  }

  .card-grid .stat-card p {
    font-size: 0.75rem !important;
  }

  .card-grid .stat-card div:first-child {
    padding: 0.625rem !important;
  }

  .card-grid .stat-card div:first-child i,
  .card-grid .stat-card div:first-child svg {
    width: 18px;
    height: 18px;
  }

  /* --------------------------------------------
   * 5. Dashboard Order & Horizontal Swipe Rows (85% Width with Peek)
   * Puts Today's Tasks block first, followed by Active Goals, Habits, Stats, Thoughts
   * -------------------------------------------- */
  #section-dashboard.active {
    display: flex;
    flex-direction: column;
  }

  #section-dashboard > * {
    order: 10;
  }

  #section-dashboard > .section-header {
    order: 1 !important;
    margin-bottom: 0.75rem;
  }

  #section-dashboard > .starter-banner-card,
  #section-dashboard > .getting-started-card,
  #section-dashboard > .backup-sync-card {
    order: 2 !important;
    margin-bottom: 1rem !important;
    padding: 1rem !important;
  }

  /* Most important block (Today's Tasks) first */
  #section-dashboard > .dashboard-split-grid {
    display: flex !important;
    flex-direction: column !important;
    gap: 1.25rem !important;
    order: 3 !important;
    margin-bottom: 1rem;
  }

  .dashboard-split-grid > div:first-child,
  .dashboard-tasks-block {
    order: 1;
  }

  .dashboard-split-grid > div:last-child,
  .dashboard-goals-block {
    order: 2;
  }

  #section-dashboard > .dashboard-habits-block {
    order: 4 !important;
    margin-top: 0 !important;
    margin-bottom: 1rem !important;
  }

  #section-dashboard > .card-grid {
    order: 5 !important;
  }

  #section-dashboard > .dashboard-recent-thoughts-heading,
  #section-dashboard > h3 {
    order: 6 !important;
    margin-top: 1rem !important;
    margin-bottom: 0.75rem !important;
    font-size: 1.1rem !important;
  }

  #section-dashboard > .braindump-grid {
    order: 7 !important;
  }

  #section-dashboard > .resurfaced-section-header {
    order: 8 !important;
    margin-top: 1.25rem !important;
    margin-bottom: 0.75rem !important;
  }

  #section-dashboard > .resurfaced-grid {
    order: 9 !important;
  }

  /* Horizontal swipe container with snap on dashboard only */
  .dashboard-swipe-row,
  #section-dashboard > .braindump-grid,
  #section-dashboard > .resurfaced-grid {
    display: flex !important;
    overflow-x: auto !important;
    scroll-snap-type: x mandatory !important;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-x pan-y;
    gap: 0.75rem !important;
    padding: 0.25rem 0 0.75rem 0 !important;
    margin: 0 0 0.5rem 0 !important;
    scrollbar-width: none;
  }

  .dashboard-swipe-row::-webkit-scrollbar,
  #section-dashboard > .braindump-grid::-webkit-scrollbar,
  #section-dashboard > .resurfaced-grid::-webkit-scrollbar {
    display: none;
  }

  /* Each card is ~85% width so the next one peeks */
  .dashboard-swipe-row > *,
  #section-dashboard > .braindump-grid > *,
  #section-dashboard > .resurfaced-grid > * {
    flex: 0 0 85% !important;
    max-width: 85% !important;
    scroll-snap-align: start !important;
    scroll-snap-stop: normal;
    margin: 0 !important;
    box-sizing: border-box;
  }

  /* Main Notes section: vertical stack 100% width */
  #section-braindump .braindump-grid {
    display: block !important;
    columns: 1 !important;
    width: 100% !important;
    margin: 0 !important;
    padding: 0 !important;
  }

  #section-braindump .braindump-grid > * {
    display: block !important;
    width: 100% !important;
    max-width: 100% !important;
    flex: none !important;
    margin-bottom: 0.875rem !important;
    box-sizing: border-box !important;
  }

  /* --------------------------------------------
   * 6. Sticky Section Headers and Filters
   * Pinned below mobile-header when scrolling long lists
   * -------------------------------------------- */
  .section.active > .section-header {
    position: sticky;
    top: 56px;
    z-index: 25;
    background: var(--bg-primary);
    padding-top: 0.5rem;
    padding-bottom: 0.5rem;
    margin-bottom: 0.5rem;
    border-bottom: 1px solid var(--border-subtle, var(--border));
  }

  .section.active > .filter-bar,
  .section.active .filter-bar,
  .filter-bar {
    position: sticky;
    top: 104px;
    z-index: 24;
    background: var(--bg-primary);
    display: flex !important;
    flex-direction: row !important;
    flex-wrap: nowrap !important;
    align-items: center !important;
    gap: 0.5rem !important;
    padding-top: 0.25rem !important;
    padding-bottom: 0.5rem !important;
    margin-bottom: 0.75rem !important;
    overflow-x: auto !important;
    white-space: nowrap !important;
    scrollbar-width: none !important;
    -webkit-overflow-scrolling: touch !important;
  }

  .filter-bar::-webkit-scrollbar {
    display: none !important;
  }

  .filter-bar .btn,
  .filter-bar button,
  .filter-bar .filter-btn {
    flex: 0 0 auto !important;
    width: auto !important;
    min-height: 40px !important;
    touch-action: manipulation;
  }

  /* --------------------------------------------
   * 7. Collapsible Task / List Groups
   * -------------------------------------------- */
  .mobile-group-header {
    display: flex;
    width: 100%;
    font-family: inherit;
    text-align: inherit;
    align-items: center;
    justify-content: space-between;
    min-height: 44px;
    padding: 0.625rem 0.875rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-primary);
    cursor: pointer;
    touch-action: manipulation;
    user-select: none;
    margin: 0.75rem 0 0.5rem 0;
    -webkit-tap-highlight-color: transparent;
  }

  .mobile-group-header i,
  .mobile-group-header svg {
    width: 16px;
    height: 16px;
    transition: transform 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  }

  .mobile-group-header.collapsed i,
  .mobile-group-header.collapsed svg {
    transform: rotate(-90deg);
  }

  .mobile-group-items {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    transition: opacity 0.2s ease;
  }

  .mobile-group-items.collapsed {
    display: none !important;
  }

  /* --------------------------------------------
   * 8. Compact Cards & Interactive Touch Targets
   * -------------------------------------------- */
  .card {
    padding: 1rem;
    border-radius: var(--radius-md);
  }

  .task-item {
    padding: 0.875rem !important;
  }

  .task-checkbox {
    min-width: 44px !important;
    min-height: 44px !important;
    width: 44px !important;
    height: 44px !important;
    margin: 0 !important;
    padding: 0 !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    appearance: none !important;
    -webkit-appearance: none !important;
    background: transparent !important;
    border: none !important;
    cursor: pointer !important;
    position: relative !important;
    flex-shrink: 0 !important;
    touch-action: manipulation;
  }

  .task-checkbox::before {
    content: '' !important;
    display: block !important;
    width: 20px !important;
    height: 20px !important;
    border: 2px solid var(--border-strong, #1e1e1e) !important;
    border-radius: 6px !important;
    background: var(--bg-primary, #ffffff) !important;
    box-sizing: border-box !important;
    transition: all 0.15s ease !important;
  }

  .task-checkbox:checked::before {
    background: var(--accent-solid, var(--accent-primary, #3D63F0)) !important;
    border-color: var(--accent-solid, var(--accent-primary, #3D63F0)) !important;
  }

  .task-checkbox::after {
    content: '' !important;
    position: absolute !important;
    width: 5px !important;
    height: 9px !important;
    border: solid var(--accent-contrast, #ffffff) !important;
    border-width: 0 2px 2px 0 !important;
    transform: rotate(45deg) scale(0) !important;
    transition: transform 0.15s ease !important;
    margin-top: -2px !important;
    box-sizing: border-box !important;
  }

  .task-checkbox:checked::after {
    transform: rotate(45deg) scale(1) !important;
  }

  .btn-icon {
    min-width: 44px !important;
    min-height: 44px !important;
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    touch-action: manipulation;
  }

  /* --------------------------------------------
   * 9. Full RTL Support
   * -------------------------------------------- */
  [dir="rtl"] .mobile-tab-bar {
    direction: rtl;
  }

  [dir="rtl"] .mobile-fab {
    right: auto;
    left: 16px;
  }

  /* Stack AI notes FAB cleanly above the main mobile FAB */
  .ask-notes-fab {
    bottom: calc(56px + env(safe-area-inset-bottom, 0px) + 80px) !important;
    right: 18px !important;
    width: 48px !important;
    height: 48px !important;
    z-index: 91 !important;
    background: var(--bg-secondary) !important;
    color: var(--accent-primary) !important;
    border: 1px solid var(--border-strong) !important;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2) !important;
    transition: transform 0.2s ease, opacity 0.2s ease !important;
  }

  .ask-notes-fab svg,
  .ask-notes-fab i {
    width: 20px !important;
    height: 20px !important;
  }

  [dir="rtl"] .ask-notes-fab {
    right: auto !important;
    left: 18px !important;
  }

  .ask-notes-fab.hidden {
    opacity: 0 !important;
    pointer-events: none !important;
    transform: scale(0.7) !important;
  }

  [dir="rtl"] .mobile-group-header.collapsed i,
  [dir="rtl"] .mobile-group-header.collapsed svg {
    transform: rotate(90deg);
  }
}
