/* ─── Shared Mobile Utilities — Strategy Compare ────────────────────────────────
   Include on all strategy-compare pages via <link rel="stylesheet" href="/strategy-compare/shared/mobile.css?v=1">
   Version: bump when changing
*/

/* ─── Table Scrolling (mandatory for ALL wide tables) ─── */
.scroll-table {
  overflow-x: auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
}
.scroll-table table {
  min-width: 600px;
}

/* ─── Table-to-Cards Transform (7+ cols on mobile <600px) ───
   Usage: Add class="cmp-tbl" (or year-tbl, data-tbl) to table,
   and data-label="Column Name" to every <td> and <th> in the table.
   The transform hides <thead> and renders each <tr> as a card.
*/
@media (max-width: 600px) {
  table.cmp-tbl,
  table.year-tbl,
  table.data-tbl {
    width: 100%;
    border-collapse: collapse;
  }
  table.cmp-tbl thead,
  table.year-tbl thead,
  table.data-tbl thead {
    display: none;
  }
  table.cmp-tbl tbody,
  table.year-tbl tbody,
  table.data-tbl tbody {
    display: block;
    width: 100%;
  }
  table.cmp-tbl tr,
  table.year-tbl tr,
  table.data-tbl tr {
    display: block;
    background: #111;
    border: 1px solid #1a1a1a;
    border-radius: 10px;
    padding: 12px;
    margin-bottom: 10px;
  }
  table.cmp-tbl td,
  table.year-tbl td,
  table.data-tbl td {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    padding: 6px 0;
    border-bottom: 1px solid #1a1a1a;
    font-size: 0.75rem;
    font-variant-numeric: tabular-nums;
    width: 100%;
    box-sizing: border-box;
  }
  table.cmp-tbl td:last-child,
  table.year-tbl td:last-child,
  table.data-tbl td:last-child {
    border-bottom: none;
  }
  table.cmp-tbl td::before,
  table.year-tbl td::before,
  table.data-tbl td::before {
    content: attr(data-label);
    color: #999;
    font-size: var(--fs-min, 0.75rem);
    font-weight: 500;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    flex-shrink: 0;
    margin-right: 12px;
    white-space: nowrap;
  }
  /* Keep first column (name/year) prominent */
  table.cmp-tbl td:first-child::before,
  table.year-tbl td:first-child::before,
  table.data-tbl td:first-child::before {
    color: #e0e0e0;
    font-weight: 600;
  }
  /* Right-align values */
  table.cmp-tbl td:not(:first-child),
  table.year-tbl td:not(:first-child),
  table.data-tbl td:not(:first-child) {
    justify-content: flex-end;
  }
  /* Preserve color classes */
  table.cmp-tbl td.pos,
  table.year-tbl td.pos,
  table.data-tbl td.pos {
    color: #4ade80;
  }
  table.cmp-tbl td.neg,
  table.year-tbl td.neg,
  table.data-tbl td.neg {
    color: #f87171;
  }
  table.cmp-tbl td.warn,
  table.year-tbl td.warn,
  table.data-tbl td.warn {
    color: #fbbf24;
  }
  table.cmp-tbl td.total,
  table.year-tbl td.total,
  table.data-tbl td.total {
    color: #e0e0e0;
    font-weight: 500;
    border-top: 1px solid #333;
  }
}

/* ─── Input Zoom Prevention (iOS Safari) ─── */
@media (max-width: 640px) {
  input[type="text"],
  input[type="number"],
  input[type="email"],
  input[type="tel"],
  input[type="search"],
  input[type="password"],
  select,
  textarea {
    font-size: 16px !important;
  }
  /* Range inputs need larger touch targets */
  input[type="range"] {
    height: 28px;
  }
  input[type="range"]::-webkit-slider-thumb {
    width: 24px !important;
    height: 24px !important;
  }
  input[type="range"]::-moz-range-thumb {
    width: 24px !important;
    height: 24px !important;
  }
}

/* ─── Responsive Grid Utilities ─── */
@media (max-width: 600px) {
  .grid-cols-1-sm {
    grid-template-columns: 1fr !important;
  }
  .grid-cols-2-sm {
    grid-template-columns: repeat(2, 1fr) !important;
  }
  .gap-sm {
    gap: 8px !important;
  }
}

@media (max-width: 480px) {
  .grid-cols-1-xs {
    grid-template-columns: 1fr !important;
  }
  .gap-xs {
    gap: 6px !important;
  }
}

/* ─── Chart Container Responsiveness ─── */
.chart-container,
.chart-container-sm {
  width: 100%;
  min-height: 280px;
}
@media (max-width: 600px) {
  .chart-container {
    height: 300px;
    min-height: 280px;
  }
  .chart-container-sm {
    height: 260px;
    min-height: 240px;
  }
}

/* ─── Cell Popup → Bottom Sheet on Mobile ─── */
@media (max-width: 640px) {
  .cell-popup-overlay {
    align-items: flex-end !important;
    padding: 0 !important;
  }
  .cell-popup-overlay.open {
    align-items: flex-end;
  }
  .cell-popup {
    position: fixed !important;
    bottom: 0;
    left: 0;
    right: 0;
    max-height: 55vh;
    max-width: 100%;
    width: 100%;
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -4px 24px rgba(0,0,0,0.5);
    margin: 0;
    padding-bottom: max(16px, env(safe-area-inset-bottom));
  }
  .cell-popup .close {
    top: 12px;
    right: 16px;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: #1a1a1a;
    border: 1px solid #333;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}

/* ─── Sub-Nav Scroll Compatibility (works with nav.js arrows) ─── */
.sub-nav {
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  -ms-overflow-style: none;
}
.sub-nav::-webkit-scrollbar {
  display: none;
}

/* ─── Button Touch Targets ─── */
@media (max-width: 480px) {
  .chip,
  .btn,
  .btn-primary,
  .btn-secondary {
    min-height: 40px;
    padding-top: 10px;
    padding-bottom: 10px;
  }
}

/* ─── Card Padding on Mobile ─── */
@media (max-width: 480px) {
  .card {
    padding: 16px !important;
  }
  .card.p-4 {
    padding: 12px !important;
  }
}

/* ─── Form Layout Stacking ─── */
@media (max-width: 600px) {
  .form-row {
    flex-direction: column;
  }
  .form-row > * {
    width: 100% !important;
  }
}

/* ─── Text Clamping for Long Labels ─── */
.truncate-label {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 140px;
}

/* ─── Spinner/Loading States ─── */
@keyframes spin {
  to { transform: rotate(360deg); }
}
.animate-spin {
  animation: spin 1s linear infinite;
}

/* ─── Focus Visible for Accessibility ─── */
:focus-visible {
  outline: 2px solid #e0e0e0;
  outline-offset: 2px;
}
button:focus-visible,
a:focus-visible,
input:focus-visible,
select:focus-visible {
  outline: 2px solid #e0e0e0;
  outline-offset: 2px;
}

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