:root {
  --bg: #0f0f14;
  --surface: #1a1a24;
  --surface-2: #242433;
  --border: #2e2e42;
  --text: #e8e8f0;
  --text-dim: #9090a8;
  --primary: #7c5cff;
  --primary-hover: #6d4dff;
  --danger: #ff5c7c;
  --success: #4ade80;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Heebo", sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.6;
}

.container {
  max-width: 1100px;
  margin: 0 auto;
  padding: 2rem 1.5rem;
}

header {
  text-align: center;
  margin-bottom: 2rem;
}

h1 {
  font-size: 2.25rem;
  font-weight: 700;
  background: linear-gradient(135deg, #7c5cff, #5cffd0);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  margin-bottom: 0.5rem;
}

.subtitle { color: var(--text-dim); font-size: 0.95rem; }

section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.5rem;
  margin-bottom: 1.25rem;
}

label {
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--text-dim);
}

.drop-zone {
  border: 2px dashed var(--border);
  border-radius: 8px;
  padding: 3rem 1.5rem;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}

.drop-zone:hover, .drop-zone.dragover {
  border-color: var(--primary);
  background: rgba(124, 92, 255, 0.05);
}

.drop-content svg {
  color: var(--text-dim);
  margin-bottom: 1rem;
}

.drop-content p { color: var(--text); }
.drop-content .small { color: var(--text-dim); font-size: 0.85rem; margin-top: 0.25rem; }

.ai-panel {
  background: linear-gradient(135deg, rgba(124,92,255,0.08), rgba(92,255,208,0.05));
  border: 1px solid rgba(124,92,255,0.3);
  border-radius: 12px;
  padding: 1.25rem;
  margin-bottom: 1.25rem;
}

.ai-status {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-dim);
  padding: 0.25rem 0.6rem;
  background: var(--surface-2);
  border-radius: 6px;
}

.ai-status.loading { color: var(--primary); }
.ai-status.ready { color: var(--success); }
.ai-status.error { color: var(--danger); }

.ai-actions {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
  margin-top: 0.75rem;
}

.ai-toggle {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.9rem;
  color: var(--text);
  font-weight: 600;
  cursor: pointer;
}

.ai-toggle input:disabled + * { opacity: 0.4; }
.ai-toggle:has(input:disabled) { opacity: 0.5; cursor: not-allowed; }

.load-progress {
  margin-top: 0.75rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.progress-bar {
  flex: 1;
  height: 8px;
  background: var(--surface-2);
  border-radius: 4px;
  overflow: hidden;
}

#progressFill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--primary), #5cffd0);
  transition: width 0.2s;
}

#progressText {
  font-size: 0.85rem;
  color: var(--text-dim);
  font-weight: 600;
  min-width: 48px;
  text-align: left;
}

.step-hint {
  font-size: 0.75rem;
  color: var(--text-dim);
  font-style: italic;
}

.ai-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.75rem;
}

.ai-header h2 {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text);
}

.btn-link {
  background: none;
  border: none;
  color: var(--primary);
  font-size: 0.85rem;
  cursor: pointer;
  font-family: inherit;
  font-weight: 600;
}

.btn-link:hover { text-decoration: underline; }

.ai-desc {
  font-size: 0.9rem;
  color: var(--text-dim);
  margin-bottom: 0.75rem;
}

.ai-desc a { color: var(--primary); text-decoration: none; }
.ai-desc a:hover { text-decoration: underline; }

.key-row {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.key-row input {
  flex: 1;
  padding: 0.6rem 0.9rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
}

.key-row input:focus { outline: none; border-color: var(--primary); }

.ai-prompt-row { margin-bottom: 0.75rem; }

.ai-prompt-row textarea {
  width: 100%;
  padding: 0.6rem 0.9rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
  resize: vertical;
  min-height: 50px;
}

.ai-prompt-row textarea:focus { outline: none; border-color: var(--primary); }

.btn-ai {
  width: 100%;
  background: linear-gradient(135deg, var(--primary), #5cffd0);
  font-size: 1rem;
  padding: 0.85rem;
}

.btn-ai:hover:not(:disabled) {
  background: linear-gradient(135deg, var(--primary-hover), #4ad6b0);
}

.divider {
  text-align: center;
  margin: 1.25rem 0;
  position: relative;
  color: var(--text-dim);
  font-size: 0.85rem;
}

.divider::before,
.divider::after {
  content: "";
  position: absolute;
  top: 50%;
  width: 40%;
  height: 1px;
  background: var(--border);
}

.divider::before { right: 0; }
.divider::after { left: 0; }

.divider span {
  background: var(--bg);
  padding: 0 1rem;
  position: relative;
}

.workflow {
  background: var(--surface-2);
  border-radius: 8px;
  padding: 1rem 1.25rem;
  margin-bottom: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.step {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.step-num {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--primary);
  color: white;
  font-weight: 700;
  font-size: 0.9rem;
  flex-shrink: 0;
}

.step-text {
  color: var(--text);
  font-size: 0.95rem;
}

.source-status {
  font-size: 0.85rem;
  color: var(--text-dim);
  font-weight: 600;
}

.controls-panel {
  background: var(--surface-2);
  border-radius: 8px;
  padding: 1rem 1.25rem;
  margin-bottom: 1rem;
  display: flex;
  flex-wrap: wrap;
  gap: 1.25rem;
  align-items: center;
}

.tool-group {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.tool-group input[type="range"] {
  width: 110px;
  accent-color: var(--primary);
}

.tool-group span {
  min-width: 28px;
  font-size: 0.9rem;
  color: var(--text);
  font-weight: 600;
}

input[type="checkbox"] {
  accent-color: var(--primary);
  margin-left: 0.4rem;
}

.canvas-wrap {
  position: relative;
  background: #000;
  border-radius: 8px;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 300px;
  max-height: 70vh;
  margin-bottom: 1rem;
}

#imgCanvas, #maskCanvas {
  position: relative;
  max-width: 100%;
  max-height: 70vh;
  display: block;
  touch-action: none;
}

#maskCanvas {
  position: absolute;
  cursor: pointer;
  opacity: 0.7;
}

.actions {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.btn {
  padding: 0.7rem 1.4rem;
  border-radius: 8px;
  border: none;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s, opacity 0.15s, transform 0.05s;
  text-decoration: none;
  display: inline-block;
  text-align: center;
}

.btn:active:not(:disabled) { transform: scale(0.98); }

.btn-primary {
  background: var(--primary);
  color: white;
}

.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }

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

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

.btn-secondary:hover:not(:disabled) { background: var(--border); }

.status {
  text-align: center;
  font-size: 0.95rem;
  margin-top: 1rem;
  padding: 0.75rem;
  border-radius: 8px;
}

.status.loading { color: var(--primary); background: rgba(124,92,255,0.1); }
.status.error { color: var(--danger); background: rgba(255,92,124,0.1); }
.status.success { color: var(--success); background: rgba(74,222,128,0.1); }

.spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid rgba(124, 92, 255, 0.3);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: middle;
  margin-left: 0.5rem;
}

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

.hidden { display: none !important; }

.ai-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 15, 20, 0.85);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  animation: overlay-fade 0.25s ease-out;
}

@keyframes overlay-fade {
  from { opacity: 0; }
  to { opacity: 1; }
}

.ai-overlay-content {
  text-align: center;
  padding: 2rem;
  max-width: 90vw;
}

.ai-orb {
  position: relative;
  width: 160px;
  height: 160px;
  margin: 0 auto 2rem;
}

.orb-ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 2px solid transparent;
  border-top-color: #7c5cff;
  border-right-color: #5cffd0;
}

.orb-ring.r1 { animation: orb-spin 1.5s linear infinite; }
.orb-ring.r2 { inset: 14px; animation: orb-spin 2s linear infinite reverse; border-top-color: #5cffd0; border-right-color: #7c5cff; }
.orb-ring.r3 { inset: 28px; animation: orb-spin 2.5s linear infinite; border-top-color: #ff5cb0; border-right-color: #7c5cff; }

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

.orb-core {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 3.5rem;
  animation: orb-pulse 1.6s ease-in-out infinite;
  filter: drop-shadow(0 0 18px rgba(124, 92, 255, 0.6));
}

@keyframes orb-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.12); }
}

.ai-overlay-title {
  font-size: 2rem;
  font-weight: 800;
  background: linear-gradient(135deg, #7c5cff, #5cffd0);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  margin-bottom: 0.75rem;
  letter-spacing: 0.5px;
}

.ai-overlay-sub {
  font-size: 1rem;
  color: var(--text-dim);
  margin-bottom: 1.5rem;
}

.ai-progress-dots {
  display: flex;
  justify-content: center;
  gap: 0.6rem;
}

.ai-progress-dots span {
  width: 10px;
  height: 10px;
  background: var(--primary);
  border-radius: 50%;
  animation: dot-bounce 1.2s ease-in-out infinite;
}

.ai-progress-dots span:nth-child(2) { animation-delay: 0.2s; background: #5cffd0; }
.ai-progress-dots span:nth-child(3) { animation-delay: 0.4s; background: #ff5cb0; }

@keyframes dot-bounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.5; }
  40% { transform: scale(1.2); opacity: 1; }
}

@media (max-width: 640px) {
  .ai-overlay-title { font-size: 1.5rem; }
  .ai-orb { width: 120px; height: 120px; }
  .orb-core { font-size: 2.5rem; }
}

footer {
  text-align: center;
  color: var(--text-dim);
  font-size: 0.85rem;
  margin-top: 2rem;
  padding: 1rem;
}

footer .small { font-size: 0.75rem; margin-top: 0.25rem; opacity: 0.7; }

@media (max-width: 640px) {
  h1 { font-size: 1.75rem; }
  .container { padding: 1rem; }
  .controls-panel { gap: 0.75rem; }
  .tool-group input[type="range"] { width: 80px; }
}
