:root {
  color-scheme: light;
  --page: #f4f6f5;
  --surface: #ffffff;
  --line: #dce2df;
  --line-strong: #b8c4be;
  --text: #17201c;
  --muted: #68746e;
  --accent: #18794e;
  --accent-hover: #12633f;
  --accent-soft: #e9f4ee;
  --danger: #b42318;
  --danger-soft: #fff0ee;
  --success: #176b45;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

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

body {
  min-height: 100dvh;
  margin: 0;
  background: var(--page);
  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 16px max(28px, env(safe-area-inset-bottom));
}

.brand {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: calc(64px + env(safe-area-inset-top));
  margin: 0 -16px 22px;
  padding: env(safe-area-inset-top) 18px 0;
  border-bottom: 1px solid var(--line);
  background: var(--page);
  font-size: 15px;
  font-weight: 700;
}

.brand-mark {
  display: grid;
  width: 32px;
  height: 32px;
  place-items: center;
  border-radius: 50%;
  background: var(--text);
  color: #fff;
  font-size: 15px;
}

.upload-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.upload-panel {
  padding: 22px 18px 18px;
}

.heading {
  margin-bottom: 22px;
}

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

h1 {
  font-size: 24px;
  line-height: 1.3;
}

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

.heading p {
  margin-top: 7px;
  color: var(--muted);
  font-size: 13px;
}

form {
  display: grid;
  gap: 16px;
}

.field {
  display: grid;
  gap: 8px;
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
}

.field input {
  width: 100%;
  min-height: 48px;
  padding: 0 14px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  outline: none;
  background: #fff;
  color: var(--text);
  font-weight: 400;
}

.field input::placeholder {
  color: #99a39e;
}

.field input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(24, 121, 78, .12);
}

.file-picker {
  display: grid;
  min-height: 184px;
  padding: 24px 16px;
  place-items: center;
  align-content: center;
  gap: 7px;
  border: 1px dashed var(--line-strong);
  border-radius: 8px;
  background: #fafcfb;
  cursor: pointer;
  text-align: center;
}

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

.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: 42px;
  height: 42px;
  margin-bottom: 3px;
  place-items: center;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 27px;
  font-weight: 300;
  line-height: 1;
}

.file-picker strong {
  font-size: 15px;
}

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

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

.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: 36px;
  padding: 0 12px;
  border: 1px solid rgba(23, 32, 28, .16);
  border-radius: 6px;
  background: rgba(255, 255, 255, .94);
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
}

.submit-button {
  display: flex;
  min-height: 50px;
  align-items: center;
  justify-content: center;
  gap: 10px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  font-weight: 700;
}

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

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

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

.button-loading {
  display: none;
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, .45);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .8s linear infinite;
}

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

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

.progress-view {
  min-height: calc(100dvh - 92px);
  padding: 20px 4px 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: 20px;
  gap: 2px;
  padding: 2px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--line);
  transition: gap .2s ease, padding .2s ease, background-color .2s ease;
}

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

.preview-cell::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 1;
  background: #50aa7b;
  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: #626d68;
  background: #626d68;
}

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

.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(255, 255, 255, .72);
}

.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(--line);
  border-radius: 10px;
  background: #eff3f1;
}

#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 14px 14px;
  z-index: 2;
  padding: 9px 12px;
  border: 1px solid rgba(23, 32, 28, .1);
  border-radius: 7px;
  background: rgba(255, 255, 255, .9);
  color: var(--muted);
  font-size: 12px;
  backdrop-filter: blur(8px);
}

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

.style-switcher {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 5px;
  margin-top: 12px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #e9eeeb;
}

.style-switcher button {
  min-width: 0;
  min-height: 38px;
  padding: 0 3px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 12px;
  font-weight: 650;
}

.style-switcher button.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 4px rgba(23, 32, 28, .12);
}

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

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

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

.progress-view.viewer-active .progress-track {
  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: 24px;
  padding: 17px 0;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}

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

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

.bar {
  height: 7px;
  margin-top: 15px;
  overflow: hidden;
  border-radius: 999px;
  background: #e3e8e5;
}

.bar span {
  display: block;
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: var(--accent);
  transition: width .25s ease;
}

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

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

.retry-button {
  width: 100%;
  min-height: 48px;
  margin-top: 24px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-weight: 700;
}

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

@media (min-width: 640px) {
  .upload-panel {
    padding: 26px 24px 22px;
  }
}

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

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

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

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

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

  #model-canvas {
    transition: none;
  }
}
