:root {
  --paper: #f3ede2;
  --paper-2: #ebe3d4;
  --card: #fbf8f2;
  --ink: #2a221c;
  --ink-2: #6b5d50;
  --line: #d8ccb8;
  --accent: #8a4b2a;
  --accent-2: #b0683c;
  --accent-ink: #fffaf3;
  --gold: #c08a3e;
  --ok: #4f7a4a;
  --warn: #a9542f;
  --stage: #2b241f;
  --shadow: 0 1px 2px rgba(60, 40, 20, .08), 0 6px 18px rgba(60, 40, 20, .08);
  --radius: 16px;
  --serif: "Songti SC", "STSong", "Noto Serif SC", "Source Han Serif SC", Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #1d1915; --paper-2: #26201b; --card: #2a241e; --ink: #f1e8da; --ink-2: #b4a592;
    --line: #43392f; --accent: #d08a5c; --accent-2: #e2a074; --accent-ink: #1d1915; --stage: #110e0b;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 6px 18px rgba(0,0,0,.3);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --paper: #1d1915; --paper-2: #26201b; --card: #2a241e; --ink: #f1e8da; --ink-2: #b4a592;
  --line: #43392f; --accent: #d08a5c; --accent-2: #e2a074; --accent-ink: #1d1915; --stage: #110e0b;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
html, body { margin: 0; overflow-x: hidden; }
body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.5;
  min-height: 100dvh;
  padding-bottom: calc(96px + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--paper) 92%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { flex: 1; min-width: 0; }
.brand h1 { font-family: var(--serif); font-size: 21px; margin: 0; letter-spacing: .04em; line-height: 1.2; }
.brand .sub { font-size: 12px; color: var(--ink-2); letter-spacing: .08em; }
.icon-btn {
  width: 44px; height: 44px; border-radius: 12px; border: 0; background: transparent;
  display: inline-grid; place-items: center; cursor: pointer;
}
.icon-btn:active { background: var(--paper-2); }
.icon-btn:disabled { opacity: .3; }
.lang-btn {
  min-width: 44px; height: 36px; padding: 0 10px; border-radius: 18px;
  border: 1px solid var(--line); background: var(--card); font-size: 13px; font-weight: 600; cursor: pointer;
}

main { max-width: 640px; margin: 0 auto; padding: 0 16px; }
.screen { padding-top: 16px; }

.hero .lede { font-family: var(--serif); font-size: 17px; margin: 4px 0 18px; color: var(--ink); }
.big-actions { display: grid; gap: 12px; }
.big-btn {
  display: flex; align-items: center; gap: 14px;
  min-height: 72px; padding: 0 22px; border-radius: var(--radius);
  background: var(--card); border: 1.5px solid var(--line); box-shadow: var(--shadow);
  font-size: 19px; font-weight: 600; cursor: pointer; user-select: none;
  transition: transform .12s ease;
}
.big-btn:active { transform: scale(.98); }
.big-btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.privacy { display: flex; gap: 6px; align-items: center; font-size: 13px; color: var(--ink-2); margin: 14px 2px 4px; }
.engine { display: flex; gap: 8px; align-items: center; font-size: 12px; color: var(--ink-2); margin: 2px 2px 0; }
.engine .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--gold); animation: pulse 1.2s infinite; flex: none; }
.engine[data-state="ready"] .dot { background: var(--ok); animation: none; }
.engine[data-state="error"] .dot { background: var(--warn); animation: none; }
@keyframes pulse { 50% { opacity: .3; } }

.section-head { display: flex; align-items: baseline; justify-content: space-between; margin: 26px 0 10px; }
.section-head h2 { font-family: var(--serif); font-size: 18px; margin: 0; }
.text-btn { border: 0; background: none; color: var(--accent); font-size: 15px; padding: 8px; cursor: pointer; }

.grid { list-style: none; padding: 0; margin: 0; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.grid li {
  position: relative; aspect-ratio: 1; border-radius: 10px; overflow: hidden;
  background: var(--paper-2); border: 1px solid var(--line); cursor: pointer;
}
.grid li img, .grid li canvas { width: 100%; height: 100%; object-fit: cover; display: block; }
.grid .badge {
  position: absolute; left: 6px; bottom: 6px; font-size: 11px; padding: 2px 7px; border-radius: 10px;
  background: rgba(30, 22, 16, .72); color: #fff;
}
.grid .badge.done { background: var(--ok); }
.grid .num { position: absolute; right: 6px; top: 6px; font-size: 11px; color: #fff; background: rgba(30,22,16,.55); border-radius: 8px; padding: 0 6px; }
.tips { font-size: 13px; color: var(--ink-2); margin-top: 8px; }

.howto { margin: 26px 0 0; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px 14px; }
.howto summary { cursor: pointer; font-weight: 600; min-height: 32px; display: flex; align-items: center; }
.howto ol { margin: 6px 0 4px; padding-left: 20px; color: var(--ink-2); font-size: 14px; }

.stepper { display: flex; gap: 8px; font-size: 13px; color: var(--ink-2); }
.stepper span { padding: 4px 10px; border-radius: 12px; background: var(--paper-2); }
.stepper span.on { background: var(--accent); color: var(--accent-ink); }
.hint { font-size: 14px; color: var(--ink-2); margin: 10px 0; }

.stage {
  position: relative; background: var(--stage); border-radius: 12px; overflow: hidden;
  touch-action: none; user-select: none; -webkit-user-select: none;
  display: flex; justify-content: center;
}
.stage canvas#cropCanvas { display: block; max-width: 100%; }
.quad-svg { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.handle {
  position: absolute; width: 48px; height: 48px; margin: -24px 0 0 -24px; border-radius: 50%;
  touch-action: none; cursor: grab; z-index: 3;
}
.handle::after {
  content: ""; position: absolute; inset: 12px; border-radius: 50%;
  background: rgba(255, 250, 240, .25); border: 3px solid #ffe6b8; box-shadow: 0 0 0 2px rgba(0,0,0,.45);
}
.handle.active::after { background: rgba(255, 210, 140, .55); }
.loupe {
  position: absolute; width: 120px; height: 120px; border-radius: 50%;
  border: 3px solid #fff; box-shadow: 0 4px 16px rgba(0,0,0,.5); pointer-events: none; z-index: 4; background: #000;
}
.row-btns { display: flex; gap: 10px; margin-top: 12px; }
.pill {
  min-height: 44px; padding: 0 16px; border-radius: 22px; border: 1px solid var(--line);
  background: var(--card); font-size: 15px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px;
}
.pill.sm { min-height: 38px; padding: 0 12px; font-size: 14px; }
.pill:active { background: var(--paper-2); }

.compare {
  position: relative; background: var(--stage); border-radius: 12px; overflow: hidden;
  touch-action: pan-y; user-select: none; -webkit-user-select: none; line-height: 0;
}
.compare canvas { display: block; width: 100%; height: auto; }
.compare #afterCanvas { position: absolute; inset: 0; }
.cmp-line { position: absolute; top: 0; bottom: 0; width: 2px; background: #fff; box-shadow: 0 0 4px rgba(0,0,0,.5); left: 50%; pointer-events: none; }
.cmp-knob {
  position: absolute; top: 50%; left: 50%; width: 36px; height: 36px; margin: -18px 0 0 -18px; border-radius: 50%;
  background: #fff; box-shadow: 0 2px 8px rgba(0,0,0,.4);
}
.cmp-knob::before { content: "‹ ›"; position: absolute; inset: 0; display: grid; place-items: center; color: #6b5d50; font: 600 15px/1 var(--sans); }
.cmp-tag { position: absolute; top: 8px; font-size: 12px; line-height: 1.4; padding: 2px 8px; border-radius: 10px; background: rgba(0,0,0,.55); color: #fff; }
.cmp-tag.left { left: 8px; } .cmp-tag.right { right: 8px; }
.busy { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(0,0,0,.15); }
.spinner { width: 30px; height: 30px; border-radius: 50%; border: 3px solid rgba(255,255,255,.35); border-top-color: #fff; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.dims { font-size: 12px; color: var(--ink-2); margin: 6px 2px 0; font-variant-numeric: tabular-nums; }

.panel { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 16px 6px; margin-top: 12px; box-shadow: var(--shadow); }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.panel-head h3 { margin: 0; font-family: var(--serif); font-size: 17px; }
.panel-actions { display: flex; gap: 8px; }
.slider { padding: 8px 0 6px; }
.slider label { display: flex; justify-content: space-between; font-size: 14px; }
.slider output { color: var(--ink-2); font-variant-numeric: tabular-nums; }
.slider input[type=range] { width: 100%; height: 36px; accent-color: var(--accent); margin: 0; }

.ai-box { margin-top: 18px; padding: 14px 16px; border: 1.5px dashed var(--line); border-radius: var(--radius); }
.ai-btn {
  width: 100%; min-height: 52px; border-radius: 14px; border: 1.5px solid var(--gold);
  background: var(--card); color: var(--ink); display: flex; align-items: center; justify-content: center; gap: 8px;
  font-size: 17px; font-weight: 600; cursor: pointer;
}
.ai-btn svg { color: var(--gold); }
.ai-btn:disabled { opacity: .55; cursor: default; }
.ai-note { font-size: 12px; color: var(--ink-2); margin: 8px 0 0; }
.ai-pass { margin-top: 12px; font-size: 14px; }
.ai-pass-row { display: flex; gap: 8px; margin-top: 6px; }
.ai-pass input { flex: 1; min-width: 0; min-height: 44px; border-radius: 12px; border: 1px solid var(--line); background: var(--card); color: var(--ink); padding: 0 12px; font-size: 16px; }
.ai-err { color: var(--warn); font-size: 13px; margin: 6px 0 0; }
.ai-progress { margin-top: 12px; }
.ai-progress .bar { height: 8px; border-radius: 4px; background: var(--paper-2); overflow: hidden; }
.ai-progress .bar span { display: block; height: 100%; width: 0; background: linear-gradient(90deg, var(--gold), var(--accent)); transition: width 1s linear; }
.ai-progress p { font-size: 13px; color: var(--ink-2); margin: 6px 0 0; font-variant-numeric: tabular-nums; }
#aiResult { margin-top: 12px; }
#aiCompare img { display: block; width: 100%; height: auto; }
#aiCompare img.over { position: absolute; inset: 0; height: 100%; object-fit: fill; }
.cmp-tag.ai-tag { background: var(--gold); color: #1d1408; font-weight: 700; }

.actionbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--paper) 94%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-top: 1px solid var(--line);
}
.nav-mini { display: flex; align-items: center; gap: 0; font-size: 13px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.cta {
  flex: 1; min-height: 56px; border-radius: 16px; border: 0; cursor: pointer;
  background: var(--accent); color: var(--accent-ink); font-size: 18px; font-weight: 700; letter-spacing: .02em;
}
.cta:active { background: var(--accent-2); }
.cta:disabled { opacity: .5; }
.cta.ghost { flex: 0 0 auto; padding: 0 16px; background: var(--card); color: var(--ink); border: 1px solid var(--line); font-size: 16px; font-weight: 600; }

.toast {
  position: fixed; left: 50%; bottom: calc(100px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  max-width: calc(100% - 32px); background: rgba(30, 22, 16, .92); color: #fff; padding: 10px 16px; border-radius: 12px;
  font-size: 14px; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 50;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
