:root {
  --bg: #f4f3ef;
  --surface: #ffffff;
  --text: #1c1f1e;
  --muted: #5f6663;
  --line: #dcdcd6;
  --accent: #16574a;
  --accent-ink: #ffffff;
  --accent-soft: #e2eeea;
  --danger: #b3261e;
  --editor-bg: #111413;
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121514;
    --surface: #1c201f;
    --text: #e9ecea;
    --muted: #9aa39f;
    --line: #313836;
    --accent: #5fbfa6;
    --accent-ink: #08201a;
    --accent-soft: #1f3530;
    --danger: #f2b8b5;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  min-height: 100dvh;
}
[hidden] { display: none !important; }
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(12px + env(safe-area-inset-top)) 16px 12px;
  background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.topbar h1 { font-size: 1.15rem; margin: 0; letter-spacing: -0.01em; }
.status { font-size: .8rem; padding: 4px 10px; border-radius: 99px; background: var(--accent-soft); color: var(--accent); white-space: nowrap; }
.status.loading::before { content: ""; display: inline-block; width: 8px; height: 8px; margin-right: 6px; border-radius: 50%; background: currentColor; animation: pulse 1s ease-in-out infinite; }
.status.error { background: #fde7e6; color: var(--danger); }
@keyframes pulse { 50% { opacity: .25; } }

main { max-width: 760px; margin: 0 auto; padding: 16px 16px calc(32px + env(safe-area-inset-bottom)); }
h2 { font-size: 1.05rem; margin: 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 0 18px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  font-weight: 600; user-select: none; -webkit-user-select: none;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.big { min-height: 56px; font-size: 1.02rem; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.icon { width: 52px; padding: 0; }
.btn:disabled { opacity: .5; pointer-events: none; }
.btn input[type=file] { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
label.btn { position: relative; cursor: pointer; }
.ic { width: 22px; height: 22px; flex: none; }
.ic.sm { width: 16px; height: 16px; vertical-align: -3px; }
.link-btn { background: none; border: 0; color: var(--danger); padding: 8px 4px; font-weight: 600; min-height: 44px; }

.add-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.empty { text-align: center; color: var(--muted); padding: 40px 12px 16px; }
.empty-title { font-size: 1.1rem; font-weight: 700; color: var(--text); margin: 0 0 6px; }
.privacy { font-size: .9rem; }

.section-head { display: flex; align-items: center; justify-content: space-between; margin-top: 22px; }
.count { color: var(--muted); font-weight: 500; }
.hint { color: var(--muted); font-size: .88rem; margin: 4px 0 10px; }

.pages { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 12px; }
.page {
  position: relative; background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  padding: 6px; touch-action: manipulation; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
.page .thumb { display: block; width: 100%; aspect-ratio: 3 / 4; object-fit: contain; background: #e9e9e4; border-radius: 6px; pointer-events: none; }
.page .num { position: absolute; left: 10px; bottom: 10px; background: rgba(0,0,0,.66); color: #fff; font-size: .78rem; font-weight: 700; padding: 1px 7px; border-radius: 99px; }
.page .del, .page .rot {
  position: absolute; top: -10px; width: 40px; height: 40px; border-radius: 50%;
  border: 1px solid var(--line); background: var(--surface); display: grid; place-items: center; padding: 0;
}
.page .del { right: -8px; color: var(--danger); }
.page .rot { right: 34px; }
.page .del .ic, .page .rot .ic { width: 16px; height: 16px; }
.page.working .thumb { opacity: .4; }
.page.sortable-ghost { opacity: .35; }
.page.sortable-chosen { box-shadow: 0 8px 24px rgba(0,0,0,.2); }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-top: 22px; }
.field { border: 0; margin: 14px 0 0; padding: 0; min-width: 0; }
.field legend, .legend { display: block; font-weight: 600; font-size: .9rem; margin-bottom: 6px; padding: 0; }
.seg { display: flex; background: var(--bg); border: 1px solid var(--line); border-radius: 12px; padding: 3px; gap: 3px; }
.seg label { flex: 1; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span, .seg > button {
  display: flex; align-items: center; justify-content: center; min-height: 44px; border-radius: 9px;
  font-weight: 600; font-size: .95rem; text-align: center; padding: 0 6px; cursor: pointer; border: 0; background: none; width: 100%;
}
.seg input:checked + span { background: var(--surface); box-shadow: 0 1px 3px rgba(0,0,0,.14); color: var(--accent); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); }
.filename { display: flex; align-items: center; border: 1px solid var(--line); border-radius: 12px; background: var(--bg); padding-right: 12px; }
.filename input { flex: 1; min-width: 0; border: 0; background: none; padding: 12px; min-height: 48px; font-size: 16px; }
.filename span { color: var(--muted); }
.estimate { margin: 16px 0 12px; color: var(--muted); }
.estimate strong { color: var(--text); }
.export-row { display: grid; grid-template-columns: 1fr; gap: 10px; }
.export-row:has(#shareBtn:not([hidden])) { grid-template-columns: 1fr 1fr; }

/* ---------- editor ---------- */
.editor {
  position: fixed; inset: 0; z-index: 20; background: var(--editor-bg); color: #eef1ef;
  display: flex; flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.ed-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 10px; }
.ed-top .btn.ghost { color: #eef1ef; }
.editor .seg.tabs { background: #252a28; border-color: #333a37; flex: 0 1 220px; }
.editor .seg.tabs button { color: #b9c2be; }
.editor .seg.tabs button[aria-selected=true] { background: #3a423f; color: #fff; }
.stage { position: relative; flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; overflow: hidden; margin: 4px 10px; }
.wrap { position: relative; line-height: 0; }
.wrap canvas { display: block; }
#quadSvg { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; overflow: visible; }
#quadSvg .shade { fill: rgba(0,0,0,.45); fill-rule: evenodd; pointer-events: none; }
#quadSvg .edge { fill: rgba(95,191,166,.10); stroke: #5fbfa6; pointer-events: none; }
#quadSvg .hit { fill: transparent; cursor: grab; }
#quadSvg .knob { fill: rgba(255,255,255,.95); stroke: #16574a; pointer-events: none; }
#quadSvg .knob.active { fill: #5fbfa6; }
.loupe { position: absolute; top: 8px; width: 130px; height: 130px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 4px 16px rgba(0,0,0,.5); pointer-events: none; background: #000; }
.stage-busy { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(17,20,19,.35); }
.ed-bottom { padding: 8px 10px 12px; }
.tools { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.editor .btn { background: #252a28; border-color: #333a37; color: #eef1ef; }
.editor .btn.primary { background: #5fbfa6; border-color: #5fbfa6; color: #08201a; }
.modes { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; flex: 1 1 100%; max-width: 520px; }
.modes button { min-height: 48px; border-radius: 10px; border: 1px solid #333a37; background: #252a28; color: #dfe5e2; font-weight: 600; font-size: .9rem; padding: 0 4px; }
.modes button[aria-pressed=true] { background: #5fbfa6; border-color: #5fbfa6; color: #08201a; }
.rot { display: flex; gap: 10px; }
.ed-hint { text-align: center; color: #9aa39f; font-size: .85rem; margin: 8px 0 0; min-height: 1.2em; }

.busy { position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,.45); display: grid; place-items: center; }
.busy-box { background: var(--surface); color: var(--text); padding: 22px 26px; border-radius: 16px; text-align: center; min-width: 200px; }
.busy-box p { margin: 12px 0 0; }
.spinner { width: 34px; height: 34px; margin: 0 auto; border-radius: 50%; border: 4px solid rgba(127,127,127,.3); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed; left: 50%; bottom: calc(18px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 50;
  background: #222826; color: #fff; padding: 10px 10px 10px 16px; border-radius: 12px; display: flex; align-items: center; gap: 12px;
  max-width: calc(100vw - 32px); box-shadow: 0 6px 20px rgba(0,0,0,.3);
}
.toast button { background: none; border: 0; color: #7fd8c0; font-weight: 700; min-height: 40px; padding: 0 8px; }

@media (min-width: 700px) {
  .export-row, .export-row:has(#shareBtn:not([hidden])) { grid-template-columns: repeat(2, minmax(0, 240px)); }
}
