/* Markets grid layer, shared by /markets (markets_grid.html) and
   /markets/compare (compare_grid.html). Theme pass Batch D (2026-11-12):
   the one place the two grids get their look, on the app kit. The table is
   the dense ui-table (12px muted header on the input fill, 12.5px cells,
   column rules), the dialogs take the popover look from markets-theme.css,
   and nothing is colored as a judgment: coverage, status and stars are
   said in words, numbers and the primary color only. */

/* ── Table ─────────────────────────────────────────────────────────────── */
/* The frame scrolls both ways inside the window (11/13, "freeze the header
   row"): the header row sticks at its top and the Market column at its left
   while the body scrolls. --mk-grid-top is the frame's page top, set by
   grid_table.js; 16px leaves the page padding below it. */
.table-wrap { overflow: auto; max-height: calc(100vh - var(--mk-grid-top, 150px) - 16px);
              min-height: 240px; background: var(--mk-bg-card);
              border: 1px solid var(--mk-line); border-radius: var(--mk-radius-lg); }
.table-wrap table { border-collapse: separate; border-spacing: 0; min-width: 100%; width: max-content;
                    white-space: nowrap; font-variant-numeric: tabular-nums; }
.table-wrap th, .table-wrap td { border-right: 1px solid var(--mk-line); border-bottom: 1px solid var(--mk-line);
                                 padding: 8px 10px; text-align: right; vertical-align: top; }
/* Body cells hold the value only (source and date are in the header). */
.table-wrap td { padding: 6px 10px; vertical-align: middle; }
.table-wrap th:last-child, .table-wrap td:last-child { border-right: none; }
.table-wrap th { position: sticky; top: 0; text-align: center; cursor: pointer; user-select: none;
                 background: var(--mk-fill); font-size: 12px; font-weight: 600; color: var(--mk-faint); }
.table-wrap tbody tr:nth-child(even) td { background: #F9FAFB; }
.table-wrap tbody tr:hover td { background: var(--bg-hover, #F3F4F6); }

/* Two row sticky header: the category band at top 0, the metric row under it. */
.table-wrap th.cat-band { height: 28px; box-sizing: border-box; padding: 0 10px; text-align: left;
                          font-size: 11.5px; font-weight: 600; color: var(--mk-muted);
                          background: var(--mk-fill); border-bottom: 1px solid var(--mk-line); cursor: default; }
.table-wrap th.cat-band-lead { background: var(--mk-bg-card); cursor: default; }
tr.cat-band-row th { top: 0; z-index: 3; }
tr.metric-head-row th { top: 28px; z-index: 2; }

/* Metric header: label (+ sort caret), the source line, then the tools
   strip (details, shade, direction) that comes up on hover. A set width
   (the table is max-content wide) lets label and source wrap instead of
   stretching the column. */
th.metric-th { width: 136px; min-width: 92px; padding: 8px 10px 7px; vertical-align: top; white-space: normal; }
th.metric-th .th-label { display: block; font-size: 12px; font-weight: 600; color: var(--mk-ink);
                         line-height: 1.3; white-space: normal; }
th.metric-th .th-units { display: block; }
/* Source and date of the column, the quiet second header line (11/13).
   A column fed by more than one source says so; its title lists them. */
.th-src { display: block; margin-top: 2px; font-size: 11px; font-weight: 500; line-height: 1.3;
          color: var(--mk-faint); cursor: help; }
.th-src.mixed { font-style: italic; }
th.metric-th .th-tools { display: flex; align-items: center; justify-content: center; gap: 4px;
                         margin-top: 6px; opacity: 0.55; transition: opacity 0.12s; }
th.metric-th:hover .th-tools, th.metric-th:focus-within .th-tools { opacity: 1; }

.info-icon { font-size: 12px; color: var(--mk-faint); cursor: pointer; padding: 1px 2px; transition: color 0.1s; }
th:hover .info-icon, th:focus-within .info-icon { color: var(--mk-navy); }

/* Shade toggle and its direction: small chip toggles, on = primary fill. */
.table-wrap .heat-btn, .table-wrap .heat-dir-btn {
  display: inline-flex; align-items: center; justify-content: center;
  height: 22px; box-sizing: border-box; padding: 0 8px;
  border: 1px solid var(--mk-line); border-radius: 12px; background: var(--mk-bg-card);
  font-family: var(--mk-font); font-size: 11.5px; font-weight: 600; line-height: 1;
  color: var(--mk-muted); cursor: pointer; transition: border-color 0.1s, color 0.1s, background 0.1s; }
.table-wrap .heat-btn:hover, .table-wrap .heat-dir-btn:hover { border-color: var(--mk-navy); color: var(--mk-navy); }
.table-wrap .heat-btn.active { background: var(--mk-navy); border-color: var(--mk-navy); color: #fff; }
.table-wrap .heat-btn.active:hover { background: var(--mk-navy-dark); border-color: var(--mk-navy-dark); color: #fff; }
/* The direction toggle only means something while the column is shaded.
   It shows the column's scale as five swatches, low value on the left. */
.table-wrap .heat-dir-btn { display: none; gap: 0; padding: 0 5px; }
.table-wrap .heat-dir-btn .shade-sw { display: inline-block; width: 6px; height: 10px; }
.table-wrap .heat-dir-btn .shade-sw:first-child { border-radius: 2px 0 0 2px; }
.table-wrap .heat-dir-btn .shade-sw:last-child { border-radius: 0 2px 2px 0; }
.table-wrap .heat-btn.active + .heat-dir-btn { display: inline-flex; }

/* Sort caret: always there so the column reads as sortable. */
.sort-arrow { display: inline-block; margin-left: 4px; vertical-align: middle;
              font-size: 11.5px; font-weight: 700; color: var(--mk-faint); }
.sort-arrow::before { content: '\2195'; }
th.metric-th:hover .sort-arrow { color: var(--mk-navy); }
th.metric-th.sort-asc  .sort-arrow::before { content: '\25B2'; color: var(--mk-navy); }
th.metric-th.sort-desc .sort-arrow::before { content: '\25BC'; color: var(--mk-navy); }

/* Coverage: one neutral chip for every level, the fraction says it. */
.table-wrap .cov-badge { cursor: help; padding: 2px 7px; }

/* ── Market column (sticky) ────────────────────────────────────────────── */
th.market-col, td.market-col { position: sticky; left: 0; z-index: 1; min-width: 185px; text-align: left; }
/* The header corner is frozen both ways: sticky top (from .table-wrap th or
   the metric head row) and left, above every scrolling header cell (z 2/3)
   and body cell (z 1), on an opaque fill so nothing shows through. The
   specificity matches tr.metric-head-row th so the z-index is not lost. */
.table-wrap thead th.market-col { left: 0; z-index: 5; background: var(--mk-fill);
                                  border-right: 1px solid var(--mk-line); }
.table-wrap thead tr.metric-head-row th.market-col { top: 28px; }
.table-wrap thead th.cat-band-lead { position: sticky; left: 0; z-index: 5; }
.table-wrap td.market-col { background: var(--mk-bg-card); border-right: 1px solid var(--mk-line); }
.table-wrap tbody tr:nth-child(even) td.market-col { background: #F9FAFB; }
.table-wrap tbody tr:hover td.market-col { background: var(--bg-hover, #F3F4F6); }
.market-col-inner { display: flex; align-items: center; gap: 7px; }
.market-col-inner > div { min-width: 0; }
.market-name { font-size: 13px; font-weight: 600; color: var(--mk-navy); text-decoration: none;
               overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.market-meta { margin-left: 6px; font-size: 11.5px; color: var(--mk-faint); white-space: nowrap; }
.tier-3 .market-name { color: var(--mk-muted); font-weight: 500; }

/* Star (pin) and the drag handle of pinned rows: the primary color, not gold. */
.star-btn { background: none; border: none; padding: 0; cursor: pointer; line-height: 1;
            font-size: 15px; color: var(--mk-faint); transition: color 0.15s; }
.star-btn:hover { color: var(--mk-navy-light); }
.star-btn.starred { color: var(--mk-navy); }
.drag-handle { cursor: grab; color: var(--mk-faint); padding: 0 1px; user-select: none; letter-spacing: -1px; }
.drag-handle:hover { color: var(--mk-ink); }
.table-wrap tbody tr.starred td.market-col { background: var(--mk-navy-tint); box-shadow: inset 3px 0 0 var(--mk-navy); }
tr.dragging { opacity: 0.4; }
tr.drag-over td.market-col { box-shadow: inset 0 2px 0 var(--mk-navy); }
.table-wrap tr.star-divider td { height: 6px; padding: 0; background: var(--mk-fill); }

/* ── Cells ─────────────────────────────────────────────────────────────── */
td > .val { font-size: 12.5px; font-weight: 600; color: var(--mk-ink); line-height: 1.25; }
/* A value from an older period than the column's date (its own on hover). */
td > .val.off-period { font-style: italic; font-weight: 500; }
.empty { color: var(--mk-faint); }
.table-wrap td.live-empty, .table-wrap td.derived-cell { background: var(--mk-bg-card); }
.table-wrap tbody tr:nth-child(even) td.live-empty,
.table-wrap tbody tr:nth-child(even) td.derived-cell { background: #F9FAFB; }
/* Status columns (needs source / research are filtered out server side; the
   estimated marker is the only one that shows): said by the marker, no tint. */
th.derived .th-label { color: var(--mk-ink); }
.derived-marker { margin-left: 3px; font-size: 12px; color: var(--mk-faint); }
/* Acwise data: columns Acwise computes itself get a faint blue header, the
   header cell only (the body stays banded like every other column). */
:root { --acw-own-data: #EAF1FB; }
.table-wrap th.metric-th.own-data { background: var(--acw-own-data); }
.own-data-swatch { display: inline-block; width: 12px; height: 12px; border-radius: 3px;
                   background: var(--acw-own-data); border: 1px solid var(--mk-line); }

/* Legend and summary under the table */
.legend { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 12px;
          font-size: 12px; color: var(--mk-faint); }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.summary { margin-top: 12px; font-size: 12px; color: var(--mk-muted); }
.summary b { color: var(--mk-ink); }

/* Page head: title, count and data date on one line (11/13). */
.markets-wrap .page-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 12px; margin: 0 0 10px; }
.markets-wrap .page-head h1 { margin: 0; }
.data-as-of { font-size: 12px; color: var(--mk-faint); }

/* Pipeline deal count beside a market name */
.deal-chip { margin-left: 6px; vertical-align: middle; }

/* ── Dialog sizes (the look is markets-theme.css) ──────────────────────── */
.modal-backdrop { display: none; position: fixed; inset: 0; z-index: 1000;
                  align-items: flex-start; justify-content: center; }
.modal-backdrop.open { display: flex; }
.modal-backdrop > .modal { width: min(560px, 100%); max-width: min(560px, 100%); }
.modal-section { margin: 10px 0; }
.modal-section-label { display: block; margin-bottom: 2px; font-size: 12px; font-weight: 500; color: var(--mk-faint); }
.modal-section-text { font-size: 13px; color: var(--mk-ink); line-height: 1.55; }
.modal-no-desc { font-size: 13px; color: var(--mk-faint); }
.modal-meta .badge { margin-left: 6px; }

/* Cell drilldown: chart beside its history table; stacks under 900px. */
.modal-backdrop > .modal.cell-modal { width: min(1080px, 100%); max-width: min(1080px, 100%); }
#m-body { flex: 1 1 auto; min-height: 0; overflow-y: auto; }
.cell-body { display: flex; gap: 14px; align-items: stretch; flex: 1 1 auto;
             min-height: 0; max-height: 420px; overflow: hidden; }
.cell-chart-wrap { flex: 1.6; min-width: 0; min-height: 0; display: flex; padding: 6px;
                   border: 1px solid var(--mk-line); border-radius: var(--radius-control, 8px); background: var(--mk-bg-card); }
.cell-chart-wrap canvas { width: 100% !important; height: auto !important;
                          max-height: 100% !important; min-height: 0 !important; }
.cell-table-wrap { flex: 1; min-width: 200px; max-width: 360px; overflow-y: auto; min-height: 0;
                   border: 1px solid var(--mk-line); border-radius: var(--radius-control, 8px); }
@media (max-width: 900px) {
  .cell-body { flex-direction: column; max-height: none; }
  .cell-table-wrap { max-width: none; max-height: 240px; }
  .cell-chart-wrap canvas { min-height: 220px !important; }
}

/* + Add market dialog: form beside a live map preview. */
.modal-backdrop > .modal.add-modal { width: min(920px, 100%); max-width: min(920px, 100%); }
.add-body { display: flex; gap: 18px; flex: 1 1 auto; min-height: 0; overflow-y: auto; }
.add-body .form-col { flex: 1; min-width: 0; }
.add-body .map-col { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.add-body .map-col #add-map { flex: 1; min-height: 320px; position: relative; overflow: hidden;
                              border: 1px solid var(--mk-line); border-radius: var(--radius-control, 8px); background: var(--mk-fill); }
.add-body .map-col .map-status { margin-top: 6px; min-height: 14px; font-size: 12px; color: var(--mk-faint); }
.add-form-row { display: grid; grid-template-columns: 110px 1fr; gap: 6px 10px; align-items: center; margin-bottom: 8px; }
.add-form-row label { font-size: 12px; font-weight: 500; color: var(--mk-faint); }
.add-form-row :is(input, select) { width: 100%; }
.add-form-divider { margin: 10px 0; border-top: 1px solid var(--mk-line); }
.add-form-help { margin-top: 4px; font-size: 12px; color: var(--mk-faint); }
.add-form-actions { display: flex; gap: 8px; align-items: center; margin-top: 14px; }
#add-status { margin-left: 8px; font-size: 12px; color: var(--mk-muted); }
#add-status.err { color: var(--mk-error); }
@media (max-width: 760px) { .add-body { flex-direction: column; } }

.legend [hidden] { display: none; }
