/* ============================================================================
   US WORKSPACE (/static/index.html) MOBILE & TABLET HYBRID RESPONSIVE LAYER
   - Phone (<=680px): Zero horizontal page overflow, >=12px readable text,
     sticky first table columns, compact KPIs, 100vw X-Ray drawer
   - Tablet (681px-1099px): Full-width chart, 2-col Bento/Hero, sticky Pro tables
   - Desktop (>=1100px): Strictly untouched
   ============================================================================ */

@media (max-width: 1099px) {
  html, body {
    overflow-x: clip !important;
    max-width: 100vw;
  }

  .app-shell,
  .work-area,
  .page-view {
    min-width: 0 !important;
    max-width: 100% !important;
  }

  /* Ensure sticky first column on all US tables when narrower than full width */
  .wl-matrix-scroll,
  .tablewrap,
  .signal-table-wrap {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
  }

  .wl-matrix-scroll table th:first-child,
  .wl-matrix-scroll table td:first-child,
  .port-table-style3 th:first-child,
  .port-table-style3 td:first-child,
  .universe-table th:first-child,
  .universe-table td:first-child {
    position: sticky;
    left: 0;
    z-index: 3;
    background: #0d131a;
    border-right: 1px solid rgba(0, 240, 255, 0.24);
    box-shadow: 4px 0 12px rgba(0, 0, 0, 0.5);
  }
  .wl-matrix-scroll table thead th:first-child,
  .port-table-style3 thead th:first-child,
  .universe-table thead th:first-child {
    z-index: 5;
    background: #111b27;
  }

  html[data-theme="light"] .wl-matrix-scroll table th:first-child,
  html[data-theme="light"] .wl-matrix-scroll table td:first-child,
  html[data-theme="light"] .port-table-style3 th:first-child,
  html[data-theme="light"] .port-table-style3 td:first-child,
  html[data-theme="light"] .universe-table th:first-child,
  html[data-theme="light"] .universe-table td:first-child {
    background: #ffffff;
    border-right-color: #cbd5e1;
    box-shadow: 4px 0 10px rgba(15, 23, 42, 0.08);
  }
}

/* ============================================================================
   TABLET MODE (681px - 1099px)
   ============================================================================ */
@media (min-width: 681px) and (max-width: 1099px) {
  .work-area {
    padding: 12px 14px 20px !important;
  }

  .topbar {
    padding: 8px 14px !important;
    gap: 10px !important;
  }

  .workspace {
    grid-template-columns: 1fr !important;
    gap: 14px !important;
  }

  .wl-kpi-strip {
    grid-template-columns: repeat(4, minmax(0, 1fr)) !important;
    gap: 10px !important;
  }

  .wl-bento-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 12px !important;
  }

  .hero-fund-card {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 12px !important;
  }

  .port-entry-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
    gap: 10px !important;
  }

  .port-entry-grid .submit-field {
    grid-column: 1 / -1;
  }
}

/* ============================================================================
   PHONE MODE (<=680px)
   ============================================================================ */
@media (max-width: 680px) {
  .topbar {
    padding: 8px 10px !important;
    gap: 8px !important;
    flex-wrap: wrap;
    min-height: 48px;
  }
  .topbar .brand small,
  .topbar .live,
  .topbar .refresh-top span,
  .topbar .theme-toggle #themeLabel {
    display: none !important;
  }
  .topbar .market-date {
    font-size: 12px !important;
  }
  .topbar .refresh-top,
  .topbar .theme-toggle {
    min-width: 40px;
    min-height: 40px;
    padding: 0 10px !important;
  }

  .work-area {
    padding: 10px 10px 24px !important;
  }

  /* Overview / Chart workspace */
  .workspace {
    grid-template-columns: 1fr !important;
    gap: 12px !important;
  }
  .instrument-head {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 8px !important;
  }
  .instrument-head .quote {
    text-align: left !important;
    width: 100%;
    display: flex;
    align-items: baseline;
    gap: 10px;
  }
  .instrument-badges {
    gap: 6px !important;
  }
  .instrument-badges .inst-pill,
  .instrument-badges .c-badge {
    font-size: 12px !important;
  }
  .chart-toolbar {
    gap: 6px !important;
  }
  .chart-toolbar .instrument-search {
    width: 100% !important;
    flex: 1 1 100% !important;
  }
  .chart-toolbar select,
  .chart-toolbar input {
    min-height: 42px;
    font-size: 13px !important;
  }
  .chartbox {
    min-height: 330px !important;
    height: 360px !important;
  }

  /* Holding & Portfolio return bars */
  .port-return-bar {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 6px !important;
    padding: 10px !important;
  }
  .port-return-btn {
    width: 100%;
    min-height: 44px;
    justify-content: center;
    font-size: 12.5px !important;
  }

  /* Watchlist mobile hybrid */
  .wl-head {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 8px !important;
  }
  .wl-head-right {
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 6px !important;
    width: 100%;
  }
  .wl-view-switch {
    width: 100%;
    display: grid !important;
    grid-template-columns: repeat(3, 1fr);
  }
  .wl-switch-btn {
    min-height: 42px !important;
    justify-content: center;
    font-size: 12px !important;
  }
  .wl-btn-edit,
  .wl-btn-add {
    flex: 1 1 calc(50% - 4px);
    min-height: 42px;
    font-size: 12px !important;
  }

  .wl-kpi-strip {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 8px !important;
  }
  .wl-kpi-strip > *,
  .wl-kpi-card {
    overflow: hidden !important;
    min-width: 0 !important;
    padding: 9px 10px !important;
    border-radius: 12px !important;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.10), 0 4px 14px rgba(0, 0, 0, 0.4) !important;
  }
  .wl-kpi-top {
    flex-wrap: wrap !important;
    gap: 4px !important;
    align-items: center !important;
  }
  .wl-kpi-lbl {
    font-size: 11px !important;
    line-height: 1.2 !important;
  }
  .wl-kpi-tag {
    max-width: 100% !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    white-space: nowrap !important;
    font-size: 10.5px !important;
    padding: 2px 6px !important;
  }
  .wl-kpi-main {
    flex-wrap: wrap !important;
    gap: 4px !important;
  }
  .wl-kpi-sub {
    width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* Keep sticky first column crisp & clipped inside 158px on phone */
  .wl-matrix-scroll table td:first-child,
  .wl-matrix-scroll table th:first-child {
    max-width: 158px !important;
    width: 158px !important;
    overflow: hidden !important;
  }
  .wl-cell-asset {
    max-width: 146px !important;
    overflow: hidden !important;
    gap: 6px !important;
  }
  .wl-cell-asset .wl-rank,
  .wl-cell-asset .wl-pdate {
    display: none !important;
  }
  .wl-asset-txt {
    min-width: 0 !important;
    overflow: hidden !important;
  }
  .wl-asset-sub {
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    white-space: nowrap !important;
  }

  .wl-control-bar {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 8px !important;
  }
  .wl-filter-chips {
    flex-wrap: nowrap !important;
    overflow-x: auto !important;
    scrollbar-width: none;
    padding-bottom: 2px;
    -webkit-overflow-scrolling: touch;
  }
  .wl-filter-chips::-webkit-scrollbar { display: none; }
  .wl-filter-chips button {
    min-height: 40px;
    white-space: nowrap;
    flex-shrink: 0;
    font-size: 12px !important;
  }

  .wl-toolbar-right {
    display: grid !important;
    grid-template-columns: 1fr 1fr;
    gap: 6px !important;
    width: 100%;
  }
  .wl-toolbar-right .wl-select {
    width: 100% !important;
    min-height: 42px;
    font-size: 12px !important;
  }
  .wl-toolbar-right .wl-select-sort,
  .wl-toolbar-right .wl-search-box {
    grid-column: 1 / -1;
    width: 100% !important;
    min-height: 44px;
  }
  .wl-search-box input {
    font-size: 13.5px !important;
  }

  .wl-bento-grid {
    grid-template-columns: 1fr !important;
    gap: 10px !important;
  }

  /* Fullscreen X-Ray drawer on phone */
  .wl-xray-drawer {
    width: 100vw !important;
    max-width: 100vw !important;
    border-left: 0 !important;
    border-radius: 0 !important;
  }

  /* Portfolio mobile hybrid */
  .portfolio-header {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 8px !important;
  }
  .portfolio-macro-rates {
    width: 100%;
    flex-wrap: wrap !important;
    gap: 6px !important;
  }
  .portfolio-macro-rates .macro-pill {
    font-size: 12px !important;
  }

  .port-entry-header {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 6px !important;
  }
  .port-badge-step {
    white-space: nowrap !important;
    width: fit-content !important;
  }
  .port-entry-grid {
    grid-template-columns: 1fr 1fr !important;
    gap: 8px !important;
  }
  .port-entry-grid .port-field:first-child,
  .port-entry-grid .auto-tot-field,
  .port-entry-grid .submit-field {
    grid-column: 1 / -1;
  }
  .port-entry-grid input,
  .port-entry-grid .port-cal-btn,
  .port-entry-grid .port-btn-save {
    min-height: 44px;
    font-size: 13.5px !important;
  }
  .port-side-toggle {
    height: 44px !important;
    padding: 3px !important;
    align-items: stretch !important;
  }
  .port-side-toggle .side-btn {
    min-height: 0 !important;
    height: 100% !important;
    padding: 0 6px !important;
    font-size: 12.5px !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
  }
  .port-entry-grid label {
    font-size: 12px !important;
  }

  .hero-fund-card {
    grid-template-columns: 1fr !important;
    gap: 12px !important;
    padding: 12px !important;
  }
  .port-table-head {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 6px !important;
  }
  .port-table-legend {
    flex-wrap: wrap !important;
    gap: 8px !important;
    font-size: 12px !important;
  }

  /* Universe tools on phone */
  .universe-tools {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 8px !important;
  }
  .universe-tools input,
  .universe-tools select,
  .universe-tools button {
    min-height: 42px;
    font-size: 12.5px !important;
  }
}
