.custom-template-page {
  --custom-template-swatch-size: 0.8125rem;
  --custom-template-swatch-size-emphasis: 0.875rem;
  --custom-template-picker-size: 1.5rem;
  --custom-template-index-badge-size: 1.375rem;
  --custom-template-picker-column: 3.75rem;
  --custom-template-hex-column: 8.25rem;
  --custom-template-preview-label-width: 3.5rem;
  --custom-template-dot-size: 18px;
}

.custom-template-body .schematic-content {
  gap: var(--space-3);
}

.custom-template-page .schematic-upload-card .card-content,
.custom-template-page .glass-card .card-content {
  gap: var(--space-2);
}

.custom-template-page .glass-card .card-header {
  gap: var(--space-2);
}

.custom-template-toolbar,
.custom-template-specials,
.custom-template-editor-grid,
.custom-template-editor-stack,
.custom-template-side-stack,
.custom-template-inline-actions,
.custom-template-list,
.custom-template-level-list,
.custom-template-mapping-list,
.custom-template-preview-layout,
.custom-template-preview-summary {
  display: grid;
  gap: var(--space-2);
}

.custom-template-preview-section {
  display: grid;
  gap: var(--space-1);
}

.custom-template-preview-section-title {
  color: var(--muted-foreground);
  font-size: var(--fs-xs);
  font-weight: 700;
  line-height: var(--lh-xs);
}

.custom-template-toolbar,
.custom-template-specials {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.custom-template-editor-grid {
  grid-template-columns: minmax(0, 1.1fr) minmax(21rem, 0.9fr);
  align-items: start;
}

.custom-template-side-stack {
  position: sticky;
  top: calc(var(--space-4) + 4.5rem);
}

.custom-template-inline-actions {
  grid-auto-flow: column;
  grid-auto-columns: max-content;
  justify-content: start;
  align-items: center;
}

.custom-template-section-header {
  align-items: center;
  justify-content: space-between;
}

.custom-template-special-item,
.custom-template-color-row,
.custom-template-level-card,
.custom-template-preview-shell,
.custom-template-preview-palette-item,
.custom-template-preview-summary-row {
  border: 1px solid color-mix(in srgb, var(--border) 84%, transparent);
  border-radius: var(--radius-lg);
  background: color-mix(in srgb, var(--muted) 32%, transparent);
}

.custom-template-special-item {
  display: flex;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  align-items: flex-start;
}

.custom-template-special-title {
  font-size: var(--fs-sm);
  font-weight: 600;
  line-height: var(--lh-sm);
}

.custom-template-swatch {
  width: var(--custom-template-swatch-size);
  height: var(--custom-template-swatch-size);
  border-radius: var(--radius-full);
  border: 1px solid color-mix(in srgb, var(--foreground) 18%, transparent);
  background: var(--swatch-color, transparent);
  flex: 0 0 auto;
}

.custom-template-color-row {
  display: grid;
  grid-template-columns: 2.25rem minmax(0, 1fr) auto;
  gap: var(--space-2);
  padding: var(--space-2);
  align-items: end;
}

.custom-template-color-index {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100%;
}

.custom-template-color-index-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--custom-template-index-badge-size);
  height: var(--custom-template-index-badge-size);
  border-radius: var(--radius-full);
  background: color-mix(in srgb, var(--primary) 16%, transparent);
  color: var(--primary);
  font-size: var(--fs-xs);
  font-weight: 700;
  line-height: 1;
}

.custom-template-color-main {
  display: grid;
  grid-template-columns: minmax(0, 1.2fr) var(--custom-template-picker-column) var(--custom-template-hex-column);
  gap: var(--space-2);
  align-items: end;
}

.custom-template-color-picker-wrap,
.custom-template-mapping-color-select {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: var(--space-2);
  align-items: center;
}

.custom-template-color-picker-wrap .custom-template-swatch,
.custom-template-mapping-color-select .custom-template-swatch {
  width: var(--custom-template-swatch-size-emphasis);
  height: var(--custom-template-swatch-size-emphasis);
}

.custom-template-color-picker {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}

.custom-template-color-picker-wrap {
  justify-content: center;
}

.custom-template-color-picker-trigger {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--custom-template-picker-size);
  height: var(--custom-template-picker-size);
  border-radius: var(--radius-full);
  background: var(--swatch-color, transparent);
  border: 1px solid color-mix(in srgb, var(--foreground) 18%, transparent);
  overflow: hidden;
  cursor: pointer;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.custom-template-color-picker-ring {
  position: absolute;
  inset: 1px;
  border-radius: inherit;
  border: 1px solid color-mix(in srgb, var(--foreground) 12%, transparent);
  pointer-events: none;
}

.custom-template-color-picker-trigger:focus-within {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--primary) 16%, transparent);
}

.custom-template-hex-input,
.custom-template-json-output {
  font-family: var(--font-mono);
}

.custom-template-row-actions {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: max-content;
  gap: var(--space-1);
  align-items: center;
  justify-content: end;
}

.custom-template-status {
  color: var(--muted-foreground);
  font-size: var(--fs-xs);
  line-height: var(--lh-xs);
}

.custom-template-level-card {
  display: grid;
  gap: var(--space-2);
  padding: var(--space-2);
}

.custom-template-level-header {
  display: grid;
  grid-template-columns: 8rem auto;
  gap: var(--space-2);
  align-items: center;
}

.custom-template-level-badge-group {
  display: grid;
  gap: var(--space-1);
  align-items: start;
}

.custom-template-level-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 2rem;
  padding: 0 0.75rem;
  border: 1px solid color-mix(in srgb, var(--border) 86%, transparent);
  border-radius: var(--radius-md);
  background: color-mix(in srgb, var(--background) 72%, transparent);
  color: var(--foreground);
  font-family: var(--font-mono);
  font-size: var(--fs-sm);
  font-weight: 700;
}

.custom-template-mapping-row {
  display: grid;
  grid-template-columns: minmax(0, 0.66fr) minmax(0, 1fr) auto;
  gap: var(--space-2);
  align-items: end;
  padding: var(--space-2);
  border-radius: var(--radius-md);
  background: color-mix(in srgb, var(--background) 58%, transparent);
}

.custom-template-mapping-actions {
  display: flex;
  align-items: end;
}

.custom-template-preview-shell {
  display: grid;
  gap: var(--space-2);
  min-height: 18rem;
  padding: var(--space-2);
}

.custom-template-preview-empty,
.custom-template-empty {
  display: grid;
  place-items: center;
  min-height: 7rem;
  padding: var(--space-3);
  border: 1px dashed color-mix(in srgb, var(--border) 82%, transparent);
  border-radius: var(--radius-lg);
  color: var(--muted-foreground);
  font-size: var(--fs-sm);
  text-align: center;
}

.custom-template-preview-grid {
  display: grid;
  gap: var(--space-2);
}

.custom-template-source-palette {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1);
  align-items: center;
}

.custom-template-source-cell,
.custom-template-sequence-cell {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  min-height: 1.75rem;
  border-radius: var(--radius-sm);
}

.custom-template-source-cell,
.custom-template-sequence-cell {
  padding: 0 0.5rem;
  border: 1px solid color-mix(in srgb, var(--border) 84%, transparent);
  background: color-mix(in srgb, var(--background) 68%, transparent);
  cursor: grab;
  user-select: none;
  transition:
    border-color 0.18s ease,
    background-color 0.18s ease,
    box-shadow 0.18s ease,
    opacity 0.18s ease;
}

.custom-template-source-cell:hover,
.custom-template-sequence-cell:hover {
  border-color: color-mix(in srgb, var(--primary) 56%, var(--border) 44%);
  background: color-mix(in srgb, var(--primary) 8%, var(--background) 92%);
}

.custom-template-source-cell.is-unmapped {
  border-style: dashed;
  background: color-mix(in srgb, var(--muted) 24%, transparent);
}

.custom-template-source-cell-dot {
  width: var(--custom-template-dot-size);
  height: var(--custom-template-dot-size);
  border-radius: 0;
  background: var(--dot-color);
  border: 1px solid color-mix(in srgb, var(--foreground) 18%, transparent);
  flex: 0 0 auto;
}

.custom-template-source-cell-copy {
  display: grid;
  gap: 1px;
  min-width: 0;
}

.custom-template-source-cell-name {
  font-size: var(--fs-xs);
  line-height: var(--lh-xs);
  color: var(--foreground);
}

.custom-template-source-cell-meta {
  color: var(--muted-foreground);
  font-size: 0.6875rem;
  line-height: 1.15;
}

.custom-template-preview-palette {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(8.5rem, 1fr));
  gap: var(--space-1);
}

.custom-template-preview-palette-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: var(--space-2);
  padding: 0.5rem 0.625rem;
}

.custom-template-preview-palette-main {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-width: 0;
}

.custom-template-preview-palette-name,
.custom-template-preview-summary-text {
  min-width: 0;
  font-size: var(--fs-sm);
  line-height: var(--lh-sm);
}

.custom-template-preview-palette-meta,
.custom-template-preview-summary-y,
.custom-template-preview-summary-empty,
.mapping-dot-label {
  color: var(--muted-foreground);
  font-size: var(--fs-xs);
  line-height: var(--lh-xs);
}

.custom-template-preview-dot-wrap {
  padding: var(--space-2);
}

.custom-template-preview-inline-note {
  margin-bottom: var(--space-2);
  color: var(--muted-foreground);
  font-size: var(--fs-xs);
  line-height: var(--lh-sm);
}

.mapping-cross-wrap {
  overflow: hidden;
}

.mapping-cross-section {
  overflow-x: auto;
}

.mapping-dot-cross {
  display: flex;
  flex-direction: column;
  gap: 0;
  min-width: max-content;
}

.mapping-dot-row {
  display: grid;
  grid-template-columns: var(--custom-template-preview-label-width) auto;
  align-items: stretch;
  gap: var(--space-2);
  min-height: var(--custom-template-dot-size);
}

.mapping-dot-label {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  font-weight: 600;
  min-height: var(--custom-template-dot-size);
  white-space: nowrap;
  line-height: 1;
}

.mapping-dot-track {
  display: grid;
  grid-template-columns: repeat(var(--dot-cols), var(--custom-template-dot-size));
  grid-auto-rows: var(--custom-template-dot-size);
  column-gap: 1px;
  row-gap: 0;
}

.mapping-dot-cell {
  width: var(--custom-template-dot-size);
  height: var(--custom-template-dot-size);
  border: none;
  box-sizing: border-box;
  background: var(--dot-color);
  cursor: grab;
  user-select: none;
  transition:
    opacity 0.18s ease,
    outline-color 0.18s ease,
    transform 0.18s ease;
}

.mapping-dot-cell.is-air {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  cursor: default;
}

.mapping-dot-air-marker {
  width: 0.5rem;
  height: 0.5rem;
  border: 1px solid color-mix(in srgb, var(--muted-foreground) 56%, transparent);
  border-radius: var(--radius-full);
  box-sizing: border-box;
}

.mapping-dot-cell:not(.is-air):hover {
  transform: translateY(-0.5px);
}

.mapping-dot-cell.is-dragging,
.custom-template-source-cell.is-dragging {
  opacity: 0.44;
}

.mapping-dot-cell.is-drop-active,
.custom-template-sequence-cell.is-drop-active {
  outline: 1px solid var(--primary);
  outline-offset: 1px;
  background: color-mix(in srgb, var(--primary) 10%, transparent);
}

.mapping-dot-cell.is-pending-remove,
.custom-template-sequence-cell.is-pending-remove,
.mapping-dot-sequence-cell.is-pending-remove {
  background: color-mix(in srgb, var(--muted) 78%, transparent);
  color: var(--muted-foreground);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: var(--fs-xs);
  font-weight: 700;
}

.mapping-dot-row-sequence {
  margin-bottom: 1px;
}

.mapping-dot-sequence-cell {
  cursor: grab;
  position: relative;
}

.mapping-dot-sequence-cell:not(.is-pending-remove):hover {
  transform: translateY(-0.5px);
}

.mapping-dot-sequence-cell.is-drop-replace {
  outline: 1px solid var(--primary);
  outline-offset: 1px;
  background: color-mix(in srgb, var(--primary) 10%, var(--dot-color) 90%);
}

.mapping-dot-sequence-cell.is-drop-insert-before,
.mapping-dot-sequence-cell.is-drop-insert-after {
  outline: none;
  background: var(--dot-color);
}

.mapping-dot-sequence-cell.is-drop-insert-before::before,
.mapping-dot-sequence-cell.is-drop-insert-after::after {
  position: relative;
  content: "";
  position: absolute;
  width: 2px;
  top: 0;
  bottom: 0;
  background: var(--primary);
  opacity: 1;
  box-shadow: 0 0 0 1px color-mix(in srgb, var(--primary) 18%, transparent);
}

.mapping-dot-sequence-cell.is-drop-insert-before::before {
  left: -1px;
}

.mapping-dot-sequence-cell.is-drop-insert-after::after {
  right: -1px;
}

.custom-template-preview-summary-row {
  display: grid;
  grid-template-columns: var(--custom-template-preview-label-width) minmax(0, 1fr);
  gap: var(--space-2);
  align-items: start;
  padding: var(--space-2) var(--space-3);
}

.custom-template-preview-summary-y {
  font-family: var(--font-mono);
  font-weight: 700;
  padding-top: 2px;
}

.custom-template-preview-summary-blocks {
  display: grid;
  gap: 0.375rem;
}

.custom-template-preview-summary-line {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-width: 0;
}

.custom-template-json-output {
  width: 100%;
  min-height: 18rem;
  padding: var(--space-3);
  border: 1px solid color-mix(in srgb, var(--border) 86%, transparent);
  border-radius: var(--radius-lg);
  background: color-mix(in srgb, var(--background) 72%, transparent);
  color: var(--foreground);
  font-size: var(--fs-xs);
  line-height: 1.6;
  resize: vertical;
}

@media (max-width: 1200px) {
  .custom-template-editor-grid,
  .custom-template-toolbar,
  .custom-template-specials {
    grid-template-columns: 1fr;
  }

  .custom-template-side-stack {
    position: static;
  }
}

@media (max-width: 840px) {
  .custom-template-color-row,
  .custom-template-color-main,
  .custom-template-level-header,
  .custom-template-mapping-row,
  .custom-template-preview-summary-row {
    grid-template-columns: 1fr;
  }

  .custom-template-row-actions,
  .custom-template-inline-actions {
    grid-auto-flow: row;
    grid-auto-columns: 1fr;
  }
}
