/**
 * Mobile Navigation & UX Polish — Family Finance Tracker
 * Bottom Nav, AI Chat Toggle, Tab Transitions, Touch Feedback,
 * Table Scroll Indicators, Skeleton Loading, Pull-to-Refresh Spinner
 */

/* ============================================================
   MOBILE BOTTOM NAVIGATION BAR
   ============================================================ */

.mobile-bottom-nav {
  display: none; /* Hidden by default; shown on mobile via media query below */
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 1100;
  background: var(--color-surface);
  border-top: 1px solid var(--color-border);
  box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.08);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  height: calc(64px + env(safe-area-inset-bottom, 0px));
}

@media (max-width: 768px) {
  .mobile-bottom-nav {
    display: flex;
    align-items: stretch;
  }
}

@media (min-width: 769px) {
  .mobile-bottom-nav {
    display: none;
  }
}

.mobile-bottom-nav__items {
  display: flex;
  width: 100%;
  height: 64px;
  align-items: stretch;
}

.mobile-bottom-nav__item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  border: none;
  background: transparent;
  color: var(--color-text-secondary);
  cursor: pointer;
  padding: 8px 4px;
  transition: color 200ms ease, transform 150ms ease;
  font-size: 0;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  position: relative;
}

.mobile-bottom-nav__item:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: -2px;
  border-radius: 8px;
}

.mobile-bottom-nav__item--active {
  color: var(--color-primary);
}

.mobile-bottom-nav__item--active .mobile-bottom-nav__icon {
  background: rgba(var(--color-teal-500-rgb, 33, 128, 141), 0.12);
}

.mobile-bottom-nav__icon {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 200ms ease, transform 150ms ease;
}

.mobile-bottom-nav__icon i {
  font-size: 18px;
}

.mobile-bottom-nav__label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.02em;
  line-height: 1;
  text-transform: uppercase;
}

/* Touch press feedback */
.mobile-bottom-nav__item:active .mobile-bottom-nav__icon {
  transform: scale(0.88);
}

/* Active indicator dot */
.mobile-bottom-nav__item--active::after {
  content: '';
  position: absolute;
  top: 6px;
  left: 50%;
  transform: translateX(-50%);
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--color-primary);
}

/* Notification badge on AI Chat item */
.mobile-bottom-nav__badge {
  position: absolute;
  top: 6px;
  right: calc(50% - 18px);
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--color-error);
  border: 2px solid var(--color-surface);
  display: none;
}

.mobile-bottom-nav__badge--visible {
  display: block;
}

/* ============================================================
   MAIN CONTENT & AI CHAT SLIDE ANIMATION
   ============================================================ */

/* On mobile, main content area is a sliding panel */
@media (max-width: 768px) {
  /* Add bottom padding for the nav bar */
  .main {
    padding-bottom: calc(80px + env(safe-area-inset-bottom, 0px));
    transition: transform 300ms ease, opacity 300ms ease;
    will-change: transform;
  }

  /* When AI chat is open, slide content left */
  body.ai-chat-open .main {
    transform: translateX(-100%);
    opacity: 0;
    pointer-events: none;
    position: fixed;
    width: 100%;
    /* Cancel the desktop margin-right shift — we use transform instead */
    margin-right: 0 !important;
  }

  /* Header also slides */
  body.ai-chat-open .header {
    transform: translateX(-100%);
    opacity: 0;
    transition: transform 300ms ease, opacity 300ms ease;
    margin-right: 0 !important;
  }

  /* Nav bar also slides */
  body.ai-chat-open .nav {
    transform: translateX(-100%);
    opacity: 0;
    transition: transform 300ms ease, opacity 300ms ease;
    margin-right: 0 !important;
  }

  /* AI chat sidebar becomes full-screen on mobile */
  .ai-chat-sidebar {
    transition: transform 300ms ease !important;
    transform: translateX(100%);
    /* Override desktop styles */
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    width: 100% !important;
    max-width: 100% !important;
    height: 100% !important;
    z-index: 1050 !important;
    padding-bottom: calc(64px + env(safe-area-inset-bottom, 0px)) !important;
  }

  /* When chat is open */
  body.ai-chat-open .ai-chat-sidebar {
    transform: translateX(0) !important;
  }

  /* Hide overlay on mobile (we use full-screen instead) */
  .ai-chat-overlay {
    display: none !important;
  }

  /* Hide desktop AI chat header button on mobile */
  #aiChatToggleBtn {
    display: none !important;
  }

  /* Keep the FAB repositioned above the bottom nav */
  .mobile-fab {
    bottom: calc(80px + env(safe-area-inset-bottom, 0px));
  }

  /* Toast container above bottom nav */
  .mobile-toast-container {
    bottom: calc(80px + env(safe-area-inset-bottom, 0px));
  }

  /* Help button positioning: left side above bottom nav on mobile */
  .tour-help-btn {
    bottom: calc(72px + env(safe-area-inset-bottom, 0px));
    left: calc(var(--space-16, 16px) + env(safe-area-inset-left, 0px));
  }

  /* Move the bottom sheet above the nav */
  .mobile-bottom-sheet {
    bottom: calc(64px + env(safe-area-inset-bottom, 0px));
  }

  /* Modals: raise above bottom nav (nav is z:1100) so they fully cover it */
  .modal {
    z-index: 1200;
    /* Pad the bottom so modal content isn't hidden behind the nav on short viewports */
    padding-bottom: calc(80px + env(safe-area-inset-bottom, 0px));
  }

  /* Dynamic modals injected via JS also need the raised z-index */
  #dynamicModalContainer .modal {
    z-index: 1200;
  }
}

/* ============================================================
   SETTINGS DRAWER (opens from bottom nav ⚙️)
   ============================================================ */

.mobile-settings-drawer {
  position: fixed;
  bottom: calc(64px + env(safe-area-inset-bottom, 0px));
  left: 0;
  right: 0;
  background: var(--color-surface);
  border-radius: 16px 16px 0 0;
  border: 1px solid var(--color-border);
  border-bottom: none;
  box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.12);
  z-index: 1090;
  transform: translateY(100%);
  transition: transform 300ms cubic-bezier(0.16, 1, 0.3, 1);
  max-height: 70vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.mobile-settings-drawer--open {
  transform: translateY(0);
}

.mobile-settings-drawer__handle {
  width: 36px;
  height: 4px;
  background: var(--color-border);
  border-radius: 2px;
  margin: 12px auto 0;
}

.mobile-settings-drawer__header {
  padding: 12px 20px 16px;
  border-bottom: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.mobile-settings-drawer__title {
  font-size: 16px;
  font-weight: 600;
  color: var(--color-text);
  margin: 0;
}

.mobile-settings-drawer__close {
  width: 32px;
  height: 32px;
  border: none;
  background: var(--color-secondary);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--color-text-secondary);
  font-size: 14px;
}

.mobile-settings-drawer__body {
  padding: 16px 20px;
}

.mobile-settings-section {
  margin-bottom: 20px;
}

.mobile-settings-section:last-child {
  margin-bottom: 0;
}

.mobile-settings-section__title {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--color-text-secondary);
  margin-bottom: 10px;
}

.mobile-settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid var(--color-border);
}

.mobile-settings-row:last-child {
  border-bottom: none;
}

.mobile-settings-row__label {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  color: var(--color-text);
}

.mobile-settings-row__label i {
  width: 20px;
  text-align: center;
  color: var(--color-text-secondary);
}

.mobile-settings-row__control {
  flex-shrink: 0;
}

/* Quick actions in settings drawer */
.mobile-settings-quick-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.mobile-settings-quick-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 12px 8px;
  border: 1px solid var(--color-border);
  border-radius: 10px;
  background: var(--color-secondary);
  cursor: pointer;
  color: var(--color-text);
  font-size: 12px;
  font-weight: 500;
  text-align: center;
  transition: all 150ms ease;
  -webkit-tap-highlight-color: transparent;
}

.mobile-settings-quick-btn:active {
  transform: scale(0.95);
  background: var(--color-secondary-active);
}

.mobile-settings-quick-btn i {
  font-size: 20px;
  color: var(--color-primary);
}

/* Overlay for settings drawer */
.mobile-drawer-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 1089;
  backdrop-filter: blur(2px);
}

.mobile-drawer-overlay--visible {
  display: block;
}

/* ============================================================
   SMOOTH TAB TRANSITIONS
   ============================================================ */

@media (max-width: 768px) {
  .tab-content {
    /* Enable GPU acceleration */
    will-change: transform, opacity;
    transition: transform 300ms ease, opacity 300ms ease;
  }

  /* Slide-in from right (switching forward) */
  .tab-content--enter-right {
    animation: tabSlideInRight 300ms ease forwards;
  }

  /* Slide-in from left (switching backward) */
  .tab-content--enter-left {
    animation: tabSlideInLeft 300ms ease forwards;
  }

  /* Slide-out to left */
  .tab-content--exit-left {
    animation: tabSlideOutLeft 300ms ease forwards;
  }

  /* Slide-out to right */
  .tab-content--exit-right {
    animation: tabSlideOutRight 300ms ease forwards;
  }
}

@keyframes tabSlideInRight {
  from { transform: translateX(40px); opacity: 0; }
  to   { transform: translateX(0);    opacity: 1; }
}

@keyframes tabSlideInLeft {
  from { transform: translateX(-40px); opacity: 0; }
  to   { transform: translateX(0);     opacity: 1; }
}

@keyframes tabSlideOutLeft {
  from { transform: translateX(0);    opacity: 1; }
  to   { transform: translateX(-40px); opacity: 0; }
}

@keyframes tabSlideOutRight {
  from { transform: translateX(0);   opacity: 1; }
  to   { transform: translateX(40px); opacity: 0; }
}

/* ============================================================
   TOUCH FEEDBACK (haptic-like visual)
   ============================================================ */

/* All tappable elements get the touch feedback mixin */
@media (hover: none) and (pointer: coarse) {
  .btn,
  .nav__tab,
  .asset-tab,
  .mobile-bottom-nav__item {
    transition: transform 100ms ease, opacity 100ms ease, background 200ms ease !important;
  }

  .btn:active,
  .nav__tab:active,
  .asset-tab:active {
    transform: scale(0.95) !important;
    opacity: 0.85;
  }

  /* Restore base hover colors for touch */
  .btn--primary:hover {
    background: var(--color-primary) !important;
  }

  .btn--outline:hover {
    background: transparent !important;
  }
}

/* ============================================================
   PULL-TO-REFRESH — PROPER SPINNER
   ============================================================ */

.mobile-refresh-indicator {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1200;
  background: var(--color-primary);
  color: white;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  font-size: 14px;
  font-weight: 600;
  transform: translateY(-100%);
  transition: transform 200ms ease, opacity 200ms ease;
  opacity: 0;
  box-shadow: 0 4px 16px rgba(33, 128, 141, 0.4);
}

.mobile-refresh-indicator--visible {
  transform: translateY(0);
  opacity: 1;
}

/* Spinner ring (CSS-only) */
.mobile-refresh-indicator .ptr-spinner {
  width: 22px;
  height: 22px;
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-top-color: white;
  border-radius: 50%;
  flex-shrink: 0;
}

.mobile-refresh-indicator.ptr-loading .ptr-spinner {
  animation: ptrSpin 0.7s linear infinite;
}

/* Progress arc (not spinning, just showing pull progress) */
.mobile-refresh-indicator.ptr-pulling .ptr-spinner {
  animation: none;
  border-top-color: white;
}

@keyframes ptrSpin {
  to { transform: rotate(360deg); }
}

/* ============================================================
   TABLE HORIZONTAL SCROLL FADE INDICATOR
   ============================================================ */

@media (max-width: 768px) {
  /* Applied to table-container when table overflows */
  .table-overflow-fade {
    position: relative;
  }

  .table-overflow-fade::after {
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: 32px;
    background: linear-gradient(to right, transparent, var(--color-surface) 90%);
    pointer-events: none;
    z-index: 1;
    border-radius: 0 var(--radius-md, 10px) var(--radius-md, 10px) 0;
    transition: opacity 200ms ease;
  }

  .table-overflow-fade.at-end::after {
    opacity: 0;
  }
}

/* ============================================================
   SKELETON / SHIMMER LOADING STATE
   ============================================================ */

@keyframes skeletonShimmer {
  0%   { background-position: -200% 0; }
  100% { background-position:  200% 0; }
}

.skeleton {
  background: linear-gradient(
    90deg,
    var(--color-secondary) 25%,
    rgba(var(--color-teal-500-rgb, 33, 128, 141), 0.08) 50%,
    var(--color-secondary) 75%
  );
  background-size: 200% 100%;
  animation: skeletonShimmer 1.6s ease-in-out infinite;
  border-radius: var(--radius-base, 8px);
}

.skeleton-card {
  background: var(--color-surface);
  border: 1px solid var(--color-card-border);
  border-radius: var(--radius-lg, 12px);
  padding: 16px;
  margin-bottom: 12px;
}

.skeleton-line {
  height: 14px;
  margin-bottom: 8px;
  border-radius: 4px;
}

.skeleton-line--title {
  height: 18px;
  width: 60%;
  margin-bottom: 10px;
}

.skeleton-line--short {
  width: 40%;
}

.skeleton-line--medium {
  width: 70%;
}

.skeleton-line--full {
  width: 100%;
}

/* Mobile cards loading skeleton */
.mobile-table-cards--loading .skeleton-card:nth-child(1) { opacity: 1; }
.mobile-table-cards--loading .skeleton-card:nth-child(2) { opacity: 0.75; }
.mobile-table-cards--loading .skeleton-card:nth-child(3) { opacity: 0.5; }

/* ============================================================
   SAFE AREA PADDING HELPERS
   ============================================================ */

/* Ensure bottom content is never hidden behind the nav */
@media (max-width: 768px) {
  /* Budget tab or any tab content */
  .tab-content--active {
    min-height: calc(100vh - 180px);
  }
}

/* ============================================================
   DARK MODE ADJUSTMENTS
   ============================================================ */

[data-color-scheme="dark"] .mobile-bottom-nav {
  background: var(--color-charcoal-800, #262828);
  border-top-color: rgba(119, 124, 124, 0.2);
  box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.3);
}

[data-color-scheme="dark"] .mobile-settings-drawer {
  background: var(--color-charcoal-800, #262828);
  box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.4);
}

/* ============================================================
   REDUCED MOTION
   ============================================================ */

@media (prefers-reduced-motion: reduce) {
  .tab-content,
  .tab-content--enter-right,
  .tab-content--enter-left,
  .tab-content--exit-left,
  .tab-content--exit-right,
  .mobile-settings-drawer,
  .ai-chat-sidebar,
  .main,
  .header,
  .nav {
    transition: none !important;
    animation: none !important;
  }
}

/* ============================================================
   DESKTOP: make sure nothing from above leaks in
   ============================================================ */

@media (min-width: 769px) {
  .mobile-bottom-nav,
  .mobile-settings-drawer,
  .mobile-drawer-overlay {
    display: none !important;
  }

  /* Restore the AI chat sidebar desktop styles */
  .ai-chat-sidebar {
    transform: none !important;
    position: fixed !important;
    top: 0 !important;
    left: auto !important;
    right: 0 !important;
    bottom: 0 !important;
    width: 400px !important;
    height: 100vh !important;
    padding-bottom: 0 !important;
  }

  .main {
    padding-bottom: 0;
    transform: none !important;
    opacity: 1 !important;
    position: static !important;
    width: auto !important;
  }

  .header,
  .nav {
    transform: none !important;
    opacity: 1 !important;
  }

  /* Restore modal defaults on desktop */
  .modal {
    z-index: 1000;
    padding-bottom: var(--space-16, 16px);
  }
}
