/* ============================================================
   Library Loot — medieval pixel theme
   (twilight sky, castle battlements, torchlight & parchment)
   ============================================================ */

:root {
  --dusk-top: #241a3d;
  --dusk-mid: #4a3370;
  --dusk-low: #7a4f8f;
  --stone: #6e6e7e;
  --stone-dark: #4c4c5a;
  --stone-light: #8d8d9d;
  --parchment: #f2e3ba;
  --parchment-dark: #e4d19c;
  --wood: #7a4526;
  --wood-dark: #4a2a12;
  --wood-light: #a5683a;
  --gold-trim: #c9a13b;
  --ink: #3d2513;
  --ink-soft: #6b4a2c;
  --gold: #f4c542;
  --gold-dark: #b8860b;
  --green: #4e9e3e;
  --green-dark: #2c5e23;
  --banner-red: #a03030;
  --banner-red-dark: #6e1f1f;
  --red: #c94f42;
  --font-pixel: 'Press Start 2P', monospace;
  --font-body: 'VT323', monospace;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html { scroll-behavior: smooth; overflow-x: hidden; max-width: 100%; }

body {
  font-family: var(--font-body);
  font-size: 22px;
  line-height: 1.35;
  color: var(--ink);
  background: linear-gradient(to bottom, var(--dusk-top) 0%, var(--dusk-mid) 55%, var(--dusk-low) 100%);
  background-attachment: fixed;
  min-height: 100vh;
  padding-bottom: 100px;
  position: relative;
  overflow-x: hidden;
}

/* castle battlements along the bottom of the viewport */
body::after {
  content: "";
  position: fixed;
  left: 0; right: 0; bottom: 0;
  height: 56px;
  background:
    /* crenellations: merlons every 40px */
    repeating-linear-gradient(to right,
      var(--stone) 0 24px, transparent 24px 40px) top / 100% 14px no-repeat,
    /* mortar joints in the wall */
    repeating-linear-gradient(to right,
      transparent 0 36px, var(--stone-dark) 36px 40px) bottom / 100% 42px no-repeat,
    repeating-linear-gradient(to bottom,
      var(--stone) 0 10px, var(--stone-dark) 10px 12px) bottom / 100% 42px no-repeat;
  z-index: 5;
  pointer-events: none;
  image-rendering: pixelated;
}

/* ---------- day theme overrides (night is the default palette) ---------- */
body.theme-day {
  --dusk-top: #4f9fd8;
  --dusk-mid: #79bce8;
  --dusk-low: #a8d8f0;
  --stone: #9a9aa8;
  --stone-dark: #6e6e7e;
  --stone-light: #b8b8c4;
}
body.theme-day .stars { display: none; }
body.theme-night .cloud { display: none; }
body.theme-day .moon { display: none; }
body.theme-night .sun { display: none; }

/* ---------- sky backdrop (stars by night, clouds by day) ---------- */
.backdrop { position: fixed; inset: 0; pointer-events: none; z-index: 0; }

.celestial {
  position: absolute;
  top: 18px;
  left: 18px;
  width: 64px;
  height: 64px;
  animation: idle-bob 3s steps(2) infinite;
}

.cloud {
  position: absolute;
  width: 96px; height: 24px;
  background: rgba(255,255,255,.92);
  box-shadow:
    24px -12px 0 0 rgba(255,255,255,.92),
    48px -6px 0 0 rgba(255,255,255,.92),
    -12px 6px 0 0 rgba(255,255,255,.75);
  animation: drift linear infinite;
}
.cloud-1 { top: 9%;  animation-duration: 70s; }
.cloud-2 { top: 24%; animation-duration: 95s;  animation-delay: -40s; transform: scale(.7); }
.cloud-3 { top: 46%; animation-duration: 120s; animation-delay: -70s; transform: scale(1.2); opacity: .6; }
@keyframes drift {
  from { left: -160px; }
  to   { left: 110%; }
}
.stars {
  position: absolute;
  top: 0; left: 0;
  width: 4px; height: 4px;
  background: transparent;
  box-shadow:
    8vw 6vh 0 #fff8dc, 22vw 12vh 0 #fff8dc, 35vw 4vh 0 #d9d0ff,
    48vw 15vh 0 #fff8dc, 61vw 7vh 0 #fff8dc, 74vw 13vh 0 #d9d0ff,
    88vw 5vh 0 #fff8dc, 15vw 22vh 0 #d9d0ff, 42vw 26vh 0 #fff8dc,
    68vw 24vh 0 #fff8dc, 93vw 20vh 0 #d9d0ff, 5vw 35vh 0 #fff8dc,
    30vw 38vh 0 #fff8dc, 55vw 33vh 0 #d9d0ff, 80vw 37vh 0 #fff8dc;
  animation: twinkle 3s steps(2) infinite;
}
.stars-2 {
  box-shadow:
    12vw 9vh 0 #d9d0ff, 27vw 18vh 0 #fff8dc, 51vw 10vh 0 #fff8dc,
    64vw 19vh 0 #d9d0ff, 83vw 9vh 0 #fff8dc, 96vw 15vh 0 #fff8dc,
    9vw 28vh 0 #fff8dc, 37vw 31vh 0 #d9d0ff, 72vw 29vh 0 #fff8dc,
    90vw 33vh 0 #d9d0ff, 20vw 42vh 0 #fff8dc, 60vw 44vh 0 #d9d0ff;
  animation-delay: 1.5s;
}
@keyframes twinkle {
  0%, 100% { opacity: 1; }
  50%      { opacity: .35; }
}

/* ---------- corner toggles (sound & theme) ---------- */
.corner-btn {
  position: fixed;
  top: 14px;
  right: 14px;
  z-index: 10;
  font-size: 22px;
  line-height: 1;
  padding: 8px 10px;
  background: var(--parchment);
  border: 3px solid var(--wood-dark);
  box-shadow: inset 0 0 0 2px var(--gold-trim), 0 4px 0 rgba(0,0,0,.35);
  cursor: pointer;
}
.corner-btn:hover { filter: brightness(1.06); }
.corner-btn:active { transform: translateY(3px); box-shadow: inset 0 0 0 2px var(--gold-trim); }
.theme-btn { right: 70px; }

/* ---------- header ---------- */
.site-header {
  position: relative;
  z-index: 1;
  text-align: center;
  padding: 40px 16px 10px;
}
.logo-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 16px;
  max-width: 100%;
}
@media (max-width: 420px) {
  .logo-row { gap: 8px; }
  .mascot-small { width: 34px; height: 38px; }
  .logo-coin { width: 22px; height: 22px; }
  .site-header h1 { letter-spacing: 1px; }
}
.site-header h1 {
  font-family: var(--font-pixel);
  font-size: clamp(20px, 5vw, 38px);
  color: #f7ecd0;
  text-shadow: 3px 3px 0 var(--gold-dark), 6px 6px 0 rgba(0,0,0,.35);
  letter-spacing: 2px;
}
.mascot-small { width: 48px; height: 54px; animation: idle-bob 1.6s steps(2) infinite; }
.logo-coin { width: 30px; height: 30px; animation: coin-spin 1.8s steps(4) infinite; }
@keyframes idle-bob {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-4px); }
}
@keyframes coin-spin {
  0%, 100% { transform: scaleX(1); }
  25%      { transform: scaleX(.35); }
  50%      { transform: scaleX(1); }
  75%      { transform: scaleX(.35); }
}
.tagline {
  margin-top: 14px;
  font-size: 24px;
  color: #f7ecd0;
  text-shadow: 2px 2px 0 rgba(0,0,0,.4);
}

/* ---------- layout ---------- */
main {
  position: relative;
  z-index: 1;
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
  padding: 20px 16px 40px;
  box-sizing: border-box;
}

.screen { display: none; }
.screen.active { display: block; animation: screen-in .25s steps(5); }
@keyframes screen-in {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ---------- panels (parchment, dark oak & gold trim) ---------- */
.panel {
  background: var(--parchment);
  border: 4px solid var(--wood-dark);
  box-shadow:
    inset 0 0 0 4px var(--wood),
    inset 0 0 0 6px var(--gold-trim),
    0 6px 0 rgba(0,0,0,.35);
  border-radius: 2px;
  padding: 22px 24px;
  margin-bottom: 22px;
}
.panel-title {
  font-family: var(--font-pixel);
  font-size: 14px;
  color: var(--wood-dark);
  margin-bottom: 14px;
  line-height: 1.6;
}
.intro-panel p { font-size: 24px; }

.hint { font-size: 19px; color: var(--ink-soft); margin-top: 8px; }

/* ---------- drop zone ---------- */
.drop-zone {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
  padding: 26px 16px;
  background: var(--parchment-dark);
  border: 3px dashed var(--wood);
  cursor: pointer;
  text-align: center;
  transition: background .15s;
}
.drop-zone:hover, .drop-zone.dragover {
  background: #f5e0a4;
  border-color: var(--green-dark);
}
.drop-icon { font-size: 34px; }
.drop-text { font-size: 21px; color: var(--ink-soft); }
.import-status { min-height: 1.2em; }
.import-status.ok    { color: var(--green-dark); }
.import-status.error { color: var(--red); }

.how-to { margin-top: 12px; font-size: 19px; color: var(--ink-soft); }
.how-to summary { cursor: pointer; }
.how-to ul { margin: 8px 0 0 24px; }

/* ---------- forms & buttons ---------- */
input[type="text"], select {
  font-family: var(--font-body);
  font-size: 21px;
  color: var(--ink);
  background: #faf0d2;
  border: 3px solid var(--wood);
  padding: 7px 10px;
  border-radius: 0;
  outline: none;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  box-sizing: border-box;
}
input[type="text"]:focus, select:focus { border-color: var(--gold-dark); }

.manual-form {
  display: grid;
  grid-template-columns: 1fr 1fr auto auto;
  gap: 10px;
  max-width: 100%;
}
.manual-form > * { min-width: 0; }
@media (max-width: 620px) {
  .manual-form { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 400px) {
  .manual-form { grid-template-columns: 1fr; }
  .manual-form .btn { width: 100%; }
}

.btn {
  font-family: var(--font-pixel);
  color: #f7ecd0;
  background: var(--green);
  border: 3px solid var(--green-dark);
  box-shadow: 0 4px 0 var(--green-dark);
  padding: 12px 18px;
  font-size: 12px;
  cursor: pointer;
  text-shadow: 1px 1px 0 rgba(0,0,0,.3);
  transition: transform .05s;
}
.btn:hover:not(:disabled) { filter: brightness(1.08); }
.btn:active:not(:disabled) { transform: translateY(4px); box-shadow: 0 0 0 var(--green-dark); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-small { padding: 9px 12px; font-size: 10px; }
.btn-big {
  padding: 18px 26px;
  font-size: 15px;
  background: var(--banner-red);
  border-color: var(--banner-red-dark);
  box-shadow: 0 4px 0 var(--banner-red-dark);
}
.btn-big:active:not(:disabled) { box-shadow: 0 0 0 var(--banner-red-dark); }
.btn-muted {
  background: var(--wood-light);
  border-color: var(--wood-dark);
  box-shadow: 0 4px 0 var(--wood-dark);
}
.btn-muted:active:not(:disabled) { box-shadow: 0 0 0 var(--wood-dark); }

.calculate-row { text-align: center; margin-top: 6px; }
.calculate-row .hint { color: #f7ecd0; text-shadow: 1px 1px 0 rgba(0,0,0,.4); }

/* ---------- book list ---------- */
.count-badge { color: var(--green-dark); }
.book-list { list-style: none; max-height: 320px; overflow-y: auto; }
.book-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 4px;
  border-bottom: 2px dotted var(--wood-light);
}
.book-list .b-title  { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.book-list .b-author { color: var(--ink-soft); }
.book-list .b-format {
  font-size: 16px;
  background: var(--parchment-dark);
  border: 2px solid var(--wood-light);
  padding: 1px 6px;
  color: var(--ink-soft);
  white-space: nowrap;
}
.remove-btn {
  font-family: var(--font-body);
  font-size: 20px;
  background: none;
  border: none;
  color: var(--red);
  cursor: pointer;
  padding: 0 6px;
}
.remove-btn:hover { transform: scale(1.2); }
.list-actions { margin-top: 12px; text-align: right; }

/* ---------- calculating screen ---------- */
.calc-panel { text-align: center; padding: 34px 24px 30px; }
.calc-heading {
  font-family: var(--font-pixel);
  font-size: 15px;
  color: var(--wood-dark);
  margin-bottom: 8px;
}
.dots span { animation: dot-blink 1.2s steps(1) infinite; }
.dots span:nth-child(2) { animation-delay: .2s; }
.dots span:nth-child(3) { animation-delay: .4s; }
@keyframes dot-blink { 0%, 40% { opacity: 0; } 50%, 100% { opacity: 1; } }

.calc-scene {
  position: relative;
  height: 190px;
  margin: 10px auto 22px;
  max-width: 340px;
  overflow: hidden;
}
.calc-ground {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: 18px;
  background: repeating-linear-gradient(to right, var(--stone) 0 10px, var(--stone-light) 10px 20px);
  border-top: 4px solid var(--stone-dark);
}
.calc-mascot {
  position: absolute;
  width: 96px; height: 108px;
  left: 50%; bottom: 16px;
  transform-origin: 50% 100%;
  animation: mascot-hop .7s infinite;
  margin-left: -48px;
}
@keyframes mascot-hop {
  0%   { transform: translateY(0) scale(1, 1); }
  15%  { transform: translateY(0) scale(1.12, .82); }
  45%  { transform: translateY(-44px) scale(.94, 1.08); }
  75%  { transform: translateY(0) scale(1, 1); }
  88%  { transform: translateY(0) scale(1.06, .92); }
  100% { transform: translateY(0) scale(1, 1); }
}
.calc-shadow {
  position: absolute;
  left: 50%; bottom: 16px;
  width: 70px; height: 10px;
  margin-left: -35px;
  background: rgba(0,0,0,.22);
  animation: shadow-squish .7s infinite;
}
@keyframes shadow-squish {
  0%, 75%, 100% { transform: scaleX(1); opacity: .9; }
  45%           { transform: scaleX(.55); opacity: .5; }
}
.calc-coin {
  position: absolute;
  width: 26px; height: 26px;
  bottom: 110px;
  opacity: 0;
}
.coin-a { left: 22%; animation: coin-pop 2.1s infinite .1s; }
.coin-b { left: 47%; animation: coin-pop 2.1s infinite .8s; }
.coin-c { left: 70%; animation: coin-pop 2.1s infinite 1.5s; }
@keyframes coin-pop {
  0%   { opacity: 0; transform: translateY(30px) scaleX(1); }
  10%  { opacity: 1; }
  30%  { transform: translateY(-34px) scaleX(.3); }
  50%  { transform: translateY(-46px) scaleX(1); opacity: 1; }
  70%  { transform: translateY(-34px) scaleX(.3); }
  90%  { opacity: 0; transform: translateY(20px) scaleX(1); }
  100% { opacity: 0; }
}

.progress-track {
  height: 26px;
  background: var(--parchment-dark);
  border: 3px solid var(--wood-dark);
  box-shadow: inset 0 3px 0 rgba(0,0,0,.12);
  max-width: 420px;
  margin: 0 auto;
}
.progress-fill {
  height: 100%;
  width: 0%;
  background: repeating-linear-gradient(to right, var(--gold) 0 10px, #e0b433 10px 20px);
  border-right: 3px solid var(--gold-dark);
  transition: width .3s steps(4);
}
.progress-label {
  margin-top: 10px;
  font-size: 20px;
  color: var(--ink-soft);
  min-height: 1.3em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---------- results ---------- */
.results-banner { text-align: center; padding: 30px 24px; }
.results-kicker {
  font-family: var(--font-pixel);
  font-size: 13px;
  color: var(--wood-dark);
}
.results-total {
  font-family: var(--font-pixel);
  font-size: clamp(34px, 9vw, 62px);
  color: var(--green-dark);
  text-shadow: 3px 3px 0 var(--gold);
  margin: 18px 0 10px;
}
.results-sub { font-size: 22px; color: var(--ink-soft); }
.results-actions {
  margin-top: 18px;
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
}

.equivalents { list-style: none; font-size: 23px; }
.equivalents li { padding: 5px 0; }

/* ---------- breakdown table ---------- */
.breakdown { width: 100%; border-collapse: collapse; font-size: 20px; }
.breakdown th {
  font-family: var(--font-pixel);
  font-size: 10px;
  text-align: left;
  color: var(--wood-dark);
  border-bottom: 3px solid var(--wood);
  padding: 6px 4px;
}
.breakdown td { padding: 6px 4px; border-bottom: 2px dotted var(--wood-light); vertical-align: top; }
.breakdown .num { text-align: right; white-space: nowrap; }
.breakdown tfoot td {
  font-family: var(--font-pixel);
  font-size: 12px;
  color: var(--green-dark);
  border-bottom: none;
  border-top: 3px solid var(--wood);
  padding-top: 10px;
}
.breakdown .b-author { display: block; font-size: 17px; color: var(--ink-soft); }
.badge {
  display: inline-block;
  font-size: 15px;
  padding: 0 5px;
  border: 2px solid;
  margin-left: 4px;
  vertical-align: 2px;
}
.badge-est  { color: var(--ink-soft); border-color: var(--wood-light); background: var(--parchment-dark); }

/* ---------- library finder ---------- */
.library-search {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-top: 12px;
}
.or { color: var(--ink-soft); }
.zip-form { display: flex; gap: 8px; flex-wrap: wrap; max-width: 100%; }
#zip-input { width: 180px; max-width: 100%; }
#library-status { min-height: 1.2em; }

.library-results { list-style: none; margin-top: 8px; }
.library-results li {
  padding: 10px 4px;
  border-bottom: 2px dotted var(--wood-light);
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  flex-wrap: wrap;
}
.library-results .lib-name { font-size: 22px; font-weight: bold; }
.library-results .lib-dist { color: var(--ink-soft); white-space: nowrap; }
.library-results a { color: var(--green-dark); }
.card-cta { margin-top: 16px; font-size: 22px; }
.card-cta a { color: var(--green-dark); }

/* ---------- footer & toast ---------- */
.site-footer {
  position: relative;
  z-index: 6;
  text-align: center;
  font-size: 17px;
  color: #f7ecd0;
  text-shadow: 1px 1px 0 rgba(0,0,0,.45);
  padding: 0 16px 70px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 80px;
  transform: translate(-50%, 20px);
  background: var(--wood-dark);
  color: #f7ecd0;
  font-size: 20px;
  padding: 10px 18px;
  border: 3px solid var(--gold);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 20;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01s !important; animation-iteration-count: 1 !important; }
}
