/* ============================================
   RESPONSIVE DESIGN & MOBILE OPTIMIZATIONS
   Enhanced UX for all screen sizes
   ============================================ */

/* Bottom nav hidden on desktop */
.mobile-bottom-nav {
  display: none;
}

/* Large Desktop: > 1400px */
@media (min-width: 1400px) {
  .main-content {
    padding-left: 60px;
    padding-right: 60px;
  }
}

/* Tablet & Medium Desktop: 768px - 1024px */
@media (max-width: 1024px) {
  .metrics {
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 16px;
  }

  .split-summary {
    grid-template-columns: 1fr;
  }

  .accounting-section {
    margin: 0 -12px;
  }
}

/* Mobile: < 768px */
@media (max-width: 768px) {
  html {
    font-size: 15px;
  }

  body {
    padding-bottom: 76px;
    padding-bottom: calc(76px + env(safe-area-inset-bottom, 0px));
  }

  .theme-toggle {
    height: 36px;
    min-width: 36px;
    padding: 6px 8px;
  }

  .theme-toggle svg {
    width: 18px;
    height: 18px;
  }

  /* ---- BOTTOM NAVIGATION BAR ---- */
  .mobile-bottom-nav {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: var(--z-modal);
    background: var(--color-neutral-white);
    border-top: 1px solid var(--color-grey-900);
    padding: 6px 4px;
    padding-bottom: calc(6px + env(safe-area-inset-bottom, 0px));
    justify-content: space-around;
    align-items: stretch;
    box-shadow: var(--shadow-md);
    gap: 2px;
  }

  .mobile-nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    padding: 6px 4px 4px;
    border-radius: var(--radius-md);
    text-decoration: none;
    color: var(--color-text-700);
    font-size: 10px;
    font-weight: 500;
    flex: 1;
    min-width: 0;
    transition: color 0.2s ease, background 0.2s ease;
    -webkit-tap-highlight-color: transparent;
  }

  .mobile-nav-item img {
    width: 22px;
    height: 22px;
    opacity: 0.6;
    transition: opacity 0.2s ease;
  }

  .mobile-nav-item.active {
    color: var(--color-text-900);
    font-weight: 600;
  }

  .mobile-nav-item.active img {
    opacity: 1;
  }

  .mobile-nav-label {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
  }

  .main-content {
    padding: 16px 12px 40px;
  }

  .page-header {
    margin-bottom: 20px;
    padding-bottom: 16px;
  }

  .page-title {
    font-size: 22px;
  }

  .page-description {
    font-size: 13px;
  }

  .toggle-container {
    width: 100%;
    overflow-x: auto;
    padding: 4px;
    margin-bottom: 20px;
    scrollbar-width: thin;
  }

  .toggle-container::-webkit-scrollbar {
    height: 4px;
  }

  .metrics {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .metric-card {
    padding: 16px;
  }

  .metric-value {
    font-size: 28px;
  }

  .card,
  .summary-card {
    padding: 16px;
    border-radius: var(--radius-md);
    margin-bottom: 16px;
  }

  .card-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }

  .table-container,
  .table-wrapper,
  .ledger-table-wrapper,
  .trial-balance-table-wrapper {
    border-radius: var(--radius-sm);
    margin-left: -12px;
    margin-right: -12px;
    border-left: none;
    border-right: none;
  }

  .transaction-table th,
  .ledger-table th,
  .trial-balance-table th,
  .statement-table th {
    padding: 12px 10px;
    font-size: 10px;
    letter-spacing: 0.05em;
    top: 0; /* Reset sticky to container-relative on mobile */
  }

  .transaction-table td,
  .ledger-table td,
  .trial-balance-table td,
  .statement-table td {
    padding: 12px 10px;
    font-size: 13px;
  }

  .controls {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
  }

  .search-bar {
    max-width: 100%;
  }

  /* Stack buttons vertically on mobile */
  .button-group {
    flex-direction: column;
    width: 100%;
    gap: 8px;
  }

  .button-group .btn-md,
  .button-group .btn-sm {
    width: 100%;
    justify-content: center;
  }

  .ledger-header,
  .accounting-card > div[style*="display: flex"] {
    flex-direction: column;
    align-items: flex-start !important;
    gap: 12px;
  }

  .ledger-header > div:last-child {
    width: 100%;
  }

  .ledger-header button {
    width: 100%;
  }

  .accounting-tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 8px;
    scrollbar-width: thin;
  }

  .accounting-tab-btn {
    flex-shrink: 0;
  }
}

/* Small Mobile: < 480px */
@media (max-width: 480px) {
  .mobile-nav-item {
    font-size: 9px;
    padding: 5px 2px 3px;
  }

  .mobile-nav-item img {
    width: 20px;
    height: 20px;
  }

  .main-content {
    padding: 12px 8px 30px;
  }

  .page-title {
    font-size: 20px;
  }

  .metric-card {
    padding: 14px;
  }

  .metric-label {
    font-size: 10px;
    margin-bottom: 8px;
  }

  .metric-value {
    font-size: 24px;
  }

  .card,
  .summary-card,
  .accounting-card {
    padding: 14px;
  }

  .card-title {
    font-size: 16px;
  }

  .btn-md {
    font-size: 13px;
    height: 34px;
    padding: 0 12px;
  }

  .btn-sm {
    font-size: 12px;
    height: 30px;
    padding: 0 10px;
  }

  /* Fullscreen modals on small phones */
  .modal-content,
  .modal-overlay .modal-content {
    width: 100%;
    max-width: 100%;
    max-height: 100vh;
    max-height: 100dvh;
    margin: 0;
    border-radius: 0;
    animation: slideUpFull 0.3s ease;
  }

  .modal-overlay {
    align-items: flex-end;
  }

  @keyframes slideUpFull {
    from {
      transform: translateY(100%);
      opacity: 1;
    }
    to {
      transform: translateY(0);
      opacity: 1;
    }
  }

  .btn-close-modal {
    width: 40px;
    height: 40px;
    font-size: 24px;
  }
}

/* Form inputs — prevent iOS zoom (requires 16px+ font-size) */
@media (max-width: 768px) {
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="number"],
  input[type="search"],
  input[type="tel"],
  input[type="url"],
  input[type="date"],
  textarea,
  select {
    font-size: 16px !important;
    min-height: 44px;
    border-radius: var(--radius-sm);
  }

  .form-group label {
    font-size: 13px;
    margin-bottom: 6px;
  }
}

/* Scrollable table fade hints */
@media (max-width: 768px) {
  .table-container,
  .table-wrapper,
  .ledger-table-wrapper,
  .trial-balance-table-wrapper {
    position: relative;
  }

  .table-container::after,
  .table-wrapper::after {
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: 24px;
    background: linear-gradient(to right, transparent, var(--color-neutral-white));
    pointer-events: none;
    opacity: 0.8;
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  }
}

/* Touch device optimizations */
@media (hover: none) and (pointer: coarse) {
  /* Larger touch targets */
  .toggle-btn,
  .btn-md,
  .btn-sm,
  .theme-toggle,
  .sidebar-nav-item,
  .mobile-nav-item {
    min-height: 44px;
  }

  /* Disable hover effects that don't work on touch */
  .transaction-table tbody tr:hover {
    transform: none;
  }

  .metric-card:hover {
    transform: none;
  }

  .card:hover {
    transform: none;
  }

  .summary-card:hover {
    transform: none;
  }

  /* Make clickable areas obvious */
  .editable-cell::after {
    content: '✎';
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    opacity: 0.3;
    font-size: 12px;
  }
}

/* Landscape mobile */
@media (max-width: 896px) and (orientation: landscape) {
  .main-content {
    padding-top: 12px;
  }

  .metrics {
    grid-template-columns: repeat(2, 1fr);
  }

  .mobile-bottom-nav {
    padding: 4px 8px;
    padding-bottom: calc(4px + env(safe-area-inset-bottom, 0px));
  }

  .mobile-nav-item {
    flex-direction: row;
    gap: 6px;
    font-size: 11px;
  }

  .mobile-nav-item img {
    width: 18px;
    height: 18px;
  }
}

/* Print styles */
@media print {
  .app-sidebar,
  .controls,
  .toggle-container,
  .btn-export,
  .theme-toggle,
  .toggle-btn,
  .edit-btn,
  .btn-lock-toggle,
  .mobile-bottom-nav {
    display: none !important;
  }

  .main-content {
    padding: 0;
    max-width: 100%;
  }

  .table-container,
  .table-wrapper,
  .ledger-table-wrapper {
    border: 1px solid #000;
    box-shadow: none;
  }

  .transaction-table,
  .ledger-table,
  .trial-balance-table {
    page-break-inside: avoid;
  }

  .transaction-table th,
  .ledger-table th,
  .trial-balance-table th {
    background: #f0f0f0 !important;
    color: #000 !important;
  }

  .card,
  .metric-card,
  .summary-card {
    page-break-inside: avoid;
    box-shadow: none;
  }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
  .card,
  .metric-card,
  .summary-card,
  .accounting-card {
    border-width: 2px;
  }

  .transaction-table th,
  .ledger-table th,
  .trial-balance-table th {
    border-bottom-width: 3px;
  }

  .btn-outline,
  .toggle-btn {
    border-width: 2px;
  }
}

/* Dark mode + reduced contrast */
@media (prefers-contrast: less) {
}

/* ============================================
   ML DASHBOARD RESPONSIVE STYLES
   ============================================ */

/* Tablet: 768px - 1024px */
@media (max-width: 1024px) {
  .cfo-dashboard {
    padding: 0 12px;
  }
  
  .executive-summary {
    padding: 24px;
    margin-bottom: 24px;
  }
  
  .summary-header {
    flex-direction: column;
    gap: 16px;
  }
  
  .company-status h1 {
    font-size: 26px;
  }
  
  .summary-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .summary-metric.primary {
    grid-column: span 2;
  }
  
  .kpi-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
  }
  
  .trajectory-bars {
    height: 260px;
  }
  
  .y-axis {
    height: 260px;
  }
  
  .forecast-table {
    overflow-x: auto;
  }
  
  .forecast-table table {
    min-width: 600px;
  }
}

/* Mobile: < 768px */
@media (max-width: 768px) {
  .cfo-dashboard {
    padding: 0 8px;
  }
  
  .executive-summary {
    padding: 20px;
    margin-bottom: 20px;
    border-radius: var(--radius-md);
  }
  
  .company-status h1 {
    font-size: 22px;
  }
  
  .status-badge {
    font-size: 12px;
    padding: 8px 14px;
  }
  
  .summary-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }
  
  .summary-metric.primary {
    grid-column: span 1;
  }
  
  .summary-metric {
    padding: 16px;
  }
  
  .metric-value {
    font-size: 28px;
  }
  
  .summary-metric.primary .metric-value {
    font-size: 36px;
  }
  
  .key-metrics-section h2,
  .critical-alerts-section h2,
  .financial-health-section h2,
  .forecast-overview-section h2,
  .risk-factors-section h2 {
    font-size: 20px;
  }
  
  .kpi-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }
  
  .kpi-card {
    padding: 18px;
  }
  
  .kpi-value {
    font-size: 26px;
  }
  
  .trajectory-chart {
    padding: 16px;
  }
  
  .trajectory-bars {
    height: 220px;
    gap: 6px;
    padding: 24px 12px 12px;
  }
  
  .y-axis {
    height: 220px;
    min-width: 35px;
    padding-top: 24px;
    padding-bottom: 32px;
  }
  
  .chart-legend {
    flex-wrap: wrap;
    gap: 12px;
  }
  
  .forecast-table th,
  .forecast-table td {
    padding: 12px 10px;
    font-size: 13px;
  }
  
  .risks-grid {
    grid-template-columns: 1fr;
  }
}

/* Small Mobile: < 480px */
@media (max-width: 480px) {
  .executive-summary {
    padding: 16px;
    border-radius: var(--radius-sm);
  }
  
  .company-status h1 {
    font-size: 18px;
  }
  
  .status-badge {
    font-size: 11px;
    padding: 6px 12px;
    letter-spacing: 0.5px;
  }
  
  .metric-icon {
    font-size: 28px;
  }
  
  .metric-value {
    font-size: 24px;
  }
  
  .summary-metric.primary .metric-value {
    font-size: 28px;
  }
  
  .metric-label {
    font-size: 11px;
  }
  
  .kpi-value {
    font-size: 22px;
  }
  
  .kpi-title {
    font-size: 11px;
  }
  
  .forecast-table {
    margin: 0 -16px;
    border-radius: 0;
    border-left: none;
    border-right: none;
  }
  
  .forecast-table th,
  .forecast-table td {
    padding: 10px 8px;
    font-size: 12px;
  }
  
  .alert-item {
    padding: 14px;
  }
  
  .alert-header h3 {
    font-size: 14px;
  }
  
  .bar-wrapper {
    min-width: 32px;
  }
  
  .bar-month {
    font-size: 9px;
  }
}

/* ============================================
   INVOICE PAGE RESPONSIVE STYLES
   ============================================ */

/* Tablet: 768px - 1024px */
@media (max-width: 1024px) {
  .invoice-upload-wrapper {
    padding: 16px;
  }

  .invoice-list-table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
}

/* Mobile: < 768px */
@media (max-width: 768px) {
  .invoice-upload-wrapper {
    padding: 0;
  }

  .invoice-upload-header h3 {
    font-size: 15px;
  }

  .invoice-upload-header p {
    font-size: 12px;
  }

  .invoice-dropzone {
    padding: 24px 16px;
    border-radius: var(--radius-md);
  }

  .invoice-dropzone svg {
    width: 32px;
    height: 32px;
  }

  .invoice-dropzone p {
    font-size: 13px;
  }

  /* Compact search/filter bar on mobile */
  .invoice-list-section > div:first-child {
    flex-direction: column;
    align-items: flex-start !important;
    gap: 10px;
  }

  .invoice-list-section > div:first-child > div:last-child {
    width: 100%;
    flex-direction: row;
    flex-wrap: wrap;
  }

  .invoice-list-section input[type="text"] {
    flex: 1;
    min-width: 120px;
    width: auto !important;
    max-width: none !important;
  }

  .invoice-list-section select {
    flex-shrink: 0;
    width: auto;
  }

  .invoice-list-section .btn-sm.btn-primary,
  #invoice-upload-container-match-all-btn {
    flex: 1;
    min-width: 0;
    justify-content: center;
  }

  /* Modal as bottom sheet on mobile */
  .invoice-modal-overlay {
    align-items: flex-end;
  }

  .invoice-modal-panel {
    width: 100%;
    max-width: 100%;
    max-height: 92vh;
    max-height: 92dvh;
    margin: 0;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    animation: invoiceSlideUp 0.25s ease-out;
  }

  @keyframes invoiceSlideUp {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
  }

  .invoice-modal-overlay.active .invoice-modal-panel {
    transform: translateY(0) scale(1);
  }

  .invoice-modal-header {
    padding: 20px 16px 12px;
    position: relative;
  }

  /* Drag handle indicator */
  .invoice-modal-header::before {
    content: '';
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    width: 36px;
    height: 4px;
    border-radius: 2px;
    background: var(--color-neutral-300);
  }

  .invoice-modal-body {
    padding: 14px 16px;
    -webkit-overflow-scrolling: touch;
  }

  .invoice-modal-top-row {
    margin-bottom: 12px;
    padding-bottom: 12px;
  }

  .invoice-modal-grid {
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .invoice-modal-grid .invoice-field {
    font-size: 12px;
  }

  .invoice-suggestion-row {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }

  .invoice-suggestion-row > div:last-child {
    width: 100%;
    justify-content: space-between;
  }

  .invoice-suggestion-match-btn {
    min-height: 36px;
    padding: 6px 14px !important;
  }

  .invoice-items-table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .invoice-modal-preview-img {
    max-height: 300px;
  }

  .invoice-modal-preview-pdf {
    height: 300px;
  }

  /* Match badge — full width on mobile */
  .invoice-match-badge {
    font-size: 12px;
    padding: 10px 12px;
  }

  /* Result card compact */
  .invoice-result-card {
    padding: 14px;
    border-radius: var(--radius-md);
  }

  .invoice-modal-unmatch-btn,
  .invoice-modal-find-match-btn {
    width: 100%;
    min-height: 40px;
    justify-content: center;
  }
}

/* Small Mobile: < 480px */
@media (max-width: 480px) {
  .invoice-dropzone {
    padding: 20px 12px;
  }

  .invoice-dropzone svg {
    width: 28px;
    height: 28px;
  }

  .invoice-parsed-grid {
    grid-template-columns: 1fr;
  }

  .invoice-parsed-grid .invoice-field[style*="grid-column: span 2"] {
    grid-column: span 1;
  }

  .invoice-modal-grid {
    grid-template-columns: 1fr;
  }

  .invoice-modal-inv-number {
    font-size: 16px;
  }

  .invoice-modal-amount {
    font-size: 18px;
  }

  .invoice-modal-header h3 {
    font-size: 14px;
  }

  .invoice-modal-header {
    padding-top: 18px;
  }
}
