/**
 * Mobile & PWA Polish Styles
 * Comprehensive mobile responsiveness and PWA experience improvements
 * 
 * Includes:
 * - Safe area handling for notched devices
 * - Touch-friendly targets (min 44px)
 * - Improved card/view responsiveness
 * - Better horizontal overflow prevention
 * - Touch feedback states
 * - PWA install banner styling
 */

/* ========================================
   SAFE AREAS (Notched Devices)
   ======================================== */

/* Apply safe area padding to main layout */
@supports (padding: env(safe-area-inset-top)) {
  .sidebar {
    padding-left: env(safe-area-inset-left);
  }
  
  /* Sidebar header needs safe area for notch/Dynamic Island */
  .sidebar-header {
    /* 1.25rem is default padding, safe-area adds to it for notched devices */
    padding-top: calc(1.25rem + env(safe-area-inset-top));
  }
  
  .main-content {
    padding-right: env(safe-area-inset-right);
  }
  
  .content-header {
    padding-top: calc(0.75rem + env(safe-area-inset-top));
  }
  
  /* Modal safe areas */
  .dv-modal {
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  }
  
  /* Bottom navigation/footer safe areas */
  .dv-pagination,
  .dv-bulk-actions {
    padding-bottom: calc(0.75rem + env(safe-area-inset-bottom));
  }
  
  /* Sidebar footer safe area */
  .sidebar-footer {
    padding-bottom: calc(1rem + env(safe-area-inset-bottom));
  }
}

/* PWA Standalone Mode - Extra safe area handling for iOS */
/* Some iOS devices in standalone mode report safe-area-inset-top as 0 */
/* despite having a notch or Dynamic Island that covers the status bar area */
@media all and (display-mode: standalone) {
  .sidebar-header {
    /* Add minimum 20px extra padding for PWA status bar area */
    /* This ensures the logo is always tappable even if safe-area reports 0 */
    padding-top: calc(1.25rem + max(0px, env(safe-area-inset-top, 20px)));
  }
  
  .content-header {
    padding-top: calc(0.75rem + max(0px, env(safe-area-inset-top, 20px)));
  }
}

/* iOS-specific PWA fallback for devices where safe-area-inset still reports 0 */
/* This catches edge cases on certain iOS versions/devices */
@supports (-webkit-touch-callout: none) {
  @media all and (display-mode: standalone) {
    .sidebar-header {
      /* Minimum 56px total top padding to clear Dynamic Island/notch */
      /* 1.25rem (20px) + 36px safety = 56px minimum */
      padding-top: max(calc(1.25rem + env(safe-area-inset-top, 0px)), 56px);
    }
    
    .content-header {
      padding-top: max(calc(0.75rem + env(safe-area-inset-top, 0px)), 48px);
    }
    
    .logo-link {
      /* Ensure the link itself has enough tap target */
      display: block;
      min-height: 44px;
    }
  }
}

/* ========================================
   GLOBAL MOBILE IMPROVEMENTS
   ======================================== */

@media (max-width: 768px) {
  /* Prevent any horizontal overflow */
  body {
    overflow-x: hidden;
    width: 100%;
    position: relative;
  }
  
  .main-content,
  .content-body,
  .dv-wrapper,
  .dv-content {
    max-width: 100vw;
    overflow-x: hidden;
  }
  
  /* Larger touch targets for all buttons */
  .dv-btn {
    min-height: 44px;
    min-width: 44px;
    padding: 0.625rem 1rem;
  }
  
  .dv-btn-sm {
    min-height: 36px;
    min-width: 36px;
    padding: 0.5rem 0.75rem;
    font-size: 0.875rem;
  }
  
  .dv-btn-icon {
    min-height: 44px;
    min-width: 44px;
    padding: 0.5rem;
  }
  
  /* Content body less padding on mobile */
  .content-body {
    padding: 0.75rem;
  }
  
  /* Content header more compact */
  .content-header {
    padding: 0.75rem 1rem;
  }
  
  .content-header h2 {
    font-size: 1.25rem;
  }
}

/* ========================================
   CARD VIEW - MOBILE FIXES
   ======================================== */

@media (max-width: 768px) {
  /* Force single column and prevent overflow */
  .dv-card-grid {
    grid-template-columns: 1fr !important;
    gap: 0.75rem;
    padding: 0.75rem;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
  }
  
  /* Cards must respect container bounds */
  .dv-card {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
    overflow: hidden;
  }
  
  /* Card header - allow wrapping */
  .dv-card-header {
    flex-wrap: wrap;
    gap: 0.5rem;
    padding: 0.875rem;
  }
  
  /* Title can wrap on mobile */
  .dv-card-title {
    white-space: normal;
    line-height: 1.3;
    word-break: break-word;
  }
  
  .dv-card-subtitle {
    white-space: normal;
    line-height: 1.3;
  }
  
  /* Status badge in header */
  .dv-card-header .dv-status {
    flex-shrink: 0;
    order: 3;
    margin-top: 0.25rem;
  }
  
  /* Card body compact */
  .dv-card-body {
    padding: 0.75rem 0.875rem;
  }
  
  /* Card fields stack better */
  .dv-card-field {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.125rem;
    padding: 0.5rem 0;
  }
  
  .dv-card-label {
    margin-right: 0;
    font-size: 0.75rem;
  }
  
  .dv-card-value {
    text-align: left;
    font-size: 0.875rem;
  }
  
  /* Card footer - always show actions */
  .dv-card-footer {
    padding: 0.625rem 0.875rem;
  }
  
  .dv-card-actions {
    opacity: 1;
    gap: 0.5rem;
  }
  
  .dv-card-actions .dv-btn {
    min-height: 40px;
    min-width: 40px;
    padding: 0.5rem;
    font-size: 1rem;
  }
  
  /* Card image header */
  .dv-card-image-header {
    height: 160px;
  }
}

/* Small phones */
@media (max-width: 400px) {
  .dv-card-grid {
    padding: 0.5rem;
    gap: 0.5rem;
  }
  
  .dv-card-header {
    padding: 0.75rem;
  }
  
  .dv-card-body {
    padding: 0.625rem 0.75rem;
  }
  
  .dv-card-footer {
    padding: 0.5rem 0.75rem;
  }
  
  .dv-card-image-header {
    height: 140px;
  }
}

/* ========================================
   TOOLBAR - MOBILE LAYOUT
   ======================================== */

@media (max-width: 768px) {
  .dv-toolbar {
    padding: 0.75rem;
    gap: 0.75rem;
  }
  
  .dv-toolbar-left {
    width: 100%;
    flex-wrap: wrap;
    gap: 0.5rem;
  }
  
  .dv-toolbar-right {
    width: 100%;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.5rem;
  }
  
  /* Search takes full width */
  .dv-search-wrapper {
    width: 100%;
    order: -1;
  }
  
  .dv-search-input {
    width: 100% !important;
  }
  
  .dv-search-input:focus {
    width: 100% !important;
  }
  
  /* View switcher and add button stay together */
  .dv-view-switcher {
    flex-shrink: 0;
  }
  
  .dv-add-btn,
  .dv-add-dropdown {
    flex-shrink: 0;
  }
  
  /* Filter toggle */
  .dv-filter-toggle {
    order: 1;
  }
}

/* ========================================
   TABLE VIEW - MOBILE IMPROVEMENTS
   ======================================== */

@media (max-width: 768px) {
  .dv-table-container {
    max-height: calc(100vh - 200px);
    max-height: calc(100dvh - 200px);
  }
  
  /* Smaller table cells */
  .dv-table th,
  .dv-table td {
    padding: 0.375rem 0.5rem;
    font-size: 0.75rem;
  }
  
  /* Always show row actions on mobile */
  .dv-table tbody tr .dv-actions-cell {
    opacity: 1;
  }
  
  /* Larger action buttons */
  .dv-actions-cell .dv-btn {
    min-height: 36px;
    min-width: 36px;
    padding: 0.375rem;
    font-size: 0.875rem;
  }
}

/* ========================================
   LIST VIEW - MOBILE IMPROVEMENTS
   ======================================== */

@media (max-width: 640px) {
  .dv-list-view {
    max-width: 100%;
    padding: 0;
  }
  
  .dv-list-item {
    padding: 0.75rem;
    gap: 0.625rem;
  }
  
  /* Larger checkboxes */
  .dv-list-checkbox {
    width: 28px;
    height: 28px;
    min-width: 28px;
  }
  
  /* Actions always visible */
  .dv-list-item-actions {
    opacity: 1;
  }
  
  .dv-list-action-btn {
    min-height: 40px;
    min-width: 40px;
    padding: 0.5rem;
    font-size: 1rem;
  }
  
  /* Quick add improvements */
  .dv-list-quick-add {
    padding: 0.875rem;
    margin: 0.75rem;
  }
  
  .dv-list-quick-add-input {
    font-size: 1rem;
  }
}

/* ========================================
   KANBAN VIEW - MOBILE IMPROVEMENTS
   ======================================== */

@media (max-width: 768px) {
  .dv-kanban {
    height: calc(100vh - 180px);
    height: calc(100dvh - 180px);
  }
  
  .dv-kanban-board {
    gap: 0.75rem;
    padding: 0.5rem;
    /* Enable snap scrolling for columns */
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
  }
  
  .dv-kanban-column {
    flex: 0 0 85vw;
    min-width: 280px;
    max-width: 85vw;
    scroll-snap-align: center;
  }
  
  /* Always show card actions */
  .dv-kanban-card-actions {
    opacity: 1;
  }
  
  .dv-kanban-card-btn {
    min-height: 36px;
    min-width: 36px;
  }
}

/* ========================================
   GALLERY VIEW - MOBILE IMPROVEMENTS
   ======================================== */

@media (max-width: 480px) {
  .dv-gallery-grid {
    grid-template-columns: 1fr;
    gap: 0.75rem;
    padding: 0.75rem;
  }
  
  .dv-gallery-info {
    padding: 0.875rem;
  }
  
  .dv-gallery-title {
    font-size: 1rem;
  }
}

/* ========================================
   MODAL - MOBILE IMPROVEMENTS
   ======================================== */

@media (max-width: 768px) {
  .dv-modal {
    padding: 0;
    align-items: flex-end;
  }
  
  .dv-modal-content {
    max-width: 100%;
    max-height: 90vh;
    max-height: 90dvh;
    border-radius: 16px 16px 0 0;
    margin: 0;
  }
  
  .dv-modal-header {
    padding: 1rem;
  }
  
  .dv-modal-body {
    padding: 1rem;
  }
  
  .dv-modal-footer {
    padding: 1rem;
    flex-wrap: wrap;
    gap: 0.5rem;
  }
  
  .dv-modal-footer .dv-btn {
    flex: 1;
    min-width: 120px;
  }
  
  /* Hide keyboard shortcut hints on mobile */
  .dv-modal-shortcut-hint {
    display: none;
  }
  
  /* Form groups */
  .dv-form-group {
    margin-bottom: 1rem;
  }
  
  .dv-input,
  .dv-textarea {
    font-size: 16px; /* Prevents iOS zoom on focus */
    padding: 0.75rem;
    min-height: 44px;
  }
  
  /* Date quick buttons wrap */
  .dv-date-quick-btns {
    flex-wrap: wrap;
  }
  
  .dv-date-quick {
    padding: 0.375rem 0.75rem;
  }
}

/* ========================================
   DASHBOARD - MOBILE IMPROVEMENTS
   ======================================== */

@media (max-width: 768px) {
  .dashboard-grid {
    grid-template-columns: 1fr;
    gap: 1rem;
  }
  
  .dashboard-section {
    padding: 1rem;
    border-radius: 10px;
  }
  
  .dashboard-section h3 {
    font-size: 0.9375rem;
  }
  
  .dashboard-item {
    padding: 0.5rem 0;
  }
}

/* ========================================
   TODAY VIEW - MOBILE IMPROVEMENTS
   ======================================== */

@media (max-width: 600px) {
  .today-container {
    padding: 0.75rem;
  }
  
  .today-header {
    padding-bottom: 0.75rem;
    margin-bottom: 1rem;
  }
  
  .today-date {
    font-size: 1.125rem;
  }
  
  .today-section {
    padding: 0.875rem;
    border-radius: 10px;
  }
  
  .today-task-check {
    width: 28px;
    height: 28px;
  }
  
  .today-habit-btn {
    padding: 0.625rem 0.875rem;
    font-size: 0.875rem;
  }
}

/* ========================================
   FILTERS - MOBILE IMPROVEMENTS
   ======================================== */

@media (max-width: 768px) {
  .dv-filters {
    padding: 0.75rem;
  }
  
  .dv-filters-grid {
    grid-template-columns: 1fr;
    gap: 0.75rem;
  }
  
  .dv-filter-group select,
  .dv-filter-group input {
    min-height: 44px;
    font-size: 16px;
  }
}

/* ========================================
   PAGINATION - MOBILE IMPROVEMENTS
   ======================================== */

@media (max-width: 768px) {
  .dv-pagination {
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.75rem;
    padding: 0.75rem;
  }
  
  .dv-pagination-info {
    width: 100%;
    text-align: center;
    order: -1;
  }
  
  .dv-pagination-controls {
    gap: 0.25rem;
  }
  
  .dv-pagination-btn {
    min-height: 40px;
    min-width: 40px;
    padding: 0.5rem;
  }
}

/* ========================================
   SIDEBAR - MOBILE IMPROVEMENTS
   ======================================== */

@media (max-width: 1024px) {
  .sidebar.open {
    box-shadow: 5px 0 25px rgba(0, 0, 0, 0.3);
  }
  
  /* Larger tap targets for nav items */
  .nav-item {
    padding: 0.75rem 1.5rem 0.75rem 2.5rem;
    min-height: 44px;
  }
  
  .nav-category-header {
    padding: 0.875rem 1.5rem;
    min-height: 44px;
  }
  
  /* Larger sidebar toggle */
  .sidebar-toggle {
    min-height: 44px;
    min-width: 44px;
    font-size: 1.75rem;
  }
}

/* ========================================
   TOUCH FEEDBACK STATES
   ======================================== */

@media (hover: none) and (pointer: coarse) {
  /* Remove hover effects that don't work well with touch */
  .dv-card:hover {
    transform: none;
  }
  
  .dv-gallery-item:hover {
    transform: none;
  }
  
  /* Add active states instead */
  .dv-btn:active {
    transform: scale(0.96);
    opacity: 0.9;
  }
  
  .dv-card:active {
    transform: scale(0.99);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  }
  
  .dv-list-item:active {
    background: var(--hover-bg, #f3f4f6);
  }
  
  .nav-item:active {
    background: var(--sidebar-active, #667eea);
  }
  
  /* Prevent text selection on tap */
  .dv-btn,
  .nav-item,
  .dv-card-actions,
  .dv-list-action-btn {
    -webkit-tap-highlight-color: transparent;
    user-select: none;
  }
}

/* ========================================
   PWA INSTALL PROMPT STYLING
   ======================================== */

.pwa-install-banner {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--content-bg, white);
  padding: 1rem;
  padding-bottom: calc(1rem + env(safe-area-inset-bottom, 0px));
  box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.15);
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  transform: translateY(100%);
  transition: transform 0.3s ease;
}

.pwa-install-banner.visible {
  transform: translateY(0);
}

.pwa-install-content {
  flex: 1;
  min-width: 0;
}

.pwa-install-title {
  font-weight: 600;
  font-size: 0.9375rem;
  color: var(--content-text, #1f2937);
  margin-bottom: 0.25rem;
}

.pwa-install-desc {
  font-size: 0.8125rem;
  color: var(--content-text-secondary, #6b7280);
}

.pwa-install-actions {
  display: flex;
  gap: 0.5rem;
  flex-shrink: 0;
}

.pwa-install-btn {
  padding: 0.625rem 1rem;
  border-radius: 8px;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  border: none;
}

.pwa-install-btn-primary {
  background: var(--primary-color, #667eea);
  color: white;
}

.pwa-install-btn-primary:hover {
  background: var(--primary-dark, #5a67d8);
}

.pwa-install-btn-dismiss {
  background: transparent;
  color: var(--content-text-secondary, #6b7280);
}

.pwa-install-btn-dismiss:hover {
  background: var(--hover-bg, #f3f4f6);
}

/* ========================================
   OFFLINE INDICATOR
   ======================================== */

.offline-indicator {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: #f59e0b;
  color: white;
  text-align: center;
  padding: 0.5rem;
  padding-top: calc(0.5rem + env(safe-area-inset-top, 0px));
  font-size: 0.8125rem;
  font-weight: 500;
  z-index: 10000;
  transform: translateY(-100%);
  transition: transform 0.3s ease;
}

.offline-indicator.visible {
  transform: translateY(0);
}

/* Adjust main content when offline indicator is visible */
body.offline .content-header {
  margin-top: calc(2rem + env(safe-area-inset-top, 0px));
}

/* ========================================
   DYNAMIC VIEWPORT HEIGHT (iOS Safari)
   ======================================== */

@supports (height: 100dvh) {
  .dv-modal-content {
    max-height: 90dvh;
  }
  
  .dv-kanban {
    height: calc(100dvh - 180px);
  }
  
  .dv-table-container {
    max-height: calc(100dvh - 200px);
  }
}

/* ========================================
   LANDSCAPE MOBILE ADJUSTMENTS
   ======================================== */

@media (max-height: 500px) and (orientation: landscape) {
  .content-header {
    padding: 0.5rem 1rem;
  }
  
  .content-header h2 {
    font-size: 1rem;
  }
  
  .dv-toolbar {
    padding: 0.5rem;
  }
  
  .dv-modal-content {
    max-height: 95vh;
    max-height: 95dvh;
  }
  
  .dv-modal-body {
    padding: 0.75rem;
  }
  
  .dashboard-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

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

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  
  .dv-card:hover,
  .dv-gallery-item:hover {
    transform: none;
  }
}

/* ========================================
   PLANNER VIEW - MOBILE IMPROVEMENTS
   ======================================== */

@media (max-width: 768px) {
  .dv-planner {
    flex-direction: column;
    height: auto;
    min-height: auto;
  }
  
  .dv-planner-calendar {
    min-height: 350px;
    max-height: 50vh;
  }
  
  .dv-planner-panel {
    width: 100% !important;
    max-height: 40vh;
    border-radius: 8px;
  }
  
  .dv-planner-day {
    min-height: 60px;
    padding: 0.125rem;
  }
  
  .dv-planner-event {
    font-size: 0.625rem;
    padding: 2px 4px;
  }
  
  .dv-planner-panel-card {
    padding: 0.625rem;
  }
  
  .dv-planner-card-image {
    width: 40px;
    height: 40px;
  }
  
  /* Meal selector mobile */
  .dv-planner-meal-selector-content {
    width: calc(100vw - 2rem);
    max-width: 320px;
  }
  
  .dv-planner-meal-option {
    padding: 0.875rem 0.5rem;
  }
}

/* ========================================
   TIMELINE VIEW - MOBILE IMPROVEMENTS
   ======================================== */

@media (max-width: 768px) {
  .dv-timeline {
    padding: 0.75rem;
  }
  
  .dv-timeline-header {
    flex-direction: column;
    gap: 0.5rem;
  }
  
  .dv-timeline-group-header {
    padding: 0.5rem 0.75rem;
  }
  
  .dv-timeline-item {
    padding: 0.75rem;
    margin-left: 1.5rem;
  }
  
  .dv-timeline-item::before {
    left: -1.75rem;
    width: 10px;
    height: 10px;
  }
  
  .dv-timeline-content {
    gap: 0.25rem;
  }
}

/* ========================================
   MATRIX VIEW - MOBILE IMPROVEMENTS
   ======================================== */

@media (max-width: 768px) {
  .dv-matrix {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  
  .dv-matrix-container {
    min-width: 600px;
  }
  
  .dv-matrix-cell {
    min-height: 60px;
    padding: 0.25rem;
  }
  
  .dv-matrix-chip {
    font-size: 0.625rem;
    padding: 2px 4px;
  }
}

/* ========================================
   SUMMARY VIEW - MOBILE IMPROVEMENTS
   ======================================== */

@media (max-width: 768px) {
  .dv-summary-grid {
    grid-template-columns: 1fr;
    gap: 0.75rem;
  }
  
  .dv-summary-card {
    padding: 0.875rem;
  }
  
  .dv-summary-value {
    font-size: 1.5rem;
  }
}

/* ========================================
   PROGRESS VIEW - MOBILE IMPROVEMENTS
   ======================================== */

@media (max-width: 768px) {
  .dv-progress-grid {
    grid-template-columns: 1fr;
    gap: 0.75rem;
  }
  
  .dv-progress-card {
    padding: 0.875rem;
  }
  
  .dv-progress-actions {
    opacity: 1;
  }
}

/* ========================================
   CHART VIEW - MOBILE IMPROVEMENTS
   ======================================== */

@media (max-width: 768px) {
  .dv-chart-container {
    min-height: 250px;
  }
  
  .dv-chart-controls {
    flex-wrap: wrap;
    gap: 0.5rem;
  }
  
  .dv-chart-type-btn,
  .dv-chart-control-btn {
    min-height: 40px;
    padding: 0.5rem 0.75rem;
  }
}

/* ========================================
   FILES VIEW - MOBILE IMPROVEMENTS
   ======================================== */

@media (max-width: 768px) {
  .dv-files-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 0.75rem;
  }
  
  .dv-file-card {
    padding: 0.625rem;
  }
  
  .dv-files-dropzone {
    padding: 2rem 1rem;
  }
}

@media (max-width: 400px) {
  .dv-files-grid {
    grid-template-columns: 1fr;
  }
}

/* ========================================
   DARK MODE + MOBILE SPECIFIC
   ======================================== */

@media (max-width: 768px) {
  [data-theme="dark"] .dv-modal-content {
    background: var(--content-bg, #1f2937);
  }
  
  [data-theme="dark"] .offline-indicator {
    background: #b45309;
  }
  
  [data-theme="dark"] .pwa-install-banner {
    background: var(--content-bg, #1f2937);
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.4);
  }
}
