/* The Bindery — lamplit workshop (dark) or reading room (light). Covers stand on wooden shelves.
   Contrast floor: body text ≥ 7:1, secondary ≥ 4.5:1, nothing under 12px; Cormorant only at ≥ 18px. */
:root, :root[data-theme="dark"] {
  --bg: #120f0b; --panel: #1d1813; --panel-2: #262019; --panel-3: #30281f; --field: #0e0c09;
  --ink: #f2e9d6;        /* 15:1 on bg */
  --ink-2: #d6cab0;      /* 11:1 */
  --ink-3: #b3a78f;      /* 7.4:1 — replaces #8f8470 (5.2:1, at 10px) */
  --placeholder: #8a8070;/* 4.9:1 — was #5f5647 (2.6:1) */
  --accent: #e0bb6a; --accent-hi: #f3d891; --accent-ink: #2a1d08;
  --ok: #a9d394; --err: #f09a86; --warn: #f0c46a;
  --line: rgba(212, 173, 94, .22); --line-2: rgba(212, 173, 94, .45);
  --wood-hi: #8a6240; --wood: #6b4a2e; --wood-lo: #3b2716; --wall: #17120d;
  --edge: #a8956c;       /* non-text edges (buttons, fields, checkboxes): 6.9:1 on bg */
  --head: #2b231b;       /* shelf header, set off from the page */
  --shadow: rgba(0, 0, 0, .65);
  color-scheme: dark;
}
:root[data-theme="light"] {
  --bg: #f4efe6; --panel: #fffdf8; --panel-2: #f3ece0; --panel-3: #e8dfcf; --field: #ffffff;
  --ink: #1f1a14; --ink-2: #3d352a; --ink-3: #5c5244; --placeholder: #7d7262;
  --accent: #8a5a12; --accent-hi: #6f4708; --accent-ink: #2a1d08;
  --ok: #2f6b1f; --err: #a3321f; --warn: #7a5200;
  --line: rgba(110, 80, 30, .2); --line-2: rgba(110, 80, 30, .4);
  --wood-hi: #b08a5a; --wood: #8e6a3e; --wood-lo: #5e4424; --wall: #d9cbb2; /* dark leather 5–8:1 on this wall */
  --edge: #7d6c52;       /* 4.3:1 on bg */
  --head: #e9dfcc;
  --shadow: rgba(60, 40, 10, .35);
  color-scheme: light;
}
:root {
  --display: 'Cormorant Garamond', 'Iowan Old Style', 'Palatino Linotype', Georgia, serif;
  --sans: 'Instrument Sans', 'Avenir Next', 'Segoe UI', system-ui, sans-serif;
  --mono: 'IBM Plex Mono', ui-monospace, Menlo, monospace;
  --libbar-h: 56px;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { background: var(--bg); }
body { margin: 0; overflow-x: clip; color: var(--ink); background: var(--bg); font: 15px/1.5 var(--sans); min-height: 100vh; }
body.has-selbar { padding-bottom: 4.5rem; }
small { color: var(--ink-3); font-weight: 400; font-size: 13px; }
h3 { font: 600 20px/1.2 var(--display); color: var(--accent-hi); margin: 1.4rem 0 .3rem; }
::selection { background: rgba(212, 173, 94, .35); }
/* Read-only visitors: no write controls at all. */
body.ro [data-write] { display: none !important; }
body:not(.ro) [data-read] { display: none !important; }

/* ---------------------------------------------------------------- library bar (sticky) */
.libbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; flex-wrap: wrap; gap: .5rem 1rem;
  padding: .6rem 1rem; background: color-mix(in srgb, var(--bg) 92%, transparent); backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--edge);
}
.brand { display: flex; align-items: center; gap: .4rem; }
.mark { font-family: var(--display); color: var(--accent); font-size: 26px; line-height: 1; }
.mark.big { font-size: 46px; text-align: center; }
.brand-name { font: 700 24px/1 var(--display); }
.lib-select { font: 700 20px var(--display); padding: .25rem 2rem .25rem .6rem; }
.lib-stats { color: var(--ink-3); font-size: 13px; }
.look-state { color: var(--ink-2); font-size: 13px; }
.look-state b { color: var(--accent-hi); }
.spacer { flex: 1; }
.lib-actions { display: flex; align-items: center; gap: .4rem; flex-wrap: wrap; }
.shelf-jump { font-size: 13px; padding: .35rem 1.9rem .35rem .6rem; }
.save-state { color: var(--ink-3); font-size: 12px; min-width: 4em; }
.more { position: relative; }
.more summary { list-style: none; }
.more summary::-webkit-details-marker { display: none; }
.more-menu { position: absolute; right: 0; top: calc(100% + .3rem); display: grid; gap: .2rem; padding: .4rem; min-width: 10rem;
  background: var(--panel); border: 1px solid var(--line-2); border-radius: 4px; box-shadow: 0 12px 30px var(--shadow); z-index: 30; }

/* ---------------------------------------------------------------- controls */
input, select, textarea {
  font: inherit; font-size: 15px; color: var(--ink); background: var(--field); border: 1px solid var(--edge); border-radius: 3px;
  padding: .5rem .6rem; min-width: 0;
}
select { appearance: none; padding-right: 1.9rem; cursor: pointer;
  background: var(--field) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%23b08a3e'/%3E%3C/svg%3E") no-repeat right .7rem center; }
textarea { resize: vertical; width: 100%; line-height: 1.5; }
input::placeholder, textarea::placeholder { color: var(--placeholder); }
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible, summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
input[readonly], textarea[readonly] { background: transparent; border-style: dashed; }
/* Custom checkbox: the native unchecked box vanishes on dark panels. */
input[type=checkbox] { appearance: none; width: 20px; height: 20px; padding: 0; margin: 0; flex: none; cursor: pointer; border: 2px solid var(--edge); border-radius: 4px; background: var(--field); display: inline-grid; place-content: center; }
input[type=checkbox]:checked { background: var(--accent); border-color: var(--accent); }
input[type=checkbox]:checked::after { content: ''; width: 5px; height: 10px; margin-top: -2px; border: solid var(--bg); border-width: 0 2.5px 2.5px 0; transform: rotate(45deg); }
input[type=checkbox]:disabled { opacity: .5; cursor: not-allowed; }
.field { display: grid; gap: .3rem; margin-bottom: .8rem; }
.field > span { font-size: 13px; font-weight: 600; color: var(--ink-2); }
.field.inline { display: flex; align-items: center; gap: .5rem; margin: 0; }
.row { display: flex; align-items: center; gap: .55rem; }
.row.wrap { flex-wrap: wrap; }
.hint { color: var(--ink-3); font-size: 13px; margin: .4rem 0; }
.check { display: flex; align-items: center; gap: .5rem; color: var(--ink-2); cursor: pointer; margin: .4rem 0; }

.btn {
  font: 600 14px var(--sans); color: var(--ink); cursor: pointer; white-space: nowrap;
  padding: .5rem .95rem; border-radius: 3px; border: 1px solid var(--edge);
  background: var(--panel-2);
  transition: border-color .2s, filter .2s;
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.small { padding: .35rem .7rem; font-size: 13px; }
.btn.wide { width: 100%; padding: .7rem; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--err); }
.btn.gold { color: #2a1d08; border-color: #6e5017; background: linear-gradient(180deg, #f3d891 0%, #d4ad5e 50%, #c29a48 100%); }
.btn.gold:hover:not(:disabled) { filter: brightness(1.07); }
.x { border: 0; background: none; font-size: 28px; line-height: 1; cursor: pointer; color: var(--ink-2); padding: 0 .3rem; }
.x:hover { color: var(--ink); }

/* ---------------------------------------------------------------- shelves */
.shelves { padding: 0 0 2rem; }
.shelf-head {
  position: sticky; top: var(--libbar-h); z-index: 10; display: flex; align-items: center; flex-wrap: wrap; gap: .3rem .8rem;
  padding: .55rem 1rem; background: var(--head); border-bottom: 1px solid var(--edge); border-top: 1px solid var(--edge);
}
.shelf-toggle { display: flex; align-items: center; gap: .45rem; border: 0; background: none; color: inherit; cursor: pointer; padding: 0; }
.shelf-toggle .chev { color: var(--accent); transition: transform .2s; font-size: 14px; }
.shelf-toggle[aria-expanded="true"] .chev { transform: rotate(90deg); }
.shelf-head h2 { margin: 0; font: 700 24px/1.1 var(--display); color: var(--ink); }
.shelf-count { color: var(--ink-3); font-size: 13px; }
.shelf-style-line { flex-basis: 100%; min-width: 0; margin: 0; color: var(--ink-3); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* The grid: fixed row height so a shelf board sits under every row of covers. */
.grid {
  /* Fixed cover size: a wider window shows MORE covers, never bigger ones. */
  --row: 330px; --board: 240px; --col: 150px;
  display: grid; grid-template-columns: repeat(auto-fill, var(--col)); grid-auto-rows: var(--row);
  column-gap: 1.25rem; padding: .4rem 1rem 0;
  background:
    repeating-linear-gradient(180deg, transparent 0 var(--board),
      var(--wood) var(--board), var(--wood-hi) calc(var(--board) + 2px), var(--wood) calc(var(--board) + 6px), var(--wood-lo) calc(var(--board) + 14px),
      var(--shadow) calc(var(--board) + 14px), transparent calc(var(--board) + 36px), transparent var(--row)),
    var(--wall);
}
@media (max-width: 600px) { .grid { --row: 270px; --board: 190px; --col: 112px; column-gap: .7rem; padding: .4rem .6rem 0; } }
.card { position: relative; display: grid; grid-template-rows: var(--board) 1fr; min-width: 0; }
.card .thumb { display: flex; align-items: flex-end; justify-content: center; padding: 1.4rem .4rem 0; cursor: pointer; min-height: 0; }
.card .thumb img { max-width: 100%; max-height: 100%; object-fit: contain; transform-origin: 50% 100%;
  filter: drop-shadow(0 8px 7px var(--shadow)); transition: transform .3s cubic-bezier(.2, .8, .2, 1); }
.card .thumb:hover img { transform: translateY(-6px); }
.card .thumb .none {
  width: 74%; aspect-ratio: 3 / 4.2; border: 2px dashed var(--edge); border-radius: 2px 4px 4px 2px;
  display: grid; place-items: center; color: var(--ink-2); font-size: 13px; text-align: center; padding: .5rem; overflow: hidden;
  background: color-mix(in srgb, var(--panel) 85%, transparent);
}
.card .meta { padding: .8rem .1rem 0; display: grid; gap: .1rem; align-content: start; text-align: center; overflow: hidden; }
.card .t { font: 600 14px/1.25 var(--sans); color: var(--ink); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card .a { font-size: 13px; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .tags { font-size: 12px; color: var(--ink-3); }
.card .pick { position: absolute; top: .4rem; left: .2rem; z-index: 2; }
.card .star { position: absolute; top: .25rem; right: .1rem; z-index: 2; border: 0; background: none; cursor: pointer; font-size: 20px; line-height: 1;
  color: var(--edge); padding: .15rem; }
.card .star.on { color: var(--accent); opacity: 1; }
.card .star:disabled { cursor: default; }
.card .star:disabled:not(.on) { display: none; }
.card.sel .thumb img, .card.sel .thumb .none { outline: 3px solid var(--accent); outline-offset: 3px; }
.card .state {
  position: absolute; top: 2rem; left: 50%; transform: translateX(-50%); z-index: 2; font-size: 12px; font-weight: 600; white-space: nowrap;
  padding: .15rem .55rem; border-radius: 20px; background: var(--panel); border: 1px solid var(--line-2); color: var(--ink);
}
.card .state.err { color: var(--err); border-color: var(--err); }
.card .state.warn { color: var(--warn); border-color: var(--warn); }
.card .state.busy::before { content: ''; display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); margin-right: .35rem; animation: pulse 1s infinite; }
.empty { color: var(--ink-2); text-align: center; padding: 4rem 1rem; font-size: 17px; }

/* ---------------------------------------------------------------- selection bar */
.selbar {
  position: fixed; left: 50%; bottom: .8rem; transform: translateX(-50%); z-index: 25; display: flex; align-items: center; flex-wrap: wrap; gap: .45rem;
  padding: .55rem .8rem; max-width: calc(100vw - 1rem); background: var(--panel); border: 1px solid var(--edge); border-radius: 6px; box-shadow: 0 14px 40px var(--shadow);
}
.sel-count { font-weight: 600; margin-right: .3rem; }
.selbar select { font-size: 13px; padding: .35rem 1.9rem .35rem .6rem; }

/* ---------------------------------------------------------------- dialogs */
.dlg {
  width: min(680px, 96vw); max-height: 92vh; padding: 0; color: var(--ink); background: var(--panel);
  border: 1px solid var(--line-2); border-radius: 6px; box-shadow: 0 40px 120px var(--shadow);
}
.dlg.wide { width: min(1300px, 98vw); }
.dlg::backdrop { background: rgba(6, 5, 3, .6); backdrop-filter: blur(3px); }
.dlg-head { position: sticky; top: 0; z-index: 2; display: flex; align-items: center; gap: .7rem; padding: .8rem 1.2rem; background: var(--panel); border-bottom: 1px solid var(--line); }
.dlg-head h2 { flex: 1; margin: 0; font: 700 26px/1.1 var(--display); color: var(--accent-hi); }
.dlg-body { padding: 1rem 1.2rem 1.3rem; overflow: auto; max-height: calc(92vh - 4rem); }
@media (max-width: 600px) { .dlg { width: 100vw; max-width: 100vw; max-height: 100dvh; height: 100dvh; margin: 0; border-radius: 0; } .dlg-body, .im-body { max-height: calc(100dvh - 4rem); } }
.layer-card { border: 1px solid var(--line); border-radius: 4px; padding: .6rem; margin-bottom: .6rem; background: var(--panel-2); }
.layer-card .row { margin-bottom: .4rem; }
.layer-card input.name { flex: 1; font-weight: 600; }
.count { font-size: 13px; color: var(--ink-3); white-space: nowrap; }
.name-row { margin-bottom: .4rem; }
.name-row input { flex: 1; }
.inherit { border: 1px solid var(--line); border-radius: 4px; padding: .5rem .75rem; margin: .4rem 0 .8rem; background: var(--panel-2); }
.inherit summary { cursor: pointer; font-weight: 600; color: var(--ink-2); }
.inherit p { margin: .45rem 0 .1rem; color: var(--ink-2); font-size: 14px; }
.inherit pre { white-space: pre-wrap; font: 12.5px/1.55 var(--mono); color: var(--ink-2); max-height: 260px; overflow: auto; }
.layer-line { font-size: 14px; margin: .45rem 0; color: var(--ink-2); }
.layer-line b { color: var(--accent); font-size: 12px; text-transform: uppercase; letter-spacing: .06em; margin-right: .2rem; }
.tabs { display: flex; gap: 1.2rem; margin-bottom: .75rem; border-bottom: 1px solid var(--line); }
.tab { font: 600 15px var(--sans); color: var(--ink-3); border: 0; background: none; padding: .25rem 0 .5rem; cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tab.on { color: var(--ink); border-bottom-color: var(--accent); }
#csv-in { font: 13px/1.5 var(--mono); }

/* Looks matrix: rows = looks, columns = books */
.looks { overflow: auto; }
.looks table { border-collapse: separate; border-spacing: .5rem; }
.looks thead th { font-size: 12px; font-weight: 600; color: var(--ink-2); max-width: 130px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: center; }
.looks tbody th { width: 260px; min-width: 220px; text-align: left; vertical-align: top; font-weight: 400; position: sticky; left: 0; background: var(--panel); z-index: 1; }
.looks .look-name { width: 100%; font-weight: 700; margin-bottom: .2rem; }
.looks .look-notes { margin: .3rem 0; font-size: 14px; }
.looks td { vertical-align: bottom; }
.look-cell { border: 0; padding: .3rem; background: var(--wall); border-radius: 4px; cursor: pointer; width: 130px; height: 170px; display: grid; place-items: end center; }
.look-cell img { max-width: 100%; max-height: 100%; filter: drop-shadow(0 4px 4px var(--shadow)); }

/* ---------------------------------------------------------------- Image Manager */
.imgmgr { width: min(1180px, 98vw); }
.im-body { display: grid; grid-template-columns: 1fr 1fr; gap: 1.4rem; padding: 1.1rem 1.2rem 1.3rem; max-height: calc(92vh - 4rem); overflow: auto; }
@media (max-width: 860px) { .im-body { grid-template-columns: 1fr; } .im-body > .im-col:last-child { order: -1; } }
.cover-grid { display: grid; grid-template-columns: 1.4fr 1fr 1fr; gap: .5rem; }
@media (max-width: 600px) { .cover-grid { grid-template-columns: 1fr; } }
.actions { margin: .9rem 0 .8rem; }
.im-preview-wrap { position: relative; }
.im-preview { position: relative; aspect-ratio: 3 / 4; display: grid; place-items: center; border-radius: 4px; border: 1px solid var(--line-2); overflow: hidden; }
.im-preview img { max-width: 82%; max-height: 84%; filter: drop-shadow(0 18px 18px rgba(0, 0, 0, .45)); }
.im-preview .hint { font-size: 16px; }
/* Backdrops to judge the cut-out: an animated sweep through dark, colour and light, or a fixed one. */
.bd-sweep { background: linear-gradient(120deg, #0b0906, #3a1d4a, #1d4a6b, #e9e2d3, #ffffff, #c96b2c, #0b0906); background-size: 600% 600%; animation: sweep 14s ease-in-out infinite; }
.bd-dark { background: #0b0906; }
.bd-light { background: #ffffff; }
.bd-checker { background: repeating-conic-gradient(#cfcfcf 0 25%, #ffffff 0 50%) 0 0 / 22px 22px; }
.bd-sweep .hint, .bd-dark .hint { color: #d6cab0; }
.bd-light .hint, .bd-checker .hint { color: #3d352a; }
.backdrops { position: absolute; bottom: .5rem; left: 50%; transform: translateX(-50%); display: flex; gap: .3rem; padding: .25rem; border-radius: 20px; background: rgba(0, 0, 0, .55); }
.bd { font: 600 12px var(--sans); text-transform: capitalize; color: #f2e9d6; background: transparent; border: 1px solid transparent; border-radius: 14px; padding: .2rem .6rem; cursor: pointer; }
.bd[aria-pressed="true"] { border-color: #f3d891; color: #f3d891; }
@keyframes sweep { 0%, 100% { background-position: 0% 50%; } 50% { background-position: 100% 50%; } }
.im-spin { position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: .8rem; background: rgba(12, 9, 6, .72); border-radius: 4px; color: #f3d891; font-size: 16px; }
.spinner { width: 40px; height: 40px; border: 3px solid rgba(212, 173, 94, .25); border-top-color: #e0bb6a; border-radius: 50%; animation: spin .9s linear infinite; }
.im-status-row { display: flex; align-items: center; gap: .6rem; min-height: 1.8rem; margin-top: .6rem; }
.status { font-size: 14px; color: var(--ink-2); }
.status.err { color: var(--err); }
.status.ok { color: var(--ok); }
.badge { font-size: 12px; font-weight: 600; padding: .15rem .5rem; border-radius: 20px; border: 1px solid var(--warn); color: var(--warn); }
.strip { display: flex; gap: .6rem; overflow-x: auto; padding: .4rem .2rem .6rem; }
.ver { flex: 0 0 92px; display: grid; grid-template-rows: 110px auto auto; justify-items: center; gap: .1rem; padding: .3rem; border: 1px solid var(--line); border-radius: 4px; cursor: pointer; background: var(--wall); color: var(--ink); }
.ver img { max-width: 100%; max-height: 110px; align-self: end; filter: drop-shadow(0 3px 3px var(--shadow)); }
.ver.on { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.ver-lab { font-size: 12px; font-weight: 600; max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ver-date { font-size: 12px; color: var(--ink-3); }

/* ---------------------------------------------------------------- login */
.login { width: min(380px, 92vw); padding: 2rem 2.2rem 1.8rem; }
.login h2 { margin: .3rem 0 1.2rem; text-align: center; font: 700 34px var(--display); color: var(--accent-hi); }

@keyframes pulse { 50% { opacity: .25; } }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { *, *::before { animation: none !important; transition: none !important; } }
