:root {
  color-scheme: light;
  --bg: #f6f5f4;
  --panel: #ffffff;
  --surface: #ffffff;
  --row: #ffffff;
  --row-alt: #f1f0ee;
  --hover: rgba(31, 30, 28, 0.06);
  --text: #1f1e1c;
  --muted: #615d59;
  --faint: #8a857f;
  --line: rgba(31, 30, 28, 0.12);
  --line-soft: rgba(31, 30, 28, 0.07);
  --accent: #5e6ad2;
  --accent-hot: #4a54c0;
  --ok: #1b7a34;
  --danger: #c24141;
  --btn: rgba(31, 30, 28, 0.04);
  --shadow: 0 12px 32px rgba(31, 30, 28, 0.12);
  --shadow-dialog: 0 24px 64px rgba(31, 30, 28, 0.18);
  --backdrop: rgba(31, 30, 28, 0.4);
  --ease-out: cubic-bezier(0.23, 1, 0.32, 1);
  --sans:
    -apple-system, blinkmacsystemfont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei",
    "Noto Sans SC", sans-serif;
  --mono: ui-monospace, "SF Mono", menlo, consolas, monospace;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100dvh;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 var(--sans);
}

.shell {
  display: grid;
  grid-template-rows: auto 1fr;
  height: 100%;
  background: var(--bg);
}

.top {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 16px;
  align-items: center;
  padding: 12px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line-soft);
}

.brand {
  display: flex;
  gap: 10px;
  align-items: center;
  min-width: 0;
}

.mark {
  width: 10px;
  height: 10px;
  border-radius: 3px;
  background: var(--accent);
  box-shadow: 0 0 0 3px rgba(94, 106, 210, 0.18);
}

h1 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.01em;
}

.sub {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
}

.sheets {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.sheets button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
  font: 13px/1.2 var(--sans);
  font-weight: 500;
}

.sheets button[aria-current="page"] {
  background: rgba(94, 106, 210, 0.12);
  color: var(--text);
}

@media (hover: hover) and (pointer: fine) {
  .sheets button:hover {
    background: var(--hover);
    color: var(--text);
  }
}

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

input,
button,
code {
  font: inherit;
}

#query {
  width: 12rem;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  border-radius: 6px;
  padding: 6px 10px;
}

#query::placeholder {
  color: var(--faint);
}

#query:focus-visible,
dialog input:focus-visible,
button:focus-visible {
  outline: 2px solid var(--accent-hot);
  outline-offset: 1px;
}

button {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--btn);
  color: var(--text);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition:
    transform 140ms var(--ease-out),
    background-color 140ms var(--ease-out);
}

button:active {
  transform: scale(0.97);
}

button.primary {
  background: var(--accent);
  border-color: transparent;
  color: #fff;
}

button.ghost {
  color: var(--muted);
}

.count,
.role {
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

#table {
  min-height: 0;
  height: 100%;
  background: var(--bg);
}

.catalog-table {
  position: relative;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  height: 100%;
  min-height: 0;
  background: transparent;
  color: var(--text);
  font: 13px/1.45 var(--sans);
}

.catalog-table-scroll {
  min-height: 0;
  overflow: auto;
}

.catalog-table table {
  border-collapse: separate;
  border-spacing: 0;
  table-layout: fixed;
  min-width: 100%;
  width: max-content;
}

.catalog-table thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--panel);
  color: var(--muted);
  font-weight: 500;
  border-bottom: 1px solid var(--line);
  border-right: 1px solid var(--line-soft);
  padding: 0;
  text-align: left;
}

.catalog-table .th-row {
  display: flex;
  align-items: center;
  min-height: 36px;
}

.catalog-table .th-btn {
  appearance: none;
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1;
  min-width: 0;
  border: 0;
  background: transparent;
  color: inherit;
  padding: 8px 4px 8px 10px;
  font: inherit;
  font-weight: 500;
  text-align: inherit;
  cursor: pointer;
  border-radius: 0;
}

.catalog-table .th-btn:disabled {
  cursor: default;
}

.catalog-table .th-btn[data-sort="asc"]::after {
  content: "↑";
  color: var(--accent-hot);
  font-size: 11px;
}

.catalog-table .th-btn[data-sort="desc"]::after {
  content: "↓";
  color: var(--accent-hot);
  font-size: 11px;
}

.catalog-table .th-filter-btn {
  appearance: none;
  flex: none;
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  margin: 0 6px 0 0;
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--faint);
  cursor: pointer;
}

.catalog-table .th-filter-btn svg {
  display: block;
}

.catalog-table .th-filter-btn.is-on {
  color: var(--accent-hot);
}

.catalog-table .th-resizer {
  position: absolute;
  top: 0;
  right: 0;
  z-index: 3;
  width: 7px;
  height: 100%;
  cursor: col-resize;
  user-select: none;
  touch-action: none;
}

.catalog-table td {
  border-right: 1px solid var(--line-soft);
  border-bottom: 1px solid var(--line-soft);
  padding: 6px 10px;
  color: var(--text);
  background: var(--row);
  min-height: 34px;
  vertical-align: middle;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.catalog-table tbody tr:nth-child(even) td {
  background: var(--row-alt);
}

.catalog-table [data-pin="start"],
.catalog-table [data-pin="end"] {
  position: sticky;
}

.catalog-table thead [data-pin="start"],
.catalog-table thead [data-pin="end"] {
  z-index: 4;
  background: var(--panel);
}

.catalog-table tbody [data-pin="start"],
.catalog-table tbody [data-pin="end"] {
  z-index: 1;
}

.catalog-table [data-pin="start"][data-pin-last] {
  box-shadow: 6px 0 8px -6px rgba(31, 30, 28, 0.28);
}

.catalog-table [data-pin="end"][data-pin-first] {
  box-shadow: -6px 0 8px -6px rgba(31, 30, 28, 0.28);
}

@media (hover: hover) and (pointer: fine) {
  .catalog-table tbody tr:hover td {
    background: var(--hover);
  }
  .catalog-table .th-btn:not(:disabled):hover {
    color: var(--text);
    background: var(--hover);
  }
  .catalog-table .th-filter-btn:hover {
    color: var(--text);
    background: var(--hover);
  }
  .catalog-table .th-resizer:hover,
  .catalog-table .th-resizer:active {
    background: rgba(94, 106, 210, 0.45);
  }
}

.catalog-table tbody tr.row-error td {
  background: rgba(194, 65, 65, 0.1);
}

.catalog-table-empty {
  position: absolute;
  inset: 44px 0 48px;
  display: grid;
  place-items: center;
  color: var(--muted);
  pointer-events: none;
}

.catalog-table-pager {
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: flex-end;
  padding: 8px 16px;
  background: var(--panel);
  border-top: 1px solid var(--line-soft);
}

.catalog-table-pager .pager-status {
  min-width: 9.5rem;
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  text-align: center;
}

.catalog-table-pager button:disabled {
  opacity: 0.4;
  cursor: default;
  transform: none;
}

.catalog-table td.is-editing {
  box-shadow: inset 0 0 0 2px var(--accent-hot);
  background: var(--surface);
}

.catalog-table td.is-editing-inline {
  padding: 2px 6px;
  box-shadow: none;
}

.catalog-table .cell-input {
  width: 100%;
  min-width: 4rem;
  border: 1px solid var(--accent-hot);
  background: var(--surface);
  color: var(--text);
  border-radius: 4px;
  padding: 4px 8px;
  font: 13px var(--sans);
}

.catalog-table .tick {
  color: var(--ok);
}

.catalog-table .cross {
  color: var(--faint);
}

.catalog-cell-editor {
  position: fixed;
  z-index: 22;
  width: min(520px, calc(100vw - 16px));
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow-dialog);
  color: var(--text);
  padding: 10px;
}

.catalog-cell-editor .cell-editor-title {
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 12px;
}

.catalog-cell-editor .cell-editor-input {
  display: block;
  width: 100%;
  min-height: 5.5rem;
  resize: vertical;
  border: 1px solid var(--accent-hot);
  background: var(--bg);
  color: var(--text);
  border-radius: 6px;
  padding: 8px 10px;
  font: 13px/1.45 var(--sans);
}

.catalog-cell-editor .cell-editor-hint {
  margin-top: 6px;
  color: var(--faint);
  font-size: 11px;
}

.catalog-list-popup {
  position: fixed;
  z-index: 20;
  max-height: 22rem;
  overflow: auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  color: var(--text);
  padding: 4px;
}

.catalog-list-popup .list-sequence {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 2px 0 6px;
  margin-bottom: 4px;
  border-bottom: 1px solid var(--line-soft);
}

.catalog-list-popup .list-sequence-row {
  display: grid;
  grid-template-columns: 1fr auto auto auto;
  gap: 2px;
  align-items: center;
}

.catalog-list-popup .list-sequence-row span {
  min-width: 0;
  padding: 4px 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.catalog-list-popup .list-seq-btn {
  width: auto;
  padding: 4px 8px;
  color: var(--muted);
}

.catalog-list-popup button {
  display: block;
  width: 100%;
  border: 0;
  background: transparent;
  color: inherit;
  text-align: left;
  border-radius: 6px;
  padding: 6px 10px;
  font: 13px var(--sans);
}

.catalog-list-popup button.active,
.catalog-list-popup button:hover {
  background: rgba(94, 106, 210, 0.12);
}

.catalog-list-popup .list-empty {
  padding: 8px 10px;
  color: var(--muted);
}

.catalog-filter-popup,
.catalog-columns-popup {
  position: fixed;
  z-index: 21;
  max-height: 22rem;
  overflow: auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  color: var(--text);
  padding: 6px;
}

.catalog-filter-popup .filter-search {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--text);
  border-radius: 6px;
  padding: 6px 8px;
  margin-bottom: 4px;
}

.catalog-filter-popup .filter-list button,
.catalog-columns-popup button {
  display: block;
  width: 100%;
  border: 0;
  background: transparent;
  color: inherit;
  text-align: left;
  border-radius: 6px;
  padding: 6px 10px;
  font: 13px var(--sans);
}

.catalog-filter-popup .filter-list button.active,
.catalog-filter-popup .filter-list button:hover,
.catalog-columns-popup button.active,
.catalog-columns-popup button:hover {
  background: rgba(94, 106, 210, 0.12);
}

.catalog-filter-popup .filter-clear,
.catalog-columns-popup .columns-reset {
  margin-top: 4px;
  color: var(--muted);
}

.catalog-columns-popup .columns-title {
  padding: 4px 8px 8px;
  color: var(--muted);
  font-size: 12px;
}

.catalog-columns-popup .columns-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 4px;
  align-items: center;
  padding: 2px 0;
}

.catalog-columns-popup .columns-row label {
  display: flex;
  gap: 8px;
  align-items: center;
  min-width: 0;
  padding: 4px 8px;
  font: 13px var(--sans);
}

.catalog-columns-popup .columns-row button {
  width: auto;
  padding: 4px 8px;
  color: var(--muted);
}

.id-mono {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.id-auto {
  color: var(--faint);
}

.ref {
  color: var(--text);
}

.row-del {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  padding: 0 4px;
  font: 12px var(--sans);
}

@media (hover: hover) and (pointer: fine) {
  .row-del:hover {
    color: var(--danger);
  }
  button.primary:hover {
    background: var(--accent-hot);
  }
}

.toast {
  position: fixed;
  right: 16px;
  bottom: 16px;
  max-width: 28rem;
  padding: 10px 14px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--text);
  opacity: 0;
  transform: translateY(8px) scale(0.98);
  pointer-events: none;
  transition:
    opacity 180ms var(--ease-out),
    transform 180ms var(--ease-out);
}

.toast[data-show="1"] {
  opacity: 1;
  transform: none;
}

.toast.is-error {
  border-color: rgba(194, 65, 65, 0.35);
  color: var(--danger);
}

.toast.is-ok {
  border-color: rgba(39, 166, 68, 0.35);
}

dialog {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  padding: 20px;
  width: min(22rem, 92vw);
  box-shadow: var(--shadow-dialog);
}

dialog::backdrop {
  background: var(--backdrop);
}

dialog h2 {
  margin: 0 0 8px;
  font-size: 16px;
  font-weight: 600;
}

dialog p {
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 13px;
}

dialog input {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--text);
  border-radius: 6px;
  padding: 8px 10px;
}

dialog code {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--accent-hot);
}

.dialog-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 16px;
}

@media (prefers-reduced-motion: reduce) {
  button,
  .toast {
    transition: none;
  }
}

@media (max-width: 860px) {
  .top {
    grid-template-columns: 1fr;
    height: auto;
  }
}
