:root {
  --font-sans: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --text-xs: 12px;
  --text-sm: 14px;
  --text-base: 16px;
  --text-lg: 20px;
  --text-xl: 24px;

  --space-half: 2px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;

  --radius-sm: 4px;
  --radius-md: 6px;
  --radius-lg: 8px;

  --sidebar-width: 320px;
}

:root {
  color-scheme: light;

  --color-bg-base: #f4f6f8;
  --color-bg-surface: #ffffff;
  --color-bg-surface-alt: #eef2f5;

  --color-text-primary: #182029;
  --color-text-secondary: #435063;
  --color-text-muted: #66717f;

  --color-border-subtle: #cfd8e2;
  --color-border-strong: #9eabb8;

  --color-primary: #167f6b;
  --color-primary-dark: #0d5f50;
  --color-danger: #a83a32;
  --color-info: #3f6e9d;

  --color-overlay: color-mix(in srgb, var(--color-bg-base), transparent 4%);
  --color-shadow: color-mix(in srgb, var(--color-text-primary), transparent 84%);

  --color-slot-bg: #d8dee6;
  --color-slot-checker: #bdc7d1;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;

    --color-bg-base: #0f1419;
    --color-bg-surface: #1a2026;
    --color-bg-surface-alt: #242b33;

    --color-text-primary: #e1e4e8;
    --color-text-secondary: #c9d1d9;
    --color-text-muted: #8b949e;

    --color-border-subtle: #30363d;
    --color-border-strong: #484f58;

    --color-primary: #1fb99c;
    --color-primary-dark: #167f6b;
    --color-danger: #f85149;
    --color-info: #58a6ff;
    
    --color-overlay: color-mix(in srgb, var(--color-bg-base), transparent 4%);
    --color-shadow: rgb(0 0 0 / 45%);

    --color-slot-bg: #161b22;
    --color-slot-checker: #21262d;
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
  background: var(--color-bg-base);
  color: var(--color-text-primary);
  font-family: var(--font-sans);
}

.wasm-overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: none;
  place-items: center;
  padding: var(--space-5);
  background: var(--color-overlay);
}

.wasm-loading .wasm-overlay,
.wasm-failed .wasm-overlay {
  display: grid;
}

.wasm-overlay-panel {
  width: min(520px, 100%);
  padding: var(--space-5);
  border: 1px solid var(--color-border-subtle);
  border-radius: var(--radius-lg);
  background: var(--color-bg-surface);
  box-shadow: 0 18px 48px var(--color-shadow);
}

.wasm-overlay-panel h2 {
  margin: 0 0 var(--space-2);
  font-size: var(--text-lg);
  letter-spacing: 0;
}

.wasm-overlay-panel p {
  margin: 0;
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  line-height: 1.45;
}

.wasm-failed .wasm-overlay-panel {
  border-color: color-mix(in srgb, var(--color-danger) 35%, var(--color-border-subtle));
}
.wasm-failed .wasm-overlay-panel h2 {
  color: var(--color-danger);
}

.sidebar {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow-y: auto;
  padding: var(--space-4);
  border-right: 1px solid var(--color-border-subtle);
  background: var(--color-bg-surface);
}

.title-block h1 {
  margin: 0;
  font-size: var(--text-lg);
}

.title-block p {
  margin: var(--space-2) 0 var(--space-4);
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  line-height: 1.35;
}

.settings,
.result-panel {
  display: grid;
  gap: var(--space-3);
}

.field {
  display: grid;
  gap: var(--space-1);
  font-size: var(--text-sm);
}

fieldset.field {
  border: 0;
  padding: 0;
  margin: 0;
  min-width: 0;
}

.field-label {
  color: var(--color-text-secondary);
  font-weight: 600;
  line-height: 1.25;
}

legend.field-label {
  display: contents;
}

.texture-format-label {
  display: flex;
  flex-direction: column;
  gap: var(--space-half);
  font-weight: 600;
}
.texture-format-label small {
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-weight: 500; 
}

.input-group {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
  gap: var(--space-1) var(--space-3);
}

.input-group div {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.option-group {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: var(--space-1) var(--space-2);
  padding: 0;
}

.option-group label {
  display: grid;
}

.option-group input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.option-group label > span {
  min-height: 54px;
  display: grid;
  align-content: start;
  gap: var(--space-half);
  padding: var(--space-2);
  border: 1px solid var(--color-border-subtle);
  border-radius: var(--radius-md);
  background: var(--color-bg-surface);
  color: var(--color-text-secondary);
  text-align: left;
  cursor: pointer;
}

.option-group strong {
  color: var(--color-text-primary);
  font-size: var(--text-sm);
}

.option-group small {
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-weight: 500;
}

.option-group input:checked + span {
  border-color: var(--color-primary);
  background: color-mix(in srgb, var(--color-primary) 12%, var(--color-bg-surface));
}

.option-group input:checked + span strong {
  color: var(--color-primary);
}

.option-group input:focus-visible + span {
  outline: 2px solid color-mix(in srgb, var(--color-primary) 28%, transparent);
}

.option-group input:disabled + span {
  cursor: not-allowed;
  opacity: .58;
}

.inline-input {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 38px;
  align-items: center;
  gap: var(--space-2);
}

.input-unit {
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-weight: 600;
}

input[type="number"] {
  width: 100%;
  min-height: 34px;
  padding: var(--space-2);
  border: 1px solid var(--color-border-subtle);
  border-radius: var(--radius-md);
  background: var(--color-bg-surface);
  color: var(--color-text-primary);
  font: inherit;
  font-weight: 600;
}

input[type="number"]:focus {
  outline: 2px solid color-mix(in srgb, var(--color-primary) 28%, transparent);
  border-color: var(--color-primary);
}

input:disabled {
  opacity: .58;
}

.result-panel {
  margin-top: var(--space-4);
  padding: var(--space-3);
  border: 1px solid var(--color-border-subtle);
  border-radius: var(--radius-lg);
  background: var(--color-bg-surface-alt);
}

.result-row {
  display: flex;
  justify-content: space-between;
  gap: var(--space-3);
  font-size: var(--text-sm);
}

.result-row span {
  color: var(--color-text-muted);
}
.result-row strong {
  text-align: right;
}

.status-text {
  text-align: center;
  margin: 0;
  color: var(--color-text-muted);
  font-size: var(--text-xs);
}
.status-text.error {
  color: var(--color-danger);
}

.export-button {
  width: 100%;
  min-height: 40px;
  margin-top: var(--space-3);
  border: 0;
  border-radius: var(--radius-md);
  background: var(--color-primary);
  color: #fff;
  font-weight: 700;
  cursor: pointer;
}
.export-button:hover:not(:disabled) {
  background: var(--color-primary-dark);
}
.export-button:disabled {
  cursor: not-allowed; opacity: .52;
}

.import-dialog {
  width: min(620px, calc(100vw - var(--space-5) * 2));
  padding: 0;
  border: 1px solid var(--color-border-subtle);
  border-radius: var(--radius-lg);
  background: var(--color-bg-surface);
  color: var(--color-text-primary);
  box-shadow: 0 18px 48px var(--color-shadow);
}

.import-dialog::backdrop {
  background: color-mix(in srgb, var(--color-bg-base), transparent 18%);
}

.import-dialog form {
  display: grid;
  gap: var(--space-3);
  padding: var(--space-4);
}

.import-dialog h2 {
  margin: 0;
  font-size: var(--text-lg);
}

.sampling-section[hidden] {
  display: none;
}

.sampling-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: var(--space-2);
}

.sampling-grid .field-label {
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-weight: 600;
}

.sampling-grid .inline-input {
  grid-template-columns: 1fr 2ch;
  gap: var(--space-1);
}

.sampling-grid .input-unit {
  text-align: center;
}

.import-progress {
  min-height: 18px;
  margin: 0;
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  text-align: center;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
  margin: var(--space-2) 0 0;
  padding: var(--space-3) 0 0;
  border-top: 1px solid var(--color-border-subtle);
}

.dialog-actions button {
  min-height: 34px;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--color-border-subtle);
  border-radius: var(--radius-md);
  background: var(--color-bg-surface);
  color: var(--color-text-primary);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.dialog-actions button[type="submit"] {
  border-color: var(--color-primary);
  background: var(--color-primary);
  color: #fff;
}

.dialog-actions button:disabled {
  cursor: not-allowed;
  opacity: .58;
}

.workspace {
  min-width: 0;
  height: 100vh;
  overflow: hidden;
}

.grid-wrap {
  container-type: size;
  width: 100%;
  height: 100%;
  overflow: auto;
  display: block;
}

.image-grid {
  --padding: var(--space-4);
  --grid-gap: var(--space-3);
  --min-slot-size: 96px;
  --row-header-size: 96px;
  --column-header-size: 34px;

  --cols: 1;
  --rows: 1;
  --header-cols: 0;
  --header-rows: 0;

  --slot-size-x: calc((100cqw - (var(--header-cols) * var(--row-header-size)) - ((var(--cols) + var(--header-cols) - 1) * var(--grid-gap)) - var(--padding) * 2) / var(--cols));
  --slot-size-y: calc((100cqh - (var(--header-rows) * var(--column-header-size)) - ((var(--rows) + var(--header-rows) - 1) * var(--grid-gap)) - var(--padding) * 2) / var(--rows));
  --slot-size: max(var(--min-slot-size), min(var(--slot-size-x), var(--slot-size-y)));

  display: grid;
  grid-template-columns: repeat(var(--cols), var(--slot-size));
  grid-template-rows: repeat(var(--rows), var(--slot-size));
  gap: var(--grid-gap);
  align-items: stretch;
  place-content: center;

  padding: var(--padding);
  width: max-content;
  height: max-content;
  min-width: 100%;
  min-height: 100%;
}

.image-grid.has-row-header {
  --header-cols: 1;
  
  grid-template-columns:
    var(--row-header-size)
    repeat(var(--cols), var(--slot-size));
}

.image-grid.has-column-header {
  --header-rows: 1;
  
  grid-template-rows:
    var(--column-header-size)
    repeat(var(--rows), var(--slot-size));
}

.image-grid.placeholder-mode {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-width: 100%;
  height: 100%;
  gap: 0;
}

.grid-header,
.row-header {
  display: grid;
  place-items: center;
  padding: var(--space-2);
  border: 1px solid var(--color-border-subtle);
  border-radius: var(--radius-md);
  background: var(--color-bg-surface);
  color: var(--color-text-secondary);
  font-size: var(--text-xs);
  font-weight: 700;
  line-height: 1.25;
  text-align: center;
  white-space: pre-line;
}

.corner {
  border: 0;
  background: transparent;
}

.image-slot {
  container-type: inline-size;
  overflow: hidden;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid var(--color-border-subtle);
  border-radius: var(--radius-lg);
  background-color: var(--color-slot-bg);
  background-image: conic-gradient(var(--color-slot-checker) 25%, transparent 25% 50%, var(--color-slot-checker) 50% 75%, transparent 75%);
  background-position: 0 0, 0 10px, 10px -10px, -10px 0;
  background-size: 20px 20px;
  cursor: pointer;
}

.image-slot.drag-over {
  outline: 3px solid color-mix(in srgb, var(--color-primary) 45%, transparent);
}

.image-slot > * {
  grid-area: 1 / 1;
}

.image-slot img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  min-height: 0;
  min-width: 0;
}

.image-slot.is-own {
  border-color: var(--color-primary);
  box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--color-primary) 35%, transparent);
}

.image-slot.is-inherited {
  border-style: dashed;
  border-color: var(--color-info);
}

.image-slot.is-inherited img {
  opacity: .48;
  filter: saturate(.72);
}

.slot-label {
  place-self: end stretch;
  height: auto;
  min-height: 22px;
  padding: 3px var(--space-2);
  margin: 3px var(--space-2);
  overflow: hidden;
  border-radius: var(--radius-sm);

  background: color-mix(in srgb, var(--color-bg-surface) 88%, transparent);
  color: var(--color-text-primary);
  font-size: clamp(var(--text-xs), 3cqi, var(--text-base));
  font-weight: 700;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.image-slot.is-inherited .slot-label {
  z-index: 0; /* Because transparent imgs cause a new stacking context. This also causes one for the labels */
  color: var(--color-info);
}

.empty-copy {
  padding: var(--space-3);
  color: var(--color-text-muted);
  font-size: clamp(var(--text-xs), 6cqi, var(--text-xl));
  font-weight: 700;
  text-align: center;
}

.grid-placeholder {
  width: 100%;
  padding: var(--space-4);
  color: var(--color-text-muted);
  font-size: var(--text-lg);
  font-weight: 700;
  line-height: 1.35;
  text-align: center;
}

.sidebar-footer {
  margin-top: auto;
  padding-bottom: var(--space-2);
}

.footer-divider {
  border: 0;
  border-top: 1px solid var(--color-border-subtle);
  margin-bottom: var(--space-4);
}

.sidebar-footer p {
  margin: 0;
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

.github-link {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  margin-top: var(--space-2);
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--color-primary);
  text-decoration: none;
  transition: color 0.2s ease;
}

.github-link:hover {
  color: var(--color-primary-dark);
  text-decoration: underline;
}

.github-link svg {
  flex-shrink: 0;
}

@media (max-width: 760px) {
  body {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr);
  }

  .sidebar {
    height: auto;
    max-height: 48vh;
    border-right: 0;
    border-bottom: 1px solid var(--color-border-subtle);
  }

  .workspace {
    height: 52vh;
    padding: var(--space-3);
  }
}