/* Worksheet Finder — mobile-first, capped width, one compact bar. */
:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --ink: #1c2330;
  --muted: #5b6473;
  --line: #e2e5ea;
  --accent: #2f6fed;
  --accent-ink: #ffffff;
  --chip: #eef1f6;
  --chip-on: #2f6fed;
  --maxw: 720px;
  --bar-h: 52px;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; } /* author display rules below must not override the attribute */
html, body { margin: 0; }
body {
  font: 16px/1.45 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
  padding-bottom: calc(var(--bar-h) + env(safe-area-inset-bottom));
}

/* ---- top bar ---- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  height: var(--bar-h);
  padding: 0 12px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
}
.title {
  margin: 0; font-size: 18px; font-weight: 700;
  flex: 1 1 auto; min-width: 0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.topbar-actions { display: flex; align-items: center; gap: 6px; flex: 0 0 auto; }

/* ---- buttons ---- */
.btn {
  font: inherit; cursor: pointer;
  border: 1px solid var(--line); background: var(--card); color: var(--ink);
  border-radius: 9px; padding: 8px 12px;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn-ghost { background: transparent; }
.btn-sm { padding: 6px 10px; }
.btn-icon { padding: 8px 10px; }
.btn-filter { font-weight: 600; display: inline-flex; align-items: center; gap: 6px; }
.badge {
  display: inline-grid; place-items: center; min-width: 20px; height: 20px;
  padding: 0 5px; border-radius: 10px; background: var(--accent); color: #fff;
  font-size: 12px; font-weight: 700;
}
.linkbtn { background: none; border: 0; color: var(--accent); font: inherit; cursor: pointer; padding: 0; }

/* ---- content ---- */
.content { max-width: var(--maxw); margin: 0 auto; padding: 10px 12px 16px; }
.resultbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 2px 10px; }
.result-count { color: var(--muted); font-size: 14px; }

.results { display: flex; flex-direction: column; gap: 10px; }

/* ---- book group ---- */
/* No overflow:hidden here — it would make the card the sticky scroll context
   and collapse the summary onto its first item. Round the inner pieces instead. */
.book { background: var(--card); border: 1px solid var(--line); border-radius: 12px; }
.book > summary {
  list-style: none; cursor: pointer;
  display: flex; align-items: center; gap: 10px;
  padding: 12px 14px; font-weight: 600;
  position: sticky; top: var(--bar-h); background: var(--card); z-index: 5;
  border-radius: 11px 11px 0 0;
}
.book:not([open]) > summary { border-radius: 11px; }
.book > summary::-webkit-details-marker { display: none; }
.book > summary::before { content: "▸"; color: var(--muted); transition: transform .15s; }
.book[open] > summary::before { transform: rotate(90deg); }
.book-title { flex: 1 1 auto; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.book-count { color: var(--muted); font-weight: 500; font-size: 13px; flex: 0 0 auto; }
.subject-tag {
  flex: 0 0 auto; font-size: 11px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .03em; color: var(--accent); background: var(--chip);
  padding: 2px 7px; border-radius: 6px;
}

.items { display: flex; flex-direction: column; border-radius: 0 0 11px 11px; overflow: hidden; }
.item { display: flex; gap: 10px; padding: 11px 14px; border-top: 1px solid var(--line); align-items: flex-start; }
.item input[type="checkbox"] { width: 20px; height: 20px; margin: 2px 0 0; flex: 0 0 auto; }
.item-main { flex: 1 1 auto; min-width: 0; }
.item-title { font-weight: 600; }
.item-sub { color: var(--muted); font-size: 13px; margin-top: 1px; }
.item-meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.tag { font-size: 12px; background: var(--chip); color: #38445a; border-radius: 6px; padding: 2px 7px; }
.tag-grade { background: #e7efff; color: #1f50b5; font-weight: 600; }
.tag-review { background: #fff2e0; color: #9a5b00; }
.item-actions { margin-top: 6px; }
.preview-link { background: none; border: 0; color: var(--accent); font: inherit; cursor: pointer; padding: 0; }

.empty { color: var(--muted); text-align: center; padding: 40px 10px; }

/* ---- batch bar ---- */
.batchbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  max-width: var(--maxw); margin: 0 auto;
  padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
  background: var(--card); border-top: 1px solid var(--line);
}
.batch-count { font-weight: 600; }
.batch-actions { display: flex; gap: 8px; }

/* ---- overlays / sheet / modal ---- */
.overlay { position: fixed; inset: 0; background: rgba(15,20,30,.45); z-index: 40; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
  max-width: var(--maxw); margin: 0 auto;
  background: var(--card); border-radius: 16px 16px 0 0;
  display: flex; flex-direction: column; max-height: 88vh;
}
.sheet-grip { width: 40px; height: 4px; border-radius: 2px; background: var(--line); margin: 8px auto 2px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 6px 16px 10px; border-bottom: 1px solid var(--line); }
.sheet-head h2 { margin: 0; font-size: 18px; }
.sheet-body { padding: 10px 16px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.sheet-foot { display: flex; gap: 10px; padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
.sheet-foot .btn { flex: 1; }

.facet { padding: 10px 0; border-bottom: 1px solid var(--line); }
.facet:last-child { border-bottom: 0; }
.facet > summary { cursor: pointer; font-weight: 600; list-style: none; }
.facet > summary::-webkit-details-marker { display: none; }
.facet > summary::after { content: "＋"; float: right; color: var(--muted); }
.facet[open] > summary::after { content: "−"; }
.facet-label { display: block; font-weight: 600; margin-bottom: 6px; }
.facet-hint { color: var(--muted); font-size: 13px; margin: 8px 0 6px; }

.text-input { width: 100%; font: inherit; padding: 10px 12px; border: 1px solid var(--line); border-radius: 9px; background: #fff; }
.std-input-row { margin-top: 8px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.chips-tight { margin-top: 8px; }
.chip {
  font: inherit; cursor: pointer; border: 1px solid var(--line);
  background: var(--chip); color: var(--ink); border-radius: 999px; padding: 7px 13px;
}
.chip[aria-pressed="true"] { background: var(--chip-on); border-color: var(--chip-on); color: #fff; }
.chip-removable { display: inline-flex; align-items: center; gap: 6px; background: #e7efff; color: #1f50b5; }
.chip-removable .x { font-weight: 700; }

.suggest { margin-top: 6px; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; }
.suggest button { display: block; width: 100%; text-align: left; padding: 9px 12px; border: 0; border-bottom: 1px solid var(--line); background: #fff; font: inherit; cursor: pointer; }
.suggest button:last-child { border-bottom: 0; }
.suggest .code { font-weight: 600; }
.suggest .desc { color: var(--muted); font-size: 13px; }

.checklist { margin-top: 8px; display: flex; flex-direction: column; gap: 2px; max-height: 240px; overflow-y: auto; }
.checklist label { display: flex; gap: 9px; align-items: flex-start; padding: 7px 4px; }
.checklist input { width: 18px; height: 18px; margin-top: 2px; }
.checklist .u-grade { color: var(--muted); font-size: 12px; }

.modal {
  position: fixed; left: 12px; right: 12px; top: 18%; z-index: 50;
  max-width: 420px; margin: 0 auto;
  background: var(--card); border-radius: 14px; padding: 18px;
}
.modal h2 { margin: 0 0 6px; font-size: 18px; }
.modal-hint { color: var(--muted); font-size: 14px; margin: 0 0 12px; }
.modal .text-input { margin-bottom: 14px; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; }

/* ---- busy ---- */
.busy { position: fixed; inset: 0; z-index: 60; display: grid; place-items: center; background: rgba(15,20,30,.35); }
.busy-card { background: var(--card); border-radius: 12px; padding: 18px 22px; display: flex; align-items: center; gap: 12px; font-weight: 600; }
.spinner { width: 18px; height: 18px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.print-frame { position: fixed; width: 0; height: 0; border: 0; left: -9999px; }

/* ---- review mode ---- */
.badge-warn { background: #d97700; }
body.reviewing .content,
body.reviewing .batchbar { display: none !important; }

.review { max-width: var(--maxw); margin: 0 auto; padding: 0 12px 24px; }
.review-head {
  position: sticky; top: var(--bar-h); z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  background: var(--bg); padding: 10px 2px; border-bottom: 1px solid var(--line);
}
.review-progress { color: var(--muted); font-weight: 600; font-size: 14px; }
.review-nav { display: flex; gap: 6px; }
.review-nav .btn { min-width: 38px; font-size: 17px; line-height: 1; }

.review-body { padding-top: 12px; }
.rc-head { margin-bottom: 12px; }
.rc-title { font-size: 18px; font-weight: 700; }
.rc-sub { color: var(--muted); font-size: 13px; margin-top: 2px; }
.rc-done-tag { display: inline-block; margin-top: 6px; font-size: 12px; font-weight: 600; color: #1f7a3d; background: #e4f6ea; border-radius: 6px; padding: 2px 8px; }
.rc-note { margin-top: 8px; font-size: 13px; color: #9a5b00; background: #fff2e0; border-radius: 8px; padding: 8px 10px; }

.rc-pdf { position: relative; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: #fff; }
.rc-frame { width: 100%; height: 46vh; border: 0; display: block; }
.rc-open { position: absolute; right: 8px; bottom: 8px; background: rgba(255,255,255,.92); border: 1px solid var(--line); border-radius: 8px; padding: 4px 9px; font-size: 12px; color: var(--accent); text-decoration: none; }
.rc-pdf-err { padding: 28px 16px; text-align: center; color: var(--muted); }

.rc-fields { display: flex; flex-direction: column; gap: 16px; margin-top: 16px; }
.rc-label { display: block; font-weight: 600; margin-bottom: 8px; }

.rc-actions {
  position: sticky; bottom: 0; display: flex; gap: 10px; margin-top: 18px;
  padding: 12px 0 calc(12px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 72%, rgba(244,245,247,0));
}
.rc-actions .btn { flex: 1; }

@media (min-width: 760px) {
  .title { font-size: 20px; }
  .review-body { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; align-items: start; }
  .rc-head { grid-column: 1 / -1; margin-bottom: 0; }
  .rc-pdf { position: sticky; top: calc(var(--bar-h) + 64px); }
  .rc-frame { height: 72vh; }
  .rc-fields { margin-top: 0; }
  .rc-actions { grid-column: 1 / -1; }
}
