:root {
  color-scheme: light;
  --bg: #eef2f7;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --line: #dbe2ea;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --success: #059669;
  --success-dark: #047857;
  --danger: #dc2626;
  --shadow: 0 12px 30px rgba(15, 23, 42, .08);
  --radius: 20px;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: linear-gradient(180deg, #0f172a 0, #172554 220px, var(--bg) 220px);
  color: var(--text);
}
button, input, select { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }

.app-shell {
  width: min(100%, 780px);
  margin: 0 auto;
  padding: max(18px, env(safe-area-inset-top)) 14px calc(30px + env(safe-area-inset-bottom));
}

.topbar { color: #fff; padding: 10px 4px 22px; position: relative; }
.eyebrow { margin: 0 0 5px; font-size: 12px; letter-spacing: .14em; font-weight: 800; color: #bfdbfe; }
h1 { margin: 0; font-size: clamp(30px, 9vw, 46px); line-height: 1; letter-spacing: -.035em; }
.subtitle { margin: 10px 0 0; max-width: 560px; color: #cbd5e1; font-size: 14px; line-height: 1.45; }
.privacy-badge {
  display: inline-flex;
  margin-top: 14px;
  padding: 7px 10px;
  border: 1px solid rgba(255,255,255,.2);
  border-radius: 999px;
  background: rgba(255,255,255,.08);
  font-size: 12px;
  font-weight: 700;
}

.card {
  background: var(--card);
  border: 1px solid rgba(219,226,234,.9);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.setup-card { padding: 16px; }
.field-label { display: block; margin-bottom: 7px; font-size: 13px; font-weight: 800; }
.text-input, .select-input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: #fff;
  color: var(--text);
  min-height: 48px;
  padding: 0 13px;
  outline: none;
}
.text-input:focus, .select-input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,.12); }

.button-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 12px; }
.btn {
  border: 0;
  border-radius: 14px;
  min-height: 52px;
  padding: 12px 14px;
  font-weight: 850;
  cursor: pointer;
  transition: transform .08s ease, opacity .15s ease, background .15s ease;
}
.btn:active { transform: scale(.985); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { color: #fff; background: var(--primary); }
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary { color: #0f172a; background: #e2e8f0; }
.btn-success { color: #fff; background: var(--success); }
.btn-success:hover { background: var(--success-dark); }
.btn-wide { width: 100%; margin-top: 16px; }
.btn-icon { margin-right: 5px; }
.hidden-input { display: none; }

.summary-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 16px 4px 10px;
}
.summary-row strong { display: block; font-size: 15px; }
.summary-row span { display: block; margin-top: 2px; color: var(--muted); font-size: 12px; }
.link-btn { border: 0; background: transparent; font-weight: 800; padding: 8px; cursor: pointer; }
.link-btn:disabled { opacity: .35; cursor: default; }
.danger { color: var(--danger); }

.empty-state {
  border: 2px dashed #cbd5e1;
  border-radius: var(--radius);
  padding: 34px 20px;
  text-align: center;
  background: rgba(255,255,255,.58);
}
.empty-icon { font-size: 42px; }
.empty-state h2 { margin: 8px 0 7px; font-size: 19px; }
.empty-state p { margin: 0 auto; max-width: 430px; color: var(--muted); line-height: 1.45; font-size: 14px; }

.pages-list { display: grid; gap: 12px; }
.page-card {
  display: grid;
  grid-template-columns: 104px 1fr;
  gap: 12px;
  padding: 10px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 17px;
  box-shadow: 0 6px 18px rgba(15,23,42,.06);
}
.page-thumb {
  width: 104px;
  height: 138px;
  object-fit: cover;
  border-radius: 11px;
  background: #e2e8f0;
  border: 1px solid #e2e8f0;
}
.page-meta { min-width: 0; display: flex; flex-direction: column; justify-content: space-between; }
.page-meta h3 { margin: 2px 0 4px; font-size: 16px; }
.page-meta p { margin: 0; color: var(--muted); font-size: 12px; }
.page-actions { display: grid; grid-template-columns: repeat(4, 1fr); gap: 7px; margin-top: 10px; }
.icon-btn {
  min-height: 40px;
  border: 1px solid var(--line);
  background: #f8fafc;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 800;
}
.icon-btn:disabled { opacity: .35; }
.icon-btn.delete { color: var(--danger); background: #fef2f2; border-color: #fecaca; }

.export-card { margin-top: 16px; padding: 16px; }
.export-head h2 { margin: 0; font-size: 19px; }
.export-head p { margin: 4px 0 0; color: var(--muted); font-size: 13px; }
.export-options { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 14px; }
.export-options label > span { display: block; margin-bottom: 6px; color: #334155; font-size: 12px; font-weight: 800; }
.small-note { margin: 10px 2px 0; color: var(--muted); font-size: 11px; line-height: 1.45; }

.toast {
  position: fixed;
  left: 50%;
  bottom: max(20px, calc(14px + env(safe-area-inset-bottom)));
  transform: translate(-50%, 20px);
  opacity: 0;
  pointer-events: none;
  z-index: 20;
  max-width: calc(100vw - 28px);
  padding: 11px 15px;
  border-radius: 999px;
  background: #0f172a;
  color: #fff;
  font-size: 13px;
  font-weight: 750;
  transition: all .2s ease;
  box-shadow: var(--shadow);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.busy {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(15,23,42,.52);
  backdrop-filter: blur(4px);
}
.busy[hidden] { display: none; }
.busy-box { display: flex; align-items: center; gap: 12px; padding: 15px 17px; border-radius: 15px; background: #fff; box-shadow: var(--shadow); }
.spinner { width: 22px; height: 22px; border: 3px solid #dbeafe; border-top-color: var(--primary); border-radius: 50%; animation: spin .75s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 430px) {
  .button-grid, .export-options { grid-template-columns: 1fr; }
  .page-card { grid-template-columns: 88px 1fr; }
  .page-thumb { width: 88px; height: 118px; }
  .page-actions { grid-template-columns: repeat(2, 1fr); }
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #020617;
    --card: #0f172a;
    --text: #f8fafc;
    --muted: #94a3b8;
    --line: #243244;
    --shadow: 0 12px 30px rgba(0,0,0,.25);
  }
  body { background: linear-gradient(180deg, #020617 0, #172554 220px, var(--bg) 220px); }
  .text-input, .select-input { background: #111827; color: #f8fafc; }
  .btn-secondary { color: #f8fafc; background: #1e293b; }
  .empty-state { background: rgba(15,23,42,.65); border-color: #334155; }
  .page-card { background: #0f172a; }
  .icon-btn { background: #111827; color: #e2e8f0; }
  .icon-btn.delete { background: #2a1114; border-color: #7f1d1d; }
  .export-options label > span { color: #cbd5e1; }
  .busy-box { background: #0f172a; }
}

/* Scanner intelligente */
.scan-options {
  display: grid;
  gap: 9px;
  margin-top: 14px;
  padding-top: 13px;
  border-top: 1px solid var(--line);
}
.toggle-row {
  display: flex;
  align-items: flex-start;
  gap: 11px;
  padding: 10px 11px;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: rgba(148, 163, 184, .07);
  cursor: pointer;
}
.toggle-row input {
  width: 21px;
  height: 21px;
  margin: 1px 0 0;
  accent-color: var(--primary);
  flex: 0 0 auto;
}
.toggle-row span { display: block; min-width: 0; }
.toggle-row strong { display: block; font-size: 13px; }
.toggle-row small { display: block; margin-top: 2px; color: var(--muted); font-size: 11px; line-height: 1.35; }

.scan-badge {
  display: inline-flex;
  align-items: center;
  margin-top: 7px;
  padding: 4px 7px;
  border-radius: 999px;
  font-size: 10px;
  font-weight: 850;
  line-height: 1.2;
}
.scan-badge.ok { background: #dcfce7; color: #166534; }
.scan-badge.warn { background: #fef3c7; color: #92400e; }
.page-actions { grid-template-columns: repeat(3, 1fr); }

body.modal-open { overflow: hidden; }
.crop-editor {
  position: fixed;
  inset: 0;
  z-index: 40;
  overflow: auto;
  padding: max(12px, env(safe-area-inset-top)) 12px max(16px, env(safe-area-inset-bottom));
  background: rgba(2, 6, 23, .9);
  backdrop-filter: blur(8px);
}
.crop-editor[hidden] { display: none; }
.crop-panel {
  width: min(100%, 760px);
  margin: 0 auto;
  padding: 14px;
  border-radius: 20px;
  background: #0f172a;
  color: #f8fafc;
  box-shadow: 0 24px 70px rgba(0,0,0,.45);
}
.crop-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 14px; }
.crop-kicker { margin: 0 0 3px; color: #93c5fd; font-size: 10px; font-weight: 900; letter-spacing: .12em; }
.crop-head h2 { margin: 0; font-size: 20px; }
.crop-close {
  width: 42px;
  height: 42px;
  border: 1px solid #334155;
  border-radius: 50%;
  background: #1e293b;
  color: #fff;
  font-weight: 900;
  cursor: pointer;
}
.crop-help { margin: 8px 0 11px; color: #cbd5e1; font-size: 12px; line-height: 1.4; }
.crop-canvas-wrap {
  display: grid;
  place-items: center;
  min-height: 260px;
  max-height: 67vh;
  overflow: hidden;
  border-radius: 14px;
  background: #020617;
  border: 1px solid #334155;
}
#cropCanvas {
  display: block;
  max-width: 100%;
  max-height: 67vh;
  width: auto;
  height: auto;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.crop-tools { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; margin-top: 10px; }
.crop-panel .btn-secondary { background: #1e293b; color: #f8fafc; border: 1px solid #334155; }

@media (max-width: 430px) {
  .page-actions { grid-template-columns: repeat(2, 1fr); }
  .crop-panel { padding: 11px; border-radius: 16px; }
  .crop-tools { grid-template-columns: 1fr; }
  .crop-canvas-wrap, #cropCanvas { max-height: 62vh; }
}

@media (prefers-color-scheme: dark) {
  .scan-badge.ok { background: #12351f; color: #86efac; }
  .scan-badge.warn { background: #3b2b0d; color: #fde68a; }
}
