:root {
  --paper: #F7F5EF;
  --card: #FFFFFF;
  --ink: #26324B;
  --ink-soft: #5B6785;
  --line: #E4E0D5;
  --accent: #9E4A3A;      /* библиотечный штамп */
  --accent-soft: #F3E4E0;
  --blue: #7B8FC2;
  --green: #5E7F5E;
  --radius: 14px;
  --shadow: 0 2px 10px rgba(38, 50, 75, .07);
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } * { transition: none !important; } }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 15px;
}

h1, h2, h3 { font-family: 'Literata', Georgia, serif; font-weight: 600; }
h1 { font-size: 1.7rem; margin: 0; }
h2 { font-size: 1.2rem; margin: 28px 0 12px; }
h3 { font-size: 1rem; margin: 0 0 10px; }
.muted { color: var(--ink-soft); font-size: .85rem; }

/* ---------- шапка ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 20px; flex-wrap: wrap;
  padding: 12px 24px;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; }
.brand-name { font-family: 'Literata', Georgia, serif; font-weight: 600; font-size: 1.05rem; }

.tabs { display: flex; gap: 4px; flex-wrap: wrap; margin-right: auto; }
.tabs button {
  border: 0; background: transparent; color: var(--ink-soft);
  font: inherit; padding: 8px 12px; border-radius: 8px; cursor: pointer;
}
.tabs button:hover { background: #EFEBE0; }
.tabs button.active { color: var(--ink); background: #ECE8DC; font-weight: 600; }
.tabs button:focus-visible, .btn:focus-visible, .input:focus-visible { outline: 2px solid var(--blue); outline-offset: 1px; }

.btn {
  border: 1px solid var(--line); background: var(--card); color: var(--ink);
  font: inherit; padding: 8px 14px; border-radius: 9px; cursor: pointer;
}
.btn:hover { border-color: var(--ink-soft); }
.btn-primary { background: var(--ink); color: #fff; border-color: var(--ink); }
.btn-primary:hover { background: #37456A; }
.btn-danger { color: var(--accent); }

main { max-width: 1200px; margin: 0 auto; padding: 24px; }
.tab-panel[hidden] { display: none; }
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; flex-wrap: wrap; margin-bottom: 18px;
}
.filters { display: flex; gap: 8px; flex-wrap: wrap; }

.input {
  font: inherit; color: var(--ink);
  padding: 8px 10px; border: 1px solid var(--line); border-radius: 9px;
  background: var(--card);
}
textarea.input { resize: vertical; width: 100%; }

/* ---------- дэшборд ---------- */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.stat-card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow);
}
.stat-card .num { font-family: 'Literata', Georgia, serif; font-size: 1.8rem; font-weight: 600; }
.stat-card .lbl { color: var(--ink-soft); font-size: .8rem; margin-top: 2px; }

.charts { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 14px; margin-top: 24px; }
.chart-card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow);
}
/* Chart.js с maintainAspectRatio:false требует родителя с фиксированной высотой,
   иначе canvas и родитель растят друг друга в бесконечном resize-цикле */
.chart-box { position: relative; height: 240px; }
.chart-empty {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  color: var(--ink-soft); font-size: .85rem; text-align: center; padding: 12px;
}

/* ---------- поиск обложек ---------- */
.cover-row { display: flex; gap: 8px; }
.cover-row .input { flex: 1; min-width: 0; }
.cover-results { display: flex; gap: 8px; flex-wrap: wrap; }
.cover-option {
  width: 72px; height: 106px; object-fit: cover; border-radius: 6px;
  cursor: pointer; border: 2px solid transparent; background: #ECE8DC;
}
.cover-option:hover { border-color: var(--blue); }
.cover-option.selected { border-color: var(--accent); }

/* ---------- сканер ---------- */
.scan-wrap {
  position: relative; width: 100%; aspect-ratio: 4/3; margin: 12px 0;
  background: #000; border-radius: var(--radius); overflow: hidden;
}
#scan-video { width: 100%; height: 100%; object-fit: cover; }
.scan-line {
  position: absolute; left: 8%; right: 8%; top: 50%; height: 2px;
  background: var(--accent); box-shadow: 0 0 8px var(--accent);
  animation: scan 1.8s ease-in-out infinite;
}
@keyframes scan { 0%,100% { top: 30%; } 50% { top: 70%; } }
@media (prefers-reduced-motion: reduce) { .scan-line { animation: none; } }

/* ---------- карточки книг ---------- */
.book-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 14px; }
.book-card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px; box-shadow: var(--shadow); cursor: pointer;
  display: flex; flex-direction: column; gap: 8px;
}
.book-card:hover { border-color: var(--blue); }
.cover {
  width: 100%; aspect-ratio: 2 / 3; object-fit: cover;
  border-radius: 8px; background: #ECE8DC;
  display: flex; align-items: center; justify-content: center;
  color: var(--ink-soft); font-size: 2rem;
}
.book-card .t { font-weight: 600; line-height: 1.25; }
.book-card .a { color: var(--ink-soft); font-size: .82rem; }

/* штампы статусов */
.stamp {
  display: inline-block; padding: 2px 8px; border-radius: 6px;
  font-size: .72rem; font-weight: 600; letter-spacing: .04em; text-transform: uppercase;
  border: 1.5px solid currentColor; width: fit-content;
}
.stamp.read     { color: var(--green); }
.stamp.reading  { color: var(--blue); }
.stamp.planned  { color: #A07E28; }
.stamp.wishlist { color: var(--accent); }
.stamp.dropped  { color: #8A8A8A; }

/* ---------- таблица ---------- */
.table-wrap { overflow-x: auto; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.books-table { width: 100%; border-collapse: collapse; min-width: 860px; }
.books-table th, .books-table td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--line); }
.books-table th { font-size: .75rem; text-transform: uppercase; letter-spacing: .05em; color: var(--ink-soft); }
.books-table tbody tr:hover { background: #FBFAF6; }
.books-table .mini-cover { width: 34px; height: 50px; object-fit: cover; border-radius: 4px; background: #ECE8DC; display: block; }
.row-actions { display: flex; gap: 6px; }
.icon-btn { border: 0; background: transparent; cursor: pointer; font-size: 1rem; padding: 4px; border-radius: 6px; }
.icon-btn:hover { background: #EFEBE0; }

/* ---------- серии ---------- */
.series-card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px 18px; margin-bottom: 14px;
}
.series-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.progress { height: 8px; background: #ECE8DC; border-radius: 6px; overflow: hidden; margin: 10px 0 12px; }
.progress > div { height: 100%; background: var(--green); border-radius: 6px; }
.series-books { display: flex; flex-direction: column; gap: 6px; }
.series-book { display: flex; align-items: center; gap: 10px; font-size: .9rem; }
.series-book .ord { color: var(--ink-soft); min-width: 28px; }

/* ---------- цель ---------- */
.goal-card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 18px 20px;
}
.goal-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 6px; }

/* ---------- календарь ---------- */
#calendar-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; }
.month-card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px;
}
.month-card h3 { text-transform: capitalize; }
.month-covers { display: grid; grid-template-columns: repeat(auto-fill, minmax(56px, 1fr)); gap: 8px; }
.month-covers .cover { aspect-ratio: 2 / 3; font-size: 1.1rem; }
.month-empty { color: var(--ink-soft); font-size: .85rem; }

/* ---------- модалки ---------- */
dialog {
  border: 0; border-radius: var(--radius); box-shadow: 0 10px 40px rgba(0,0,0,.2);
  padding: 22px; width: min(680px, 94vw); background: var(--card); color: var(--ink);
}
dialog::backdrop { background: rgba(38, 50, 75, .45); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 14px; }
.form-grid label { display: flex; flex-direction: column; gap: 4px; font-size: .82rem; color: var(--ink-soft); }
.form-grid .input { width: 100%; }
.span2 { grid-column: span 2; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }

/* ---------- мобильная версия ---------- */
@media (max-width: 720px) {
  body { font-size: 14px; }
  main { padding: 12px; }

  .topbar { padding: 10px 12px; gap: 10px; }
  .brand { order: 1; }
  #btn-add { order: 2; margin-left: auto; padding: 7px 11px; }
  .tabs {
    order: 3; width: 100%; margin: 0;
    flex-wrap: nowrap; overflow-x: auto;
    -webkit-overflow-scrolling: touch; scrollbar-width: none;
  }
  .tabs::-webkit-scrollbar { display: none; }
  .tabs button { flex: 0 0 auto; padding: 8px 11px; }

  .panel-head { flex-direction: column; align-items: stretch; gap: 10px; }
  .filters { flex-direction: column; }
  .filters .input { width: 100%; }
  h1 { font-size: 1.35rem; }

  .cards { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .stat-card { padding: 10px 12px; }
  .stat-card .num { font-size: 1.4rem; }

  .charts { grid-template-columns: 1fr; gap: 10px; }
  .chart-box { height: 210px; }

  .book-grid { grid-template-columns: repeat(auto-fill, minmax(128px, 1fr)); gap: 10px; }
  #calendar-grid { grid-template-columns: 1fr; }

  .form-grid { grid-template-columns: 1fr; }
  .span2 { grid-column: span 1; }
  dialog { padding: 16px; width: 96vw; max-height: 92vh; overflow-y: auto; }
  .modal-actions { position: sticky; bottom: -16px; background: var(--card); padding: 10px 0 2px; }
}
