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

html, body {
  height: 100%;
  overflow: hidden;
  background: #0a0a0a;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  color: #fff;
}

.hidden {
  display: none !important;
}

/* App container */
.app {
  position: relative;
  width: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* Debug log */
#debug-log {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  max-height: 150px;
  overflow-y: auto;
  background: rgba(0,0,0,0.9);
  color: #0f0;
  font-family: monospace;
  font-size: 11px;
  padding: 8px;
  z-index: 9999;
  pointer-events: none;
}

/* States */
.state {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
}

/* ============== Loading State ============== */
.state-loading {
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #0a0a0a 0%, #1a1a2e 100%);
}

.loading-content {
  text-align: center;
  padding: 32px;
  max-width: 400px;
}

.loading-icon {
  width: 64px;
  height: 64px;
  margin: 0 auto 24px;
  color: #6366f1;
}

.loading-icon svg {
  width: 100%;
  height: 100%;
}

.spinner-path {
  animation: spin 1s linear infinite;
  transform-origin: center;
}

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

.loading-title {
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 24px;
}

.loading-stages {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 24px;
}

.loading-stage {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  background: rgba(255,255,255,0.05);
  border-radius: 8px;
  font-size: 14px;
  transition: all 0.3s ease;
}

.stage-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #333;
  transition: all 0.3s ease;
}

.loading-stage.pending .stage-dot {
  background: #333;
}

.loading-stage.loading .stage-dot {
  background: #f59e0b;
  animation: pulse 1s ease-in-out infinite;
}

.loading-stage.ready .stage-dot {
  background: #22c55e;
}

.loading-stage.error .stage-dot {
  background: #ef4444;
}

.loading-stage.ready {
  background: rgba(34, 197, 94, 0.1);
}

.loading-stage.error {
  background: rgba(239, 68, 68, 0.1);
}

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.5; transform: scale(0.8); }
}

.progress-bar {
  width: 100%;
  height: 4px;
  background: rgba(255,255,255,0.1);
  border-radius: 2px;
  overflow: hidden;
  margin-bottom: 16px;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #6366f1, #8b5cf6);
  border-radius: 2px;
  transition: width 0.3s ease;
  width: 0%;
}

.loading-message {
  font-size: 14px;
  color: rgba(255,255,255,0.6);
}

/* ============== Live Camera State ============== */
#video {
  flex: 1;
  width: 100%;
  object-fit: cover;
  background: #111;
}

.controls {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 24px;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 24px;
  background: linear-gradient(transparent, rgba(0,0,0,0.7));
}

.btn-capture {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  border: 4px solid #fff;
  background: transparent;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.1s ease;
  -webkit-tap-highlight-color: transparent;
}

.btn-capture:active {
  transform: scale(0.92);
}

.btn-capture-inner {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: #fff;
  transition: background 0.1s ease;
}

.btn-capture:active .btn-capture-inner {
  background: #ddd;
}

.btn-upload,
.btn-switch {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.2);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
  -webkit-tap-highlight-color: transparent;
}

.btn-upload:hover,
.btn-switch:hover {
  background: rgba(255,255,255,0.3);
}

.btn-upload svg,
.btn-switch svg {
  width: 24px;
  height: 24px;
  color: #fff;
}

/* Model status indicator */
.model-status {
  position: absolute;
  top: 16px;
  left: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(8px);
  border-radius: 20px;
  font-size: 12px;
  color: #fff;
  opacity: 0.8;
  transition: opacity 0.5s ease;
}

.model-status .model-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #f5a623;
  animation: pulse 1s ease-in-out infinite;
}

.model-status.ready .model-dot {
  background: #22c55e;
  animation: none;
}

/* ============== Preview State ============== */
#preview {
  flex: 1;
  width: 100%;
  object-fit: contain;
  background: #111;
}

.controls-preview {
  gap: 16px;
}

.btn-action {
  padding: 14px 28px;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s ease, opacity 0.1s ease;
  -webkit-tap-highlight-color: transparent;
}

.btn-action:active {
  transform: scale(0.96);
}

.btn-retake {
  background: rgba(255,255,255,0.2);
  color: #fff;
}

.btn-confirm {
  background: #6366f1;
  color: #fff;
}

.btn-confirm:hover {
  background: #5558e3;
}

.btn-export {
  background: rgba(255,255,255,0.1);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.2);
}

/* ============== Processing State ============== */
.state-processing {
  justify-content: center;
  align-items: center;
  background: #0a0a0a;
}

.processing-content {
  position: relative;
  width: 90%;
  max-width: 400px;
}

.processing-image {
  width: 100%;
  border-radius: 16px;
  opacity: 0.7;
}

.processing-overlay {
  position: absolute;
  inset: 0;
  border-radius: 16px;
  overflow: hidden;
  pointer-events: none;
}

.scanner-line {
  position: absolute;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, transparent, #6366f1, transparent);
  box-shadow: 0 0 20px #6366f1;
  animation: scan 2s ease-in-out infinite;
}

@keyframes scan {
  0%, 100% { top: 0; }
  50% { top: 100%; }
}

.processing-info {
  text-align: center;
  margin-top: 24px;
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid rgba(255,255,255,0.2);
  border-top-color: #6366f1;
  border-radius: 50%;
  margin: 0 auto 16px;
  animation: spin 1s linear infinite;
}

#processingMessage {
  font-size: 16px;
  margin-bottom: 8px;
}

.processing-step {
  font-size: 14px;
  color: rgba(255,255,255,0.5);
}

/* ============== Result State ============== */
.state-result {
  background: #0a0a0a;
  overflow-y: auto;
}

.result-container {
  display: flex;
  flex-direction: column;
  min-height: 100%;
}

.result-image {
  width: 100%;
  max-height: 40vh;
  object-fit: contain;
  background: #111;
}

.result-panel {
  flex: 1;
  padding: 24px;
  background: linear-gradient(180deg, #1a1a2e 0%, #0a0a0a 100%);
}

.result-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.result-header h2 {
  font-size: 24px;
  font-weight: 700;
}

.confidence-badge {
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 600;
}

.confidence-badge.high {
  background: rgba(34, 197, 94, 0.2);
  color: #22c55e;
}

.confidence-badge.medium {
  background: rgba(245, 158, 11, 0.2);
  color: #f59e0b;
}

.confidence-badge.low {
  background: rgba(239, 68, 68, 0.2);
  color: #ef4444;
}

.result-section {
  margin-bottom: 24px;
}

.result-section h3 {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: rgba(255,255,255,0.5);
  margin-bottom: 12px;
}

.result-path {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.path-segment {
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 14px;
}

.path-segment.main {
  background: #6366f1;
  font-weight: 600;
}

.path-segment.sub {
  background: rgba(99, 102, 241, 0.3);
}

.path-arrow {
  color: rgba(255,255,255,0.4);
}

.alternatives {
  display: flex;
  gap: 8px;
  margin-top: 12px;
  flex-wrap: wrap;
}

.alternative-tag {
  padding: 4px 10px;
  background: rgba(255,255,255,0.1);
  border-radius: 4px;
  font-size: 12px;
  color: rgba(255,255,255,0.6);
}

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

.attribute-card {
  background: rgba(255,255,255,0.05);
  border-radius: 12px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.attribute-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: rgba(255,255,255,0.4);
}

.attribute-value {
  font-size: 16px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
}

.color-swatch {
  width: 16px;
  height: 16px;
  border-radius: 4px;
  border: 1px solid rgba(255,255,255,0.2);
}

.attribute-confidence {
  font-size: 12px;
  color: rgba(255,255,255,0.4);
}

.description-text {
  font-size: 14px;
  line-height: 1.6;
  color: rgba(255,255,255,0.8);
  background: rgba(255,255,255,0.05);
  padding: 12px;
  border-radius: 8px;
  font-style: italic;
}

.result-actions {
  display: flex;
  gap: 12px;
  margin-top: 24px;
  padding-bottom: env(safe-area-inset-bottom);
}

.result-actions .btn-action {
  flex: 1;
}

/* ============== Error State ============== */
.state-error {
  justify-content: center;
  align-items: center;
  background: #0a0a0a;
}

.error-content {
  text-align: center;
  padding: 24px;
}

.error-icon {
  width: 64px;
  height: 64px;
  margin: 0 auto 24px;
  color: #ef4444;
}

.error-icon svg {
  width: 100%;
  height: 100%;
}

.error-content p {
  font-size: 18px;
  margin-bottom: 24px;
  opacity: 0.8;
}

/* ============== Warning State ============== */
.state-warning {
  justify-content: center;
  align-items: center;
  background: #0a0a0a;
}

.warning-content {
  text-align: center;
  padding: 24px;
  max-width: 400px;
}

.warning-title {
  color: #f5a623;
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 16px;
}

.warning-content p {
  font-size: 14px;
  margin-bottom: 16px;
  opacity: 0.8;
  line-height: 1.5;
}

.warning-content code {
  display: block;
  background: #151515;
  border: 1px solid #333;
  color: #22c55e;
  padding: 12px;
  border-radius: 8px;
  font-size: 12px;
  text-align: left;
  margin-bottom: 16px;
  user-select: all;
  cursor: pointer;
}

.warning-hint {
  font-size: 12px !important;
  opacity: 0.6 !important;
}

/* ============== Flash Effect ============== */
.flash {
  position: fixed;
  inset: 0;
  background: #fff;
  opacity: 0;
  pointer-events: none;
  z-index: 100;
}

.flash.active {
  animation: flash 0.15s ease-out;
}

@keyframes flash {
  0% { opacity: 0.8; }
  100% { opacity: 0; }
}

/* ============== Safe Area Support ============== */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .controls {
    padding-bottom: calc(24px + env(safe-area-inset-bottom));
  }
}

/* ============== Responsive ============== */
@media (min-width: 768px) {
  .result-container {
    flex-direction: row;
    max-width: 1000px;
    margin: 0 auto;
  }

  .result-image {
    width: 50%;
    max-height: none;
    height: 100vh;
    object-fit: cover;
  }

  .result-panel {
    width: 50%;
    overflow-y: auto;
  }

  .attributes-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (min-width: 1024px) {
  .loading-content {
    max-width: 500px;
  }

  .processing-content {
    max-width: 500px;
  }
}
