:root,
[data-theme="dark"] {
  --bg: #0e1117;
  --card: #191e28;
  --card-elevated: #222938;
  --card-border: rgba(255, 255, 255, 0.07);
  --card-shadow: 0 4px 16px rgba(0, 0, 0, 0.28);
  --input-bg: #131720;
  --text: #f8fafc;
  --muted: #94a3b8;
  --primary: #2f7cf6;
  --primary-hover: #2563eb;
  --secondary: #222938;
  --secondary-border: rgba(255, 255, 255, 0.1);
  --pill-bg: #1f2532;
  --radius: 20px;
}

[data-theme="light"] {
  --bg: #eff3f8;
  --card: #ffffff;
  --card-elevated: #f1f5f9;
  --card-border: rgba(15, 23, 42, 0.07);
  --card-shadow: 0 4px 18px rgba(15, 23, 42, 0.06);
  --input-bg: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #2f7cf6;
  --primary-hover: #2563eb;
  --secondary: #f1f5f9;
  --secondary-border: rgba(15, 23, 42, 0.1);
  --pill-bg: #f8fafc;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  max-width: 100%;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
  line-height: 1.4;
  overscroll-behavior-y: contain;
  transition: background 0.2s ease, color 0.2s ease;
}

.app-shell {
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  overflow-x: hidden;
  padding: calc(env(safe-area-inset-top) + 12px) 16px calc(env(safe-area-inset-bottom) + 24px);
}

/* ================= HEADER ================= */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.topbar h1 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.025em;
}

.status-dot {
  font-size: 0.72rem;
  font-weight: 600;
  padding: 4px 9px;
  border-radius: 999px;
  background: rgba(16, 185, 129, 0.14);
  color: #10b981;
  border: 1px solid rgba(16, 185, 129, 0.25);
  white-space: nowrap;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-circle-btn {
  width: 40px;
  height: 40px;
  border-radius: 999px;
  border: 1px solid var(--card-border);
  background: var(--card);
  color: var(--text);
  box-shadow: var(--card-shadow);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

/* ================= VIEWS ================= */
.view { display: none; width: 100%; }
.view.active { display: block; }
.hidden { display: none !important; }

/* ================= 2-TILE GRID ================= */
.tile-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.tile {
  appearance: none;
  border: 1px solid var(--card-border);
  background: var(--card);
  color: var(--text);
  border-radius: 22px;
  padding: 18px 14px;
  box-shadow: var(--card-shadow);
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  cursor: pointer;
  min-width: 0;
  transition: transform 0.12s ease;
}

.tile:active { transform: scale(0.975); }

.tile-hero {
  background: var(--primary);
  border-color: transparent;
  color: #ffffff;
}

.tile-icon-circle {
  width: 52px;
  height: 52px;
  border-radius: 999px;
  background: var(--card-elevated);
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 12px;
}

.hero-circle {
  background: #ffffff;
  color: var(--primary);
}

.tile-title {
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: -0.015em;
  margin-bottom: 4px;
}

.tile-desc {
  font-size: 0.76rem;
  line-height: 1.3;
  color: var(--muted);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.tile-hero .tile-desc {
  color: rgba(255, 255, 255, 0.85);
}

/* ================= SEARCH BAR ================= */
.search-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  margin-bottom: 18px;
}

.search-box {
  position: relative;
  display: flex;
  align-items: center;
  min-width: 0;
}

.search-icon {
  position: absolute;
  left: 14px;
  color: var(--muted);
  pointer-events: none;
}

.search-box input {
  width: 100%;
  height: 46px;
  border-radius: 999px;
  border: 1px solid var(--card-border);
  background: var(--card);
  color: var(--text);
  box-shadow: var(--card-shadow);
  padding: 0 16px 0 40px;
  font-size: 0.92rem;
  outline: none;
}

.search-box input::placeholder { color: var(--muted); }

.search-filter-btn {
  height: 46px;
  min-width: 54px;
  padding: 0 14px;
  border-radius: 999px;
  border: none;
  background: var(--primary);
  color: #ffffff;
  font-size: 0.82rem;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(47, 124, 246, 0.35);
}

/* ================= RECENT DOCUMENTS ================= */
.recents-section { width: 100%; }

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
}

.section-title {
  margin: 0;
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.text-btn {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  padding: 4px 6px;
}

.recents-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.recent-card {
  display: grid;
  grid-template-columns: 58px minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: 18px;
  padding: 12px;
  box-shadow: var(--card-shadow);
  width: 100%;
}

.recent-thumb-wrap {
  position: relative;
  width: 58px;
  height: 68px;
  border-radius: 10px;
  overflow: hidden;
  background: var(--card-elevated);
  border: 1px solid var(--card-border);
  display: flex;
  align-items: center;
  justify-content: center;
}

.recent-thumb-wrap img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.type-badge {
  position: absolute;
  top: 4px;
  left: 4px;
  font-size: 0.62rem;
  font-weight: 800;
  padding: 2px 5px;
  border-radius: 4px;
  color: #fff;
  letter-spacing: 0.03em;
}
.type-badge.pdf { background: #dc2626; }
.type-badge.jpg { background: #2f7cf6; }

.recent-info {
  min-width: 0;
  overflow: hidden;
}

.recent-title {
  font-size: 0.92rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 2px;
}

.recent-meta {
  font-size: 0.76rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 8px;
}

.recent-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.mini-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 5px 11px;
  border-radius: 999px;
  border: 1px solid var(--card-border);
  background: var(--pill-bg);
  color: var(--text);
  font-size: 0.76rem;
  font-weight: 600;
  cursor: pointer;
}

.recent-del-btn {
  width: 32px;
  height: 32px;
  border-radius: 999px;
  border: none;
  background: transparent;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.empty-recents {
  background: var(--card);
  border: 1px dashed var(--secondary-border);
  border-radius: 18px;
  padding: 22px 16px;
  text-align: center;
  color: var(--muted);
  font-size: 0.86rem;
}

/* ================= SUBVIEW NAV & CARDS ================= */
.subview-nav {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
}

.back-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 8px 12px 8px 8px;
  border-radius: 999px;
  border: 1px solid var(--card-border);
  background: var(--card);
  color: var(--text);
  font-size: 0.86rem;
  font-weight: 600;
  cursor: pointer;
}

.subview-title {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
}

.card {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
  box-shadow: var(--card-shadow);
  width: 100%;
  overflow: hidden;
}

.card h3 { margin: 0 0 6px; font-size: 1.02rem; }
.muted { color: var(--muted); margin: 0 0 12px; font-size: 0.86rem; }
.small { font-size: 0.78rem; }
.mt-8 { margin-top: 8px; }
.mt-12 { margin-top: 12px; }

.action-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 46px;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid transparent;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  color: #fff;
  min-width: 0;
}

.btn-primary { background: var(--primary); }
.btn-primary:active { background: var(--primary-hover); }
.btn-secondary {
  background: var(--secondary);
  border-color: var(--secondary-border);
  color: var(--text);
}
.btn-danger {
  background: #dc2626;
  color: #ffffff;
}

.btn-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: var(--secondary);
  color: var(--text);
  border: 1px solid var(--secondary-border);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
}
.btn-chip.danger { color: #ef4444; border-color: rgba(239, 68, 68, 0.3); }

.mini-actions { display: flex; gap: 6px; flex-wrap: wrap; }

.icon { width: 18px; height: 18px; flex-shrink: 0; }
.icon.lg { width: 23px; height: 23px; }
.icon.sm { width: 16px; height: 16px; }
.icon.xs { width: 13px; height: 13px; }

.control-group { margin-top: 14px; min-width: 0; }
.control-group label {
  display: block;
  font-size: 0.82rem;
  font-weight: 500;
  color: var(--muted);
  margin-bottom: 6px;
}

.control-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}
.control-header label { margin-bottom: 0; }
.control-header strong { font-size: 0.92rem; color: var(--primary); }

input[type="range"] { width: 100%; accent-color: var(--primary); height: 28px; }

.range-hints {
  display: flex;
  justify-content: space-between;
  font-size: 0.74rem;
  color: var(--muted);
}

select {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  min-height: 44px;
  background: var(--input-bg);
  color: var(--text);
  border: 1px solid var(--secondary-border);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 0.86rem;
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--input-bg);
  border: 1px solid var(--secondary-border);
  font-size: 0.84rem;
  font-weight: 500;
  cursor: pointer;
}

.toggle-row input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--primary);
  flex-shrink: 0;
}

.grid-2 {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}
@media (max-width: 440px) {
  .grid-2 { grid-template-columns: 1fr; }
}

/* ================= COMPRESS RESULTS ================= */
.results-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
}

.result-card {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  padding: 12px;
  box-shadow: var(--card-shadow);
  display: grid;
  grid-template-columns: 68px minmax(0, 1fr);
  gap: 12px;
  align-items: center;
  width: 100%;
}

.result-thumb {
  width: 68px;
  height: 84px;
  object-fit: cover;
  border-radius: 10px;
  background: var(--card-elevated);
}

.result-info {
  min-width: 0;
  overflow: hidden;
}

.result-title {
  font-size: 0.88rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 4px;
}

.size-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  font-size: 0.8rem;
  margin-bottom: 8px;
}

.pill-size {
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.75rem;
}
.pill-ok { background: rgba(16, 185, 129, 0.15); color: #10b981; }
.pill-warn { background: rgba(245, 158, 11, 0.15); color: #f59e0b; }

.result-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}
.result-actions .btn {
  min-height: 38px;
  font-size: 0.8rem;
  padding: 6px 8px;
}

/* ================= SCANNER CANVAS & FILTERS ================= */
.canvas-stage,
.preview-box {
  width: 100%;
  background: #05080f;
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid var(--card-border);
  display: flex;
  justify-content: center;
  align-items: center;
}

#edgeCanvas {
  width: 100%;
  height: auto;
  display: block;
  touch-action: none;
  cursor: crosshair;
}

.preview-box { max-height: 52vh; }
#filterPreviewCanvas {
  max-width: 100%;
  max-height: 52vh;
  width: auto;
  height: auto;
  display: block;
}

.filter-pills {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
  margin-top: 12px;
}

.pill {
  border: 1px solid var(--secondary-border);
  background: var(--secondary);
  color: var(--muted);
  border-radius: 10px;
  padding: 9px 4px;
  font-size: 0.74rem;
  font-weight: 600;
  cursor: pointer;
}
.pill.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.pages-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 10px;
  margin-top: 8px;
}

.page-thumb-card {
  background: var(--card-elevated);
  border: 1px solid var(--secondary-border);
  border-radius: 10px;
  overflow: hidden;
}
.page-thumb-card img {
  width: 100%;
  height: 124px;
  object-fit: cover;
  display: block;
}
.page-thumb-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5px 8px;
  font-size: 0.74rem;
  color: var(--muted);
}
.page-del-btn {
  display: inline-flex;
  background: transparent;
  border: none;
  color: #ef4444;
  cursor: pointer;
  padding: 2px;
}

/* ================= MODALS ================= */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
}

.modal-card {
  width: 100%;
  max-width: 360px;
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: 18px;
  padding: 18px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.4);
}
.modal-card h3 { margin: 0 0 8px; font-size: 1rem; }

.modal-input {
  width: 100%;
  height: 44px;
  border-radius: 10px;
  border: 1px solid var(--secondary-border);
  background: var(--input-bg);
  color: var(--text);
  padding: 0 12px;
  font-size: 0.94rem;
  min-width: 0;
}

.filename-input-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
}

.ext-badge {
  flex-shrink: 0;
  height: 44px;
  padding: 0 12px;
  border-radius: 10px;
  background: var(--secondary);
  border: 1px solid var(--secondary-border);
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
}