/* ============================================
   Document Scanner — Styles
   Mobile-first, clean, professional
   ============================================ */

:root {
  --primary: #1e40af;
  --primary-light: #3b82f6;
  --primary-bg: #eff6ff;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;

  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-hover: #f1f5f9;
  --surface-border: #e2e8f0;

  --text: #0f172a;
  --text-secondary: #475569;
  --text-muted: #94a3b8;

  --danger: #dc2626;
  --danger-bg: #fef2f2;
  --success: #16a34a;
  --success-bg: #f0fdf4;

  --radius: 12px;
  --radius-sm: 8px;
  --radius-lg: 16px;

  --shadow-sm: 0 1px 2px rgba(0,0,0,.05);
  --shadow: 0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,.1), 0 4px 6px -4px rgba(0,0,0,.1);
  --shadow-xl: 0 20px 25px -5px rgba(0,0,0,.1), 0 8px 10px -6px rgba(0,0,0,.1);

  --header-h: 56px;
  --bottom-bar-h: 80px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

html {
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  overflow-x: hidden;
  min-height: 100dvh;
}

/* ---- Header ---- */
.app-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--surface-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  background: rgba(255,255,255,.92);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.header-icon {
  width: 24px;
  height: 24px;
  color: var(--primary);
  flex-shrink: 0;
}

.app-header h1 {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
}

/* ---- Buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: none;
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all .15s ease;
  padding: 10px 20px;
  text-decoration: none;
  -webkit-user-select: none;
  user-select: none;
  white-space: nowrap;
}

.btn svg { width: 18px; height: 18px; flex-shrink: 0; }

.btn-primary {
  background: var(--accent);
  color: #fff;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:active { transform: scale(.97); }

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border: 1.5px solid var(--surface-border);
}
.btn-secondary:hover { background: var(--surface-hover); border-color: var(--text-muted); }

.btn-lg { padding: 14px 24px; font-size: 16px; border-radius: var(--radius-lg); }
.btn-lg svg { width: 20px; height: 20px; }

.btn-block { width: 100%; }

.btn-icon {
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  border-radius: 50%;
  cursor: pointer;
  color: var(--text-secondary);
  transition: background .15s;
}
.btn-icon:hover { background: var(--surface-hover); }
.btn-icon svg { width: 22px; height: 22px; }

.btn-icon-text {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: none;
  background: transparent;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  transition: background .15s;
}
.btn-icon-text svg { width: 18px; height: 18px; }
.btn-icon-text:hover { background: var(--surface-hover); }

.btn-danger { color: var(--danger); }
.btn-danger:hover { background: var(--danger-bg); }

.btn-text {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
}
.btn-text:hover { background: var(--primary-bg); }

.btn-text-primary {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  padding: 8px 16px;
  border-radius: var(--radius-sm);
}
.btn-text-primary:hover { background: var(--primary-bg); }

/* FABs */
.btn-fab {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all .15s;
  background: var(--accent);
  color: #fff;
  box-shadow: var(--shadow-lg);
}
.btn-fab:active { transform: scale(.92); }
.btn-fab svg { width: 22px; height: 22px; }

.btn-fab-secondary {
  background: var(--surface);
  color: var(--text-secondary);
  border: 1.5px solid var(--surface-border);
  box-shadow: var(--shadow);
}

/* ---- Screens ---- */
.screen {
  display: none;
  min-height: 100dvh;
  padding-top: var(--header-h);
}
.screen.active { display: block; }

/* ---- Empty State ---- */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 40px 24px;
  min-height: calc(100dvh - var(--header-h));
}

.empty-icon {
  width: 100px;
  height: 100px;
  margin-bottom: 24px;
  color: var(--text-muted);
}
.empty-icon svg { width: 100%; height: 100%; }

.empty-state h2 {
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 8px;
  color: var(--text);
}

.empty-state > p {
  color: var(--text-secondary);
  font-size: 15px;
  max-width: 320px;
  margin-bottom: 32px;
}

.empty-actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
  max-width: 300px;
}

.empty-hint {
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 16px;
}

/* ---- Pages Container ---- */
.pages-container {
  padding: 8px 12px calc(var(--bottom-bar-h) + var(--safe-bottom) + 16px);
}

.pages-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 4px;
}

.page-count {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
}

/* ---- Pages Grid ---- */
.pages-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 12px;
  padding-bottom: 16px;
}

.page-card {
  position: relative;
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  cursor: grab;
  transition: box-shadow .15s, transform .15s;
  border: 2px solid transparent;
}
.page-card:active { cursor: grabbing; }
.page-card.selected { border-color: var(--accent); }
.page-card.sortable-ghost {
  opacity: .4;
  transform: scale(.95);
}
.page-card.sortable-drag {
  box-shadow: var(--shadow-xl);
  transform: rotate(2deg) scale(1.05);
  z-index: 50;
}

.page-thumb {
  width: 100%;
  aspect-ratio: 210 / 297;
  object-fit: cover;
  display: block;
  background: var(--bg);
}

.page-number {
  position: absolute;
  top: 6px;
  left: 6px;
  background: rgba(0,0,0,.6);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.page-select {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,.8);
  background: rgba(0,0,0,.25);
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  transition: all .15s;
}
.page-select.visible { display: flex; }
.page-select.checked {
  background: var(--accent);
  border-color: var(--accent);
}
.page-select.checked::after {
  content: '';
  width: 10px;
  height: 6px;
  border-left: 2px solid #fff;
  border-bottom: 2px solid #fff;
  transform: rotate(-45deg) translateY(-1px);
}

.page-actions {
  display: flex;
  justify-content: space-around;
  padding: 6px 4px;
  border-top: 1px solid var(--surface-border);
}

.page-action-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--text-secondary);
  transition: all .15s;
}
.page-action-btn:hover { background: var(--surface-hover); color: var(--text); }
.page-action-btn.delete:hover { color: var(--danger); background: var(--danger-bg); }
.page-action-btn svg { width: 18px; height: 18px; }

/* ---- Selection Bar ---- */
.selection-bar {
  position: fixed;
  bottom: calc(var(--bottom-bar-h) + var(--safe-bottom));
  left: 12px;
  right: 12px;
  background: var(--text);
  color: #fff;
  border-radius: var(--radius);
  padding: 10px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  z-index: 90;
  box-shadow: var(--shadow-xl);
  animation: slideUp .2s ease;
}
.selection-bar.hidden { display: none; }
.selection-bar span { font-size: 14px; font-weight: 600; }
.selection-bar .btn-icon-text { color: #fff; }
.selection-bar .btn-icon-text:hover { background: rgba(255,255,255,.15); }

@keyframes slideUp {
  from { transform: translateY(20px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

/* ---- Bottom Bar ---- */
.bottom-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: calc(var(--bottom-bar-h) + var(--safe-bottom));
  padding: 12px 16px calc(12px + var(--safe-bottom));
  background: rgba(255,255,255,.95);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--surface-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  z-index: 80;
}

.bottom-bar-left {
  display: flex;
  gap: 10px;
}

.btn-export {
  padding: 12px 24px;
}

/* ---- Edit Screen ---- */
.edit-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--header-h);
  background: rgba(255,255,255,.95);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--surface-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 8px;
  z-index: 100;
}

.edit-title {
  font-size: 17px;
  font-weight: 700;
}

.edit-canvas-wrap {
  position: fixed;
  top: var(--header-h);
  left: 0;
  right: 0;
  bottom: 72px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f1f5f9;
  overflow: hidden;
}

.edit-canvas-wrap img {
  max-width: 100%;
  max-height: 100%;
  display: block;
}

.edit-tools {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: calc(72px + var(--safe-bottom));
  padding: 8px 16px calc(8px + var(--safe-bottom));
  background: var(--surface);
  border-top: 1px solid var(--surface-border);
  display: flex;
  align-items: center;
  justify-content: space-around;
  z-index: 100;
}

.btn-tool {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  border: none;
  background: transparent;
  cursor: pointer;
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  transition: all .15s;
}
.btn-tool svg { width: 22px; height: 22px; }
.btn-tool span { font-size: 11px; font-weight: 600; }
.btn-tool:hover, .btn-tool.active { color: var(--accent); background: var(--primary-bg); }

/* Cropper overrides */
.cropper-view-box { outline: 2px solid var(--accent); outline-color: var(--accent); }
.cropper-line { background-color: var(--accent); }
.cropper-point { background-color: var(--accent); width: 12px; height: 12px; border-radius: 50%; }
.cropper-modal { background-color: rgba(15,23,42,.5); }

/* ---- Export Screen ---- */
.export-content {
  padding: 16px;
  max-width: 480px;
  margin: 0 auto;
}

.export-preview {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 8px 0 16px;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.export-preview::-webkit-scrollbar { display: none; }

.export-preview-page {
  flex-shrink: 0;
  width: 80px;
  height: 113px;
  object-fit: cover;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  background: var(--surface);
}

.export-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.form-label {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: .04em;
  margin-bottom: -8px;
}

.input-group {
  display: flex;
  align-items: center;
  border: 1.5px solid var(--surface-border);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
  transition: border-color .15s;
}
.input-group:focus-within { border-color: var(--accent); }

.form-input {
  flex: 1;
  border: none;
  padding: 12px 14px;
  font-size: 16px;
  font-family: inherit;
  background: transparent;
  outline: none;
  color: var(--text);
}

.input-suffix {
  padding: 12px 14px 12px 0;
  color: var(--text-muted);
  font-size: 16px;
  font-weight: 500;
}

.form-select {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--surface-border);
  border-radius: var(--radius);
  font-size: 15px;
  font-family: inherit;
  background: var(--surface);
  color: var(--text);
  outline: none;
  -webkit-appearance: none;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg width='12' height='8' viewBox='0 0 12 8' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1.5L6 6.5L11 1.5' stroke='%2394a3b8' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 14px center;
  padding-right: 36px;
}
.form-select:focus { border-color: var(--accent); }

.export-info {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 14px;
  background: var(--success-bg);
  border-radius: var(--radius);
  color: var(--success);
}
.export-info svg { width: 20px; height: 20px; flex-shrink: 0; margin-top: 1px; }
.export-info p { font-size: 13px; line-height: 1.5; }

.export-share {
  padding-top: 4px;
}

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

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,.4);
}

.modal-content {
  position: relative;
  background: var(--surface);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  width: 100%;
  max-width: 480px;
  max-height: 80vh;
  overflow-y: auto;
  animation: modalSlideUp .25s ease;
}

@keyframes modalSlideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 16px 8px;
}
.modal-header h3 { font-size: 18px; font-weight: 700; }

.modal-body {
  padding: 8px 20px 32px;
}
.modal-body p { margin-bottom: 12px; color: var(--text-secondary); font-size: 15px; line-height: 1.6; }
.modal-body ul { padding-left: 20px; }
.modal-body li { color: var(--text-secondary); font-size: 14px; line-height: 1.7; }

/* ---- Loading ---- */
.loading-overlay {
  position: fixed;
  inset: 0;
  z-index: 300;
  background: rgba(255,255,255,.9);
  backdrop-filter: blur(4px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
}
.loading-overlay.hidden { display: none; }

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--surface-border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}

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

.loading-overlay p {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-secondary);
}

/* ---- Toast ---- */
.toast-container {
  position: fixed;
  bottom: calc(var(--bottom-bar-h) + var(--safe-bottom) + 16px);
  left: 16px;
  right: 16px;
  z-index: 250;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  background: var(--text);
  color: #fff;
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  animation: toastIn .3s ease;
  pointer-events: auto;
}
.toast.toast-out {
  animation: toastOut .2s ease forwards;
}

@keyframes toastIn {
  from { transform: translateY(10px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}
@keyframes toastOut {
  to { transform: translateY(10px); opacity: 0; }
}

/* ---- Utilities ---- */
.hidden { display: none !important; }

/* ---- Responsive ---- */
@media (min-width: 480px) {
  .pages-grid {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 16px;
  }
}

@media (min-width: 768px) {
  .pages-grid {
    grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  }

  .empty-actions {
    flex-direction: row;
    max-width: none;
    justify-content: center;
  }

  .modal-content {
    border-radius: var(--radius-lg);
    margin-bottom: 40px;
  }
}

@media (min-width: 1024px) {
  .pages-container {
    max-width: 960px;
    margin: 0 auto;
  }
}
