/* ==========================================================================
   shelf.css — the wooden bookcase and the CSS-drawn book spines
   Spine size:  data-size  xs|s|m|l|xl   → height
                data-thick 1..4           → width
   Texture:     data-binding leather|paper|board|cello, data-kind book|magazine
   Colour:      data-group + data-shade   → --bk (base.css)
   ========================================================================== */

.bookcase {
  --row: 268px;     /* one shelf row, board included */
  --board: 20px;    /* board thickness (books stand on it) */
  --k: 1;           /* spine scale */
  position: relative;
  padding: 0 14px 14px;
  border-radius: 12px 12px 6px 6px;
  background:
    repeating-linear-gradient(90deg, rgba(255, 255, 255, .035) 0 2px, transparent 2px 9px),
    repeating-linear-gradient(90deg, rgba(0, 0, 0, .06) 0 1px, transparent 1px 17px),
    linear-gradient(180deg, var(--wood-hi), var(--wood) 26%, var(--wood-2));
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, .22),
    inset 0 -2px 0 rgba(0, 0, 0, .25),
    0 22px 40px -16px rgba(46, 28, 15, .6),
    0 4px 10px rgba(46, 28, 15, .22);
}
.bookcase--few {
  width: fit-content;
  min-width: min(100%, 340px);
  max-width: 100%;
  margin-inline: auto;
}

.bookcase__crown {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 34px;
  margin-inline: -14px;
  border-radius: 12px 12px 0 0;
  background:
    repeating-linear-gradient(90deg, rgba(255, 255, 255, .04) 0 2px, transparent 2px 11px),
    linear-gradient(180deg, #9C6C43, var(--wood) 60%, #5A3920);
  border-bottom: 3px solid var(--wood-dark);
  box-shadow: 0 3px 6px rgba(0, 0, 0, .35);
  z-index: 1;
}
.bookcase__plate {
  max-width: calc(100% - 40px);
  padding: 1px 16px;
  border-radius: 3px;
  background: linear-gradient(180deg, #F0D9A2, #D2A452 55%, #B8893B);
  color: #2E1F07;
  font-family: var(--font-title);
  font-weight: 700;
  font-size: .95rem;
  line-height: 1.55;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  box-shadow: 0 1px 0 rgba(0, 0, 0, .45), inset 0 1px 0 rgba(255, 255, 255, .6), inset 0 -1px 0 rgba(0, 0, 0, .15);
}
.bookcase__plate::before, .bookcase__plate::after {
  /* the plate's two screws */
  content: "";
  display: inline-block;
  width: 4px;
  height: 4px;
  margin: 0 7px;
  border-radius: 50%;
  vertical-align: middle;
  background: #7A5A22;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .5);
}

/* the interior: back panel, shadows and boards drawn with repeating gradients */
.shelf {
  position: relative;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  column-gap: 2px;
  row-gap: 0;
  min-height: var(--row);
  margin: 0;
  padding: 0 12px;
  list-style: none;
  background:
    /* boards: front-edge highlight, body, dark underside */
    repeating-linear-gradient(180deg,
      transparent 0 calc(var(--row) - var(--board)),
      #B07C4C calc(var(--row) - var(--board)) calc(var(--row) - var(--board) + 2px),
      #8E613B calc(var(--row) - var(--board) + 2px) calc(var(--row) - var(--board) + 5px),
      #74492A calc(var(--row) - var(--board) + 5px) calc(var(--row) - 5px),
      #3A2413 calc(var(--row) - 5px) var(--row)),
    /* shadow cast by the board above + contact shadow behind the books */
    repeating-linear-gradient(180deg,
      rgba(0, 0, 0, .55) 0,
      rgba(0, 0, 0, 0) 36px,
      rgba(0, 0, 0, 0) calc(var(--row) - var(--board) - 14px),
      rgba(0, 0, 0, .32) calc(var(--row) - var(--board)),
      transparent calc(var(--row) - var(--board)) var(--row)),
    /* back panel grain */
    repeating-linear-gradient(90deg, rgba(0, 0, 0, .09) 0 2px, transparent 2px 26px),
    linear-gradient(90deg, #36200F, #4A2E19 10%, #4E311B 50%, #4A2E19 90%, #36200F);
  box-shadow:
    inset 12px 0 14px -10px rgba(0, 0, 0, .75),
    inset -12px 0 14px -10px rgba(0, 0, 0, .75);
}

.shelf__slot,
.shelf__bookend {
  display: flex;
  align-items: flex-end;
  height: var(--row);
  padding-bottom: var(--board);
}
.shelf__slot { transform-origin: 50% 100%; }

/* --------------------------------------------------------------------------
   Spine
   -------------------------------------------------------------------------- */
.spine {
  --w: 37px;
  --h: 192px;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: calc(var(--w) * var(--k, 1));
  height: calc(var(--h) * var(--k, 1));
  padding: calc(var(--h) * var(--k, 1) * .12) 0 calc(var(--h) * var(--k, 1) * .1);
  color: var(--gold-ink);
  text-decoration: none;
  background-color: var(--bk, #5C5446);
  border-radius: 3px 3px 1px 1px;
  box-shadow:
    inset 3px 0 4px -2px rgba(255, 255, 255, .2),
    inset -4px 0 5px -2px rgba(0, 0, 0, .45),
    0 0 0 .5px rgba(0, 0, 0, .4);
  transform-origin: 50% 100%;
  transition: transform .3s var(--ease);
  -webkit-tap-highlight-color: transparent;
  cursor: pointer;
}
.spine:hover, .spine:focus { color: var(--gold-ink); }

/* size → height */
.spine[data-size="xs"] { --h: 150px; }
.spine[data-size="s"]  { --h: 171px; }
.spine[data-size="m"]  { --h: 192px; }
.spine[data-size="l"]  { --h: 213px; }
.spine[data-size="xl"] { --h: 234px; }
/* thickness → width */
.spine[data-thick="1"] { --w: 30px; }
.spine[data-thick="2"] { --w: 37px; }
.spine[data-thick="3"] { --w: 44px; }
.spine[data-thick="4"] { --w: 52px; }
.spine[data-kind="magazine"][data-thick="1"] { --w: 20px; }
.spine[data-kind="magazine"][data-thick="2"] { --w: 23px; }
.spine[data-kind="magazine"][data-thick="3"] { --w: 26px; }
.spine[data-kind="magazine"][data-thick="4"] { --w: 29px; }

.spine__title {
  flex: 1 1 auto;
  min-height: 0;
  max-height: 100%;
  writing-mode: vertical-rl;
  text-orientation: mixed;
  font-family: var(--font-title);
  font-weight: 700;
  font-size: clamp(10px, calc(var(--w) * var(--k, 1) * .4), 17px);
  line-height: 1.2;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-block: 3px;
  text-shadow: 0 1px 0 rgba(0, 0, 0, .5);
  pointer-events: none;
}
.spine__plate {
  flex: none;
  max-width: 84%;
  min-width: 62%;
  margin-top: 5px;
  padding: 1px 2px;
  border-radius: 2px;
  background: linear-gradient(180deg, #EFDDAA, #D6AE62);
  color: #2A1D08;
  font: 700 10px/1.35 var(--font-ui);
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  box-shadow: 0 1px 0 rgba(0, 0, 0, .35), inset 0 0 0 .5px rgba(0, 0, 0, .25);
  pointer-events: none;
}
.spine__shadow {
  position: absolute;
  inset: 0;
  border-radius: inherit;
  box-shadow: 0 16px 20px -8px rgba(0, 0, 0, .65), 0 0 0 1px rgba(232, 207, 143, .35);
  opacity: 0;
  transition: opacity .3s var(--ease);
  pointer-events: none;
}
.spine__ribbon {
  /* bookmark ribbon: the title itself matches the search */
  position: absolute;
  top: -12px;
  left: 26%;
  width: 7px;
  height: 20px;
  background: linear-gradient(90deg, #9E2F2A, #C4453C 50%, #9E2F2A);
  clip-path: polygon(0 0, 100% 0, 100% 100%, 50% 78%, 0 100%);
  pointer-events: none;
}

/* bindings ------------------------------------------------------------------ */
.spine[data-binding="leather"] {
  background-image:
    linear-gradient(90deg, rgba(0, 0, 0, .4) 0%, rgba(0, 0, 0, .06) 16%, rgba(255, 255, 255, .15) 40%, rgba(255, 255, 255, .05) 58%, rgba(0, 0, 0, .12) 80%, rgba(0, 0, 0, .42) 100%),
    radial-gradient(rgba(0, 0, 0, .09) 1px, transparent 1.2px);
  background-size: auto, 4px 4px;
}
.spine[data-binding="leather"]::before,
.spine[data-binding="leather"]::after {
  /* raised bands with gold fillets */
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  height: 7px;
  border-block: 1.5px solid #D9B26A;
  background: linear-gradient(180deg, rgba(255, 255, 255, .28), rgba(0, 0, 0, .28)), var(--bk, #5C5446);
  box-shadow: 0 2px 2px rgba(0, 0, 0, .35), 0 -1px 0 rgba(255, 255, 255, .1);
  pointer-events: none;
}
.spine[data-binding="leather"]::before { top: calc(var(--h) * var(--k, 1) * .045); }
.spine[data-binding="leather"]::after { bottom: calc(var(--h) * var(--k, 1) * .04); }

.spine[data-binding="paper"] {
  background-image:
    linear-gradient(90deg, rgba(0, 0, 0, .2), rgba(255, 255, 255, .07) 30%, rgba(0, 0, 0, .02) 70%, rgba(0, 0, 0, .22)),
    repeating-linear-gradient(0deg, rgba(255, 255, 255, .025) 0 1px, transparent 1px 3px);
  border-radius: 1px;
}
.spine[data-binding="paper"]::before {
  /* small paper label */
  content: "";
  position: absolute;
  top: calc(var(--h) * var(--k, 1) * .03);
  left: 17%;
  right: 17%;
  height: calc(var(--h) * var(--k, 1) * .06);
  border-radius: 1px;
  background: repeating-linear-gradient(180deg, #F4EBD6 0 3px, #DCCDAA 3px 4px);
  box-shadow: 0 0 0 .5px rgba(0, 0, 0, .3);
  pointer-events: none;
}

.spine[data-binding="board"] {
  background-image:
    linear-gradient(90deg, rgba(0, 0, 0, .28), rgba(0, 0, 0, 0) 20%, rgba(0, 0, 0, 0) 80%, rgba(0, 0, 0, .28)),
    linear-gradient(180deg, transparent 0 7%, rgba(255, 255, 255, .13) 7% 17%, transparent 17% 84%, rgba(0, 0, 0, .14) 84% 93%, transparent 93%);
  border-radius: 2px;
}

.spine[data-binding="cello"] {
  background-image:
    linear-gradient(90deg, rgba(0, 0, 0, .26) 0%, rgba(255, 255, 255, 0) 18%, rgba(255, 255, 255, .45) 32%, rgba(255, 255, 255, .1) 42%, rgba(0, 0, 0, .04) 70%, rgba(0, 0, 0, .32) 100%),
    linear-gradient(180deg, rgba(255, 255, 255, .12), transparent 30%);
}
.spine[data-binding="cello"]::before {
  content: "";
  position: absolute;
  top: calc(var(--h) * var(--k, 1) * .05);
  left: 0;
  right: 0;
  height: 2px;
  background: rgba(232, 207, 143, .75);
  pointer-events: none;
}

/* magazines: thinner, coloured band, issue number */
.spine[data-kind="magazine"] {
  border-radius: 1px;
  padding-top: calc(var(--h) * var(--k, 1) * .2);
}
.spine[data-kind="magazine"]::before {
  content: "";
  position: absolute;
  top: calc(var(--h) * var(--k, 1) * .05);
  left: 0;
  right: 0;
  height: calc(var(--h) * var(--k, 1) * .1);
  border: 0;
  border-radius: 0;
  background: #E8CF8F;
  background: color-mix(in srgb, var(--bk, #5C5446) 30%, #F1E3BE);
  box-shadow: 0 1px 0 rgba(0, 0, 0, .25);
}
.spine[data-kind="magazine"]::after { content: none; }
.spine[data-kind="magazine"] .spine__title { font-size: clamp(10px, calc(var(--w) * var(--k, 1) * .5), 13px); }
.spine[data-kind="magazine"] .spine__plate {
  writing-mode: vertical-rl;
  min-width: 0;
  max-width: none;
  max-height: 34%;
  padding: 3px 1px;
  font-size: 9.5px;
  text-overflow: clip;
}

/* interactions ---------------------------------------------------------------- */
@media (hover: hover) {
  .spine:hover { transform: translate3d(0, -12px, 0); }
  .spine:hover .spine__shadow { opacity: 1; }
}
.spine:focus-visible {
  transform: translate3d(0, -12px, 0);
  outline: 3px solid var(--gold-2);
  outline-offset: 3px;
  border-radius: 3px;
}
.spine:focus-visible .spine__shadow { opacity: 1; }
.spine:active { transform: translate3d(0, -9px, 0); }

.spine.is-current {
  transform: translate3d(0, -8px, 0);
  box-shadow:
    inset 3px 0 4px -2px rgba(255, 255, 255, .2),
    inset -4px 0 5px -2px rgba(0, 0, 0, .45),
    0 0 0 2px var(--gold-2),
    0 0 16px 2px rgba(210, 178, 108, .55);
}

/* neighbours lean into the gap left by a pulled book */
.spine.lean-r { transform-origin: 100% 100%; transform: rotate(2.4deg); transition: transform .45s var(--spring); }
.spine.lean-l { transform-origin: 0 100%; transform: rotate(-2.4deg); transition: transform .45s var(--spring); }

/* the pulled book leaves a gap (the slot keeps its width) */
.shelf__slot.is-away .spine {
  visibility: hidden;
  transform: none;
  transition: none;
}
.shelf__slot.is-back .spine { animation: spine-back .5s var(--spring); }

/* first paint: books slide onto the shelf, staggered (see animations.css) */
html.js .shelf:not(.is-in) .shelf__slot {
  opacity: 0;
  animation: failsafe-show 0s 2.5s forwards;
}
.shelf.is-in .shelf__slot {
  animation: slot-settle .75s var(--spring) backwards;
  animation-delay: calc(min(var(--i, 0), 24) * 26ms);
}

/* brass bookend after the last book */
.shelf__bookend { margin-inline-start: 4px; }
.shelf__bookend::before {
  content: "";
  width: 11px;
  height: calc(118px * var(--k, 1));
  border-radius: 7px 7px 1px 1px;
  background: linear-gradient(90deg, #7A5A22, #D9B26A 38%, #F0D9A2 50%, #B8893B 70%, #6F511F);
  box-shadow: 2px 0 4px rgba(0, 0, 0, .45), -1px 0 0 rgba(0, 0, 0, .3);
}

/* bookcase base (plinth) */
.bookcase__base {
  height: 6px;
  margin: 0 -8px -14px;
  border-radius: 0 0 6px 6px;
  background: linear-gradient(180deg, #3A2413, #2A190C);
}

/* --------------------------------------------------------------------------
   Tooltip card
   -------------------------------------------------------------------------- */
.spine-tip {
  position: fixed;
  z-index: 60;
  top: 0;
  left: 0;
  width: max-content;
  max-width: min(280px, calc(100vw - 24px));
  padding: 10px 14px 11px;
  border-radius: 12px;
  background: var(--card);
  color: var(--ink);
  border: 1px solid var(--line);
  border-top: 3px solid var(--tip-c, var(--gold));
  box-shadow: var(--sh-2);
  pointer-events: none;
  opacity: 0;
  transform: translate3d(var(--x, 0), calc(var(--y, 0) + 6px), 0);
  transition: opacity .16s ease, transform .2s var(--ease);
  text-align: start;
}
.spine-tip.is-on { opacity: 1; transform: translate3d(var(--x, 0), var(--y, 0), 0); }
.spine-tip::after {
  content: "";
  position: absolute;
  top: 100%;
  left: var(--ax, 50%);
  width: 12px;
  height: 12px;
  margin: -6px 0 0 -6px;
  background: var(--card);
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  transform: rotate(45deg);
}
.spine-tip.is-below::after {
  top: auto;
  bottom: 100%;
  margin: 0 0 -6px -6px;
  transform: rotate(-135deg);
}
.spine-tip__title {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
  font-family: var(--font-title);
  font-weight: 700;
  font-size: 1.06rem;
  line-height: 1.45;
}
.spine-tip__author { display: block; font-size: .875rem; color: var(--ink-2); line-height: 1.5; }
.spine-tip__meta { display: block; margin-top: 2px; font-size: .8125rem; color: var(--muted); line-height: 1.5; }
.spine-tip__hint { display: block; margin-top: 4px; font-size: .75rem; color: var(--gold-text); }

/* --------------------------------------------------------------------------
   Responsive rows
   -------------------------------------------------------------------------- */
@media (max-width: 599px) {
  .bookcase { --k: .72; --row: 197px; --board: 15px; padding: 0 8px 10px; }
  .bookcase__crown { margin-inline: -8px; height: 30px; }
  .shelf { padding: 0 6px; column-gap: 1px; }
  .spine__plate { font-size: 9px; }
  .bookcase__base { margin: 0 -4px -10px; }
}
@media (min-width: 600px) and (max-width: 899px) {
  .bookcase { --k: .86; --row: 233px; --board: 18px; }
}
