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

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #242835;
  --border: #333748;
  --text: #e4e6ed;
  --text-dim: #8b8fa3;
  --accent: #6c5ce7;
  --accent-hover: #7f71ef;
  --success: #00d68f;
  --error: #ff6b6b;
  --warning: #ffc048;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
}

#app {
  position: relative;
  width: 100vw;
  height: 100vh;
}

/* Collapsible left drawer ------------------------------------------------- */
.drawer {
  position: fixed;
  top: 0;
  left: 0;
  width: 400px;
  height: 100vh;
  background: var(--surface);
  border-right: 1px solid var(--border);
  box-shadow: 6px 0 24px rgba(0,0,0,0.35);
  display: flex;
  flex-direction: column;
  z-index: 20;
  transform: translateX(0);
  transition: transform 260ms cubic-bezier(0.4, 0, 0.2, 1);
}

.drawer.collapsed { transform: translateX(-100%); }

.drawer-header {
  padding: 18px 20px 14px;
  border-bottom: 1px solid var(--border);
}

.drawer-header-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.btn-icon {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  width: 36px;
  height: 36px;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.15s;
}
.btn-icon:hover { background: var(--accent); border-color: var(--accent); }

.settings-panel {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.settings-panel h2 {
  font-size: 14px;
  letter-spacing: 0.3px;
  margin-bottom: 8px;
}
.settings-panel label {
  margin-top: 12px;
}
.settings-panel input {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 10px 12px;
  font-size: 13px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.settings-panel input:focus {
  outline: none;
  border-color: var(--accent);
}
.settings-hint {
  font-size: 11px;
  color: var(--text-dim);
  line-height: 1.5;
  margin-top: 6px;
}
.settings-hint a { color: var(--accent-hover); }
.settings-buttons {
  display: flex;
  gap: 8px;
  margin-top: 14px;
}
.settings-buttons .btn-primary { margin-top: 0; flex: 1; padding: 10px; font-size: 13px; }
.settings-buttons .btn-export { flex: 0 0 auto; }

/* Mode tabs (describe vs upload) */
.mode-tabs {
  display: flex;
  gap: 0;
  margin-bottom: 14px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px;
}
.mode-tab {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text-dim);
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.mode-tab:hover { color: var(--text); }
.mode-tab.active {
  background: var(--accent);
  color: white;
}

.mode-panel { display: block; }
.mode-panel.hidden { display: none; }

/* Dropzone */
.dropzone {
  border: 2px dashed var(--border);
  border-radius: 10px;
  background: var(--surface2);
  padding: 24px 16px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  position: relative;
  min-height: 160px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.dropzone:hover, .dropzone:focus-visible {
  border-color: var(--accent);
  background: rgba(108, 92, 231, 0.08);
  outline: none;
}
.dropzone.dragover {
  border-color: var(--accent);
  background: rgba(108, 92, 231, 0.15);
}
.dropzone-empty {
  text-align: center;
  color: var(--text-dim);
  pointer-events: none;
}
.dropzone-icon {
  font-size: 36px;
  margin-bottom: 8px;
}
.dropzone-text {
  font-size: 13px;
  color: var(--text);
  font-weight: 500;
}
.dropzone-hint {
  font-size: 11px;
  margin-top: 4px;
}
.dropzone-preview {
  position: relative;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}
.dropzone-preview img {
  max-width: 100%;
  max-height: 220px;
  border-radius: 8px;
  border: 1px solid var(--border);
}
.dropzone-preview .btn-small {
  position: absolute;
  top: -4px;
  right: -4px;
  width: 26px;
  height: 26px;
  padding: 0;
  border-radius: 50%;
  background: var(--surface);
  z-index: 2;
}
.dropzone-filename {
  font-size: 11px;
  color: var(--text-dim);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  word-break: break-all;
  text-align: center;
}

.drawer-header h1 {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.3px;
}

.subtitle {
  color: var(--text-dim);
  font-size: 12px;
  margin-top: 4px;
}

.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 18px 20px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.drawer-body::-webkit-scrollbar { width: 8px; }
.drawer-body::-webkit-scrollbar-thumb {
  background: var(--surface2);
  border-radius: 4px;
}

/* Drawer open/close tab --------------------------------------------------- */
.drawer-toggle {
  position: fixed;
  top: 50%;
  left: 400px;
  transform: translateY(-50%);
  width: 28px;
  height: 56px;
  border: 1px solid var(--border);
  border-left: none;
  border-radius: 0 8px 8px 0;
  background: var(--surface);
  color: var(--text-dim);
  cursor: pointer;
  padding: 0;
  z-index: 21;
  transition: left 260ms cubic-bezier(0.4, 0, 0.2, 1), background 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
}
.drawer-toggle:hover { background: var(--surface2); color: var(--text); }

.drawer-toggle-icon {
  width: 0; height: 0;
  border-top: 6px solid transparent;
  border-bottom: 6px solid transparent;
  border-right: 7px solid currentColor; /* arrow pointing left ◀ */
  transition: transform 260ms cubic-bezier(0.4, 0, 0.2, 1);
}

.drawer.collapsed ~ .drawer-toggle {
  left: 0;
}
.drawer.collapsed ~ .drawer-toggle .drawer-toggle-icon {
  transform: rotate(180deg); /* arrow flips to point right ▶ */
}

/* Fullscreen viewer ------------------------------------------------------- */
.viewer-panel {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  z-index: 1;
}

.sample-picker {
  margin-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.sample-buttons {
  display: flex;
  gap: 8px;
}

.sample-buttons .btn-export {
  flex: 1;
}

/* Input Section */
label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 8px;
}

textarea {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 12px;
  font-size: 14px;
  font-family: inherit;
  resize: vertical;
  transition: border-color 0.2s;
}

textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.options-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 12px;
}

select {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 8px 12px;
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
}

select:focus {
  outline: none;
  border-color: var(--accent);
}

.btn-primary {
  width: 100%;
  margin-top: 16px;
  padding: 14px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
}

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

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

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

/* Progress */
.progress-section {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.step {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  background: var(--surface2);
  border-radius: 8px;
  font-size: 13px;
  flex-wrap: wrap;
}

.step-icon { font-size: 18px; }
.step-label { flex: 1; }

.step.active { border-left: 3px solid var(--accent); }
.step.done { border-left: 3px solid var(--success); }
.step.error { border-left: 3px solid var(--error); }

.progress-bar {
  width: 100%;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
  margin-top: 6px;
}

.progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 2px;
  transition: width 0.3s;
  width: 0%;
}

/* Image Preview */
#imagePreview img {
  width: 100%;
  border-radius: 8px;
  border: 1px solid var(--border);
}

/* Export */
.export-section { }

.export-buttons {
  display: flex;
  gap: 8px;
}

.btn-export {
  flex: 1;
  padding: 10px;
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-export:hover:not(:disabled) {
  background: var(--accent);
  border-color: var(--accent);
}

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

.export-hint {
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 8px;
  line-height: 1.5;
}

/* API Status */
.api-status {
  font-size: 12px;
  color: var(--text-dim);
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

.api-status .key-status {
  display: flex;
  justify-content: space-between;
  padding: 3px 0;
}

/* 3D Viewer */
#viewer3d {
  flex: 1;
  position: relative;
  background: #15171f;
}

#viewerPlaceholder {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  gap: 8px;
}

#viewerPlaceholder p:first-child { font-size: 48px; }
#viewerPlaceholder .hint { font-size: 12px; }

#threeCanvas {
  width: 100%;
  height: 100%;
  display: block;
}

.viewer-controls {
  position: absolute;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 10px 16px;
  background: rgba(26, 29, 39, 0.85);
  border: 1px solid var(--border);
  border-radius: 12px;
  font-size: 12px;
  color: var(--text-dim);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  box-shadow: 0 8px 24px rgba(0,0,0,0.35);
  z-index: 5;
  max-width: calc(100vw - 40px);
  justify-content: center;
}

.viewer-hint {
  display: none;
}

@media (min-width: 1100px) {
  .viewer-hint { display: inline; }
}

.btn-small {
  padding: 6px 14px;
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  transition: background 0.2s;
}

.btn-small:hover { background: var(--accent); border-color: var(--accent); }

.btn-small.active {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.head-track-confidence {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--text-dim);
  padding: 3px 8px;
  border-radius: 4px;
  background: var(--surface2);
}

.confidence-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-dim);
  box-shadow: 0 0 0 0 transparent;
  transition: background 160ms, box-shadow 160ms;
}

.confidence-dot[data-state="tracking"] {
  background: var(--success);
  box-shadow: 0 0 8px rgba(0, 214, 143, 0.7);
}
.confidence-dot[data-state="searching"] {
  background: var(--warning);
  animation: conf-pulse 1.1s ease-in-out infinite;
}
.confidence-dot[data-state="error"] { background: var(--error); }
.confidence-dot[data-state="off"]   { background: var(--text-dim); }

@keyframes conf-pulse {
  0%, 100% { opacity: 1;   }
  50%      { opacity: 0.35; }
}

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--error);
  color: white;
  padding: 12px 24px;
  border-radius: 8px;
  font-size: 14px;
  max-width: 600px;
  z-index: 100;
  box-shadow: 0 4px 20px rgba(0,0,0,0.4);
}

.hidden { display: none !important; }

/* Responsive */
@media (max-width: 640px) {
  .drawer {
    width: 100vw;
  }
  .drawer:not(.collapsed) ~ .drawer-toggle {
    left: calc(100vw - 28px);
  }
}
