.grid-wrap {
  /* Double the default column width (token is 100px). */
  --cell-w: 200px;
  flex: 1;
  overflow: auto;
  position: relative;
  padding: 0 40px 60px 0;
}

.grid {
  border-collapse: separate;
  border-spacing: 0;
  position: relative;
  user-select: none;
}

.grid th, .grid td {
  border-right: 1px solid var(--color-border);
  border-bottom: 1px solid var(--color-border);
  height: var(--cell-h);
  padding: 0;
  margin: 0;
}

/* Corner + headers */
.grid thead th {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--color-header);
  color: var(--color-text-muted);
  font-weight: 600;
  font-size: 11px;
  text-align: center;
  min-width: var(--cell-w);
  width: var(--cell-w);
  cursor: pointer;
}

.grid thead th.corner {
  left: 0;
  z-index: 30;
  width: var(--gutter-w);
  min-width: var(--gutter-w);
}

.grid tbody th.row-head {
  position: sticky;
  left: 0;
  z-index: 15;
  background: var(--color-header);
  color: var(--color-text-muted);
  font-weight: 600;
  font-size: 11px;
  text-align: center;
  width: var(--gutter-w);
  min-width: var(--gutter-w);
  cursor: pointer;
}

.grid thead th.col-head.sel-head,
.grid tbody th.row-head.sel-head {
  background: var(--color-accent-soft);
  color: var(--color-accent);
}

.grid td.cell {
  background: var(--color-surface);
  min-width: var(--cell-w);
  width: var(--cell-w);
  font-size: 13px;
  position: relative;
}

.grid td.cell .cell-content {
  padding: 3px 6px;
  height: 100%;
  width: 100%;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  display: flex;
  align-items: center;
  font-variant-numeric: tabular-nums;
}

.grid td.cell.num .cell-content { justify-content: flex-end; }
.grid td.cell.error .cell-content { color: var(--color-danger); }

.grid td.cell.selected { background: var(--color-selection); }

.grid td.cell.active {
  outline: 2px solid var(--color-accent);
  outline-offset: -2px;
  z-index: 1;
  background: var(--color-surface);
}

.grid td.cell .cell-editor {
  position: absolute;
  inset: 0;
  z-index: 12;
  border: 2px solid var(--color-accent);
  background: var(--color-surface);
  color: var(--color-text);
  font-family: var(--mono);
  font-size: 13px;
  padding: 0 4px;
  outline: none;
}

/* Drag reorder visuals */
.grid th.dragging { opacity: 0.4; }

.drop-indicator {
  position: absolute;
  background: var(--color-accent-line);
  z-index: 40;
  pointer-events: none;
}
.drop-indicator.vertical { width: 2px; }
.drop-indicator.horizontal { height: 2px; }

.drag-ghost {
  position: fixed;
  pointer-events: none;
  z-index: 100;
  background: var(--color-accent-soft);
  border: 1px solid var(--color-accent);
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
  color: var(--color-accent);
  padding: 3px 8px;
  box-shadow: var(--shadow-md);
}

.grid td.cell .cell-content code {
  font-family: var(--mono);
  font-size: 12px;
  background: var(--color-surface-alt);
  border: 1px solid var(--color-border);
  border-radius: 4px;
  padding: 0 4px;
}
.grid td.cell .cell-content a { color: var(--color-accent); }
.grid td.cell .cell-content strong { font-weight: 700; }
.grid td.cell .cell-content del { opacity: 0.7; }

/* Distinct outline when the active cell holds a formula. */
.grid td.cell.active-formula {
  outline-color: #16a34a;
}
[data-theme="dark"] .grid td.cell.active-formula {
  outline-color: #4ade80;
}

/* Zebra striping (skips the active/selected highlight which override bg). */
.grid.zebra tbody tr:nth-child(even) td.cell { background: var(--color-surface-alt); }

/* Header row emphasis. */
.grid tr.header-row td.cell { font-weight: 600; background: var(--color-header); }

/* Freeze first data row: sticky just below the column-header row. */
.grid.freeze-row tbody tr.frozen-row td.cell,
.grid.freeze-row tbody tr.frozen-row th.row-head {
  position: sticky;
  top: var(--cell-h);
  z-index: 14;
}
.grid.freeze-row tbody tr.frozen-row th.row-head { z-index: 16; }

/* Freeze first column: sticky just right of the row-header gutter. */
.grid.freeze-col tbody td.cell.frozen-col {
  position: sticky;
  left: var(--gutter-w);
  z-index: 13;
}
/* Corner of the two freezes needs to win. */
.grid.freeze-row.freeze-col tbody tr.frozen-row td.cell.frozen-col { z-index: 17; }
