:root {
  color-scheme: light;
  --ink: #18201f;
  --muted: #66716f;
  --paper: #fbfaf7;
  --panel: #ffffff;
  --line: #d7ddd8;
  --accent: #23433f;
  --accent-2: #b7472a;
  --soft: #e8f0ed;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-size: 16px;
  line-height: 1.45;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

a {
  color: var(--accent);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

button,
.primary-action,
.secondary-button,
.icon-link {
  min-height: 44px;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.65rem 0.95rem;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

button:disabled {
  cursor: wait;
  opacity: 0.72;
}

.secondary-button {
  background: var(--panel);
  color: var(--accent);
}

.icon-link {
  width: 44px;
  padding: 0;
  font-size: 1.6rem;
  line-height: 1;
  text-decoration: none;
}

.top-nav {
  position: sticky;
  top: 0;
  z-index: 20;
  min-height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: env(safe-area-inset-top) 1rem 0.6rem;
  background: rgba(251, 250, 247, 0.96);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(8px);
}

.brand {
  color: var(--ink);
  font-weight: 800;
  font-size: 1.15rem;
}

.nav-actions {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.nav-actions form {
  margin: 0;
}

.nav-actions button {
  min-height: 38px;
  padding: 0.45rem 0.7rem;
  background: transparent;
  color: var(--accent);
}

.app-shell {
  width: min(1100px, 100%);
  margin: 0 auto;
  padding: 1rem;
  flex: 1 0 auto;
}

.impersonation-banner {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 0.6rem 1rem;
  padding: 0.65rem 1rem;
  background: #fff2c7;
  border-bottom: 1px solid #d6b656;
  font-weight: 700;
}

.impersonation-banner form { margin: 0; }
.impersonation-banner button { min-height: 36px; padding: 0.35rem 0.7rem; }

.pwa-status-region {
  position: sticky;
  top: 60px;
  z-index: 19;
  display: grid;
  gap: 0.5rem;
}

.pwa-banner {
  display: grid;
  gap: 0.6rem;
  padding: 0.75rem 1rem;
  background: #fff8dc;
  border-bottom: 1px solid #dfc96b;
}

.pwa-banner p {
  margin: 0;
  font-weight: 700;
}

.pwa-banner .action-row {
  gap: 0.5rem;
}

.pwa-banner button,
.pwa-banner .secondary-button {
  min-height: 38px;
  padding: 0.45rem 0.7rem;
}

h1,
h2 {
  line-height: 1.1;
  margin: 0 0 0.7rem;
}

h1 {
  font-size: 1.7rem;
}

h2 {
  font-size: 1.15rem;
}

p {
  margin: 0 0 0.85rem;
}

label {
  display: grid;
  gap: 0.35rem;
  color: var(--ink);
  font-weight: 700;
}

input,
select,
textarea {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 0.65rem 0.75rem;
  font: inherit;
}

input[readonly] {
  background: var(--soft);
  color: var(--muted);
}

textarea {
  resize: vertical;
}

.stack {
  display: grid;
  gap: 1rem;
}

.narrow {
  max-width: 460px;
  margin: 1rem auto;
}

.split-section {
  margin-top: 2rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--line);
}

.notice,
.error {
  border-radius: 8px;
  padding: 0.8rem 0.9rem;
  margin-bottom: 1rem;
}

.notice {
  background: var(--soft);
  border: 1px solid #c8dbd3;
}

.error {
  background: #fff0ec;
  border: 1px solid #e5b2a5;
  color: #7b2611;
}

.dismissible-alert {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.8rem;
}

.dismissible-alert p {
  margin: 0;
}

.dismiss-alert {
  min-height: 36px;
  min-width: 36px;
  padding: 0;
  border-color: transparent;
  background: transparent;
  color: inherit;
}

.error-details {
  margin-top: 0.6rem;
}

.error-details summary {
  min-height: 36px;
  display: inline-flex;
  align-items: center;
  color: var(--accent-2);
  font-weight: 800;
  cursor: pointer;
}

.error-details pre {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  max-height: 220px;
  overflow: auto;
  margin: 0.35rem 0 0;
  padding: 0.7rem;
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
}

.muted {
  color: var(--muted);
}

.library-head,
.page-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin: 0.2rem 0 1rem;
}

.library-head p,
.page-title p {
  color: var(--muted);
}

.filters,
.tool-panel,
.detail-section {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 1rem;
  margin-bottom: 1rem;
}

.filter-disclosure {
  margin-bottom: 1rem;
}

.filter-disclosure summary {
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  padding: 0.65rem 0.95rem;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--panel);
  color: var(--accent);
  font-weight: 800;
  cursor: pointer;
}

.filter-disclosure .filters {
  margin-top: 0.8rem;
}

.offline-filter {
  margin-bottom: 1rem;
}

.filter-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.8rem;
  margin: 0.8rem 0;
}

.cover-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(132px, 1fr));
  gap: 1rem;
}

.book-card {
  color: var(--ink);
  display: grid;
  gap: 0.35rem;
  min-width: 0;
}

.book-card img,
.book-card .cover-placeholder,
.result-row img,
.result-row .cover-placeholder,
.detail-cover img,
.detail-cover .cover-placeholder {
  width: 100%;
  aspect-ratio: 2 / 3;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--soft);
}

.book-card strong,
.book-card span,
.book-card small {
  overflow-wrap: anywhere;
}

.book-card span,
.book-card small {
  color: var(--muted);
}

.cover-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--accent);
  font-size: 2.2rem;
  font-weight: 800;
}

.empty-state {
  min-height: 50vh;
  display: grid;
  place-items: center;
  align-content: center;
  text-align: center;
  gap: 0.7rem;
}

.action-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.7rem;
}

.ownership-controls {
  display: grid;
  gap: 0.8rem;
  margin: 0.8rem 0;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.checkbox-label input {
  width: auto;
  min-height: 24px;
  min-width: 24px;
}

.ownership-badge {
  display: inline-flex;
  width: fit-content;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0.15rem 0.45rem;
  background: var(--soft);
}

.results-list {
  display: grid;
  gap: 0.9rem;
}

.result-row {
  display: grid;
  grid-template-columns: 86px minmax(0, 1fr);
  gap: 0.85rem;
  align-items: start;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.8rem;
}

.result-row h2 {
  margin-bottom: 0.25rem;
}

.book-detail {
  display: grid;
  gap: 1rem;
  margin-bottom: 1rem;
}

.detail-cover {
  max-width: 220px;
}

.byline {
  color: var(--muted);
  font-weight: 700;
}

.metadata {
  display: grid;
  grid-template-columns: minmax(110px, 0.35fr) 1fr;
  gap: 0.5rem 0.8rem;
  margin: 0;
}

.metadata dt {
  color: var(--muted);
}

.metadata dd {
  margin: 0;
}

.preserve-lines {
  white-space: pre-wrap;
}

.edit-form {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 1rem;
}

.delete-form {
  margin-top: 1rem;
}

.delete-form button {
  background: var(--accent-2);
  border-color: var(--accent-2);
}

.floating-add {
  position: fixed;
  right: max(1rem, env(safe-area-inset-right));
  bottom: max(1rem, env(safe-area-inset-bottom));
  z-index: 30;
  width: 60px;
  height: 60px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  font-weight: 800;
  line-height: 1;
  text-decoration: none;
  box-shadow: 0 10px 24px rgba(24, 32, 31, 0.22);
}

.floating-add:hover {
  text-decoration: none;
}

.site-footer {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.4rem 1rem;
  padding: 1.2rem 1rem calc(5rem + env(safe-area-inset-bottom));
  color: var(--muted);
  font-size: 0.9rem;
}

.search-row {
  display: flex;
  align-items: end;
  gap: 0.7rem;
  margin-bottom: 1rem;
}

.search-row label { flex: 1; }

.table-scroll { overflow-x: auto; }

table {
  width: 100%;
  border-collapse: collapse;
  background: var(--panel);
}

th, td {
  padding: 0.7rem;
  text-align: left;
  border: 1px solid var(--line);
  vertical-align: top;
}

.table-actions { display: grid; gap: 0.35rem; min-width: 110px; justify-items: start; text-align: left; }
.table-actions form { margin: 0; }
.table-actions .link-button { justify-content: flex-start; text-align: left; }
.link-button {
  min-height: auto;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--accent);
  font-weight: 400;
}
.break-all { overflow-wrap: anywhere; }
.back-link { text-align: center; }

#scanner {
  display: block;
  width: 100%;
  height: 100%;
  max-height: min(62vh, 420px);
  background: #111;
  object-fit: cover;
  position: relative;
  z-index: 1;
}

.scanner-frame {
  position: relative;
  isolation: isolate;
  overflow: hidden;
  border-radius: 8px;
  background: #111;
  margin-bottom: 0.8rem;
  aspect-ratio: 3 / 4;
  max-height: min(62vh, 420px);
}

.scan-overlay {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  border: 2px solid rgba(255, 255, 255, 0.72);
  border-radius: 8px;
  transform: translateZ(0);
}

.scan-overlay::before,
.scan-overlay::after {
  content: "";
  position: absolute;
  left: 12%;
  right: 12%;
  height: 1px;
  background: rgba(255, 255, 255, 0.55);
}

.scan-overlay::before {
  top: 32%;
}

.scan-overlay::after {
  bottom: 32%;
}

.scan-overlay span {
  position: absolute;
  left: 10%;
  right: 10%;
  top: 18%;
  height: 2px;
  border-radius: 999px;
  background: #e53935;
  box-shadow: 0 0 10px rgba(229, 57, 53, 0.8);
  animation: scan-line 1.8s ease-in-out infinite alternate;
}

@keyframes scan-line {
  from {
    top: 18%;
  }
  to {
    top: 82%;
  }
}

@media (prefers-reduced-motion: reduce) {
  .scan-overlay span {
    animation: none;
    top: 50%;
  }
}

@media (min-width: 680px) {
  .app-shell {
    padding: 1.4rem;
  }

  .filter-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .cover-grid {
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  }

  .book-detail {
    grid-template-columns: 220px minmax(0, 1fr);
    align-items: start;
  }
}
