:root {
  color-scheme: light;
  --page: #f1f1ed;
  --surface: #f8f8f4;
  --line: #c3c3bd;
  --line-strong: #74746f;
  --text: #121211;
  --muted: #666661;
  --accent: #9a3f30;
  --accent-hover: #743025;
  --accent-soft: #e8d9d4;
  --danger: #a1322a;
  --danger-soft: #ead9d5;
  --success: #5a6250;
  --material-red: #ff466d;
  --material-orange: #ffb84d;
  --material-yellow: #e6ff5a;
  --material-green: #42e8c4;
  --material-blue: #4d8dff;
  --material-purple: #c65cff;
  font-family: Arial, "Noto Sans SC", "Microsoft YaHei", sans-serif;
}

@property --material-border-angle {
  syntax: "<angle>";
  inherits: false;
  initial-value: 0deg;
}

@property --generation-style-sweep {
  syntax: "<percentage>";
  inherits: false;
  initial-value: -30%;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html {
  min-width: 320px;
  background: var(--page);
}

body {
  min-height: 100dvh;
  margin: 0;
  background-color: var(--page);
  background-image: url("/upload-assets/paper-grain.webp?v=1");
  background-size: 256px 256px;
  background-blend-mode: soft-light;
  color: var(--text);
}

button,
input {
  font: inherit;
  letter-spacing: 0;
}

button,
label[for="image"] {
  -webkit-tap-highlight-color: transparent;
}

.shell {
  width: min(100%, 480px);
  margin: 0 auto;
  padding: 0 18px max(30px, env(safe-area-inset-bottom));
  background: rgba(248, 248, 244, .38);
  forced-color-adjust: none;
}

.brand {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: calc(54px + env(safe-area-inset-top));
  margin: 0 -18px 0;
  padding: env(safe-area-inset-top) 18px 0;
  border-bottom: 1px solid var(--text);
  background: var(--page);
  text-transform: uppercase;
}

.brand-name {
  font-size: 13px;
  font-weight: 900;
  line-height: 1;
}

.brand-edition {
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  line-height: 1;
}

.upload-panel {
  padding: 31px 0 0;
}

.connection-panel {
  padding: clamp(72px, 16vh, 140px) 18px 0;
  text-align: center;
}

.connection-panel h1 {
  margin: 20px 0 8px;
  font-size: clamp(28px, 7vw, 36px);
}

.connection-panel p {
  color: var(--muted);
}

.connection-mark {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid var(--text);
  border-radius: 50%;
  animation: connection-pulse 1.6s ease-in-out infinite;
}

@keyframes connection-pulse {
  0%, 100% { opacity: .28; transform: scale(.8); }
  50% { opacity: 1; transform: scale(1); }
}

.heading {
  padding: 0 0 27px;
  text-align: left;
}

h1,
h2,
p {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  margin: 0;
  font-family: "Arial Narrow", Arial, "Microsoft YaHei", sans-serif;
  font-size: 32px;
  font-weight: 500;
  line-height: 1.12;
}

h2 {
  font-size: 16px;
  line-height: 1.4;
}

.heading p {
  margin-top: 10px;
  color: var(--muted);
  font-size: 10px;
  line-height: 1.4;
  text-transform: uppercase;
}

form {
  display: grid;
  gap: 0;
  border-top: 1px solid var(--text);
}

.form-section {
  border-bottom: 1px solid var(--text);
}

.section-label {
  display: flex;
  min-height: 34px;
  align-items: center;
  justify-content: space-between;
  color: var(--text);
  font-size: 10px;
  text-transform: uppercase;
}

.section-label b {
  font-weight: 900;
}

.section-label small {
  color: var(--muted);
  font-size: 9px;
  font-weight: 500;
}

.field {
  display: grid;
  color: var(--text);
}

.field input {
  width: 100%;
  min-height: 56px;
  padding: 0 0 10px;
  border: 0;
  border-radius: 0;
  outline: none;
  background: transparent;
  color: var(--text);
  font-size: 21px;
  font-weight: 400;
}

.field input::placeholder {
  color: #918f88;
}

.field input:focus {
  box-shadow: inset 0 -3px 0 var(--accent);
}

.file-picker {
  position: relative;
  display: flex;
  min-height: 168px;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 13px;
  padding: 20px;
  cursor: pointer;
  text-align: center;
}

.file-picker:active {
  color: var(--accent);
}

.file-picker input {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.picker-icon {
  display: grid;
  width: 50px;
  height: 50px;
  flex: 0 0 auto;
  order: -1;
  place-items: center;
  border: 1px solid var(--text);
  background: transparent;
  color: var(--text);
  font-size: 31px;
  font-weight: 300;
  line-height: 1;
}

.picker-copy {
  display: grid;
  gap: 6px;
  text-align: center;
}

.picker-copy strong {
  font-size: 18px;
  font-weight: 600;
}

.picker-copy small {
  color: var(--muted);
  font-size: 9px;
}

.preview-wrap {
  position: relative;
  display: none;
  width: 100%;
  aspect-ratio: 4 / 3;
  overflow: hidden;
  border: 0;
  border-top: 1px solid var(--text);
  border-radius: 0;
  background: #d4d4ce;
}

.preview-wrap.visible {
  display: block;
}

.preview-wrap.visible + .submit-button {
  margin-top: 0;
}

.preview-wrap img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

#replace-image {
  position: absolute;
  right: 10px;
  bottom: 10px;
  min-height: 38px;
  padding: 0 13px;
  border: 1px solid var(--text);
  border-radius: 0;
  background: rgba(248, 248, 244, .94);
  color: var(--text);
  cursor: pointer;
  font-size: 12px;
  font-weight: 700;
}

.submit-button {
  display: flex;
  min-height: 58px;
  margin-top: 20px;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  border: 1px solid var(--text);
  border-radius: 0;
  background: var(--text);
  color: var(--surface);
  cursor: pointer;
  padding: 0 18px;
  font-size: 13px;
  font-weight: 800;
  text-transform: uppercase;
}

.submit-button:not(:disabled):active {
  border-color: var(--accent-hover);
  background: var(--accent-hover);
}

.submit-button:disabled {
  border-color: #a5a39d;
  background: #a5a39d;
  color: #e8e7e1;
  cursor: not-allowed;
}

.submit-button.loading .button-loading {
  display: block;
}

.button-loading {
  display: none;
  width: 15px;
  height: 15px;
  border: 2px solid rgba(248, 247, 242, .4);
  border-top-color: var(--surface);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}

.button-arrow {
  font-size: 23px;
  font-weight: 400;
  line-height: 1;
}

.submit-button.loading .button-arrow {
  display: none;
}

.status {
  min-height: 20px;
  padding-top: 10px;
  color: var(--danger);
  font-size: 12px;
  line-height: 1.5;
  text-align: center;
}

.status.success {
  color: var(--success);
}

.progress-view {
  min-height: calc(100dvh - 55px - env(safe-area-inset-top));
  padding: 28px 2px max(32px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.preview-grid {
  position: relative;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  width: min(100%, 360px);
  aspect-ratio: 444 / 576;
  margin-bottom: 24px;
  gap: 2px;
  padding: 2px;
  overflow: hidden;
  border: 1px solid var(--text);
  border-radius: 0;
  background: var(--text);
  transition: gap .2s ease, padding .2s ease, background-color .2s ease;
}

.preview-cell {
  position: relative;
  overflow: hidden;
  background: #d8d7d0;
}

.preview-cell::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 1;
  background: #98685e;
  opacity: 0;
  pointer-events: none;
  animation: cell-fade 1.56s linear infinite;
}

.progress-view.stage-creating_texture .preview-grid,
.progress-view.stage-texturing .preview-grid,
.progress-view.stage-copying_artifact .preview-grid {
  border-color: #575650;
  background: #575650;
}

.progress-view.stage-creating_texture .preview-cell,
.progress-view.stage-texturing .preview-cell,
.progress-view.stage-copying_artifact .preview-cell {
  background: #77756f;
}

.progress-view.stage-creating_texture .preview-cell::after,
.progress-view.stage-texturing .preview-cell::after,
.progress-view.stage-copying_artifact .preview-cell::after {
  background: #fff;
  box-shadow: 0 0 18px rgba(248, 247, 242, .7);
}

.preview-cell:nth-of-type(1)::after { animation-delay: -1.56s; }
.preview-cell:nth-of-type(2)::after { animation-delay: -1.43s; }
.preview-cell:nth-of-type(3)::after { animation-delay: -1.3s; }
.preview-cell:nth-of-type(4)::after { animation-delay: -1.17s; }
.preview-cell:nth-of-type(5)::after { animation-delay: -1.04s; }
.preview-cell:nth-of-type(6)::after { animation-delay: -.91s; }
.preview-cell:nth-of-type(7)::after { animation-delay: -.78s; }
.preview-cell:nth-of-type(8)::after { animation-delay: -.65s; }
.preview-cell:nth-of-type(9)::after { animation-delay: -.52s; }
.preview-cell:nth-of-type(10)::after { animation-delay: -.39s; }
.preview-cell:nth-of-type(11)::after { animation-delay: -.26s; }
.preview-cell:nth-of-type(12)::after { animation-delay: -.13s; }

.progress-view.succeeded .preview-cell,
.progress-view.failed .preview-cell,
.progress-view.cancelled .preview-cell {
  animation: none;
}

.progress-view.succeeded .preview-cell::after,
.progress-view.failed .preview-cell::after,
.progress-view.cancelled .preview-cell::after {
  display: none;
}

.progress-view.failed .preview-cell,
.progress-view.cancelled .preview-cell {
  background: var(--danger-soft);
}

.preview-cell img {
  position: absolute;
  width: 300%;
  height: 400%;
  max-width: none;
  object-fit: cover;
  opacity: 0;
  transition: opacity .24s ease;
}

.preview-cell.preview-ready img {
  opacity: 1;
}

.preview-cell.preview-ready {
  background: #000;
}

.complete-preview {
  position: absolute;
  inset: 0;
  z-index: 3;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000;
  opacity: 0;
  pointer-events: none;
  transition: opacity .28s ease;
}

.preview-grid.complete {
  gap: 0;
  padding: 0;
  border-color: #000;
  background: #000;
}

.preview-grid.complete .complete-preview {
  opacity: 1;
}

.model-viewer {
  width: min(100%, 420px);
  margin-bottom: 8px;
}

.viewer-stage {
  position: relative;
  width: 100%;
  aspect-ratio: 444 / 576;
  overflow: hidden;
  border: 1px solid var(--text);
  border-radius: 0;
  background: #deddd6;
}

.model-viewer.generation-preview .viewer-stage::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 2;
  background: url("/upload-assets/paper-grain.webp") center / 320px 320px repeat;
  mix-blend-mode: multiply;
  opacity: .18;
  pointer-events: none;
}

.model-viewer.generation-preview .viewer-loading {
  border-color: rgba(74, 70, 61, .58);
  background: rgba(248, 246, 236, .86);
  color: #565149;
}

.generation-style-stack,
.generation-style-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.generation-style-stack {
  z-index: 1;
  opacity: 0;
  transition: opacity .3s ease;
}

.generation-style-stack.ready {
  opacity: 1;
}

.generation-style-canvas {
  display: block;
  outline: none;
  pointer-events: none;
}

.generation-style-canvas[data-generation-style="sketch"] {
  z-index: 1;
}

.generation-style-canvas[data-generation-style="toon"],
.generation-style-canvas[data-generation-style="scan"] {
  --generation-style-sweep: -30%;
  z-index: 2;
  -webkit-mask-image: linear-gradient(
    to bottom,
    transparent calc(var(--generation-style-sweep) - 28%),
    #000 calc(var(--generation-style-sweep) - 12%),
    #000 calc(var(--generation-style-sweep) + 12%),
    transparent calc(var(--generation-style-sweep) + 28%)
  );
  mask-image: linear-gradient(
    to bottom,
    transparent calc(var(--generation-style-sweep) - 28%),
    #000 calc(var(--generation-style-sweep) - 12%),
    #000 calc(var(--generation-style-sweep) + 12%),
    transparent calc(var(--generation-style-sweep) + 28%)
  );
  animation: generation-style-sweep 6.4s linear infinite;
}

.generation-style-canvas[data-generation-style="scan"] {
  z-index: 3;
  animation-delay: -3.2s;
}

.progress-view.stage-creating_texture .viewer-stage::after,
.progress-view.stage-texturing .viewer-stage::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 4;
  padding: 3px;
  background: conic-gradient(
    from var(--material-border-angle),
    var(--material-red) 0deg,
    var(--material-orange) 60deg,
    var(--material-yellow) 120deg,
    var(--material-green) 180deg,
    var(--material-blue) 240deg,
    var(--material-purple) 300deg,
    var(--material-red) 360deg
  );
  filter: drop-shadow(0 0 5px rgba(113, 160, 255, .62));
  pointer-events: none;
  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  -webkit-mask-composite: xor;
  mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  mask-composite: exclude;
  animation: material-border-orbit 2.4s linear infinite;
}

#model-canvas,
.viewer-fallback {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
}

#model-canvas {
  z-index: 1;
  opacity: 0;
  outline: none;
  touch-action: none;
  transition: opacity .28s ease;
}

#model-canvas.ready {
  opacity: 1;
}

.viewer-fallback {
  z-index: 0;
  object-fit: contain;
  background: #000;
}

.viewer-loading {
  position: absolute;
  inset: auto 12px 12px;
  z-index: 3;
  padding: 9px 11px;
  border: 1px solid var(--text);
  border-radius: 0;
  background: rgba(248, 247, 242, .92);
  color: var(--muted);
  font-size: 11px;
}

.viewer-loading.error {
  color: var(--danger);
}

.viewer-controls {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: stretch;
  gap: 10px;
  margin-top: 12px;
}

.model-source-toggle {
  display: flex;
  min-height: 44px;
  align-items: center;
  gap: 9px;
  padding: 0 11px;
  border: 1px solid var(--text);
  color: var(--text);
  cursor: pointer;
  font-size: 11px;
  font-weight: 700;
}

.model-source-toggle input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.toggle-track {
  position: relative;
  width: 30px;
  height: 17px;
  border: 1px solid var(--text);
  background: transparent;
  transition: background-color .18s ease;
}

.toggle-track span {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 11px;
  height: 11px;
  background: var(--text);
  transition: transform .18s ease, background-color .18s ease;
}

.model-source-toggle input:checked + .toggle-track {
  background: var(--text);
}

.model-source-toggle input:checked + .toggle-track span {
  background: var(--surface);
  transform: translateX(13px);
}

.model-source-toggle:has(input:focus-visible) {
  outline: 2px solid var(--text);
  outline-offset: 2px;
}

.model-source-toggle:has(input:disabled) {
  cursor: default;
  opacity: .45;
}

.style-switcher {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0;
  padding: 0;
  border: 1px solid var(--text);
  border-radius: 0;
  background: rgba(248, 247, 242, .6);
}

.style-switcher button {
  min-width: 0;
  min-height: 42px;
  padding: 0 3px;
  border: 0;
  border-right: 1px solid var(--line);
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 11px;
  font-weight: 700;
}

@media (max-width: 390px) {
  .viewer-controls {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .model-source-toggle {
    justify-content: space-between;
  }
}

.style-switcher button:last-child {
  border-right: 0;
}

.style-switcher button.active {
  background: var(--text);
  color: var(--surface);
}

.style-switcher button:disabled {
  cursor: wait;
  opacity: .45;
}

.viewer-hint {
  margin-top: 10px;
  color: var(--muted);
  font-size: 10px;
}

.progress-view.viewer-active {
  justify-content: flex-start;
  padding-top: 8px;
}

.progress-view.viewer-active .progress-track {
  margin-top: 18px;
}

.progress-view.succeeded .progress-track {
  margin-top: 10px;
  padding: 10px 0;
}

.progress-view.succeeded .bar {
  margin-top: 8px;
}

.progress-view.succeeded .home-button {
  margin-top: 14px;
}

.preview-cell:nth-of-type(1) img,
.preview-cell:nth-of-type(4) img,
.preview-cell:nth-of-type(7) img,
.preview-cell:nth-of-type(10) img { left: 0; }
.preview-cell:nth-of-type(2) img,
.preview-cell:nth-of-type(5) img,
.preview-cell:nth-of-type(8) img,
.preview-cell:nth-of-type(11) img { left: -100%; }
.preview-cell:nth-of-type(3) img,
.preview-cell:nth-of-type(6) img,
.preview-cell:nth-of-type(9) img,
.preview-cell:nth-of-type(12) img { left: -200%; }
.preview-cell:nth-of-type(-n+3) img { top: 0; }
.preview-cell:nth-of-type(n+4):nth-of-type(-n+6) img { top: -100%; }
.preview-cell:nth-of-type(n+7):nth-of-type(-n+9) img { top: -200%; }
.preview-cell:nth-of-type(n+10) img { top: -300%; }

@keyframes cell-fade {
  0% { opacity: 1; }
  8% { opacity: .9; }
  16% { opacity: .78; }
  24% { opacity: .65; }
  32% { opacity: .52; }
  40% { opacity: .4; }
  48% { opacity: .29; }
  56% { opacity: .19; }
  64% { opacity: .11; }
  72% { opacity: .05; }
  80%, 100% { opacity: 0; }
}

.progress-track {
  width: 100%;
  margin-top: 26px;
  padding: 17px 0;
  border-top: 1px solid var(--text);
  border-bottom: 1px solid var(--text);
}

.progress-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
}

#progress-value {
  color: var(--accent);
  font-size: 13px;
  font-weight: 800;
}

.bar {
  height: 4px;
  margin-top: 15px;
  overflow: hidden;
  background: #cfcdc5;
}

.bar span {
  display: block;
  width: 0;
  height: 100%;
  background: var(--accent);
  transition: none;
}

.progress-view.stage-creating_texture .bar span,
.progress-view.stage-texturing .bar span {
  background: linear-gradient(
    90deg,
    var(--material-red) 0%,
    var(--material-orange) 8.333%,
    var(--material-yellow) 16.667%,
    var(--material-green) 25%,
    var(--material-blue) 33.333%,
    var(--material-purple) 41.667%,
    var(--material-red) 50%,
    var(--material-orange) 58.333%,
    var(--material-yellow) 66.667%,
    var(--material-green) 75%,
    var(--material-blue) 83.333%,
    var(--material-purple) 91.667%,
    var(--material-red) 100%
  );
  background-size: 200% 100%;
  animation: material-progress-flow 1.8s linear infinite;
}

.job-state {
  display: none;
  margin-top: 10px;
  color: var(--muted);
  font-size: 13px;
}

.job-state.error {
  display: block;
  color: var(--danger);
}

.result-button,
.retry-button {
  width: 100%;
  min-height: 50px;
  margin-top: 24px;
  border: 1px solid var(--text);
  border-radius: 0;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  font-weight: 800;
}

.home-button {
  background: var(--text);
  color: var(--surface);
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@keyframes material-border-orbit {
  to { --material-border-angle: 360deg; }
}

@keyframes generation-style-sweep {
  from { --generation-style-sweep: -30%; }
  to { --generation-style-sweep: 130%; }
}

@keyframes material-progress-flow {
  to { background-position: 100% 0; }
}

@media (min-width: 640px) {
  .shell {
    padding-right: 24px;
    padding-left: 24px;
  }

  .brand {
    margin-right: -24px;
    margin-left: -24px;
    padding-right: 26px;
    padding-left: 26px;
  }

}

@media (hover: hover) {
  .file-picker:hover {
    color: var(--accent);
  }

  .file-picker:hover .picker-icon {
    border-color: var(--accent);
    color: var(--accent);
  }

  .submit-button:not(:disabled):hover {
    border-color: var(--accent-hover);
    background: var(--accent-hover);
  }

  .result-button:hover,
  .retry-button:hover,
  #replace-image:hover {
    border-color: var(--accent);
    color: var(--accent);
  }
}

@media (prefers-reduced-motion: reduce) {
  .bar span {
    transition: none;
  }

  .button-loading {
    animation-duration: 1.5s;
  }

  .preview-cell::after {
    animation: none;
  }

  #model-canvas {
    transition: none;
  }

  .progress-view.stage-creating_texture .viewer-stage::after,
  .progress-view.stage-texturing .viewer-stage::after {
    animation: none;
  }

  .progress-view.stage-creating_texture .bar span,
  .progress-view.stage-texturing .bar span {
    animation: none;
  }

  .generation-style-canvas[data-generation-style="toon"],
  .generation-style-canvas[data-generation-style="scan"] {
    animation: none;
  }

  .generation-style-canvas[data-generation-style="toon"] {
    --generation-style-sweep: 34%;
  }

  .generation-style-canvas[data-generation-style="scan"] {
    --generation-style-sweep: 78%;
  }
}
