:root {
  --bg: #f3f1ec;
  --surface: #ffffff;
  --ink: #1d221e;
  --muted: #6b716c;
  --line: #dcd8cf;
  --brand: #1f5130;
  --brand-ink: #ffffff;
  --brand-soft: #e3eee5;
  --ok: #1f7a3d;
  --ok-soft: #dff1e4;
  --warn: #9a6200;
  --warn-soft: #fbefd6;
  --bad: #b3261e;
  --bad-soft: #fbe3e1;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 4px 14px rgba(0, 0, 0, 0.06);
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font-size: 18px;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
[hidden] { display: none !important; }
h1, h2 { margin: 0; }
h2 { font-size: 20px; }
.muted { color: var(--muted); font-weight: normal; }
.hint { color: var(--muted); margin: 12px 0 0; line-height: 1.4; }
.hint.small { font-size: 15px; }
.error { color: var(--bad); font-weight: 600; }

/* Buttons */
.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: 17px;
  font-weight: 600;
  padding: 12px 18px;
  min-height: 52px;
  border-radius: 12px;
  cursor: pointer;
  touch-action: manipulation;
}
.btn:active { transform: translateY(1px); filter: brightness(0.96); }
.btn:disabled { opacity: 0.5; }
.btn.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.btn.big { font-size: 20px; min-height: 60px; width: 100%; }
.btn.huge { font-size: 22px; min-height: 72px; width: 100%; margin-top: 16px; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.danger { color: var(--bad); }
.btn.link { background: none; border: none; color: var(--brand); text-decoration: underline; padding: 8px; min-height: 0; font-weight: 500; }
.btn.small { font-size: 14px; }
.btn.icon { font-size: 24px; min-width: 56px; padding: 8px; }

/* Setup */
#setup { display: grid; place-items: center; min-height: 100%; padding: 24px; }
.setup-card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 32px; width: min(460px, 100%); }
.setup-card form { display: grid; gap: 12px; margin-top: 16px; }
input, textarea {
  font: inherit;
  font-size: 18px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fff;
  color: var(--ink);
  width: 100%;
}
textarea { resize: none; }

/* Home */
#home { min-height: 100%; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 20px; background: var(--brand); color: var(--brand-ink);
  padding-top: max(14px, env(safe-area-inset-top));
}
.brand { font-size: 22px; display: flex; gap: 8px; align-items: baseline; }
.brand span { opacity: 0.8; font-size: 18px; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.chip { font-size: 14px; font-weight: 600; padding: 6px 12px; border-radius: 999px; background: rgba(255,255,255,0.15); white-space: nowrap; }
.chip.ok { background: var(--ok-soft); color: var(--ok); }
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.chip.bad { background: var(--bad-soft); color: var(--bad); }

.home-main {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1.5fr) minmax(300px, 1fr);
  gap: 20px;
  padding: 20px;
  align-items: start;
}
@media (max-width: 860px) { .home-main { grid-template-columns: 1fr; } }

.compose, .history { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; }

.capture-hero { text-align: center; padding: 24px 8px; }
.capture-btn {
  width: 100%; min-height: 260px; border-radius: 20px; border: 3px dashed var(--brand);
  background: var(--brand-soft); color: var(--brand); font: inherit; font-size: 28px; font-weight: 700;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px; cursor: pointer;
}
.capture-btn svg { width: 96px; height: 96px; }
.capture-btn:active { filter: brightness(0.95); }

.draft-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.pages { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 14px; }
.page {
  position: relative; border: 1px solid var(--line); border-radius: 12px; background: #fafaf8; overflow: hidden;
  display: flex; flex-direction: column;
}
.page img { width: 100%; aspect-ratio: 0.72; object-fit: contain; background: #eceae4; display: block; }
.page .num { position: absolute; top: 6px; left: 6px; background: var(--brand); color: #fff; font-weight: 700; font-size: 14px; border-radius: 999px; padding: 2px 9px; }
.page .tools { display: flex; justify-content: space-between; border-top: 1px solid var(--line); }
.page .tools button { flex: 1; border: none; background: none; font-size: 20px; min-height: 48px; cursor: pointer; color: var(--ink); }
.page .tools button.del { color: var(--bad); }
.page.add {
  border: 2px dashed var(--brand); color: var(--brand); background: var(--brand-soft); font-size: 18px; font-weight: 700;
  align-items: center; justify-content: center; min-height: 220px; cursor: pointer; gap: 8px; font-family: inherit;
}
.page.add b { font-size: 44px; line-height: 1; }
.note-label { display: block; margin: 18px 0 6px; font-weight: 600; }

/* History */
.history h2 { margin-bottom: 12px; }
.history-list { display: grid; gap: 10px; }
.history-empty { color: var(--muted); padding: 16px 0; }
.entry { display: grid; grid-template-columns: 64px 1fr; gap: 12px; padding: 10px; border: 1px solid var(--line); border-radius: 12px; align-items: center; }
.entry img { width: 64px; height: 84px; object-fit: cover; border-radius: 6px; background: #eceae4; }
.entry .title { font-weight: 700; }
.entry .sub { color: var(--muted); font-size: 15px; margin-top: 2px; }
.steps { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.step { font-size: 14px; font-weight: 600; padding: 4px 10px; border-radius: 999px; background: #eeece6; color: var(--muted); }
.step.ok { background: var(--ok-soft); color: var(--ok); }
.step.warn { background: var(--warn-soft); color: var(--warn); }
.step.bad { background: var(--bad-soft); color: var(--bad); }
.entry .actions { display: flex; gap: 8px; margin-top: 8px; }
.entry .actions .btn { min-height: 40px; padding: 6px 12px; font-size: 15px; }
.entry.pending { border-color: var(--warn); background: #fffaf0; }
.entry.failed { border-color: var(--bad); background: #fff6f5; }

.foot { text-align: center; color: var(--muted); font-size: 13px; padding: 8px 0 16px; }

/* Editor */
.editor { position: fixed; inset: 0; background: #111; color: #fff; display: flex; flex-direction: column; z-index: 10; }
.editor-stage { flex: 1; position: relative; overflow: hidden; display: grid; place-items: center; touch-action: none; }
#crop-canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
#preview-img { position: absolute; inset: 12px; width: calc(100% - 24px); height: calc(100% - 24px); object-fit: contain; filter: drop-shadow(0 4px 24px rgba(0,0,0,0.6)); }
.editor-bar {
  display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: center;
  padding: 12px 16px; padding-bottom: max(12px, env(safe-area-inset-bottom)); background: #1c1c1c;
}
.editor-bar .btn { background: #2c2c2c; color: #fff; border-color: #3a3a3a; }
.editor-bar .btn.primary { background: #2f8a4b; border-color: #2f8a4b; font-size: 19px; padding: 12px 26px; }
.bar-hint { color: #bbb; font-size: 15px; flex: 1 1 200px; text-align: center; }
.segmented { display: inline-flex; border-radius: 12px; overflow: hidden; border: 1px solid #3a3a3a; }
.segmented button { background: #2c2c2c; color: #ddd; border: none; font: inherit; font-size: 16px; font-weight: 600; padding: 0 16px; min-height: 52px; cursor: pointer; }
.segmented button + button { border-left: 1px solid #3a3a3a; }
.segmented button.active { background: #e9e9e9; color: #111; }
.editor-warning { margin: 0; padding: 8px 16px; background: var(--warn-soft); color: var(--warn); text-align: center; font-weight: 600; }

.loupe { position: absolute; width: 160px; height: 160px; border-radius: 50%; overflow: hidden; border: 3px solid #fff; box-shadow: 0 4px 20px rgba(0,0,0,0.5); pointer-events: none; left: 16px; top: 16px; }
.busy { position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 16px; background: rgba(0,0,0,0.55); font-size: 20px; }
.spinner { width: 56px; height: 56px; border-radius: 50%; border: 5px solid rgba(255,255,255,0.25); border-top-color: #fff; animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%); z-index: 50;
  background: #222; color: #fff; padding: 14px 22px; border-radius: 12px; font-weight: 600; box-shadow: var(--shadow);
  max-width: min(90vw, 640px); text-align: center;
}
.toast.bad { background: var(--bad); }
.toast.ok { background: var(--ok); }
