/* Filing Cabinet — mobile-first, paper-and-ink palette */
:root {
  --bg: #f6f4ef;
  --panel: #fffdf9;
  --ink: #23201b;
  --muted: #6d675d;
  --line: #ddd7ca;
  --accent: #8a5a2b;
  --accent-soft: #f0e6d8;
  --ok: #3f7d4e;
  --warn: #a8621b;
  --bad: #a33a2c;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(35, 32, 27, 0.06), 0 4px 14px rgba(35, 32, 27, 0.05);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1815;
    --panel: #232019;
    --ink: #ece7dd;
    --muted: #9b9488;
    --line: #383326;
    --accent: #d0a36a;
    --accent-soft: #322a1e;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 14px rgba(0, 0, 0, 0.25);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}

a { color: var(--accent); }

.wrap { max-width: 1000px; margin: 0 auto; padding: 0 16px 64px; }

/* ---- header ---- */
header.bar {
  position: sticky; top: 0; z-index: 20;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  box-shadow: var(--shadow);
}
.bar-in {
  max-width: 1000px; margin: 0 auto; padding: 10px 16px;
  display: flex; align-items: center; gap: 12px;
}
.brand {
  font-weight: 700; letter-spacing: -0.01em; font-size: 17px;
  color: var(--ink); text-decoration: none; display: flex; align-items: center; gap: 8px;
}
.brand svg { flex: none; }
.bar nav { margin-left: auto; display: flex; align-items: center; gap: 6px; }
.bar nav a, .bar nav button {
  font-size: 14px; padding: 7px 12px; border-radius: 8px;
  text-decoration: none; color: var(--muted);
  background: none; border: 0; cursor: pointer; font-family: inherit;
}
.bar nav a:hover, .bar nav button:hover { background: var(--accent-soft); color: var(--ink); }
.bar nav a.on { background: var(--accent-soft); color: var(--ink); font-weight: 600; }

/* ---- generic bits ---- */
h1 { font-size: 24px; margin: 24px 0 4px; letter-spacing: -0.02em; }
h2 { font-size: 16px; margin: 24px 0 10px; }
.sub { color: var(--muted); font-size: 14px; margin: 0 0 20px; }

.panel {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow);
}

label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 5px; color: var(--muted); }

input[type=text], input[type=password], input[type=date], input[type=search], select, textarea {
  width: 100%; padding: 10px 12px; font: inherit; font-size: 16px;
  border: 1px solid var(--line); border-radius: 8px;
  background: var(--bg); color: var(--ink);
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent;
}
textarea { resize: vertical; min-height: 70px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 11px 18px; font: inherit; font-size: 15px; font-weight: 600;
  border: 1px solid var(--accent); border-radius: 9px;
  background: var(--accent); color: #fff; cursor: pointer; text-decoration: none;
}
.btn:hover { filter: brightness(1.08); }
.btn.ghost { background: none; color: var(--accent); }
.btn.ghost:hover { background: var(--accent-soft); }
.btn.danger { background: none; border-color: var(--bad); color: var(--bad); }
.btn.danger:hover { background: var(--bad); color: #fff; }
.btn.big { width: 100%; padding: 16px; font-size: 17px; }
.btn:disabled { opacity: 0.55; cursor: default; filter: none; }

.row { display: flex; gap: 10px; flex-wrap: wrap; }
.grow { flex: 1 1 180px; }

.note { font-size: 13px; color: var(--muted); }
.err {
  background: #fbeae7; border: 1px solid #e8c4bd; color: #7d2c20;
  padding: 10px 12px; border-radius: 8px; font-size: 14px; margin-bottom: 14px;
}
@media (prefers-color-scheme: dark) {
  .err { background: #3a1f1a; border-color: #5c3128; color: #f0b6ab; }
}

/* ---- search ---- */
.search { margin: 18px 0; }
.search .main { display: flex; gap: 8px; margin-bottom: 10px; }
.search .main input { flex: 1; }
details.filters summary {
  cursor: pointer; font-size: 14px; color: var(--muted); padding: 4px 0;
  list-style: none;
}
details.filters summary::-webkit-details-marker { display: none; }
details.filters summary::before { content: "⌄ "; }
details.filters[open] summary::before { content: "⌃ "; }
.filters .grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px; margin-top: 10px;
}

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0 0; }
.chip {
  font-size: 13px; padding: 5px 10px; border-radius: 999px;
  background: var(--accent-soft); color: var(--ink);
  text-decoration: none; border: 1px solid transparent;
}
.chip:hover { border-color: var(--accent); }
.chip .n { color: var(--muted); font-size: 11px; margin-left: 4px; }

/* ---- results ---- */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; text-decoration: none; color: inherit;
  display: flex; flex-direction: column; box-shadow: var(--shadow);
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}
.card:hover { transform: translateY(-2px); box-shadow: 0 4px 20px rgba(35, 32, 27, 0.12); }
.card .shot {
  aspect-ratio: 3 / 4; background: var(--accent-soft) center/cover no-repeat;
  border-bottom: 1px solid var(--line);
  display: flex; align-items: center; justify-content: center;
  color: var(--muted); font-size: 13px;
}
.card .shot img { width: 100%; height: 100%; object-fit: cover; display: block; }
.card .meta { padding: 11px 12px; }
.card .t { font-weight: 600; font-size: 14px; line-height: 1.35; margin-bottom: 4px; }
.card .s { font-size: 12.5px; color: var(--muted); }

.badge {
  display: inline-block; font-size: 11px; font-weight: 600; text-transform: uppercase;
  letter-spacing: 0.04em; padding: 2px 7px; border-radius: 5px;
  background: var(--accent-soft); color: var(--accent);
}
.badge.pending { background: #fdf0dd; color: var(--warn); }
.badge.failed { background: #fbeae7; color: var(--bad); }

.empty { text-align: center; padding: 56px 20px; color: var(--muted); }
.empty .big { font-size: 40px; margin-bottom: 10px; }

.banner {
  background: var(--accent-soft); border: 1px solid var(--line);
  border-radius: 8px; padding: 10px 14px; font-size: 14px; margin-bottom: 16px;
}

/* ---- document detail ---- */
.doc-grid { display: grid; grid-template-columns: 1fr; gap: 20px; }
@media (min-width: 760px) { .doc-grid { grid-template-columns: 1.4fr 1fr; } }
.pageshot {
  width: 100%; border: 1px solid var(--line); border-radius: 8px;
  display: block; margin-bottom: 12px; background: #fff;
}
.kv { font-size: 14px; }
.kv div { padding: 8px 0; border-bottom: 1px solid var(--line); display: flex; gap: 12px; }
.kv div:last-child { border-bottom: 0; }
.kv .k { color: var(--muted); min-width: 78px; flex: none; }
.field { margin-bottom: 12px; }

/* ---- scan page ---- */
.dropzone {
  border: 2px dashed var(--line); border-radius: var(--radius);
  padding: 36px 20px; text-align: center; background: var(--panel);
  cursor: pointer; transition: border-color 0.15s, background 0.15s;
}
.dropzone.hot { border-color: var(--accent); background: var(--accent-soft); }
.dropzone .icon { font-size: 40px; line-height: 1; margin-bottom: 10px; }
.previews { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 10px; margin: 16px 0; }
.previews figure { margin: 0; position: relative; }
.previews img { width: 100%; aspect-ratio: 3/4; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); }
.previews button {
  position: absolute; top: 5px; right: 5px; width: 26px; height: 26px;
  border-radius: 50%; border: 0; background: rgba(0,0,0,0.65); color: #fff;
  font-size: 15px; line-height: 1; cursor: pointer;
}
.status { margin-top: 16px; font-size: 14px; }
.status .line { padding: 7px 0; display: flex; gap: 9px; align-items: center; }
.spinner {
  width: 15px; height: 15px; border: 2px solid var(--line);
  border-top-color: var(--accent); border-radius: 50%;
  animation: spin 0.8s linear infinite; flex: none;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- print ---- */
@media print {
  header.bar, .noprint { display: none !important; }
  body { background: #fff; }
  .panel { border: 0; box-shadow: none; padding: 0; }
  .pageshot { break-inside: avoid; page-break-after: always; border: 0; }
}

/* ---- manual crop ---- */
/* The photo's pixel size is set by crop.js, not by CSS. A percentage max-width
   inside a shrink-to-fit box is a circular rule browsers disagree on, and on
   Safari it could fall back to the photo's intrinsic 3000px width. */
.cropwrap { padding: 10px; overflow: hidden; text-align: center; }
.cropstage {
  position: relative; display: inline-block; line-height: 0;
  max-width: 100%;
  touch-action: none; /* we handle the drag ourselves */
  user-select: none; -webkit-user-select: none;
}
.cropstage img {
  display: block; border-radius: 6px; -webkit-user-drag: none;
  /* Safety net if the script never runs: vw/vh are absolute, so these cap the
     photo without reintroducing the circular percentage rule. */
  max-width: 92vw; max-height: 70vh;
}
.cropstage svg { position: absolute; inset: 0; overflow: visible; }
.cropshade { fill: rgba(20, 18, 15, 0.55); }
.croppoly { fill: none; stroke: #ffb454; stroke-width: 2.5; stroke-dasharray: 7 5; }
.crophandle .ring {
  fill: rgba(255, 180, 84, 0.22); stroke: #ffb454; stroke-width: 2.5;
  transition: r 0.1s ease;
}
.crophandle .dot { fill: #fff; }
.crophandle.active .ring { r: 21; fill: rgba(255, 180, 84, 0.4); }

.loupe {
  position: absolute; width: 120px; height: 120px; margin: -140px 0 0 -60px;
  border-radius: 50%; border: 3px solid #ffb454; pointer-events: none;
  background-repeat: no-repeat; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
  z-index: 5;
}
.loupe.below { margin-top: 20px; }
.loupe::after {
  content: ""; position: absolute; inset: 0;
  background:
    linear-gradient(#ffb454, #ffb454) center/1px 18px no-repeat,
    linear-gradient(#ffb454, #ffb454) center/18px 1px no-repeat;
  opacity: 0.85; border-radius: 50%;
}

.pagefig { margin: 0 0 18px; }
.pagefig .pageshot { margin-bottom: 6px; }
.pagefig figcaption {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; flex-wrap: wrap;
}
.btn.small { padding: 6px 12px; font-size: 13px; }
