/* Light theme — the default. */
:root {
  --bg: #f4f5f8;
  --panel: #ffffff;
  --panel-2: #eceef3;
  --border: #d7dae2;
  --text: #1a1c22;
  --muted: #6b7280;
  --accent: #4a6cff;
  --accent-2: #0ea394;
  --danger: #e5484d;

  --header-grad: linear-gradient(180deg, #ffffff, #f4f5f8);
  --viewer-bg: radial-gradient(circle at 50% 40%, #ffffff, #e8eaf0);
  --on-accent: #ffffff;
  --del-bg: rgba(20, 22, 28, 0.55);
  --logo-glow: rgba(74, 108, 255, 0.35);
}

/* Dark theme — opt-in via the toggle. */
:root[data-theme="dark"] {
  --bg: #0a0a0c;
  --panel: #141419;
  --panel-2: #1c1c24;
  --border: #2a2a34;
  --text: #f2f2f5;
  --muted: #8a8a99;
  --accent: #6c8cff;
  --accent-2: #4ad6c0;
  --danger: #ff6b6b;

  --header-grad: linear-gradient(180deg, #101017, #0a0a0c);
  --viewer-bg: radial-gradient(circle at 50% 40%, #101018, #0a0a0c);
  --on-accent: #0a0a0c;
  --del-bg: rgba(10, 10, 12, 0.8);
  --logo-glow: rgba(108, 140, 255, 0.5);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

header {
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--header-grad);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}
.brand { display: flex; align-items: center; gap: 0.9rem; }
.logo {
  font-size: 1.8rem;
  color: var(--accent);
  filter: drop-shadow(0 0 12px var(--logo-glow));
}

.theme-toggle {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  font-size: 1.15rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.15s, background 0.15s, transform 0.1s;
}
.theme-toggle:hover { border-color: var(--accent); background: var(--panel-2); }
.theme-toggle:active { transform: scale(0.94); }
h1 { font-size: 1.25rem; letter-spacing: -0.02em; }
.tagline { color: var(--muted); font-size: 0.85rem; margin-top: 0.15rem; }

main {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(360px, 460px) 1fr;
  gap: 1px;
  background: var(--border);
  min-height: 0;
}
.panel { background: var(--bg); padding: 1.25rem; overflow-y: auto; }
.panel.right { display: flex; flex-direction: column; }

/* Tabs */
.tabs { display: flex; gap: 0.5rem; margin-bottom: 1rem; }
.tab {
  flex: 1;
  padding: 0.6rem 0.5rem;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--muted);
  font-size: 0.85rem;
  cursor: pointer;
  transition: all 0.15s;
  text-align: center;
}
.tab .badge {
  display: inline-block;
  min-width: 1.4em;
  margin-left: 0.35em;
  padding: 0 0.4em;
  border-radius: 999px;
  background: var(--panel-2);
  font-size: 0.72rem;
  color: var(--muted);
}
.tab.active {
  background: var(--panel-2);
  border-color: var(--accent);
  color: var(--text);
}
.tab.active .badge { background: var(--accent); color: var(--on-accent); }
.tab.custom { border-style: dashed; }
.tab.custom.active { border-color: var(--accent-2); }
.tab.custom.active .badge { background: var(--accent-2); }

.readonly-hint {
  display: flex;
  align-items: flex-start;
  gap: 0.6rem;
  padding: 0.8rem 0.9rem;
  margin-bottom: 1rem;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel);
  font-size: 0.85rem;
  color: var(--muted);
}
.readonly-hint .ro-icon { font-size: 1.1rem; line-height: 1.2; }
.readonly-hint strong { color: var(--text); font-weight: 600; }

/* Dropzone */
.dropzone {
  border: 2px dashed var(--border);
  border-radius: 14px;
  padding: 1.6rem 1rem;
  text-align: center;
  cursor: pointer;
  transition: all 0.15s;
  background: var(--panel);
}
.dropzone:hover, .dropzone.drag {
  border-color: var(--accent);
  background: var(--panel-2);
}
.dz-icon {
  font-size: 1.8rem;
  color: var(--accent);
  display: block;
  margin-bottom: 0.4rem;
}
.dropzone p { font-size: 0.9rem; }
.hint { color: var(--muted); font-size: 0.78rem; margin-top: 0.25rem; }

.upload-progress {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-top: 0.8rem;
}
.up-bar {
  flex: 1;
  height: 8px;
  border-radius: 999px;
  background: var(--panel-2);
  overflow: hidden;
  border: 1px solid var(--border);
}
.up-fill {
  height: 100%;
  width: 0%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  transition: width 0.15s ease;
}
.up-pct {
  font-size: 0.78rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  min-width: 3em;
  text-align: right;
}

.gallery-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 1rem 0 0.6rem;
  font-size: 0.82rem;
  color: var(--muted);
}
.link {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 0.8rem;
}
.link:hover { color: var(--text); }
.link.danger:hover { color: var(--danger); }

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
  gap: 0.5rem;
  margin-bottom: 1.2rem;
}
.thumb {
  position: relative;
  aspect-ratio: 1;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--panel);
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb .del {
  position: absolute;
  top: 3px; right: 3px;
  width: 20px; height: 20px;
  border-radius: 50%;
  background: var(--del-bg);
  border: none;
  color: #fff;
  cursor: pointer;
  font-size: 0.8rem;
  line-height: 1;
  opacity: 0;
  transition: opacity 0.15s;
}
.thumb:hover .del { opacity: 1; }

.process {
  width: 100%;
  padding: 0.85rem;
  border: none;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), #8a6cff);
  color: #fff;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  transition: opacity 0.15s, transform 0.1s;
}
.process:hover:not(:disabled) { transform: translateY(-1px); }
.process:disabled { opacity: 0.4; cursor: not-allowed; }
.spinner {
  width: 15px; height: 15px;
  border: 2px solid rgba(255,255,255,0.35);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.status { margin-top: 0.7rem; font-size: 0.8rem; color: var(--muted); min-height: 1.2em; }
.status.error { color: var(--danger); }
.status.ok { color: var(--accent-2); }

/* Viewer */
.viewer-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.75rem;
}
.viewer-head h2 { font-size: 1rem; }
.viewer-toggles { display: flex; gap: 0.9rem; font-size: 0.8rem; color: var(--muted); }
.viewer-toggles label { display: flex; align-items: center; gap: 0.3rem; cursor: pointer; }
.viewer-toggles label:has(:disabled) { opacity: 0.5; cursor: default; }
.mini-spinner {
  width: 11px; height: 11px;
  border: 2px solid var(--border);
  border-top-color: var(--accent-2);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
.mesh-pct {
  color: var(--accent-2);
  font-variant-numeric: tabular-nums;
  font-size: 0.72rem;
  min-width: 2.4em;
}

.viewer {
  flex: 1;
  min-height: 400px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--viewer-bg);
  position: relative;
  overflow: hidden;
}
.viewer canvas { display: block; }
.viewer-empty {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--muted);
  gap: 0.3rem;
  pointer-events: none;
}

.stats {
  margin-top: 0.75rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  font-size: 0.8rem;
}
.stat {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.4rem 0.7rem;
}
.stat b { color: var(--accent-2); font-variant-numeric: tabular-nums; }
.stat span { color: var(--muted); margin-left: 0.3rem; }

@media (max-width: 900px) {
  main { grid-template-columns: 1fr; }
  .viewer { min-height: 340px; }
}
