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

:root {
  --bg: #f0f0f2;
  --surface: #ffffff;
  --surface-2: #f7f7f9;
  --border: #e2e2e6;
  --border-strong: #c8c8ce;
  --text: #1a1a1f;
  --text-muted: #7a7a85;
  --text-faint: #a8a8b3;
  --accent: #5b4fcf;
  --accent-light: #eeecfb;
  --accent-hover: #4a3fb8;
  --accent-soft: rgba(91,79,207,0.12);
  --red: #e53e3e;
  --red-light: #fff5f5;
  --green: #38a169;
  --green-light: #f0fff4;
  --radius-sm: 6px;
  --radius: 10px;
  --radius-lg: 16px;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.06);
  --shadow: 0 2px 8px rgba(0,0,0,0.08);
  --shadow-lg: 0 8px 24px rgba(0,0,0,0.12);
  --transition: 0.15s ease;
  --font: 'Inter', system-ui, -apple-system, sans-serif;
}

html, body {
  height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ─── LAYOUT ─── */
.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

.header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 24px;
  height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 10;
}

.logo {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.5px;
  color: var(--text);
  flex-shrink: 0;
}

.logo span { color: var(--accent); }

.header-steps {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1;
  justify-content: center;
}

.step-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--border-strong);
  transition: var(--transition);
}

.step-dot.active { background: var(--accent); transform: scale(1.3); }
.step-dot.done { background: var(--accent); opacity: 0.4; }

.header-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

main {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 24px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

/* ─── BUTTONS ─── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  font-family: var(--font);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border: none;
  transition: var(--transition);
  text-decoration: none;
  white-space: nowrap;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}
.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-strong);
}
.btn-secondary:hover { background: var(--surface-2); }

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
}
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }

.btn-danger {
  background: var(--red-light);
  color: var(--red);
}
.btn-danger:hover { background: #fed7d7; }

.btn-sm {
  padding: 5px 10px;
  font-size: 12px;
}

.btn-icon {
  padding: 7px;
  width: 32px;
  height: 32px;
  justify-content: center;
}

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ─── STEP 1: GENDER ─── */
.gender-screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 32px;
  padding: 40px;
}

.gender-screen h1 {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.5px;
  color: var(--text);
}

.gender-screen p {
  color: var(--text-muted);
  margin-top: -20px;
  font-size: 15px;
}

.gender-cards {
  display: flex;
  gap: 20px;
}

.gender-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  width: 180px;
  height: 200px;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius-lg);
  cursor: pointer;
  transition: var(--transition);
  box-shadow: var(--shadow);
}

.gender-card:hover {
  border-color: var(--accent);
  box-shadow: var(--shadow-lg), 0 0 0 4px var(--accent-soft);
  transform: translateY(-2px);
}

.gender-card.selected {
  border-color: var(--accent);
  background: var(--accent-light);
}

.gender-card svg {
  width: 72px;
  height: 72px;
  opacity: 0.7;
}

.gender-card span {
  font-weight: 600;
  font-size: 16px;
  color: var(--text);
}

/* ─── BUILDER LAYOUT (steps 2 & 3) ─── */
.builder-layout {
  display: grid;
  grid-template-columns: 260px 1fr 200px;
  flex: 1;
  overflow: hidden;
}

.panel-items {
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  background: var(--surface);
  overflow: hidden;
}

.panel-canvas {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  padding: 24px;
  gap: 16px;
}

.panel-layers {
  display: flex;
  flex-direction: column;
  border-left: 1px solid var(--border);
  background: var(--surface);
  overflow: hidden;
}

/* ─── CATEGORY TABS ─── */
.cat-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.cat-tab {
  padding: 5px 10px;
  border-radius: var(--radius-sm);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-muted);
  font-family: var(--font);
  transition: var(--transition);
}

.cat-tab:hover { background: var(--surface-2); color: var(--text); }
.cat-tab.active {
  background: var(--accent-light);
  color: var(--accent);
  border-color: var(--accent);
}

/* ─── ITEM GRID ─── */
.item-grid-wrap {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
}

.item-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.item-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 8px;
  border-radius: var(--radius);
  border: 2px solid transparent;
  cursor: pointer;
  transition: var(--transition);
  background: var(--surface-2);
}

.item-card:hover {
  border-color: var(--border-strong);
  background: var(--surface);
}

.item-card.selected {
  border-color: var(--accent);
  background: var(--accent-light);
}

.item-thumb {
  width: 100%;
  aspect-ratio: 1;
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: #f0f0f0;
  background-image:
    linear-gradient(45deg, #ddd 25%, transparent 25%),
    linear-gradient(-45deg, #ddd 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #ddd 75%),
    linear-gradient(-45deg, transparent 75%, #ddd 75%);
  background-size: 10px 10px;
  background-position: 0 0, 0 5px, 5px -5px, -5px 0px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.item-thumb img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

.item-thumb.none-thumb {
  background: var(--surface-2);
  background-image: none;
}

.item-name {
  font-size: 11px;
  font-weight: 500;
  color: var(--text-muted);
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
}

.item-card.selected .item-name { color: var(--accent); }

.empty-state {
  grid-column: 1 / -1;
  text-align: center;
  padding: 40px 16px;
  color: var(--text-faint);
  font-size: 13px;
}

/* ─── CANVAS ─── */
.canvas-wrap {
  position: relative;
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-lg);
  background: #f8f8fa;
  background-image:
    linear-gradient(45deg, #ebebed 25%, transparent 25%),
    linear-gradient(-45deg, #ebebed 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #ebebed 75%),
    linear-gradient(-45deg, transparent 75%, #ebebed 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
}

#avatar-canvas {
  display: block;
  width: 400px;
  height: 400px;
}

.canvas-label {
  font-size: 12px;
  color: var(--text-faint);
  text-align: center;
}

/* ─── LAYER PANEL ─── */
.layer-panel-header {
  padding: 12px 14px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-faint);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.layer-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.layer-section-label {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-faint);
  padding: 6px 6px 2px;
}

.layer-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 12px;
  color: var(--text);
  user-select: none;
}

.layer-item.locked {
  opacity: 0.5;
}

.layer-item.empty {
  opacity: 0.3;
  font-style: italic;
  color: var(--text-faint);
}

.layer-drag-handle {
  cursor: grab;
  color: var(--text-faint);
  flex-shrink: 0;
  display: flex;
}

.layer-drag-handle:active { cursor: grabbing; }

.layer-item.dragging {
  opacity: 0.4;
}

.layer-item.drag-over {
  border-color: var(--accent);
  background: var(--accent-light);
}

.layer-thumb {
  width: 28px;
  height: 28px;
  border-radius: 4px;
  overflow: hidden;
  background: #e8e8ea;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.layer-thumb img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.layer-item-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 11px;
}

/* ─── ADMIN ─── */
.admin-app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.admin-header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 24px;
  height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.admin-header .logo { flex: 1; }

.admin-main {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.admin-sidebar {
  width: 200px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 16px 12px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.admin-nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  border: none;
  background: transparent;
  font-family: var(--font);
  width: 100%;
  text-align: left;
  transition: var(--transition);
}

.admin-nav-item:hover { background: var(--surface-2); color: var(--text); }
.admin-nav-item.active { background: var(--accent-light); color: var(--accent); }

.admin-content {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
}

/* ─── AUTH SCREEN ─── */
.auth-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
}

.auth-card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 40px;
  width: 360px;
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  gap: 20px;
  border: 1px solid var(--border);
}

.auth-card h2 { font-size: 20px; font-weight: 700; }
.auth-card p { color: var(--text-muted); font-size: 13px; margin-top: -10px; }

/* ─── FORMS ─── */
.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.form-input,
.form-select,
.form-textarea {
  padding: 9px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  font-family: var(--font);
  font-size: 14px;
  color: var(--text);
  background: var(--surface);
  transition: var(--transition);
  outline: none;
  width: 100%;
}

.form-input:focus,
.form-select:focus,
.form-textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.form-textarea { resize: vertical; min-height: 80px; }

.form-radio-group {
  display: flex;
  gap: 8px;
}

.radio-option {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  transition: var(--transition);
}

.radio-option input { display: none; }

.radio-option:has(input:checked) {
  border-color: var(--accent);
  background: var(--accent-light);
  color: var(--accent);
}

/* ─── DROP ZONE ─── */
.drop-zone {
  border: 2px dashed var(--border-strong);
  border-radius: var(--radius);
  padding: 40px;
  text-align: center;
  cursor: pointer;
  transition: var(--transition);
  background: var(--surface-2);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.drop-zone:hover,
.drop-zone.drag-active {
  border-color: var(--accent);
  background: var(--accent-light);
}

.drop-zone svg { color: var(--text-faint); }
.drop-zone p { color: var(--text-muted); font-size: 13px; }
.drop-zone small { color: var(--text-faint); font-size: 12px; }

/* ─── UPLOAD PREVIEW ─── */
.upload-preview {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.upload-preview-img {
  width: 160px;
  height: 160px;
  border-radius: var(--radius);
  object-fit: contain;
  background: #eee;
  background-image:
    linear-gradient(45deg, #ddd 25%, transparent 25%),
    linear-gradient(-45deg, #ddd 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #ddd 75%),
    linear-gradient(-45deg, transparent 75%, #ddd 75%);
  background-size: 10px 10px;
  background-position: 0 0, 0 5px, 5px -5px, -5px 0px;
  align-self: flex-start;
  border: 1px solid var(--border);
}

/* ─── ASSET GRID (admin) ─── */
.asset-filters {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
  flex-wrap: wrap;
  align-items: center;
}

.admin-asset-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 12px;
}

.admin-asset-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  transition: var(--transition);
}

.admin-asset-card:hover {
  box-shadow: var(--shadow);
  border-color: var(--border-strong);
}

.admin-asset-thumb {
  width: 100%;
  aspect-ratio: 1;
  background: #f0f0f0;
  background-image:
    linear-gradient(45deg, #e2e2e2 25%, transparent 25%),
    linear-gradient(-45deg, #e2e2e2 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #e2e2e2 75%),
    linear-gradient(-45deg, transparent 75%, #e2e2e2 75%);
  background-size: 10px 10px;
  background-position: 0 0, 0 5px, 5px -5px, -5px 0px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.admin-asset-thumb img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.admin-asset-info {
  padding: 10px;
  border-top: 1px solid var(--border);
}

.admin-asset-name {
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.admin-asset-meta {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}

.badge {
  display: inline-flex;
  padding: 2px 6px;
  border-radius: 100px;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.badge-base { background: #e8f4fd; color: #1a6fa8; }
.badge-clothing { background: #fdf3e8; color: #c05621; }
.badge-both { background: var(--surface-2); color: var(--text-muted); }
.badge-male { background: #ebf8ff; color: #2b6cb0; }
.badge-female { background: #fff0f3; color: #97266d; }

.admin-asset-actions {
  display: flex;
  gap: 4px;
}

/* ─── MODAL ─── */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  backdrop-filter: blur(4px);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.modal {
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 560px;
  max-height: 90vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px 16px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 1;
}

.modal-title { font-size: 16px; font-weight: 700; }

.modal-body { padding: 24px; display: flex; flex-direction: column; gap: 20px; }

.modal-footer {
  padding: 16px 24px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  position: sticky;
  bottom: 0;
  background: var(--surface);
}

/* ─── RECOLOR TOOL ─── */
.recolor-preview-area {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.recolor-canvas-wrap {
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: center;
}

.recolor-canvas-label {
  font-size: 11px;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.recolor-canvas-bg {
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1px solid var(--border);
  background: #f0f0f0;
  background-image:
    linear-gradient(45deg, #e2e2e2 25%, transparent 25%),
    linear-gradient(-45deg, #e2e2e2 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #e2e2e2 75%),
    linear-gradient(-45deg, transparent 75%, #e2e2e2 75%);
  background-size: 10px 10px;
  background-position: 0 0, 0 5px, 5px -5px, -5px 0px;
}

.recolor-canvas-bg canvas {
  display: block;
  width: 180px;
  height: 180px;
}

.slider-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.slider-label-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.slider-label { font-size: 12px; font-weight: 600; color: var(--text-muted); }
.slider-value { font-size: 12px; font-weight: 600; color: var(--accent); font-variant-numeric: tabular-nums; }

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
}

/* ─── TOAST ─── */
.toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 999;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  animation: slide-in 0.2s ease;
  max-width: 300px;
}

.toast-success { background: #1a1a1f; color: #fff; }
.toast-error { background: var(--red); color: #fff; }

@keyframes slide-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ─── MISC ─── */
.divider {
  height: 1px;
  background: var(--border);
  margin: 4px 0;
}

.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px;
  color: var(--text-faint);
  font-size: 13px;
  gap: 10px;
}

.spinner {
  width: 18px;
  height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

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

.page-title {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 20px;
}

.section-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 24px;
  margin-bottom: 20px;
}

.section-card h3 {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
}

/* ─── SCROLLBARS ─── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 100px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-faint); }

/* ─── RESPONSIVE ─── */
@media (max-width: 768px) {
  .builder-layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr auto;
  }

  .panel-layers { display: none; }
  #avatar-canvas { width: 280px; height: 280px; }

  .gender-cards { flex-direction: column; }
}
