/* ============================================================
   RESET & ROOT VARIABLES
   ============================================================ */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg:           #111111;
  --surface:      #1c1c1e;
  --surface2:     #2c2c2e;
  --accent:       #00cfff;
  --accent-dark:  #0099cc;
  --danger:       #ff453a;
  --text:         #f0f0f0;
  --text-muted:   #8e8e93;
  --btn-h:        52px;
  --radius:       14px;
  --safe-bottom:  env(safe-area-inset-bottom, 0px);
}

html, body {
  height: 100%;
  width: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

/* ============================================================
   LOADING OVERLAY
   Covers the whole screen while OpenCV.js is downloading (~8MB).
   ============================================================ */
#loading-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  z-index: 999;
  transition: opacity 0.3s ease;
}

#loading-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.spinner {
  width: 52px;
  height: 52px;
  border: 4px solid #333;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
}

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

#loading-overlay p {
  color: var(--text-muted);
  font-size: 15px;
  letter-spacing: 0.2px;
}

/* ============================================================
   SCREENS
   Each screen fills the full viewport. Only one is active.
   ============================================================ */
.screen {
  display: none;
  flex-direction: column;
  position: fixed;
  inset: 0;
  height: 100dvh; /* dvh = dynamic viewport height, accounts for mobile browser bars */
  width: 100%;
}

.screen.active {
  display: flex;
}

/* ============================================================
   SCREEN 1: CAMERA
   ============================================================ */
#screen-camera {
  background: #000;
}

.camera-viewport {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
}

#video {
  width: 100%;
  height: 100%;
  object-fit: contain; /* show full camera frame, no cropping */
  display: block;
}

/* Overlay canvas sits exactly on top of the video element */
#overlay-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none; /* clicks pass through to the video */
}

/* Hint text shown when no document is auto-detected */
.detection-hint {
  position: absolute;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 13px;
  padding: 6px 14px;
  border-radius: 20px;
  white-space: nowrap;
  pointer-events: none;
  transition: opacity 0.3s;
}

.detection-hint.detected {
  color: var(--accent);
}

.camera-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 32px;
  padding-bottom: calc(24px + var(--safe-bottom));
  background: rgba(0, 0, 0, 0.85);
}

/* Large circular shutter button */
.capture-btn {
  width: 76px;
  height: 76px;
  border-radius: 50%;
  border: 4px solid rgba(255, 255, 255, 0.8);
  background: transparent;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.1s ease;
  -webkit-tap-highlight-color: transparent;
}

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

.capture-btn-inner {
  display: block;
  width: 58px;
  height: 58px;
  border-radius: 50%;
  background: #fff;
  transition: background 0.15s;
}

.capture-btn:active .capture-btn-inner {
  background: var(--accent);
}

/* ============================================================
   SCREEN 2: CROP
   ============================================================ */
#screen-crop {
  background: #000;
}

.screen-header {
  padding: 14px 20px 10px;
  background: rgba(0, 0, 0, 0.85);
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex-shrink: 0;
}

.screen-title {
  font-size: 17px;
  font-weight: 600;
  color: var(--text);
}

.screen-hint {
  font-size: 13px;
  color: var(--text-muted);
}

.crop-viewport {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  padding: 8px;
  background: #000;
}

#crop-canvas {
  display: block;
  max-width: 100%;
  max-height: 100%;
  /* actual size set dynamically by perspective.js */
}

.crop-controls {
  display: flex;
  gap: 12px;
  padding: 16px;
  padding-bottom: calc(16px + var(--safe-bottom));
  background: rgba(0, 0, 0, 0.85);
  flex-shrink: 0;
}

/* ============================================================
   SCREEN 3: ADJUST
   ============================================================ */
#screen-adjust {
  background: var(--bg);
}

.adjust-preview {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background: #000;
  padding: 8px;
  min-height: 0; /* allow flex child to shrink */
}

#result-canvas {
  display: block;
  max-width: 100%;
  max-height: 100%;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.6);
}

.adjust-panel {
  background: var(--surface);
  padding: 16px;
  padding-bottom: calc(16px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
  flex-shrink: 0;
}

/* Preset buttons grid */
.presets {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.preset-btn {
  padding: 9px 4px;
  border: 1.5px solid #3a3a3c;
  border-radius: 10px;
  background: var(--surface2);
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s, background 0.15s;
}

.preset-btn:active,
.preset-btn.active {
  border-color: var(--accent);
  background: rgba(0, 207, 255, 0.12);
  color: var(--accent);
}

/* Slider rows */
.slider-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
  max-height: 200px;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none; /* Firefox */
}

.slider-group::-webkit-scrollbar {
  display: none; /* Chrome / Safari */
}

.slider-section-label {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-muted);
  padding-top: 6px;
}

.slider-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.slider-label {
  font-size: 13px;
  color: var(--text-muted);
  width: 75px;
  flex-shrink: 0;
}

input[type="range"] {
  flex: 1;
  height: 4px;
  accent-color: var(--accent);
  cursor: pointer;
  border-radius: 2px;
}

.slider-value {
  font-size: 12px;
  color: var(--text-muted);
  width: 28px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* Save / action buttons */
.save-buttons {
  display: grid;
  grid-template-columns: auto 1fr 1fr;
  gap: 10px;
}

/* ============================================================
   SHARED BUTTON STYLES
   ============================================================ */
.btn {
  height: var(--btn-h);
  border: none;
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.12s, transform 0.08s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  letter-spacing: 0.1px;
  -webkit-tap-highlight-color: transparent;
}

.btn:active {
  opacity: 0.75;
  transform: scale(0.97);
}

.btn-primary {
  background: var(--accent);
  color: #000;
}

.btn-secondary {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid #3a3a3c;
}

.btn-accent {
  background: #1a3a4a;
  color: var(--accent);
  border: 1px solid var(--accent-dark);
}

.scanner-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
}

.btn-scanner {
  background: linear-gradient(135deg, #1a3a4a, #0d2233);
  color: var(--accent);
  border: 1.5px solid var(--accent);
  font-size: 15px;
}

.btn-scanner.active {
  background: linear-gradient(135deg, var(--accent), var(--accent-dark));
  color: #000;
  border-color: transparent;
  box-shadow: 0 0 14px rgba(0, 207, 255, 0.45);
}

.btn-reset {
  background: var(--surface2);
  color: var(--text-muted);
  border: 1px solid #3a3a3c;
  padding: 0 16px;
  width: auto;
}

/* The "Neu" button is compact */
#btn-retake-from-adjust {
  width: var(--btn-h);
  padding: 0;
  flex-shrink: 0;
}
