/* SPDX-License-Identifier: Apache-2.0 */
/* AGY-Nexus /web/v9 — Analytics Surface, Unboxed Control Bar, 2-Up Subgrid Plots & Compact Tables */

@layer views {
  .v9-stage {
    inline-size: min(1480px, calc(100% - 1.5rem));
    max-inline-size: 1480px;
    margin-inline: auto;
    padding-block: 0.65rem 1.75rem;
    display: flex;
    flex-direction: column;
    gap: var(--density-grid-gap, 0.75rem);
    min-inline-size: 0;
  }

  /* Flat unboxed single-row analytics toolbar */
  .v9-analytics-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.35rem 0.5rem;
    padding: 0.15rem 0;
    background: transparent;
    border: 0;
  }
  .v9-analytics-bar-grp {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.35rem;
    min-inline-size: 0;
  }
  .v9-bar-break { display: none; }
  .v9-horizon-label {
    padding-inline: 0.5rem;
    display: inline-flex;
    align-items: center;
    font-family: var(--font-mono);
    font-size: var(--ts-xs);
    font-weight: 700;
    color: var(--text-primary);
    min-inline-size: 5rem;
    justify-content: center;
  }

  /* Cards & Headers */
  .v9-card {
    background: var(--bg-surface);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-card);
    padding: var(--density-card-pad, 0.85rem);
    box-shadow: var(--v9-shadow-card);
    display: flex;
    flex-direction: column;
    gap: 0.55rem;
    min-inline-size: 0;
  }
  .v9-card-head {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    justify-content: space-between;
    gap: 0.3rem 0.65rem;
  }
  .v9-card-title {
    margin: 0;
    font-size: var(--ts-sm);
    font-weight: 700;
    color: var(--text-primary);
  }
  .v9-card-sub {
    font-family: var(--font-mono);
    font-size: var(--ts-xs);
    color: var(--text-muted);
    font-variant-numeric: var(--font-nums);
  }

  /* 52-Week Activity Heatmap Grid */
  .v9-heat-outer { display: flex; flex-direction: column; gap: 0.4rem; min-inline-size: 0; }
  .v9-heat-scroll { overflow-x: auto; padding-block-end: 0.15rem; }
  .v9-heat-layout { display: flex; align-items: flex-start; gap: 0.4rem; inline-size: 100%; min-inline-size: 0; }
  .v9-heat-dow {
    display: grid;
    grid-template-rows: repeat(7, 14px);
    gap: 3px;
    padding-block-start: calc(1.15rem + 3px);
    font-family: var(--font-mono);
    font-size: var(--ts-xs);
    color: var(--text-muted);
    flex-shrink: 0;
  }
  .v9-heat-dow > span { block-size: 14px; line-height: 14px; }
  .v9-heat-main { flex: 1 1 auto; min-inline-size: 0; }
  .v9-heat-months {
    display: grid;
    gap: 3px;
    inline-size: 100%;
    block-size: 1.15rem;
    font-family: var(--font-mono);
    font-size: var(--ts-xs);
    color: var(--text-muted);
    margin-block-end: 3px;
  }
  .v9-heat-month-cell { white-space: nowrap; overflow: visible; }
  .v9-heat-grid {
    display: grid;
    grid-template-rows: repeat(7, 14px);
    grid-auto-flow: column;
    grid-auto-columns: minmax(10px, 1fr);
    inline-size: 100%;
    gap: 3px;
  }
  .v9-heat-cell {
    border-radius: 2px;
    background: var(--v9-heat-0);
    border: 1px solid transparent;
    cursor: pointer;
    min-block-size: 14px;
  }
  .v9-heat-cell[data-level="1"] { background: var(--v9-heat-1); }
  .v9-heat-cell[data-level="2"] { background: var(--v9-heat-2); }
  .v9-heat-cell[data-level="3"] { background: var(--v9-heat-3); }
  .v9-heat-cell[data-level="4"] { background: var(--v9-heat-4); }
  .v9-heat-cell.is-out { filter: grayscale(0.92); opacity: 0.28; }
  .v9-heat-cell.is-out[data-level="0"] { opacity: 0.42; }
  .v9-heat-cell.is-out:hover { filter: grayscale(0.25); opacity: 0.85; }
  .v9-heat-cell.is-future { background: transparent; border: 1px dotted var(--border-subtle); opacity: 0.3; }
  .v9-heat-cell:hover { border-color: var(--text-primary); }
  .v9-heat-foot {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.45rem;
    font-family: var(--font-mono);
    font-size: var(--ts-xs);
    color: var(--text-secondary);
  }
  .v9-heat-scale { display: inline-flex; align-items: center; gap: 3px; }
  .v9-heat-key { inline-size: 11px; block-size: 11px; border-radius: 2px; display: inline-block; }

  /* 1-Column Daily Stack & >=1100px 2-Up Secondary Pair */
  .v9-daily-stack {
    display: flex;
    flex-direction: column;
    gap: var(--density-grid-gap, 0.75rem);
    min-inline-size: 0;
  }
  .v9-secondary-pair {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: var(--density-grid-gap, 0.75rem);
    min-inline-size: 0;
  }
  @media (min-width: 1100px) {
    .v9-secondary-pair { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .v9-secondary-pair > .va-card {
      display: grid;
      grid-row: span 3;
      grid-template-rows: auto minmax(0, 1fr) auto;
      grid-template-rows: subgrid;
      align-content: stretch;
    }
  }
  .v9-echarts-host { inline-size: 100%; block-size: 210px; min-inline-size: 0; position: relative; }
  .v9-echarts-host-daily { block-size: 224px; }
  .v9-echarts-host-compact { block-size: 210px; }

  .v9-table-scroll {
    overflow-x: auto;
    border-block-start: 1px solid var(--border-hairline);
    padding-block-start: 0.3rem;
  }
  .v9-table {
    inline-size: 100%;
    border-collapse: collapse;
    font-size: var(--ts-xs);
    font-variant-numeric: var(--font-nums);
  }
  .v9-table th {
    text-align: start;
    padding: 0.28rem 0.45rem;
    font-weight: 700;
    color: var(--text-muted);
    border-block-end: 1px solid var(--border-strong);
    white-space: nowrap;
  }
  .v9-table td {
    padding: 0.28rem 0.45rem;
    border-block-end: 1px solid var(--border-hairline);
    vertical-align: middle;
    color: var(--text-primary);
  }
  .v9-row-compact td { white-space: nowrap; }
  .v9-cell-main { font-weight: 600; color: var(--text-primary); }
  .v9-cell-sub {
    font-family: var(--font-mono);
    font-size: var(--ts-xs);
    color: var(--text-muted);
    line-height: 1.25;
  }
  .v9-inline-share {
    display: inline-flex;
    align-items: center;
    gap: 0.45rem;
    inline-size: 100%;
    min-inline-size: 120px;
  }
  .v9-inline-share .va-inline-bar { flex: 1 1 72px; min-inline-size: 56px; }
  .v9-inline-pct { font-family: var(--font-mono); color: var(--text-muted); min-inline-size: 3.1rem; text-align: end; }
  .v9-bar-cell { min-inline-size: 120px; }
  .v9-table tr.v9-row-click { cursor: pointer; }
  .v9-table tr.v9-row-click:hover td { background: var(--bg-elevated); }
  .v9-table tr.is-solo td { background: var(--bg-elevated); font-weight: 600; }
  .v9-table tr.is-dim td { opacity: 0.45; }
  .v9-swatch-dot {
    display: inline-block;
    inline-size: 8px;
    block-size: 8px;
    border-radius: 2px;
    margin-inline-end: 0.38rem;
    vertical-align: middle;
    flex-shrink: 0;
  }
  .v9-delta-pos { color: var(--accent-primary); font-family: var(--font-mono); }
  .v9-delta-neg { color: var(--text-muted); font-family: var(--font-mono); }
  .v9-delta-warn { color: var(--status-warn); font-family: var(--font-mono); font-weight: 600; }

  /* Wire Foot & Wrap */
  .v9-wire-foot {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    border-block-start: 1px solid var(--border-hairline);
    padding-block-start: 0.4rem;
    min-inline-size: 0;
  }
  .v9-wire-clients {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem 1rem;
    font-family: var(--font-mono);
    font-size: var(--ts-xs);
    color: var(--text-secondary);
  }
  .v9-wire-client-item { display: inline-flex; align-items: center; gap: 0.35rem; min-inline-size: 0; }
  .v9-wrap-anywhere { white-space: normal; overflow-wrap: anywhere; }

  /* Collapsed Code Health & Audit Drawer */
  details.v9-audit-drawer > summary {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    cursor: pointer;
    font-weight: 700;
    font-size: var(--ts-sm);
    color: var(--text-primary);
  }
  .v9-health-strip {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem 1.15rem;
    font-family: var(--font-mono);
    font-size: var(--ts-xs);
    color: var(--text-secondary);
  }
  .v9-sess-id-btn {
    border: 1px solid var(--border-subtle);
    background: var(--bg-elevated);
    color: var(--text-secondary);
    border-radius: var(--radius-xs);
    padding: 1px 6px;
    font-family: var(--font-mono);
    font-size: var(--ts-xs);
    cursor: pointer;
  }
  .v9-sess-id-btn:hover { border-color: var(--accent-primary); color: var(--accent-primary); }
  .v9-warmup-box {
    display: flex;
    align-items: center;
    justify-content: center;
    min-block-size: 160px;
    border: 1px dashed var(--border-strong);
    border-radius: var(--radius-inner);
    color: var(--text-muted);
    font-family: var(--font-mono);
    font-size: var(--ts-sm);
    padding: 1rem;
    text-align: center;
  }

  /* Inline Project & Session Roster Table Action Controls */
  .v9-tbl-name-wrap {
    display: inline-flex;
    align-items: center;
    gap: 0.36rem;
    min-inline-size: 0;
    max-inline-size: 100%;
  }
  .v9-tbl-name-wrap .v9-swatch-dot { margin-inline-end: 0; }
  .v9-tbl-name-txt {
    font-weight: 600;
    color: var(--text-primary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-inline-size: 260px;
  }
  .v9-tbl-actions {
    display: inline-flex;
    align-items: center;
    justify-content: flex-end;
    gap: 0.28rem;
    white-space: nowrap;
  }
  .v9-tbl-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.22rem;
    min-block-size: 24px;
    padding: 0.1rem 0.42rem;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-xs);
    background: var(--bg-surface);
    color: var(--text-secondary);
    font-family: var(--font-sans);
    font-size: 11px;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
  }
  .v9-tbl-btn:hover:not(:disabled) { border-color: var(--accent-primary); color: var(--accent-primary); }
  .v9-tbl-btn.is-primary { background: var(--bg-elevated); color: var(--text-primary); }
  .v9-tbl-btn:disabled { opacity: 0.42; cursor: not-allowed; }
  .v9-status-tag {
    display: inline-block;
    padding: 1px 5px;
    border-radius: var(--radius-xs);
    border: 1px solid var(--border-subtle);
    background: var(--bg-elevated);
    font-family: var(--font-mono);
    font-size: 11px;
    font-weight: 600;
    color: var(--text-secondary);
    white-space: nowrap;
  }
  .v9-status-tag[data-tone="run"] { border-color: var(--accent-primary); color: var(--accent-primary); }
  .v9-status-tag[data-tone="warn"] { border-color: var(--status-warn); color: var(--status-warn); }

  /* Compound Period Stepper & 6-Preset Calendar / Rolling Popover */
  .v9-period-wrap { position: relative; display: inline-flex; max-inline-size: 100%; }
  .v9-period-stepper { gap: 1px; max-inline-size: 100%; }
  .v9-step-btn { min-inline-size: 24px; padding-inline: 0.32rem; }
  .v9-period-trigger {
    display: inline-flex;
    align-items: center;
    gap: 0.32rem;
    padding-inline: 0.5rem;
    font-family: var(--font-mono);
    font-size: var(--ts-xs);
    font-weight: 600;
    color: var(--text-primary);
    min-inline-size: 0;
  }
  .v9-period-trigger[aria-expanded="true"] {
    background: var(--bg-surface);
    box-shadow: 0 1px 2px rgba(15, 23, 42, 0.08);
  }
  .v9-period-trigger-txt { white-space: nowrap; }
  .v9-p-short, .v9-metric-short { display: none; }
  .v9-caret-down { flex-shrink: 0; color: var(--text-muted); transition: transform 0.14s ease; }
  .v9-period-trigger[aria-expanded="true"] .v9-caret-down { transform: rotate(180deg); }
  .v9-period-today { font-family: var(--font-mono); font-weight: 600; color: var(--accent-primary); }

  .v9-popover.v9-period-popover {
    inset-inline-end: 0;
    inset-inline-start: auto;
    inline-size: min(272px, calc(100dvw - 24px));
    padding: 0.55rem;
    gap: 0.45rem;
    background-color: var(--bg-surface);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-card);
    z-index: 60;
    opacity: 1;
  }
  .v9-period-group { display: flex; flex-direction: column; gap: 0.16rem; }
  .v9-menu-sep { border: 0; border-block-start: 1px solid var(--border-hairline); margin: 0.15rem 0; }
  .v9-period-opt {
    display: flex;
    align-items: center;
    gap: 0.45rem;
    inline-size: 100%;
    min-block-size: 32px;
    padding: 0.25rem 0.5rem;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-primary);
    font-family: var(--font-sans);
    font-size: var(--ts-xs);
    font-weight: 600;
    text-align: start;
    cursor: pointer;
  }
  .v9-period-opt:hover:not(:disabled),
  .v9-period-opt:focus-visible:not(:disabled) {
    background: var(--bg-elevated);
    border-color: var(--border-subtle);
  }
  .v9-period-opt.is-active,
  .v9-period-opt[aria-checked="true"] {
    background: var(--bg-elevated);
    border-color: var(--accent-primary);
    color: var(--accent-primary);
  }
  .v9-period-opt:disabled { opacity: 0.44; cursor: not-allowed; }
  .v9-period-check { flex-shrink: 0; visibility: hidden; color: var(--accent-primary); }
  .v9-period-opt[aria-checked="true"] .v9-period-check { visibility: visible; }
  .v9-period-opt-title { flex: 1 1 auto; }
  .v9-period-opt-hint {
    font-family: var(--font-mono);
    font-size: 11px;
    color: var(--text-muted);
    font-weight: 500;
  }

  .v9-billing-kpis {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.45rem;
    margin-block-end: 0.65rem;
  }
  .v9-billing-kpi {
    padding: 0.45rem 0.55rem;
    background: var(--bg-elevated);
    border: 1px solid var(--border-hairline);
    border-radius: var(--radius-inner);
    min-inline-size: 0;
  }
  .v9-billing-lbl { font-size: 11px; color: var(--text-muted); font-weight: 600; }
  .v9-billing-val { font-size: var(--ts-md); font-weight: 700; color: var(--text-primary); line-height: 1.25; margin-block: 0.1rem; }
  .v9-billing-sub { font-family: var(--font-mono); font-size: 11px; color: var(--text-secondary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
}

@layer utilities {
  @media (max-width: 960px) {
    .v9-p-long { display: none; }
    .v9-p-short { display: inline; }
  }
  @media (min-width: 350px) and (max-width: 1099px) {
    .v9-stage .va-kpi-gauge-grid {
      grid-template-columns: repeat(2, minmax(0, 1fr));
    }
  }
  @media (max-width: 639px) {
    .v9-stage { inline-size: calc(100% - 1rem); }
    .v9-stage .v9-card { padding: 0.65rem; }
    .v9-stage .va-kpi-gauge-grid { gap: 0.45rem; }
    .v9-stage .va-kpi-gauge-grid .va-kpi-gauge-card { padding: 0.6rem 0.65rem; gap: 0.28rem; }
    .v9-stage .va-kpi-gauge-grid .va-kpi-gauge-val { font-size: clamp(1.08rem, 4.4vw, 1.35rem); }
    .v9-stage .va-kpi-gauge-grid .va-kpi-gauge-stats { font-size: 11px; gap: 0.15rem 0.45rem; }
    .v9-stage .v9-col-secondary { display: none; }
    .v9-stage .v9-table { min-inline-size: 0; }
    .v9-stage .v9-table th,
    .v9-stage .v9-table td { min-inline-size: 0; padding: 0.24rem 0.22rem; font-size: 11px; }
    .v9-stage .v9-table td:first-child .v9-cell-main { white-space: nowrap; word-break: keep-all; hyphens: none; }
    .v9-stage #v9ModelTableBody td:first-child .v9-cell-main { white-space: normal; }
    .v9-stage .v9-tbl-name-txt { max-inline-size: 108px; }
    .v9-stage button.v9-tbl-btn { min-block-size: 26px; min-inline-size: 0; padding: 0.12rem 0.34rem; font-size: 11px; }
    .v9-stage .v9-inline-share { min-inline-size: 0; gap: 0; }
    .v9-stage .v9-inline-share .va-inline-bar { display: none; }
    .v9-stage .v9-inline-pct { min-inline-size: auto; }
    .v9-stage .v9-heat-dow,
    .v9-stage .v9-heat-months,
    .v9-stage .v9-heat-grid { gap: 2px; }
    .v9-stage .v9-heat-dow { padding-block-start: calc(1.15rem + 2px); }
    .v9-stage .v9-heat-months { margin-block-end: 2px; }
    .v9-stage .v9-heat-dow > span:nth-child(even) { visibility: hidden; }
  }
  @media (max-width: 419px) {
    .v9-metric-full { display: none; }
    .v9-metric-short { display: inline; }
  }
}
