:root {
  --bg: #f6f9fc;
  --bg-grad-1: #fafcfe;
  --bg-grad-2: #edf4fa;
  --card: #ffffff;
  --ink: #0d1440;
  --ink-soft: #5a6684;
  --line: #dfe7f0;
  --accent: #0084e8;
  --accent-dark: #00125e;
  --accent-soft: #e7f3fc;
  --green: #00c853;
  --green-soft: #e6f8ee;
  --danger: #bf3b2b;
  --warn-bg: #fff7e0;
  --warn-ink: #7a5c12;
  --radius: 14px;
  --shadow: 0 10px 30px rgba(13, 20, 64, 0.08);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", ui-sans-serif, system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  color: var(--ink);
  background: linear-gradient(160deg, var(--bg-grad-1), var(--bg-grad-2));
  min-height: 100vh;
  line-height: 1.5;
}

.site-header {
  max-width: 820px;
  margin: 0 auto;
  padding: 40px 20px 8px;
  text-align: center;
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 1.5rem;
  letter-spacing: -0.02em;
}
.brand-mark { font-size: 1.4rem; }
.brand-name { color: var(--accent-dark); }
.tagline {
  color: var(--ink-soft);
  margin: 10px auto 0;
  max-width: 540px;
}

main {
  max-width: 820px;
  margin: 0 auto;
  padding: 16px 20px 40px;
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px;
  margin-bottom: 22px;
}

.step-title {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 1.15rem;
  margin: 0 0 16px;
}
.step-num {
  display: inline-grid;
  place-items: center;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent-dark);
  font-size: 0.85rem;
  font-weight: 700;
}

/* Dropzone */
.dropzone {
  border: 2px dashed #b9cede;
  border-radius: var(--radius);
  background: #fbfdff;
  padding: 34px 20px;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, transform 0.05s;
}
.dropzone:hover { border-color: var(--accent); background: var(--accent-soft); }
.dropzone:focus-visible { outline: 3px solid var(--accent-soft); border-color: var(--accent); }
.dropzone.dragover { border-color: var(--accent); background: var(--accent-soft); transform: scale(1.005); }
.dropzone-icon { font-size: 2.2rem; color: var(--accent); }
.dropzone-primary { font-weight: 600; margin: 8px 0 2px; }
.dropzone-secondary { margin: 0; color: var(--ink-soft); }
.dropzone-hint { margin: 8px 0 0; font-size: 0.82rem; color: var(--ink-soft); }
.link-btn {
  background: none; border: none; padding: 0;
  color: var(--accent); font: inherit; font-weight: 600;
  cursor: pointer; text-decoration: underline;
}

/* Thumbnails */
.thumb-list {
  list-style: none;
  padding: 0;
  margin: 18px 0 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 12px;
}
.thumb {
  position: relative;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  background: #f3f8fc;
  aspect-ratio: 3 / 4;
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb .page-badge {
  position: absolute; top: 6px; left: 6px;
  background: rgba(31, 36, 48, 0.78); color: #fff;
  font-size: 0.72rem; padding: 2px 7px; border-radius: 20px;
}
.thumb .remove-thumb {
  position: absolute; top: 6px; right: 6px;
  width: 24px; height: 24px; border-radius: 50%;
  border: none; cursor: pointer;
  background: rgba(31, 36, 48, 0.78); color: #fff;
  font-size: 0.95rem; line-height: 1; display: grid; place-items: center;
}
.thumb .remove-thumb:hover { background: var(--danger); }

/* Buttons */
.actions { display: flex; gap: 10px; margin-top: 20px; align-items: center; }
.actions.wrap { flex-wrap: wrap; }
.btn {
  appearance: none;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 11px 18px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, box-shadow 0.15s, transform 0.05s, opacity 0.15s;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--accent-dark); color: #fff; }
.btn-primary:hover:not(:disabled) { background: #012182; }
.btn-secondary { background: #fff; color: var(--accent-dark); border-color: var(--line); }
.btn-secondary:hover:not(:disabled) { background: var(--accent-soft); }
.btn-ghost { background: transparent; color: var(--ink-soft); }
.btn-ghost:hover:not(:disabled) { color: var(--ink); }

/* Processing */
.processing-msg { text-align: center; color: var(--ink-soft); margin: 14px 0 0; }
.spinner {
  width: 42px; height: 42px; margin: 6px auto 0;
  border: 4px solid var(--accent-soft);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Result */
.result-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.result-head .step-title { margin-bottom: 0; }
.result-hint { color: var(--ink-soft); font-size: 0.9rem; margin: 12px 0 14px; }
.result-hint code { background: #e9f1f8; padding: 1px 5px; border-radius: 5px; font-size: 0.85em; }

.view-toggle { display: inline-flex; background: #e9f1f8; border-radius: 9px; padding: 3px; }
.toggle-btn {
  border: none; background: transparent; cursor: pointer;
  font: inherit; font-weight: 600; color: var(--ink-soft);
  padding: 6px 14px; border-radius: 7px;
}
.toggle-btn.active { background: #fff; color: var(--accent-dark); box-shadow: 0 1px 3px rgba(0,0,0,0.08); }

.document {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 28px 30px;
  min-height: 200px;
}
.preview.document h1 { font-size: 1.5rem; margin: 0.2em 0 0.4em; }
.preview.document h2 { font-size: 1.2rem; margin: 1em 0 0.4em; border-bottom: 1px solid var(--line); padding-bottom: 4px; }
.preview.document h3 { font-size: 1.05rem; margin: 0.9em 0 0.3em; }
.preview.document p { margin: 0.5em 0; }
.preview.document ul, .preview.document ol { margin: 0.5em 0; padding-left: 1.4em; }
.preview.document li { margin: 0.2em 0; }
.preview.document hr { border: none; border-top: 1px solid var(--line); margin: 1.4em 0; }
.preview.document table { border-collapse: collapse; margin: 0.6em 0; width: 100%; }
.preview.document th, .preview.document td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; }
.preview.document .unsure { background: #fdeede; border-radius: 4px; padding: 0 3px; }
.preview.document .illegible { color: var(--danger); font-style: italic; }

.edit-pane {
  width: 100%;
  min-height: 360px;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 18px 20px;
  font-family: ui-monospace, "SF Mono", "Cascadia Code", Menlo, Consolas, monospace;
  font-size: 0.92rem;
  line-height: 1.6;
  resize: vertical;
  color: var(--ink);
  background: #fbfdff;
}

.title-input {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  font: inherit;
  flex: 1 1 160px;
  min-width: 140px;
  background: #fbfdff;
}

/* Banner */
.banner {
  border-radius: 10px;
  padding: 12px 16px;
  margin-bottom: 18px;
  font-size: 0.92rem;
}
.banner.warn { background: var(--warn-bg); color: var(--warn-ink); border: 1px solid #ecd9a0; }
.banner.error { background: #fbe6e0; color: var(--danger); border: 1px solid #f0c4b8; }
.banner.info { background: var(--accent-soft); color: #0b57a4; border: 1px solid #cde4f8; }

.site-footer { text-align: center; color: var(--ink-soft); font-size: 0.82rem; padding: 0 20px 36px; }

.hidden { display: none !important; }
.muted { color: var(--ink-soft); }
.small { font-size: 0.88rem; }

/* Mode tabs */
.mode-tabs {
  max-width: 820px;
  margin: 6px auto 0;
  padding: 0 20px;
  display: flex;
  gap: 8px;
}
.mode-tab {
  border: 1px solid var(--line);
  border-bottom: none;
  background: #eaf1f8;
  color: var(--ink-soft);
  font: inherit;
  font-weight: 600;
  padding: 10px 18px;
  border-radius: 12px 12px 0 0;
  cursor: pointer;
}
.mode-tab.active { background: var(--card); color: var(--accent-dark); box-shadow: 0 -2px 8px rgba(0,0,0,0.04); }

/* Detected badge */
.detect-badge { font-size: 0.85rem; font-weight: 600; padding: 5px 12px; border-radius: 20px; }
.detect-badge.citizens { background: var(--green-soft); color: #067a3e; }
.detect-badge.merrick { background: #e9eefb; color: #2f3e7a; }
.detect-badge.unknown { background: var(--warn-bg); color: var(--warn-ink); }

/* Review form */
.review-section { border: 1px solid var(--line); border-radius: 10px; margin: 14px 0; overflow: hidden; }
.review-section > summary {
  cursor: pointer; padding: 12px 16px; font-weight: 700; background: #f3f8fc;
  list-style: none; display: flex; align-items: center; justify-content: space-between;
}
.review-section > summary::-webkit-details-marker { display: none; }
.review-section > summary::after { content: "▾"; color: var(--ink-soft); }
.review-section[open] > summary::after { content: "▴"; }
.section-body { padding: 14px 16px; }

.field-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 0.82rem; color: var(--ink-soft); }
.field > span { font-weight: 600; }
.field input, .field select {
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px;
  font-size: 0.95rem; color: var(--ink); background: #fbfdff; font-family: inherit;
}
.field input:focus, .field select:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.field.empty input { background: #fffaeb; border-color: #ecd9a0; }
.checkfield { flex-direction: row; align-items: center; gap: 8px; font-size: 0.92rem; color: var(--ink); }

.generate-actions { margin-top: 18px; border-top: 1px solid var(--line); padding-top: 18px; }

/* Menu table */
.table-wrap { overflow-x: auto; margin: 12px 0; }
.menu-table { border-collapse: collapse; width: 100%; font-size: 0.92rem; }
.menu-table th, .menu-table td { border: 1px solid var(--line); padding: 4px; text-align: left; }
.menu-table th { background: #f3f8fc; font-size: 0.8rem; }
.menu-table input { width: 100%; border: none; padding: 6px; font: inherit; background: transparent; }
.menu-table input:focus { outline: 2px solid var(--accent-soft); border-radius: 4px; }
.menu-table td.price-col { width: 90px; }
.menu-table td.cat-col { width: 150px; }
.row-del { background: none; border: none; color: var(--danger); cursor: pointer; font-size: 1.1rem; padding: 0 6px; }

@media (max-width: 560px) {
  .card { padding: 18px; }
  .actions { flex-wrap: wrap; }
  .document { padding: 20px 18px; }
}

/* ---------------- toolbar (rep / manual form / settings) ---------------- */
.toolbar { display: flex; align-items: flex-end; gap: 14px; flex-wrap: wrap; margin-top: 14px; }
.tool-field { display: flex; flex-direction: column; gap: 4px; font-size: 0.78rem; color: var(--ink-soft); }
.tool-field > span { font-weight: 600; }
.tool-field select {
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; min-width: 180px;
  font-size: 0.92rem; color: var(--ink); background: #fbfdff; font-family: inherit;
}
.tool-field select:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 38px; height: 38px; border-radius: 10px; border: 1px solid var(--line);
  background: #fbfdff; color: var(--accent-dark); cursor: pointer;
}
.icon-btn:hover { background: var(--accent-soft); border-color: var(--accent); }

/* ---------------- review extras ---------------- */
.review-topline { margin: 14px 0 4px; }
.sub-head {
  grid-column: 1 / -1; font-size: 0.78rem; font-weight: 700; letter-spacing: 0.04em;
  text-transform: uppercase; color: var(--accent-dark); border-bottom: 1px solid var(--line);
  padding-bottom: 4px; margin-top: 6px;
}
.sig-row { display: flex; align-items: center; gap: 10px; }
.sig-btn.signed { border-color: #9adfb6; color: #067a3e; background: var(--green-soft); }

.kinds {
  border: 1px solid var(--line); border-radius: 10px; margin: 18px 0 0; padding: 12px 16px 14px;
  display: flex; flex-wrap: wrap; gap: 10px 18px; background: #f3f8fc;
}
.kinds legend { font-weight: 700; font-size: 0.9rem; padding: 0 6px; color: var(--accent-dark); }
.kinds label { display: inline-flex; align-items: center; gap: 7px; font-size: 0.92rem; cursor: pointer; }
.kinds input { accent-color: var(--accent); width: 16px; height: 16px; }

/* ---------------- modals ---------------- */
.modal {
  position: fixed; inset: 0; z-index: 1000; display: flex; align-items: center; justify-content: center;
  background: rgba(30, 34, 30, 0.45); padding: 20px;
  -webkit-backdrop-filter: blur(3px); backdrop-filter: blur(3px);
}
.modal-card {
  background: var(--card); border-radius: 14px; padding: 22px 24px; width: 100%; max-width: 460px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.25); display: flex; flex-direction: column; gap: 14px;
}
.modal-title { margin: 0; font-size: 1.15rem; color: var(--accent-dark); }
.modal-note { margin: 0; font-size: 0.8rem; color: var(--ink-soft); }
.key-row { display: flex; gap: 8px; align-items: stretch; }
.key-row input { flex: 1; }
.key-row .icon-btn { width: 42px; height: auto; }
.sig-card { max-width: 620px; }
.sig-canvas-wrap {
  border: 1.5px dashed var(--line); border-radius: 10px; background: #fbfdff; padding: 2px;
  display: flex; justify-content: center;
}
#sigCanvas { touch-action: none; cursor: crosshair; border-radius: 8px; }

/* ---------------- history ---------------- */
.hist-card { margin-top: 26px; }
.hist-note { font-size: 0.78rem; color: var(--ink-soft); font-weight: 600; }
.hist-controls { display: flex; flex-wrap: wrap; gap: 10px; margin: 12px 0 4px; align-items: center; }
.hist-controls input[type="search"] {
  flex: 1; min-width: 160px; border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px;
  font: inherit; font-size: 0.92rem; background: #fbfdff; color: var(--ink);
}
.hist-controls input[type="search"]:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.hist-controls select {
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font: inherit;
  font-size: 0.9rem; background: #fbfdff; color: var(--ink);
}
.hist-empty { color: var(--ink-soft); font-size: 0.88rem; margin: 12px 2px; }
.hist-list { list-style: none; margin: 10px 0 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.hist-row {
  display: flex; align-items: center; gap: 8px; border: 1px solid var(--line); border-radius: 10px;
  background: #fbfdff; padding: 4px 8px 4px 4px;
}
.hist-row:hover { border-color: var(--accent); }
.hist-main {
  flex: 1; display: flex; flex-direction: column; align-items: flex-start; gap: 2px; text-align: left;
  background: none; border: none; font: inherit; cursor: pointer; padding: 8px 10px; border-radius: 8px;
}
.hist-main:hover { background: var(--accent-soft); }
.hist-name { font-weight: 700; color: var(--ink); }
.hist-meta { font-size: 0.78rem; color: var(--ink-soft); }
.hist-actions { display: flex; gap: 2px; }
.hist-dl, .hist-del {
  background: none; border: none; cursor: pointer; padding: 8px; border-radius: 8px; color: var(--ink-soft);
  display: inline-flex; align-items: center;
}
.hist-dl:hover { color: var(--accent-dark); background: var(--accent-soft); }
.hist-del:hover { color: var(--danger); background: #fbe6e0; }

/* ---------------- detect badge variants ---------------- */
.detect-badge.fd_north { background: #eef0f7; color: #33406e; }
.detect-badge.pbt { background: #f2ecf7; color: #59396e; }
.detect-badge.manual { background: #eaf1f8; color: var(--ink-soft); }
