:root {
  --bg: #ffffff;
  --bg-soft: #f7f6f3;
  --ink: #37352f;
  --muted: #787774;
  --line: #e9e9e7;
  --line-strong: #d3d1cb;
  --hover: rgba(55, 53, 47, 0.06);
  --active: rgba(55, 53, 47, 0.1);
  --ok: #0f7b6c;
  --ok-bg: #eef6f4;
  --bad: #c4554d;
  --bad-bg: #f9efee;
  --busy: #9a6700;
  --busy-bg: #fbf3db;
  --accent: #8b84ef;
  --accent-soft: #f4f3ff;
  --accent-line: #b7b1f5;
  --accent-btn: #a59ef3;
  --accent-btn-hover: #948ceb;
  --radius: 6px;
  --font: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, "Apple Color Emoji", Arial, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font: 14px/1.5 var(--font);
  color: var(--ink);
  background: var(--bg-soft);
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: inherit; text-decoration: none; }
img { max-width: 100%; display: block; }
[hidden] { display: none !important; }
#app { min-height: 100vh; }

.page {
  width: min(1120px, calc(100% - 48px));
  margin: 0 auto;
  padding: 20px 0 56px;
}
.page-home .hero,
.page-home .history {
  width: min(760px, 100%);
  margin-inline: auto;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 28px;
}
.topbar-left {
  display: flex;
  align-items: center;
  gap: 20px;
  min-width: 0;
  flex: 1 1 auto;
}
.brand, .brand-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  border: 0;
  background: transparent;
  padding: 4px 0;
  cursor: pointer;
  font-size: 17px;
  font-weight: 800;
  letter-spacing: -0.03em;
  flex: 0 0 auto;
}
.brand-logo {
  width: 32px;
  height: 32px;
  border-radius: 7px;
  flex: 0 0 auto;
}
.top-nav {
  display: flex;
  align-items: center;
  gap: 4px;
  min-width: 0;
  flex-wrap: wrap;
}
.top-nav-link {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 14px;
  font-weight: 400;
  padding: 6px 0;
  margin: 0 10px;
  border-radius: 0;
  border-bottom: 1.5px solid transparent;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  display: inline-block;
  width: fit-content;
  line-height: 1.3;
}
.top-nav-link:hover {
  color: var(--ink);
  background: transparent;
}
.top-nav-link.is-active {
  color: var(--ink);
  font-weight: 500;
  background: transparent;
  border-bottom-color: var(--ink);
}
.topbar-actions {
  display: flex;
  gap: 6px;
  align-items: center;
  flex: 0 0 auto;
  min-height: 28px;
}
.composer-mode {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 36px;
}
.btn-icon-plain {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 0;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.btn-icon-plain:hover {
  color: var(--ink);
  background: var(--hover);
}
.topbar-settings {
  width: 32px;
  height: 32px;
}

.more-menu {
  position: relative;
  flex-shrink: 0;
}
.more-pop {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  z-index: 20;
  min-width: 128px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
  display: grid;
  gap: 2px;
}
.more-item {
  appearance: none;
  border: 0;
  background: transparent;
  text-align: left;
  padding: 8px 10px;
  border-radius: calc(var(--radius) - 2px);
  font: inherit;
  font-size: 13px;
  color: var(--ink);
  cursor: pointer;
}
.more-item:hover:not(:disabled) {
  background: var(--hover);
}
.more-item:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.replan-modal {
  width: min(92vw, 480px);
}
.replan-lead {
  margin: 0 0 14px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.5;
}
.replan-body {
  display: grid;
  gap: 14px;
  margin-bottom: 16px;
}
.replan-body .layer-picker {
  display: grid;
  gap: 8px;
}

.hero { margin-bottom: 36px; }
.lede {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.55;
}

.panel {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  padding: 16px;
}

.composer {
  display: grid;
  gap: 14px;
  padding: 20px;
  border-radius: 16px;
  border: 1px solid var(--line);
  box-shadow: 0 8px 28px rgba(55, 53, 47, 0.06);
}
.composer-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px 12px;
  padding: 10px 12px;
  background: var(--bg-soft);
  border-radius: 12px;
}
.layer-picker {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 10px;
  min-width: 0;
}
.layer-label {
  color: var(--muted);
  font-size: 12.5px;
  font-weight: 550;
  white-space: nowrap;
  flex: 0 0 auto;
}
.layer-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.layer-chip {
  min-width: 34px;
  height: 30px;
  padding: 0 7px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--ink);
  font-size: 12.5px;
  font-weight: 500;
  cursor: pointer;
}
.layer-chip:hover {
  border-color: var(--line-strong);
  background: var(--hover);
}
.layer-chip.active {
  border-color: var(--ink);
  color: var(--ink);
  background: var(--bg);
}
.layer-hint {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}
.btn-note {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 34px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--bg);
  color: var(--ink);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  flex: 0 0 auto;
}
.btn-note:hover {
  border-color: var(--line-strong);
  background: var(--hover);
}
.btn-note.is-on {
  border-color: var(--ink);
  color: var(--ink);
  background: var(--bg);
}
.composer-note { display: grid; }
.composer-actions {
  display: grid;
  justify-items: center;
  gap: 10px;
  padding-top: 2px;
}
.btn-start {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-width: 168px;
  height: 44px;
  padding: 0 28px;
  border: 0;
  border-radius: 11px;
  background: var(--ink);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 6px 16px rgba(55, 53, 47, 0.16);
}
.btn-start:hover:not(:disabled) { background: #2f2e2a; }
.btn-start:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  box-shadow: none;
}
.composer-foot {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  text-align: center;
}
.ico { display: block; flex: 0 0 auto; }

.dropzone {
  border: 1.5px dashed var(--line-strong);
  border-radius: 14px;
  background: var(--bg-soft);
  min-height: 180px;
  transition: background .12s, border-color .12s, box-shadow .12s;
}
.dropzone:hover,
.dropzone.dragover,
.dropzone:focus-visible {
  border-color: var(--ink);
  background: var(--hover);
  outline: none;
  box-shadow: inset 0 0 0 1px rgba(55, 53, 47, 0.06);
}
.dropzone-inner {
  min-height: 180px;
  display: grid;
  place-content: center;
  justify-items: center;
  text-align: center;
  gap: 4px;
  padding: 28px 16px 24px;
}
.dropzone-title {
  margin: 0 0 4px;
  font-weight: 700;
  font-size: 16px;
  letter-spacing: -0.02em;
  color: var(--ink);
}
.dropzone-hint {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}
.btn-pick {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  margin-top: 14px;
  height: 36px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--bg);
  color: var(--ink);
  font-size: 13px;
  font-weight: 560;
  cursor: pointer;
  box-shadow: 0 1px 2px rgba(55, 53, 47, 0.04);
}
.btn-pick:hover {
  border-color: var(--line-strong);
  background: var(--hover);
  color: var(--ink);
}
.dropzone-preview {
  padding: 18px;
  display: grid;
  gap: 12px;
  justify-items: center;
}
.dropzone-preview img {
  max-height: 160px;
  border-radius: 8px;
  object-fit: contain;
}
.dropzone-preview-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}
.dropzone-preview-actions .btn-pick { margin-top: 0; }

.field { display: grid; gap: 6px; }
.field-inline {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}
.field-inline > span { margin: 0; }
.field > span,
.field .field-head {
  font-size: 12px;
  font-weight: 550;
  color: var(--muted);
}
.field .field-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.field .field-head .btn.sm {
  font-weight: 500;
}
.field > span em {
  font-style: normal;
  font-weight: 400;
  opacity: 0.85;
}
.field [data-model-value]:not([hidden]) {
  margin-top: 0;
}
.field input,
.field textarea,
.field select {
  width: 100%;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0 10px;
  font-size: 13px;
  line-height: 1.4;
  background: var(--bg);
  color: var(--ink);
}
.field textarea {
  height: auto;
  min-height: 64px;
  padding: 8px 10px;
  resize: vertical;
}
.field select {
  appearance: none;
  -webkit-appearance: none;
  padding-right: 28px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23787774' d='M2.2 4.2L6 8l3.8-3.8-.9-.9L6 6.2 3.1 3.3z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 12px;
  cursor: pointer;
}
.field input:focus,
.field textarea:focus,
.field select:focus {
  outline: none;
  border-color: var(--line-strong);
}
.field-row { display: flex; gap: 8px; flex-wrap: wrap; }
.field-row-3 {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  flex-wrap: nowrap;
}
.field-row-3 > .field { min-width: 0; }

.seg {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 2px;
  padding: 2px;
  background: var(--bg-soft);
  border-radius: var(--radius);
  border: 1px solid var(--line);
}
.seg-btn {
  border: 0;
  background: transparent;
  color: var(--muted);
  border-radius: 4px;
  padding: 5px 10px;
  cursor: pointer;
  font-weight: 500;
  font-size: 13px;
}
.seg-btn:hover { background: var(--hover); color: var(--ink); }
.seg-btn.active {
  background: var(--bg);
  color: var(--ink);
  box-shadow: 0 0 0 1px var(--line);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  padding: 6px 12px;
  font-weight: 500;
  font-size: 13px;
  cursor: pointer;
  background: transparent;
  transition: background .1s;
}
.btn:hover { background: var(--hover); }
.btn:disabled, .btn.disabled {
  opacity: 0.4;
  pointer-events: none;
}
.btn.primary {
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
}
.btn.primary:hover { background: #2f2e2a; }
.btn.ghost {
  border-color: var(--line);
  background: var(--bg);
}
.btn.ghost:hover { background: var(--bg-soft); }
.btn.sm { padding: 4px 8px; font-size: 12px; }
.file-btn { position: relative; overflow: hidden; cursor: pointer; }

.section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin: 0 0 10px;
}
.section-head h2 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
}
.section-head p {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

.empty {
  padding: 20px;
  text-align: center;
  color: var(--muted);
  border: 1px dashed var(--line);
  border-radius: var(--radius);
}

.history-list {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.history-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  border-bottom: 1px solid var(--line);
}
.history-item:last-child { border-bottom: 0; }
.history-item:hover { background: var(--hover); }
.history-open {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  width: 100%;
  padding: 10px 12px;
  border: 0;
  background: transparent;
  text-align: left;
  cursor: pointer;
}
.history-thumb {
  width: 44px;
  height: 44px;
  border-radius: 4px;
  overflow: hidden;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  display: grid;
  place-items: center;
}
.history-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.thumb-fallback {
  color: var(--muted);
  font-size: 10px;
  font-weight: 600;
}
.history-meta {
  min-width: 0;
  display: grid;
  gap: 2px;
}
.history-meta strong {
  font-size: 13px;
  font-weight: 550;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.history-meta time {
  color: var(--muted);
  font-size: 12px;
}
.history-del {
  border: 0;
  background: transparent;
  color: var(--muted);
  width: 28px;
  height: 28px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
}
.history-del:hover { background: var(--hover); color: var(--bad); }

.pill {
  display: inline-flex;
  align-items: center;
  padding: 1px 6px;
  border-radius: 3px;
  font-size: 11px;
  font-weight: 500;
  background: var(--bg-soft);
  color: var(--muted);
  white-space: nowrap;
}
.pill.ok { background: var(--ok-bg); color: var(--ok); }
.pill.bad { background: var(--bad-bg); color: var(--bad); }
.pill.busy { background: var(--busy-bg); color: var(--busy); }

.detail-crumb {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 0 0 14px;
  min-width: 0;
}
.crumb-back {
  flex: 0 0 auto;
}
.crumb-trail {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0;
  padding: 0;
  list-style: none;
  min-width: 0;
  font-size: 13px;
  color: var(--muted);
}
.crumb-link {
  appearance: none;
  border: 0;
  background: transparent;
  padding: 0;
  font: inherit;
  color: var(--muted);
  cursor: pointer;
}
.crumb-link:hover { color: var(--ink); }
.crumb-sep { color: var(--line-strong); }
.crumb-current {
  color: var(--ink);
  font-weight: 560;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}
.detail-body {
  display: grid;
  grid-template-columns: minmax(240px, 280px) minmax(0, 1fr);
  grid-template-rows: auto auto;
  gap: 16px;
  align-items: start;
}
.detail-body > .layers-side {
  grid-column: 1;
  grid-row: 1 / span 2;
  position: sticky;
  top: 16px;
}
.detail-body > .main {
  grid-column: 2;
  grid-row: 1;
  min-width: 0;
}
.detail-body > .project-side {
  grid-column: 2;
  grid-row: 2;
}
.side {
  display: grid;
  gap: 12px;
  padding: 14px;
}
.project-side .meta {
  grid-template-columns: 1fr 1fr;
}
.project-side .actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.project-side .actions .btn {
  flex: 1 1 auto;
  min-width: 120px;
}
.side h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  line-height: 1.35;
  word-break: break-word;
}
.side-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.side-head-main {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex: 1 1 auto;
}
.side-head-main h1 {
  margin: 0;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.status-msg {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}
.progress {
  height: 4px;
  border-radius: 99px;
  background: var(--bg-soft);
  overflow: hidden;
}
.progress > i {
  display: block;
  height: 100%;
  background: var(--ink);
}
.meta {
  display: grid;
  gap: 8px;
  margin: 0;
}
.meta div {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-size: 13px;
}
.meta dt { color: var(--muted); }
.meta dd { margin: 0; font-weight: 500; }
.actions { display: grid; gap: 6px; }
.note {
  margin: 0;
  font-size: 12px;
  color: var(--busy);
  background: var(--busy-bg);
  padding: 8px 10px;
  border-radius: var(--radius);
}
.error, .warn {
  margin: 0;
  padding: 8px 10px;
  border-radius: var(--radius);
  font-size: 12px;
}
.error { background: var(--bad-bg); color: var(--bad); }
.warn { background: var(--busy-bg); color: var(--busy); }

.jobs summary {
  cursor: pointer;
  font-weight: 550;
  font-size: 13px;
  color: var(--ink);
}
.job-log {
  list-style: none;
  margin: 6px 0 0;
  padding: 0;
}
.job-log li {
  display: grid;
  grid-template-columns: 4.75em minmax(0, 1fr);
  gap: 12px;
  align-items: baseline;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
  font-size: 13px;
  line-height: 1.45;
  background: transparent;
  border-radius: 0;
}
.job-log li:last-child {
  border-bottom: 0;
}
.job-log .t {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
  white-space: nowrap;
}
.job-log .m {
  color: var(--ink);
  min-width: 0;
  word-break: break-word;
}
.job-log .m.ok {
  color: #1a7f37;
}
.job-log .m.bad {
  color: var(--bad);
}
.job-log-empty .m {
  color: var(--muted);
}

.main { display: grid; gap: 12px; min-width: 0; }
.compare { overflow: hidden; padding: 0; }
.compare-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}
.compare-bar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
  flex: 0 0 auto;
}
.compare-stage {
  display: grid;
  min-height: 320px;
  background: var(--bg-soft);
}
.compare-stage.mode-split { grid-template-columns: 1fr 1fr; }
.compare-stage.mode-source .preview-pane,
.compare-stage.mode-preview .source-pane { display: none; }
.compare-pane {
  position: relative;
  display: grid;
  place-items: center;
  padding: 16px;
  min-height: 320px;
}
.compare-stage.mode-split .source-pane { border-right: 1px solid var(--line); }
.pane-label {
  position: absolute;
  top: 10px;
  left: 10px;
  z-index: 30;
  padding: 2px 6px;
  border-radius: 3px;
  background: rgba(255,255,255,0.92);
  border: 1px solid var(--line);
  font-size: 11px;
  color: var(--muted);
}
.compare-pane img {
  max-height: 480px;
  width: auto;
  object-fit: contain;
  border-radius: 4px;
  border: 1px solid var(--line);
  background: #fff;
}
.stack-preview {
  position: relative;
  /* Definite width so .stack-sizer percentages don't collapse under fit-content. */
  width: min(100%, calc(480px * var(--canvas-w, 1) / var(--canvas-h, 1)));
  max-width: 100%;
  margin: 0 auto;
  border-radius: 4px;
  border: 1px solid var(--line);
  overflow: hidden;
  line-height: 0;
  --canvas-w: 1;
  --canvas-h: 1;
  --stack-scale: 1;
}
.stack-preview.is-interactive {
  cursor: default;
  touch-action: none;
}
.stack-preview.is-generating::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 5;
  pointer-events: none;
  transform: translateX(-100%);
  background: linear-gradient(
    90deg,
    transparent,
    rgba(255, 255, 255, 0.55),
    transparent
  );
  animation: thumb-shimmer 1.2s ease-in-out infinite;
}
/* Canvas-sized layout box so cropped layers can sit at absolute offsets. */
.stack-sizer {
  display: block;
  width: 100%;
  max-height: 480px;
  aspect-ratio: var(--canvas-w, 1) / var(--canvas-h, 1);
  background: transparent;
  pointer-events: none;
}
.stack-layer {
  display: block;
  border: 0 !important;
  border-radius: 0 !important;
  background: transparent !important;
  pointer-events: none;
  user-select: none;
  position: absolute;
  left: 0;
  top: 0;
  width: auto !important;
  height: auto !important;
  max-width: none !important;
  max-height: none !important;
  object-fit: fill;
  transform-origin: top left;
  transform: translate(
    calc(var(--ox, 0px) * var(--stack-scale, 1)),
    calc(var(--oy, 0px) * var(--stack-scale, 1))
  ) scale(calc(var(--stack-scale, 1) * var(--layer-scale, 1)));
  will-change: transform;
}
.layer-selection {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--sel-w, 0px);
  height: var(--sel-h, 0px);
  transform: translate(var(--sel-x, 0px), var(--sel-y, 0px));
  box-sizing: border-box;
  border: 1px solid #4c8bf5;
  pointer-events: none;
  z-index: 20;
}
.layer-selection[hidden] { display: none !important; }
.layer-selection [data-handle] {
  position: absolute;
  width: 8px;
  height: 8px;
  margin: -4px 0 0 -4px;
  border: 1px solid #4c8bf5;
  background: #fff;
  box-sizing: border-box;
  pointer-events: auto;
  touch-action: none;
}
.layer-selection [data-handle="nw"] { left: 0; top: 0; cursor: nwse-resize; }
.layer-selection [data-handle="ne"] { left: 100%; top: 0; cursor: nesw-resize; }
.layer-selection [data-handle="sw"] { left: 0; top: 100%; cursor: nesw-resize; }
.layer-selection [data-handle="se"] { left: 100%; top: 100%; cursor: nwse-resize; }
.pane-empty { color: var(--muted); font-size: 13px; }
.preview-pane [data-live="stack-host"] {
  width: 100%;
  max-width: 100%;
  display: grid;
  place-items: center;
}
.preview-loading {
  position: relative;
  overflow: hidden;
  box-sizing: border-box;
  max-width: 100%;
  margin: 0 auto;
  border-radius: 4px;
  border: 1px solid var(--line);
  background: #ebebea;
  display: grid;
  place-items: center;
}
.preview-loading::after {
  content: "";
  position: absolute;
  inset: 0;
  transform: translateX(-100%);
  background: linear-gradient(
    90deg,
    transparent,
    rgba(255, 255, 255, 0.65),
    transparent
  );
  animation: thumb-shimmer 1.2s ease-in-out infinite;
}
.preview-loading-label {
  position: relative;
  z-index: 1;
  max-width: 88%;
  padding: 8px 12px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.4;
  text-align: center;
  word-break: break-word;
}

.layers { padding: 12px; }
.layers-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
}
.layers-head h2 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
}
.layers-head span { color: var(--muted); font-size: 12px; }
.layers-head-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
  flex-wrap: wrap;
  justify-content: flex-end;
}
.layers-head-actions .btn.sm {
  white-space: nowrap;
}
.muted { color: var(--muted); }
.pad { padding: 8px; margin: 0; }

.layer-list {
  display: grid;
  gap: 2px;
}
.layer-row.is-skeleton {
  pointer-events: none;
}
.skel-line {
  display: block;
  height: 10px;
  border-radius: 4px;
  background: #ebebea;
  position: relative;
  overflow: hidden;
}
.skel-line::after {
  content: "";
  position: absolute;
  inset: 0;
  transform: translateX(-100%);
  background: linear-gradient(
    90deg,
    transparent,
    rgba(255, 255, 255, 0.65),
    transparent
  );
  animation: thumb-shimmer 1.2s ease-in-out infinite;
}
.skel-title { width: 72%; height: 12px; }
.skel-sub { width: 48%; margin-top: 6px; }
.layer-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 6px;
  align-items: center;
  border-radius: var(--radius);
  padding: 2px;
  position: relative;
}
.layer-row:hover { background: var(--hover); }
.layer-row.active { background: var(--active); }
.layer-row.is-hidden { opacity: 0.48; }
.layer-row.is-hidden .layer-thumb { filter: grayscale(0.35); }
.layer-row.is-draggable { cursor: grab; }
.layer-row.is-dragging {
  opacity: 0.45;
  cursor: grabbing;
}
.layer-row.drop-before::before,
.layer-row.drop-after::after {
  content: "";
  position: absolute;
  left: 8px;
  right: 8px;
  height: 2px;
  border-radius: 2px;
  background: var(--ink);
  pointer-events: none;
  z-index: 2;
}
.layer-row.drop-before::before { top: 0; }
.layer-row.drop-after::after { bottom: 0; }
.layer-main {
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  width: 100%;
  border: 0;
  background: transparent;
  border-radius: var(--radius);
  padding: 6px 8px;
  text-align: left;
  cursor: inherit;
  color: inherit;
  font: inherit;
}
.layer-tools {
  display: flex;
  align-items: center;
  gap: 2px;
  padding-right: 4px;
  cursor: default;
}
.layer-tools .btn.sm {
  min-width: 28px;
  padding-left: 6px;
  padding-right: 6px;
  cursor: pointer;
}
.layer-vis[aria-pressed="true"] {
  color: var(--muted);
}
.layer-tools .layer-dl {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
}
.layer-thumb {
  position: relative;
  width: 36px;
  height: 36px;
  border-radius: 4px;
  overflow: hidden;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  flex-shrink: 0;
}
.layer-order {
  position: absolute;
  z-index: 1;
  min-width: 14px;
  padding: 0 2px;
  border-radius: 2px;
  background: rgba(20, 20, 20, 0.5);
  color: #fff;
  font-size: 9px;
  font-weight: 650;
  line-height: 14px;
  text-align: center;
  pointer-events: none;
}
.layer-thumb img { width: 100%; height: 100%; object-fit: contain; }
.layer-thumb-fallback { width: 100%; height: 100%; background: var(--bg-soft); }
.layer-thumb-fallback.is-loading {
  position: relative;
  overflow: hidden;
  background: #ebebea;
}
.layer-thumb-fallback.is-loading::after {
  content: "";
  position: absolute;
  inset: 0;
  transform: translateX(-100%);
  background: linear-gradient(
    90deg,
    transparent,
    rgba(255, 255, 255, 0.65),
    transparent
  );
  animation: thumb-shimmer 1.2s ease-in-out infinite;
}
@keyframes thumb-shimmer {
  100% { transform: translateX(100%); }
}
.layer-row-meta { min-width: 0; display: grid; gap: 1px; }
.layer-row-meta strong {
  font-size: 13px;
  font-weight: 550;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.layer-row-meta em {
  font-style: normal;
  color: var(--muted);
  font-size: 11px;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(15, 15, 15, 0.35);
}
.modal {
  width: min(520px, 100%);
  max-height: min(86vh, 720px);
  overflow: auto;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.18);
  padding: 16px;
}
.settings-modal { width: min(560px, 100%); }
.settings-body { display: grid; gap: 12px; }
.settings-divider {
  margin: 4px 0;
  border: 0;
  border-top: 1px solid var(--line);
  height: 0;
}
.settings-section-title {
  margin: 0;
  font-size: 13px;
  font-weight: 650;
  color: var(--ink);
}
.settings-modal .editor-actions { margin-top: 14px; justify-content: flex-end; }
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}
.modal-head h2 {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
}
.modal-preview {
  margin: 0 0 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background:
    linear-gradient(45deg, #eee 25%, transparent 25%),
    linear-gradient(-45deg, #eee 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #eee 75%),
    linear-gradient(-45deg, transparent 75%, #eee 75%);
  background-size: 14px 14px;
  background-position: 0 0, 0 7px, 7px -7px, -7px 0;
  background-color: #fff;
  display: grid;
  place-items: center;
  min-height: 140px;
}
.modal-preview img {
  max-width: 100%;
  max-height: 220px;
  width: auto;
  height: auto;
  object-fit: contain;
  border: 0;
  border-radius: 0;
  background: transparent;
}
.modal-preview-empty {
  padding: 28px 12px;
  color: var(--muted);
  font-size: 13px;
}
.editor-grid { display: grid; gap: 10px; }
.editor-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.loading, .error-state {
  grid-column: 1 / -1;
  padding: 48px 20px;
  text-align: center;
  color: var(--muted);
}
.error-state {
  display: grid;
  gap: 10px;
  justify-items: center;
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.error-state h2 { margin: 0; color: var(--ink); font-size: 16px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  z-index: 50;
  max-width: min(92vw, 380px);
  padding: 10px 14px;
  border-radius: var(--radius);
  background: var(--ink);
  color: #fff;
  font-size: 13px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.18);
}

@media (max-width: 900px) {
  .detail-body {
    grid-template-columns: 1fr;
    grid-template-rows: none;
  }
  .detail-body > .main,
  .detail-body > .layers-side,
  .detail-body > .project-side {
    grid-column: 1;
    grid-row: auto;
  }
  .detail-body > .layers-side {
    position: static;
    order: 2;
  }
  .detail-body > .main { order: 1; }
  .detail-body > .project-side { order: 3; }
  .compare-stage.mode-split { grid-template-columns: 1fr; }
  .compare-stage.mode-split .source-pane { border-right: 0; border-bottom: 1px solid var(--line); }
}

@media (max-width: 640px) {
  body {
    padding-left: env(safe-area-inset-left, 0);
    padding-right: env(safe-area-inset-right, 0);
  }
  .page,
  .page-detail {
    width: calc(100% - 20px);
    padding: max(12px, env(safe-area-inset-top, 0px)) 0 max(40px, env(safe-area-inset-bottom, 0px));
  }
  .topbar {
    flex-wrap: wrap;
    gap: 10px;
    margin-bottom: 18px;
    align-items: flex-start;
  }
  .topbar-left {
    flex: 1 1 auto;
    min-width: 0;
    gap: 10px;
  }
  /* Keep brand + primary nav; hide secondary links on narrow screens. */
  .top-nav-link[href="https://www.genpsd.com"],
  .top-nav-link[data-action="nav-soon"] {
    display: none;
  }
  .top-nav-link {
    margin: 0 6px 0 0;
    padding: 8px 0;
    font-size: 13px;
  }
  .topbar-actions {
    flex: 1 1 auto;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: 4px;
  }
  .credits-chip {
    padding: 6px 8px;
    min-height: 36px;
  }
  .account-trigger {
    width: 36px;
    height: 36px;
  }
  .composer-mode .seg {
    width: 100%;
  }
  .composer-mode .seg-btn {
    flex: 1 1 0;
    min-height: 40px;
    padding: 8px 10px;
  }
  .hero { margin-bottom: 24px; }
  .composer {
    padding: 14px;
    gap: 12px;
    border-radius: 14px;
  }
  .dropzone-inner {
    min-height: 148px;
    padding: 20px 12px 18px;
  }
  .dropzone-title { font-size: 15px; }
  .dropzone-hint { font-size: 12px; }
  .composer-toolbar {
    flex-direction: column;
    align-items: stretch;
    padding: 10px;
  }
  .layer-chips { gap: 8px; }
  .layer-chip {
    min-width: 40px;
    height: 36px;
    padding: 0 10px;
  }
  .btn-note {
    justify-content: center;
    min-height: 40px;
    width: 100%;
  }
  .layer-hint { width: 100%; white-space: normal; }
  .composer-actions .btn.primary {
    width: 100%;
    min-height: 44px;
    font-size: 14px;
  }
  .history-open {
    grid-template-columns: 44px minmax(0, 1fr);
    gap: 10px;
    padding: 10px;
    min-height: 56px;
  }
  .history-open .pill { display: none; }
  .history-del {
    width: 36px;
    height: 36px;
  }
  .detail-crumb {
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 12px;
  }
  .crumb-back {
    min-height: 36px;
  }
  .compare {
    border-radius: 12px;
  }
  .compare-bar {
    flex-wrap: wrap;
    gap: 8px;
    padding: 10px;
  }
  .compare-bar .seg {
    width: 100%;
  }
  .compare-bar .seg-btn {
    flex: 1 1 0;
    min-height: 40px;
    text-align: center;
  }
  .compare-pane {
    padding: 12px;
    min-height: 0;
  }
  .compare-pane img,
  .stack-preview {
    max-height: min(52vh, 360px);
  }
  .stack-sizer {
    max-height: min(52vh, 360px);
  }
  .stack-preview {
    width: min(100%, calc(min(52vh, 360px) * var(--canvas-w, 1) / var(--canvas-h, 1)));
  }
  .layers,
  .project-side {
    padding: 12px;
    border-radius: 12px;
  }
  .layers-head {
    flex-wrap: wrap;
    gap: 8px;
  }
  .layers-head-actions {
    width: 100%;
    justify-content: flex-start;
  }
  .layers-head-actions .btn {
    min-height: 36px;
  }
  .layer-row {
    padding: 6px 4px;
  }
  .layer-main {
    gap: 10px;
    min-height: 44px;
  }
  .project-side .actions {
    display: grid;
    grid-template-columns: 1fr;
  }
  .project-side .actions .btn {
    min-width: 0;
    width: 100%;
    min-height: 44px;
  }
  .project-side .meta {
    grid-template-columns: 1fr;
    gap: 8px;
  }
  .seg-btn {
    min-height: 36px;
  }
  .btn {
    min-height: 36px;
  }
  .btn.sm {
    min-height: 32px;
  }
  .modal {
    width: min(100vw - 24px, 520px);
    max-height: min(88vh, 720px);
    overflow: auto;
  }
}

/* Thin auto-hiding scrollbars (visible while scrolling / hover). */
* {
  scrollbar-width: thin;
  scrollbar-color: transparent transparent;
}
*:hover,
*:focus-within,
*.is-scrolling {
  scrollbar-color: rgba(55, 53, 47, 0.28) transparent;
}
*::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
*::-webkit-scrollbar-track {
  background: transparent;
}
*::-webkit-scrollbar-thumb {
  background: transparent;
  border-radius: 999px;
}
*:hover::-webkit-scrollbar-thumb,
*:focus-within::-webkit-scrollbar-thumb,
*.is-scrolling::-webkit-scrollbar-thumb {
  background: rgba(55, 53, 47, 0.28);
}
*::-webkit-scrollbar-corner {
  background: transparent;
}

/* Auth / account */
.topbar-auth {
  padding: 6px 12px;
  font-size: 13px;
}
.credits-chip {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--ink);
  border-radius: var(--radius);
  padding: 4px 6px;
  font-size: 12.5px;
  font-weight: 560;
  cursor: pointer;
  font-feature-settings: "tnum";
}
.credits-chip:hover {
  color: var(--muted);
  background: transparent;
}
.account-menu { position: relative; }
.account-trigger {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 11px;
  font-weight: 650;
  letter-spacing: 0.02em;
  line-height: 1;
  cursor: pointer;
  flex: 0 0 auto;
}
.account-trigger:hover { background: var(--hover); }
.account-pop {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  z-index: 30;
  min-width: 148px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
  display: grid;
  gap: 2px;
}
.auth-modal { width: min(400px, 100%); }
.auth-body { display: grid; gap: 12px; }
.auth-submit {
  width: 100%;
  justify-content: center;
  margin-top: 4px;
}
.auth-switch {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
}
.linkish {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--accent);
  font: inherit;
  padding: 0;
  cursor: pointer;
  text-decoration: underline;
}
.page-account .account-panel {
  width: min(960px, 100%);
  margin-inline: auto;
  padding: 18px;
}
.credits-balance {
  font-feature-settings: "tnum";
  font-size: 14px;
}
.ledger-list { display: grid; gap: 0; }
.ledger-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 12px;
  align-items: center;
  padding: 12px 0;
  border-top: 1px solid var(--line);
}
.ledger-row:first-child { border-top: 0; }
.ledger-row strong { display: block; font-size: 13.5px; }
.ledger-row time { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }
.ledger-delta { font-weight: 650; font-feature-settings: "tnum"; }
.ledger-delta.is-plus { color: var(--ok); }
.ledger-delta.is-minus { color: var(--bad); }
.ledger-bal { color: var(--muted); font-size: 12.5px; font-feature-settings: "tnum"; min-width: 3.5rem; text-align: right; }
.admin-tabs { display: flex; gap: 4px; }
.admin-table-wrap { overflow: auto; }
.admin-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.admin-table th,
.admin-table td {
  text-align: left;
  padding: 10px 8px;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
}
.admin-table th {
  color: var(--muted);
  font-weight: 560;
  font-size: 12px;
}
.admin-project-row { cursor: pointer; }
.admin-project-row:hover td { background: var(--hover); }
.admin-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}
.btn.ghost.sm.danger {
  color: var(--bad);
  border-color: #e7c9c6;
}
.btn.ghost.sm.danger:hover {
  background: var(--bad-bg);
}
