:root {
  --paper: #efe8dc;
  --paper-2: #f7f1e6;
  --ink: #1c1710;
  --ink-2: #4a4338;
  --muted: #8a8174;
  --line: #d9cfc0;
  --accent: #d85a31;
  --accent-2: #b54422;
  --dark: #14110e;
  --handle: #fff8ee;
  --shadow: 0 18px 50px rgba(28, 23, 16, 0.16);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }

/* 必须盖过 .busy/.editor 的 display:flex，否则 hidden 无效，一打开就转圈 */
[hidden] { display: none !important; }
body {
  font-family: "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--paper);
  color: var(--ink);
  line-height: 1.45;
}

button, input { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; }
button:disabled { opacity: 0.4; cursor: not-allowed; }

#app {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  padding-top: var(--safe-t);
}

.file-hint {
  margin: 8px 16px 0;
  padding: 10px 12px;
  border-radius: 12px;
  background: #fff3e4;
  border: 1px solid #e8c8a8;
  color: #6a3b1c;
  font-size: 13px;
  line-height: 1.55;
}
.file-hint code {
  font-size: 12px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px 8px;
}
.brand { display: flex; gap: 12px; align-items: center; }
.stamp {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: var(--ink);
  position: relative;
  box-shadow: 0 8px 18px rgba(28, 23, 16, 0.18);
}
.stamp:before {
  content: "";
  position: absolute;
  inset: 8px 9px 9px;
  border-radius: 3px;
  background: var(--paper);
}
.stamp:after {
  content: "";
  position: absolute;
  left: 12px;
  right: 12px;
  top: 13px;
  height: 3px;
  border-radius: 2px;
  background: var(--accent);
}
.brand h1 {
  font-family: "Songti SC", "Noto Serif SC", Georgia, serif;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: 0.12em;
  line-height: 1;
}
.brand p { color: var(--muted); font-size: 12px; margin-top: 4px; }
.page-count {
  font-size: 13px;
  color: var(--ink-2);
  background: #fff8ee;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 12px;
}

.stage {
  flex: 1;
  padding: 8px 16px 110px;
}

.empty {
  text-align: center;
  max-width: 360px;
  margin: 48px auto 0;
}
.empty-art {
  width: 150px;
  height: 120px;
  margin: 0 auto 22px;
  position: relative;
}
.paper {
  position: absolute;
  left: 38px;
  top: 18px;
  width: 78px;
  height: 96px;
  background: #fffdf8;
  border: 1.5px solid var(--ink);
  border-radius: 4px;
  transform: rotate(-6deg);
  box-shadow: 8px 10px 0 var(--ink);
}
.paper.p2 {
  left: 52px;
  top: 10px;
  transform: rotate(8deg);
  background: #f3ece1;
  box-shadow: none;
  opacity: 0.9;
}
.scan-line {
  position: absolute;
  left: 24px;
  right: 24px;
  top: 54px;
  height: 2px;
  background: var(--accent);
  box-shadow: 0 0 12px var(--accent);
  animation: scan 2.4s ease-in-out infinite;
}
@keyframes scan {
  0%, 100% { top: 28px; opacity: 0.2; }
  50% { top: 88px; opacity: 1; }
}
.empty h2 { font-size: 22px; margin-bottom: 8px; }
.empty p { color: var(--ink-2); font-size: 14px; line-height: 1.7; }
.empty .home-tip {
  margin-top: 14px;
  font-size: 13px;
  color: var(--muted);
}

.pages {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(148px, 1fr));
  gap: 12px;
}
.page-card {
  background: #fffdf8;
  border: 1px solid var(--line);
  border-radius: 16px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: 0 8px 24px rgba(28, 23, 16, 0.06);
}
.page-card .thumb {
  aspect-ratio: 3 / 4;
  background: #ddd4c6;
  display: block;
  width: 100%;
  object-fit: cover;
  cursor: pointer;
}
.page-card .meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 8px 8px 12px;
  font-size: 12px;
  color: var(--ink-2);
}
.page-card .ops { display: flex; gap: 2px; }
.icon-btn {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  color: var(--ink-2);
}
.icon-btn:hover { background: var(--paper); }
.icon-btn svg { width: 16px; height: 16px; stroke: currentColor; fill: none; stroke-width: 1.8; }

.dock {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: calc(12px + var(--safe-b));
  display: grid;
  grid-template-columns: 1fr 1fr 1.2fr;
  gap: 8px;
  padding: 10px;
  background: rgba(255, 253, 248, 0.9);
  border: 1px solid var(--line);
  border-radius: 20px;
  backdrop-filter: blur(16px);
  box-shadow: var(--shadow);
  z-index: 20;
}
.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 48px;
  border-radius: 14px;
  font-weight: 600;
  font-size: 15px;
}
.btn svg {
  width: 18px;
  height: 18px;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.7;
  stroke-linejoin: round;
  stroke-linecap: round;
}
.btn.ghost { background: var(--paper); }
.btn.primary {
  background: var(--ink);
  color: #f7f1e6;
}
.btn.primary:hover { background: #2a241c; }
.btn.block { width: 100%; }

.editor {
  position: fixed;
  inset: 0;
  background: var(--dark);
  z-index: 40;
  display: flex;
  flex-direction: column;
  padding-top: var(--safe-t);
  color: #f4eee4;
}
.editor-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 8px 4px;
}
.text-btn {
  padding: 10px 14px;
  color: #cfc4b4;
  font-size: 15px;
}
.text-btn.strong { color: #fff; font-weight: 700; }
.text-btn.block { width: 100%; margin-top: 6px; color: var(--muted); }

.crop-wrap {
  flex: 1;
  position: relative;
  margin: 4px 12px;
  min-height: 0;
  touch-action: none;
}
#cropCanvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}
.crop-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
}
.crop-svg .poly {
  fill: rgba(20, 17, 14, 0.28);
  fill-rule: evenodd;
  stroke: none;
  pointer-events: none;
}
.crop-svg .edge {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2.5;
  vector-effect: non-scaling-stroke;
  pointer-events: none;
}
.crop-svg .handle-hit {
  fill: transparent;
  stroke: none;
  pointer-events: all;
}
.crop-svg .handle {
  fill: var(--handle);
  stroke: var(--accent);
  stroke-width: 3;
  vector-effect: non-scaling-stroke;
  pointer-events: none;
}

.editor-tools {
  padding: 8px 16px calc(18px + var(--safe-b));
}
.filters {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 10px;
}
.filters button, .chip {
  height: 40px;
  border-radius: 12px;
  background: #24201b;
  color: #d8cfc2;
  font-size: 14px;
}
.filters button.on {
  background: var(--accent);
  color: #fff;
  font-weight: 700;
}
.tool-row { display: flex; gap: 8px; }
.chip { flex: 1; }

.sheet {
  position: fixed;
  inset: 0;
  z-index: 50;
}
.sheet-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(20, 17, 14, 0.45);
}
.sheet-card {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--paper-2);
  border-radius: 24px 24px 0 0;
  padding: 10px 20px calc(20px + var(--safe-b));
  box-shadow: 0 -12px 40px rgba(28, 23, 16, 0.18);
}
.sheet-handle {
  width: 40px;
  height: 4px;
  border-radius: 4px;
  background: var(--line);
  margin: 4px auto 14px;
}
.sheet-card h3 { font-size: 20px; margin-bottom: 16px; }
.field { display: block; margin-bottom: 14px; }
.field span, .seg > span {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 6px;
}
.field input {
  width: 100%;
  height: 46px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #fffdf8;
  padding: 0 12px;
  outline: none;
}
.field input:focus { border-color: var(--ink); }
.seg { margin-bottom: 14px; }
.seg-btns { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.seg-btns button {
  height: 42px;
  border-radius: 12px;
  background: #fffdf8;
  border: 1px solid var(--line);
}
.seg-btns button.on {
  background: var(--ink);
  color: #f7f1e6;
  border-color: var(--ink);
}
.hint { font-size: 12px; color: var(--muted); margin: 4px 0 14px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(96px + var(--safe-b));
  transform: translateX(-50%);
  background: var(--ink);
  color: #f7f1e6;
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 13px;
  z-index: 80;
  white-space: nowrap;
}

.busy {
  position: fixed;
  inset: 0;
  background: rgba(20, 17, 14, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 90;
}
.busy-card {
  background: #fffdf8;
  border-radius: 18px;
  padding: 22px 28px;
  text-align: center;
  box-shadow: var(--shadow);
}
.spinner {
  width: 28px;
  height: 28px;
  border: 3px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  margin: 0 auto 10px;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (min-width: 720px) {
  #app { max-width: 760px; margin: 0 auto; }
  .dock { max-width: 736px; left: 50%; right: auto; transform: translateX(-50%); width: calc(100% - 24px); }
  .pages { grid-template-columns: repeat(3, 1fr); }
}
