/**
 * Responsive Design & Mobile Optimization
 * Ensures the app works beautifully on all screen sizes
 */

/* ============================================================================
   MOBILE BREAKPOINTS
   ============================================================================ */

/* Small phones (320px - 480px) */
@media (max-width: 480px) {
  :root {
    --header-height: 60px;
    --radius-md: 8px;
    --radius-lg: 12px;
  }

  body {
    font-size: 14px;
  }

  header {
    flex-direction: row;
    flex-wrap: wrap;
    padding: 12px;
    gap: 8px;
    align-items: center;
    justify-content: flex-start;
  }

  .brand {
    flex-shrink: 0;
    min-width: auto;
  }

  .brand-name {
    display: inline;
    font-size: 12px;
  }

  #main-nav {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    gap: 4px;
    display: flex !important;
    flex-wrap: nowrap;
    order: 3;
  }

  .nav-tab {
    padding: 6px 10px;
    font-size: 11px;
    white-space: nowrap;
    flex-shrink: 0;
    display: inline-block;
  }

  #app-content {
    padding: 12px;
    margin-top: 0;
  }

  #header-actions {
    display: none !important;
  }

  .card {
    padding: 16px;
  }

  .modal {
    width: 95vw !important;
    max-height: 90vh !important;
    border-radius: 12px !important;
  }

  .modal-body {
    max-height: calc(90vh - 120px);
    overflow-y: auto;
  }

  .form-group {
    margin-bottom: 12px;
  }

  .form-group label {
    font-size: 12px;
  }

  .form-group input,
  .form-group select,
  .form-group textarea {
    font-size: 16px; /* Prevents zoom on iOS */
  }

  .btn {
    padding: 10px 12px;
    font-size: 12px;
    min-height: 44px; /* Touch target */
  }

  .btn-sm {
    padding: 8px 10px;
    min-height: 40px;
  }

  /* Tracker module */
  #tracker-shell {
    grid-template-columns: 1fr !important;
    height: auto;
  }

  #tracker-sidebar {
    display: block;
    width: 100%;
    margin-bottom: 16px;
    max-height: 200px;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 12px;
  }

  #tracker-main {
    width: 100%;
  }

  /* Dashboard grid */
  [style*="grid-template-columns: repeat(auto-fit"] {
    grid-template-columns: 1fr !important;
  }

  [style*="grid-template-columns: 2fr 1fr"] {
    grid-template-columns: 1fr !important;
  }

  /* Map container */
  #map-container {
    flex-direction: column;
  }

  #map-canvas {
    height: 300px !important;
  }

  .map-sidebar {
    width: 100% !important;
    max-height: 200px;
    overflow-y: auto;
  }

  /* Budget module */
  .budget-grid {
    font-size: 12px;
  }

  .budget-grid th,
  .budget-grid td {
    padding: 8px 4px;
  }

  /* Badge sizing */
  .badge {
    padding: 4px 8px;
    font-size: 10px;
  }

  /* Spacing adjustments */
  h1 { font-size: 24px; }
  h2 { font-size: 20px; }
  h3 { font-size: 16px; }

  /* Modal positioning */
  .modal {
    top: 50% !important;
    left: 50% !important;
    transform: translate(-50%, -50%) !important;
  }
}

/* Tablets (480px - 768px) */
@media (max-width: 768px) and (min-width: 481px) {
  #main-nav {
    flex-wrap: wrap;
    gap: 8px;
  }

  .nav-tab {
    padding: 8px 12px;
    font-size: 13px;
  }

  #app-content {
    padding: 16px;
  }

  .card {
    padding: 20px;
  }

  /* Tracker module */
  #tracker-shell {
    grid-template-columns: 300px 1fr !important;
  }

  /* Dashboard grid */
  [style*="grid-template-columns: 2fr 1fr"] {
    grid-template-columns: 1fr !important;
  }

  /* Budget grid */
  .budget-grid {
    font-size: 13px;
  }

  .budget-grid th,
  .budget-grid td {
    padding: 10px 8px;
  }
}

/* Large tablets & small desktops (768px - 1024px) */
@media (max-width: 1024px) and (min-width: 769px) {
  #tracker-shell {
    grid-template-columns: 350px 1fr !important;
  }

  [style*="grid-template-columns: 2fr 1fr"] {
    grid-template-columns: 1.5fr 1fr !important;
  }
}

/* ============================================================================
   TOUCH DEVICE OPTIMIZATIONS
   ============================================================================ */

@media (hover: none) and (pointer: coarse) {
  /* Touch-friendly button sizes */
  .btn {
    min-height: 44px;
    min-width: 44px;
  }

  /* Increase tap target sizes */
  button,
  a,
  input[type="checkbox"],
  input[type="radio"] {
    min-height: 44px;
    min-width: 44px;
  }

  /* Remove hover effects on touch */
  .btn:hover {
    background-color: inherit;
    border-color: inherit;
  }

  /* Add active state instead */
  .btn:active {
    opacity: 0.8;
    transform: scale(0.98);
  }

  /* Improve form input spacing */
  .form-group input,
  .form-group select,
  .form-group textarea {
    padding: 12px;
    font-size: 16px; /* Prevents zoom */
  }

  /* Better list item touch targets */
  .household-row {
    min-height: 60px;
    padding: 12px 16px !important;
  }

  /* Improve modal usability */
  .modal-footer {
    gap: 12px;
  }

  .modal-footer .btn {
    flex: 1;
  }
}

/* ============================================================================
   LANDSCAPE ORIENTATION
   ============================================================================ */

@media (orientation: landscape) and (max-height: 500px) {
  header {
    padding: 8px 12px;
  }

  #app-content {
    padding: 8px;
  }

  .card {
    padding: 12px;
  }

  h1 { font-size: 20px; }
  h2 { font-size: 18px; }
  h3 { font-size: 14px; }

  .modal {
    max-height: 80vh !important;
  }
}

/* ============================================================================
   HIGH DPI SCREENS (RETINA)
   ============================================================================ */

@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
  /* Thinner borders on high DPI */
  .card,
  .modal,
  input,
  select,
  textarea {
    border-width: 0.5px;
  }

  /* Smoother shadows */
  .card {
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  }

  .modal {
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
  }
}

/* ============================================================================
   PRINT STYLES
   ============================================================================ */

@media print {
  header,
  #main-nav,
  .btn,
  .modal,
  .modal-overlay {
    display: none !important;
  }

  body {
    background: white;
    color: black;
  }

  .card {
    page-break-inside: avoid;
    border: 1px solid #ccc;
    box-shadow: none;
  }

  a {
    text-decoration: underline;
  }
}

/* ============================================================================
   ACCESSIBILITY
   ============================================================================ */

/* Reduced motion for users who prefer it */
@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* Dark mode support */
@media (prefers-color-scheme: dark) {
  :root {
    --bg-app: #0f172a;
    --bg-main: #1e293b;
    --text-main: #f1f5f9;
    --text-muted: #cbd5e1;
    --border-color: #334155;
  }
}

/* ============================================================================
   UTILITY CLASSES FOR RESPONSIVE DESIGN
   ============================================================================ */

.hide-mobile {
  display: none;
}

@media (min-width: 769px) {
  .hide-mobile {
    display: block;
  }

  .show-mobile {
    display: none;
  }
}

.container-responsive {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 16px;
}

@media (max-width: 768px) {
  .container-responsive {
    padding: 0 12px;
  }
}

/* Flexible grid */
.grid-responsive {
  display: grid;
  gap: 16px;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
}

@media (max-width: 768px) {
  .grid-responsive {
    grid-template-columns: 1fr;
    gap: 12px;
  }
}

/* Flexible flex */
.flex-responsive {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}

@media (max-width: 768px) {
  .flex-responsive {
    flex-direction: column;
    gap: 12px;
  }
}
