/* Scan to PDF – Mobile First, Business-Style */

:root {
  --bg: #0d1117;
  --bg-card: #161b22;
  --bg-card-hover: #21262d;
  --border: rgba(255, 255, 255, 0.08);
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #238636;
  --accent-hover: #2ea043;
  --accent-dim: rgba(35, 134, 54, 0.2);
  --danger: #f85149;
  --radius: 12px;
  --radius-lg: 16px;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.3);
  /* Safe Area für alle Smartphones (Notch, Insel, Gesten) */
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

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

html {
  -webkit-tap-highlight-color: transparent;
  min-height: 100%;
  min-height: -webkit-fill-available;
}

body {
  font-family: 'DM Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  min-height: -webkit-fill-available;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

.app {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
}

/* Header */
.back-link {
  display: inline-block;
  margin-bottom: 0.75rem;
  font-size: 0.9rem;
  color: var(--accent);
  text-decoration: none;
}
.back-link:hover {
  text-decoration: underline;
}

.header {
  padding: 1.25rem 1.25rem 0.75rem;
  text-align: center;
}

.logo {
  font-size: clamp(1.5rem, 5vw, 1.75rem);
  font-weight: 700;
  letter-spacing: -0.02em;
}

.tagline {
  font-size: 0.85rem;
  color: var(--muted);
  margin-top: 0.25rem;
}

/* Main */
.main {
  flex: 1;
  max-width: 560px;
  margin: 0 auto;
  width: 100%;
  padding: 0 1.25rem 1.5rem;
}

/* Tiles – große Kachel-Buttons */
.tiles {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.tile {
  display: flex;
  align-items: center;
  gap: 1rem;
  width: 100%;
  padding: 1.25rem 1.5rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  color: var(--text);
  font-family: inherit;
  font-size: 1rem;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
  transition: background 0.2s, border-color 0.2s, transform 0.15s;
  -webkit-tap-highlight-color: transparent;
}

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

.tile:hover {
  background: var(--bg-card-hover);
  border-color: var(--accent);
}

.tile:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.tile-icon {
  font-size: 2rem;
  line-height: 1;
  flex-shrink: 0;
}

.tile-label {
  flex: 1;
}

.tile-desc {
  font-size: 0.8rem;
  font-weight: 400;
  color: var(--muted);
  display: block;
  margin-top: 0.2rem;
}

.tile-scan { border-left: 4px solid #58a6ff; }
.tile-pdf  { border-left: 4px solid var(--accent); }
.tile-ocr  { border-left: 4px solid #a371f7; }

/* Upload Zone */
.upload-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  text-align: center;
  color: var(--muted);
  font-size: 0.9rem;
  transition: border-color 0.2s, background 0.2s;
}

.upload-zone.drag-over {
  border-color: var(--accent);
  background: var(--accent-dim);
  color: var(--text);
}

.upload-zone:hover {
  border-color: var(--muted);
}

/* Pages Section */
.pages-section {
  margin-top: 1.5rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
}

.section-title {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 0.25rem;
}

.section-hint {
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 0.75rem;
  line-height: 1.4;
}

.pages-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.page-thumb {
  position: relative;
  aspect-ratio: 3/4;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--bg-card);
}

.page-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.page-thumb .remove {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.7);
  color: #fff;
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.page-actions {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
}

/* Quality */
.quality-section {
  margin-top: 1.5rem;
}

.quality-options {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.radio-option {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.6rem 1rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--muted);
  font-size: 0.9rem;
  cursor: pointer;
  transition: all 0.2s;
}

.radio-option input {
  accent-color: var(--accent);
}

.radio-option:has(input:checked) {
  border-color: var(--accent);
  color: var(--accent);
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.75rem 1.25rem;
  border: none;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}

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

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

.btn-secondary {
  background: var(--bg-card);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-secondary:hover {
  background: var(--bg-card-hover);
}

.btn-capture {
  background: var(--accent);
  color: #fff;
  padding: 1rem 1.5rem;
  font-weight: 600;
}

.btn-capture-icon {
  width: 24px;
  height: 24px;
  border: 3px solid #fff;
  border-radius: 50%;
  display: inline-block;
}

.btn-export {
  width: 100%;
  padding: 1rem 1.25rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: inherit;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  transition: all 0.2s;
}

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

/* Footer */
.footer {
  padding: 1rem 1.25rem;
  text-align: center;
}
.footer-links {
  margin-top: 0.5rem;
  font-size: 0.9rem;
}
.footer-links a {
  color: var(--accent);
  text-decoration: none;
}
.footer-links a:hover {
  text-decoration: underline;
  font-size: 0.8rem;
  color: var(--muted);
  border-top: 1px solid var(--border);
}

.footer .pwa-hint {
  margin-top: 0.5rem;
}

.footer .pwa-hint[hidden] {
  display: none !important;
}

.pwa-install-btn {
  padding: 0.4rem 0.75rem;
  font-size: 0.8rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--accent);
  cursor: pointer;
  font-family: inherit;
  -webkit-tap-highlight-color: transparent;
}

.pwa-install-btn:hover {
  background: var(--bg-card-hover);
  border-color: var(--accent);
}

.pwa-ios-hint {
  font-size: 0.75rem;
  color: var(--muted);
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 0;
  pointer-events: none;
  visibility: hidden;
}

.modal[hidden] {
  display: none;
}

.modal.is-open {
  pointer-events: auto;
  visibility: visible;
}

.modal.is-open .modal-content {
  transform: translateY(0);
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  z-index: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(4px);
  pointer-events: auto;
}

.modal-content {
  position: relative;
  z-index: 2;
  width: 100%;
  max-height: 95vh;
  background: var(--bg);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  transform: translateY(100%);
  transition: transform 0.3s ease;
  overflow: hidden;
  pointer-events: auto;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.modal-header h2 {
  font-size: 1.1rem;
  font-weight: 600;
}

.camera-header-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.btn-light {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-card);
  color: var(--muted);
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.2s, border-color 0.2s, color 0.2s;
  -webkit-tap-highlight-color: transparent;
}

.btn-light:hover {
  background: var(--bg-card-hover);
  color: var(--text);
  border-color: var(--muted);
}

.btn-light.on {
  background: rgba(255, 193, 7, 0.2);
  border-color: rgba(255, 193, 7, 0.6);
  color: #ffc107;
}

.btn-light-icon {
  font-size: 1.1rem;
  line-height: 1;
}

.btn-close {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-close:hover {
  background: var(--bg-card);
  color: var(--text);
}

/* Kamera-Modal: Vollbild ohne transform (iOS zeigt Video nur so) */
#camera-modal.camera-fullscreen.is-open {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  padding: 0;
  pointer-events: auto;
  visibility: visible;
}
.camera-fullscreen .camera-header-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  padding-top: calc(0.75rem + var(--safe-top));
  padding-left: calc(1rem + var(--safe-left));
  padding-right: calc(1rem + var(--safe-right));
  background: rgba(13, 17, 23, 0.95);
  border-bottom: 1px solid var(--border);
}
.camera-fullscreen .camera-header-bar h2 {
  font-size: 1.1rem;
  font-weight: 600;
}

.camera-auto-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.85rem;
  color: var(--muted);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.camera-auto-toggle input {
  accent-color: var(--accent);
  width: 18px;
  height: 18px;
}
.camera-auto-toggle input:checked + .camera-auto-label {
  color: var(--accent);
}
.camera-ai-status {
  position: absolute;
  top: 0.75rem;
  left: 50%;
  transform: translateX(-50%);
  z-index: 4;
  padding: 0.4rem 0.75rem;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.7);
  color: #fff;
  font-size: 0.8rem;
  font-weight: 500;
  pointer-events: none;
  transition: opacity 0.2s;
}
.camera-ai-status:empty {
  display: none;
}
.camera-ai-status.status-ok {
  background: rgba(35, 134, 54, 0.9);
}
.camera-ai-status.status-countdown {
  background: rgba(210, 153, 34, 0.9);
}
/* Video-Bereich: transparent, damit Video auf body sichtbar ist */
#camera-modal .camera-wrap {
  position: fixed;
  top: calc(52px + var(--safe-top));
  left: var(--safe-left);
  right: var(--safe-right);
  bottom: calc(68px + var(--safe-bottom));
  z-index: 1;
  min-height: 240px;
  background: transparent;
  overflow: visible;
  pointer-events: none;
}
#camera-modal .camera-wrap .camera-loading,
#camera-modal .camera-wrap .camera-overlay {
  pointer-events: auto;
}
#camera-modal .camera-wrap .camera-overlay {
  pointer-events: none;
}
#camera-modal .camera-wrap .camera-overlay * {
  pointer-events: none;
}

/* Video auf body: iOS rendert nur so zuverlässig (kein transform/overflow in Vorfahren) */
#camera-video.camera-video-on-body {
  position: fixed !important;
  top: calc(52px + var(--safe-top)) !important;
  left: var(--safe-left) !important;
  right: var(--safe-right) !important;
  bottom: calc(68px + var(--safe-bottom)) !important;
  width: 100% !important;
  height: 100% !important;
  z-index: 999 !important;
  object-fit: cover !important;
  background: #000 !important;
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
}

/* Live-Filter: Canvas-Overlay (über Video, gleiche Position) */
#camera-canvas.camera-canvas-on-body {
  position: fixed !important;
  top: calc(52px + var(--safe-top)) !important;
  left: var(--safe-left) !important;
  right: var(--safe-right) !important;
  bottom: calc(68px + var(--safe-bottom)) !important;
  width: 100% !important;
  height: 100% !important;
  z-index: 1000 !important;
  object-fit: cover !important;
  display: block !important;
  pointer-events: none;
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
}

#camera-modal #camera-video:not(.camera-video-on-body),
#camera-modal #camera-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
  z-index: 1;
  opacity: 1;
  visibility: visible;
}
#camera-modal #camera-video {
  background: #000;
}
#camera-modal .camera-wrap.is-stream-ready .camera-loading {
  display: none !important;
}

/* Ladehinweis, bis der Kamerastream läuft */
.camera-loading {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000;
  color: rgba(255, 255, 255, 0.8);
  font-size: 1rem;
  pointer-events: none;
}
.camera-loading[hidden] {
  display: none;
}

.camera-overlay {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.camera-overlay *,
.scan-frame {
  pointer-events: none;
}

.scan-frame {
  width: 72%;
  max-width: min(72vw, 320px);
  max-height: 58vh;
  aspect-ratio: 3/4;
  min-width: 140px;
  min-height: 140px;
  border: 4px solid #fff;
  border-radius: 8px;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.5), 0 0 0 2px rgba(0,0,0,0.3);
  box-sizing: border-box;
  flex-shrink: 0;
}
/* Rahmen an erkanntes Dokument angepasst */
.scan-frame.scan-frame-detected {
  position: absolute;
  width: auto;
  max-width: none;
  max-height: none;
  min-width: 0;
  min-height: 0;
  aspect-ratio: auto;
}

/* Kamera-Toolbar: Rahmen Auto/Manuell + Live Farbe/Grau/SW */
.camera-toolbar {
  display: none;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10001;
  gap: 1rem;
  flex-wrap: wrap;
  justify-content: center;
  padding: 0.6rem calc(1rem + var(--safe-right)) calc(0.6rem + 68px + var(--safe-bottom)) calc(1rem + var(--safe-left));
  padding-bottom: calc(0.6rem + 68px + var(--safe-bottom));
  background: linear-gradient(to top, #0d1117 60%, transparent);
  pointer-events: none;
}
.camera-toolbar .camera-toolbar-group {
  display: flex;
  gap: 0.25rem;
  pointer-events: auto;
}
.camera-tool-btn {
  padding: 0.4rem 0.65rem;
  font-size: 0.8rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: rgba(22, 27, 34, 0.9);
  color: var(--muted);
  font-family: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.2s, color 0.2s, border-color 0.2s;
}
.camera-tool-btn:hover {
  background: var(--bg-card-hover);
  color: var(--text);
}
.camera-tool-btn.is-active,
.camera-tool-btn[aria-pressed="true"] {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: var(--accent);
}
#camera-modal.is-open .camera-toolbar {
  display: flex;
}

/* Live-Vorschau: Filter auf Video */
#camera-video.preview-grayscale {
  filter: grayscale(100%);
}
#camera-video.preview-bw {
  filter: grayscale(100%) contrast(180%) brightness(0.95);
}

/* Kamera-Buttons: außerhalb von modal-content, Safe Area für alle Smartphones */
.camera-actions {
  display: none;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10002;
  gap: 0.75rem;
  padding: 1rem calc(1.25rem + var(--safe-right)) calc(1rem + var(--safe-bottom)) calc(1.25rem + var(--safe-left));
  background: #0d1117;
  pointer-events: auto;
  -webkit-user-select: none;
  user-select: none;
  box-shadow: 0 -4px 24px rgba(0,0,0,0.3);
}

#camera-modal.is-open .camera-actions {
  display: flex;
}

.camera-actions .btn,
.camera-actions button {
  pointer-events: auto !important;
  cursor: pointer !important;
  -webkit-appearance: none;
  appearance: none;
  opacity: 1 !important;
  touch-action: manipulation;
}

#btn-capture {
  flex: 1;
  min-height: 52px;
  -webkit-tap-highlight-color: transparent;
  background: #238636 !important;
  color: #ffffff !important;
  border: none !important;
  font-weight: 600;
  font-size: 1rem;
}

#btn-capture:hover:not(:disabled) {
  background: #2ea043 !important;
}

#btn-capture:disabled {
  opacity: 0.85;
  cursor: wait;
}

#btn-camera-done {
  min-height: 52px;
  min-width: 88px;
  -webkit-tap-highlight-color: transparent;
  background: #21262d !important;
  color: #ffffff !important;
  border: 2px solid rgba(255,255,255,0.2) !important;
  font-size: 1rem;
  font-weight: 500;
}

#btn-camera-done:hover,
#btn-camera-done:active {
  background: #30363d !important;
  border-color: rgba(255,255,255,0.35) !important;
}

/* Crop Modal */
.modal-crop .crop-wrap {
  padding: 1rem;
  overflow: auto;
  flex: 1;
  min-height: 200px;
  display: flex;
  align-items: center;
  justify-content: center;
}

#crop-canvas {
  max-width: 100%;
  height: auto;
  display: block;
}

.crop-hint {
  padding: 0 1.25rem 0.5rem;
  font-size: 0.85rem;
  color: var(--muted);
  line-height: 1.4;
}
.crop-manual-hint {
  padding: 0.25rem 1.25rem 0.5rem;
}
.modal-crop.crop-mode-manual #crop-canvas {
  cursor: crosshair;
}
.crop-source-toggle {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  padding: 0 1.25rem 0.75rem;
}
.crop-source-toggle .camera-tool-btn {
  font-size: 0.8rem;
}

.crop-actions {
  display: flex;
  gap: 0.75rem;
  padding: 1rem 1.25rem;
  padding-bottom: calc(1rem + var(--safe-bottom));
  flex-shrink: 0;
}

.crop-actions .btn-primary {
  flex: 1;
}

/* Export Modal */
.modal-export .export-intro {
  padding: 0 1.25rem 0.5rem;
  font-size: 0.9rem;
  color: var(--text);
  line-height: 1.5;
}

.modal-export .export-options {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 1rem 1.25rem;
}

.export-hint {
  padding: 0 1.25rem 1.25rem;
  font-size: 0.85rem;
  color: var(--muted);
}

/* Overlay (Loading) */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 2000;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.25rem;
}

.overlay[hidden] {
  display: none;
}

.overlay-content {
  text-align: center;
  color: #fff;
}

.spinner {
  width: 48px;
  height: 48px;
  border: 4px solid rgba(255, 255, 255, 0.2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto 1rem;
}

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

.status-text {
  font-size: 0.95rem;
}

/* Kleine Smartphones (z. B. 320px Breite) */
@media (max-width: 360px) {
  .main { padding: 0 0.75rem 1rem; }
  .header { padding: 0.75rem 0.75rem 0.5rem; }
  .logo { font-size: 1.35rem; }
  .tile { padding: 1rem 1rem; gap: 0.75rem; }
  .tile-icon { font-size: 1.5rem; }
  .scan-frame { min-width: 100px; min-height: 100px; }
}

/* Desktop: Modal zentriert */
@media (min-width: 640px) {
  .modal {
    align-items: center;
    padding: 1rem;
  }

  .modal-content {
    max-width: 480px;
    max-height: 90vh;
    border-radius: var(--radius-lg);
    transform: scale(0.95);
  }

  .modal.is-open .modal-content {
    transform: scale(1);
  }

  .tiles {
    flex-direction: row;
    flex-wrap: wrap;
  }

  .tile {
    flex: 1;
    min-width: 160px;
    flex-direction: column;
    text-align: center;
  }

  .tile-desc {
    margin-top: 0.35rem;
  }
}
