﻿/* ========================================
   CRITICAL FIX #1: Table Column Widths (Mobile)
   ======================================== */

@media (max-width: 640px) {

  /* Ensure table columns have min-width for horizontal scroll */
  table th:first-child,
  table td:first-child {
    min-width: 80px;
  }

  table th:nth-child(3),
  table td:nth-child(3) {
    min-width: 110px;
  }

  /* Opex table column widths for horizontal scroll */
  #page-operational table th:nth-child(2),
  #page-operational table td:nth-child(2) {
    min-width: 80px;
  }

  #page-operational table th:nth-child(4),
  #page-operational table td:nth-child(4) {
    min-width: 70px;
  }

  /* Stat cards in Opex - stack on small screens to prevent overflow */
  @media (max-width: 480px) {
    #opex-stats.sg {
      grid-template-columns: 1fr !important;
    }

    #page-operational table th:nth-child(4),
    /* Tangal */
    #page-operational table td:nth-child(4) {
      display: none;
    }
  }

  /* Trip Docs Manifest table column widths */
  #td-manifest-tbl th,
  #td-manifest-tbl td {
    min-width: 80px;
  }

  #td-manifest-tbl th:nth-child(1),
  #td-manifest-tbl td:nth-child(1) {
    min-width: 100px;
  }

  #td-manifest-tbl th:nth-child(4),
  #td-manifest-tbl td:nth-child(4) {
    min-width: 70px;
  }

  /* Crew table column widths */
  #crew-tbl th:nth-child(1),
  #crew-tbl td:nth-child(1) {
    min-width: 120px;
  }

  #crew-tbl th:nth-child(2),
  #crew-tbl td:nth-child(2) {
    min-width: 80px;
  }

  #crew-tbl th:nth-child(5),
  #crew-tbl td:nth-child(5) {
    min-width: 70px;
  }

  /* Report table column widths */
  #rep-tbl th:nth-child(1),
  #rep-tbl td:nth-child(1) {
    min-width: 100px;
  }

  #rep-tbl th:nth-child(3),
  #rep-tbl td:nth-child(3) {
    min-width: 60px;
  }

  #rep-tbl th:nth-child(4),
  #rep-tbl td:nth-child(4) {
    min-width: 90px;
  }

  #rep-tbl th:nth-child(5),
  #rep-tbl td:nth-child(5) {
    min-width: 90px;
  }

  #rep-tbl th:nth-child(7),
  #rep-tbl td:nth-child(7) {
    min-width: 90px;
  }

  /* Calculator RAB table column widths */
  #page-calculator table th:nth-child(3),
  #page-calculator table td:nth-child(3) {
    min-width: 100px;
  }

  #page-calculator table th:nth-child(4),
  #page-calculator table td:nth-child(4) {
    min-width: 80px;
  }

  #page-calculator table th:nth-child(7),
  #page-calculator table td:nth-child(7) {
    min-width: 80px;
  }

  #page-calculator table th:nth-child(8),
  #page-calculator table td:nth-child(8) {
    min-width: 70px;
  }

  /* Tighter spacing on mobile */
  th {
    padding: 10px 8px !important;
    font-size: 11px !important;
  }

  td {
    padding: 8px !important;
    font-size: 11px !important;
  }

  /* Trip Schedule Manifest Optimization */
  .sched-manifest-tbl th,
  .sched-manifest-tbl td {
    padding: 8px 6px !important;
  }

  .sched-manifest-tbl th:first-child,
  .sched-manifest-tbl td:first-child {
    min-width: 36px !important;
    width: 36px !important;
    padding: 8px 2px !important;
    text-align: center !important;
  }

  .sched-manifest-tbl th:nth-child(2),
  .sched-manifest-tbl td:nth-child(2) {
    min-width: 90px !important;
  }

}

/* ========================================
   CRITICAL FIX #2: Chart Container Sizing
   ======================================== */

@media (max-width: 640px) {

  /* Chart wrappers */
  .chart-wrap,
  .opex-chart-wrap,
  [id^="chart-container"],
  [class*="chart-container"] {
    width: 100%;
    max-width: 100%;
    overflow: hidden;
    padding: 0;
    margin: 0;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  /* Canvas elements */
  canvas {
    max-width: 100% !important;
    height: auto !important;
    display: block;
  }

  /* Specific chart sizing */
  #chart-rev,
  #chart-profit {
    max-height: 250px;
  }

  #chart-expense,
  #chart-opex {
    max-height: 220px;
  }

  /* SVG charts */
  svg {
    max-width: 100%;
    height: auto;
    overflow: visible;
  }

  /* Parent section padding adjustment */
  section:has(.chart-wrap),
  .card:has(canvas) {
    padding: 1rem 0.5rem !important;
    overflow: hidden;
  }

  .opex-grid,
  .sg {
    width: 100% !important;
    max-width: 100% !important;
    overflow-x: hidden;
  }

  .opex-grid>*,
  .sg>* {
    min-width: 0 !important;
  }

  .opex-chart-wrap {
    height: 200px !important;
    width: 100% !important;
  }

  /* Pagination Mobile Optimization */
  .pg-wrap {
    justify-content: center !important;
    gap: 4px !important;
    padding: 12px 8px !important;
  }

  .pg-wrap .rpp {
    width: 100% !important;
    justify-content: center !important;
    margin-bottom: 8px !important;
  }

  .pg-wrap .pg-info {
    width: 100% !important;
    text-align: center !important;
    margin-top: 8px !important;
    padding: 0 !important;
  }

  .pg-btn {
    padding: 6px 10px !important;
    font-size: 11px !important;
  }
}

@media (min-width: 641px) {

  canvas,
  svg {
    max-width: none;
  }
}

/* ========================================
   CHART.JS MOBILE RESPONSIVE CONFIG
   ========================================
   Note: Chart.js options need dynamic configuration
   for mobile vs desktop legends and labels.
   This CSS supports the responsive behavior. */

@media (max-width: 640px) {

  /* Chart legend positioning for mobile */
  .chart-wrap {
    flex-direction: column;
  }

  /* Ensure chart doesn't overflow its container */
  canvas[id*="chart"] {
    margin: 0 auto;
  }

  /* ========================================
     ALERT ITEMS MOBILE OPTIMIZATION
     ======================================== */
  .dash-alert-item {
    padding: 12px 14px;
    border-left-width: 3px;
    font-size: 11px;
    line-height: 1.4;
  }

  #dash-alerts {
    gap: 6px !important;
  }
}

@media (max-width: 640px) {
  .sc {
    display: flex !important;
    flex-direction: column !important;
    padding: 20px 16px !important;
    gap: 12px !important;
    text-align: left !important;
  }

  .sc-ico {
    margin: 0 !important;
    width: 13px !important;
    height: 13px !important;
    font-size: 15px !important;
  }

  .sc h3,
  .sc .val,
  .sc .sub {
    grid-column: auto !important;
    text-align: left !important;
    width: auto !important;
  }

  .sc .val {
    font-size: 22px !important;
  }

  .dash-quick-actions {
    display: flex !important;
    overflow-x: auto !important;
    flex-wrap: nowrap !important;
    gap: 10px !important;
    padding: 2px 4px 14px !important;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

  .dash-quick-actions::-webkit-scrollbar {
    display: none;
  }

  .dash-quick-actions .btn {
    flex: 0 0 auto !important;
    flex-direction: row !important;
    text-align: left !important;
    justify-content: flex-start !important;
    align-items: center !important;
    padding: 8px 14px !important;
    min-height: 48px !important;
    min-width: 140px !important;
    font-size: 12px !important;
    gap: 10px !important;
  }

  .dash-quick-actions .btn .ico {
    margin: 0 !important;
    width: 28px !important;
    height: 28px !important;
    font-size: 16px !important;
  }
}

/* ========================================
   CARD VIEW â€” Bookings & Reports tables
   on screens narrower than 768px each
   <tr> is rendered as a stacked card.
   Desktop layout is completely untouched.
   ======================================== */

@media (max-width: 767px) {

  /* â”€â”€ Shared card-view mechanics â”€â”€ */
  #bk-tbl thead,
  #rep-tbl thead {
    display: none;
  }

  /* â”€â”€ Report Table Card View (Classic) â”€â”€ */
  #rep-tbl { display: block; }
  #rep-tbl tbody { display: block; }
  #rep-tbl tr {
    display: block;
    margin-bottom: 12px;
    border-radius: 10px;
    border: 1px solid var(--border-color, #e2e8f0);
    padding: 10px 12px;
    background: var(--card, #fff);
    box-shadow: 0 1px 4px rgba(var(--ws-black-rgb), .06);
  }
  #rep-tbl td {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    padding: 5px 0 !important;
    border: none !important;
    font-size: 12px !important;
    line-height: 1.4;
    gap: 8px;
  }
  #rep-tbl td[data-label]::before {
    content: attr(data-label);
    font-weight: 700;
    color: var(--g3, #64748b);
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    flex-shrink: 0;
    min-width: 72px;
    padding-top: 1px;
  }
  /* Keep totals row in reports readable */
  #rep-tbl tr[style*="font-weight:700"] td {
    font-weight: 700;
  }

  /* â”€â”€ Booking Table Card View (Redesign) â”€â”€ */
  #bk-tbl { display: block; }
  #bk-tbl tbody { display: block; }

  #bk-tbl tr {
    display: flex;
    flex-wrap: wrap;
    position: relative;
    margin-bottom: 12px;
    border-radius: 12px;
    border: 1px solid var(--border);
    padding: 16px;
    background: var(--card);
    box-shadow: var(--shadow-sm);
    transition: all 0.3s ease;
  }

  /* Reset all TDs inside bk-tbl for custom flow */
  #bk-tbl td {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    padding: 0 !important;
    border: none !important;
    font-size: 13px !important;
    line-height: 1.4;
    width: 100%;
    max-width: none !important;
    margin-bottom: 10px;
  }

  #bk-tbl td[data-label]::before {
    content: attr(data-label);
    font-weight: 700;
    color: var(--muted);
    font-size: 9px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 4px;
    min-width: unset;
  }

  /* â”€â”€ VISUAL ORDERING FOR MOBILE CARD â”€â”€ */
  #bk-tbl td:first-child { order: -1; }
  #bk-tbl td[data-label="No"] { order: 0; }
  #bk-tbl td[data-label="Nama"] { order: 1; }
  #bk-tbl td[data-label="Telpon"] { order: 2; }
  #bk-tbl td[data-label="Sisa"] { order: 3; }
  #bk-tbl td[data-label="Status"] { order: 4; }
  #bk-tbl td[data-label="Layanan"] { order: 5; }
  #bk-tbl td[data-label="Trip"] { order: 6; }
  #bk-tbl td[data-label="Asal"] { order: 7; }
  #bk-tbl td[data-label="PAX"] { order: 8; }
  #bk-tbl td[data-label="Pick Up"] { order: 9; }
  #bk-tbl td[data-label="Drop Off"] { order: 10; }
  #bk-tbl td[data-label="Total"] { order: 11; }
  #bk-tbl td[data-label="Dibayar"] { order: 12; }
  #bk-tbl td[data-label="Aksi"] { order: 99; }

  /* â”€â”€ HIDDEN FIELDS â”€â”€ */
  #bk-tbl td[data-label="Trip"],
  #bk-tbl td[data-label="Asal"],
  #bk-tbl td[data-label="PAX"],
  #bk-tbl td[data-label="Pick Up"],
  #bk-tbl td[data-label="Drop Off"],
  #bk-tbl td[data-label="Total"],
  #bk-tbl td[data-label="Dibayar"] {
    display: none;
  }

  /* â”€â”€ MOBILE ACTIONS VISIBILITY â”€â”€ */
  .bk-more-desktop { display: none !important; }
  .bk-more-mobile { display: flex !important; }

  /* â”€â”€ SPECIFIC COLUMN STYLING â”€â”€ */

  /* 1. Checkbox: absolute top left */
  #bk-tbl td:first-child {
    position: absolute;
    top: 16px;
    left: 16px;
    width: auto;
    margin-bottom: 0;
  }

  /* 2. No: absolute top right */
  #bk-tbl td[data-label="No"] {
    position: absolute;
    top: 16px;
    right: 16px;
    width: auto;
    align-items: flex-end;
    font-size: 16px !important;
    font-weight: 700;
    color: var(--g3);
    margin-bottom: 0;
  }
  #bk-tbl td[data-label="No"]::before { display: none; }

  /* 3. Nama: full width, offset to clear checkbox */
  #bk-tbl td[data-label="Nama"] {
    margin-top: 24px;
    font-size: 16px !important;
    margin-bottom: 2px;
  }
  #bk-tbl td[data-label="Nama"]::before { display: none; }
  #bk-tbl td[data-label="Nama"] b { font-weight: 700; color: var(--navy); }

  /* 4. Telpon: Always visible right under Nama */
  #bk-tbl td[data-label="Telpon"] {
    margin-bottom: 12px;
    font-size: 11px !important;
    color: var(--muted);
  }
  #bk-tbl td[data-label="Telpon"]::before { display: none; }

  /* 5. 50/50 split for Sisa and Status (always visible) */
  #bk-tbl td[data-label="Sisa"] { width: 50%; }
  #bk-tbl td[data-label="Status"] { 
    width: 50%; 
    align-items: flex-end;
  }
  
  /* Layanan gets a nice box */
  #bk-tbl td[data-label="Layanan"] {
    background: rgba(var(--ws-cyan-deep-rgb), .05);
    padding: 10px !important;
    border-radius: 8px;
    border: 1px solid var(--border) !important;
    margin-top: 8px;
  }

  /* 6. Aksi (Actions): full width at bottom â€” single row */
  #bk-tbl td[data-label="Aksi"] {
    width: 100%;
    flex-direction: row !important;
    align-items: center !important;
    flex-wrap: wrap;
    gap: 6px;
    border-top: 1px solid var(--border) !important;
    padding-top: 10px !important;
    margin-top: 4px;
    margin-bottom: 0;
    justify-content: flex-end;
  }
  #bk-tbl td[data-label="Aksi"]::before { display: none; }

  /* Flatten the outer wrapper div so buttons are direct flex siblings */
  #bk-tbl td[data-label="Aksi"] > div {
    display: contents !important;
  }


  /* bk-more-desktop is already hidden; make bk-more-mobile's children
     flow inline as direct siblings (no extra wrapper block) */
  #bk-tbl .bk-more-mobile {
    display: contents !important;
  }

  /* Uniform square icon buttons for ALL actions in Aksi row */
  #bk-tbl td[data-label="Aksi"] .ib {
    width: 38px !important;
    height: 38px !important;
    padding: 0 !important;
    font-size: 17px !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    border-radius: 10px !important;
    gap: 0 !important;
    background: var(--card) !important;
    border: 1.5px solid var(--border) !important;
    box-shadow: none !important;
    flex-shrink: 0;
  }
  #bk-tbl td[data-label="Aksi"] .ib:hover {
    border-color: var(--teal) !important;
    background: var(--card) !important;
  }
  #bk-tbl td[data-label="Aksi"] .ib.danger:hover {
    border-color: var(--red) !important;
  }

  /* â”€â”€ Kill hover teal flash and zebra stripe on card view â”€â”€ */
  #bk-tbl tbody tr:hover,
  #bk-tbl tbody tr:hover td {
    background: inherit !important;
    border-left: none !important;
  }
  #bk-tbl tbody tr:nth-child(even) td {
    background: transparent !important;
  }
}

/* ========================================
   TRIP ACTIONS MOBILE OVERFLOW FIX
   Show dropdown menu on mobile, inline buttons on desktop
   ======================================== */

/* Desktop: show inline buttons, hide mobile dropdown */
.trip-more-desktop {
  display: flex !important;
}

.trip-more-mobile {
  display: none !important;
}

/* Mobile: hide inline buttons, show dropdown menu */
@media (max-width: 640px) {
  .trip-more-desktop {
    display: none !important;
  }

  .trip-more-mobile {
    display: flex !important;
  }

  /* Style the dropdown menu */
  .trip-more-mobile > div[id^="tr-more-"] {
    display: none !important; /* starts hidden */
  }

  .trip-more-mobile > div[id^="tr-more-"][style*="display: flex"] {
    display: flex !important;
  }
}