:root {
  color-scheme: light;
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-soft: #f8fafc;
  --ink: #172033;
  --muted: #667085;
  --line: #dfe5ee;
  --blue: #2563eb;
  --blue-dark: #1d4ed8;
  --blue-soft: #eaf1ff;
  --green: #15803d;
  --green-soft: #eaf7ef;
  --red: #c2413a;
  --red-soft: #fff0ee;
  --shadow: 0 16px 50px rgba(23, 32, 51, .08);
  --radius: 20px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-synthesis: none;
}

* { box-sizing: border-box; }
html { min-height: 100%; background: var(--bg); }
body { margin: 0; min-height: 100%; color: var(--ink); background: var(--bg); -webkit-font-smoothing: antialiased; }
button, input { font: inherit; }
button { color: inherit; }
button:focus-visible, input:focus-visible, [tabindex]:focus-visible { outline: 3px solid rgba(37, 99, 235, .25); outline-offset: 2px; }
.icon { width: 20px; height: 20px; flex: 0 0 auto; }

#header {
  height: calc(64px + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) max(20px, env(safe-area-inset-right)) 0 max(20px, env(safe-area-inset-left));
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid rgba(223, 229, 238, .85);
  background: rgba(245, 247, 251, .9);
  backdrop-filter: blur(18px);
  position: sticky;
  top: 0;
  z-index: 30;
}

.brand { display: inline-flex; align-items: center; gap: 10px; color: var(--ink); text-decoration: none; font-size: 19px; font-weight: 750; letter-spacing: -.02em; }
.brand-mark { width: 27px; height: 32px; display: inline-block; background: var(--blue); border-radius: 5px; position: relative; box-shadow: inset 0 0 0 1px rgba(255,255,255,.2); }
.brand-mark::before { content: ""; position: absolute; inset: 7px 6px; border-top: 2px solid white; border-bottom: 2px solid white; }
.brand-mark::after { content: ""; position: absolute; left: 6px; right: 9px; top: 15px; border-top: 2px solid white; }
.icon-button { width: 42px; height: 42px; display: grid; place-items: center; padding: 0; border: 1px solid var(--line); border-radius: 13px; background: var(--surface); cursor: pointer; }
.icon-button:disabled { opacity: .35; cursor: default; }

#app { width: min(720px, 100%); margin: 0 auto; padding: 38px 20px calc(80px + env(safe-area-inset-bottom)); }
.boot { min-height: 55vh; display: grid; place-items: center; color: var(--muted); }
h1, h2, p { margin-top: 0; }
h1 { margin-bottom: 10px; font-size: clamp(30px, 8vw, 42px); line-height: 1.06; letter-spacing: -.045em; }
h2 { margin-bottom: 0; font-size: 24px; letter-spacing: -.03em; }
.section-kicker { color: var(--blue); font-size: 12px; font-weight: 800; letter-spacing: .1em; text-transform: uppercase; margin-bottom: 8px; }
.lead { color: var(--muted); font-size: 16px; line-height: 1.55; margin-bottom: 26px; max-width: 560px; }

.scanner { background: var(--surface); border: 1px solid rgba(223,229,238,.8); border-radius: 26px; padding: 28px; box-shadow: var(--shadow); }
.camera-button { width: 100%; min-height: 104px; display: flex; align-items: center; justify-content: center; gap: 18px; border: 0; border-radius: 20px; background: var(--blue); color: white; cursor: pointer; box-shadow: 0 10px 28px rgba(37, 99, 235, .25); transition: transform .15s ease, background .15s ease; }
.camera-button:active { transform: scale(.985); background: var(--blue-dark); }
.camera-button strong, .camera-button small { display: block; text-align: left; }
.camera-button strong { font-size: 19px; line-height: 1.35; }
.camera-button small { opacity: .8; margin-top: 2px; font-size: 13px; }
.camera-ring { width: 58px; height: 58px; border-radius: 50%; display: grid; place-items: center; background: rgba(255,255,255,.16); border: 1px solid rgba(255,255,255,.26); }
.camera-ring .icon { width: 29px; height: 29px; }
.gallery-button { width: 100%; margin-top: 12px; padding: 14px 18px; border: 0; background: transparent; color: var(--blue); display: flex; align-items: center; justify-content: center; gap: 9px; font-weight: 700; cursor: pointer; }

.pending-panel { margin-top: 20px; padding-top: 20px; border-top: 1px solid var(--line); }
.pending-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.text-button { border: 0; padding: 4px; background: transparent; color: var(--blue); font-weight: 700; cursor: pointer; }
.pending-pages { display: grid; grid-auto-flow: column; grid-auto-columns: 96px; gap: 10px; overflow-x: auto; padding: 3px 2px 10px; scroll-snap-type: x proximity; }
.pending-page { height: 124px; position: relative; border-radius: 12px; overflow: hidden; background: #edf1f6; border: 1px solid var(--line); scroll-snap-align: start; }
.pending-page img { width: 100%; height: 100%; object-fit: cover; }
.pending-page > span { position: absolute; left: 7px; bottom: 7px; min-width: 23px; height: 23px; display: grid; place-items: center; border-radius: 8px; background: rgba(23,32,51,.82); color: white; font-size: 12px; font-weight: 800; }
.pending-page button { position: absolute; top: 6px; right: 6px; width: 27px; height: 27px; padding: 0; display: grid; place-items: center; border: 0; border-radius: 9px; background: rgba(23,32,51,.78); color: white; cursor: pointer; }
.pending-page button .icon { width: 15px; height: 15px; }
.add-page { width: 100%; border: 1px dashed #b9c5d7; border-radius: 13px; padding: 12px; background: var(--surface-soft); color: var(--blue); font-weight: 700; display: flex; align-items: center; justify-content: center; gap: 7px; cursor: pointer; }
.title-label { display: block; margin: 18px 0 7px; font-size: 13px; font-weight: 750; }
.title-label span { color: var(--muted); font-weight: 500; }
.title-input, .rename-row input { width: 100%; height: 48px; border: 1px solid var(--line); border-radius: 12px; background: white; color: var(--ink); padding: 0 14px; }
.title-input::placeholder, .rename-row input::placeholder { color: #98a2b3; }

.button { min-height: 46px; padding: 0 18px; border-radius: 13px; border: 1px solid transparent; display: inline-flex; align-items: center; justify-content: center; gap: 8px; font-weight: 750; cursor: pointer; }
.button:disabled { opacity: .55; cursor: default; }
.button.primary { background: var(--blue); color: white; }
.button.secondary { background: var(--surface); border-color: var(--line); color: var(--ink); }
.button.danger { background: var(--red-soft); border-color: #ffd5d1; color: var(--red); }
.button.small { min-height: 38px; padding: 0 13px; font-size: 13px; }
.save-button { width: 100%; margin-top: 12px; }
.spinner { width: 19px; height: 19px; border: 2px solid rgba(255,255,255,.35); border-top-color: white; border-radius: 50%; animation: spin .75s linear infinite; display: inline-block; }
.spinner.dark { border-color: rgba(37,99,235,.18); border-top-color: var(--blue); }
@keyframes spin { to { transform: rotate(360deg); } }

.history { margin-top: 38px; }
.history-heading { display: flex; justify-content: space-between; align-items: end; margin-bottom: 14px; padding: 0 2px; }
.document-row { min-height: 104px; display: flex; align-items: center; gap: 14px; margin-bottom: 10px; padding: 12px; border-radius: 18px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; transition: border-color .15s ease, transform .15s ease; }
.document-row:active { transform: scale(.992); }
.document-row:hover { border-color: #c5d0df; }
.cover { width: 62px; height: 78px; flex: 0 0 auto; position: relative; border-radius: 9px; overflow: hidden; background: #e9edf3; border: 1px solid #dbe1ea; }
.cover img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: 0; transition: opacity .2s; }
.cover img.loaded { opacity: 1; }
.cover-placeholder { width: 100%; height: 100%; display: grid; place-items: center; color: #a0aabc; }
.document-copy { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: flex-start; }
.document-copy strong { max-width: 100%; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-size: 15px; }
.document-copy > span:not(.status) { color: var(--muted); font-size: 12px; margin-top: 4px; white-space: nowrap; max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
.status { margin-top: 9px; min-height: 23px; padding: 4px 8px; border-radius: 8px; font-size: 11px; font-weight: 800; display: inline-flex; align-items: center; gap: 6px; }
.status.done { color: var(--green); background: var(--green-soft); }
.status.error { color: var(--red); background: var(--red-soft); }
.status.working { color: var(--blue); background: var(--blue-soft); }
.status.working i { width: 6px; height: 6px; border-radius: 50%; background: var(--blue); animation: pulse 1.1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .25; } }
.row-actions { display: flex; align-items: center; gap: 4px; color: #98a2b3; }
.quick-download { width: 40px; height: 40px; display: grid; place-items: center; border: 0; border-radius: 12px; background: var(--blue-soft); color: var(--blue); cursor: pointer; }
.row-chevron { width: 18px; height: 18px; }
.empty-history { padding: 48px 20px; border: 1px dashed #ccd5e2; border-radius: 20px; color: var(--muted); text-align: center; }
.empty-history strong { display: block; color: var(--ink); margin: 11px 0 5px; }
.empty-history p { margin: 0; font-size: 14px; }
.empty-icon { width: 52px; height: 52px; display: grid; place-items: center; margin: auto; border-radius: 16px; background: var(--blue-soft); color: var(--blue); }
.history-loading { display: grid; gap: 10px; }
.history-loading span { height: 104px; border-radius: 18px; background: linear-gradient(100deg, #e9edf3 30%, #f5f7fa 50%, #e9edf3 70%); background-size: 400%; animation: shimmer 1.4s infinite; }
@keyframes shimmer { to { background-position: -100% 0; } }
.inline-error { padding: 28px; text-align: center; color: var(--muted); }

.auth-shell { min-height: 75vh; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 28px; }
.auth-intro { text-align: center; max-width: 380px; }
.auth-intro h1 { font-size: 34px; margin: 18px 0 9px; }
.auth-intro p { color: var(--muted); line-height: 1.5; }
.auth-logo .brand-mark { width: 40px; height: 48px; border-radius: 8px; }
#clerk-sign-in { min-height: 420px; }
.message-card { max-width: 460px; margin: 15vh auto 0; padding: 28px; border: 1px solid var(--line); border-radius: 22px; background: var(--surface); box-shadow: var(--shadow); text-align: center; }
.message-card h1, .message-card h2 { font-size: 26px; }
.message-card p { color: var(--muted); line-height: 1.55; }

#toast { position: fixed; z-index: 100; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%); max-width: calc(100vw - 32px); padding: 12px 16px; border-radius: 12px; background: #172033; color: white; box-shadow: 0 12px 30px rgba(23,32,51,.26); font-size: 14px; font-weight: 650; text-align: center; }
#toast.error { background: #9f2f29; }

.modal-open { overflow: hidden; }
.modal-backdrop { position: fixed; z-index: 70; inset: 0; display: flex; align-items: center; justify-content: center; padding: 18px; background: rgba(15,23,42,.48); backdrop-filter: blur(7px); }
.modal { width: min(720px, 100%); max-height: calc(100dvh - 36px); overflow-y: auto; background: var(--surface); border-radius: 24px; box-shadow: 0 30px 90px rgba(15,23,42,.3); }
.modal-loading { min-height: 400px; display: flex; align-items: center; justify-content: center; gap: 10px; color: var(--muted); }
.modal-head { min-height: 80px; display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 18px 20px; border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 2; background: rgba(255,255,255,.94); backdrop-filter: blur(12px); }
.modal-head > div { min-width: 0; }
.modal-head span { color: var(--muted); font-size: 12px; }
.modal-head h2 { margin-top: 3px; max-width: 530px; font-size: 19px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.processing-banner, .error-banner { margin: 16px 20px 0; padding: 13px 14px; display: flex; align-items: center; gap: 10px; border-radius: 13px; font-size: 13px; }
.processing-banner { background: var(--blue-soft); color: var(--blue-dark); }
.error-banner { background: var(--red-soft); color: var(--red); flex-wrap: wrap; }
.error-banner strong { width: 100%; }
.error-banner span { flex: 1; }
.viewer { padding: 18px 20px 0; }
.viewer-toolbar { min-height: 40px; display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.viewer-toolbar > span { color: var(--muted); font-size: 12px; font-weight: 650; }
.segmented { padding: 3px; border-radius: 11px; background: #eef2f7; display: inline-flex; }
.segmented button { min-height: 32px; padding: 0 12px; border: 0; border-radius: 8px; background: transparent; color: var(--muted); font-size: 12px; font-weight: 750; cursor: pointer; }
.segmented button.active { color: var(--ink); background: white; box-shadow: 0 2px 8px rgba(23,32,51,.08); }
.segmented button:disabled { opacity: .4; cursor: default; }
.page-stage { height: min(58vh, 600px); position: relative; display: grid; place-items: center; overflow: hidden; border-radius: 16px; background: #e9edf3; }
.page-stage > .spinner { position: absolute; }
.page-stage img { max-width: 100%; max-height: 100%; object-fit: contain; opacity: 0; transition: opacity .18s; }
.page-stage img.visible { opacity: 1; }
.page-navigation { min-height: 58px; display: flex; align-items: center; justify-content: center; gap: 18px; }
#page-dots { display: flex; align-items: center; gap: 5px; max-width: 220px; overflow: hidden; }
#page-dots i { width: 6px; height: 6px; flex: 0 0 auto; border-radius: 50%; background: #c5cedb; }
#page-dots i.active { width: 16px; border-radius: 5px; background: var(--blue); }
.document-editor { padding: 18px 20px; border-top: 1px solid var(--line); }
.document-editor label { display: block; margin-bottom: 7px; font-size: 12px; font-weight: 750; }
.rename-row { display: flex; gap: 8px; }
.rename-row input { flex: 1; min-width: 0; }
.modal-actions { padding: 0 20px 24px; display: flex; gap: 9px; }
.modal-actions .primary { flex: 1; }

@media (max-width: 600px) {
  #app { padding: 24px 14px calc(64px + env(safe-area-inset-bottom)); }
  .scanner { margin: 0 -2px; padding: 22px 18px; border-radius: 22px; }
  h1 { font-size: 34px; }
  .camera-button { min-height: 96px; }
  .document-row { padding: 10px; }
  .cover { width: 56px; height: 72px; }
  .modal-backdrop { padding: 0; align-items: flex-end; }
  .modal { width: 100%; max-height: calc(100dvh - env(safe-area-inset-top)); border-radius: 22px 22px 0 0; padding-bottom: env(safe-area-inset-bottom); }
  .page-stage { height: 52vh; }
  .modal-actions { flex-direction: column; }
  .rename-row .button { padding: 0 13px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
