/* ══ Lists sheets (lists-sheet.js) ══
   A dense grid that reads like Excel, in SAG Graphite & Slate. Loaded after the
   density layer. With a mouse: 30px rows, 26px header, 26px row buttons.
   Fingers keep 44px targets. Status colours are for pills, text and dots,
   never a button fill; SAG yellow is not used here at all. Nothing is cut:
   cells never ellipsis, the grid scrolls inside its own box instead. */

.sh{--sh-row:30px;--sh-head:26px;--sh-max-h:calc(100vh - 200px);
  font-family:var(--sag-font);color:var(--sag-text);font-size:13px;line-height:1.35;position:relative;min-width:0}
.sh *,.sh *::before,.sh *::after{box-sizing:border-box}
.sh svg{width:14px;height:14px;flex:none}
.sh-sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}

/* ── Bar: chips left, export + search right ── */
.sh-bar{display:flex;flex-wrap:wrap;align-items:center;gap:8px 12px;padding:0 0 8px}
.sh-chips{display:flex;flex-wrap:wrap;gap:6px;flex:1 1 auto;min-width:0}
.sh-chip{display:inline-flex;align-items:center;gap:6px;height:26px;padding:0 10px;border-radius:var(--sag-r-sm,6px);
  border:1px solid var(--sag-line);background:var(--sag-surface);color:var(--sag-text-2);font:500 12.5px/1 var(--sag-font);
  cursor:pointer;white-space:nowrap}
.sh-chip b{font-weight:700;color:var(--sag-text);font-variant-numeric:tabular-nums}
.sh-chip.sh-ok b{color:var(--sag-ok-fg)}
.sh-chip.sh-wait b{color:var(--sag-wait-fg)}
.sh-chip.sh-risk b{color:var(--sag-risk-fg)}
.sh-chip.sh-idle b{color:var(--sag-idle-fg)}
.sh-chip:hover{background:var(--sag-hover);border-color:var(--sag-line-strong)}
.sh-chip[aria-pressed="true"]{background:var(--sag-primary);border-color:var(--sag-primary);color:var(--sag-on-primary)}
.sh-chip[aria-pressed="true"] b{color:var(--sag-on-primary)}
/* A chip with a tone is tinted whole in its family; chosen, it is the
   family's deep colour with white words (contrast above 4.5:1 in each). */
.sh-chip.sh-wait{background:var(--sag-wait-bg);border-color:color-mix(in srgb,var(--sag-wait) 30%,transparent);color:var(--sag-wait-fg)}
.sh-chip.sh-ok{background:var(--sag-ok-bg);border-color:color-mix(in srgb,var(--sag-ok) 30%,transparent);color:var(--sag-ok-fg)}
.sh-chip.sh-risk{background:var(--sag-risk-bg);border-color:color-mix(in srgb,var(--sag-risk) 30%,transparent);color:var(--sag-risk-fg)}
.sh-chip.sh-slate{background:var(--sag-slate-bg);border-color:color-mix(in srgb,var(--sag-slate) 30%,transparent);color:var(--sag-slate-fg)}
.sh-chip.sh-idle{background:var(--sag-idle-bg);border-color:color-mix(in srgb,var(--sag-idle) 30%,transparent);color:var(--sag-idle-fg)}
.sh-chip.sh-slate b{color:var(--sag-slate-fg)}
.sh-chip.sh-wait:hover,.sh-chip.sh-ok:hover,.sh-chip.sh-risk:hover,.sh-chip.sh-slate:hover,.sh-chip.sh-idle:hover{border-color:currentColor}
.sh-chip.sh-wait[aria-pressed="true"]{background:var(--sag-wait-fg);border-color:var(--sag-wait-fg)}
.sh-chip.sh-ok[aria-pressed="true"]{background:var(--sag-ok-fg);border-color:var(--sag-ok-fg)}
.sh-chip.sh-risk[aria-pressed="true"]{background:var(--sag-risk-fg);border-color:var(--sag-risk-fg)}
.sh-chip.sh-slate[aria-pressed="true"]{background:var(--sag-slate-fg);border-color:var(--sag-slate-fg)}
.sh-chip.sh-idle[aria-pressed="true"]{background:var(--sag-idle-fg);border-color:var(--sag-idle-fg)}
.sh-chip[class*="sh-"][aria-pressed="true"],.sh-chip[class*="sh-"][aria-pressed="true"] b{color:var(--sag-on-primary)}
.sh-tools{display:flex;align-items:center;gap:8px;margin-left:auto;flex:0 1 auto;min-width:0}
.sh-search{display:flex;align-items:center;gap:6px;height:30px;width:220px;max-width:100%;padding:0 10px;
  border:1px solid var(--sag-line-strong);border-radius:var(--sag-r,8px);background:var(--sag-surface);color:var(--sag-icon)}
.sh-search input{border:0;outline:0;background:transparent;font:13px var(--sag-font);color:var(--sag-text);width:100%;min-width:0;height:100%;padding:0}
.sh-search:focus-within{border-color:var(--sag-focus);box-shadow:0 0 0 1px var(--sag-focus)}
.sh-phone-sort{display:none}
/* "2 filters on · Clear": column filters are never silent, even on a column
   the screen has no room for. */
.sh-filters-on{display:inline-flex;align-items:center;gap:6px;height:26px;padding:0 10px;flex:none;border:1px solid var(--sag-line-strong);
  border-radius:var(--sag-r-sm,6px);background:var(--sag-surface);color:var(--sag-text-2);font:500 12.5px/1 var(--sag-font);cursor:pointer;white-space:nowrap}
.sh-filters-on svg{width:12px;height:12px;color:var(--sag-text)}
.sh-filters-on b{font-weight:700;color:var(--sag-text);text-decoration:underline;text-underline-offset:2px}
.sh-filters-on:hover{background:var(--sag-hover)}
.sh-filters-on:focus-visible{outline:2px solid var(--sag-focus);outline-offset:1px}

/* ── The grid ── */
.sh-scroll{overflow:auto;max-height:var(--sh-max-h);scroll-padding:var(--sh-head) 0 var(--sh-row);min-height:120px;background:var(--sag-surface);
  border:1px solid var(--sag-line);border-radius:var(--sag-r-lg,12px)}
.sh-grid{border-collapse:separate;border-spacing:0;min-width:100%;font-size:13px}
.sh-grid th,.sh-grid td{border-right:1px solid var(--sag-line);border-bottom:1px solid var(--sag-line);text-align:left}
.sh-grid tr > :last-child{border-right:0}
.sh-grid thead th{position:sticky;top:0;z-index:2;height:var(--sh-head);padding:0 4px 0 8px;background:var(--sag-app);
  color:var(--sag-text-2);font:600 12px/1.2 var(--sag-font);white-space:normal;vertical-align:middle}
.sh-hc{display:flex;align-items:center;gap:2px}
.sh-num .sh-hc{justify-content:flex-end}
.sh-hl{display:inline-flex;align-items:center;gap:3px;flex:1 1 auto;min-width:0;border:0;background:none;
  color:inherit;font:inherit;text-align:inherit;cursor:pointer;white-space:normal;min-height:22px;height:auto;padding:2px 0}
.sh-num .sh-hl{justify-content:flex-end}
span.sh-hl{cursor:default}
button.sh-hl:hover{color:var(--sag-text)}
.sh-arrow{display:inline-flex;color:var(--sag-text)}
.sh-arrow svg{width:12px;height:12px}
.sh-fbtn{display:inline-flex;align-items:center;justify-content:center;width:20px;height:20px;padding:0;flex:none;
  border:1px solid transparent;border-radius:4px;background:none;color:var(--sag-icon);cursor:pointer}
.sh-fbtn svg{width:12px;height:12px}
.sh-fbtn:hover{border-color:var(--sag-line-strong);background:var(--sag-surface);color:var(--sag-text-2)}
.sh-fbtn.on{background:var(--sag-primary);border-color:var(--sag-primary);color:var(--sag-on-primary)}
.sh-grid td{height:var(--sh-row);padding:2px 8px;white-space:nowrap;vertical-align:middle;background:var(--sag-surface);cursor:default}
/* Cells stay on one line like Excel; the `wrap` column (the long text) is the
   one that wraps when the sheet is narrow. Headers may take two lines. If the
   sheet is still wider than its box it scrolls inside the box: nothing is cut. */
.sh-grid td.sh-wrap{white-space:normal;min-width:180px}
@media (max-width:1100px){.sh-grid td.sh-wrap{min-width:120px}}
.sh-grid .sh-num{text-align:right;font-variant-numeric:tabular-nums}
.sh-grid .sh-rn{width:36px;min-width:36px;padding:0 4px;text-align:center;color:var(--sag-text-3);background:var(--sag-app);
  font-size:11.5px;font-variant-numeric:tabular-nums;cursor:default}
.sh-grid tbody tr:hover td:not(.sh-rn){background:var(--sag-hover)}
.sh-grid td.sh-acts{cursor:default;padding:1px 6px}
.sh-grid td.sh-acts > *{vertical-align:middle}
/* The selected cell, as in Excel: an inset ring and a light tint, shown only
   while the cell itself has focus. A button inside it carries its own ring, so
   a click on it never leaves two rings, and nothing stays ringed once the
   sheet has lost focus. sh-focus only remembers where Tab comes back to. */
.sh-grid td.sh-focus,.sh-grid td:focus{outline:0}
.sh .sh-grid tbody tr td:not(.sh-rn):focus{box-shadow:var(--sag-ring-inset);background:var(--sag-selected)}
.sh-grid td.sh-copied:focus{box-shadow:inset 0 0 0 2px var(--sag-primary);outline:1px dashed var(--sag-surface);outline-offset:-4px}
.sh-grid tfoot td,.sh-foot-t td{height:var(--sh-row);padding:3px 8px;background:var(--sag-app);
  border-top:2px solid var(--sag-primary);border-bottom:0;font-weight:700;white-space:nowrap;cursor:default}
/* Desktop: the totals show in a strip under the scrolling box, so no row is
   ever hidden behind them. The table's own tfoot stays in the layout only to
   size the columns (visibility:collapse takes no room). */
@media (min-width:721px){
  .sh-scroll .sh-grid tfoot tr{visibility:collapse}
  .sh-scroll:has(+ .sh-foot){border-bottom-left-radius:0;border-bottom-right-radius:0}
  .sh-foot{overflow:hidden;border:1px solid var(--sag-line);border-top:0;border-radius:0 0 var(--sag-r-lg,12px) var(--sag-r-lg,12px);
    background:var(--sag-app)}
  .sh-foot-t{table-layout:fixed;min-width:0}
  .sh-foot-t tbody tr:hover td:not(.sh-rn){background:var(--sag-app)}
}
.sh-tot-mark{font-size:11px;margin-left:1px;color:var(--sag-text-2)}
.sh-tot-notes{margin:6px 2px 0;font-size:12px;line-height:1.4;color:var(--sag-text-3);text-align:right}
.sh-n{font-weight:500;color:var(--sag-text-3);margin-left:6px;font-size:12px}
.sh-empty{padding:28px 16px;text-align:center;color:var(--sag-text-3)}
.sh-empty b{display:block;color:var(--sag-text);font-size:14px;margin-bottom:8px;font-weight:600}

/* ── Row buttons: one short word + icon, outlined, sized to their words ── */
.sh-act{display:inline-flex;align-items:center;justify-content:center;gap:5px;height:26px;width:auto;padding:0 9px;flex:none;
  border:1px solid var(--sag-line-strong);border-radius:var(--sag-r,8px);background:var(--sag-surface);color:var(--sag-text);
  font:600 12.5px/1 var(--sag-font);white-space:nowrap;cursor:pointer}
.sh-act svg{width:14px;height:14px;color:var(--sag-text-2)}
.sh-act:hover{background:var(--sag-hover);border-color:var(--sag-text-2)}
.sh-act-quiet{border-color:var(--sag-line);font-weight:500;color:var(--sag-text-2)}
.sh-act-primary{background:var(--sag-primary);border-color:var(--sag-primary);color:var(--sag-on-primary)}
.sh-act-primary:hover{background:var(--sag-primary);border-color:var(--sag-primary)}
.sh-ibtn{display:inline-flex;align-items:center;justify-content:center;width:26px;height:26px;padding:0;flex:none;margin-left:4px;
  border:1px solid var(--sag-line);border-radius:var(--sag-r,8px);background:var(--sag-surface);color:var(--sag-text-2);cursor:pointer}
.sh-ibtn:hover{background:var(--sag-hover);border-color:var(--sag-line-strong)}
.sh-act:focus-visible,.sh-ibtn:focus-visible,.sh-chip:focus-visible,.sh-fbtn:focus-visible,.sh-hl:focus-visible{
  outline:2px solid transparent;outline-offset:2px;box-shadow:var(--sag-ring)}
.sh-opt:focus-visible{outline:2px solid transparent;box-shadow:var(--sag-ring-inset)}

/* ── Status pill: tone family text + dot ── */
.sh-st{display:inline-flex;align-items:center;gap:5px;min-height:20px;padding:2px 7px;border-radius:var(--sag-r-sm,6px);
  font:600 11.5px/1.25 var(--sag-font);white-space:nowrap;background:var(--sag-idle-bg);color:var(--sag-idle-fg)}
.sh-st::before{content:"";width:6px;height:6px;border-radius:50%;background:var(--sag-idle);flex:none}
.sh-st.sh-ok{background:var(--sag-ok-bg);color:var(--sag-ok-fg)}
.sh-st.sh-ok::before{background:var(--sag-ok)}
.sh-st.sh-wait{background:var(--sag-wait-bg);color:var(--sag-wait-fg)}
.sh-st.sh-wait::before{background:var(--sag-wait)}
.sh-st.sh-risk{background:var(--sag-risk-bg);color:var(--sag-risk-fg)}
.sh-st.sh-risk::before{background:var(--sag-risk)}

/* ── AutoFilter popover ── */
.sh-pop{position:fixed;z-index:60;width:max-content;min-width:220px;max-width:min(340px,calc(100vw - 16px));
  padding:4px 0;background:var(--sag-surface);border-radius:var(--sag-r,8px);box-shadow:var(--sag-shadow-2);font:13px var(--sag-font);color:var(--sag-text)}
.sh-opt{display:flex;align-items:center;gap:8px;width:100%;min-height:28px;padding:4px 12px;border:0;background:none;
  color:inherit;font:inherit;text-align:left;cursor:pointer}
.sh-opt span{white-space:normal;overflow-wrap:anywhere}
.sh-opt:hover{background:var(--sag-hover)}
.sh-opt input{width:15px;height:15px;margin:0;accent-color:var(--sag-primary);flex:none}
.sh-sortopt svg{color:var(--sag-icon)}
.sh-sortopt + .sh-pop-sec,.sh-sortopt + .sh-vals{border-top:1px solid var(--sag-line);margin-top:4px;padding-top:4px}
.sh-pop-sec{padding:6px 10px}
.sh-pop-sec input{width:100%;height:28px;padding:0 8px;border:1px solid var(--sag-line-strong);border-radius:6px;font:13px var(--sag-font);
  background:var(--sag-surface);color:var(--sag-text)}
.sh-vals{max-height:220px;overflow:auto}
.sh-allopt{border-bottom:1px solid var(--sag-line)}
.sh-pop-foot{display:flex;flex-wrap:wrap;justify-content:flex-end;gap:6px;padding:8px 10px 4px;border-top:1px solid var(--sag-line);margin-top:4px}

/* ── Phone: each row becomes a compact card. What it is and its button first,
      then the other fields as label: value, wrapping. No sideways scroll. ── */
/* Group headings (spec.groups): a band across the sheet, the outer level
   stronger than the inner. Not a row: the keyboard and a click pass over it. */
.sh-grid tr.sh-group>th{height:auto;padding:10px 10px 6px;text-align:left;white-space:normal;background:var(--sag-app);
  font-size:13.5px;font-weight:750;color:var(--sag-text)}
.sh-grid tr.sh-group-1>th{padding:6px 10px 5px;font-size:12.5px;font-weight:650;color:var(--sag-text-2);background:var(--sag-surface)}
.sh-group-n{margin-left:10px;font-size:12px;font-weight:500;color:var(--sag-text-3);font-variant-numeric:tabular-nums}
@media (max-width:720px){
  .sh{--sh-max-h:none}
  .sh-bar{flex-direction:column;flex-wrap:nowrap;align-items:stretch}
  .sh-filters-on{align-self:flex-start}
  /* A sort menu as wide as the choice it shows, not as its longest option. */
  .sh-phone-sort{field-sizing:content;min-width:0}
  .sh-tools{margin-left:0;flex-wrap:wrap}
  .sh-search{flex:1 1 160px;width:auto}
  .sh-phone-sort{display:block;flex:0 1 auto;max-width:100%;height:30px;padding:0 28px 0 10px;border:1px solid var(--sag-line-strong);
    border-radius:var(--sag-r,8px);font:13px var(--sag-font);color:var(--sag-text);background-color:var(--sag-surface)}
  .sh-scroll{overflow:visible;max-height:none;min-height:0}
  .sh-grid,.sh-grid tbody,.sh-grid tfoot{display:block;min-width:0;width:100%}
  .sh-grid thead{display:none}
  .sh-grid tbody tr{display:grid;grid-template-columns:minmax(0,1fr) auto;column-gap:10px;row-gap:2px;padding:8px 12px;
    border-bottom:1px solid var(--sag-line)}
  .sh-grid tbody tr:hover td:not(.sh-rn){background:none}
  .sh-grid tbody tr.sh-group{display:block;padding:0;border-bottom:0}
  .sh-grid tr.sh-group>th{display:block;border-right:0}
  .sh-grid td{display:block;height:auto;min-width:0;padding:0;border:0;white-space:normal;overflow-wrap:anywhere;background:none;text-align:left}
  .sh-grid td.sh-wrap{min-width:0}
  .sh-grid .sh-rn{display:none}
  .sh-grid td:empty{display:none}
  .sh-grid td.sh-main{grid-column:1;grid-row:1;font-weight:600;font-size:14px;align-self:center;overflow-wrap:break-word}
  /* "Paid amount: —" on every waiting row is clutter: an empty value is left
     out of the card, unless it is a value that can be filled in there. */
  .sh-grid tbody td[data-empty]:not(:has(input,button,[data-catalogue-field])){display:none}
  /* The label sits on the same line as its status pill. */
  .sh-grid tbody td:not(.sh-main):not(.sh-acts):has(> .sh-st){display:flex;flex-wrap:wrap;align-items:center;gap:0 4px}
  .sh-grid td.sh-acts{grid-column:2;grid-row:1;padding:0;display:flex;flex-wrap:wrap;justify-content:flex-end;align-items:flex-start;gap:4px}
  .sh-grid tbody td:not(.sh-main):not(.sh-acts){grid-column:1 / -1;font-size:13px;color:var(--sag-text-2)}
  .sh-grid tbody td:not(.sh-main):not(.sh-acts)::before{content:attr(data-label) ": ";color:var(--sag-text-3);font-weight:500}
  .sh-grid td[data-label=""]::before{content:none}
  .sh-grid td.sh-num{text-align:left}
  .sh-grid tfoot tr{display:flex;flex-wrap:wrap;gap:4px 14px;padding:10px 12px;background:var(--sag-app);border-top:2px solid var(--sag-primary)}
  .sh-grid tfoot td{position:static;display:block;height:auto;padding:0;border:0;background:none;white-space:normal}
  .sh-grid tfoot td.sh-blank{display:none}
  .sh-grid tfoot td.sh-tot{text-align:left}
  .sh-grid tfoot td.sh-tot::before{content:attr(data-label) ": ";font-weight:500;color:var(--sag-text-3)}
  .sh-grid tfoot td.sh-tot-l{flex-basis:100%}
  .sh-grid tfoot tr{border-radius:0 0 11px 11px}
  .sh-tot-notes{text-align:left}
  .sh-foot{display:none}
}

/* ── Fingers: 44px targets wherever a row or control is tapped ── */
@media (pointer:coarse){
  .sh-chip{height:44px;padding:0 14px;font-size:14px}
  .sh-filters-on{height:44px;padding:0 14px;font-size:14px}
  .sh-search,.sh-phone-sort{height:44px}
  .sh-search input,.sh-phone-sort{font-size:16px}
  .sh-act{height:44px;padding:0 14px;font-size:14px}
  .sh-ibtn{width:44px;height:44px}
  .sh-fbtn{width:32px;height:32px}
  .sh-opt{min-height:44px}
  .sh-opt input{width:20px;height:20px}
}
