@import url('https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;600;700&family=Libre+Baskerville:wght@400;700&display=swap');

:root {
  --ink: #271b15;
  --muted: #75645a;
  --paper: #fbf7ef;
  --paper-deep: #f1e8d9;
  --wood: #4a2b1b;
  --wood-dark: #28160f;
  --rust: #a84f2b;
  --gold: #d59b43;
  --green: #416c5a;
  --red: #a33d36;
  --line: #ded1c0;
  --shadow: 0 18px 45px rgba(45, 25, 15, .14);
  --radius: 16px;
}

* { box-sizing: border-box; }
html { min-height: 100%; scroll-behavior: smooth; }
body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  font-family: "DM Sans", system-ui, sans-serif;
  background-color: #eadfce;
}
body.app-page {
  background:
    linear-gradient(rgba(239, 230, 214, .9), rgba(239, 230, 214, .93)),
    repeating-linear-gradient(90deg, transparent 0 18px, rgba(62, 35, 23, .4) 18px 22px, transparent 22px 45px),
    repeating-linear-gradient(0deg, #5b3521 0 110px, #2e1a12 110px 126px);
  background-attachment: fixed;
}
a { color: inherit; }
button, input, textarea, select { font: inherit; }
button, .button { -webkit-tap-highlight-color: transparent; }
h1, h2, h3 { font-family: "Libre Baskerville", Georgia, serif; line-height: 1.2; }
h1 { margin: .25rem 0 1rem; font-size: clamp(2rem, 5vw, 3.6rem); letter-spacing: -.04em; }
h2 { margin: .2rem 0 .55rem; }
p { line-height: 1.65; }
.muted { color: var(--muted); }
.eyebrow { margin: 0 0 .5rem; color: var(--rust); font-size: .75rem; font-weight: 700; letter-spacing: .16em; text-transform: uppercase; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

.topbar {
  position: sticky; z-index: 20; top: 0;
  display: flex; align-items: center; justify-content: space-between;
  min-height: 68px; padding: 0 max(22px, calc((100% - 1180px) / 2));
  color: #fff8ec; background: rgba(43, 24, 16, .96); border-bottom: 3px solid #a36b36;
  box-shadow: 0 5px 20px rgba(35, 17, 10, .25);
}
.brand { display: flex; align-items: center; gap: .75rem; font-family: "Libre Baskerville", serif; text-decoration: none; font-weight: 700; }
.brand-mark, .login-emblem {
  display: grid; place-items: center; border-radius: 4px 10px 10px 4px;
  color: white; background: var(--rust); border-left: 5px solid #71351e; box-shadow: inset -3px 0 rgba(255,255,255,.12);
  font-family: "Libre Baskerville", serif; font-weight: 700;
}
.brand-mark { width: 34px; height: 42px; }
.main-nav { display: flex; align-items: center; gap: .35rem; }
.main-nav > a, .link-button {
  padding: .65rem .8rem; border: 0; border-radius: 8px; color: #e9dccd;
  background: transparent; text-decoration: none; cursor: pointer;
}
.main-nav > a:hover, .main-nav > a.active, .link-button:hover { color: white; background: rgba(255,255,255,.1); }
.logout-form { margin: 0; }
.nav-toggle { display: none; padding: .5rem .75rem; border: 1px solid #83664f; border-radius: 7px; color: white; background: transparent; }

.page-shell { width: min(1180px, calc(100% - 32px)); min-height: calc(100vh - 130px); margin: 0 auto; padding: 42px 0 64px; }
.hero {
  display: flex; align-items: end; justify-content: space-between; gap: 2rem;
  padding: clamp(28px, 6vw, 58px); border: 1px solid rgba(255,255,255,.65); border-radius: 24px;
  background: linear-gradient(120deg, rgba(255,252,246,.96), rgba(245,232,212,.94)); box-shadow: var(--shadow);
}
.hero h1 span { display: block; margin-top: .35rem; color: var(--rust); font-size: .42em; letter-spacing: 0; }
.hero p:last-child { max-width: 620px; margin-bottom: 0; color: var(--muted); }
.hero.compact { align-items: center; }

.button {
  display: inline-flex; align-items: center; justify-content: center; min-height: 42px; padding: .65rem 1rem;
  border: 1px solid transparent; border-radius: 9px; text-decoration: none; font-weight: 700; cursor: pointer;
  transition: transform .15s, box-shadow .15s, background .15s;
}
.button:hover:not(:disabled) { transform: translateY(-1px); box-shadow: 0 5px 12px rgba(40,20,10,.14); }
.button:disabled { opacity: .45; cursor: not-allowed; }
.button-primary { color: white; background: var(--wood); }
.button-accent { color: #23160f; background: var(--gold); }
.button-ghost, .button-small { border-color: var(--line); background: #fffaf2; }
.button-danger { color: white; background: var(--red); }
.button-camera { color: white; background: var(--green); }
.button-wide { width: 100%; }
.button-small { min-height: 34px; padding: .4rem .65rem; font-size: .84rem; }

.stat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin: 22px 0; }
.stat { display: flex; align-items: baseline; justify-content: space-between; padding: 18px 22px; border-radius: 12px; color: var(--ink); background: rgba(255,250,242,.93); text-decoration: none; box-shadow: 0 7px 18px rgba(48,28,17,.1); }
.stat strong { color: var(--rust); font-family: "Libre Baskerville", serif; font-size: 1.8rem; }
.stat span { color: var(--muted); font-size: .85rem; }
.menu-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 18px; }
.menu-card {
  position: relative; min-height: 260px; padding: 28px; overflow: hidden;
  border: 1px solid rgba(255,255,255,.7); border-radius: var(--radius); text-align: left;
  color: var(--ink); background: rgba(255,250,242,.95); box-shadow: var(--shadow); text-decoration: none; cursor: pointer;
}
.menu-card::after { content: ""; position: absolute; right: -35px; bottom: -55px; width: 150px; height: 190px; border-radius: 7px 12px 12px 7px; background: var(--rust); transform: rotate(12deg); opacity: .14; }
.menu-card:hover { transform: translateY(-3px); transition: transform .2s; }
.menu-card p { max-width: 250px; color: var(--muted); }
.menu-icon, .admin-icon { display: grid; place-items: center; width: 58px; height: 66px; margin-bottom: 1.5rem; border-radius: 5px 12px 12px 5px; color: white; background: var(--rust); box-shadow: inset -5px 0 rgba(255,255,255,.12); font-family: "Libre Baskerville", serif; font-weight: 700; }
.card-author .menu-icon { background: var(--green); }
.card-genre .menu-icon { background: #5f4d78; }
.card-link { position: absolute; bottom: 25px; color: var(--rust); font-weight: 700; }

.dialog {
  width: min(520px, calc(100% - 24px)); padding: 30px; border: 0; border-radius: 18px;
  color: var(--ink); background: var(--paper); box-shadow: 0 30px 100px rgba(25,13,8,.45);
}
.dialog::backdrop { background: rgba(29,17,12,.66); backdrop-filter: blur(3px); }
.dialog-close { position: absolute; right: 15px; top: 12px; border: 0; font-size: 1.8rem; color: var(--muted); background: transparent; cursor: pointer; }
.dialog label { display: block; margin: 1.2rem 0 .4rem; font-weight: 700; }
.dialog input { width: 100%; }
.search-results { display: grid; gap: 6px; max-height: 300px; margin-top: 12px; overflow: auto; }
.search-results a { display: flex; justify-content: space-between; gap: 1rem; padding: 12px; border-radius: 8px; background: white; text-decoration: none; }
.search-results a:hover { background: var(--paper-deep); }
.search-results span { color: var(--muted); font-size: .85rem; }

.page-heading { display: flex; align-items: end; justify-content: space-between; gap: 1rem; margin-bottom: 24px; }
.page-heading h1 { margin-bottom: 0; }
.filter-bar { display: grid; grid-template-columns: minmax(180px, 1fr) 190px auto auto; gap: 10px; padding: 16px; border-radius: 13px; background: rgba(255,250,242,.95); box-shadow: 0 7px 20px rgba(40,20,10,.1); }
input, textarea, select {
  width: 100%; padding: .72rem .8rem; border: 1px solid #cdbda9; border-radius: 8px;
  color: var(--ink); background: white; outline: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--rust); box-shadow: 0 0 0 3px rgba(168,79,43,.13); }
textarea { resize: vertical; }
.book-list { display: grid; gap: 12px; margin-top: 18px; }
.book-row { display: grid; grid-template-columns: 78px minmax(0, 1fr) auto; gap: 20px; align-items: center; min-height: 116px; padding: 14px; border-radius: 12px; background: rgba(255,251,245,.97); box-shadow: 0 6px 15px rgba(45,25,15,.1); }
.cover-small, .cover-large { display: grid; place-items: center; overflow: hidden; color: white; background: linear-gradient(135deg, #a85231, #64311e); box-shadow: 0 7px 15px rgba(50,25,15,.25); font-family: "Libre Baskerville", serif; font-size: 1.7rem; }
.cover-small { width: 70px; height: 92px; border-radius: 3px 7px 7px 3px; }
.cover-small img, .cover-large img { width: 100%; height: 100%; object-fit: cover; }
.book-summary h2 { margin: 0 0 .35rem; font-size: 1.2rem; }
.book-summary h2 a { text-decoration: none; }
.book-summary h2 a:hover { color: var(--rust); }
.book-author, .book-genres { margin: 0; }
.book-author { font-weight: 600; }
.book-genres { color: var(--muted); font-size: .88rem; }
.book-badges { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 6px; max-width: 230px; }
.badge { display: inline-flex; width: fit-content; padding: .28rem .55rem; border-radius: 99px; color: #634f42; background: #eee5da; font-size: .72rem; font-weight: 700; text-transform: capitalize; }
.badge-owned, .badge-read { color: #285344; background: #d9eadf; }
.badge-wanted { color: #804326; background: #f7deca; }
.empty-state { padding: 60px 25px; border-radius: var(--radius); text-align: center; background: rgba(255,250,242,.95); box-shadow: var(--shadow); }
.empty-state > span:first-child { display: block; color: var(--rust); font-family: "Libre Baskerville", serif; font-size: 3rem; }

.back-link { display: inline-block; margin-bottom: 22px; color: var(--rust); font-weight: 700; text-decoration: none; }
.book-detail { display: grid; grid-template-columns: minmax(220px, 330px) 1fr; gap: clamp(32px, 7vw, 80px); padding: clamp(25px, 5vw, 55px); border-radius: 20px; background: rgba(255,250,242,.97); box-shadow: var(--shadow); }
.cover-large { width: 100%; aspect-ratio: 2 / 3; border-radius: 5px 12px 12px 5px; font-size: 5rem; }
.book-detail h1 { font-size: clamp(2rem, 4vw, 3.2rem); }
.detail-author { margin: -.4rem 0 1.5rem; color: var(--rust); font-family: "Libre Baskerville", serif; font-size: 1.25rem; }
.detail-badges { justify-content: flex-start; max-width: none; margin-bottom: 2rem; }
.synopsis { white-space: pre-line; }
.metadata { padding-top: 1rem; border-top: 1px solid var(--line); }
.action-row { display: flex; gap: 10px; margin-top: 2rem; }

.genre-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 18px; }
.genre-card { position: relative; min-height: 190px; padding: 28px; overflow: hidden; border-radius: 15px; color: white; background: #79402c; box-shadow: var(--shadow); text-decoration: none; }
.genre-card::before { content: ""; position: absolute; inset: 0 auto 0 13px; width: 5px; background: rgba(255,255,255,.22); }
.genre-card:hover { transform: translateY(-2px); }
.genre-card h2, .genre-card p { position: relative; margin-left: 58px; }
.genre-card p { opacity: .8; }
.genre-letter { position: absolute; left: 29px; top: 30px; font-family: "Libre Baskerville", serif; font-size: 2.4rem; font-weight: 700; opacity: .85; }
.tone-2 { background: #456859; } .tone-3 { background: #5d5077; } .tone-4 { background: #946c33; } .tone-5 { background: #3e6370; }

.admin-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 18px; margin-top: 22px; }
.admin-card { display: flex; gap: 22px; align-items: center; min-height: 160px; padding: 26px; border-radius: 15px; background: rgba(255,250,242,.96); box-shadow: var(--shadow); text-decoration: none; }
.admin-card:hover { transform: translateY(-2px); }
.admin-card .admin-icon { flex: 0 0 auto; margin: 0; background: var(--wood); }
.admin-card p { margin: 0; color: var(--muted); }
.admin-card.primary-card { color: white; background: var(--wood); }
.admin-card.primary-card p { color: #d9c9bc; }
.admin-card.primary-card .admin-icon { color: var(--ink); background: var(--gold); }

.book-form { display: grid; gap: 18px; }
.form-panel { padding: clamp(20px, 4vw, 32px); border-radius: 15px; background: rgba(255,250,242,.97); box-shadow: 0 9px 24px rgba(45,25,15,.12); }
.isbn-panel { display: grid; grid-template-columns: minmax(240px, 1fr) minmax(300px, 1.5fr); gap: 25px; align-items: center; }
.isbn-tools { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; }
.isbn-panel .form-status { grid-column: 1 / -1; margin: 0; }
.form-columns { display: grid; grid-template-columns: 1.1fr .9fr; gap: 18px; }
.stack-form { display: grid; gap: 16px; }
.stack-form label { display: grid; gap: 6px; font-weight: 600; }
.stack-form small { color: var(--muted); font-weight: 400; }
fieldset { margin: 0; padding: 14px; border: 1px solid var(--line); border-radius: 9px; }
legend { padding: 0 .35rem; font-weight: 700; }
.check-option { display: inline-flex !important; grid-template-columns: none !important; align-items: center; gap: .5rem !important; margin: .3rem 1rem .3rem 0; font-weight: 500 !important; }
.check-option input { width: 18px; height: 18px; margin: 0; accent-color: var(--rust); }
.genre-checks { max-height: 220px; overflow: auto; }
.genre-checks .check-option { display: flex !important; }
.form-cover-preview { width: 120px; max-height: 170px; object-fit: cover; border-radius: 5px; box-shadow: 0 6px 14px rgba(0,0,0,.18); }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; padding: 18px; border-radius: 12px; background: rgba(255,250,242,.93); }
.form-status { color: var(--muted); font-weight: 600; }
.status-success { color: var(--green); }
.status-error { color: var(--red); }
.scanner-dialog { width: min(650px, calc(100% - 20px)); }
.scanner-frame { position: relative; aspect-ratio: 4 / 3; overflow: hidden; border-radius: 10px; background: #1b1512; }
.scanner-frame video { width: 100%; height: 100%; object-fit: cover; }
.scanner-guide { position: absolute; inset: 32% 8%; border: 3px solid var(--gold); border-radius: 8px; box-shadow: 0 0 0 999px rgba(0,0,0,.28); }

.maintenance-section { margin-bottom: 24px; padding: clamp(20px, 4vw, 32px); border-radius: 16px; background: rgba(255,250,242,.97); box-shadow: var(--shadow); }
.section-title { display: flex; justify-content: space-between; }
.section-title p { margin-top: 0; color: var(--muted); }
.inline-create { display: grid; grid-template-columns: 1fr auto; gap: 8px; margin: 15px 0 22px; padding: 14px; border-radius: 10px; background: var(--paper-deep); }
.user-create { grid-template-columns: 1fr 1fr 130px auto; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 12px 10px; border-bottom: 1px solid var(--line); text-align: left; }
th { color: var(--muted); font-size: .76rem; letter-spacing: .08em; text-transform: uppercase; }
.table-actions, .user-edit { display: flex; gap: 7px; align-items: center; }
.table-actions form { margin: 0; }
.user-edit select { width: 105px; }
.user-edit input { width: 130px; }

.alert { margin: 0 0 18px; padding: 13px 16px; border: 1px solid #d5c2a8; border-radius: 9px; background: #fff9e8; }
.alert-success { color: #265343; border-color: #b9d5c5; background: #e5f2e9; }
.alert-warning { color: #6e5023; background: #fff3cf; }
.alert-danger { color: #7a2925; border-color: #e0b9b5; background: #f9e4e2; }

.login-page {
  background:
    linear-gradient(rgba(31,17,11,.48), rgba(31,17,11,.67)),
    repeating-linear-gradient(90deg, #7d5035 0 50px, #633c27 50px 56px, #9b623c 56px 91px, #493022 91px 98px),
    #342015;
}
.login-shell { display: grid; place-items: center; min-height: calc(100vh - 52px); padding: 25px; }
.login-card { width: min(440px, 100%); padding: clamp(30px, 6vw, 52px); border: 1px solid rgba(255,255,255,.45); border-radius: 20px; background: rgba(255,249,239,.97); box-shadow: 0 30px 90px rgba(22,11,7,.5); }
.login-emblem { width: 62px; height: 76px; margin-bottom: 25px; font-size: 2rem; }
.login-card h1 { font-size: clamp(2rem, 8vw, 2.8rem); }
.site-footer { min-height: 52px; padding: 17px; color: rgba(255,248,236,.76); background: var(--wood-dark); text-align: center; font-size: .8rem; }
.error-shell { display: grid; place-items: center; min-height: calc(100vh - 120px); padding: 30px; }
.error-shell .empty-state { width: min(600px, 100%); }
.error-code { font-size: 4rem !important; }

@media (max-width: 820px) {
  .nav-toggle { display: block; }
  .main-nav { position: absolute; left: 0; right: 0; top: 68px; display: none; align-items: stretch; padding: 10px 18px 18px; background: var(--wood-dark); box-shadow: 0 12px 20px rgba(0,0,0,.25); }
  .main-nav.open { display: grid; }
  .link-button { width: 100%; text-align: left; }
  .hero { align-items: flex-start; flex-direction: column; }
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  .menu-grid, .genre-grid { grid-template-columns: 1fr; }
  .menu-card { min-height: 230px; }
  .book-detail { grid-template-columns: 1fr; }
  .cover-large { width: min(300px, 100%); margin: auto; }
  .admin-grid, .form-columns { grid-template-columns: 1fr; }
  .isbn-panel { grid-template-columns: 1fr; }
  .isbn-tools { grid-template-columns: 1fr 1fr; }
  .isbn-tools input { grid-column: 1 / -1; }
  .user-create { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 560px) {
  .page-shell { width: min(100% - 20px, 1180px); padding-top: 24px; }
  .hero { padding: 25px 20px; }
  .stat { display: grid; padding: 14px; }
  .filter-bar { grid-template-columns: 1fr 1fr; }
  .filter-bar input { grid-column: 1 / -1; }
  .book-row { grid-template-columns: 65px 1fr; gap: 13px; }
  .cover-small { width: 62px; height: 84px; }
  .book-badges { grid-column: 2; justify-content: flex-start; }
  .book-detail { padding: 20px; }
  .page-heading { align-items: flex-start; flex-direction: column; }
  .admin-card { align-items: flex-start; flex-direction: column; }
  .isbn-tools { grid-template-columns: 1fr; }
  .isbn-tools input { grid-column: auto; }
  .form-actions { align-items: stretch; flex-direction: column; }
  .inline-create, .user-create { grid-template-columns: 1fr; }
  .table-actions { align-items: stretch; flex-direction: column; }
}

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