#dialog[data-kind="mode-library"] {
  width: min(95vw, 920px, calc(100% - var(--phone-left, 0px) - var(--phone-right, 0px)));
  background: #fffaf0;
}

#dialog[data-kind="mode-library"] .dialog-content { padding: clamp(14px, 2.6vw, 25px); }
#dialog[data-kind="mode-library"] h2 { display: flex; align-items: center; min-height: 44px; margin-bottom: 8px; padding-right: 46px; }
#dialog[data-kind="mode-library"] .dialog-close { min-width: 44px; min-height: 44px; }
.library-head { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; padding-right: 47px; }
#dialog[data-kind="mode-library"] .library-head h2 { min-width: 0; margin: 0; padding: 0; }
.library-practice {
  display: inline-flex; align-items: center; justify-content: center; flex: none; gap: 5px;
  min-height: 44px; padding: 3px 9px; margin-left: auto;
  background: #f5f0e3; border: 1px solid #aa9d89; border-radius: 8px;
  color: #4c4a55; font-size: 12px; font-weight: 790; white-space: nowrap;
}
.library-practice .glyph { width: 16px; height: 16px; color: #6f5a8d; }
.library-finder + .library-practice { margin-left: 0; }
.library-practice:hover, .library-practice:focus-visible { border-color: #64517a; background: #eee5ee; }

.library-challenges {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 14px;
  padding-bottom: 12px;
  border-bottom: 1px solid #74596a29;
}

.library-challenge {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  min-height: 62px;
  padding: 7px 9px;
  border: 1px solid var(--challenge-trim);
  border-radius: 11px;
  background: var(--challenge-paper);
  color: var(--challenge-ink);
  text-align: left;
  box-shadow: 0 2px 0 var(--challenge-trim);
  transition: transform 150ms ease, box-shadow 150ms ease;
}

.library-challenge[data-mode="echo"] { --challenge-paper: #f0e8fb; --challenge-trim: #bba4d6; --challenge-ink: #4f3672; --challenge-mark: #614986; --challenge-light: #ffdc97; }
.library-challenge[data-mode="nursery"] { --challenge-paper: #e9f1dc; --challenge-trim: #aabd90; --challenge-ink: #3f5a36; --challenge-mark: #4b6a49; --challenge-light: #eaf3ba; }
.library-challenge[data-mode="relay"] { --challenge-paper: #ddf0ed; --challenge-trim: #90b8b7; --challenge-ink: #285c62; --challenge-mark: #2c6971; --challenge-light: #fae4aa; }
.library-challenge[data-mode="harbor"] { --challenge-paper: #e6edf5; --challenge-trim: #9db6c9; --challenge-ink: #284c62; --challenge-mark: #396481; --challenge-light: #ffdb9e; }

@media (max-width: 540px) {
  .library-challenges { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

.library-challenge-icon {
  display: grid;
  place-items: center;
  width: 37px;
  aspect-ratio: 1;
  border: 1px solid #2b12284d;
  border-radius: 8px;
  box-shadow: inset 0 1px 0 #ffffff36;
  color: var(--challenge-light);
  background: linear-gradient(#ffffff17, transparent 64%), var(--challenge-mark);
}

.library-challenge-icon .glyph { width: 23px; height: 23px; }
.library-challenge-copy { display: grid; gap: 3px; line-height: 1.25; }
.library-challenge-copy strong { font-size: 17px; font-weight: 950; }
.library-challenge-copy span { font-size: 11px; font-weight: 700; }
.library-challenge-arrow .glyph { width: 14px; height: 14px; }
.library-challenge:focus-visible { outline-offset: 2px; z-index: 1; }
.library-challenge:active { transform: translateY(1px); box-shadow: 0 1px 0 var(--challenge-trim); }

@media (hover: hover) {
  .library-challenge:hover { transform: translateY(-1px); box-shadow: 0 3px 0 var(--challenge-trim); }
}

.library-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.library-card {
  position: relative;
  display: grid;
  grid-template-columns: clamp(61px, 8.7vw, 88px) minmax(0, 1fr);
  min-height: 118px;
  padding: 0;
  overflow: hidden;
  border: 1px solid var(--trim);
  border-radius: 13px;
  background: var(--paper);
  color: #302a43;
  text-align: left;
  box-shadow: 0 3px 0 var(--lip);
  transition: transform 150ms ease, box-shadow 150ms ease;
}

.library-card[data-mode="ball"] { --paper: #eaf3dd; --trim: #9bb58a; --lip: #d6e2c8; --tone: #355d40; --sea: #1d4d52; --sky: #507b65; }
.library-card[data-mode="arena"] { --paper: #f0e8fb; --trim: #b8a2d7; --lip: #e6d9f3; --tone: #68479a; --sea: #302957; --sky: #585186; }
.library-card[data-mode="race"] { --paper: #fbecd7; --trim: #d7af87; --lip: #f2dfc8; --tone: #97592d; --sea: #59445c; --sky: #977158; }
.library-card[data-mode="rescue"] { --paper: #def2eb; --trim: #8cbcb6; --lip: #cde7df; --tone: #306561; --sea: #25546a; --sky: #47878a; }
.library-card[data-mode="workshop"] { --paper: #e7edf9; --trim: #a0afcf; --lip: #dce5f5; --tone: #465e94; --sea: #243654; --sky: #445d82; }
.library-card[data-mode="festival"] { --paper: #fae9ec; --trim: #d2a1b2; --lip: #f0dce4; --tone: #824a72; --sea: #4b335d; --sky: #946282; }
.library-card[data-mode="glide"] { --paper: #e2f4f4; --trim: #82b6bf; --lip: #cadfe4; --tone: #2b626d; --sea: #264d68; --sky: #5790a5; }
.library-card[data-mode="rhythm"] { --paper: #fbeddc; --trim: #c7999e; --lip: #ead9d0; --tone: #814b67; --sea: #38304f; --sky: #956574; }
.library-card[data-mode="tide"] { --paper: #e3f2e8; --trim: #8cb6a8; --lip: #cde5d7; --tone: #37655a; --sea: #285364; --sky: #548e85; }

.library-card:focus-visible {
  z-index: 1;
  outline-offset: 2px;
}

@media (hover: hover) {
  .library-card:hover { transform: translateY(-2px); box-shadow: 0 4px 0 var(--trim); }
}

.library-card:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--trim); }

.library-copy {
  display: flex;
  flex-direction: column;
  align-self: center;
  min-width: 0;
  padding: 9px 16px 9px 9px;
}

.library-tag { color: var(--tone); font-size: 11px; line-height: 1.25; font-weight: 850; }
.library-copy strong { margin-block: 3px 4px; font-size: clamp(15px, 2.15vw, 22px); line-height: 1.14; font-weight: 950; overflow-wrap: anywhere; }
.library-details { display: grid; gap: 4px; color: #554960; }
.library-description { font-size: 11px; line-height: 1.45; font-weight: 650; }
.library-status { color: var(--tone); font-size: 10px; line-height: 1.3; font-weight: 850; }
.library-arrow { position: absolute; right: 5px; top: calc(50% - 8px); color: var(--tone); }
.library-arrow .glyph { width: 15px; height: 15px; }

.library-art {
  position: relative;
  isolation: isolate;
  display: block;
  min-width: 0;
  min-height: 44px;
  overflow: hidden;
  border-right: 1px solid var(--trim);
  background: radial-gradient(ellipse at 49% 30%, var(--sky), var(--sea) 86%);
}

.library-art::before {
  content: "";
  position: absolute;
  inset: 0;
  background-image: linear-gradient(90deg, #fffbdf0d 1px, transparent 1px), linear-gradient(#fffbdf0d 1px, transparent 1px);
  background-size: 12px 12px;
  background-position: center;
  mask-image: linear-gradient(transparent, #000 40%);
}

.library-scene {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 64px;
  height: 58px;
  transform: translate(-50%, -50%);
  filter: drop-shadow(0 5px 3px #191a3150);
}

.library-scene > i { position: absolute; display: block; }

.library-card[data-mode="tide"] .library-scene i:nth-child(1) { inset: 25px 4px 7px; border: 1px solid #b4f3e6; background: repeating-linear-gradient(0deg,#8bd4d680 0 1px,#489da8c4 1px 6px); transform: skewY(-12deg); }
.library-card[data-mode="tide"] .library-scene i:nth-child(2) { left: 10px; top: 31px; width: 15px; height: 7px; background: #eac18c; box-shadow: 0 4px #9c775b, 13px -7px #eaca9a, 13px -3px #a58260; transform: skewY(-12deg); }
.library-card[data-mode="tide"] .library-scene i:nth-child(3) { left: 41px; top: 20px; width: 7px; height: 23px; background: #b8e0a5; box-shadow: 5px -4px #87c0a2; }
.library-card[data-mode="tide"] .library-scene i:nth-child(4) { left: 30px; top: 10px; width: 29px; height: 17px; background: linear-gradient(#f7e8ae 32%,#f4aeb7 33% 72%,#c989ad 73%); clip-path: polygon(0 35%,25% 47%,34% 13%,50% 0,68% 14%,75% 47%,100% 35%,84% 91%,50% 100%,16% 91%); }

/* Three stage drums and their struck surfaces. */
.library-card[data-mode="rhythm"] .library-scene i:nth-child(-n+3) { bottom: 12px; width: 15px; height: 15px; border-block: 2px solid #70465c; background: #c47d79; transform: skewY(-10deg); }
.library-card[data-mode="rhythm"] .library-scene i:nth-child(1) { left: 5px; box-shadow: 0 -5px #fac580, 0 3px #6b415b; }
.library-card[data-mode="rhythm"] .library-scene i:nth-child(2) { left: 24px; height: 24px; background: #5e9c96; box-shadow: 0 -5px #8de5d4, 0 3px #426579; }
.library-card[data-mode="rhythm"] .library-scene i:nth-child(3) { right: 6px; height: 18px; background: #9a69a0; box-shadow: 0 -5px #e2a6e2, 0 3px #6b415b; }
.library-card[data-mode="rhythm"] .library-scene i:nth-child(4) { left: 30px; top: 6px; width: 4px; height: 5px; background: #fff4c6; box-shadow: -16px 12px #fac580, 20px 7px #e2a6e2, -23px 3px #edc0cd85; }

/* Folded sail and rising air over the terraces. */
.library-card[data-mode="glide"] .library-scene i:nth-child(1) { inset: 12px 5px 21px; background: linear-gradient(156deg, #fff4c7 54%, #f3ae78 55%); clip-path: polygon(52% 0, 100% 50%, 69% 49%, 50% 100%, 31% 57%, 0 70%); transform: rotate(-9deg); }
.library-card[data-mode="glide"] .library-scene i:nth-child(2) { left: 27px; top: 31px; width: 9px; height: 8px; background: #fdf1d5; box-shadow: inset -2px -2px #ba886e; }
.library-card[data-mode="glide"] .library-scene i:nth-child(3) { left: 6px; top: 42px; width: 17px; height: 6px; transform: skewY(-12deg); background: #a3d9ca; box-shadow: 0 3px #528d91, 36px -8px #c0ddd2, 36px -5px #61919d; }
.library-card[data-mode="glide"] .library-scene i:nth-child(4) { left: 43px; top: 30px; width: 12px; height: 2px; transform: rotate(-38deg); background: #bdebf1; box-shadow: 7px 3px #bdebf195, -35px -8px #bdebf195; }

/* Stadium lines and color-coded goals. */
.library-card[data-mode="ball"] .library-scene i:nth-child(1) {
  inset: 12px 5px 13px;
  border: 1px solid #e6f3b5;
  background: repeating-linear-gradient(90deg, #759e62 0 9px, #6c9558 9px 18px);
  box-shadow: 0 3px 0 #3c7054, 0 5px 0 #26564d;
  transform: rotate(-13deg) skewX(-10deg);
}
.library-card[data-mode="ball"] .library-scene i:nth-child(1)::before,
.library-card[data-mode="ball"] .library-scene i:nth-child(1)::after { content: ""; position: absolute; }
.library-card[data-mode="ball"] .library-scene i:nth-child(1)::before { inset: 0 auto 0 50%; border-left: 1px solid #f5f7c8; }
.library-card[data-mode="ball"] .library-scene i:nth-child(1)::after { inset: 10px calc(50% - 6px); border: 1px solid #f5f7c8; border-radius: 50%; }
.library-card[data-mode="ball"] .library-scene i:nth-child(2),
.library-card[data-mode="ball"] .library-scene i:nth-child(3) { width: 10px; height: 10px; border: 2px solid #fdd483; background: #ecf3db5e; transform: skewY(-14deg); }
.library-card[data-mode="ball"] .library-scene i:nth-child(2) { left: 4px; top: 26px; }
.library-card[data-mode="ball"] .library-scene i:nth-child(3) { right: 5px; top: 15px; border-color: #a9eafa; }
.library-card[data-mode="ball"] .library-scene i:nth-child(4) { left: 26px; top: 21px; width: 8px; height: 8px; background: #fff2bb; border: 1px solid #675c58; border-radius: 3px; box-shadow: inset -1px -1px #dca17b; }

/* Floating tiles with a gap and a star. */
.library-card[data-mode="arena"] .library-scene i:nth-child(1) { left: 9px; top: 19px; width: 19px; height: 16px; transform: skewY(-16deg); background: #b8a9f3; box-shadow: 21px 0 #7f72b8, 21px 18px #a89dda, 0 4px #5e4e96, 21px 4px #584b82, 21px 22px #63558b; }
.library-card[data-mode="arena"] .library-scene i:nth-child(2) { left: 9px; top: 43px; width: 19px; height: 11px; transform: skewY(-16deg); background: #7c6db9; box-shadow: 0 4px #584982; }
.library-card[data-mode="arena"] .library-scene i:nth-child(3) { left: 23px; top: 5px; width: 18px; height: 19px; background: #ffe792; clip-path: polygon(50% 0, 66% 34%, 100% 50%, 66% 66%, 50% 100%, 34% 66%, 0 50%, 34% 34%); }
.library-card[data-mode="arena"] .library-scene i:nth-child(4) { left: 50px; top: 6px; width: 3px; height: 3px; background: #efe0ff; box-shadow: -39px 2px #cdbffc, 5px 7px #a3c7f6, -3px 18px #ead9fc; }

/* The race climbs three floating steps to a pennant. */
.library-card[data-mode="race"] .library-scene i:nth-child(1) { left: 5px; top: 37px; width: 19px; height: 9px; transform: skewY(-17deg); background: #eda76c; box-shadow: 16px -8px #efa66c, 33px -17px #f5bd7f, 0 4px #a96b55, 16px -4px #a87561, 33px -13px #b17a65; }
.library-card[data-mode="race"] .library-scene i:nth-child(2) { right: 14px; top: 8px; width: 2px; height: 19px; background: #ffe8ae; }
.library-card[data-mode="race"] .library-scene i:nth-child(3) { right: 3px; top: 8px; width: 13px; height: 8px; clip-path: polygon(0 0, 100% 0, 78% 50%, 100% 100%, 0 100%); background: #ffd498; }
.library-card[data-mode="race"] .library-scene i:nth-child(4) { left: 13px; top: 23px; width: 12px; height: 9px; background: #ffe4a7; clip-path: polygon(0 20%, 61% 20%, 61% 0, 100% 50%, 61% 100%, 61% 80%, 0 80%); transform: rotate(-22deg); }

/* Two island banks and their timber link. */
.library-card[data-mode="rescue"] .library-scene i:nth-child(1) { left: 4px; top: 26px; width: 20px; height: 12px; transform: skewY(-13deg); background: #94cc98; box-shadow: 0 4px #5a957d, 0 8px #386f69, 36px -11px #b0d698, 36px -7px #689a78, 36px -3px #386f69; }
.library-card[data-mode="rescue"] .library-scene i:nth-child(2) { left: 21px; top: 23px; width: 21px; height: 9px; transform: skewY(-13deg); background: repeating-linear-gradient(90deg, #f6c781 0 3px, #c0925d 3px 4px); box-shadow: 0 3px #876349; }
.library-card[data-mode="rescue"] .library-scene i:nth-child(3) { right: 9px; top: 11px; width: 8px; height: 9px; background: #f3d198; border-top: 3px solid #d58373; transform: skewY(-13deg); }
.library-card[data-mode="rescue"] .library-scene i:nth-child(4) { left: 7px; top: 47px; width: 11px; height: 2px; background: #baf3ef9e; box-shadow: 15px 3px #baf3ef6e, 34px -4px #baf3ef6e, 24px -5px #baf3ef6e; }

/* Prism and split beams. */
.library-card[data-mode="workshop"] .library-scene i:nth-child(1) { left: 17px; top: 14px; width: 33px; height: 32px; background: #c5f2ef; clip-path: polygon(50% 0, 100% 91%, 50% 100%, 0 91%); }
.library-card[data-mode="workshop"] .library-scene i:nth-child(2) { left: 21px; top: 19px; width: 25px; height: 24px; background: linear-gradient(90deg, #73c5db 50%, #a0a6db 50%); clip-path: polygon(50% 0, 100% 91%, 50% 100%, 0 91%); }
.library-card[data-mode="workshop"] .library-scene i:nth-child(3) { left: 2px; top: 33px; width: 28px; height: 2px; transform: rotate(-13deg); background: #e4f5b8; box-shadow: 0 0 5px #e4f5b8; }
.library-card[data-mode="workshop"] .library-scene i:nth-child(4) { left: 39px; top: 25px; width: 23px; height: 2px; transform: rotate(-24deg); background: #ffadab; box-shadow: -7px 7px #a6e9e0, -11px 12px #bba9fb; }

/* Three lanterns at different depths. */
.library-card[data-mode="festival"] .library-scene i:nth-child(1),
.library-card[data-mode="festival"] .library-scene i:nth-child(2),
.library-card[data-mode="festival"] .library-scene i:nth-child(3) { width: 13px; height: 16px; border-block: 2px solid #c28778; border-inline: 2px solid #d89c7b; background: linear-gradient(90deg, #ffd080 49%, #fff0b5 50%); box-shadow: 0 0 10px #fabc805e; }
.library-card[data-mode="festival"] .library-scene i:nth-child(1) { left: 6px; top: 19px; transform: scale(.85); }
.library-card[data-mode="festival"] .library-scene i:nth-child(2) { left: 25px; top: 11px; transform: scale(1.15); }
.library-card[data-mode="festival"] .library-scene i:nth-child(3) { right: 6px; top: 22px; transform: scale(.9); }
.library-card[data-mode="festival"] .library-scene i:nth-child(-n+3)::before { content: ""; position: absolute; width: 1px; height: 8px; background: #eaceac; left: calc(50% - .5px); top: -10px; }
.library-card[data-mode="festival"] .library-scene i:nth-child(-n+3)::after { content: ""; position: absolute; width: 2px; height: 4px; background: #ffcd91; left: calc(50% - 1px); bottom: -6px; }
.library-card[data-mode="festival"] .library-scene i:nth-child(4) { left: 12px; right: 12px; top: 48px; height: 2px; background: #deb9d582; box-shadow: 6px 4px #d59cbe52, -4px -4px #f1c1ad52; }

@media (orientation: landscape) and (max-height: 420px) {
  #dialog[data-kind="mode-library"] .dialog-content { padding: 12px 14px 15px; }
  #dialog[data-kind="mode-library"] h2 { font-size: 27px; line-height: 1.12; }
  .library-challenges { gap: 8px; margin-bottom: 10px; padding-bottom: 9px; }
  .library-challenge { min-height: 48px; padding-block: 4px; }
  .library-challenge-icon { width: 31px; }
  .library-challenge-icon .glyph { width: 19px; height: 19px; }
  .library-challenge-copy strong { font-size: 15px; }
  .library-grid { gap: 8px; }
  .library-card { height: 78px; min-height: 78px; grid-template-columns: clamp(48px, 7vw, 61px) minmax(0, 1fr); }
  .library-copy { padding: 3px 16px 3px 7px; }
  .library-tag { font-size: 10px; line-height: 1.1; }
  .library-copy strong { margin-block: 2px; font-size: 16px; }
  .library-details { gap: 2px; }
  .library-description { display: -webkit-box; overflow: hidden; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-height: 1.24; }
  .library-scene { transform: translate(-50%, -50%) scale(.9); }
}

@media (orientation: landscape) and (min-width: 720px) and (max-height: 420px) {
  .library-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .library-card { grid-template-columns: 52px minmax(0, 1fr); }
  .library-scene { transform: translate(-50%, -50%) scale(.77); }
}

@media (orientation: landscape) and (max-height: 300px) {
  #dialog[data-kind="mode-library"] h2 { font-size: 24px; }
  .library-grid { gap: 6px; }
  .library-card { height: 70px; min-height: 70px; }
}

@media (orientation: portrait) and (max-width: 590px) {
  .library-challenges { gap: 6px; margin-bottom: 10px; padding-bottom: 9px; }
  .library-challenge { min-height: 76px; grid-template-columns: minmax(0, 1fr); align-content: start; gap: 4px; padding: 5px 6px 6px; }
  .library-challenge-icon { width: 26px; border-radius: 6px; }
  .library-challenge-icon .glyph { width: 17px; height: 17px; }
  .library-challenge-copy { gap: 2px; }
  .library-challenge-copy strong { font-size: 14px; }
  .library-challenge-copy span { font-size: 10px; }
  .library-challenge-arrow { display: none; }
  .library-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .library-card { min-height: 129px; grid-template-columns: minmax(0, 1fr); grid-template-rows: 45px 1fr; }
  .library-art { border-right: 0; border-bottom: 1px solid var(--trim); }
  .library-scene { transform: translate(-50%, -50%) scale(.74); }
  .library-copy { align-self: start; padding: 6px 8px 8px; }
  .library-copy strong { font-size: 16px; }
  .library-description { font-size: 10px; }
  .library-arrow { top: 4px; right: 4px; color: #fff2d6; }
}

@media (prefers-reduced-motion: reduce) { .library-card, .library-challenge { transition: none; } }
