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

:root {
  --bg-primary: #0f0f14;
  --bg-secondary: #1a1a24;
  --bg-tertiary: #252533;
  --border: #2e2e3e;
  --text-primary: #e8e8f0;
  --text-secondary: #9898a8;
  --text-hint: #6e6e80;
  --accent: #c066d0;
  --accent-hover: #d480e2;
  --accent-glow: rgba(192, 102, 208, 0.25);
  --success: #4ade80;
  --error: #f87171;
  --warning: #fbbf24;
  --radius: 8px;
  --radius-lg: 12px;
}

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

.app { max-width: 1400px; margin: 0 auto; padding: 1rem 1.5rem; }

/* === Header === */
.app-header {
  display: flex; align-items: center; justify-content: space-between;
  padding-bottom: 1rem; border-bottom: 1px solid var(--border); margin-bottom: 1.5rem;
  gap: 1rem;
}
.app-header h1 { font-size: 1.25rem; font-weight: 600; white-space: nowrap; }

.btn-icon {
  background: var(--bg-tertiary); border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--text-secondary); padding: 0.45rem; cursor: pointer; transition: all 0.2s;
  display: flex; align-items: center; justify-content: center;
}
.btn-icon:hover { color: var(--text-primary); border-color: var(--text-hint); }

/* === Settings Panel === */
.settings-panel {
  background: var(--bg-secondary); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: 1.25rem; margin-bottom: 1.5rem;
}
.settings-panel.hidden { display: none; }
.settings-inner { max-width: 600px; }

/* === Tabs === */
.tab-nav { display: flex; gap: 0.25rem; background: var(--bg-secondary); border-radius: var(--radius); padding: 3px; }
.tab-btn {
  padding: 0.5rem 1rem; border: none; background: transparent;
  color: var(--text-hint); font-size: 0.8rem; font-weight: 500;
  border-radius: 6px; cursor: pointer; transition: all 0.2s; white-space: nowrap;
}
.tab-btn:hover { color: var(--text-secondary); }
.tab-btn.active { background: var(--bg-tertiary); color: var(--text-primary); }
.tab-btn.completed { color: var(--success); }

/* === Steps === */
.step { display: none; }
.step.active { display: block; }

/* === Panels === */
.panel {
  background: var(--bg-secondary); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: 1.25rem;
}
.panel h2 {
  font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.08em;
  color: var(--text-secondary); margin-bottom: 0.75rem; font-weight: 600;
}
.panel h3 {
  font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.05em;
  color: var(--text-hint); margin-bottom: 0.5rem; font-weight: 600;
}
.panel-header { display: flex; justify-content: space-between; align-items: center; }

/* === Form === */
label {
  display: block; font-size: 0.8rem; font-weight: 600; text-transform: uppercase;
  letter-spacing: 0.05em; color: var(--text-secondary); margin-bottom: 0.35rem; margin-top: 0.75rem;
}
input[type="text"], input[type="password"], textarea, select {
  width: 100%; background: var(--bg-tertiary); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 0.6rem 0.75rem;
  color: var(--text-primary); font-size: 0.9rem; font-family: inherit;
  transition: border-color 0.2s; outline: none;
}
input[type="text"]:focus, input[type="password"]:focus, textarea:focus, select:focus {
  border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-glow);
}
.settings-row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; }
.hint { font-size: 0.8rem; color: var(--text-hint); margin-bottom: 0.75rem; }

/* === Buttons === */
.btn-primary {
  padding: 0.65rem 1.5rem; background: var(--accent); color: white; border: none;
  border-radius: var(--radius); font-size: 0.9rem; font-weight: 600;
  cursor: pointer; transition: all 0.2s;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-secondary {
  padding: 0.65rem 1.5rem; background: transparent; color: var(--text-primary);
  border: 1px solid var(--border); border-radius: var(--radius);
  font-size: 0.9rem; font-weight: 500; cursor: pointer; transition: all 0.2s;
}
.btn-secondary:hover { background: var(--bg-tertiary); }

.btn-small {
  padding: 0.35rem 0.75rem; background: var(--bg-tertiary); color: var(--text-secondary);
  border: 1px solid var(--border); border-radius: 6px; font-size: 0.75rem;
  cursor: pointer; transition: all 0.15s;
}
.btn-small:hover { color: var(--text-primary); border-color: var(--text-hint); }
.btn-small.btn-accent { border-color: var(--accent); color: var(--accent); }
.btn-small.btn-accent:hover { background: var(--accent); color: white; }

/* === Step 1: Upload === */
.upload-hero { max-width: 700px; margin: 0 auto; text-align: center; }

.upload-zone {
  border: 2px dashed var(--border); border-radius: var(--radius-lg);
  padding: 3rem 2rem; text-align: center; cursor: pointer; transition: all 0.2s;
}
.upload-zone:hover { border-color: var(--accent); background: rgba(192, 102, 208, 0.05); }
.upload-zone.drag-over { border-color: var(--accent); background: rgba(192, 102, 208, 0.1); }
.upload-zone.small { padding: 1.25rem; }

.upload-placeholder { color: var(--text-hint); }
.upload-placeholder svg { margin-bottom: 0.75rem; opacity: 0.5; }
.upload-placeholder h2 { font-size: 1.25rem; color: var(--text-primary); margin-bottom: 0.5rem; text-transform: none; letter-spacing: normal; }
.upload-placeholder p { font-size: 0.9rem; }
.upload-count { font-size: 0.75rem; color: var(--text-hint); }

.uploaded-preview {
  max-width: 100%; max-height: 450px; border-radius: var(--radius-lg);
  margin: 0 auto; display: block; object-fit: contain;
}
.uploaded-preview.hidden { display: none; }
.upload-actions { display: flex; justify-content: center; gap: 1rem; margin-top: 1.25rem; }
.upload-actions.hidden { display: none; }
.template-meta { text-align: left; margin-top: 1.5rem; max-width: 400px; margin-left: auto; margin-right: auto; }
.template-meta.hidden { display: none; }

/* === Step 2: Analysis === */

/* Loading State */
.analysis-loading {
  display: flex; align-items: center; justify-content: center;
  min-height: 400px; text-align: center;
}
.analysis-loading.hidden { display: none; }

.loader-container { display: flex; flex-direction: column; align-items: center; gap: 1.5rem; }

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

.loader-container h2 {
  font-size: 1.1rem; color: var(--text-primary); font-weight: 600;
}
.loader-container p { font-size: 0.9rem; color: var(--text-hint); max-width: 400px; }

/* Analysis Layout */
.analyze-layout {
  display: grid; grid-template-columns: 1fr 480px; gap: 1.5rem; align-items: start;
}

.analyze-left { position: sticky; top: 1rem; }

.image-ref-wrapper {
  position: relative; border-radius: var(--radius); overflow: hidden; background: #111118;
}
.image-ref-wrapper img { width: 100%; display: block; }

/* Raw JSON */
.json-output {
  background: var(--bg-primary); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1rem; font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 0.78rem; color: var(--text-primary); overflow-x: auto;
  white-space: pre-wrap; word-break: break-word; max-height: 400px; overflow-y: auto;
  line-height: 1.6;
}

.json-edit {
  width: 100%; background: var(--bg-primary); border: 1px solid var(--accent);
  border-radius: var(--radius); padding: 1rem;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 0.78rem; color: var(--text-primary); resize: vertical;
  line-height: 1.6; outline: none; min-height: 300px;
}
.json-edit.hidden { display: none; }

/* Elements List */
.elements-list {
  max-height: 600px; overflow-y: auto; padding-right: 0.25rem;
}

.element-group { margin-bottom: 1rem; }

.element-group-header {
  font-size: 0.7rem; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.08em; color: var(--accent); padding: 0.5rem 0;
  border-bottom: 1px solid var(--border); margin-bottom: 0.5rem;
}

.element-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 0.75rem; padding: 0.6rem 0.75rem; border-radius: var(--radius);
  border: 1px solid transparent; transition: all 0.15s;
  margin-bottom: 0.25rem;
}
.element-row:hover { background: var(--bg-tertiary); }
.element-row.editable {
  background: rgba(192, 102, 208, 0.08); border-color: var(--accent);
}

.element-info { flex: 1; min-width: 0; }

.element-label {
  display: block; font-size: 0.75rem; font-weight: 600;
  color: var(--text-secondary); margin-bottom: 0.15rem;
}
.element-value {
  display: block; font-size: 0.8rem; color: var(--text-hint);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.element-controls {
  display: flex; align-items: center; gap: 0.5rem; flex-shrink: 0;
}

.element-controls .field-key-input {
  width: 100px; font-size: 0.75rem; padding: 0.3rem 0.5rem;
  background: var(--bg-primary); border-radius: 4px;
}
.element-controls .field-type-select {
  width: 80px; font-size: 0.7rem; padding: 0.25rem 0.35rem;
  background: var(--bg-primary); border-radius: 4px;
}

/* Field Config (expanded when toggled on) */
.field-config { display: flex; flex-direction: column; gap: 0.4rem; min-width: 0; }
.field-config-row { display: flex; gap: 0.4rem; align-items: center; }

/* Mode Toggle (full vs inline) */
.field-mode-row {
  display: flex; gap: 0.75rem; align-items: center;
}
.mode-toggle {
  display: flex; align-items: center; gap: 0.3rem; cursor: pointer;
  font-size: 0.7rem; color: var(--text-hint); margin: 0; text-transform: none;
  letter-spacing: normal; font-weight: 500;
}
.mode-toggle input[type="radio"] {
  width: auto; margin: 0; accent-color: var(--accent);
}
.mode-toggle span { white-space: nowrap; }

/* Inline Template Row */
.inline-template-row {
  display: flex; flex-direction: column; gap: 0.3rem;
}
.inline-label {
  font-size: 0.7rem; color: var(--text-secondary); margin: 0;
  text-transform: none; letter-spacing: normal; font-weight: 500;
}
.inline-hint { color: var(--accent); font-weight: 400; }
.inline-template-input {
  font-size: 0.8rem !important; padding: 0.4rem 0.6rem !important;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace !important;
  background: var(--bg-primary) !important;
}
.insert-placeholder-btn { align-self: flex-start; margin-top: 0.15rem; }

/* Inline Preview in User View */
.inline-preview-text {
  display: flex; align-items: center; gap: 0.25rem; flex-wrap: wrap;
  background: var(--bg-tertiary); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 0.5rem 0.75rem;
  font-size: 0.9rem;
}
.inline-preview-text .locked-text {
  color: var(--text-hint); white-space: pre-wrap;
}
.inline-preview-text .inline-input {
  width: auto !important; min-width: 80px; max-width: 200px;
  display: inline-block; border-radius: 4px !important;
  padding: 0.25rem 0.5rem !important; font-size: 0.9rem;
  background: var(--bg-primary); border: 1px solid var(--accent) !important;
  color: var(--accent); font-weight: 600;
}

/* Toggle Switch */
.toggle-switch {
  position: relative; display: inline-block; width: 36px; height: 20px;
  margin: 0; cursor: pointer; flex-shrink: 0;
}
.toggle-switch input { opacity: 0; width: 0; height: 0; }

.toggle-slider {
  position: absolute; inset: 0; background: var(--bg-primary);
  border: 1px solid var(--border); border-radius: 20px; transition: all 0.2s;
}
.toggle-slider::before {
  content: ''; position: absolute; height: 14px; width: 14px;
  left: 2px; bottom: 2px; background: var(--text-hint);
  border-radius: 50%; transition: all 0.2s;
}

.toggle-switch input:checked + .toggle-slider {
  background: var(--accent); border-color: var(--accent);
}
.toggle-switch input:checked + .toggle-slider::before {
  transform: translateX(16px); background: white;
}

/* Analyze Footer */
.analyze-footer {
  display: flex; justify-content: space-between; gap: 0.75rem;
  margin-top: 1rem; padding-top: 1rem; border-top: 1px solid var(--border);
}

/* === Step 3: Output === */
.output-layout { display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem; }

.annotated-preview {
  position: relative; border-radius: var(--radius); overflow: hidden; background: #111118;
}
.annotated-preview img { width: 100%; display: block; }

.output-actions { display: flex; gap: 0.75rem; margin-top: 1rem; flex-wrap: wrap; }

.editable-summary { margin-top: 1rem; }
.editable-summary h3 {
  font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.05em;
  color: var(--text-secondary); margin-bottom: 0.5rem;
}
.editable-summary ul { list-style: none; padding: 0; }
.editable-summary li {
  display: flex; align-items: center; gap: 0.5rem;
  padding: 0.35rem 0; font-size: 0.85rem;
}
.editable-tag {
  background: rgba(192, 102, 208, 0.15); color: var(--accent);
  padding: 0.15rem 0.5rem; border-radius: 4px; font-family: monospace;
  font-size: 0.8rem;
}
.editable-type {
  font-size: 0.7rem; color: var(--text-hint); text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* Ref grid */
.ref-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.5rem; margin-top: 0.75rem; }
.ref-item { position: relative; border-radius: var(--radius); overflow: hidden; aspect-ratio: 1; background: var(--bg-tertiary); }
.ref-item img { width: 100%; height: 100%; object-fit: cover; }
.ref-item .remove-btn {
  position: absolute; top: 4px; right: 4px; width: 22px; height: 22px; border-radius: 50%;
  border: none; background: rgba(0,0,0,0.7); color: white; font-size: 14px; cursor: pointer;
  display: flex; align-items: center; justify-content: center; opacity: 0; transition: opacity 0.15s;
}
.ref-item:hover .remove-btn { opacity: 1; }

/* === Step 4: User View === */
.user-layout { display: flex; flex-direction: column; align-items: center; gap: 1.5rem; }
.user-modal {
  background: var(--bg-secondary); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: 1.5rem; width: 100%; max-width: 700px;
}
.modal-header { margin-bottom: 1rem; }
.modal-header h2 { font-size: 1rem; text-transform: none; letter-spacing: normal; color: var(--text-primary); font-weight: 600; }
.modal-preview-area {
  border-radius: var(--radius); overflow: hidden; margin-bottom: 1.25rem;
  background: var(--bg-tertiary); display: flex; justify-content: center;
}
.modal-preview-img { max-width: 300px; max-height: 200px; object-fit: contain; }

.user-fields-container { margin-bottom: 1.25rem; }

.user-field-group { margin-bottom: 1rem; }
.user-field-group label {
  font-size: 0.85rem; font-weight: 700; text-transform: none;
  letter-spacing: 0; color: var(--text-primary); margin-bottom: 0.35rem;
}
.user-field-group input[type="text"],
.user-field-group textarea {
  border-radius: 20px; padding: 0.6rem 1rem;
}
.user-field-group input[type="text"]:focus,
.user-field-group textarea:focus {
  border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-glow);
}

.user-image-upload {
  width: 100%; max-width: 200px; height: 120px; border: 2px dashed var(--border); border-radius: var(--radius);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  cursor: pointer; transition: all 0.2s; color: var(--text-hint); font-size: 0.75rem;
  text-align: center; gap: 0.35rem; position: relative; overflow: hidden;
}
.user-image-upload:hover { border-color: var(--accent); background: rgba(192, 102, 208, 0.05); }
.user-image-upload img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }

.modal-footer { display: flex; justify-content: flex-end; gap: 0.75rem; padding-top: 1rem; border-top: 1px solid var(--border); }

/* === Utility === */
.hidden { display: none !important; }

/* === Responsive === */
@media (max-width: 1024px) {
  .analyze-layout { grid-template-columns: 1fr; }
  .analyze-left { position: static; }
  .output-layout { grid-template-columns: 1fr; }
}
@media (max-width: 600px) {
  .tab-nav { flex-wrap: wrap; }
  .settings-row { grid-template-columns: 1fr; }
}
