/* ===================== Design tokens ===================== */
:root {
  --bg: #eef0f5;
  --surface: #ffffff;
  --surface-2: #f7f8fb;
  --surface-3: #f0f2f8;
  --border: #e3e6ee;
  --border-strong: #cdd2df;
  --text: #161c2c;
  --text-dim: #545e75;
  --text-faint: #8b93a8;
  --accent: #107c41;
  --accent-hover: #0e6f3c;
  --accent-deep: #185c37;
  --accent-soft: #e2f0e8;
  --accent-line: #5aa77d;
  --row-hover: #f3f5f4;
  --row-active: #e9f3ee;
  --row-active-line: #107c41;
  --zebra: #fafafa;
  --row-num-bg: #f4f4f4;
  --row-num-text: #8a8f96;
  --row-num-active: #107c41;
  --row-num-active-bg: #e9f3ee;
  --row-add-bg: #f6f8f7;
  --row-add-text: #8b94a9;
  --danger: #e5484d;
  --topbar-bg: #f3f2f0;
  --topbar-bg-2: #ecebe8;
  --topbar-border: #dcd9d4;
  --topbar-text: #1f1f1f;
  --topbar-text-dim: #5c6168;
  --topbar-accent: #107c41;
  --topbar-hairline: rgba(0,0,0,.05);
  --topbar-chip-bg: rgba(0,0,0,.035);
  --topbar-chip-border: rgba(0,0,0,.10);
  --topbar-chip-hover: rgba(0,0,0,.06);
  --topbar-btn-bg: rgba(0,0,0,.03);
  --topbar-btn-border: rgba(0,0,0,.11);
  --topbar-btn-hover: rgba(0,0,0,.07);
  --shadow-1: 0 1px 2px rgba(16, 20, 36, .05), 0 1px 3px rgba(16, 20, 36, .07);
  --shadow-2: 0 12px 32px rgba(16, 20, 36, .14), 0 3px 8px rgba(16, 20, 36, .08);
  --radius: 10px;
  --radius-sm: 7px;
  --topbar-h: 56px;
  --toolbar-h: 46px;
  --fxbar-h: 28px;
  --statusbar-h: 34px;
  --font-ui: "Poppins", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-grid: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --font: var(--font-ui);
  /* grid surfaces (overridden by [data-theme="dark"]) */
  --head-grad-a: #f4f4f4; --head-grad-b: #ececeb; --head-border: #d7d6d4;
  --cell-input-bg: #fff;
  --thead-label-line: #107c41;
  --menu-bg: var(--surface);
  --menu-row-hover: var(--surface-2);
  --filter-check: var(--accent);
  --stats-stripe: var(--surface-2);
}
/* ===================== Dark theme — spec palette =====================
   #1E1E1E canvas · #141414 surface-low · #2D2D2D surface-high ·
   #3B82F6 accent · #264F78 selection · #E3E3E3 / #858585 text. */
[data-theme="dark"] {
  --bg: #1e1e1e;
  --surface: #141414;
  --surface-2: #2d2d2d;
  --surface-3: #333333;
  --border: #2b2b2b;
  --border-strong: #3a3a3a;
  --text: #e3e3e3;
  --text-dim: #858585;
  --text-faint: #6a6a6a;
  --accent: #21a366;
  --accent-hover: #28bd74;
  --accent-deep: #1b8a55;
  --accent-soft: #163b2c;
  --accent-line: #21a366;
  --row-hover: #242424;
  --row-active: #12352a;
  --row-active-line: #21a366;
  --zebra: #1b1b1b;
  --row-num-bg: #161616;
  --row-num-text: #858585;
  --row-num-active: #21a366;
  --row-num-active-bg: #12352a;
  --row-add-bg: #1c1c1c;
  --row-add-text: #858585;
  --danger: #ef5350;
  --shadow-1: 0 1px 2px rgba(0, 0, 0, .4), 0 1px 3px rgba(0, 0, 0, .5);
  --shadow-2: 0 12px 32px rgba(0, 0, 0, .55), 0 3px 8px rgba(0, 0, 0, .4);
  --head-grad-a: #262626; --head-grad-b: #1e1e1e; --head-border: #343434;
  --cell-input-bg: #101010;
  --menu-bg: #2d2d2d;
  --menu-row-hover: #333333;
  --stats-stripe: #2d2d2d;
  --topbar-bg: #2b2b2b;
  --topbar-bg-2: #242424;
  --topbar-border: #3c3c3c;
  --topbar-text: #e8e8e8;
  --topbar-text-dim: #9a9a9a;
  --topbar-accent: #21a366;
  --topbar-hairline: rgba(255,255,255,.10);
  --topbar-chip-bg: rgba(255,255,255,.05);
  --topbar-chip-border: rgba(255,255,255,.12);
  --topbar-chip-hover: rgba(255,255,255,.10);
  --topbar-btn-bg: rgba(255,255,255,.05);
  --topbar-btn-border: rgba(255,255,255,.12);
  --topbar-btn-hover: rgba(255,255,255,.11);
}
[data-theme="dark"] .topbar { border-bottom-color: #000; }
[data-theme="dark"] .toast { background: #e7eaf3; color: #10141f; border-color: rgba(0,0,0,.15); }
[data-theme="dark"] .toast.error { background: #ff8a8e; }
[data-theme="dark"] .th-actions button.danger:hover { background: #452226; }
[data-theme="dark"] ::-webkit-scrollbar-thumb { background: #39415a; }
/* theme-icon swap: light shows moon (tap → dark), dark shows sun (tap → light) */
#btnTheme .ico-sun { display: none; }
#btnTheme .ico-moon { display: inline-block; }
[data-theme="dark"] #btnTheme .ico-sun { display: inline-block; }
[data-theme="dark"] #btnTheme .ico-moon { display: none; }
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 14px/1.45 var(--font);
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overflow: hidden;
}
button, input, textarea, select { font: inherit; color: inherit; }
kbd {
  font: 11px/1.6 var(--mono);
  color: var(--text-dim);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 5px;
  padding: 1px 5px;
  margin: 0 1px;
}

#app {
  display: grid;
  grid-template-rows: var(--topbar-h) var(--toolbar-h) 1fr 1fr auto;
  grid-template-areas:
    "top"
    "toolbar"
    "grid"
    "grid"
    "status";
  height: 100vh;
}
/* simpler fixed layout */
#app { grid-template-rows: var(--topbar-h) var(--toolbar-h) var(--fxbar-h) 1fr var(--statusbar-h); grid-template-areas: "top" "toolbar" "formula" "grid" "status"; }

/* ===================== Top bar — VS Code-style command bar =====================
   Palette (user spec):  bg #1E1E1E · surface-low #141414 · surface-high #2D2D2D
   accent #3B82F6 · sel #264F78 · text #E3E3E3 · dim #858585. */
 .topbar {
   grid-area: top;
   position: relative;
   display: flex;
   align-items: center;
   gap: 14px;
   padding: 0 18px 0 20px;
   height: var(--topbar-h);
   background: var(--topbar-bg);
   border-bottom: 1px solid var(--topbar-border);
   z-index: 5;
   isolation: isolate;
   color: var(--topbar-text);
 }
 /* subtle top edge, theme-aware (a crisp 1px line instead of a glow) */
 .topbar::before {
   content: "";
   position: absolute;
   inset: 0 0 auto 0;
   height: 1px;
   background: linear-gradient(90deg, var(--topbar-hairline), transparent 45%);
   pointer-events: none;
 }

 /* accent rail — the Excel-green signature left edge */
 .accent-rail {
   position: absolute;
   left: 0; top: 0; bottom: 0;
   width: 3px;
   background: var(--accent);
 }
 .topbar-sep { width: 1px; height: 24px; background: var(--topbar-border); flex: none; }

/* Brand block */
 .brand { display: flex; align-items: center; gap: 11px; user-select: none; flex: none; }
 .logo {
   position: relative;
   display: grid;
   place-items: center;
   filter: drop-shadow(0 1px 3px rgba(16,124,65,.22));
 }
 .logo svg { display: block; width: 28px; height: 28px; }
 .brand-txt { display: flex; flex-direction: column; line-height: 1.18; }
 .brand-name {
   font-weight: 700;
   font-size: 15px;
   letter-spacing: -.2px;
   color: var(--topbar-text);
 }
 .brand-tag {
   font-size: 9.5px;
   font-weight: 650;
   letter-spacing: 1.6px;
   color: var(--topbar-text-dim, #858585);
   text-transform: uppercase;
 }

/* File identity — centered, pill + editable name + dirty dot */
.file-identity {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-width: 0;
}
.file-icon { color: var(--topbar-text-dim, #858585); flex: none; }
.filename {
  width: min(380px, 56vw);
  text-align: center;
  border: 1px solid var(--topbar-chip-border);
  background: var(--topbar-chip-bg);
  border-radius: 8px;
  padding: 6px 12px;
  font-weight: 600;
  font-size: 13px;
  color: var(--topbar-text);
  font-family: var(--mono);
  letter-spacing: .1px;
  outline: none;
  transition: border-color .14s, background .14s, box-shadow .14s;
}
.filename::placeholder { color: var(--topbar-text-dim, #858585); }
.filename:hover { background: var(--topbar-chip-hover); }
.filename:focus {
  border-color: var(--accent);
  background: var(--surface);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent);
}
.dirty-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: #fbbf24;
  flex: none;
  box-shadow: 0 0 0 3px rgba(251,191,36,.2);
  animation: pulse-dot 2s ease-in-out infinite;
}
@keyframes pulse-dot {
  0%, 100% { opacity: 1; box-shadow: 0 0 0 3px rgba(251,191,36,.2); }
  50% { opacity: .7; box-shadow: 0 0 0 5px rgba(251,191,36,.1); }
}

/* Grid-size chip — rows × cols (live, updated by renderStats) */
.grid-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  flex: none;
  padding: 4px 10px;
  border: 1px solid var(--topbar-chip-border);
  border-radius: 999px;
  background: var(--topbar-chip-bg);
  color: var(--topbar-text-dim, #858585);
  font: 11px/1 var(--mono);
  letter-spacing: .2px;
  white-space: nowrap;
  transition: border-color .14s, color .14s, background .14s;
}
.grid-chip .gc { color: var(--topbar-text); font-weight: 600; }
.grid-chip .gc-sep { color: var(--topbar-text-dim, #858585); opacity: .55; }
.grid-chip:hover { border-color: var(--topbar-chip-hover); }

/* Top actions — theme, open, export */
.top-actions { display: flex; align-items: center; gap: 8px; flex: none; }
.topbar .btn {
  background: var(--topbar-btn-bg);
  border: 1px solid var(--topbar-btn-border);
  color: var(--topbar-text);
  font-size: 13px;
  border-radius: 8px;
}
.topbar .btn:hover { background: var(--topbar-btn-hover); border-color: var(--topbar-btn-hover); }
.topbar .btn:active { transform: translateY(.5px); }
.topbar .icon-btn { width: 34px; min-width: 34px; height: 34px; padding: 0; justify-content: center; }
.topbar .btn-primary {
  background: var(--accent);
  border: 1px solid var(--accent);
  color: #fff;
  font-weight: 650;
  box-shadow: var(--shadow-1);
}
.topbar .btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.topbar .btn-primary:focus-visible { outline: none; box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 30%, transparent); }
.topbar .btn-primary .caret { border-color: rgba(255,255,255,.5); background: rgba(255,255,255,.18); }

/* ===================== Buttons ===================== */
.btn {
  -webkit-appearance: none;
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13.5px;
  font-weight: 600;
  letter-spacing: .1px;
  padding: 6px 12px;
  white-space: nowrap;
  user-select: none;
  transition: background .12s, border-color .12s, box-shadow .12s, transform .05s;
}
.btn:hover { background: var(--surface-2); border-color: var(--border-strong); }
.btn:active { transform: translateY(.5px); }
.btn:disabled { opacity: .45; cursor: not-allowed; transform: none; }
.btn:disabled:hover { background: var(--surface); border-color: var(--border); }
.btn:focus-visible { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; box-shadow: var(--shadow-1); }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-primary:focus-visible { box-shadow: 0 0 0 3px var(--accent-soft); }
.btn-danger { color: var(--danger); }
.toolbar .btn svg, .topbar .btn svg { opacity: .8; }
.toolbar {
  grid-area: toolbar;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 4;
}
.tools { display: flex; align-items: center; gap: 7px; flex-wrap: wrap; }
/* Formatting button group */
.fmt-group { display: inline-flex; align-items: center; gap: 4px; flex: none; }
.fmt-btn {
  padding: 5px 7px;
  min-width: 30px;
  border-radius: 7px;
  font-weight: 600;
  letter-spacing: 0;
}
.fmt-btn span { font-size: 12.5px; }
.fmt-btn svg { opacity: .85; }
/* toggle buttons reflect active state for the current selection */
.fmt-btn.on, .fmt-btn[aria-pressed="true"] {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent-line);
}

/* ===================== Formula bar — Excel-style name box + input ===================== */
.fxbar {
  grid-area: formula;
  display: flex;
  align-items: center;
  gap: 8px;
  height: var(--fxbar-h);
  padding: 0 14px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  z-index: 4;
}
.fx-name {
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.02em;
  min-width: 34px;
  text-align: center;
  color: var(--text-dim);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 6px;
  flex: none;
}
.fx-sep { width: 1px; height: 16px; background: var(--border); flex: none; }
.fx-glyph {
  font-family: Georgia, "Times New Roman", serif;
  font-style: italic;
  font-weight: 700;
  font-size: 15px;
  color: var(--text-faint);
  flex: none;
  padding: 0 2px 0 0;
}
.fx-input {
  flex: 1 1 auto;
  min-width: 0;
  font-family: var(--mono);
  font-size: 12.5px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 2px 8px;
  outline: none;
  line-height: 1.4;
}
.fx-input::placeholder { color: var(--text-faint); font-style: italic; font-family: var(--font-ui); }
.fx-input:hover { border-color: var(--border-strong); }
.fx-input:focus { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.sep { width: 1px; height: 20px; background: var(--border); margin: 0 3px; }

/* ===================== Search ===================== */
.search-wrap {
  position: relative;
  display: flex;
  align-items: center;
  width: min(320px, 46vw);
}
.search {
  width: 100%;
  padding: 6.5px 28px 6.5px 30px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  font-weight: 500;
  outline: none;
  transition: border-color .12s, background .12s, box-shadow .12s;
}
.search:focus { border-color: var(--accent-line); background: var(--surface); box-shadow: 0 0 0 3px var(--accent-soft); }
.search-icon { position: absolute; left: 10px; color: var(--text-faint); pointer-events: none; }
.search-clear {
  position: absolute; right: 6px;
  width: 18px; height: 18px;
  border: none; background: transparent;
  color: var(--text-faint);
  border-radius: 50%;
  cursor: pointer;
  font-size: 15px; line-height: 1;
  display: grid; place-items: center;
}
.search-clear:hover { background: var(--border); color: var(--text-dim); }

/* ===================== Grid area ===================== */
#dropzone {
  grid-area: grid;
  position: relative;
  overflow: hidden;
  background: var(--surface);
}
 .scroller {
   position: absolute;
   inset: 0;
   overflow: auto;
   overflow-anchor: none;
   font: 13px/1.45 var(--font-grid);
 }
table.grid {
   border-collapse: separate;
   border-spacing: 0;
   border-left: 1px solid var(--border);
   border-right: 1px solid var(--border);
   border-top: 1px solid var(--border);
   text-align: left;
   table-layout: fixed;
   /* The JS sets the exact width to the sum of column widths (Google-Sheets
      model: the grid is its columns, blank canvas beyond). The CSS here is a
      fallback and must NOT stretch past that. */
   width: auto;
   min-width: 0;
}

/* --- headers --- */
.grid thead th {
  position: sticky;
  top: 0;
  z-index: 3;
  background: linear-gradient(180deg, var(--head-grad-a) 0%, var(--head-grad-b) 100%);
  border-bottom: 1px solid var(--head-border);
  font-size: 11.5px;
  font-weight: 650;
  color: var(--text-dim);
  text-transform: none;
  letter-spacing: .3px;
  padding: 0;
  height: 34px;
  min-width: 96px;
  max-width: 260px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  vertical-align: middle;
}
.grid thead th.colnum {
  position: sticky;
  left: 0;
  z-index: 6;
  width: 46px;
  min-width: 46px;
  max-width: 46px;
  text-align: center;
  color: var(--text-faint);
  border-right: 1px solid var(--border);
}
.grid thead th .th-inner {
  position: relative;
  display: flex;
  align-items: center;
  height: 100%;
  padding: 0 2px 0 10px;
}
.grid thead th .th-label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
/* smart column-type badge (detected from cell values) */
.col-type-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 16px;
  height: 15px;
  padding: 0 4px;
  margin: 0 4px 0 2px;
  border-radius: 4px;
  font-size: 9.5px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  flex: none;
  letter-spacing: .2px;
  background: rgba(255,255,255,.55);
  border: 1px solid rgba(0,0,0,.05);
}
[data-theme="dark"] .col-type-badge { background: rgba(255,255,255,.08); border-color: rgba(255,255,255,.06); }
.col-type-badge.t-num   { color:#2563eb; background:rgba(59,130,246,.12);  border-color:rgba(59,130,246,.25); }
.col-type-badge.t-pct   { color:#0e9e8e; background:rgba(16,185,162,.14);  border-color:rgba(16,185,162,.3); }
.col-type-badge.t-cur   { color:#b45309; background:rgba(245,158,11,.14);  border-color:rgba(245,158,11,.3); }
.col-type-badge.t-date  { color:#0284c7; background:rgba(14,165,233,.14);  border-color:rgba(14,165,233,.3); }
.col-type-badge.t-time  { color:#7c3aed; background:rgba(124,58,237,.14); border-color:rgba(124,58,237,.3); }
.col-type-badge.t-bool  { color:#059669; background:rgba(16,185,129,.14); border-color:rgba(16,185,129,.3); }
.col-type-badge.t-text  { color:var(--text-dim); background:rgba(0,0,0,.06); border-color:rgba(0,0,0,.08); }
.col-type-badge.t-mix   { color:#c2410c; background:rgba(249,115,22,.14); border-color:rgba(249,115,22,.32); }
[data-theme="dark"] .col-type-badge.t-text { background: rgba(255,255,255,.07); border-color: rgba(255,255,255,.1); }
 .th-actions {
   position: absolute;
   right: 2px;
   display: flex;
   gap: 1px;
   opacity: 0;
   transition: opacity .12s;
 }
  /* The sort button is a single, persistent affordance (dimmed when the column is
     not sorted), kept visible unlike the other hover-only header actions. */
  th:hover .th-actions, th:focus-within .th-actions, th:has(.th-sort-toggle) .th-actions { opacity: 1; }
  .th-actions button {
    width: 22px; height: 22px;
    border: none;
    background: transparent;
    color: var(--text-faint);
    border-radius: 5px;
    cursor: pointer;
    display: grid; place-items: center;
    font-size: 13px;
    padding: 0;
    position: relative;
  }
  .th-actions button:hover { background: var(--border); color: var(--text); }
  /* Sort toggle: ONE state-aware icon. Dim & muted when unsorted; accent color with
     a direction arrow (A→Z up / Z→A down) once sorted. */
  th:is(.sorted-asc, .sorted-desc) .th-sort-toggle { color: var(--accent); }
  th.sorted-asc .th-sort-toggle:hover,
  th.sorted-desc .th-sort-toggle:hover { background: color-mix(in srgb, var(--accent) 16%, transparent); color: var(--accent); }
.th-addcol {
  width: 34px;
  min-width: 34px;
  max-width: 34px;
  position: sticky;
  right: 0;
  z-index: 4;
  background: var(--surface-2);
  border-left: 1px solid var(--border);
}
.th-actions button.danger:hover { background: #fbe9e9; color: var(--danger); }
.th-actions button[data-tip]::after {
  content: attr(data-tip);
  position: absolute;
  top: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: #262b36; color: #fff;
  font: 11.5px/1.3 var(--font);
  padding: 4px 8px;
  border-radius: 6px;
  white-space: nowrap;
  pointer-events: none;
  box-shadow: var(--shadow-2);
  z-index: 20;
}
/* Subtle full-header tint on the sorted column (the arrow itself carries the
   unsorted→asc→desc state in the toggle button on the right). */
th.sorted-asc, th.sorted-desc { color: var(--accent); }

/* --- row number cell --- */
.grid tbody td.rownum {
  position: sticky;
  left: 0;
  z-index: 2;
  background: var(--row-num-bg);
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  text-align: center;
  font: 500 11.5px var(--mono);
  font-variant-numeric: tabular-nums;
  color: var(--row-num-text);
  width: 46px;
  min-width: 46px;
  max-width: 46px;
  padding: 0 4px;
  user-select: none;
}
tr.active-row td.rownum {
  background: var(--row-num-active-bg);
  color: var(--row-num-active);
  border-left: 3px solid var(--row-active-line);
  padding-left: 1px;
}
tr.zebra td.rownum { background: var(--surface-2); }
tr.is-match td.rownum { background: var(--row-num-bg); }
tr.is-match.active-row td.rownum { background: var(--row-num-active-bg); }

/* --- data cells --- */
.grid tbody td.cell {
  border-bottom: 1px solid var(--border);
  padding: 0;
  height: 30px;
  background: var(--surface);
}
tr:not(.active-row):hover td.cell { background: var(--row-hover); }
tr.zebra td.cell { background: var(--zebra); }
tr.zebra:not(.active-row):hover td.cell { background: var(--row-hover); }
tr.active-row td.cell { background: var(--row-active); }

.cell .cell-display {
  display: block;
  padding: 5px 11px 5px 11px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}
.cell .cell-display.is-empty { color: var(--text-faint); font-style: italic; }

/* Conditional formatting — Google-Sheets style: numbers right-align & tabular,
   negatives get a red tint, dates left-align as text. */
.cell .cell-display.is-num { text-align: right; font-variant-numeric: tabular-nums; }
.cell .cell-display.is-date { text-align: left; }
td.cell.is-neg .cell-display { color: var(--danger); font-weight: 560; }
td.cell.is-neg .cell-input { color: var(--danger); font-weight: 560; }

/* ============ Cell formatting: bold / italic / underline / align / wrap ============ */
/* B/I/U — apply to both display span and edit input */
td.f-bold       .cell-display, td.f-bold .cell-input        { font-weight: 700; }
td.f-italic     .cell-display, td.f-italic .cell-input      { font-style: italic; }
td.f-underline  .cell-display, td.f-underline .cell-input   { text-decoration: underline; }
/* Alignment overrides on the display span */
td.f-align-left     .cell-display { text-align: left; }
td.f-align-center   .cell-display { text-align: center; }
td.f-align-right    .cell-display { text-align: right; font-variant-numeric: tabular-nums; }
/* Word wrap — normalise line breaking and let the row height follow */
td.f-wrap .cell-display { white-space: normal; overflow-wrap: break-word; word-break: break-word; }
/* Formula errors read like spreadsheet #REF! / #CYCLE! */
td.cell.is-err .cell-display {
  color: var(--danger);
  font-weight: 600;
  background: color-mix(in srgb, var(--danger) 10%, transparent);
}

/* Conditional formatting — colors come from inline --cf-bg / --cf-fg set by the rule */
td.cell.cf-on .cell-display {
  color: var(--cf-fg);
  background: var(--cf-bg);
}
td.cell.cf-on .cell-input {
  color: var(--cf-fg);
  background: var(--cf-bg);
}

/* in-cell editor */
.cell .cell-input {
  position: absolute;
  inset: -1px;
  width: 100%; height: 100%;
  border: none;
  outline: none;
  padding: 5px 11px;
  font: 13px var(--font-grid);
  background: var(--cell-input-bg);
  color: var(--text);
  box-shadow: inset 0 0 0 1.5px var(--accent);
  border-radius: 0;
}
.cell.editing { position: relative; z-index: 8; background: var(--cell-input-bg); }
tr.active-row .cell.editing { background: var(--cell-input-bg); }

/* --- row add button cells --- */
.row-add td {
  border-bottom: none;
  background: var(--row-add-bg);
}
.row-add td.rownum {
  color: var(--row-add-text);
  font-size: 15px;
  cursor: pointer;
}
.row-add td.rownum:hover { color: var(--accent); background: var(--accent-soft); }
.row-add .add-hint {
  padding: 0 11px;
  font-size: 12px;
  color: var(--row-add-text);
  white-space: nowrap;
  overflow: hidden;
}
.row-add td:first-child { cursor: pointer; }

/* --- per-row delete button (last column) --- */
.grid tbody td.rowdel {
  border-bottom: 1px solid var(--border);
  width: 34px;
  min-width: 34px;
  max-width: 34px;
  text-align: center;
  padding: 0;
  background: var(--surface);
}
tr:not(.active-row):hover td.rowdel { background: var(--row-hover); }
tr.zebra td.rowdel { background: var(--zebra); }
tr.zebra:not(.active-row):hover td.rowdel { background: var(--row-hover); }
tr.active-row td.rowdel { background: var(--row-active); }
.rowdel-btn {
  width: 20px; height: 20px;
  border: none;
  background: transparent;
  color: var(--text-faint);
  border-radius: 5px;
  cursor: pointer;
  display: grid; place-items: center;
  visibility: hidden;
}
tr:hover .rowdel-btn { visibility: visible; }
.rowdel-btn:hover { background: color-mix(in srgb, var(--danger) 18%, transparent); color: var(--danger); }

/* --- padding rows for virtualization --- */
tr.pad td { padding: 0 !important; border: none !important; background: transparent !important; }
tr.pad { height: 0; }

/* ===================== Empty state ===================== */
.empty {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  color: var(--text-dim);
  text-align: center;
  padding: 24px;
  background: radial-gradient(ellipse at top, var(--accent-soft) 0%, transparent 60%);
}
.empty svg { color: var(--accent); width: 88px; height: 88px; margin-bottom: 6px; filter: drop-shadow(0 8px 20px rgba(91,124,250,.15)); animation: float 4s ease-in-out infinite; }
@keyframes float {
  0%,100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}
.empty h2 { margin: 4px 0 0; font-size: 22px; font-weight: 700; color: var(--text); letter-spacing: -.2px; }
.empty p { margin: 0; max-width: 380px; font-size: 14px; color: var(--text-dim); }
.empty-actions { display: flex; gap: 10px; margin-top: 18px; }
.empty-foot { margin-top: 22px; font-size: 11.5px; color: var(--text-faint); font-weight: 600; letter-spacing: .3px; }
.empty-foot::before, .empty-foot::after { content: ""; display: inline-block; width: 20px; height: 1px; background: var(--border); vertical-align: middle; margin: 0 10px; }

/* ===================== Drag overlay ===================== */
.drag-overlay {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  background: rgba(244, 245, 248, .86);
  backdrop-filter: blur(2px);
  border: 3px dashed var(--accent);
  border-radius: 4px;
}
.drag-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  color: var(--accent);
  padding: 34px 46px;
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow-2);
}
.drag-box p { margin: 0; font-weight: 600; font-size: 15px; }

/* ===================== Status bar ===================== */
.statusbar {
  grid-area: status;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 14px;
  background: var(--surface-2);
  border-top: 1px solid var(--border);
  font-size: 12.5px;
  font-weight: 500;
  color: var(--text-dim);
  z-index: 5;
}
/* Excel-style sheet tab — active sheet, Excel-green underline */
.sheet-tab {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 24px;
  padding: 0 14px 0 10px;
  margin-left: -14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom: 2px solid var(--accent);
  border-radius: 3px 3px 0 0;
  color: var(--text);
  font-size: 12.5px;
  font-weight: 600;
  flex: none;
  user-select: none;
}
.sheet-icon {
  width: 12px; height: 12px;
  flex: none;
  color: var(--accent);
  opacity: .9;
}

/* ============ Freeze panes ============ */
/* Pinned top rows: keep their borders crisp when free rows scroll underneath.
   The inline `position:sticky;top:Npx;z-index:3` (set in rowHtml) does the
   pinning; this block supplies the visual cue that the row is frozen. */
tr.frozen-row { will-change: transform; }
tr.frozen-row td.cell, tr.frozen-row td.rownum, tr.frozen-row td.rowdel {
  box-shadow: inset 0 -1px 0 var(--border-strong);
}
/* Freeze chip in the status bar (only visible while frozen) */
.freeze-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 10px;
  background: color-mix(in srgb, var(--accent) 10%, transparent);
  color: var(--accent-deep, var(--accent));
  border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent);
  border-radius: 999px;
  font-size: 11.5px; font-weight: 650;
  flex: none;
}
[data-theme="dark"] .freeze-chip { color: var(--accent-hover); }
/* Freeze button active state */
#btnFreeze.on {
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  border-color: color-mix(in srgb, var(--accent) 35%, transparent);
  color: var(--accent-deep, var(--accent));
}
[data-theme="dark"] #btnFreeze.on { color: var(--accent-hover); }
.stat-left, .status-right { display: flex; align-items: center; gap: 12px; }
.stat.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }
#statSort { color: var(--accent); font-weight: 600; }
#statSearch { background: var(--accent-soft); color: var(--accent); padding: 2px 9px; border-radius: 999px; font-weight: 600; }
.kbd-hints { color: var(--text-faint); }
@media (max-width: 760px) { .kbd-hints { display: none; } }

/* ===================== Toast ===================== */
.toast {
  position: fixed;
  left: 50%;
  bottom: 48px;
  transform: translateX(-50%) translateY(12px);
  background: #16191f;
  color: #fff;
  padding: 10px 18px;
  border: 1px solid rgba(255,255,255,.06);
  border-radius: 10px;
  box-shadow: var(--shadow-2);
  font-size: 13.5px;
  font-weight: 500;
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s, transform .18s;
  z-index: 50;
  max-width: 86vw;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { background: #b32b2f; }
.toast.ok { background: #148a52; }

/* ===================== Modal ===================== */
.modal-root {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  place-items: center;
  background: rgba(20, 24, 40, .42);
  backdrop-filter: blur(1.5px);
}
.modal-card {
  width: min(400px, 90vw);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px 20px 16px;
  box-shadow: var(--shadow-2);
  color: var(--text);
}
.modal-card h3 { margin: 0 0 14px; font-size: 15px; font-weight: 650; letter-spacing: .1px; color: var(--text); }
.modal-card p { margin: 9px 0 0; color: var(--text-dim); font-size: 14px; }
.modal-card p .mono { font-family: var(--mono); color: var(--accent); font-weight: 600; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; flex-wrap: wrap; }

/* ---------- Shortcuts (?) overlay ---------- */
.help-card { width: min(680px, 94vw); }
.help-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.help-head h3 { margin: 0 0 12px; }
.help-close { flex: none; }
.help-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; }
@media (max-width: 640px) { .help-grid { grid-template-columns: repeat(2, 1fr); } }
.help-group { display: flex; flex-direction: column; gap: 6px; }
.help-label {
  font-size: 10.5px; font-weight: 700; letter-spacing: .8px; text-transform: uppercase;
  color: var(--text-faint); margin-bottom: 2px;
}
.help-item { display: flex; align-items: center; gap: 9px; font-size: 13px; color: var(--text-dim); min-width: 0; }
.help-item span { flex: 1 1 auto; min-width: 0; }
.help-item kbd {
  flex: none; font-family: var(--mono); font-size: 11px; font-weight: 600;
  color: var(--text); background: var(--surface-2);
  border: 1px solid var(--border); border-bottom-width: 2px;
  border-radius: 6px; padding: 2px 7px; white-space: nowrap;
}
.help-head h3 { font-size: 16px; font-weight: 700; }

/* ---------- Chart card ---------- */
.chart-card { width: min(560px, 94vw); padding: 18px 18px 14px; }
.chart-card h3 { margin-bottom: 6px; }
.chart-hint { font-size: 12.5px; color: var(--text-faint); margin: 0 0 12px; }
.chart-types { display: flex; gap: 6px; margin-bottom: 12px; }
.chart-type {
  -webkit-appearance: none; appearance: none;
  flex: 1;
  display: flex; align-items: center; justify-content: center; gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font-size: 13px; font-weight: 550;
  cursor: pointer;
}
.chart-type input { -webkit-appearance: none; appearance: none; width: 0; height: 0; border: 0; margin: 0; }
.chart-type.on, .chart-type:has(input:checked) {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 10%, transparent);
  color: var(--accent);
}
.chart-canvas-wrap {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  display: grid;
  place-items: center;
  padding: 6px;
  transition: opacity .15s ease;
}
.chart-canvas-wrap canvas { display: block; max-width: 100%; }

/* ---------- Floating chart ---------- */
.chart-float {
  position: fixed;
  z-index: 70;
  right: 14px;
  top: 14px;
  width: 512px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow-2);
  overflow: hidden;
}
.chart-float-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 12px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  font-size: 12.5px; font-weight: 700; color: var(--text);
}
.chart-float-head span { display: flex; align-items: center; gap: 4px; }
.chart-float-title { letter-spacing: .2px; }
.chart-float-body {
  padding: 8px;
  display: grid; place-items: center;
}
.chart-float-body canvas { display: block; }

/* ---------- Conditional formatting card ---------- */
.cf-card { width: min(460px, 94vw); padding: 18px 18px 14px; }
.cf-card h3 { margin-bottom: 4px; }
.cf-hint { font-size: 12.5px; color: var(--text-faint); margin: 0 0 14px; }
.cf-grid { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
.cf-field { display: flex; flex-direction: column; gap: 5px; flex: 1 1 120px; }
.cf-field > span { font-size: 11.5px; font-weight: 600; color: var(--text-faint); }
.cf-field select, .cf-field input {
  font-size: 13px; font-weight: 500; padding: 7px 9px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text); font-family: inherit; outline: none;
}
.cf-field select:focus, .cf-field input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 12%, transparent); }
.cf-between { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.cf-between-lab { font-size: 12px; color: var(--text-faint); font-weight: 600; }
.cf-between input { font-size: 13px; font-weight: 500; padding: 7px 9px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--text); }
.cf-between span { font-size: 12px; color: var(--text-faint); }
.cf-swatches { display: flex; gap: 8px; margin-bottom: 14px; }
.cf-sw { -webkit-appearance: none; appearance: none; flex: 1; display: grid; place-items: center; padding: 4px; border: 1px solid var(--border); border-radius: 9px; cursor: pointer; background: var(--surface); }
.cf-sw input { position: absolute; width: 0; height: 0; opacity: 0; }
.cf-sw.on, .cf-sw:has(input:checked) { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 12%, transparent); }
.cf-swatch { display: inline-grid; place-items: center; width: 26px; height: 26px; border-radius: 7px; font-weight: 700; font-size: 13px; }
.cf-list { list-style: none; margin: 0 0 4px; padding: 0; display: flex; flex-direction: column; gap: 6px; max-height: 200px; overflow-y: auto; }
.cf-list li { display: flex; align-items: center; gap: 9px; padding: 7px 10px; border: 1px solid var(--border); border-radius: 9px; background: var(--surface); font-size: 13px; }
.cf-list .cf-dot { width: 16px; height: 16px; border-radius: 5px; flex: none; }
.cf-list .cf-rule-txt { flex: 1; }
.cf-list .cf-none { color: var(--text-faint); font-size: 12.5px; border-style: dashed; }
.cf-list .button { border: none; background: transparent; color: var(--text-faint); font-size: 16px; cursor: pointer; border-radius: 6px; padding: 0 4px; line-height: 1; }
.cf-list .button:hover { color: var(--danger); background: color-mix(in srgb, var(--danger) 10%, transparent); }

/* ---------- Find & Replace card ---------- */
.find-card { width: min(420px, 90vw); padding: 18px 18px 14px; }
.find-card h3 { margin-bottom: 12px; }
.find-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 9px;
}
.find-row > span {
  flex: none;
  width: 88px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-dim);
  letter-spacing: .1px;
}
.find-row input {
  flex: 1;
  min-width: 0;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2, #f4f5f9);
  color: var(--text);
  font-size: 13px;
  font-family: var(--font-ui, sans-serif);
  outline: none;
  transition: border-color .12s, box-shadow .12s, background .12s;
}
.find-row input::placeholder { color: var(--text-faint, #9aa2b7); }
.find-row input:hover { border-color: var(--border-strong); }
.find-row input:focus {
  border-color: var(--accent);
  background: var(--surface);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.find-options {
  display: flex;
  gap: 14px;
  margin: 2px 0 10px;
  padding-left: 98px;
}
.find-check {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  font-weight: 500;
  color: var(--text-dim);
  cursor: pointer;
  user-select: none;
}
.find-check input[type="checkbox"] {
  width: 14px; height: 14px;
  accent-color: var(--accent);
  cursor: pointer;
}
.find-meta {
  font-size: 12px;
  color: var(--text-faint, #8b93a8);
  margin: 0 0 4px;
  padding-left: 98px;
  line-height: 1.45;
}
.find-card .modal-actions { margin-top: 14px; }

/* ===================== Popovers / menus ===================== */
.popover {
  position: fixed;
  z-index: 70;
  min-width: 210px;
  background: var(--menu-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow-2);
  padding: 5px;
  animation: pop-in .13s ease-out;
}
/* context menu separator & danger rows */
.popover .ctx-sep, .menu-sep {
  height: 1px;
  margin: 4px 6px;
  background: var(--border);
  border: none;
}
.popover button.danger { color: var(--danger); }
.popover button.danger:hover { background: color-mix(in srgb, var(--danger) 12%, transparent); }
@keyframes pop-in {
  from { opacity: 0; transform: translateY(4px) scale(.985); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}
.popover .menu-label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .5px;
  text-transform: uppercase;
  color: var(--text-faint);
  padding: 6px 10px 4px;
}
.popover button {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  border: none;
  background: transparent;
  color: var(--text);
  font: 600 13px var(--font);
  padding: 8px 10px;
  border-radius: 7px;
  cursor: pointer;
  text-align: left;
  line-height: 1.35;
}
.popover button:hover { background: var(--menu-row-hover); }
.popover button:focus-visible { outline: none; box-shadow: 0 0 0 2px var(--accent-soft); }
.popover button kbd { margin-left: auto; font-size: 10px; opacity: .75; }
.popover .dim { color: var(--text-faint); font-weight: 500; }

/* ---------- Hierarchical (two-pane) context menu ---------- */
/* Left: a narrow category rail. Right: the item panel for the active category. */
#ctxMenu.ctx-hier {
  display: flex;
  min-width: 0;
  width: 300px;
  gap: 2px;
  padding: 6px;
  align-items: stretch;
}
#ctxMenu.ctx-hier .ctx-rail {
  display: flex;
  flex-direction: column;
  gap: 2px;
  width: 104px;
  flex: none;
  border-right: 1px solid var(--border);
  padding-right: 4px;
  margin-right: 4px;
}
#ctxMenu.ctx-hier .ctx-cat {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  width: 100%;
  border: none;
  background: transparent;
  color: var(--text-dim);
  font: 600 12.5px var(--font);
  padding: 7px 9px;
  border-radius: 7px;
  cursor: pointer;
  text-align: left;
  white-space: nowrap;
}
#ctxMenu.ctx-hier .ctx-cat:hover { background: var(--menu-row-hover); color: var(--text); }
#ctxMenu.ctx-hier .ctx-cat:focus-visible { outline: none; box-shadow: 0 0 0 2px var(--accent-soft); }
#ctxMenu.ctx-hier .ctx-cat.ctx-active { background: var(--accent-soft); color: var(--accent); }
[data-theme="dark"] #ctxMenu.ctx-hier .ctx-cat.ctx-active { color: var(--accent-hover); }
/* chevron affordance on rail items that open a panel */
#ctxMenu.ctx-hier .ctx-cat::after {
  content: "";
  width: 5px; height: 5px;
  border-top: 1.5px solid currentColor;
  border-right: 1.5px solid currentColor;
  transform: rotate(45deg);
  opacity: .55;
  flex: none;
}
#ctxMenu.ctx-hier .ctx-panels { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; }
#ctxMenu.ctx-hier .ctx-panel {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}
#ctxMenu.ctx-hier .ctx-panel[hidden] { display: none; }
#ctxMenu.ctx-hier .ctx-panel > button { padding: 7px 9px; }
#ctxMenu.ctx-hier .ctx-sep { margin: 4px 4px; }
.export-wrap, .tools-wrap { position: static; display: inline-flex; }

/* icon-only button (theme toggle) */
.icon-btn { padding: 6px 8px; }

/* ===================== Column resize handles ===================== */
.th-inner { position: relative; }
/* Full 12px strip sits INSIDE the current column (right edge = column edge),
   so nothing overlaps the next header and the whole strip is grabbable. */
.col-resize {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 12px;
  cursor: col-resize;
  z-index: 7;
}
.col-resize::after {
  content: "";
  position: absolute;
  right: 1px;
  top: 25%;
  bottom: 25%;
  width: 1px;
  background: currentColor;
  opacity: 0;
  transition: opacity .12s;
}
.grid thead th:hover .col-resize::after { opacity: .5; }
.grid thead th .col-resize:hover::after { opacity: .8; }
.grid thead th .col-resize::before {
  content: "";
  position: absolute;
  right: 3px;
  top: 50%;
  transform: translateY(-50%);
  width: 6px;
  height: 14px;
  border-radius: 3px;
  background: transparent;
  opacity: 0;
  transition: opacity .12s, background .12s;
}
.grid thead th:hover .col-resize::before,
.grid thead th .col-resize:hover::before {
  opacity: .22;
  background: var(--text-dim);
}
body.user-select-none { user-select: none !important; }
/* light column separators in the data grid */
.grid tbody td.cell:not(:last-of-type) { border-right: 1px solid #eef1f7; }
[data-theme="dark"] .grid tbody td.cell:not(:last-of-type) { border-right-color: #2b3348; }

/* ===================== Selection (Sheets-style) ===================== */
td.cell.sel-cell {
  background: var(--accent-soft) !important;
  box-shadow: inset 0 0 0 1px var(--accent-line);
}
[data-theme="dark"] td.cell.sel-cell { background: rgba(33,163,102,.20) !important; }
td.rownum.sel-row { background: var(--row-num-active-bg) !important; color: var(--row-num-active); font-weight: 700; }
tr.row-sel td.cell { background: var(--accent-soft) !important; }
th.col-sel { background: linear-gradient(180deg, var(--accent-soft), var(--head-grad-b)) !important; color: var(--accent); }
th.col-sel .th-label { color: var(--accent); }

/* ===================== Selection stats bar ===================== */
.sel-stats {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 2px 8px;
  background: var(--accent-soft);
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  max-width: 100%;
  overflow: hidden;
}
.sel-stats .sel-n { font-variant-numeric: tabular-nums; opacity: .8; }
.sel-stats .sel-item {
  cursor: pointer;
  padding: 2px 7px;
  background: var(--surface);
  border-radius: 999px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  transition: filter .1s;
}
.sel-stats .sel-item:hover { filter: brightness(.97); }
.sel-stats .sel-item:active { filter: brightness(.92); }
/* type-specific stat chips: non-numeric & meta counts sit slightly dimmer */
.sel-stats .sel-item.text { color: var(--text-dim); background: var(--surface-2); }
.sel-stats .sel-sigma, .sel-stats .sel-act {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: 600 11.5px var(--font);
  border-radius: 999px;
  padding: 3px 9px;
  cursor: pointer;
  white-space: nowrap;
}
.sel-stats .sel-sigma:hover, .sel-stats .sel-act:hover { background: var(--surface-2); border-color: var(--border-strong); }
.sel-stats .sel-act.danger { color: var(--danger); }
.sel-stats .sel-act.danger:hover { background: color-mix(in srgb, var(--danger) 12%, transparent); }

/* split export button */
.export-wrap { display: inline-flex; }
.export-wrap #btnExport { border-top-right-radius: 0; border-bottom-right-radius: 0; }
.export-wrap .caret { border-top-left-radius: 0; border-bottom-left-radius: 0; border-left: 1px solid var(--accent-deep); }
.copyas-wrap { display: inline-flex; }
.copyas-wrap .caret { border-left: none; }
.copyas-wrap #copyAsMenu[hidden] { display: none; }
.copyas-wrap #copyAsMenu { right: auto; left: 0; }
.tools-wrap .caret { border-left-color: var(--border-strong); background: var(--surface); }

/* ===================== Column filter button ===================== */
.grid thead th .th-filter {
  position: absolute;
  left: 2px;
  top: 50%;
  transform: translateY(-50%);
  width: 20px; height: 22px;
  z-index: 4;
  border: none;
  background: transparent;
  color: var(--text-faint);
  border-radius: 5px;
  cursor: pointer;
  display: grid; place-items: center;
  padding: 0;
}
.grid thead th .th-filter:hover { background: var(--border); color: var(--text); }
.grid thead th .th-filter.on { color: var(--accent); opacity: 1 !important; }
.grid thead th.has-filter .th-label { color: var(--accent); }
.grid thead th .th-inner { padding-left: 2px; }
.grid thead th .th-label { margin-left: 24px; }

/* ===================== Filter panel ===================== */
.filter-root {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: grid;
  place-items: start center;
  background: transparent;
}
.filter-card {
  position: fixed;
  width: min(360px, 92vw);
  max-height: min(62vh, 560px);
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow-2);
  animation: pop-in .14s ease-out;
  overflow: hidden;
}
.filter-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  padding: 14px 16px 10px;
  border-bottom: 1px solid var(--border);
}
.filter-title { font-size: 15px; font-weight: 650; color: var(--text); }
.filter-sub { font-size: 11.5px; color: var(--text-faint); margin-top: 2px; font-weight: 500; }
.filter-body {
  overflow-y: auto;
  padding: 6px 6px;
  flex: 1;
  min-height: 60px;
}
.filter-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 9px;
  border-radius: 7px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
}
.filter-item:hover { background: var(--surface-2); }
.filter-item input[type="checkbox"] {
  width: 15px; height: 15px;
  accent-color: var(--filter-check);
  flex: none;
  margin: 0;
}
.filter-item .filter-value {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.filter-item .dim { font-size: 11.5px; flex: none; }
.filter-item.disabled { opacity: .4; cursor: not-allowed; }
.filter-truncate { font-size: 11.5px; color: var(--text-faint); padding: 8px 10px; text-align: center; }
.filter-foot {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-top: 1px solid var(--border);
  background: var(--surface-2);
}
.filter-live {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-faint);
  margin-right: auto;
  font-variant-numeric: tabular-nums;
}
.filter-live[aria-hidden="true"]:empty { display: none; }

.filter-mode-opt {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  border-radius: 7px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 550;
  color: var(--text);
  user-select: none;
}
.filter-mode-opt:hover { background: var(--surface-2); }
.filter-mode-opt input[type="radio"] {
  accent-color: var(--filter-check);
  margin: 0;
  width: 14px; height: 14px;
}
.filter-mode-opt.on { background: color-mix(in srgb, var(--filter-check) 12%, transparent); }
.filter-mode-opt.on span { font-weight: 700; color: var(--filter-check); }
.filter-mode-opt + .filter-mode-opt { margin-top: 1px; }
.filter-input-text {
  padding: 10px 8px 6px;
}
.filter-input-text input[type="text"] {
  width: 100%;
  font-size: 13.5px;
  font-weight: 500;
  padding: 7px 10px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  outline: none;
}
.filter-input-text input[type="text"]:focus {
  border-color: var(--filter-check);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--filter-check) 14%, transparent);
}
.filter-input-text .dim {
  font-size: 12.5px;
  color: var(--text-faint);
  display: block;
  padding: 4px 2px;
}
.filter-input-values { padding: 2px 0 0; }
.filter-nc { flex: none; }

/* Values-mode search: a small bar above the value list that narrows shown rows */
.filter-searchbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 8px 6px;
  border-bottom: 1px solid var(--border);
}
.filter-searchbar input[type="text"] {
  flex: 1;
  min-width: 0;
  font-size: 13px;
  font-weight: 500;
  padding: 6px 10px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}
.filter-searchbar input[type="text"]:focus { outline: none; border-color: var(--filter-check); }
.filter-search-meta { font-size: 11px; flex: none; }
.filter-item.hide { display: none; }

/* ===================== Stats card ===================== */
.stats-card { width: min(600px, 94vw); max-height: min(80vh, 640px); display: flex; flex-direction: column; }
.stats-body { overflow-y: auto; margin-top: 14px; flex: 1; }
.stats-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}
.stats-table th, .stats-table td {
  text-align: left;
  padding: 7px 10px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.stats-table th {
  position: sticky; top: 0;
  background: var(--surface-2);
  font-size: 11px;
  font-weight: 650;
  letter-spacing: .5px;
  text-transform: uppercase;
  color: var(--text-faint);
  z-index: 1;
}
.stats-table td { color: var(--text); font-family: var(--mono); font-size: 12.5px; }
.stats-table td.name { font-family: var(--font); font-weight: 600; max-width: 220px; overflow: hidden; text-overflow: ellipsis; }
.stats-table tbody tr:nth-child(even) td { background: var(--stats-stripe); }

/* ===================== Status bar filter chip ===================== */
.filter-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--accent-soft);
  color: var(--accent);
  padding: 2px 9px;
  border-radius: 999px;
  font-weight: 600;
  cursor: pointer;
  max-width: 60vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.filter-chip svg { flex: none; }
.filter-clear {
  width: 20px; height: 20px;
  border: none; background: transparent;
  color: var(--text-faint);
  border-radius: 50%;
  cursor: pointer;
  font-size: 16px; line-height: 1;
  display: grid; place-items: center;
}
.filter-clear:hover { background: var(--border); color: var(--text); }

/* ===================== Empty-state pills ===================== */
.empty-pills { display: flex; flex-wrap: wrap; gap: 7px; justify-content: center; margin-top: 2px; max-width: 420px; }
.empty-pill {
  font-size: 11.5px;
  font-weight: 600;
  color: var(--text-dim);
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 4px 11px;
  border-radius: 999px;
  letter-spacing: .2px;
}

/* ===================== Scrollbars ===================== */
.scroller::-webkit-scrollbar { width: 12px; height: 12px; }
.scroller::-webkit-scrollbar-thumb { background: #cdd2de; border-radius: 8px; border: 3px solid var(--surface); }
.scroller::-webkit-scrollbar-thumb:hover { background: #b7bdcd; }
.scroller::-webkit-scrollbar-corner { background: transparent; }
.scroller { scrollbar-color: #cdd2de var(--surface); }

/* Accessible, always-visible focus indicator. Catches any focusable element
   that still relies on the browser default, in BOTH themes. Components that
   define their own `outline:none` + box-shadow are more specific and keep
   their bespoke ring, so this acts only as a safety net. */
a:focus-visible,
button:focus-visible,
[tabindex]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 3px;
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

/* `hidden` attribute must always win, even where we set an explicit display */
[hidden] { display: none !important; }
