/* =========================================================
   SmartScan V0
   Mobile-first visual system
   ========================================================= */

:root {
  --bg: #f7f8fc;
  --surface: #ffffff;
  --surface-2: #f0f2f7;
  --text: #171922;
  --muted: #747987;
  --border: #e5e7ed;

  --primary: #3157e8;
  --primary-dark: #2446c9;
  --primary-soft: #eaf0ff;

  --accent: #3157e8;

  --danger: #d83b4a;
  --success: #248a5a;

  --shadow: 0 10px 30px rgba(22, 29, 55, 0.08);
  --radius: 20px;
  --nav-height: 78px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  font-family:
    Inter,
    ui-sans-serif,
    system-ui,
    -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    sans-serif;
  background: var(--bg);
  color: var(--text);
}

body {
  min-height: 100vh;
}

button,
input {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

button:active {
  transform: scale(0.98);
}

.hidden {
  display: none !important;
}

/* ---------------------------------------------------------
   App shell
   --------------------------------------------------------- */

#app {
  min-height: 100vh;
  max-width: 680px;
  margin: 0 auto;
  position: relative;
  padding-bottom: calc(var(--nav-height) + 30px);
}

.topbar {
  min-height: 78px;
  padding:
    max(18px, env(safe-area-inset-top))
    18px
    12px;

  display: flex;
  align-items: center;
  gap: 12px;

  background: rgba(247, 248, 252, 0.94);
  backdrop-filter: blur(18px);

  position: sticky;
  top: 0;
  z-index: 20;
}

.brand {
  display: flex;
  align-items: center;
  gap: 11px;
  flex: 1;
}

.brand-mark {
  width: 38px;
  height: 38px;
  border-radius: 12px;
  background: var(--primary);
  color: white;

  display: grid;
  place-items: center;

  font-weight: 800;
  font-size: 19px;

  box-shadow: 0 7px 18px rgba(49, 87, 232, 0.25);
}

.brand-name {
  font-weight: 800;
  letter-spacing: -0.3px;
}

.brand-subtitle {
  font-size: 11px;
  color: var(--muted);
  margin-top: 1px;
}

.icon-btn {
  width: 42px;
  height: 42px;
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.06);
  font-size: 19px;
  display: grid;
  place-items: center;
}

/* ---------------------------------------------------------
   Main
   --------------------------------------------------------- */

.main {
  padding: 8px 18px 30px;
}

.page-heading {
  margin: 12px 0 20px;
}

.page-heading h1 {
  font-size: 29px;
  letter-spacing: -1px;
  margin: 0 0 7px;
}

.page-heading p {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
}

/* ---------------------------------------------------------
   Search
   --------------------------------------------------------- */

.search {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 15px;
  padding: 13px 15px;
  outline: none;
  color: var(--text);
  margin-bottom: 18px;
}

.search:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(49, 87, 232, 0.1);
}

/* ---------------------------------------------------------
   Hero
   --------------------------------------------------------- */

.hero {
  background:
    radial-gradient(circle at 90% 10%, rgba(255,255,255,.22), transparent 35%),
    linear-gradient(135deg, #3157e8, #553fe1);

  color: white;
  border-radius: 24px;
  padding: 22px;
  margin-bottom: 24px;

  box-shadow: 0 15px 35px rgba(49, 87, 232, 0.22);
}

.hero-kicker {
  font-size: 12px;
  opacity: .78;
  margin-bottom: 7px;
}

.hero h2 {
  margin: 0 0 8px;
  font-size: 24px;
  letter-spacing: -.7px;
}

.hero p {
  margin: 0 0 18px;
  color: rgba(255,255,255,.82);
  font-size: 13px;
  line-height: 1.5;
}

.primary-btn,
.secondary-btn,
.danger-btn {
  min-height: 46px;
  padding: 0 17px;
  border-radius: 14px;
  font-weight: 700;
}

.primary-btn {
  background: var(--primary);
  color: white;
}

.hero .primary-btn {
  background: white;
  color: var(--primary);
}

.secondary-btn {
  background: var(--surface-2);
  color: var(--text);
}

.danger-btn {
  background: #fff0f1;
  color: var(--danger);
}

/* ---------------------------------------------------------
   Sections
   --------------------------------------------------------- */

.section {
  margin-bottom: 27px;
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 11px;
}

.section-header h2 {
  margin: 0;
  font-size: 17px;
}

.section-header button {
  background: transparent;
  color: var(--primary);
  font-size: 13px;
  font-weight: 700;
}

/* ---------------------------------------------------------
   Empty state
   --------------------------------------------------------- */

.empty-state {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 32px 20px;
  text-align: center;
}

.empty-icon {
  width: 66px;
  height: 66px;
  border-radius: 21px;
  background: var(--primary-soft);
  color: var(--primary);

  margin: 0 auto 15px;

  display: grid;
  place-items: center;

  font-size: 29px;
}

.empty-state h2 {
  margin: 0 0 7px;
  font-size: 18px;
}

.empty-state p {
  max-width: 310px;
  margin: 0 auto 19px;
  color: var(--muted);
  line-height: 1.5;
  font-size: 13px;
}

/* ---------------------------------------------------------
   Document cards
   --------------------------------------------------------- */

.document-list {
  display: grid;
  gap: 10px;
}

.document-card {
  display: flex;
  align-items: center;
  gap: 13px;

  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;

  transition: .15s ease;
  cursor: pointer;
}

.document-card:hover {
  box-shadow: var(--shadow);
}

.document-card:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.document-thumb {
  width: 54px;
  height: 68px;
  border-radius: 10px;

  background:
    linear-gradient(145deg, #edf0f7, #dce1ec);

  flex: 0 0 auto;

  display: grid;
  place-items: center;

  color: #81889a;
  overflow: hidden;
}

.document-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.document-info {
  flex: 1;
  min-width: 0;
}

.document-info h3 {
  margin: 0 0 5px;
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.document-meta {
  color: var(--muted);
  font-size: 11px;
}

.card-menu {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--muted);
}

/* ---------------------------------------------------------
   Folders
   --------------------------------------------------------- */

.folder-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 11px;
}

.folder-card {
  padding: 17px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
}

.folder-icon {
  font-size: 24px;
  color: #e6a52d;
  margin-bottom: 13px;
}

.folder-card h3 {
  margin: 0 0 4px;
  font-size: 14px;
}

.folder-card p {
  margin: 0;
  color: var(--muted);
  font-size: 11px;
}

/* ---------------------------------------------------------
   Scanner
   --------------------------------------------------------- */

.scanner {
  margin: -8px -18px 0;
  min-height: calc(100vh - 120px);
  background: #101116;
  color: white;
  display: flex;
  flex-direction: column;
}

.scanner-preview {
  flex: 1;
  min-height: 430px;
  margin: 18px;
  border-radius: 25px;

  background:
    linear-gradient(135deg, #252833, #12141b);

  position: relative;
  overflow: hidden;

  display: flex;
  align-items: center;
  justify-content: center;
}

.scanner-preview-hint {
  position: absolute;
  bottom: 18px;
  left: 0;
  right: 0;

  text-align: center;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: .02em;

  color: #fff;
  text-shadow: 0 1px 4px rgba(0, 0, 0, .6);

  padding: 0 18px;
  pointer-events: none;

  z-index: 3;
}

.scanner-preview img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  border-radius: 25px;
  position: relative;
  z-index: 1;
}

.scanner-preview::before {
  content: "";
  width: 72%;
  height: 58%;
  border: 2px solid rgba(255,255,255,.75);
  border-radius: 7px;

  box-shadow:
    0 0 0 999px rgba(0,0,0,.22);

  transform: perspective(500px) rotateX(3deg);

  position: relative;
  z-index: 2;
}

.scanner-controls {
  padding: 8px 22px 30px;
}

.scanner-top-controls {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.scanner-control {
  width: 45px;
  height: 45px;
  border-radius: 50%;
  background: #292c36;
  color: white;
}

.capture-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 25px;
}

.capture-button {
  width: 76px;
  height: 76px;
  border-radius: 50%;
  background: white;
  border: 7px solid rgba(255,255,255,.2);
  background-clip: padding-box;
  box-shadow: 0 0 0 2px white;
}

/* ---------------------------------------------------------
   Page editor
   --------------------------------------------------------- */

.editor-preview {
  background: #1b1d24;
  border-radius: 22px;
  min-height: 390px;

  display: flex;
  align-items: center;
  justify-content: center;

  margin-bottom: 17px;
  overflow: hidden;
}

.editor-preview img {
  max-width: 88%;
  max-height: 360px;
  object-fit: contain;
  border-radius: 3px;
  box-shadow: 0 12px 35px rgba(0,0,0,.35);
}

/* Saved-crop preview: the untouched original clipped to the crop
   region. Rotation and filter are applied to the frame so they
   take effect after the crop, matching the exported result. */
.crop-result {
  position: relative;
  overflow: hidden;
  line-height: 0;
  border-radius: 3px;
  box-shadow: 0 12px 35px rgba(0,0,0,.35);
}

.crop-result img {
  position: absolute;
  max-width: none;
  max-height: none;
  object-fit: fill;
  border-radius: 0;
  box-shadow: none;
}

/* ---------------------------------------------------------
   Crop
   --------------------------------------------------------- */

.crop-stage {
  position: relative;
  display: inline-block;
  line-height: 0;
}

.crop-overlay {
  position: absolute;
  inset: 0;
  cursor: move;
  touch-action: none;
}

.crop-rect {
  position: absolute;

  box-shadow: 0 0 0 9999px rgba(0,0,0,.55);
  outline: 1px solid rgba(255,255,255,.9);

  cursor: move;
  touch-action: none;
}

.crop-handle {
  position: absolute;

  width: 30px;
  height: 30px;

  background: transparent;
  border: 0;

  touch-action: none;
}

.crop-handle::after {
  content: "";
  position: absolute;

  width: 15px;
  height: 15px;

  border: 3px solid white;
  border-radius: 3px;

  background: rgba(49, 87, 232, .35);
  box-shadow: 0 1px 4px rgba(0,0,0,.4);
}

.crop-handle[data-crop-handle="nw"] { top: 0; left: 0; }
.crop-handle[data-crop-handle="nw"]::after {
  top: 7px; left: 7px;
  transform: translate(-50%, -50%);
}

.crop-handle[data-crop-handle="ne"] { top: 0; right: 0; }
.crop-handle[data-crop-handle="ne"]::after {
  top: 7px; right: 7px;
  transform: translate(50%, -50%);
}

.crop-handle[data-crop-handle="sw"] { bottom: 0; left: 0; }
.crop-handle[data-crop-handle="sw"]::after {
  bottom: 7px; left: 7px;
  transform: translate(-50%, 50%);
}

.crop-handle[data-crop-handle="se"] { bottom: 0; right: 0; }
.crop-handle[data-crop-handle="se"]::after {
  bottom: 7px; right: 7px;
  transform: translate(50%, 50%);
}

.editor-tools {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 18px;
}

.tool-btn {
  min-height: 66px;
  border-radius: 15px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);

  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 5px;

  font-size: 11px;
}

.tool-btn span:first-child {
  font-size: 19px;
}

.tool-btn.active {
  background: var(--primary-soft);
  border-color: var(--primary);
  color: var(--primary);
}

.tool-btn[disabled] {
  opacity: .4;
  cursor: default;
}

.filter-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 20px;
}

.filter-btn {
  border-radius: 12px;
  padding: 9px 4px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 10px;
}

.filter-btn.active {
  background: var(--primary-soft);
  color: var(--primary);
  font-weight: 700;
}

/* ---------------------------------------------------------
   Document editor
   --------------------------------------------------------- */

.document-editor-title {
  width: 100%;
  background: transparent;
  border: 0;
  border-bottom: 2px solid var(--border);
  padding: 9px 0;
  outline: none;
  font-size: 22px;
  font-weight: 800;
  margin-bottom: 20px;
  color: var(--text);
}

.page-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.page-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 9px;
  position: relative;
}

.page-number {
  position: absolute;
  top: 14px;
  left: 14px;
  background: rgba(0,0,0,.65);
  color: white;
  border-radius: 8px;
  padding: 3px 7px;
  font-size: 10px;
}

.page-image {
  width: 100%;
  aspect-ratio: .76;
  border-radius: 10px;
  background: #e9ebf0;
  object-fit: contain;
}

.page-actions {
  display: flex;
  gap: 6px;
  margin-top: 7px;
}

.page-actions button {
  flex: 1;
  min-height: 32px;
  border-radius: 9px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 11px;
}

/* ---------------------------------------------------------
   Settings
   --------------------------------------------------------- */

.settings-list {
  display: grid;
  gap: 9px;
}

.setting-row {
  min-height: 58px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 12px 14px;

  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 15px;
}

.setting-info {
  flex: 1;
  min-width: 0; /* prevents flex item overflow */
}

.setting-info strong {
  display: block;
  font-size: 13px;
  margin-bottom: 3px;
}

.setting-info small {
  color: var(--muted);
  font-size: 11px;
}

select {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 10px;
  padding: 8px 12px;
  outline: none;
  min-width: 0;
  max-width: 100%; /* prevent overflow on mobile */
  flex-shrink: 1;
}

/* ---------------------------------------------------------
   Export sheet
   --------------------------------------------------------- */

.export-formats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 14px 0 16px;
}

.export-format {
  padding: 12px 5px;
  border-radius: 14px;
  background: var(--surface-2);
  color: var(--muted);
  text-align: center;
}

.export-format strong {
  display: block;
  font-size: 13px;
  margin-bottom: 3px;
}

.export-format small {
  display: block;
  font-size: 10px;
  line-height: 1.3;
}

.export-format.active {
  background: var(--primary-soft);
  color: var(--primary);
  box-shadow: inset 0 0 0 1px var(--primary);
}

/* ---------------------------------------------------------
   Modal
   --------------------------------------------------------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 100;

  background: rgba(9, 11, 18, .5);
  backdrop-filter: blur(5px);

  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.modal {
  width: min(680px, 100%);
  background: var(--surface);
  border-radius: 25px 25px 0 0;
  padding: 22px 18px calc(22px + env(safe-area-inset-bottom));
  animation: slideUp .18s ease;
}

@keyframes slideUp {
  from {
    transform: translateY(30px);
    opacity: 0;
  }

  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.modal h2 {
  margin: 0 0 7px;
  font-size: 20px;
}

.modal p {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.5;
}

.modal-input {
  width: 100%;
  padding: 13px;
  border: 1px solid var(--border);
  border-radius: 13px;
  outline: none;
  margin: 8px 0 15px;
  background: var(--surface);
  color: var(--text);
}

.modal-actions {
  display: flex;
  gap: 9px;
}

.modal-actions button {
  flex: 1;
}

.modal-note {
  margin: 14px 0 0;
  text-align: center;
  color: var(--text-secondary);
  font-size: 12px;
  line-height: 1.5;
}

/* ---------------------------------------------------------
   Toast
   --------------------------------------------------------- */

.toast {
  position: fixed;
  left: 18px;
  right: 18px;
  bottom: calc(var(--nav-height) + 18px);

  background: #171922;
  color: white;

  padding: 13px 15px;
  border-radius: 14px;

  font-size: 13px;
  text-align: center;

  z-index: 200;

  animation: toastIn .2s ease;
}

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

  to {
    transform: translateY(0);
    opacity: 1;
  }
}

/* ---------------------------------------------------------
   Spinner (for OCR loading)
   --------------------------------------------------------- */

.spinner {
  width: 28px;
  height: 28px;
  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); }
}

/* ---------------------------------------------------------
   OCR Modal adjustments
   --------------------------------------------------------- */

#ocrModal .modal {
  max-width: 420px;
  border-radius: 20px;
  animation: fadeIn .15s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

#ocrModal .modal-body textarea {
  width: 100%;
  min-height: 200px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 14px;
  line-height: 1.5;
  resize: vertical;
  box-sizing: border-box;
  outline: none;
}

#ocrModal .modal-body textarea:focus {
  border-color: var(--accent);
}

#ocrModal .modal-footer {
  display: flex;
  gap: 8px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  margin-top: 12px;
}

#ocrModal .modal-footer button {
  flex: 1;
}

/* ---------------------------------------------------------
   Bottom navigation
   --------------------------------------------------------- */

.bottom-nav {
  position: fixed;
  z-index: 30;

  left: 50%;
  bottom: 0;
  transform: translateX(-50%);

  width: min(680px, 100%);

  height: var(--nav-height);
  padding-bottom: env(safe-area-inset-bottom);

  background: rgba(255,255,255,.94);
  backdrop-filter: blur(18px);

  border-top: 1px solid var(--border);

  display: grid;
  grid-template-columns: repeat(4, 1fr);
}

.nav-item {
  background: transparent;
  color: var(--muted);

  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
}

.nav-item span {
  font-size: 20px;
}

.nav-item small {
  font-size: 10px;
}

.nav-item.active {
  color: var(--primary);
}

/* ---------------------------------------------------------
   Floating scan button
   --------------------------------------------------------- */

.scan-fab {
  position: fixed;
  z-index: 25;

  right: max(12px, calc((100vw - 680px) / 2 + 12px));
  bottom: calc(var(--nav-height) + 12px);

  min-height: 44px;
  padding: 0 14px 0 10px;

  border-radius: 16px;

  background: var(--primary);
  color: white;

  display: flex;
  align-items: center;
  gap: 6px;

  font-weight: 700;
  font-size: 11px;

  box-shadow: 0 8px 20px rgba(49,87,232,.28);
}

.scan-icon {
  width: 26px;
  height: 26px;
  border-radius: 8px;
  background: rgba(255,255,255,.16);
  display: grid;
  place-items: center;
  font-size: 18px;
}

/* ---------------------------------------------------------
   Dark mode
   --------------------------------------------------------- */

body.dark {
  --bg: #101116;
  --surface: #181a21;
  --surface-2: #22252e;
  --text: #f4f5f8;
  --muted: #999eac;
  --border: #2d3039;

  --primary-soft: #1d2b5c;
}

body.dark .topbar {
  background: rgba(16,17,22,.94);
}

body.dark .bottom-nav {
  background: rgba(24,26,33,.95);
}

body.dark .empty-icon {
  background: var(--primary-soft);
}

body.dark .hero {
  box-shadow: none;
}

body.dark .danger-btn {
  background: #3a2025;
}

/* ---------------------------------------------------------
   Larger screens
   --------------------------------------------------------- */

@media (min-width: 700px) {
  #app {
    box-shadow:
      0 0 70px rgba(0,0,0,.05);
    min-height: 100vh;
  }

  .main {
    padding-left: 25px;
    padding-right: 25px;
  }

  .topbar {
    padding-left: 25px;
    padding-right: 25px;
  }

  .scanner {
    margin-left: -25px;
    margin-right: -25px;
  }
}

@media (max-width: 480px) {
  :root {
    --nav-height: 64px;
  }

  .scan-fab {
    right: 12px;
    bottom: calc(var(--nav-height) + 12px);
    min-height: 40px;
    padding: 0 12px 0 8px;
    border-radius: 14px;
    font-size: 10px;
    gap: 4px;
  }

  .scan-icon {
    width: 22px;
    height: 22px;
    border-radius: 6px;
    font-size: 16px;
  }

  .bottom-nav {
    height: var(--nav-height);
  }

  .nav-item span {
    font-size: 18px;
  }

  .nav-item small {
    font-size: 9px;
  }

  #app {
    padding-bottom: calc(var(--nav-height) + 20px);
  }

  .main {
    padding: 8px 12px 20px;
  }

  .topbar {
    min-height: 64px;
    padding: max(12px, env(safe-area-inset-top)) 12px 8px;
  }

  .page-heading h1 {
    font-size: 24px;
  }

  .search {
    padding: 11px 12px;
    border-radius: 12px;
  }

  .setting-row {
    padding: 12px;
    gap: 10px;
    flex-wrap: wrap;
  }

  .setting-info strong {
    font-size: 12px;
  }

  .setting-info small {
    font-size: 10px;
  }

  select {
    padding: 8px 10px;
    font-size: 13px;
  }

  .document-card {
    padding: 10px;
    gap: 10px;
  }

  .document-thumb {
    width: 44px;
    height: 56px;
    border-radius: 8px;
  }
}
