:root {
  --paper: #f6f1e7;
  --card: #fffdf8;
  --ink: #2a2620;
  --muted: #7a7062;
  --line: #e4dccb;
  --green: #2f4a3a;
  --green-2: #3f6450;
  --green-soft: #e3ece4;
  --amber: #c8892b;
  --amber-soft: #fff3d6;
  --red: #a8432f;
  --red-soft: #fbe7e2;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(60, 45, 20, .06), 0 4px 16px rgba(60, 45, 20, .06);
  --serif: "PT Serif", "Georgia", "Times New Roman", serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--paper);
  color: var(--ink);
  font: 16px/1.45 var(--sans);
  -webkit-text-size-adjust: 100%;
  padding-bottom: env(safe-area-inset-bottom);
}
button, input, textarea { font: inherit; color: inherit; }
.ic { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--green); color: #f4efe2;
}
.brand { display: flex; align-items: center; gap: 8px; font-family: var(--serif); font-size: 19px; font-weight: 700; letter-spacing: .2px; }
.brand-icon { font-size: 22px; }
.topbar .btn.ghost { color: #f4efe2; border-color: rgba(255,255,255,.25); background: transparent; }
.topbar .btn.ghost:hover { background: rgba(255,255,255,.08); }

.wrap { max-width: 980px; margin: 0 auto; padding: 16px; display: grid; gap: 16px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 10px 16px;
  border: 1px solid var(--line); border-radius: 10px; background: #fff;
  cursor: pointer; text-decoration: none; font-weight: 600; font-size: 15px; white-space: nowrap;
  transition: background .15s, border-color .15s, transform .05s;
}
.btn:hover { background: #faf6ec; }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn.primary:hover { background: var(--green-2); }
.btn.danger { color: var(--red); border-color: #e9c8bf; }
.btn.danger:hover { background: var(--red-soft); }
.btn.block { width: 100%; }
.btn[aria-disabled="true"] { opacity: .5; pointer-events: none; }
.icon-btn { border: 0; background: transparent; padding: 8px; border-radius: 8px; cursor: pointer; color: var(--muted); display: inline-flex; }
.icon-btn:hover { background: rgba(0,0,0,.05); color: var(--ink); }

/* Документ */
.doc-head { padding: 18px 18px 16px; }
.doc-title-row { display: flex; align-items: center; gap: 4px; }
.doc-name { font-family: var(--serif); font-size: 24px; line-height: 1.2; margin: 0; cursor: text; word-break: break-word; }
.doc-name[contenteditable="true"] { outline: 2px solid var(--green-soft); border-radius: 6px; padding: 0 4px; background: #fff; }
.doc-meta { margin: 6px 0 14px; color: var(--muted); font-size: 14px; display: flex; gap: 12px; flex-wrap: wrap; }
.doc-meta #docStatus:not(:empty)::before { content: "•"; margin-right: 12px; }
.doc-actions { display: flex; gap: 10px; flex-wrap: wrap; }

/* Загрузка */
.upload { border: 2px dashed #cdbf9f; background: #fffaf0; box-shadow: none; transition: background .15s, border-color .15s; }
.upload.drag { background: var(--green-soft); border-color: var(--green-2); }
.upload-inner { padding: 22px 18px; text-align: center; }
.upload-art svg { width: 64px; height: 64px; }
.upload-art rect { fill: #e8dcc0; stroke: #b9a67c; stroke-width: 1.5; }
.upload-art rect.front { fill: #fff; }
.upload-art .lines { stroke: #b9a67c; stroke-width: 2; stroke-linecap: round; fill: none; }
.upload-title { font-family: var(--serif); font-size: 20px; font-weight: 700; margin: 6px 0 2px; }
.upload-hint { margin: 0 0 14px; color: var(--muted); }
.upload-buttons { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.upload-tip { margin: 14px auto 0; max-width: 520px; font-size: 13px; color: var(--muted); }

.mobile-only { display: none !important; }
@media (pointer: coarse) {
  .mobile-only { display: inline-flex !important; }
  p.mobile-only { display: block !important; }
  .desktop-only { display: none !important; }
}

/* Список книг */
.books-head { display: flex; align-items: baseline; justify-content: space-between; margin: 4px 2px 10px; }
.books-head h2 { font-family: var(--serif); font-size: 19px; margin: 0; }
.legend { font-size: 13px; color: var(--muted); display: inline-flex; align-items: center; gap: 6px; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.dot.warn { background: var(--amber); }

.book-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; counter-reset: book; }
.book {
  counter-increment: book;
  display: grid; grid-template-columns: 64px 1fr auto; gap: 14px; align-items: start;
  padding: 12px; background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  cursor: pointer; transition: border-color .15s, box-shadow .15s;
  animation: appear .25s ease-out;
}
@keyframes appear { from { opacity: 0; transform: translateY(4px); } }
.book:hover { border-color: #cdbf9f; box-shadow: var(--shadow); }
.book.warn { border-left: 4px solid var(--amber); background: linear-gradient(90deg, var(--amber-soft), var(--card) 40%); }
.book.error { border-left: 4px solid var(--red); }
.book-thumb { width: 64px; height: 90px; border-radius: 4px; object-fit: cover; background: #eee5d2; box-shadow: 2px 2px 0 #e0d5bd; }
.book-body { min-width: 0; }
.book-title { font-family: var(--serif); font-weight: 700; font-size: 17px; line-height: 1.25; margin: 0 0 2px; }
.book-title::before { content: counter(book) ". "; color: var(--muted); font-weight: 400; }
.book-author { margin: 0 0 4px; }
.book-line { margin: 0; color: var(--muted); font-size: 14px; }
.book-err { color: var(--red); font-size: 14px; margin: 4px 0 0; }
.book-side { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }
.badge { font-size: 12px; font-weight: 600; padding: 3px 8px; border-radius: 99px; white-space: nowrap; }
.badge.warn { background: var(--amber-soft); color: #8a5a12; }
.badge.proc { background: var(--green-soft); color: var(--green); display: inline-flex; align-items: center; gap: 6px; }
.badge.err { background: var(--red-soft); color: var(--red); }
.spinner { width: 12px; height: 12px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.skeleton { height: 12px; border-radius: 6px; background: linear-gradient(90deg, #eee6d4, #f7f1e4, #eee6d4); background-size: 200% 100%; animation: shimmer 1.2s infinite; margin: 6px 0; }
@keyframes shimmer { to { background-position: -200% 0; } }

.empty { text-align: center; color: var(--muted); padding: 24px 12px; }
.book-list:not(:empty) + .empty { display: none; }

/* Перетаскивание на всё окно */
.drop-overlay {
  position: fixed; inset: 0; z-index: 50; display: none;
  align-items: center; justify-content: center;
  background: rgba(47, 74, 58, .82); color: #fff; font-family: var(--serif); font-size: 26px;
  pointer-events: none;
}
.drop-overlay > div { border: 3px dashed rgba(255,255,255,.7); border-radius: 20px; padding: 48px 56px; }
body.dragging .drop-overlay { display: flex; }

/* Нижние листы / модальные окна */
.sheet { position: fixed; inset: 0; z-index: 40; display: flex; align-items: flex-end; justify-content: center; }
.sheet[hidden] { display: none; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(30, 25, 15, .45); animation: fade .2s; }
@keyframes fade { from { opacity: 0; } }
.sheet-panel {
  position: relative; width: 100%; max-width: 520px; max-height: 92vh; overflow: auto;
  background: var(--card); border-radius: 18px 18px 0 0; padding: 16px 18px calc(18px + env(safe-area-inset-bottom));
  animation: up .22s ease-out;
}
.sheet-panel.wide { max-width: 860px; }
@keyframes up { from { transform: translateY(30px); opacity: 0; } }
@media (min-width: 720px) {
  .sheet { align-items: center; }
  .sheet-panel { border-radius: 18px; }
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.sheet-head h3 { font-family: var(--serif); font-size: 20px; margin: 0; }

.doc-list { list-style: none; padding: 0; margin: 14px 0; display: grid; gap: 8px; }
.doc-item { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; cursor: pointer; }
.doc-item:hover { background: #faf6ec; }
.doc-item.active { border-color: var(--green-2); background: var(--green-soft); }
.doc-item-body { flex: 1; min-width: 0; }
.doc-item-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.doc-item-meta { font-size: 13px; color: var(--muted); }

.editor { display: grid; gap: 16px; }
@media (min-width: 720px) { .editor { grid-template-columns: 220px 1fr; } }
.ed-photo img { width: 100%; max-height: 340px; object-fit: contain; border-radius: 8px; background: #eee5d2; }
.ed-form { display: grid; gap: 10px; }
@media (min-width: 720px) { .ed-form { grid-template-columns: 1fr 1fr; } .ed-form .full { grid-column: 1 / -1; } }
.field label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 3px; }
.field input, .field textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; background: #fff;
  font-size: 16px; /* без автозума на iOS */
}
.field textarea { resize: vertical; min-height: 44px; }
.field input:focus, .field textarea:focus { outline: 2px solid var(--green-soft); border-color: var(--green-2); }
.check { display: flex; align-items: center; gap: 10px; font-size: 15px; }
.check input { width: 20px; height: 20px; accent-color: var(--amber); }
.ed-actions { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; margin-top: 16px; position: sticky; bottom: -18px; background: var(--card); padding: 10px 0; }
.ed-actions .danger { margin-right: auto; }

.muted { color: var(--muted); }
.small { font-size: 13px; }

.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 15px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 60; max-width: 90vw; text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 520px) {
  .wrap { padding: 12px; gap: 12px; }
  .doc-name { font-size: 21px; }
  .doc-actions .btn { flex: 1; }
  .topbar .btn span { display: none; }
  .book { grid-template-columns: 56px 1fr; }
  .book-thumb { width: 56px; height: 78px; }
  .book-side { grid-column: 2; flex-direction: row; align-items: center; justify-content: flex-start; }
}

/* Вход */
.login-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; padding: 26px 22px; text-align: center; }
.login-card h1 { font-family: var(--serif); margin: 6px 0 4px; font-size: 24px; }
.login-card form { display: grid; gap: 10px; margin-top: 16px; }
.login-card input { padding: 12px; border: 1px solid var(--line); border-radius: 10px; font-size: 16px; }
.login-err { color: var(--red); margin: 8px 0 0; }
