@import url('https://fonts.googleapis.com/css2?family=Fraunces:ital,opsz,wght@0,9..144,500;0,9..144,650;1,9..144,500;1,9..144,650&display=swap');

/* ---------- Midnight reading room tokens ---------- */
:root {
  --ink: #141018;          /* warm plum-black room */
  --shelf: #1e1823;        /* panel */
  --shelf2: #292031;       /* raised panel */
  --line: #322a3a;         /* hairlines */
  --paper: #efe7da;        /* warm text */
  --paper-dim: #a89db0;    /* muted text */
  --lamp: #e9a23b;         /* amber reading lamp — primary accent */
  --manga: #e4574c;        /* spine: manga */
  --comics: #5fa8e8;       /* spine: comics */
  --books: #58b98b;        /* spine: books */
  --ok: #58b98b;
  --err: #e05656;
  --serif: "Fraunces", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; margin: 0; -webkit-tap-highlight-color: transparent; }

body {
  color: var(--paper);
  font-family: var(--sans);
  background:
    radial-gradient(1100px 520px at 50% -160px, rgba(233, 162, 59, .10), transparent 62%),
    var(--ink);
  min-height: 100vh;
}

/* ---------- Header: the lamp ---------- */
header {
  position: sticky; top: 0; z-index: 5;
  padding: 14px 16px 10px;
  padding-top: max(14px, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--ink) 84%, transparent);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--line);
  animation: lamp-on .7s ease-out both;
}
#topRow { display: flex; align-items: baseline; justify-content: space-between;
  margin-bottom: 12px; }
h1 {
  font-family: var(--serif); font-style: italic; font-weight: 650;
  font-size: 1.45rem; letter-spacing: .01em;
  display: flex; align-items: center; gap: 10px;
}
.spines { display: inline-flex; gap: 3px; align-items: flex-end; }
.spines i { width: 5px; border-radius: 2px; display: block; }
.spines i:nth-child(1) { height: 20px; background: var(--manga); }
.spines i:nth-child(2) { height: 16px; background: var(--comics); }
.spines i:nth-child(3) { height: 23px; background: var(--books); }

#logoutBtn { min-height: 36px; padding: 0 14px; border: 1px solid var(--line);
  border-radius: 999px; background: transparent; color: var(--paper-dim);
  font-size: .82rem; cursor: pointer; transition: color .2s, border-color .2s; }
#logoutBtn:hover { color: var(--err); border-color: var(--err); }

#searchForm { display: flex; gap: 8px; }
input[type="search"]::-webkit-search-cancel-button { -webkit-appearance: none; }
#q {
  appearance: none; -webkit-appearance: none;
  flex: 1; min-height: 48px; padding: 0 18px; font-size: 1rem;
  border-radius: 14px; border: 1px solid var(--line);
  background: var(--shelf); color: var(--paper);
  transition: border-color .25s, box-shadow .25s;
}
#q::placeholder { color: var(--paper-dim); }
#q:focus { outline: none; border-color: var(--lamp);
  box-shadow: 0 0 0 3px rgba(233, 162, 59, .18), 0 0 34px rgba(233, 162, 59, .12); }
#go {
  min-height: 48px; min-width: 48px; padding: 0 22px; border: 0; border-radius: 14px;
  background: var(--lamp); color: #201505; font-weight: 700; font-size: .98rem;
  cursor: pointer; transition: transform .15s, filter .2s;
}
#go:hover { filter: brightness(1.08); }
#go:active { transform: scale(.97); }

#pills { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.pill {
  min-height: 38px; padding: 0 18px; border-radius: 999px; font-size: .92rem;
  border: 1px solid var(--line); background: transparent; color: var(--paper-dim);
  cursor: pointer; transition: color .2s, background .2s, border-color .2s;
}
.pill:hover { color: var(--paper); }
.pill.active { color: #201505; font-weight: 700; }
.pill.active[data-type="all"]    { background: var(--lamp);   border-color: var(--lamp); }
.pill.active[data-type="manga"]  { background: var(--manga);  border-color: var(--manga); color: #2a0c09; }
.pill.active[data-type="comics"] { background: var(--comics); border-color: var(--comics); color: #0a1c2c; }
.pill.active[data-type="ebooks"] { background: var(--books);  border-color: var(--books); color: #0a231a; }

/* ---------- Results: the shelves ---------- */
main { padding: 18px 16px calc(28px + env(safe-area-inset-bottom));
  max-width: 1400px; margin: 0 auto; }
.hint, .empty { color: var(--paper-dim); padding: 26px 4px; font-size: .95rem; }

.section-head { display: flex; align-items: center; gap: 10px; margin: 26px 2px 14px; }
.section-head .spine { width: 5px; height: 22px; border-radius: 2px;
  background: var(--lamp); flex: none; }
.section-head[data-type="manga"] .spine { background: var(--manga); }
.section-head[data-type="comics"] .spine { background: var(--comics); }
.section-head[data-type="ebooks"] .spine { background: var(--books); }
.section-title { font-family: var(--serif); font-style: italic; font-weight: 650;
  font-size: 1.18rem; }
.section-head .count { color: var(--paper-dim); font-size: .8rem;
  font-variant-numeric: tabular-nums; margin-left: 2px; }
.section-head::after { content: ""; flex: 1; height: 1px; background: var(--line);
  margin-left: 6px; }

.error-banner { background: rgba(224, 86, 86, .1); border: 1px solid var(--err);
  color: #f2b8b8; border-radius: 12px; padding: 12px 16px; margin: 10px 0;
  font-size: .92rem; }

.grid { display: grid; gap: 20px 14px;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }

.card { display: flex; flex-direction: column;
  animation: shelf-in .5s cubic-bezier(.2, .7, .3, 1) both; }
.cover, .cover-fallback {
  aspect-ratio: 2/3; width: 100%; border-radius: 10px; display: block;
  object-fit: cover; background: var(--shelf);
  box-shadow: 0 6px 18px rgba(0, 0, 0, .5);
  transition: transform .25s cubic-bezier(.2, .7, .3, 1), box-shadow .25s;
}
.card:hover .cover, .card:hover .cover-fallback {
  transform: translateY(-4px);
  box-shadow: 0 14px 30px rgba(0, 0, 0, .55), 0 4px 22px rgba(233, 162, 59, .14);
}
.cover-fallback { display: flex; align-items: center; justify-content: center;
  font-family: var(--serif); font-style: italic; font-size: 2.6rem;
  color: var(--paper-dim); }

.meta { padding: 10px 2px 2px; }
.title { font-family: var(--serif); font-weight: 500; font-size: .98rem;
  line-height: 1.3; display: -webkit-box; -webkit-line-clamp: 2;
  -webkit-box-orient: vertical; overflow: hidden; min-height: 2.5em; }
.subtitle { font-size: .76rem; color: var(--paper-dim); white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; margin-top: 3px;
  letter-spacing: .02em; }

.card .actions { display: flex; flex-direction: column; gap: 4px; margin-top: 8px; }
.card button.add {
  min-height: 44px; border: 1px solid var(--line); border-radius: 11px;
  background: var(--shelf); color: var(--lamp); font-weight: 650; font-size: .88rem;
  cursor: pointer; transition: background .2s, border-color .2s, transform .12s;
}
.card[data-type="manga"] button.add  { color: var(--manga); }
.card[data-type="comics"] button.add { color: var(--comics); }
.card[data-type="ebooks"] button.add { color: var(--books); }
.card button.add:hover { background: var(--shelf2); border-color: #4a4056; }
.card button.add:active { transform: scale(.98); }
.card button.add:disabled { color: var(--paper-dim); }
.card button.add.done { background: rgba(88, 185, 139, .12);
  border-color: var(--ok); color: var(--ok) !important; }
.card button.pick { min-height: 38px; border: 0; background: transparent;
  color: var(--paper-dim); font-size: .82rem; cursor: pointer;
  transition: color .2s; }
.card button.pick:hover { color: var(--paper); text-decoration: underline;
  text-underline-offset: 3px; }

/* ---------- Chapter picker ---------- */
#modalBackdrop { position: fixed; inset: 0; z-index: 20;
  background: rgba(10, 7, 12, .68); backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: flex; align-items: center; justify-content: center; padding: 16px; }
#modalBackdrop[hidden] { display: none; }
#modal { background: var(--shelf); border: 1px solid var(--line);
  border-radius: 18px; width: min(560px, 96vw); max-height: 84vh;
  display: flex; flex-direction: column;
  box-shadow: 0 24px 70px rgba(0, 0, 0, .6);
  animation: modal-in .2s cubic-bezier(.2, .7, .3, 1) both; }
#modalHead { display: flex; align-items: center; gap: 12px; padding: 16px 18px 6px; }
#modalHead::before { content: ""; width: 5px; height: 22px; border-radius: 2px;
  background: var(--manga); flex: none; }
#modalTitle { font-family: var(--serif); font-style: italic; font-weight: 650;
  font-size: 1.08rem; flex: 1; overflow: hidden; text-overflow: ellipsis;
  white-space: nowrap; }
#modalClose { min-width: 40px; min-height: 40px; border: 0; border-radius: 11px;
  background: var(--shelf2); color: var(--paper-dim); font-size: 1rem;
  cursor: pointer; transition: color .2s; }
#modalClose:hover { color: var(--paper); }
#modalHint { color: var(--paper-dim); font-size: .76rem; padding: 0 18px 10px 35px; }
#chapterList { overflow-y: auto; -webkit-overflow-scrolling: touch;
  border-top: 1px solid var(--line); border-bottom: 1px solid var(--line);
  user-select: none; -webkit-user-select: none; overscroll-behavior: contain; }
.chrow { min-height: 46px; display: flex; align-items: center; gap: 12px;
  padding: 0 18px; cursor: pointer; font-size: .92rem;
  border-bottom: 1px solid #241d2b; transition: background .15s; }
.chrow:hover { background: var(--shelf2); }
.chrow .tick { width: 20px; height: 20px; border-radius: 6px; flex: none;
  border: 2px solid #453b52; display: flex; align-items: center;
  justify-content: center; font-size: .72rem; color: #201505;
  transition: background .15s, border-color .15s; }
.chrow.sel { background: rgba(233, 162, 59, .1); }
.chrow.sel .tick { background: var(--lamp); border-color: var(--lamp); }
.chrow .chname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#modalFoot { display: flex; align-items: center; gap: 8px; padding: 14px 18px;
  padding-bottom: max(14px, env(safe-area-inset-bottom)); }
#selCount { color: var(--paper-dim); font-size: .86rem;
  font-variant-numeric: tabular-nums; }
#modalFoot .spacer { flex: 1; }
.mbtn { min-height: 42px; padding: 0 16px; border: 1px solid var(--line);
  border-radius: 11px; background: var(--shelf2); color: var(--paper);
  font-size: .88rem; cursor: pointer; transition: filter .2s; }
.mbtn:hover { filter: brightness(1.15); }
.mbtn.primary { background: var(--lamp); border-color: var(--lamp);
  color: #201505; font-weight: 700; }
.mbtn.primary:disabled { opacity: .4; }

/* ---------- Toast ---------- */
#toast { position: fixed; left: 50%; transform: translateX(-50%);
  bottom: max(26px, env(safe-area-inset-bottom)); z-index: 30;
  background: var(--shelf2); border: 1px solid var(--line);
  border-left: 3px solid var(--lamp); color: var(--paper);
  border-radius: 12px; padding: 13px 18px; max-width: 92vw; font-size: .93rem;
  box-shadow: 0 10px 34px rgba(0, 0, 0, .55);
  animation: toast-up .25s cubic-bezier(.2, .7, .3, 1) both; }

/* ---------- Login ---------- */
.loginbody { min-height: 100vh; min-height: 100dvh; display: flex;
  align-items: center; justify-content: center; padding: 20px;
  background:
    radial-gradient(700px 480px at 50% 30%, rgba(233, 162, 59, .12), transparent 65%),
    var(--ink); }
.logincard { background: var(--shelf); border: 1px solid var(--line);
  border-radius: 20px; padding: 32px 28px; width: min(390px, 94vw);
  display: flex; flex-direction: column; gap: 13px;
  box-shadow: 0 24px 70px rgba(0, 0, 0, .55);
  animation: shelf-in .55s cubic-bezier(.2, .7, .3, 1) both; }
.logincard h1 { justify-content: center; margin-bottom: 6px; }
.logincard input { appearance: none; -webkit-appearance: none;
  min-height: 48px; padding: 0 16px; border-radius: 13px;
  border: 1px solid var(--line); background: var(--shelf2); color: var(--paper);
  font-size: 1rem; transition: border-color .25s, box-shadow .25s; }
.logincard input:focus { outline: none; border-color: var(--lamp);
  box-shadow: 0 0 0 3px rgba(233, 162, 59, .16); }
.logincard button { min-height: 48px; border: 0; border-radius: 13px;
  background: var(--lamp); color: #201505; font-weight: 700; font-size: 1rem;
  cursor: pointer; transition: filter .2s, transform .12s; }
.logincard button:hover { filter: brightness(1.08); }
.logincard button:active { transform: scale(.98); }
.logincard .hint { text-align: center; padding: 4px 0 0; font-size: .84rem; }

/* ---------- Motion & a11y ---------- */
.spinner { display: inline-block; width: 16px; height: 16px; vertical-align: -3px;
  border: 2px solid currentColor; border-top-color: transparent;
  border-radius: 50%; animation: spin .8s linear infinite; opacity: .8; }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes lamp-on { from { opacity: 0; transform: translateY(-8px); } }
@keyframes shelf-in { from { opacity: 0; transform: translateY(14px); } }
@keyframes modal-in { from { opacity: 0; transform: scale(.96); } }
@keyframes toast-up { from { opacity: 0;
  transform: translateX(-50%) translateY(10px); } }

:focus-visible { outline: 2px solid var(--lamp); outline-offset: 2px; }

@media (max-width: 480px) {
  .grid { grid-template-columns: repeat(2, 1fr); gap: 18px 12px; }
  h1 { font-size: 1.3rem; }
  main { padding: 14px 12px calc(24px + env(safe-area-inset-bottom)); }
}
@media (pointer: coarse) {
  #modalHint { display: none; }
  .card:hover .cover, .card:hover .cover-fallback { transform: none; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important; animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}

/* ---------- Trending shelves ---------- */
.trend-head { margin: 10px 2px 2px; }
.trend-head .eyebrow { display: block; color: var(--lamp); font-size: .72rem;
  font-weight: 700; letter-spacing: .18em; text-transform: uppercase; }
.trend-head h2 { font-family: var(--serif); font-style: italic; font-weight: 650;
  font-size: 1.5rem; margin-top: 2px; display: inline-block; }
.trend-head .trend-note { color: var(--paper-dim); font-size: .78rem;
  margin-left: 10px; }
.shelf { display: grid; grid-auto-flow: column; grid-auto-columns: 150px;
  gap: 14px; overflow-x: auto; scroll-snap-type: x proximity;
  padding: 4px 2px 10px; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
.shelf::-webkit-scrollbar { display: none; }
.shelf .card { scroll-snap-align: start; }
@media (max-width: 480px) {
  .shelf { grid-auto-columns: 38vw; }
}

/* ---------- Discovery tag chips ---------- */
.tagrow { display: flex; gap: 8px; overflow-x: auto; padding: 12px 2px 4px;
  scrollbar-width: none; -webkit-overflow-scrolling: touch; }
.tagrow::-webkit-scrollbar { display: none; }
.tag { flex: none; min-height: 34px; padding: 0 14px; border-radius: 999px;
  border: 1px solid var(--line); background: transparent; color: var(--paper-dim);
  font-size: .84rem; cursor: pointer;
  transition: color .2s, background .2s, border-color .2s; }
.tag:hover { color: var(--paper); }
.tag.active { background: var(--lamp); border-color: var(--lamp);
  color: #201505; font-weight: 650; }
.section-head .tag-note { color: var(--paper-dim); font-size: .74rem;
  font-style: italic; white-space: nowrap; }
