:root {
  --ink: #14241e;
  --ink-soft: #3d5349;
  --paper: #f3f6f2;
  --paper-2: #e8efe6;
  --brand: #1a4d3e;
  --brand-2: #2a6b55;
  --accent: #c4a35a;
  --danger: #b33a2b;
  --white: #ffffff;
  --shadow: 0 12px 40px rgba(20, 36, 30, 0.12);
  --radius: 16px;
  --radius-sm: 10px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --font: "Noto Sans SC", "PingFang SC", "Hiragino Sans GB", sans-serif;
  --display: "Outfit", "Noto Sans SC", sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
}

body {
  overscroll-behavior: none;
  touch-action: manipulation;
}

button,
input,
label,
.btn,
.icon-btn,
.shutter,
.filter-chip,
.sheet-item,
.link-btn,
.page-add-card,
.page-more,
.recent-item {
  font: inherit;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

[hidden] {
  display: none !important;
  pointer-events: none !important;
}

#app {
  min-height: 100dvh;
  max-width: 480px;
  margin: 0 auto;
  position: relative;
}

.view {
  min-height: 100dvh;
  display: none;
  flex-direction: column;
  padding-bottom: calc(16px + var(--safe-bottom));
}

.view.active {
  display: flex;
}

.view[hidden] {
  display: none !important;
}

/* —— Home —— */
.view-home {
  position: relative;
  padding: calc(28px + var(--safe-top)) 22px 24px;
}

.home-bg {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(120% 80% at 10% -10%, rgba(42, 107, 85, 0.22), transparent 55%),
    radial-gradient(90% 60% at 100% 20%, rgba(196, 163, 90, 0.18), transparent 50%),
    linear-gradient(180deg, #eef4ec 0%, var(--paper) 45%, #e4ebe1 100%);
  z-index: 0;
}

.home-bg::after {
  content: "";
  position: absolute;
  inset: 0;
  opacity: 0.35;
  background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M0 30h60M30 0v60' stroke='%231a4d3e' stroke-opacity='0.06' stroke-width='1'/%3E%3C/svg%3E");
  pointer-events: none;
}

.home-hero,
.home-actions,
.recent-section {
  position: relative;
  z-index: 1;
}

.home-hero {
  padding: 12px 4px 28px;
}

.brand {
  margin: 0;
  font-family: var(--display);
  font-weight: 700;
  font-size: clamp(2.6rem, 12vw, 3.4rem);
  letter-spacing: -0.03em;
  line-height: 1;
  color: var(--brand);
}

.tagline {
  margin: 18px 0 8px;
  font-size: 1.25rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  color: var(--ink);
}

.sub {
  margin: 0;
  color: var(--ink-soft);
  font-size: 0.95rem;
}

.home-actions {
  display: grid;
  gap: 12px;
  position: relative;
  z-index: 2;
}

.recent-section {
  margin-top: 36px;
}

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

.section-head h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

.link-btn {
  border: 0;
  background: none;
  color: var(--brand-2);
  padding: 4px 0;
  cursor: pointer;
}

.recent-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 10px;
}

.recent-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 14px;
  background: rgba(255, 255, 255, 0.72);
  border: 1px solid rgba(26, 77, 62, 0.08);
  border-radius: var(--radius-sm);
  backdrop-filter: blur(8px);
  cursor: pointer;
  text-align: left;
  width: 100%;
  color: inherit;
}

.recent-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  background: var(--paper-2);
  color: var(--brand);
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

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

.recent-name {
  display: block;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.recent-meta {
  display: block;
  margin-top: 2px;
  font-size: 0.82rem;
  color: var(--ink-soft);
}

.empty-hint {
  margin: 8px 0 0;
  color: var(--ink-soft);
  font-size: 0.9rem;
}

.empty-hint[hidden] {
  display: none;
}

/* —— Buttons —— */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 0;
  border-radius: var(--radius-sm);
  padding: 12px 18px;
  font-weight: 600;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  text-align: center;
  transition: transform 0.15s ease, opacity 0.15s ease, background 0.15s ease;
}

.btn:active {
  transform: scale(0.98);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

.btn-lg {
  min-height: 54px;
  font-size: 1.05rem;
}

.btn-block {
  width: 100%;
}

.btn-primary {
  background: var(--brand);
  color: var(--white);
  box-shadow: 0 8px 24px rgba(26, 77, 62, 0.28);
}

.btn-primary:hover {
  background: var(--brand-2);
}

.btn-secondary {
  background: var(--white);
  color: var(--brand);
  border: 1px solid rgba(26, 77, 62, 0.18);
}

.btn-ghost {
  background: transparent;
  color: var(--ink-soft);
}

.btn-icon {
  display: inline-flex;
}

/* —— Top bar —— */
.top-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(10px + var(--safe-top)) 12px 10px;
  background: var(--paper);
  position: sticky;
  top: 0;
  z-index: 5;
}

.top-bar-dark {
  background: #0d1f18;
  color: #f2f7f4;
}

.top-title {
  flex: 1;
  text-align: center;
  font-weight: 600;
  font-size: 1rem;
}

.top-spacer,
.page-count {
  width: 40px;
  text-align: right;
  font-size: 0.85rem;
  color: var(--ink-soft);
}

.top-bar-dark .page-count {
  color: rgba(255, 255, 255, 0.7);
}

.page-count {
  width: auto;
  min-width: 40px;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: inherit;
  display: grid;
  place-items: center;
  cursor: pointer;
}

/* —— Camera —— */
.view-camera {
  background: #0d1f18;
  color: #f2f7f4;
  padding-bottom: 0;
}

.camera-stage {
  flex: 1;
  position: relative;
  margin: 0 12px;
  border-radius: 18px;
  overflow: hidden;
  background: #000;
  min-height: 0;
}

#camera-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.camera-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.camera-frame {
  position: absolute;
  inset: 12% 10%;
  border: 1.5px solid rgba(255, 255, 255, 0.55);
  border-radius: 8px;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.28);
  pointer-events: none;
}

.camera-hint {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 16px;
  margin: 0;
  text-align: center;
  font-size: 0.85rem;
  color: rgba(255, 255, 255, 0.85);
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.5);
}

.camera-controls {
  display: flex;
  justify-content: center;
  padding: 22px 0 calc(28px + var(--safe-bottom));
}

.shutter {
  width: 74px;
  height: 74px;
  border-radius: 50%;
  border: 0;
  background: transparent;
  padding: 0;
  cursor: pointer;
  position: relative;
}

.shutter-ring {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  border: 3px solid #fff;
  box-shadow: inset 0 0 0 4px #0d1f18, inset 0 0 0 28px #fff;
  transition: transform 0.12s ease;
}

.shutter:active .shutter-ring {
  transform: scale(0.92);
}

/* —— Crop / Enhance —— */
.crop-stage,
.enhance-preview {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 8px 16px;
  min-height: 0;
  position: relative;
}

#crop-canvas,
#enhance-canvas {
  max-width: 100%;
  max-height: min(62vh, 520px);
  width: auto;
  height: auto;
  border-radius: 8px;
  box-shadow: var(--shadow);
  background: #111;
  touch-action: none;
}

.process-status {
  margin: 14px 0 0;
  color: var(--ink-soft);
  font-size: 0.9rem;
}

.filter-row {
  display: flex;
  gap: 8px;
  padding: 8px 16px 12px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.filter-chip {
  flex: 0 0 auto;
  border: 1px solid rgba(26, 77, 62, 0.16);
  background: var(--white);
  color: var(--ink-soft);
  border-radius: 999px;
  padding: 10px 16px;
  font-weight: 500;
  cursor: pointer;
}

.filter-chip.active {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
}

.bottom-actions {
  display: flex;
  gap: 10px;
  padding: 12px 16px calc(12px + var(--safe-bottom));
  background: linear-gradient(180deg, transparent, var(--paper) 28%);
}

.bottom-actions.dual > .btn {
  flex: 1;
}

.bottom-actions.stacked {
  flex-direction: column;
  padding: 0;
  background: none;
  margin-top: 20px;
}

/* —— Pages —— */
.pages-grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  padding: 8px 16px 100px;
  align-content: start;
  overflow-y: auto;
}

.page-card {
  position: relative;
  background: var(--white);
  border-radius: var(--radius-sm);
  border: 1px solid rgba(26, 77, 62, 0.1);
  overflow: hidden;
  aspect-ratio: 3 / 4;
  user-select: none;
  touch-action: none;
  transition: transform 0.15s ease, box-shadow 0.15s ease, opacity 0.15s ease;
}

.page-card.dragging {
  opacity: 0.85;
  transform: scale(1.03);
  box-shadow: var(--shadow);
  z-index: 3;
}

.page-card img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  pointer-events: none;
}

.page-badge {
  position: absolute;
  left: 8px;
  top: 8px;
  min-width: 24px;
  height: 24px;
  padding: 0 6px;
  border-radius: 6px;
  background: rgba(20, 36, 30, 0.78);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 600;
  display: grid;
  place-items: center;
}

.page-more {
  position: absolute;
  right: 6px;
  top: 6px;
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.9);
  color: var(--ink);
  cursor: pointer;
  font-size: 1.1rem;
  line-height: 1;
}

.page-add-card {
  aspect-ratio: 3 / 4;
  border: 1.5px dashed rgba(26, 77, 62, 0.28);
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.45);
  color: var(--brand);
  font-weight: 600;
  cursor: pointer;
  display: grid;
  place-items: center;
  gap: 6px;
  padding: 12px;
}

/* —— Settings —— */
.settings-form {
  flex: 1;
  padding: 8px 18px 24px;
  display: grid;
  gap: 22px;
  align-content: start;
}

.field {
  margin: 0;
  border: 0;
  padding: 0;
  display: grid;
  gap: 10px;
}

.field-label {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--ink-soft);
}

.field-input {
  width: 100%;
  border: 1px solid rgba(26, 77, 62, 0.16);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  background: var(--white);
  color: var(--ink);
  outline: none;
}

.field-input:focus {
  border-color: var(--brand-2);
  box-shadow: 0 0 0 3px rgba(42, 107, 85, 0.15);
}

.option-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.radio-card {
  position: relative;
  cursor: pointer;
}

.radio-card input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.radio-card span {
  display: grid;
  place-items: center;
  min-height: 44px;
  border-radius: var(--radius-sm);
  border: 1px solid rgba(26, 77, 62, 0.14);
  background: var(--white);
  font-size: 0.9rem;
  font-weight: 500;
}

.radio-card input:checked + span {
  border-color: var(--brand);
  background: rgba(26, 77, 62, 0.08);
  color: var(--brand);
  font-weight: 600;
}

/* —— PDF result —— */
.view-pdf-result {
  justify-content: center;
  padding: 24px 20px;
  background:
    radial-gradient(80% 50% at 50% 0%, rgba(42, 107, 85, 0.14), transparent 70%),
    var(--paper);
}

.result-card {
  background: rgba(255, 255, 255, 0.86);
  border: 1px solid rgba(26, 77, 62, 0.08);
  border-radius: var(--radius);
  padding: 28px 22px;
  box-shadow: var(--shadow);
  text-align: center;
}

.result-card h2 {
  margin: 0 0 18px;
  font-size: 1.35rem;
}

.progress-track {
  height: 10px;
  border-radius: 999px;
  background: var(--paper-2);
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--brand), var(--brand-2));
  border-radius: inherit;
  transition: width 0.2s ease;
}

.progress-text {
  margin: 14px 0 0;
  color: var(--ink-soft);
  font-size: 0.92rem;
}

.done-mark {
  color: var(--brand);
  margin-bottom: 10px;
  animation: pop 0.45s ease both;
}

.result-filename {
  margin: 0;
  font-weight: 600;
  word-break: break-all;
}

.result-meta {
  margin: 8px 0 0;
  color: var(--ink-soft);
  font-size: 0.9rem;
}

/* —— Toast / Sheet —— */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(28px + var(--safe-bottom));
  transform: translateX(-50%);
  max-width: min(90vw, 420px);
  background: rgba(20, 36, 30, 0.92);
  color: #fff;
  padding: 12px 18px;
  border-radius: 12px;
  font-size: 0.9rem;
  z-index: 50;
  animation: rise 0.25s ease;
}

.sheet {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 40;
}

.sheet:not([hidden]) {
  display: block;
}

.sheet-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(10, 18, 14, 0.45);
}

.sheet-panel {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 12px 12px calc(12px + var(--safe-bottom));
  display: grid;
  gap: 8px;
  animation: sheet-up 0.25s ease;
}

.sheet-item {
  display: grid;
  place-items: center;
  border: 0;
  background: var(--white);
  border-radius: 12px;
  min-height: 50px;
  font-weight: 600;
  cursor: pointer;
  color: var(--ink);
}

.sheet-item.danger {
  color: var(--danger);
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes pop {
  from {
    opacity: 0;
    transform: scale(0.85);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes sheet-up {
  from {
    transform: translateY(24px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

@media (min-width: 481px) {
  #app {
    box-shadow: 0 0 0 1px rgba(26, 77, 62, 0.06), var(--shadow);
    min-height: 100dvh;
  }
}
