*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

:root {
  --bg: #111;
  --surface: #1c1c1e;
  --border: #2c2c2e;
  --accent: #4a9eff;
  --accent-glow: rgba(74,158,255,0.35);
  --text: #f0f0f0;
  --muted: #6e6e73;
  --danger: #ff453a;
  --handle: #4a9eff;
}

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

/* ---- SCREENS ---- */
.screen {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
  background: var(--bg);
}
.screen.active { display: flex; }

/* ---- TOPBAR ---- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  height: 56px;
  padding-top: env(safe-area-inset-top, 0px);
  min-height: calc(56px + env(safe-area-inset-top, 0px));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.topbar h1 { font-size: 20px; font-weight: 700; letter-spacing: -0.3px; }
.topbar-title { font-size: 16px; font-weight: 600; }

.btn-text {
  background: none; border: none; color: var(--muted);
  font-size: 15px; cursor: pointer; padding: 6px 8px; border-radius: 6px;
}
.btn-text:disabled { opacity: 0.3; cursor: default; }
.btn-text:not(:disabled):active { background: var(--border); }

.btn-text-accent {
  background: none; border: none; color: var(--accent);
  font-size: 15px; font-weight: 600; cursor: pointer; padding: 6px 8px; border-radius: 6px;
}
.btn-text-accent:active { background: rgba(74,158,255,0.15); }

.btn-icon-sm {
  background: none; border: none; color: var(--text);
  cursor: pointer; padding: 6px; border-radius: 8px; display: flex; align-items: center;
}
.btn-icon-sm svg { width: 22px; height: 22px; }
.btn-icon-sm:active { background: var(--border); }

/* ---- HOME: PAGES GRID ---- */
.pages-grid {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  gap: 12px;
  align-content: start;
  -webkit-overflow-scrolling: touch;
}

.empty-state {
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 24px;
  color: var(--muted);
  text-align: center;
  gap: 10px;
}
.empty-icon svg { width: 72px; height: 72px; }
.empty-state p { font-size: 15px; }
.hint { font-size: 13px; }

.page-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  position: relative;
  aspect-ratio: 3/4;
  cursor: pointer;
  transition: transform 0.15s;
}
.page-card:active { transform: scale(0.97); }
.page-card img {
  width: 100%; height: 100%;
  object-fit: cover; display: block;
}
.page-num {
  position: absolute; bottom: 6px; left: 6px;
  background: rgba(0,0,0,0.65); color: #fff;
  font-size: 11px; padding: 2px 7px; border-radius: 20px;
  backdrop-filter: blur(4px);
}
.btn-del {
  position: absolute; top: 5px; right: 5px;
  background: rgba(0,0,0,0.7); border: none; color: var(--danger);
  width: 26px; height: 26px; border-radius: 50%;
  font-size: 15px; cursor: pointer; display: flex; align-items: center; justify-content: center;
  backdrop-filter: blur(4px);
}

/* ---- BOTTOM BAR ---- */
.bottombar {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  padding-bottom: max(16px, env(safe-area-inset-bottom, 16px));
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.btn-scan {
  width: 68px; height: 68px; border-radius: 50%;
  background: var(--accent); border: none; color: #fff;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  box-shadow: 0 4px 24px var(--accent-glow);
  transition: transform 0.12s, box-shadow 0.12s;
}
.btn-scan:active { transform: scale(0.93); box-shadow: 0 2px 12px var(--accent-glow); }
.btn-scan svg { width: 30px; height: 30px; }

/* ---- CAMERA ---- */
.camera-wrap {
  flex: 1;
  position: relative;
  background: #000;
  overflow: hidden;
}
#video {
  width: 100%; height: 100%;
  object-fit: cover; display: block;
}
.cam-frame-guide {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -52%);
  width: 76%; aspect-ratio: 3/4;
  border: 2px solid rgba(255,255,255,0.55);
  border-radius: 6px;
  box-shadow: 0 0 0 2000px rgba(0,0,0,0.28);
  pointer-events: none;
}
.cam-frame-guide::before,
.cam-frame-guide::after {
  content: '';
  position: absolute;
  width: 22px; height: 22px;
  border-color: #fff; border-style: solid;
}
.cam-frame-guide::before { top: -2px; left: -2px; border-width: 3px 0 0 3px; border-radius: 4px 0 0 0; }
.cam-frame-guide::after  { bottom: -2px; right: -2px; border-width: 0 3px 3px 0; border-radius: 0 0 4px 0; }

.cam-hint {
  position: absolute;
  bottom: 24px; left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.6); color: rgba(255,255,255,0.85);
  font-size: 13px; padding: 6px 14px; border-radius: 20px;
  backdrop-filter: blur(6px);
  pointer-events: none;
  white-space: nowrap;
}

.camera-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 40px;
  padding-bottom: max(20px, env(safe-area-inset-bottom, 20px));
  background: #000;
  flex-shrink: 0;
}

.btn-circle-sm {
  background: rgba(255,255,255,0.12); border: none; color: #fff;
  width: 50px; height: 50px; border-radius: 50%;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: background 0.15s;
}
.btn-circle-sm:active { background: rgba(255,255,255,0.25); }
.btn-circle-sm svg { width: 22px; height: 22px; }

#btn-gallery { cursor: pointer; }

.btn-capture {
  width: 72px; height: 72px; border-radius: 50%;
  background: #fff; border: 5px solid rgba(255,255,255,0.45);
  cursor: pointer; box-shadow: 0 0 0 3px rgba(255,255,255,0.25);
  transition: transform 0.1s, box-shadow 0.1s;
  flex-shrink: 0;
}
.btn-capture:active { transform: scale(0.9); box-shadow: 0 0 0 1px rgba(255,255,255,0.2); }

/* ---- EDITOR ---- */
.canvas-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background: #0a0a0a;
  padding: 8px;
  position: relative;
}

#editor-canvas {
  touch-action: none;
  cursor: crosshair;
  border-radius: 4px;
  /* sized in JS */
}

.filter-row {
  display: flex;
  gap: 8px;
  padding: 8px 16px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  overflow-x: auto;
  flex-shrink: 0;
  -ms-overflow-style: none; scrollbar-width: none;
}
.filter-row::-webkit-scrollbar { display: none; }

.filter-btn {
  flex-shrink: 0;
  display: flex; flex-direction: column; align-items: center; gap: 5px;
  background: var(--bg); border: 1px solid var(--border);
  color: var(--muted); padding: 6px 10px 8px;
  border-radius: 10px; font-size: 12px; cursor: pointer;
  transition: all 0.15s;
}
.filter-btn.active {
  background: rgba(74,158,255,0.15);
  border-color: var(--accent); color: var(--accent);
}
.filter-thumb {
  width: 44px; height: 58px;
  object-fit: cover; border-radius: 4px;
  background: #222; display: block;
}

.cv-bar {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  padding: 7px 16px;
  background: rgba(74,158,255,0.1);
  border-top: 1px solid rgba(74,158,255,0.2);
  font-size: 12px; color: var(--accent);
  flex-shrink: 0;
}
.cv-bar::before {
  content: '';
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--accent);
  animation: pulse 1s ease-in-out infinite;
}
.hidden { display: none !important; }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

/* ---- PROCESSING OVERLAY ---- */
.overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.75);
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 16px; z-index: 100;
  backdrop-filter: blur(4px);
}
.spinner {
  width: 44px; height: 44px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.overlay p { font-size: 15px; color: var(--muted); }
