:root {
  --bg: #0f0f0f;
  --surface: #1a1a1a;
  --surface-2: #252525;
  --text: #f0f0f0;
  --text-2: #a0a0a0;
  --accent: #ff3366;
  --accent-2: #ff8844;
  --warning: #ffcc00;
  --safe: #44cc88;
  --border: #333;
}

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

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

#app {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
  min-height: 100vh;
}

header {
  text-align: center;
  margin-bottom: 24px;
}

header h1 {
  font-size: 28px;
  font-weight: 800;
  letter-spacing: -0.5px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

header p {
  color: var(--text-2);
  font-size: 14px;
  margin-top: 6px;
}

section {
  margin-bottom: 24px;
}

.hidden { display: none !important; }

/* Upload area */
.upload-area {
  border: 2px dashed var(--border);
  border-radius: 16px;
  padding: 40px 20px;
  text-align: center;
  color: var(--text-2);
  cursor: pointer;
  transition: all 0.2s;
  margin-bottom: 12px;
}

.upload-area:hover, .upload-area.dragover {
  border-color: var(--accent);
  background: var(--surface);
}

.upload-area svg {
  margin-bottom: 12px;
  opacity: 0.6;
}

.upload-area p {
  font-size: 14px;
}

/* Buttons */
.btn {
  border: none;
  border-radius: 12px;
  padding: 14px 24px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: white;
}

.btn-secondary {
  background: var(--surface-2);
  color: var(--text);
}

.btn-full { width: 100%; }

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

/* Camera */
#camera-section {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: black;
  z-index: 100;
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh; /* dynamic viewport height for mobile */
}

#video {
  flex: 1 1 auto;
  width: 100%;
  min-height: 0;
  object-fit: cover;
}

.camera-controls {
  flex: 0 0 auto;
  padding: 20px 20px calc(20px + env(safe-area-inset-bottom));
  display: flex;
  gap: 12px;
  justify-content: center;
  background: rgba(0,0,0,0.9);
}

/* Selection UI */
#select-section {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: var(--bg);
  z-index: 100;
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

.select-header {
  flex: 0 0 auto;
  padding: 16px 20px;
  text-align: center;
}

.select-header h2 {
  font-size: 20px;
  margin-bottom: 4px;
}

.select-header p {
  color: var(--text-2);
  font-size: 14px;
}

#select-container {
  flex: 1 1 auto;
  position: relative;
  min-height: 0;
  overflow: hidden;
  background: #000;
}

#select-image-canvas,
#select-overlay-canvas {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.select-actions {
  flex: 0 0 auto;
  padding: 16px 20px calc(16px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--bg);
}

/* Crop guide overlay */
.crop-guide {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  pointer-events: none;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 101;
}

.crop-guide-inner {
  width: 88vw;
  height: 28vh;
  border: 2px dashed rgba(255,255,255,0.6);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.crop-guide-label {
  background: rgba(0,0,0,0.7);
  color: white;
  padding: 6px 14px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 600;
  position: absolute;
  top: -14px;
}

/* Processing */
#processing-section {
  text-align: center;
  padding: 60px 20px;
}

.spinner {
  width: 48px;
  height: 48px;
  border: 4px solid var(--surface-2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 16px;
}

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

/* Results */
.results-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.results-header h2 {
  font-size: 22px;
}

.risk-summary {
  display: flex;
  gap: 8px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.risk-pill {
  padding: 8px 14px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.risk-pill.critical { background: rgba(255, 51, 102, 0.2); color: #ff4466; border: 1px solid rgba(255, 51, 102, 0.3); }
.risk-pill.warning { background: rgba(255, 136, 68, 0.2); color: #ff9955; border: 1px solid rgba(255, 136, 68, 0.3); }
.risk-pill.caution { background: rgba(255, 204, 0, 0.15); color: #ffcc44; border: 1px solid rgba(255, 204, 0, 0.3); }

.detected-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.ingredient-card {
  background: var(--surface);
  border-radius: 14px;
  padding: 16px;
  cursor: pointer;
  transition: all 0.2s;
  border-left: 4px solid transparent;
}

.ingredient-card:hover { background: var(--surface-2); }

.ingredient-card.critical { border-left-color: #ff4466; }
.ingredient-card.warning { border-left-color: #ff9955; }
.ingredient-card.caution { border-left-color: #ffcc44; }

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

.card-name {
  font-weight: 700;
  font-size: 16px;
}

.risk-badge {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  padding: 4px 10px;
  border-radius: 6px;
}

.risk-badge.critical { background: rgba(255, 51, 102, 0.2); color: #ff4466; }
.risk-badge.warning { background: rgba(255, 136, 68, 0.2); color: #ff9955; }
.risk-badge.caution { background: rgba(255, 204, 0, 0.15); color: #ffcc44; }

.card-why {
  color: var(--text-2);
  font-size: 13px;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.safe-ingredients {
  margin-top: 24px;
  padding-top: 24px;
  border-top: 1px solid var(--border);
}

.safe-ingredients h3 {
  font-size: 16px;
  color: var(--text-2);
  margin-bottom: 12px;
}

.safe-ingredients ul {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.safe-ingredients li {
  background: var(--surface);
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 13px;
  color: var(--text-2);
}

/* Manual input */
#manual-section {
  margin-top: 32px;
  padding-top: 24px;
  border-top: 1px solid var(--border);
}

#manual-section h3 {
  font-size: 16px;
  margin-bottom: 12px;
  color: var(--text-2);
}

#manual-input {
  width: 100%;
  min-height: 100px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  color: var(--text);
  font-size: 15px;
  resize: vertical;
  margin-bottom: 12px;
}

#manual-input:focus {
  outline: none;
  border-color: var(--accent);
}

/* Modal */
.modal {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.85);
  z-index: 200;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 20px;
}

.modal-content {
  background: var(--surface);
  border-radius: 20px;
  padding: 28px;
  width: 100%;
  max-width: 500px;
  max-height: 80vh;
  overflow-y: auto;
  position: relative;
  border: 1px solid var(--border);
}

.modal-close {
  position: absolute;
  top: 16px;
  right: 20px;
  background: none;
  border: none;
  color: var(--text-2);
  font-size: 28px;
  cursor: pointer;
}

#modal-title {
  font-size: 22px;
  margin-bottom: 10px;
  padding-right: 30px;
}

#modal-why {
  color: var(--text-2);
  line-height: 1.6;
  margin: 16px 0;
}

.source {
  color: var(--text-2);
  font-size: 12px;
  opacity: 0.7;
}

/* No harmful found */
.no-harmful {
  text-align: center;
  padding: 40px 20px;
}

.no-harmful svg {
  color: var(--safe);
  margin-bottom: 12px;
}

.no-harmful h3 {
  color: var(--safe);
  margin-bottom: 8px;
}

.no-harmful p {
  color: var(--text-2);
  font-size: 14px;
}

/* Raw OCR text display */
.raw-text-section {
  background: var(--surface);
  border-radius: 14px;
  padding: 16px;
  margin-bottom: 20px;
  border: 1px solid var(--border);
}

.raw-text-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.raw-text-header h3 {
  font-size: 14px;
  color: var(--text-2);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.btn-small {
  padding: 6px 12px;
  font-size: 12px;
}

#raw-text-display {
  background: var(--bg);
  border-radius: 10px;
  padding: 12px;
  font-size: 13px;
  color: var(--text-2);
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 200px;
  overflow-y: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.raw-text-note {
  font-size: 12px;
  color: var(--text-2);
  opacity: 0.7;
  margin-top: 8px;
}

/* OCR Editor */
#ocr-editor-section {
  padding-top: 10px;
}

.edit-hint {
  color: var(--text-2);
  font-size: 14px;
  margin-bottom: 12px;
}

#ocr-editor-text {
  width: 100%;
  min-height: 200px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  color: var(--text);
  font-size: 15px;
  resize: vertical;
  margin-bottom: 16px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

#ocr-editor-text:focus {
  outline: none;
  border-color: var(--accent);
}

/* Verify Section */
#verify-section {
  padding-top: 10px;
}

.verify-header {
  margin-bottom: 16px;
}

.verify-header h2 {
  font-size: 22px;
  margin-bottom: 8px;
}

.verify-header p {
  color: var(--text-2);
  font-size: 14px;
  line-height: 1.5;
}

#verify-text {
  width: 100%;
  min-height: 200px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  color: var(--text);
  font-size: 15px;
  resize: vertical;
  margin-bottom: 16px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

#verify-text:focus {
  outline: none;
  border-color: var(--accent);
}

.verify-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
