/* ===== CSS Variables ===== */
:root {
  --bg-primary: #09090b;
  --bg-secondary: #13131a;
  --bg-tertiary: #1a1a24;
  --border: #27272d;
  --border-hover: #3f3f50;
  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --accent: #818cf8;
  --accent-2: #a78bfa;
  --accent-3: #6366f1;
  --gradient: linear-gradient(135deg, #6366f1, #8b5cf6, #a78bfa);
  --gradient-subtle: linear-gradient(135deg, rgba(99,102,241,.12), rgba(139,92,246,.12), rgba(167,139,250,.08));
  --radius: 12px;
  --radius-sm: 8px;
  --radius-lg: 16px;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 24px rgba(0,0,0,.24);
}

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

html { font-size: 16px; -webkit-font-smoothing: antialiased; scrollbar-color: var(--border) transparent; }

/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--border-hover); }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans SC", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* ===== Navbar ===== */
.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  height: 56px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-secondary);
  flex-shrink: 0;
}
.navbar-brand { display: flex; align-items: center; gap: 10px; }
.logo { font-size: 24px; line-height: 1; }
.title { font-size: 16px; font-weight: 600; letter-spacing: -0.01em; color: var(--text-primary); }
.navbar-badge {
  font-size: 11px; font-weight: 500; color: var(--accent);
  background: rgba(129,140,248,.12); border: 1px solid rgba(129,140,248,.2);
  padding: 3px 10px; border-radius: 20px;
}

/* ===== App Layout ===== */
.app-layout {
  display: grid;
  grid-template-columns: 1fr 300px;
  flex: 1;
  overflow: hidden;
  min-height: 0;
}

/* ===== Main Area ===== */
.main-area {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px;
  gap: 24px;
  overflow-y: auto;
  min-height: 0;
}

/* ===== Upload Zone ===== */
.upload-zone {
  width: 100%;
  max-width: 520px;
  aspect-ratio: 4 / 3;
  border: 2px dashed var(--border);
  border-radius: var(--radius-lg);
  cursor: pointer;
  transition: all .25s ease;
  background: var(--gradient-subtle);
  display: flex;
  align-items: center;
  justify-content: center;
}
.upload-zone:hover {
  border-color: var(--accent);
  background: linear-gradient(135deg, rgba(99,102,241,.16), rgba(139,92,246,.16), rgba(167,139,250,.1));
  box-shadow: 0 0 40px rgba(99,102,241,.1);
}
.upload-zone.drag-over {
  border-color: #a78bfa;
  background: linear-gradient(135deg, rgba(99,102,241,.2), rgba(139,92,246,.2), rgba(167,139,250,.14));
  box-shadow: 0 0 32px rgba(139,92,246,.35), 0 0 64px rgba(99,102,241,.18);
  transform: scale(1.01);
  transition: all .15s ease;
}
.upload-zone-inner {
  text-align: center;
  pointer-events: none;
}
.upload-icon {
  color: var(--text-muted);
  margin-bottom: 16px;
  transition: color .25s ease;
}
.upload-zone:hover .upload-icon { color: var(--accent); }
.upload-title {
  font-size: 16px; font-weight: 500; color: var(--text-secondary);
  margin-bottom: 6px;
}
.upload-hint {
  font-size: 13px; color: var(--text-muted);
}

/* ===== Preview Area ===== */
.preview-area {
  display: none;
  width: 100%;
  max-width: 800px;
}
.preview-area.visible { display: block; }

#stage-container {
  position: relative;
  display: inline-block;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  line-height: 0;
  perspective: 800px;
}
#target-image {
  display: block;
  width: 100%;
  height: auto;
  border-radius: var(--radius);
}
.stage-mask {
  position: absolute;
  pointer-events: auto;
  transform-origin: center center;
  transform-style: preserve-3d;
}
.stage-mask.selected {
  outline: 2px dashed var(--accent);
  outline-offset: 3px;
  pointer-events: auto;
}

/* 微调把手 */
.mask-handle {
  position: absolute;
  z-index: 50;
  pointer-events: auto;
  user-select: none;
}
.mask-handle-corner {
  width: 7px; height: 7px;
  background: #fff;
  border: 1px solid var(--accent);
  border-radius: 1px;
  transform: translate(-50%, -50%);
  cursor: nwse-resize;
}
.mask-handle-corner:nth-child(odd) {
  cursor: nesw-resize;
}
/* 边中点单向拉伸把手 */
.mask-handle-edge {
  position: absolute;
  z-index: 50;
  pointer-events: auto;
  user-select: none;
  background: #fff;
  border: 1px solid var(--accent);
  border-radius: 1px;
  transform: translate(-50%, -50%);
}
.mask-handle-edge-t,
.mask-handle-edge-b {
  width: 16px;
  height: 4px;
  cursor: ns-resize;
}
.mask-handle-edge-l,
.mask-handle-edge-r {
  width: 4px;
  height: 16px;
  cursor: ew-resize;
}
.mask-handle-rotate {
  width: 12px; height: 12px;
  background: var(--accent);
  border: 1.5px solid #fff;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  cursor: grab;
  box-shadow: 0 0 4px rgba(129,140,248,.5);
}
.mask-handle-rotate:active { cursor: grabbing; }
.mask-handle-line {
  position: absolute;
  left: 50%;
  width: 2px;
  background: var(--accent);
  z-index: 49;
  pointer-events: none;
  transform: translateX(-50%);
}

.face-count {
  text-align: center;
  margin-top: 12px;
  font-size: 13px;
  color: var(--text-muted);
  min-height: 20px;
}

/* ===== Panel Block (per-image) ===== */
.panel-block {
  width: 100%;
  max-width: 800px;
  flex-shrink: 0;
  position: relative;
}
.panel-block .stage-container {
  position: relative;
  display: inline-block;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  line-height: 0;
  perspective: 800px;
}
.panel-block .stage-container.active-panel {
  outline: 2px solid var(--accent);
  outline-offset: 4px;
  border-radius: var(--radius);
}
.panel-block .target-image {
  display: block;
  width: 100%;
  height: auto;
  border-radius: var(--radius);
}
.panel-block .face-count {
  text-align: center;
  margin-top: 8px;
  font-size: 13px;
  color: var(--text-muted);
  min-height: 20px;
}

/* ===== Upload Zone Bottom (compact, shown below panels) ===== */
.upload-zone-bottom {
  width: 100%;
  max-width: 520px;
  flex-shrink: 0;
  aspect-ratio: auto;
  padding: 20px 24px;
  border: 2px dashed var(--border);
  border-radius: var(--radius-lg);
  cursor: pointer;
  transition: all .25s ease;
  background: var(--gradient-subtle);
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  margin-bottom: 16px;
}
.upload-zone-bottom:hover {
  border-color: var(--accent);
  background: linear-gradient(135deg, rgba(99,102,241,.16), rgba(139,92,246,.16), rgba(167,139,250,.1));
  box-shadow: 0 0 40px rgba(99,102,241,.1);
}
.upload-zone-bottom.drag-over {
  border-color: #a78bfa;
  background: linear-gradient(135deg, rgba(99,102,241,.2), rgba(139,92,246,.2), rgba(167,139,250,.14));
  box-shadow: 0 0 32px rgba(139,92,246,.35), 0 0 64px rgba(99,102,241,.18);
  transform: scale(1.01);
  transition: all .15s ease;
}
.upload-zone-bottom .upload-zone-inner {
  pointer-events: none;
  display: flex;
  align-items: center;
  gap: 12px;
}
.upload-zone-bottom .upload-icon {
  flex-shrink: 0;
  margin-bottom: 0;
  width: 32px;
  height: 32px;
}
.upload-zone-bottom:hover .upload-icon { color: var(--accent); }
.upload-zone-bottom .upload-title {
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 2px;
  color: var(--text-secondary);
}
.upload-zone-bottom .upload-hint {
  font-size: 12px;
  color: var(--text-muted);
}

/* ===== Loading Overlay ===== */
.loading-overlay {
  display: none;
  position: absolute; inset: 0;
  background: rgba(9,9,11,.85);
  flex-direction: column;
  align-items: center; justify-content: center;
  gap: 12px;
  z-index: 10;
}
.loading-overlay.visible { display: flex; }

.spinner {
  width: 36px; height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loading-text { font-size: 14px; color: var(--text-secondary); }
.loading-hint { font-size: 12px; color: var(--text-muted); }

/* ===== Toolbar ===== */
.toolbar {
  background: var(--bg-secondary);
  border-left: 1px solid var(--border);
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  overflow-y: auto;
  min-height: 0;
}

.toolbar-section { margin-bottom: 4px; }

.toolbar-heading {
  font-size: 12px; font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 12px;
}

.toolbar-divider {
  height: 1px;
  background: var(--border);
  margin: 8px 0 16px;
}

/* ===== Effect Grid ===== */
.effect-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.effect-card {
  aspect-ratio: 1;
  border-radius: var(--radius-sm);
  border: 2px solid transparent;
  background: var(--bg-tertiary);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  transition: all .2s ease;
  font-size: 24px;
  color: var(--text-muted);
}
.effect-card:hover {
  border-color: var(--border-hover);
  color: var(--text-secondary);
}
.effect-card.active {
  border-color: var(--accent);
  background: rgba(129,140,248,.1);
  color: var(--accent);
}
.effect-card .effect-emoji { font-size: 40px; line-height: 1; display: flex; align-items: center; justify-content: center; width: 70%; aspect-ratio: 1; }
.effect-card .effect-icon-img { width: 70%; aspect-ratio: 1; object-fit: contain; border-radius: 4px; }
.effect-card .effect-name { font-size: 10px; color: inherit; }

/* ===== Sliders ===== */
.slider-group { margin-bottom: 14px; }
.slider-group:last-child { margin-bottom: 0; }

.slider-label {
  display: flex; justify-content: space-between;
  font-size: 12px; color: var(--text-muted);
  margin-bottom: 6px;
}
.slider-value { color: var(--text-secondary); font-variant-numeric: tabular-nums; }

.slider {
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 4px;
  border-radius: 2px;
  background: var(--border);
  outline: none;
  cursor: pointer;
}
.slider::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 14px; height: 14px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg-secondary);
  box-shadow: 0 0 0 2px var(--accent);
  cursor: pointer;
  transition: box-shadow .15s ease;
}
.slider::-webkit-slider-thumb:hover { box-shadow: 0 0 0 4px rgba(129,140,248,.4); }
.slider:disabled { opacity: .4; cursor: not-allowed; }
.slider:disabled::-webkit-slider-thumb { cursor: not-allowed; }

/* ===== Download Button ===== */
.btn-enhance {
  width: 100%;
  padding: 10px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  font-size: 13px; font-weight: 500;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  gap: 6px;
  transition: all .2s ease;
  margin-bottom: 8px;
}
.btn-enhance:hover {
  border-color: var(--accent);
  color: var(--text-primary);
  background: rgba(129,140,248,.08);
}

.btn-download {
  margin-top: 8px;
  width: 100%;
  padding: 12px 16px;
  border: none; border-radius: var(--radius-sm);
  background: var(--gradient);
  color: #fff;
  font-size: 14px; font-weight: 600;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  gap: 8px;
  transition: opacity .2s ease, transform .1s ease;
}
.btn-download:hover:not(:disabled) { opacity: .9; transform: translateY(-1px); }
.btn-download:active:not(:disabled) { transform: translateY(0); }
.btn-download:disabled {
  opacity: .35; cursor: not-allowed;
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
  .app-layout {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr auto;
  }
  .main-area { padding: 20px; }
  .upload-zone { max-width: 100%; }

  .toolbar {
    border-left: none;
    border-top: 1px solid var(--border);
    padding: 16px;
    max-height: 40vh;
  }
  .effect-grid { grid-template-columns: repeat(4, 1fr); }

  .panel-block .target-image { max-height: 45vh; }
  .upload-zone { aspect-ratio: auto; min-height: 180px; }
}

/* ===== 马赛克画笔 ===== */
.mosaic-canvas {
  position: absolute;
  top: 0; left: 0;
  z-index: 5;
  pointer-events: auto;
  cursor: crosshair;
}

.mosaic-toolbar {
  display: none;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-top: 10px;
}
.mosaic-toolbar.visible { display: flex; }

.mosaic-btn {
  padding: 6px 16px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  font-size: 13px;
  cursor: pointer;
  transition: all .15s ease;
  user-select: none;
}
.mosaic-btn:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--text-primary);
  background: rgba(129,140,248,.1);
}
.mosaic-btn:disabled {
  opacity: .35;
  cursor: not-allowed;
}

/* ===== Responsive ===== */
@media (max-width: 480px) {
  .navbar { padding: 0 16px; }
  .title { font-size: 14px; }
  .main-area { padding: 16px; }
  .upload-zone { aspect-ratio: auto; min-height: 200px; }
  .effect-grid { grid-template-columns: repeat(3, 1fr); }
}
