/* md-tabulate — Notion-flavoured UI
 * --------------------------------------------------------------------------
 * Tokens live in :root (light) and [data-theme="dark"]. Everything else
 * references the variables, so light/dark stay in lockstep automatically.
 */

:root {
  /* Surfaces */
  --bg: #ffffff;
  --bg-subtle: #f7f7f5;
  --bg-soft: #f1f1ef;
  --bg-elev: #ffffff;
  --bg-hover: rgba(55, 53, 47, 0.06);
  --bg-active: rgba(35, 131, 226, 0.08);
  --bg-backdrop: rgba(15, 15, 15, 0.36);

  /* Text */
  --text: #37352f;
  --text-soft: #6b6a64;
  --text-dim: #9b9a97;
  --text-on-accent: #ffffff;

  /* Lines */
  --border: #e9e9e7;
  --border-strong: #d3d3d0;
  --border-focus: #2383e2;

  /* Accents */
  --accent: #2383e2;
  --accent-hover: #1a6dc1;
  --accent-soft: rgba(35, 131, 226, 0.12);
  --accent-soft-strong: rgba(35, 131, 226, 0.18);

  /* States */
  --danger: #e03e3e;
  --danger-soft: rgba(224, 62, 62, 0.12);
  --success: #0f7b6c;

  /* Effects */
  --shadow-sm: 0 1px 2px rgba(15, 15, 15, 0.06);
  --shadow-md: 0 4px 14px rgba(15, 15, 15, 0.06), 0 1px 3px rgba(15, 15, 15, 0.05);
  --shadow-lg: 0 18px 60px -12px rgba(15, 15, 15, 0.28),
    0 8px 24px -8px rgba(15, 15, 15, 0.18);
  --ring: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent-soft-strong);

  /* Geometry */
  --radius-sm: 4px;
  --radius: 6px;
  --radius-lg: 10px;
  --radius-xl: 14px;

  /* Motion */
  --ease: cubic-bezier(0.2, 0, 0, 1);
  --dur-fast: 100ms;
  --dur: 160ms;
  --dur-slow: 240ms;

  /* Typography */
  --font-sans: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI",
    "Inter", Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "JetBrains Mono", Menlo,
    "Liberation Mono", monospace;

  font-family: var(--font-sans);
  color-scheme: light;
}

[data-theme="dark"] {
  --bg: #191919;
  --bg-subtle: #202020;
  --bg-soft: #2a2a2a;
  --bg-elev: #242424;
  --bg-hover: rgba(255, 255, 255, 0.05);
  --bg-active: rgba(72, 161, 235, 0.18);
  --bg-backdrop: rgba(0, 0, 0, 0.55);

  --text: #eaeaea;
  --text-soft: #c6c5c1;
  --text-dim: #8c8a85;
  --text-on-accent: #ffffff;

  --border: #2e2e2e;
  --border-strong: #424242;
  --border-focus: #48a1eb;

  --accent: #48a1eb;
  --accent-hover: #6cb6ef;
  --accent-soft: rgba(72, 161, 235, 0.18);
  --accent-soft-strong: rgba(72, 161, 235, 0.28);

  --danger: #ef6363;
  --danger-soft: rgba(239, 99, 99, 0.16);
  --success: #4ec9a3;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 14px rgba(0, 0, 0, 0.32);
  --shadow-lg: 0 18px 60px -12px rgba(0, 0, 0, 0.55),
    0 8px 24px -8px rgba(0, 0, 0, 0.45);

  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  color: var(--text);
  background: var(--bg);
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  transition: background-color var(--dur) var(--ease),
    color var(--dur) var(--ease);
}

::selection {
  background: var(--accent-soft-strong);
  color: var(--text);
}

button {
  font-family: inherit;
}

input,
textarea,
select {
  font-family: inherit;
  color: inherit;
}

#app {
  display: flex;
  min-height: 100vh;
}

/* ── sidebar ─────────────────────────────────────────────────────────────── */

#sidebar {
  width: 260px;
  flex: 0 0 260px;
  background: var(--bg-subtle);
  border-right: 1px solid var(--border);
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  transition: background-color var(--dur) var(--ease);
}

.sidebar-inner {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  padding: 14px 10px 8px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px 12px;
  font-weight: 600;
  font-size: 14.5px;
  color: var(--text);
  letter-spacing: -0.005em;
}

.brand .brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 6px;
  background: linear-gradient(135deg, var(--accent), #6db1ff);
  color: #fff;
  box-shadow: var(--shadow-sm);
}

.sidebar-search {
  position: relative;
  margin: 0 4px 10px;
}
.sidebar-search input {
  width: 100%;
  background: var(--bg-soft);
  border: 1px solid transparent;
  border-radius: var(--radius);
  padding: 6px 10px 6px 30px;
  font-size: 13px;
  color: var(--text);
  outline: none;
  transition: border-color var(--dur) var(--ease),
    background-color var(--dur) var(--ease);
}
.sidebar-search input::placeholder {
  color: var(--text-dim);
}
.sidebar-search input:focus {
  background: var(--bg-elev);
  border-color: var(--border-strong);
  box-shadow: var(--shadow-sm);
}
.sidebar-search .search-icon {
  position: absolute;
  top: 50%;
  left: 9px;
  transform: translateY(-50%);
  color: var(--text-dim);
  pointer-events: none;
  display: inline-flex;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 600;
  color: var(--text-dim);
  padding: 6px 8px 4px;
}

#table-list {
  list-style: none;
  margin: 4px 0 0;
  padding: 0 0 8px;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}
#table-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 5px 8px;
  border-radius: var(--radius);
  cursor: pointer;
  color: var(--text-soft);
  gap: 6px;
  margin: 1px 0;
  font-size: 13.5px;
  user-select: none;
  transition: background-color var(--dur-fast) var(--ease),
    color var(--dur-fast) var(--ease);
}
#table-list li:hover {
  background: var(--bg-hover);
  color: var(--text);
}
#table-list li.active {
  background: var(--bg-active);
  color: var(--text);
  font-weight: 500;
}
#table-list li.active .table-icon {
  color: var(--accent);
}
#table-list li .table-row-main {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex: 1;
}
#table-list li .name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#table-list li .table-icon {
  color: var(--text-dim);
  display: inline-flex;
  flex-shrink: 0;
}
#table-list li .del {
  opacity: 0;
  transform: translateX(4px);
  color: var(--text-dim);
  border: none;
  background: none;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 3px;
  border-radius: var(--radius-sm);
  transition: opacity var(--dur-fast) var(--ease),
    transform var(--dur-fast) var(--ease),
    color var(--dur-fast) var(--ease),
    background-color var(--dur-fast) var(--ease);
}
#table-list li:hover .del,
#table-list li.active .del {
  opacity: 1;
  transform: translateX(0);
}
#table-list li .del:hover {
  background: var(--danger-soft);
  color: var(--danger);
}

.sidebar-empty {
  color: var(--text-dim);
  font-size: 13px;
  padding: 14px 10px;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  margin: 6px 4px;
  text-align: center;
}

.sidebar-foot {
  margin-top: auto;
  padding: 8px 12px 14px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: var(--text-dim);
  font-size: 12px;
}

.sidebar-foot-actions {
  display: flex;
  align-items: center;
  gap: 2px;
}

/* linked-view sidebar entries read as shortcuts, not tables */
#table-list .linked-row .table-icon {
  color: var(--accent, #6b7cff);
}
#table-list .linked-row .name {
  font-style: italic;
}

/* settings / linked-view dialog fields */
.settings-field {
  margin: 12px 0;
  display: flex;
  flex-direction: column;
  gap: 5px;
}
.settings-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dim);
}
.settings-hint {
  margin: 2px 0 0;
  font-size: 11px;
  color: var(--text-dim);
}
.settings-check {
  display: flex;
  align-items: center;
  gap: 9px;
  margin: 10px 0;
  font-size: 14px;
  cursor: pointer;
}
.settings-check input {
  width: 16px;
  height: 16px;
  cursor: pointer;
}

/* spin the sync icon while a git sync is in flight */
@keyframes md-spin {
  to {
    transform: rotate(360deg);
  }
}
.icon-btn .spin {
  animation: md-spin 1s linear infinite;
}

/* ── main area ───────────────────────────────────────────────────────────── */

#main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  transition: background-color var(--dur) var(--ease);
}

#toolbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  padding: 28px 36px 14px;
  gap: 16px;
  background: var(--bg);
}

.title-block {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}
#table-title {
  font-size: 28px;
  font-weight: 700;
  margin: 0;
  letter-spacing: -0.02em;
  color: var(--text);
  display: inline-flex;
  align-items: center;
  gap: 10px;
}
#table-title .title-icon {
  color: var(--text-dim);
  display: inline-flex;
}
.title-meta {
  font-size: 12.5px;
  color: var(--text-dim);
}

.toolbar-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

#grid-wrap {
  padding: 8px 36px 120px;
  overflow-x: auto;
  flex: 1;
}

/* ── empty state ─────────────────────────────────────────────────────────── */

.empty {
  color: var(--text-dim);
  padding: 60px 4px;
  text-align: center;
  font-size: 14px;
}
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 80px 24px;
  color: var(--text-dim);
  text-align: center;
}
.empty-state .empty-icon {
  width: 56px;
  height: 56px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-lg);
  background: var(--bg-soft);
  color: var(--text-dim);
}
.empty-state .empty-title {
  color: var(--text);
  font-weight: 600;
  font-size: 16px;
}
.empty-state .empty-sub {
  max-width: 360px;
  font-size: 13.5px;
  line-height: 1.5;
}

/* ── grid ────────────────────────────────────────────────────────────────── */

#grid {
  border-collapse: separate;
  border-spacing: 0;
  width: 100%;
  min-width: 520px;
  /* Fixed layout makes the per-column <col> widths authoritative, so drag-to-
     resize moves only the dragged edge instead of the browser re-flowing every
     column to fit content. This is what makes resizing feel smooth. */
  table-layout: fixed;
  background: var(--bg);
  font-size: 13.5px;
}
#grid th,
#grid td {
  border-bottom: 1px solid var(--border);
  border-right: 1px solid var(--border);
  padding: 0;
  text-align: left;
  vertical-align: top;
}
#grid th:first-child,
#grid td:first-child {
  border-left: 1px solid var(--border);
}
#grid thead th:first-child {
  border-top-left-radius: var(--radius);
}
#grid thead th:last-child {
  border-top-right-radius: var(--radius);
}
#grid tbody tr:last-child td:first-child {
  border-bottom-left-radius: var(--radius);
}
#grid tbody tr:last-child td:last-child {
  border-bottom-right-radius: var(--radius);
}

#grid thead th {
  background: var(--bg-subtle);
  border-top: 1px solid var(--border);
  font-weight: 500;
  font-size: 12px;
  color: var(--text-dim);
  position: sticky;
  top: 0;
  z-index: 2;
  letter-spacing: 0.01em;
}

.col-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  gap: 6px;
  min-height: 32px;
}
.col-sort {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  border: none;
  background: none;
  font: inherit;
  font-weight: 600;
  font-size: 12px;
  text-transform: none;
  color: var(--text-soft);
  cursor: pointer;
  padding: 2px 4px;
  margin: -2px -4px;
  border-radius: var(--radius-sm);
  transition: background-color var(--dur-fast) var(--ease),
    color var(--dur-fast) var(--ease);
}
.col-sort:hover {
  background: var(--bg-hover);
  color: var(--text);
}
.sort-ind {
  color: var(--accent);
  display: inline-flex;
}
.col-head .col-del {
  opacity: 0;
  border: none;
  background: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 3px;
  border-radius: var(--radius-sm);
  display: inline-flex;
  transition: opacity var(--dur-fast) var(--ease),
    background-color var(--dur-fast) var(--ease),
    color var(--dur-fast) var(--ease);
}
#grid thead th:hover .col-del {
  opacity: 1;
}
.col-head .col-del:hover {
  background: var(--danger-soft);
  color: var(--danger);
}

.row-actions-head,
.add-col-head {
  width: 44px;
}
.add-col-cell {
  background: var(--bg);
  border-right-color: transparent !important;
}
[data-theme="dark"] .add-col-cell {
  background: var(--bg);
}
.add-col-head .col-head {
  justify-content: center;
}
.add-col-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border: none;
  background: transparent;
  color: var(--text-dim);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: color var(--dur-fast) var(--ease),
    background-color var(--dur-fast) var(--ease);
}
.add-col-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
}

#grid tbody tr {
  transition: background-color var(--dur-fast) var(--ease);
}
#grid tbody tr:hover {
  background: var(--bg-hover);
}

.cell-input {
  width: 100%;
  border: none;
  background: transparent;
  padding: 8px 10px;
  font: inherit;
  color: inherit;
  outline: none;
  min-width: 90px;
  border-radius: 0;
  transition: box-shadow var(--dur-fast) var(--ease),
    background-color var(--dur-fast) var(--ease);
}
.cell-input::placeholder {
  color: var(--text-dim);
}
.cell-input:hover {
  background: var(--bg-hover);
}
.cell-input:focus {
  background: var(--bg-elev);
  box-shadow: inset 0 0 0 2px var(--accent);
}

/* Wrapped text cells: an auto-growing textarea that reveals full contents. */
.cell-input-wrap {
  display: block;
  resize: none;
  overflow: hidden;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  line-height: 1.4;
}
#grid td.cell-wrap {
  vertical-align: top;
}

/* Numbered-hierarchy title cells: the inline paddingLeft (set per row in the
   Grid) shifts the editor right by depth; a faint guide rule hints the nesting
   without stealing focus. `100` is flush left, `130` indents one level, `139`
   two, and so on. */
#grid td.cell-indent {
  box-shadow: inset 1px 0 0 var(--border);
}

/* Title cell with a collapse toggle (or a spacer to keep leaf rows aligned)
   sitting to the left of the text editor. */
.cell-hierarchy {
  display: flex;
  align-items: flex-start;
}
.hierarchy-toggle {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 34px;
  padding: 0;
  border: none;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  border-radius: var(--radius-sm, 4px);
}
.hierarchy-toggle:hover {
  color: var(--text);
  background: var(--bg-hover);
}
.hierarchy-spacer {
  flex: 0 0 auto;
  width: 20px;
}
.cell-hierarchy .cell-input,
.cell-hierarchy .cell-text-wrap {
  flex: 1 1 auto;
  min-width: 0;
}

/* Thin toolbar above the grid with Expand all / Collapse all for the numbered
   hierarchy. Only rendered when a hierarchy is detected in the title column. */
.hierarchy-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 2px 10px;
}
.hierarchy-bar-label {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-soft);
}

/* Copy / open affordances for text cells that contain a URL. */
.cell-text-wrap {
  position: relative;
  display: flex;
  align-items: center;
}
.cell-text-wrap-linked .cell-input {
  padding-right: 48px;
}
.cell-link-actions {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  align-items: center;
  gap: 2px;
  opacity: 0;
  transition: opacity var(--dur-fast) var(--ease);
}
.cell-text-wrap:hover .cell-link-actions,
.cell-text-wrap:focus-within .cell-link-actions {
  opacity: 1;
}
.cell-link-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-elev);
  color: var(--text-soft);
  cursor: pointer;
  padding: 0;
  text-decoration: none;
}
.cell-link-btn:hover {
  background: var(--bg-hover);
  color: var(--accent);
  border-color: var(--accent);
}

/* ── link column cells ─────────────────────────────────────────────────────── */
.cell-link {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 34px;
  padding: 4px 8px;
}
.cell-link-list {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 10px;
  min-width: 0;
  flex: 1 1 auto;
}
.cell-link-anchor {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  max-width: 100%;
  color: var(--accent);
  text-decoration: none;
  font-weight: 500;
}
.cell-link-anchor:hover .cell-link-text {
  text-decoration: underline;
}
.cell-link-text {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.cell-link .cell-link-actions {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  gap: 2px;
  opacity: 0;
  padding-left: 10px;
  background: linear-gradient(
    to right,
    transparent 0,
    var(--bg-elev) 10px
  );
  border-radius: 6px;
  transition: opacity var(--dur-fast) var(--ease);
}
.cell-link:hover .cell-link-actions,
.cell-link:focus-within .cell-link-actions {
  opacity: 1;
}

/* Right-click context menu for link cells (space-constrained fallback). */
.cell-link-menu {
  position: fixed;
  z-index: 1000;
  min-width: 150px;
  padding: 4px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.cell-link-menu-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 7px 10px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.cell-link-menu-item:hover {
  background: var(--bg-hover);
  color: var(--accent);
}
.cell-link-empty {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  min-height: 34px;
  padding: 4px 8px;
  border: 0;
  background: transparent;
  color: var(--text-dim);
  cursor: text;
  text-align: left;
  font: inherit;
}
.cell-link-empty:hover {
  color: var(--accent);
}
.cell-link-edit {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 4px;
}
.cell-link-edit-row {
  display: flex;
  align-items: center;
  gap: 4px;
}
.cell-link-edit-row .cell-link-label-input {
  flex: 0 1 40%;
}
.cell-link-edit-row .cell-link-url-input {
  flex: 1 1 60%;
}
.cell-link-edit-actions {
  display: flex;
  justify-content: flex-end;
  gap: 4px;
}

.cell-bool {
  text-align: center;
  padding: 0;
}
.cell-bool label {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
  cursor: pointer;
  min-height: 32px;
}
.cell-bool input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border: 1.5px solid var(--border-strong);
  border-radius: 4px;
  background: var(--bg);
  cursor: pointer;
  position: relative;
  transition: background-color var(--dur-fast) var(--ease),
    border-color var(--dur-fast) var(--ease);
}
.cell-bool input[type="checkbox"]:hover {
  border-color: var(--accent);
}
.cell-bool input[type="checkbox"]:checked {
  background: var(--accent);
  border-color: var(--accent);
}
.cell-bool input[type="checkbox"]:checked::after {
  content: "";
  position: absolute;
  inset: 0;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'><path d='M20 6 9 17l-5-5'/></svg>");
  background-repeat: no-repeat;
  background-position: center;
  background-size: 12px;
}

.id-cell {
  color: var(--text-soft);
  font-size: 12.5px;
  padding: 0;
  white-space: nowrap;
  min-width: 140px;
}
.id-cell .open-body {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  border: none;
  background: none;
  color: var(--text-soft);
  font: inherit;
  font-size: 12.5px;
  padding: 8px 10px;
  width: 100%;
  text-align: left;
  border-radius: 0;
  transition: color var(--dur-fast) var(--ease),
    background-color var(--dur-fast) var(--ease);
}
.id-cell .open-body:hover {
  background: var(--bg-hover);
  color: var(--accent);
}
.id-cell .open-body .row-handle {
  display: inline-flex;
  color: var(--text-dim);
  flex-shrink: 0;
}
.id-cell .open-body:hover .row-handle {
  color: var(--accent);
}
.id-cell .open-body .row-id-text {
  overflow: hidden;
  text-overflow: ellipsis;
}

.row-actions {
  padding: 0;
  text-align: center;
  width: auto;
  min-width: 44px;
  white-space: nowrap;
}
.row-actions > * + * {
  margin-left: 2px;
}
.row-del,
.row-actions .open-body,
.row-actions .open-page {
  border: none;
  background: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius-sm);
  opacity: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: opacity var(--dur-fast) var(--ease),
    color var(--dur-fast) var(--ease),
    background-color var(--dur-fast) var(--ease);
}
#grid tbody tr:hover .row-del,
#grid tbody tr:focus-within .row-del,
#grid tbody tr:hover .row-actions .open-body,
#grid tbody tr:focus-within .row-actions .open-body,
#grid tbody tr:hover .row-actions .open-page,
#grid tbody tr:focus-within .row-actions .open-page {
  opacity: 1;
}
.row-del:hover {
  color: var(--danger);
  background: var(--danger-soft);
}
.row-actions .open-body:hover,
.row-actions .open-page:hover {
  color: var(--text);
  background: var(--bg-hover);
}
.row-actions .row-grip {
  opacity: 0;
  border: none;
  background: none;
  color: var(--text-dim);
  cursor: grab;
  padding: 6px;
  border-radius: var(--radius-sm);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: opacity var(--dur-fast) var(--ease);
}
#grid tbody tr:hover .row-actions .row-grip,
#grid tbody tr:focus-within .row-actions .row-grip {
  opacity: 1;
}

/* ── select column (multi-row checkbox) ────────────────────────────────── */
.select-col {
  width: 38px;
}
/* Higher specificity than `#grid td` (which forces vertical-align: top) so the
 * checkbox sits dead-centre vertically regardless of the cells beside it. */
#grid th.select-head,
#grid td.select-cell {
  width: 38px;
  min-width: 38px;
  padding: 0;
  text-align: center;
  vertical-align: middle;
}
.select-head-inner,
.select-cell-inner {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 6px 0;
  /* Match the typical cell row height so the checkbox sits centred relative
   * to the rest of the row's controls (input, select, multi-tag chips, …). */
  min-height: 34px;
}
.row-select {
  appearance: auto;
  cursor: pointer;
  width: 16px;
  height: 16px;
  margin: 0;
  accent-color: var(--accent, #4f46e5);
}
.row-select:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}
#grid tbody tr.row-selected {
  background: var(--bg-hover);
}
#grid tbody tr.row-selected td:not(.select-cell) {
  box-shadow: inset 0 0 0 9999px var(--accent-soft, rgba(99, 102, 241, 0.06));
}

/* ── floating selection bar above the grid ─────────────────────────────── */
.selection-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 12px;
  margin: 0 0 8px 0;
  border-radius: var(--radius-sm);
  background: var(--accent-soft, rgba(99, 102, 241, 0.08));
  border: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 5;
}
.selection-count {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}
.selection-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

/* The bottom “+ New row” pseudo-row inside the grid. */
.new-row td {
  border-bottom: 1px solid var(--border);
  padding: 0;
  background: var(--bg);
}
.new-row .new-row-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  padding: 8px 12px;
  color: var(--text-dim);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  border-radius: 0;
  transition: background-color var(--dur-fast) var(--ease),
    color var(--dur-fast) var(--ease);
}
.new-row .new-row-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.row-count-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-dim);
  margin: 10px 4px 0;
}

/* ── buttons ─────────────────────────────────────────────────────────────── */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border-strong);
  background: var(--bg);
  color: var(--text);
  border-radius: var(--radius);
  padding: 6px 12px;
  font-size: 13px;
  cursor: pointer;
  font-weight: 500;
  line-height: 1.2;
  transition: background-color var(--dur-fast) var(--ease),
    border-color var(--dur-fast) var(--ease),
    color var(--dur-fast) var(--ease),
    transform var(--dur-fast) var(--ease);
}
.btn:hover {
  background: var(--bg-subtle);
  border-color: var(--border-strong);
}
.btn:active {
  transform: translateY(1px);
}
.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--text-on-accent);
}
.btn.primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}
.btn.ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text-soft);
}
.btn.ghost:hover {
  background: var(--bg-hover);
  color: var(--text);
}
.btn.danger {
  color: var(--danger);
  border-color: transparent;
  background: transparent;
}
.btn.danger:hover {
  background: var(--danger-soft);
}
.btn.danger-solid {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}
.btn.danger-solid:hover {
  background: #c93535;
  border-color: #c93535;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: none;
  cursor: pointer;
  color: var(--text-dim);
  padding: 4px;
  border-radius: var(--radius-sm);
  transition: background-color var(--dur-fast) var(--ease),
    color var(--dur-fast) var(--ease);
}
.icon-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
}
.icon-btn.active {
  color: var(--accent);
  background: var(--accent-soft);
}

.kbd {
  font-family: var(--font-mono);
  font-size: 11px;
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 1px 5px;
  color: var(--text-dim);
}

/* ── view bar (tabs + sort/filter) ───────────────────────────────────────── */

#viewbar {
  padding: 4px 36px 0;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 5;
}
.view-tabs {
  display: flex;
  gap: 2px;
  flex-wrap: wrap;
  align-items: center;
  margin-bottom: -1px;
}
.view-tab {
  border: none;
  background: none;
  font: inherit;
  font-size: 13px;
  color: var(--text-dim);
  padding: 8px 12px;
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: color var(--dur-fast) var(--ease),
    background-color var(--dur-fast) var(--ease),
    border-color var(--dur-fast) var(--ease);
}
.view-tab:hover {
  background: var(--bg-hover);
  color: var(--text);
}
.view-tab.active {
  color: var(--text);
  font-weight: 600;
  border-bottom-color: var(--accent);
}
.view-tab-del {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: none;
  color: var(--text-dim);
  font: inherit;
  padding: 2px;
  border-radius: 3px;
  cursor: pointer;
}
.view-tab-del:hover {
  background: var(--danger-soft);
  color: var(--danger);
}
.view-tab-default {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: none;
  color: var(--text-dim);
  font: inherit;
  padding: 2px;
  border-radius: 3px;
  cursor: pointer;
}
.view-tab-default:hover {
  background: var(--bg-hover);
  color: var(--accent);
}
.view-tab-default[aria-pressed="true"] {
  color: var(--accent);
}
.view-tab.is-default > :first-child {
  color: var(--accent);
}

.view-controls {
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 8px 0;
  flex-wrap: wrap;
}
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-soft);
  border-radius: var(--radius);
  padding: 4px 10px;
  font: inherit;
  font-size: 12.5px;
  cursor: pointer;
  line-height: 1.2;
  transition: background-color var(--dur-fast) var(--ease),
    border-color var(--dur-fast) var(--ease),
    color var(--dur-fast) var(--ease);
}
.chip:hover {
  background: var(--bg-hover);
  color: var(--text);
}
.chip:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
.chip.on {
  background: var(--accent-soft);
  border-color: var(--accent-soft-strong);
  color: var(--accent);
}
.chip.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--text-on-accent);
}
.chip.primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
  color: var(--text-on-accent);
}

.chip-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border-radius: 8px;
  font-size: 10.5px;
  background: var(--accent);
  color: var(--text-on-accent);
}

.rule-panel {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  margin: 4px 0 12px;
  background: var(--bg-subtle);
  box-shadow: var(--shadow-sm);
  animation: rule-panel-in var(--dur) var(--ease);
}
@keyframes rule-panel-in {
  from {
    opacity: 0;
    transform: translateY(-4px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
.rule-empty {
  color: var(--text-dim);
  margin: 2px 0 10px;
  font-size: 13px;
}
.rule-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}
.rule-prefix {
  color: var(--text-dim);
  font-size: 12.5px;
  min-width: 52px;
}
.rule-col,
.rule-op,
.rule-conj,
.rule-val {
  border: 1px solid var(--border-strong);
  border-radius: 5px;
  padding: 5px 8px;
  font: inherit;
  font-size: 12.5px;
  background: var(--bg);
  color: var(--text);
  transition: border-color var(--dur-fast) var(--ease);
}
.rule-col:focus,
.rule-op:focus,
.rule-conj:focus,
.rule-val:focus {
  outline: none;
  border-color: var(--accent);
}
.rule-val {
  min-width: 140px;
}
.rule-del {
  border: none;
  background: none;
  cursor: pointer;
  padding: 3px;
  border-radius: var(--radius-sm);
  color: var(--text-dim);
  display: inline-flex;
}
.rule-del:hover {
  background: var(--danger-soft);
  color: var(--danger);
}
.rule-add {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px dashed var(--border-strong);
  background: none;
  color: var(--text-dim);
  border-radius: 5px;
  padding: 5px 10px;
  font: inherit;
  font-size: 12.5px;
  cursor: pointer;
  margin-top: 2px;
  transition: color var(--dur-fast) var(--ease),
    border-color var(--dur-fast) var(--ease);
}
.rule-add:hover {
  color: var(--accent);
  border-color: var(--accent);
}

/* ── side panel (Markdown body) ──────────────────────────────────────────── */

.panel-backdrop {
  position: fixed;
  inset: 0;
  background: var(--bg-backdrop);
  z-index: 30;
  animation: fade-in var(--dur) var(--ease);
}
@keyframes fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.panel {
  position: fixed;
  top: 0;
  right: 0;
  width: 480px;
  max-width: 92vw;
  height: 100vh;
  background: var(--bg);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  z-index: 40;
  animation: panel-in var(--dur-slow) var(--ease);
}
@keyframes panel-in {
  from {
    transform: translateX(24px);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.panel-head .panel-icon {
  display: inline-flex;
  color: var(--text-dim);
}
.panel-id {
  flex: 1;
  min-width: 0;
  font-size: 17px;
  font-weight: 600;
  border: none;
  outline: none;
  background: transparent;
  color: var(--text);
  padding: 4px 6px;
  border-radius: var(--radius-sm);
}
.panel-id::placeholder {
  color: var(--text-dim);
}
.panel-id:hover {
  background: var(--bg-hover);
}
.panel-id:focus {
  background: var(--bg-subtle);
  box-shadow: inset 0 0 0 1.5px var(--accent);
}

.panel-body-wrap {
  flex: 1;
  padding: 14px 18px 0;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.panel-label {
  font-size: 11px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 600;
}
#panel-body {
  flex: 1;
  min-height: 360px;
  resize: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.55;
  outline: none;
  background: var(--bg-subtle);
  color: var(--text);
  transition: border-color var(--dur-fast) var(--ease),
    background-color var(--dur-fast) var(--ease);
}
#panel-body:focus {
  border-color: var(--accent);
  background: var(--bg);
}

.panel-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 18px;
  border-top: 1px solid var(--border);
  background: var(--bg);
}

/* ── modal ───────────────────────────────────────────────────────────────── */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: var(--bg-backdrop);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
  animation: fade-in var(--dur) var(--ease);
}
.modal {
  width: min(440px, 100%);
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: 18px 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  animation: modal-in var(--dur-slow) var(--ease);
}
@keyframes modal-in {
  from {
    opacity: 0;
    transform: translateY(8px) scale(0.985);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}
.modal-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
}
.modal-title {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
  letter-spacing: -0.005em;
}
.modal-desc {
  margin: -4px 0 0;
  color: var(--text-dim);
  font-size: 13px;
  line-height: 1.5;
}
.modal-input {
  margin-top: 4px;
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 9px 12px;
  font: inherit;
  font-size: 14px;
  color: var(--text);
  outline: none;
  transition: border-color var(--dur-fast) var(--ease),
    box-shadow var(--dur-fast) var(--ease);
}
.modal-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.modal-textarea {
  resize: vertical;
  min-height: 140px;
  line-height: 1.5;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  white-space: pre-wrap;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 6px;
}

/* ── toast ───────────────────────────────────────────────────────────────── */

.toast {
  position: fixed;
  bottom: 28px;
  right: 28px;
  background: var(--text);
  color: var(--bg);
  padding: 10px 14px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  z-index: 200;
  font-size: 13px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  animation: toast-in var(--dur-slow) var(--ease);
}
[data-theme="dark"] .toast {
  background: #efefee;
  color: #1a1a1a;
}
.toast .toast-icon {
  display: inline-flex;
  color: var(--success);
}
.toast.error {
  background: var(--danger);
  color: #fff;
}
.toast.error .toast-icon {
  color: #fff;
}
@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* ── scrollbars (subtle, theme-aware) ────────────────────────────────────── */

* {
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
}
*::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}
*::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 8px;
  border: 2px solid transparent;
  background-clip: padding-box;
}
*::-webkit-scrollbar-track {
  background: transparent;
}

/* ── column types · drag handles · type menu (added) ──────────────────────── */

/* New header buttons sit inline with sort/delete. */
.col-grip,
.col-type {
  border: none;
  background: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 4px;
  border-radius: var(--radius-sm);
  display: inline-flex;
  align-items: center;
  flex-shrink: 0;
  transition: color var(--dur-fast) var(--ease),
    background-color var(--dur-fast) var(--ease), opacity var(--dur-fast) var(--ease);
}
.col-grip {
  cursor: grab;
  touch-action: none; /* let pointer-drag own the gesture on touch */
  opacity: 0;
}
#grid thead th:hover .col-grip {
  opacity: 1;
}
.col-grip:active {
  cursor: grabbing;
}
.col-type:hover,
.col-grip:hover {
  color: var(--accent);
  background: var(--accent-soft);
}

/* Column drag feedback. */
#grid thead th.col-dragging {
  opacity: 0.45;
}
#grid thead th.col-drop {
  box-shadow: inset 2px 0 0 0 var(--accent);
}

/* Row grip lives beside the open-body button in the id cell. */
.id-cell-inner {
  display: flex;
  align-items: center;
}
.row-grip {
  border: none;
  background: none;
  color: var(--text-dim);
  cursor: grab;
  touch-action: none;
  padding: 6px 2px 6px 8px;
  display: inline-flex;
  align-items: center;
  flex-shrink: 0;
  opacity: 0;
  transition: color var(--dur-fast) var(--ease), opacity var(--dur-fast) var(--ease);
}
#grid tbody tr:hover .row-grip,
#grid tbody tr:focus-within .row-grip {
  opacity: 1;
}
.row-grip:active {
  cursor: grabbing;
}
.row-grip:hover {
  color: var(--accent);
}
#grid tbody tr.row-dragging {
  opacity: 0.45;
  background: var(--accent-soft);
}
#grid tbody tr.row-drop td {
  box-shadow: inset 0 2px 0 0 var(--accent);
}

/* Select + date cells. */
.cell-select,
td[data-type="select"] {
  padding: 0;
}
.cell-select-input {
  appearance: none;
  -webkit-appearance: none;
  width: 100%;
  border: none;
  background: transparent;
  color: inherit;
  font: inherit;
  font-size: 13.5px;
  padding: 8px 26px 8px 12px;
  cursor: pointer;
  outline: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='gray' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><path d='m6 9 6 6 6-6'/></svg>");
  background-repeat: no-repeat;
  background-position: right 8px center;
  background-size: 13px;
}
.cell-select-input:focus {
  box-shadow: inset 0 0 0 2px var(--accent-soft-strong);
}
.cell-date {
  cursor: text;
}

.cell-datetime {
  cursor: text;
}

/* Fallback shown when a stored date can't be coerced to the picker format. */
.cell-date-raw {
  font-style: italic;
  color: var(--warn, var(--text-dim));
}

/* Relative "ago" date display — a full-width button that swaps in the native
   picker on click (see RelativeDateCell). */
.cell-relative {
  display: block;
  width: 100%;
  border: none;
  background: transparent;
  padding: 6px 8px;
  text-align: left;
  font: inherit;
  color: inherit;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.cell-relative:hover {
  background: var(--hover, rgba(127, 127, 127, 0.08));
}

.cell-relative-empty {
  color: var(--text-dim);
}

/* Column type popover. */
.type-menu {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 40;
  margin-top: 4px;
  width: 230px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg, 0 10px 30px rgba(0, 0, 0, 0.18));
  padding: 8px;
  text-align: left;
  font-weight: 400;
}
.type-menu-head {
  font-size: 11.5px;
  color: var(--text-dim);
  padding: 4px 8px 8px;
  text-transform: none;
}
.type-menu-head strong {
  color: var(--text);
}
.type-menu-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.type-opt {
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  border: none;
  background: none;
  color: var(--text);
  font: inherit;
  font-size: 13.5px;
  padding: 7px 8px;
  border-radius: var(--radius);
  cursor: pointer;
  text-align: left;
  transition: background-color var(--dur-fast) var(--ease);
}
.type-opt:hover {
  background: var(--bg-hover);
}
.type-opt.active {
  color: var(--accent);
}
.type-opt-check {
  margin-left: auto;
  color: var(--accent);
  display: inline-flex;
}
.type-menu-options {
  margin-top: 8px;
  padding: 8px 4px 0;
  border-top: 1px solid var(--border);
}
.type-menu-label {
  display: block;
  font-size: 11.5px;
  color: var(--text-dim);
  margin-bottom: 6px;
}
.type-options-input {
  width: 100%;
  resize: vertical;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-soft);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  padding: 7px 9px;
  outline: none;
}
.type-options-input:focus {
  border-color: var(--accent);
}
.type-menu-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 10px;
}
.type-menu-hint {
  font-size: 11px;
  color: var(--text-dim);
  margin: 6px 2px 0;
  line-height: 1.4;
}
.type-menu-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
}
.type-menu-toggle input {
  margin: 0;
}

/* ── reference cells (@table/rowId links) ─────────────────────────────────── */

.cell-ref,
td[data-type="reference"] {
  padding: 0;
  position: relative;
}
.ref-chips {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  padding: 5px 8px;
  min-height: 32px;
}
.ref-chip-wrap {
  display: inline-flex;
  align-items: center;
  gap: 2px;
}
.ref-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 100%;
  border: 1px solid var(--border);
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: 999px;
  padding: 3px 10px 3px 8px;
  font: inherit;
  font-size: 12.5px;
  cursor: pointer;
  transition: background-color var(--dur-fast) var(--ease),
    border-color var(--dur-fast) var(--ease);
}
.ref-chip:hover {
  background: var(--accent-soft-strong);
  border-color: var(--accent);
}
.ref-chip-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 500;
}
.ref-chip-table {
  font-size: 10.5px;
  color: var(--text-dim);
  background: var(--bg);
  border-radius: 999px;
  padding: 1px 6px;
  flex-shrink: 0;
}
.ref-chip.broken {
  background: var(--danger-soft);
  border-color: var(--danger);
  color: var(--danger);
  cursor: default;
}
.ref-chip-edit {
  border: none;
  background: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 3px;
  border-radius: var(--radius-sm);
  display: inline-flex;
  opacity: 0;
  transition: opacity var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
#grid tbody tr:hover .ref-chip-edit,
.cell-ref:focus-within .ref-chip-edit {
  opacity: 1;
}
.ref-chip-edit:hover {
  color: var(--danger);
}
/* The small "+" button that opens the picker to add another reference. */
.ref-add {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px dashed var(--border);
  background: none;
  color: var(--text-dim);
  border-radius: 999px;
  width: 22px;
  height: 22px;
  padding: 0;
  cursor: pointer;
  opacity: 0;
  transition: opacity var(--dur-fast) var(--ease),
    color var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease);
}
#grid tbody tr:hover .ref-add,
.cell-ref:focus-within .ref-add {
  opacity: 1;
}
.ref-add:hover {
  color: var(--accent);
  border-color: var(--accent);
}

/* The @-search picker. */
.ref-empty {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  min-height: 32px;
  padding: 5px 8px;
  border: none;
  background: none;
  color: var(--text-dim);
  font: inherit;
  font-size: 13px;
  text-align: left;
  cursor: pointer;
  transition: color var(--dur-fast) var(--ease);
}
.ref-empty:hover {
  color: var(--text);
}
.ref-empty-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.ref-picker {
  position: relative;
}
/* Currently-selected chips shown inside the open picker. */
.ref-picker-selected {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 4px 4px 6px;
}
.ref-picker-selected .ref-chip {
  cursor: default;
}
.ref-chip-x {
  border: none;
  background: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 0 0 0 2px;
  display: inline-flex;
}
.ref-chip-x:hover {
  color: var(--danger);
}
.ref-input {
  width: 100%;
}
.ref-menu {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 45;
  margin-top: 2px;
  min-width: 240px;
  max-width: 320px;
  max-height: 260px;
  overflow-y: auto;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg, 0 10px 30px rgba(0, 0, 0, 0.18));
  padding: 4px;
}
.ref-menu-empty {
  padding: 10px 12px;
  font-size: 12.5px;
  color: var(--text-dim);
}
.ref-opt {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  border: none;
  background: none;
  color: var(--text);
  font: inherit;
  font-size: 13px;
  padding: 7px 9px;
  border-radius: var(--radius);
  cursor: pointer;
  text-align: left;
}
.ref-opt:hover,
.ref-opt.active {
  background: var(--bg-hover);
}
.ref-opt-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}
.ref-opt-table {
  font-size: 10.5px;
  color: var(--text-dim);
  flex-shrink: 0;
}
.ref-opt.picked {
  color: var(--accent);
}
.ref-opt.picked .ref-opt-label {
  font-weight: 500;
}
.ref-opt-done {
  color: var(--text-dim);
  border-top: 1px solid var(--border);
  border-radius: 0;
  margin-top: 2px;
}
.ref-opt-done:hover {
  color: var(--accent);
  background: var(--accent-soft);
}
.ref-opt-clear {
  color: var(--text-dim);
  border-top: 1px solid var(--border);
  border-radius: 0;
  margin-top: 2px;
}
.ref-opt-clear:hover {
  color: var(--danger);
  background: var(--danger-soft);
}

/* Hamburger + mobile drawer scaffolding (hidden on desktop). */.hamburger {
  display: none;
}
.drawer-backdrop {
  display: none;
}
.drawer-grip {
  display: none;
}

/* ── responsive: real mobile layout ──────────────────────────────────────── */

@media (max-width: 760px) {
  /* Sidebar becomes an off-canvas drawer. */
  .sidebar-shell {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 60;
    transform: translateX(-100%);
    transition: transform var(--dur) var(--ease);
    box-shadow: var(--shadow-lg, 0 10px 40px rgba(0, 0, 0, 0.3));
    /* Horizontal swipe-to-dismiss is owned by JS; let vertical scroll through. */
    touch-action: pan-y;
  }
  .sidebar-shell.open {
    transform: translateX(0);
  }
  /* While the finger is dragging, follow it 1:1 with no transition lag. */
  .sidebar-shell.dragging {
    transition: none;
  }
  #sidebar {
    width: 82vw;
    max-width: 320px;
    flex-basis: auto;
    height: 100%;
    position: static;
  }
  .drawer-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 55;
    background: rgba(0, 0, 0, 0.45);
    animation: fade-in var(--dur) var(--ease);
  }
  .sidebar-close {
    margin-left: auto;
  }

  .hamburger {
    display: inline-flex;
    align-self: center;
    flex-shrink: 0;
  }

  /* A visible pull-tab on the drawer's right edge: hints "grab me to close". */
  .drawer-grip {
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    top: 50%;
    right: -16px;
    transform: translateY(-50%);
    width: 16px;
    height: 64px;
    padding: 0;
    border: none;
    border-radius: 0 8px 8px 0;
    background: var(--bg-subtle);
    box-shadow: var(--shadow-sm);
    cursor: grab;
    touch-action: none;
  }
  .drawer-grip:active {
    cursor: grabbing;
  }
  .drawer-grip-bar {
    width: 3px;
    height: 32px;
    border-radius: 3px;
    background: var(--border-strong);
  }

  #toolbar {
    align-items: center;
    padding: 14px 16px 12px;
    gap: 10px;
    flex-wrap: wrap;
  }
  #table-title {
    font-size: 20px;
  }
  .title-meta {
    font-size: 11.5px;
  }
  .toolbar-actions {
    width: 100%;
    justify-content: flex-end;
  }

  #viewbar,
  #grid-wrap {
    padding-left: 12px;
    padding-right: 12px;
  }

  /* Grid scrolls horizontally; keep columns tappable, not cramped. */
  #grid-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .cell-input,
  .cell-select-input {
    font-size: 16px; /* ≥16px stops iOS auto-zoom on focus */
    padding-top: 11px;
    padding-bottom: 11px;
  }
  .cell-bool label {
    min-height: 40px;
  }
  .id-cell {
    min-width: 120px;
  }

  /* Touch: grips + row delete are always visible (no hover on mobile). */
  .col-grip,
  .row-grip,
  .row-del {
    opacity: 1;
  }

  .type-menu {
    width: 210px;
  }
  .panel {
    width: 100vw;
    max-width: 100vw;
  }
  .row-count-bar {
    font-size: 11px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* ── feature batch · groups, lock, resize, footer, collapse ──────────────── */

/* Notion-style flat tree: every row is a single line; depth comes from a
 * computed `padding-left` set inline so we don't need nested <ul>s or
 * compounding borders that get noisy at 3+ levels. Prefix selectors with
 * `#table-list` so they beat the id-scoped padding/font rules above. */
#table-list li.group-row {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 4px;
  padding: 4px 8px;
  color: var(--text-soft);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  border-radius: 4px;
  user-select: none;
}
#table-list li.group-row:hover {
  background: var(--bg-hover);
  color: var(--text);
}
#table-list li.group-row .name {
  flex: 1 1 auto;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#table-list .tree-twist {
  flex: 0 0 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  opacity: 0.75;
}
#table-list .tree-twist-spacer {
  opacity: 0;
}

/* Per-table actions in the sidebar. */
#table-list li {
  position: relative;
  display: flex;
  align-items: center;
  gap: 4px;
}
.table-row-main {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}
.table-row-main .name {
  flex: 1 1 auto;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.table-row-actions {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  opacity: 0;
  transition: opacity 0.12s;
}
#table-list li:hover .table-row-actions,
#table-list li.active .table-row-actions {
  opacity: 1;
}
.kebab {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  background: transparent;
  border: 0;
  border-radius: 4px;
  color: var(--text-soft);
  cursor: pointer;
}
.kebab:hover {
  background: var(--bg-hover);
  color: var(--text);
}
.lock-chip {
  font-size: 11px;
  line-height: 1;
  margin-left: 2px;
}
.table-icon {
  display: inline-flex;
  color: var(--text-soft);
}

/* Actions menu (encrypt / lock / move / delete). */
.tact-menu {
  position: absolute;
  top: 100%;
  right: 6px;
  z-index: 30;
  min-width: 180px;
  display: flex;
  flex-direction: column;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow-lg);
  padding: 4px;
  margin-top: 4px;
}
.tact-item {
  display: block;
  padding: 7px 10px;
  background: transparent;
  border: 0;
  border-radius: 4px;
  color: var(--text);
  text-align: left;
  font-size: 13px;
  cursor: pointer;
}
.tact-item:hover {
  background: var(--bg-hover);
}
.tact-item.danger {
  color: var(--danger, #d23030);
}

/* Cursor-anchored right-click row menu (portaled to <body>). Positioned via
   inline fixed left/top, so neutralise the .tact-menu default top/right. */
.row-ctx-menu {
  position: fixed;
  top: auto;
  right: auto;
  margin-top: 0;
  min-width: 200px;
  z-index: 200;
}
.row-ctx-menu .tact-item {
  display: flex;
  align-items: center;
  gap: 8px;
}
.row-ctx-menu .tact-item svg {
  width: 15px;
  height: 15px;
  flex: 0 0 auto;
}
.tact-sep {
  height: 1px;
  margin: 4px 2px;
  background: var(--border);
}

/* Section header inside the table-actions menu (Export / Security). */
.tact-section {
  padding: 8px 10px 4px;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-soft, #888);
  font-weight: 600;
}
.tact-section:not(:first-child) {
  border-top: 1px solid var(--border);
  margin-top: 4px;
}

/* GroupRow kebab — visible on hover/focus, like table rows. */
#table-list .group-row {
  position: relative;
}
#table-list .group-row .table-row-actions {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  display: inline-flex;
  opacity: 0;
  transition: opacity 120ms ease;
}
#table-list .group-row:hover .table-row-actions,
#table-list .group-row:focus-within .table-row-actions,
#table-list .group-row .table-row-actions:focus-within {
  opacity: 1;
}

/* Sidebar collapse (desktop). */
.sidebar-shell.collapsed #sidebar {
  width: 0;
  min-width: 0;
  border-right-width: 0;
  overflow: hidden;
}
.sidebar-shell.collapsed .sidebar-inner,
.sidebar-shell.collapsed .sidebar-foot {
  display: none;
}
.sidebar-collapse-btn {
  display: inline-flex;
  margin-right: 6px;
}
@media (max-width: 760px) {
  .sidebar-collapse-btn {
    display: none;
  }
}

/* Prompt-modal file attachments (populate-from-text). */
.modal-attach {
  margin-top: 10px;
}
.modal-attach-input {
  display: none;
}
.modal-attach-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.modal-attach-list {
  list-style: none;
  margin: 10px 0 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.modal-attach-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 240px;
  padding: 4px 6px 4px 8px;
  border: 1px solid var(--border, #d0d7de);
  border-radius: 8px;
  background: var(--surface-2, #f6f8fa);
  font-size: 13px;
}
.modal-attach-thumb {
  width: 28px;
  height: 28px;
  object-fit: cover;
  border-radius: 4px;
  flex: 0 0 auto;
}
.modal-attach-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.modal-attach-remove {
  flex: 0 0 auto;
  padding: 2px;
}

/* Column resize handle (right edge of header). */
#grid thead th {
  position: relative;
}
.col-resize {
  position: absolute;
  top: 0;
  right: 0;
  width: 5px;
  height: 100%;
  cursor: col-resize;
  user-select: none;
  background: transparent;
  z-index: 5;
}
.col-resize:hover,
.col-resize:active {
  background: var(--accent, #3b82f6);
  opacity: 0.4;
}
/* While a column is being dragged, lock the cursor and kill text selection. */
body.col-resizing {
  cursor: col-resize !important;
  user-select: none !important;
}
body.col-resizing * {
  cursor: col-resize !important;
}
#grid colgroup col {
  /* No defaults — header `style` widths win when present. */
}

/* Footer aggregate row. */
#grid tfoot {
  position: sticky;
  bottom: 0;
}
#grid tfoot td {
  padding: 8px 10px;
  border-top: 1px solid var(--border);
  background: var(--bg-hover);
  font-size: 12px;
  color: var(--text-soft);
  font-variant-numeric: tabular-nums;
}
#grid tfoot td.agg-cell {
  font-weight: 600;
  color: var(--text);
}
#grid tfoot td.agg-label {
  text-align: center;
  color: var(--text-dim);
  font-weight: 700;
}

/* Aggregate picker inside the column type menu uses the same
   type-menu-options shell as the rest; nothing extra needed. */

/* ── multi-select cells + filter tag picker ────────────────────────────── */

.cell-multiselect {
  padding: 4px 6px;
  vertical-align: middle;
}
.ms-cell {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  align-items: center;
  min-height: 24px;
}
.ms-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 1px 4px 1px 8px;
  font-size: 12px;
  line-height: 1.4;
  color: var(--text);
  max-width: 100%;
}
.ms-chip-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 180px;
}
.ms-chip-x {
  background: transparent;
  border: 0;
  color: var(--text-soft);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  padding: 0 4px;
  border-radius: 999px;
}
.ms-chip-x:hover {
  background: var(--bg-hover);
  color: var(--danger);
}
.ms-add {
  background: transparent;
  border: 1px dashed var(--border);
  border-radius: 999px;
  width: 20px;
  height: 20px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text-soft);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
}
.ms-add:hover {
  background: var(--bg-hover);
  color: var(--text);
}
.ms-input {
  flex: 1 1 80px;
  min-width: 80px;
  border: 1px solid var(--border-strong);
  border-radius: 4px;
  padding: 2px 6px;
  font-size: 12px;
  background: var(--bg);
  color: var(--text);
}
.ms-input:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: 0 0 0 2px var(--accent-soft);
}

/* Filter row tag picker (hasAny/hasAll/hasNone). */
.rule-tags {
  display: inline-flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  padding: 2px 4px;
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: 4px;
  min-height: 28px;
  min-width: 160px;
  flex: 1 1 200px;
}
.rule-chip {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  background: var(--accent-soft);
  color: var(--text);
  border-radius: 4px;
  padding: 1px 4px 1px 6px;
  font-size: 12px;
  line-height: 1.4;
}
.rule-chip-x {
  background: transparent;
  border: 0;
  color: var(--text-soft);
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
  padding: 0 2px;
}
.rule-chip-x:hover {
  color: var(--danger);
}
.rule-tag-input {
  flex: 1 1 60px;
  min-width: 50px;
  border: 0;
  outline: none;
  background: transparent;
  font-size: 13px;
  color: var(--text);
  padding: 2px 0;
}

/* ── Favorites section ────────────────────────────────────────────────────
   A pinned list of starred tables shown above the main tree. Each row uses
   the same TableItem markup as the tree leaves but renders with a drag grip
   instead of the tree-twist spacer. Reorder is pointer-based via
   useReorder; .fav-dragging and .fav-drop are the live drag visualisers. */
#favorites-list {
  list-style: none;
  margin: 4px 0 0;
  padding: 0 0 4px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
}
#favorites-list li {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
  padding: 5px 8px;
  border-radius: var(--radius);
  cursor: pointer;
  color: var(--text-dim);
  user-select: none;
  -webkit-user-select: none;
}
#favorites-list li:hover {
  background: var(--bg-hover);
  color: var(--text);
}
#favorites-list li.active {
  background: var(--bg-active);
  color: var(--text);
  font-weight: 500;
}
#favorites-list li.active .table-icon {
  color: var(--accent);
}
#favorites-list li .table-row-main {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex: 1;
}
#favorites-list li .name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#favorites-list li .table-icon {
  color: var(--text-dim);
  display: inline-flex;
  flex-shrink: 0;
}
#favorites-list li .table-row-actions {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  opacity: 0;
  transition: opacity 0.12s;
}
#favorites-list li:hover .table-row-actions,
#favorites-list li.active .table-row-actions {
  opacity: 1;
}
#favorites-list li.fav-dragging {
  opacity: 0.5;
}
#favorites-list li.fav-drop {
  box-shadow: inset 0 2px 0 0 var(--accent);
}

/* Drag handle on each favorite row. Hidden until hover/active to keep the
   sidebar quiet, but always interactive to taps so touch reorder works. */
.fav-grip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 14px;
  height: 18px;
  flex: 0 0 14px;
  border: 0;
  padding: 0;
  background: none;
  color: var(--text-dim);
  cursor: grab;
  opacity: 0;
  transition: opacity 0.12s;
  touch-action: none;
}
#favorites-list li:hover .fav-grip,
#favorites-list li.active .fav-grip,
#favorites-list li.fav-dragging .fav-grip {
  opacity: 0.85;
}
.fav-grip:active {
  cursor: grabbing;
}

/* Star toggle that lives in the row's hover actions, plus a dimmed inline
   star marker on tree-side favorited tables so they're recognisable even
   when the kebab cluster is hidden. */
.fav-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border: 0;
  border-radius: var(--radius);
  background: none;
  color: var(--text-dim);
  cursor: pointer;
}
.fav-toggle:hover {
  background: var(--bg-hover);
  color: var(--text);
}
.fav-toggle.on {
  color: var(--accent);
  opacity: 1;
}
/* Always keep an "on" star visible — even when the row isn't hovered —
   so the user can find their favorited tables at a glance. */
#table-list li .table-row-actions .fav-toggle.on,
#favorites-list li .table-row-actions .fav-toggle.on {
  opacity: 1;
}
#table-list li .table-row-actions:has(.fav-toggle.on),
#favorites-list li .table-row-actions:has(.fav-toggle.on) {
  opacity: 1;
}
.fav-star {
  display: inline-flex;
  align-items: center;
  color: var(--accent);
  flex-shrink: 0;
}

/* ── body panel: mode toggle, preview, drag-over ─────────────────────────── */

.panel.panel-split {
  width: 920px;
}
.panel.panel-preview {
  width: 720px;
}
/* Full-screen mode: take over most of the viewport, render edit + preview side
 * by side, and pin to the centre via the backdrop's flex layout. */
.panel.panel-fullscreen {
  position: fixed;
  inset: 2vh 2vw;
  width: auto;
  max-width: none;
  height: auto;
  border-left: none;
  border-radius: var(--radius-lg);
  overflow: hidden;
}
.body-split-fullscreen > #panel-body,
.body-split-fullscreen > .body-preview {
  flex: 1 1 50%;
  min-width: 0;
}
.body-split-fullscreen {
  min-height: 0;
}

.panel-mode {
  display: inline-flex;
  align-items: center;
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2px;
  gap: 2px;
}
.panel-mode .mode-btn {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--text-dim);
  font: inherit;
  font-size: 11px;
  font-weight: 600;
  padding: 3px 9px;
  border-radius: 4px;
  cursor: pointer;
  letter-spacing: 0.02em;
}
.panel-mode .mode-btn:hover {
  color: var(--text);
}
.panel-mode .mode-btn.active {
  background: var(--bg);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

.panel-body-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.panel-body-actions {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.panel-file-input {
  display: none;
}
.btn.small {
  font-size: 11px;
  padding: 3px 8px;
}
.panel-upload-err {
  background: var(--danger-soft, #fde8e8);
  color: var(--danger, #b42318);
  border: 1px solid var(--danger, #b42318);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  font-size: 12px;
}

.body-split {
  flex: 1;
  display: flex;
  gap: 12px;
  min-height: 360px;
  align-items: stretch;
}
.body-split-edit > .body-preview,
.body-split-preview > #panel-body {
  display: none;
}
.body-split-split > #panel-body,
.body-split-split > .body-preview {
  flex: 1 1 50%;
  min-width: 0;
}
.body-split-edit > #panel-body,
.body-split-preview > .body-preview {
  flex: 1;
}
.body-split-preview > .rowpage-body-input {
  display: none;
}
.body-split-split > .rowpage-body-input {
  flex: 1 1 50%;
  min-width: 0;
}
.body-split-edit > .rowpage-body-input {
  flex: 1;
}

.panel-body-wrap.drag-over {
  outline: 2px dashed var(--accent);
  outline-offset: -8px;
  background: var(--accent-soft);
  border-radius: var(--radius);
}

.body-preview {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  background: var(--bg);
  color: var(--text);
  overflow: auto;
  font-size: 14px;
  line-height: 1.6;
}
.body-preview-empty {
  margin: 0;
  color: var(--text-dim);
  font-style: italic;
}

/* ── markdown render ─────────────────────────────────────────────────────── */

.md-render > :first-child {
  margin-top: 0;
}
.md-render > :last-child {
  margin-bottom: 0;
}
.md-h {
  font-weight: 600;
  letter-spacing: -0.005em;
  line-height: 1.25;
  margin: 1.2em 0 0.4em;
}
.md-h1 {
  font-size: 1.55em;
}
.md-h2 {
  font-size: 1.3em;
}
.md-h3 {
  font-size: 1.12em;
}
.md-h4,
.md-h5,
.md-h6 {
  font-size: 1em;
  color: var(--text-dim);
}
.md-p {
  margin: 0.4em 0 0.7em;
}
/* GitHub-style task-list checkboxes rendered in the Markdown preview. */
.md-task {
  list-style: none;
  margin-left: -1.2em;
  display: flex;
  align-items: flex-start;
  gap: 0.5em;
}
.md-task-check {
  margin: 0.28em 0 0;
  flex: 0 0 auto;
  width: 15px;
  height: 15px;
  cursor: pointer;
  accent-color: var(--accent);
}
.md-task-check:disabled {
  cursor: default;
}
.md-task-text {
  flex: 1 1 auto;
}
.md-task-done > .md-task-text {
  color: var(--text-dim);
  text-decoration: line-through;
}
/* Autosave status pill shown where the Save button used to live. */
.save-status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.82em;
  color: var(--text-dim);
  user-select: none;
  white-space: nowrap;
}
.save-status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--success);
  flex: 0 0 auto;
}
.save-status[data-state="saved"] .save-status-dot {
  background: var(--success);
}
.save-status[data-state="dirty"] .save-status-dot {
  background: color-mix(in srgb, var(--text-dim) 60%, transparent);
}
.save-status[data-state="saving"] .save-status-dot {
  background: var(--accent);
  animation: save-pulse 0.9s ease-in-out infinite;
}
.save-status[data-state="error"] {
  color: var(--danger);
}
.save-status[data-state="error"] .save-status-dot {
  background: var(--danger);
}
@keyframes save-pulse {
  0%,
  100% {
    opacity: 0.35;
  }
  50% {
    opacity: 1;
  }
}
.md-link {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px solid color-mix(in srgb, var(--accent) 35%, transparent);
}
.md-link:hover {
  border-bottom-color: var(--accent);
}
.md-code {
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 0 4px;
  font-family: var(--font-mono);
  font-size: 0.92em;
}
.md-pre {
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  margin: 0.6em 0 0.9em;
  overflow: auto;
}
.md-pre code {
  font-family: var(--font-mono);
  font-size: 12.5px;
  line-height: 1.55;
  background: none;
  border: none;
  padding: 0;
}
.md-ul,
.md-ol {
  margin: 0.4em 0 0.7em;
  padding-left: 1.4em;
}
.md-ul li,
.md-ol li {
  margin: 0.2em 0;
}
.md-quote {
  margin: 0.6em 0;
  padding: 4px 12px;
  border-left: 3px solid var(--border-strong);
  color: var(--text-dim);
  background: var(--bg-subtle);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}
.md-hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 1em 0;
}
.md-img {
  max-width: 100%;
  height: auto;
  border-radius: var(--radius-sm);
  display: block;
  margin: 0.4em 0;
}

/* ── image / attachment cells ────────────────────────────────────────────── */

.cell-image,
.cell-attachment {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 2px 4px;
  width: 100%;
  min-height: 28px;
}
.cell-image-thumb {
  appearance: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-subtle);
  padding: 0;
  cursor: zoom-in;
  overflow: hidden;
  flex: 0 0 auto;
  line-height: 0;
}
.cell-image-thumb img {
  max-height: 56px;
  max-width: 180px;
  display: block;
  object-fit: contain;
}
.cell-image-clear {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  padding: 2px 6px;
  border-radius: 50%;
  margin-left: auto;
  opacity: 0;
  transition: opacity var(--dur-fast) var(--ease);
}
.cell-image:hover .cell-image-clear,
.cell-attachment:hover .cell-image-clear,
.cell-image-clear:focus-visible {
  opacity: 1;
}
.cell-image-clear:hover {
  background: var(--bg-hover);
  color: var(--text);
}
.cell-image-empty,
.cell-attachment-empty {
  justify-content: flex-start;
}
.cell-image-empty.busy,
.cell-attachment-empty.busy {
  opacity: 0.7;
}
.cell-image-upload {
  appearance: none;
  border: 1px dashed var(--border-strong);
  background: transparent;
  color: var(--text-dim);
  font: inherit;
  font-size: 12px;
  padding: 4px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  width: 100%;
  justify-content: center;
  transition:
    border-color var(--dur-fast) var(--ease),
    color var(--dur-fast) var(--ease),
    background-color var(--dur-fast) var(--ease);
}
.cell-image-upload:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-soft);
}
.cell-image-file {
  display: none;
}

.attachment-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  text-decoration: none;
  color: var(--text);
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 3px 9px;
  font-size: 12px;
  max-width: calc(100% - 24px);
  overflow: hidden;
}
.attachment-pill:hover {
  background: var(--bg-hover);
  border-color: var(--border-strong);
}
.attachment-name {
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
  max-width: 220px;
}

/* ── image modal ─────────────────────────────────────────────────────────── */

.image-modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.78);
  z-index: 120;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  animation: fade-in var(--dur) var(--ease);
}
.image-modal-shell {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: min(100%, 1200px);
  height: 100%;
  max-height: 100%;
}
.image-modal-toolbar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  color: #fff;
}
.image-modal-toolbar .btn.ghost {
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #fff;
}
.image-modal-toolbar .btn.ghost:hover {
  background: rgba(255, 255, 255, 0.18);
}
.image-modal-toolbar .icon-btn {
  color: #fff;
}
.image-modal-toolbar .icon-btn:hover {
  background: rgba(255, 255, 255, 0.15);
}
.image-modal-canvas {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
}
.image-modal-img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: var(--radius);
  box-shadow: 0 18px 60px rgba(0, 0, 0, 0.55);
  background: #111;
}

/* ── ViewBar: Copy-as-MD + Export menu ──────────────────────────────────── */

.view-spacer {
  flex: 1 1 auto;
}
.export-wrap {
  position: relative;
  display: inline-flex;
}
.export-menu {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  z-index: 40;
  min-width: 180px;
  display: flex;
  flex-direction: column;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow-lg);
  padding: 4px;
}
.export-item {
  display: block;
  padding: 7px 10px;
  background: transparent;
  border: 0;
  border-radius: 4px;
  color: var(--text);
  text-align: left;
  font-size: 13px;
  cursor: pointer;
}
.export-item:hover {
  background: var(--bg-hover);
}

/* ── Row grouping ───────────────────────────────────────────────────────── */

.group-wrap {
  position: relative;
  display: inline-flex;
}
.group-menu {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  z-index: 40;
  min-width: 180px;
  max-height: 320px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow-lg);
  padding: 4px;
}
.group-item {
  display: block;
  padding: 7px 10px;
  background: transparent;
  border: 0;
  border-radius: 4px;
  color: var(--text);
  text-align: left;
  font-size: 13px;
  cursor: pointer;
}
.group-item:hover {
  background: var(--bg-hover);
}
.group-item.active {
  background: var(--bg-hover);
  color: var(--accent);
  font-weight: 600;
}

/* Hover-revealed group actions (Expand all / Collapse all) below the Group
   button. A small padding-top bridge keeps the pointer inside .group-wrap so
   the flyout doesn't flicker when moving from the button to the actions. */
.group-actions {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 40;
  padding-top: 4px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 150px;
}
.group-actions::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
}
.group-action {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 10px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  text-align: left;
  font-size: 13px;
  cursor: pointer;
  box-shadow: var(--shadow-lg);
}
.group-action:hover {
  background: var(--bg-hover);
  color: var(--accent);
}

tr.group-row > .group-cell {
  padding: 0;
  background: var(--bg-elev);
  border-top: 1px solid var(--border);
  position: sticky;
  left: 0;
}
.group-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 7px 12px;
  background: transparent;
  border: 0;
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  text-align: left;
}
.group-toggle:hover {
  background: var(--bg-hover);
}
.group-toggle .group-key {
  color: var(--text-dim);
  font-weight: 500;
  text-transform: lowercase;
}
.group-toggle .group-key::after {
  content: ":";
  color: var(--text-dim);
}
.group-toggle .group-val {
  color: var(--text);
}
.group-toggle .group-count {
  margin-left: 6px;
  padding: 1px 7px;
  border-radius: 9px;
  background: var(--bg-hover);
  color: var(--text-soft);
  font-size: 11px;
  font-weight: 600;
}

/* Notion-style "+ New" button below each group's rows. */
tr.group-add-row > .group-add-cell {
  padding: 0;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.group-add-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  text-align: left;
  padding: 6px 12px 6px 30px;
  background: transparent;
  border: none;
  color: var(--text-soft);
  font-size: 13px;
  cursor: pointer;
}
.group-add-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
}


.rowpage {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow-y: auto;
  padding: 20px 24px 48px;
  gap: 20px;
}
.rowpage-head {
  display: flex;
  align-items: center;
  gap: 12px;
  position: sticky;
  top: 0;
  z-index: 5;
  padding-bottom: 12px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.rowpage-id {
  flex: 1 1 auto;
  min-width: 0;
  font-size: 20px;
  font-weight: 700;
  color: var(--text);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 6px 8px;
}
.rowpage-id:hover {
  border-color: var(--border);
}
.rowpage-id:focus {
  outline: none;
  border-color: var(--accent);
  background: var(--bg-elev);
}
.rowpage-head-actions {
  display: inline-flex;
  gap: 8px;
  flex: 0 0 auto;
}
.rowpage-grid {
  display: grid;
  grid-template-columns: minmax(320px, 460px) 1fr;
  gap: 28px;
  align-items: start;
}
@media (max-width: 880px) {
  .rowpage-grid {
    grid-template-columns: 1fr;
  }
}
.rowpage-h2 {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
  margin: 0 0 10px;
}
.rowpage-attrs {
  width: 100%;
  border-collapse: collapse;
}
.rowpage-attrs tr {
  border-bottom: 1px solid var(--border);
}
.rowpage-attr-key {
  text-align: left;
  vertical-align: middle;
  width: 38%;
  padding: 4px 12px 4px 0;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-soft);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
/* The reused grid <Cell> renders a <td>; relax its grid-specific sizing so it
 * sits comfortably in the attribute list. */
.rowpage-attrs td {
  padding: 2px 0;
  border: none;
}
.rowpage-empty {
  color: var(--text-dim);
  font-size: 13px;
}
.rowpage-content-sec {
  min-width: 0;
  display: flex;
  flex-direction: column;
}
.rowpage-content-sec.drag-over {
  outline: 2px dashed var(--accent);
  outline-offset: 4px;
  border-radius: 8px;
}
.rowpage-content-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}
.rowpage-content-actions {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.rowpage-body-input {
  width: 100%;
  min-height: 320px;
  resize: vertical;
  font-family: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace);
  font-size: 13px;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
}
.rowpage-body-input:focus {
  outline: none;
  border-color: var(--accent);
}
.rowpage-content-sec .body-split {
  flex: 1 1 auto;
}
.rowpage-content-sec .body-preview {
  min-height: 320px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 16px;
  background: var(--bg-elev);
  overflow-y: auto;
}

/* ── Formula column ─────────────────────────────────────────────────────── */

/* Formula values render right-aligned (most are numeric); use the system
 * mono stack so quote-style symbols / decimals line up neatly. */
td.cell-formula {
  text-align: right;
  font-variant-numeric: tabular-nums;
  padding: 6px 8px;
}
.cell-formula-val {
  font-family: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace);
  color: var(--text);
}
.cell-formula-spinner {
  color: var(--text-soft, #888);
  font-style: italic;
}
.cell-formula-err {
  color: var(--danger, #d23030);
  font-weight: 600;
  cursor: help;
}

/* Formula type-options block in the column-type popover: monospace textarea
 * for the formula body. The textarea itself wears `.type-options-formula`
 * (alongside the standard `.type-options-input`). */
textarea.type-options-formula {
  font-family: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace);
  font-size: 12px;
  min-height: 6em;
  line-height: 1.4;
  white-space: pre;
}
.type-menu-hint code {
  font-family: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace);
  font-size: 11px;
  background: var(--bg-subtle);
  padding: 0 4px;
  border-radius: 3px;
}

/* ── Row "more actions" menu (move row to another table) ─────────────────── */
.row-actions-menu-wrap {
  position: relative;
  display: inline-flex;
}
.row-more {
  border: none;
  background: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius-sm);
  opacity: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: opacity var(--dur-fast) var(--ease),
    color var(--dur-fast) var(--ease),
    background-color var(--dur-fast) var(--ease);
}
#grid tbody tr:hover .row-more,
#grid tbody tr:focus-within .row-more,
.row-actions-menu-wrap .row-more[aria-expanded="true"] {
  opacity: 1;
}
.row-more:hover {
  color: var(--text);
  background: var(--bg-hover);
}
.row-actions-menu {
  text-align: left;
  max-height: 320px;
}
.row-move-search {
  margin: 4px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
}
.row-move-list {
  display: flex;
  flex-direction: column;
  max-height: 220px;
  overflow-y: auto;
}

/* ── Custom actions modal ────────────────────────────────────────────────── */
.actions-modal {
  width: min(1280px, 96vw);
  max-width: 96vw;
  height: min(860px, 92vh);
  max-height: 92vh;
  display: flex;
  flex-direction: column;
}
.actions-body {
  display: grid;
  grid-template-columns: 220px 1fr;
  gap: 16px;
  flex: 1;
  min-height: 0;
}
.actions-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  border-right: 1px solid var(--border);
  padding-right: 12px;
  overflow-y: auto;
  min-height: 0;
}
.actions-list .actions-new {
  width: 100%;
  justify-content: center;
}
.actions-list ul {
  list-style: none;
  margin: 6px 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.actions-list li {
  display: flex;
  align-items: center;
  border-radius: 6px;
}
.actions-list li.active {
  background: var(--bg-hover);
}
.actions-list .action-pick {
  flex: 1;
  text-align: left;
  background: none;
  border: 0;
  color: var(--text);
  padding: 7px 8px;
  font-size: 13px;
  cursor: pointer;
  border-radius: 6px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.actions-list .action-pick:hover {
  background: var(--bg-hover);
}
.actions-list .action-del {
  opacity: 0.6;
}
.actions-list .action-del:hover {
  color: var(--danger);
  opacity: 1;
}
.actions-empty {
  color: var(--text-soft, #888);
  font-size: 12px;
  padding: 8px;
}
.actions-editor {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 0;
  min-height: 0;
}
.action-code {
  flex: 1;
  min-height: 320px;
  resize: vertical;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px;
  line-height: 1.5;
  tab-size: 2;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  white-space: pre;
  overflow: auto;
}
.actions-toolbar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.actions-meta-row {
  display: flex;
  gap: 8px;
  align-items: stretch;
}
.actions-meta-row .modal-input {
  margin-top: 0;
  flex: 1;
}
.action-kind {
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 9px 10px;
  font: inherit;
  font-size: 13px;
  color: var(--text);
  outline: none;
}
.action-fav-toggle {
  flex: 0 0 auto;
  color: var(--text-soft, #888);
}
.action-fav-toggle.on {
  color: #f5b301;
}
.action-fav {
  color: var(--text-soft, #888);
}
.action-fav.on {
  color: #f5b301;
}
.action-gen-row {
  display: flex;
  gap: 8px;
  align-items: stretch;
  margin-top: 8px;
}
.action-gen-row .modal-input {
  margin-top: 0;
  flex: 1;
}
.action-gen-btn {
  flex: 0 0 auto;
  white-space: nowrap;
}
.fav-action-btn {
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.actions-scope {
  font-weight: 400;
  font-size: 12px;
  color: var(--text-soft, #888);
}
.action-output {
  margin: 0;
  max-height: 180px;
  overflow: auto;
  background: var(--bg-elev, #111);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--text);
}
@media (max-width: 640px) {
  .actions-body {
    grid-template-columns: 1fr;
  }
  .actions-list {
    border-right: 0;
    border-bottom: 1px solid var(--border);
    padding-right: 0;
    padding-bottom: 10px;
    max-height: 160px;
  }
}

/* ── code editor (syntax highlight + autocomplete) ───────────────────────── */
.code-editor {
  position: relative;
  flex: 1;
  min-height: 320px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  overflow: visible;
}
/* The highlight layer and the textarea must share identical typography + box
   metrics so the coloured text sits exactly under the (transparent) caret. */
.code-editor-highlight,
.code-editor-input {
  margin: 0;
  padding: 10px 12px;
  border: 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px;
  line-height: 1.5;
  tab-size: 2;
  white-space: pre;
  overflow-wrap: normal;
  word-break: normal;
}
.code-editor-highlight {
  position: absolute;
  inset: 0;
  overflow: auto;
  color: var(--text);
  pointer-events: none;
  z-index: 0;
  border-radius: 8px;
}
.code-editor-input {
  position: relative;
  width: 100%;
  height: 100%;
  resize: none;
  background: transparent;
  color: transparent;
  caret-color: var(--text);
  outline: none;
  overflow: auto;
  z-index: 1;
  border-radius: 8px;
}
.code-editor-input::selection {
  background: rgba(120, 160, 255, 0.35);
}
/* Token colours (theme-aware via CSS vars with sensible fallbacks). */
.tok-comment { color: var(--text-soft, #7a8290); font-style: italic; }
.tok-string { color: #98c379; }
.tok-number { color: #d19a66; }
.tok-keyword { color: #c678dd; font-weight: 600; }
.tok-helper { color: #61afef; font-weight: 600; }
.tok-fn { color: #56b6c2; }

/* Autocomplete popup. */
.code-autocomplete {
  position: absolute;
  z-index: 20;
  margin: 2px 0 0;
  padding: 4px;
  list-style: none;
  min-width: 240px;
  max-width: 380px;
  max-height: 220px;
  overflow-y: auto;
  background: var(--bg-elev, #1b1f27);
  border: 1px solid var(--border-strong, var(--border));
  border-radius: 8px;
  box-shadow: var(--shadow-lg);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
}
.code-autocomplete li {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 5px 8px;
  border-radius: 6px;
  cursor: pointer;
  white-space: nowrap;
}
.code-autocomplete li.active {
  background: var(--bg-hover);
}
.code-autocomplete .ac-label {
  font-weight: 600;
  color: var(--text);
}
.code-autocomplete .ac-fn { color: #61afef; }
.code-autocomplete .ac-var { color: #e5c07b; }
.code-autocomplete .ac-keyword { color: #c678dd; }
.code-autocomplete .ac-detail {
  color: var(--text-soft, #8a919c);
  font-size: 11px;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── AI generation progress panel + diff ─────────────────────────────────── */
.action-gen-panel {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-elev, #14181f);
  overflow: hidden;
}
.action-gen-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-soft, #8a919c);
}
.action-gen-panel-title {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.action-gen-elapsed {
  color: var(--text);
  font-variant-numeric: tabular-nums;
}
.action-gen-spinner {
  width: 12px;
  height: 12px;
  border: 2px solid var(--border-strong, #444);
  border-top-color: #61afef;
  border-radius: 50%;
  display: inline-block;
  animation: gen-spin 0.7s linear infinite;
}
@keyframes gen-spin {
  to { transform: rotate(360deg); }
}
.action-gen-progress {
  margin: 0;
  max-height: 150px;
  min-height: 40px;
  overflow: auto;
  padding: 8px 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11.5px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--text);
}
.action-diff-btn {
  padding: 3px 8px;
  font-size: 12px;
}
.action-diff-stat .diff-add { color: #98c379; }
.action-diff-stat .diff-del { color: #e06c75; }
.diff-backdrop {
  z-index: 60;
}
.diff-modal {
  width: min(900px, 94vw);
  max-width: 94vw;
  max-height: 86vh;
  display: flex;
  flex-direction: column;
}
.diff-view {
  margin: 0;
  flex: 1;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  line-height: 1.5;
}
.diff-line {
  display: flex;
  gap: 8px;
  padding: 0 10px;
  white-space: pre-wrap;
  word-break: break-word;
}
.diff-gutter {
  flex: 0 0 12px;
  color: var(--text-soft, #8a919c);
  user-select: none;
}
.diff-add {
  background: rgba(80, 200, 120, 0.14);
}
.diff-add .diff-gutter { color: #98c379; }
.diff-del {
  background: rgba(224, 108, 117, 0.14);
}
.diff-del .diff-gutter { color: #e06c75; }


/* ── app-wide auth gate (MDTABULATE_PASSWORD) ─────────────────────────────── */
.auth-gate-checking {
  position: fixed;
  inset: 0;
  background: var(--bg);
}
.auth-gate {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--bg);
}
.auth-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  width: 100%;
  max-width: 340px;
  padding: 28px 24px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow-lg);
  text-align: center;
}
.auth-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--bg-hover);
  color: var(--accent);
}
.auth-title {
  margin: 4px 0 0;
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
}
.auth-sub {
  margin: 0 0 4px;
  font-size: 13px;
  color: var(--text-dim);
}
.auth-input {
  width: 100%;
  padding: 9px 12px;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.auth-input:focus {
  outline: none;
  border-color: var(--accent);
}
.auth-error {
  width: 100%;
  font-size: 12px;
  color: var(--danger, #d33);
  text-align: left;
}
.auth-submit {
  width: 100%;
  justify-content: center;
  margin-top: 4px;
}
