/* ---------- Tokens ---------- */
:root {
  --bg: #f4f5f8;
  --bg-elev: #ffffff;
  --bg-sunk: #e9ebf1;
  --text: #15171c;
  --text-2: #5b6170;
  --text-3: #8a90a0;
  --line: #dfe2ea;
  --accent: #ff4d6d;
  --accent-ink: #ffffff;
  --accent-soft: rgba(255, 77, 109, .12);
  --focus: #3b82f6;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 8px 24px -12px rgba(0,0,0,.25);
  --shadow-lg: 0 24px 48px -16px rgba(0,0,0,.35);
  --r: 12px;
  --r-lg: 16px;
  --bar-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b0d12;
    --bg-elev: #141824;
    --bg-sunk: #0f121a;
    --text: #eef0f5;
    --text-2: #a3a9b8;
    --text-3: #6e7486;
    --line: #232838;
    --accent: #ff5c7a;
    --accent-soft: rgba(255, 92, 122, .16);
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px -12px rgba(0,0,0,.7);
    --shadow-lg: 0 24px 48px -16px rgba(0,0,0,.8);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b0d12;
  --bg-elev: #141824;
  --bg-sunk: #0f121a;
  --text: #eef0f5;
  --text-2: #a3a9b8;
  --text-3: #6e7486;
  --line: #232838;
  --accent: #ff5c7a;
  --accent-soft: rgba(255, 92, 122, .16);
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px -12px rgba(0,0,0,.7);
  --shadow-lg: 0 24px 48px -16px rgba(0,0,0,.8);
  color-scheme: dark;
}

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}
body.locked { overflow: hidden; }
button { font: inherit; color: inherit; }
a { color: var(--accent); }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip-link { position: absolute; left: -999px; top: 8px; background: var(--accent); color: var(--accent-ink); padding: 8px 12px; border-radius: 8px; z-index: 999; }
.skip-link:focus { left: 8px; }
[hidden] { display: none !important; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 50;
  background: color-mix(in srgb, var(--bg-elev) 82%, transparent);
  backdrop-filter: saturate(160%) blur(14px);
  -webkit-backdrop-filter: saturate(160%) blur(14px);
  border-bottom: 1px solid var(--line);
}
.topbar-inner {
  max-width: 1400px; margin: 0 auto; padding: 0 20px;
  min-height: var(--bar-h);
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 16px;
}
.brand { display: inline-flex; align-items: center; gap: 8px; color: var(--text); text-decoration: none; font-weight: 800; letter-spacing: -.02em; font-size: 18px; }
.brand-mark { width: 28px; height: 28px; color: var(--accent); }

.search { position: relative; max-width: 560px; width: 100%; justify-self: center; }
.search-icon { position: absolute; left: 12px; top: 50%; translate: 0 -50%; width: 18px; height: 18px; color: var(--text-3); pointer-events: none; }
.search input {
  width: 100%; height: 40px; padding: 0 44px 0 38px;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg-sunk); color: var(--text); font: inherit;
  transition: border-color .15s, box-shadow .15s, background .15s;
}
.search input:focus { outline: none; border-color: var(--focus); box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 25%, transparent); background: var(--bg-elev); }
.search input::-webkit-search-cancel-button { -webkit-appearance: none; }
.search-kbd { position: absolute; right: 10px; top: 50%; translate: 0 -50%; font: 11px/1 ui-monospace, monospace; color: var(--text-3); border: 1px solid var(--line); border-radius: 5px; padding: 3px 6px; }
.search input:focus ~ .search-kbd { opacity: 0; }

.controls { display: flex; align-items: center; gap: 8px; }
.select select {
  height: 40px; padding: 0 30px 0 12px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg-sunk) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23888' stroke-width='2'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E") no-repeat right 8px center / 16px;
  color: var(--text); appearance: none; -webkit-appearance: none; cursor: pointer; max-width: 200px;
}
.icon-btn {
  width: 40px; height: 40px; display: inline-grid; place-items: center;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg-sunk); cursor: pointer;
  transition: background .15s, border-color .15s, transform .1s;
}
.icon-btn:hover { background: var(--bg-elev); border-color: var(--text-3); }
.icon-btn:active { transform: scale(.96); }
.icon-btn svg { width: 20px; height: 20px; }
.heart { fill: none; stroke: currentColor; stroke-width: 2; stroke-linejoin: round; transition: fill .15s, stroke .15s; }
[aria-pressed="true"] .heart { fill: var(--accent); stroke: var(--accent); }
#favToggle[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); }

/* ---------- Page ---------- */
.page { max-width: 1400px; margin: 0 auto; padding: 24px 20px 80px; }
.notice { margin: 0 0 20px; padding: 12px 16px; border-radius: var(--r); background: var(--accent-soft); border: 1px solid color-mix(in srgb, var(--accent) 40%, transparent); display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.notice.error { background: rgba(239,68,68,.12); border-color: rgba(239,68,68,.4); }
.shelf { margin-bottom: 32px; }
.shelf-head { display: flex; align-items: baseline; gap: 12px; margin: 0 0 12px; }
.shelf-head h2 { margin: 0; font-size: 17px; font-weight: 700; letter-spacing: -.01em; }
.count { color: var(--text-3); font-size: 13px; }
.link-btn { background: none; border: 0; padding: 0; color: var(--text-2); cursor: pointer; font-size: 13px; text-decoration: underline; text-underline-offset: 3px; }
.link-btn:hover { color: var(--text); }
.btn {
  height: 38px; padding: 0 16px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--bg-elev); cursor: pointer; font-weight: 600;
}
.btn:hover { border-color: var(--text-3); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: #ef4444; }

/* Horizontal shelf */
.row {
  display: grid; grid-auto-flow: column; grid-auto-columns: 168px; gap: 14px;
  overflow-x: auto; padding: 4px 4px 12px; margin: -4px -4px 0;
  scroll-snap-type: x proximity; scrollbar-width: thin;
}
.row > .card { scroll-snap-align: start; }

/* Grid */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(168px, 1fr)); gap: 16px; }
.skeleton { aspect-ratio: 1 / 1.35; border-radius: var(--r-lg); background: linear-gradient(90deg, var(--bg-sunk) 25%, var(--bg-elev) 50%, var(--bg-sunk) 75%); background-size: 200% 100%; animation: shimmer 1.2s infinite; }
@keyframes shimmer { to { background-position: -200% 0; } }
.empty { text-align: center; padding: 60px 0; color: var(--text-2); }

/* ---------- Card ---------- */
.card {
  position: relative; border-radius: var(--r-lg); background: var(--bg-elev);
  border: 1px solid var(--line); overflow: hidden; box-shadow: var(--shadow);
  transition: transform .18s cubic-bezier(.2,.8,.2,1), box-shadow .18s, border-color .18s;
  will-change: transform;
}
.card:hover { transform: translateY(-4px); box-shadow: var(--shadow-lg); border-color: color-mix(in srgb, var(--accent) 50%, var(--line)); }
.card-hit { display: block; width: 100%; padding: 0; border: 0; background: none; text-align: left; cursor: pointer; }
.cover { position: relative; aspect-ratio: 1; background: var(--bg-sunk); overflow: hidden; }
.cover img { width: 100%; height: 100%; object-fit: cover; display: block; transition: transform .3s cubic-bezier(.2,.8,.2,1); opacity: 0; }
.cover img.loaded { opacity: 1; }
.card:hover .cover img { transform: scale(1.05); }
.cover-fallback { position: absolute; inset: 0; display: grid; place-items: center; font-size: 44px; font-weight: 800; color: var(--text-3); background: linear-gradient(135deg, var(--bg-sunk), var(--bg-elev)); }
.cover img.loaded + .cover-fallback { display: none; }
.card-body { padding: 10px 12px 12px; display: grid; gap: 2px; }
.card-title { font-weight: 600; font-size: 14px; line-height: 1.3; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.card-sub { font-size: 12px; color: var(--text-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .fav {
  position: absolute; top: 8px; right: 8px; width: 34px; height: 34px; border-radius: 50%;
  border: 0; display: grid; place-items: center; cursor: pointer;
  background: rgba(0,0,0,.45); color: #fff; backdrop-filter: blur(6px);
  opacity: 0; transition: opacity .15s, transform .1s;
}
.card .fav svg { width: 18px; height: 18px; }
.card:hover .fav, .card .fav[aria-pressed="true"], .card .fav:focus-visible { opacity: 1; }
.card .fav:active { transform: scale(.9); }
.badge { position: absolute; left: 8px; top: 8px; font-size: 11px; font-weight: 700; padding: 3px 7px; border-radius: 6px; background: rgba(0,0,0,.55); color: #fff; backdrop-filter: blur(6px); }
@media (hover: none) { .card .fav { opacity: 1; } }

/* ---------- Viewer ---------- */
.viewer { position: fixed; inset: 0; z-index: 100; background: #000; display: flex; flex-direction: column; animation: viewer-in .22s cubic-bezier(.2,.8,.2,1); }
@keyframes viewer-in { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }
.viewer-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 8px 14px; background: var(--bg-elev); border-bottom: 1px solid var(--line); color: var(--text);
  min-height: 52px;
}
.viewer-meta { flex: 1; min-width: 0; display: flex; align-items: baseline; gap: 10px; }
.viewer-title { margin: 0; min-width: 0; font-size: 16px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.viewer-author { font-size: 13px; color: var(--text-2); text-decoration: none; white-space: nowrap; }
.viewer-author:hover { color: var(--accent); text-decoration: underline; }
.viewer-actions { display: flex; gap: 6px; flex-shrink: 0; }
.vbtn { height: 34px; padding: 0 12px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg-sunk); cursor: pointer; font-size: 13px; font-weight: 600; display: inline-flex; align-items: center; gap: 6px; }
.vbtn:hover { border-color: var(--text-3); }
.vbtn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.vbtn svg { width: 18px; height: 18px; }
.viewer-stage { position: relative; flex: 1; min-height: 0; }
#frame { width: 100%; height: 100%; border: 0; display: block; background: #000; }
.viewer-status { position: absolute; inset: 0; display: grid; place-content: center; gap: 12px; justify-items: center; color: #ddd; background: #000; z-index: 1; }
.viewer-status.error { color: #fca5a5; }
.spinner { width: 34px; height: 34px; border: 3px solid rgba(255,255,255,.15); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Dialog ---------- */
.dialog { border: 1px solid var(--line); border-radius: var(--r-lg); padding: 0; width: min(560px, calc(100vw - 32px)); max-height: 85vh; background: var(--bg-elev); color: var(--text); box-shadow: var(--shadow-lg); }
.dialog::backdrop { background: rgba(0,0,0,.55); backdrop-filter: blur(4px); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 12px 20px; border-bottom: 1px solid var(--line); margin: 0; }
.dialog-head h2 { margin: 0; font-size: 17px; }
.dialog-body { padding: 18px 20px 20px; overflow: auto; max-height: calc(85vh - 60px); }
.dialog-body h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--text-3); margin: 18px 0 8px; }
.dialog-body h3:first-child { margin-top: 0; }
.dialog-body p { margin: 0 0 10px; color: var(--text-2); }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg button { padding: 8px 14px; border: 0; background: var(--bg-sunk); cursor: pointer; font-weight: 600; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }
.stack { display: grid; gap: 10px; }
.kv { display: grid; grid-template-columns: 140px 1fr; gap: 6px 12px; font-size: 14px; }
.kv dt { color: var(--text-3); overflow-wrap: anywhere; }
.kv dd { margin: 0; word-break: break-word; }
.chip { display: inline-block; font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); margin: 0 4px 4px 0; font-weight: 600; }
.status { font-size: 13px; color: var(--text-3); min-height: 1.2em; }
.toggle { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.toggle:last-child { border-bottom: 0; }
.toggle input { width: 18px; height: 18px; accent-color: var(--accent); }

/* ---------- Footer ---------- */
.foot { max-width: 1400px; margin: 0 auto; padding: 20px; color: var(--text-3); font-size: 13px; display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; border-top: 1px solid var(--line); }
.foot-links { display: flex; gap: 14px; }
.foot a { color: var(--text-2); }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  .topbar-inner { grid-template-columns: auto 1fr; grid-template-areas: "brand controls" "search search"; padding: 10px 14px; gap: 10px; }
  .brand { grid-area: brand; }
  .search { grid-area: search; max-width: none; }
  .controls { grid-area: controls; justify-self: end; }
  .select select { max-width: 150px; }
  .viewer-actions .vbtn:not(.primary):not(#vClose) span { display: none; }
  #vDownload, #vInfo { display: none; }
  .page { padding: 16px 14px 60px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; }
  .row { grid-auto-columns: 140px; }
}
@media (max-width: 520px) {
  .select select { max-width: 120px; padding-left: 8px; font-size: 13px; }
  .search-kbd { display: none; }
  .grid { grid-template-columns: repeat(2, 1fr); }
  .viewer-author { display: none; }
  #vNewTab, #vReload { display: none; }
  .viewer-bar { padding: 6px 10px; gap: 8px; }
  .vbtn { padding: 0 10px; font-size: 12px; }
  .kv { grid-template-columns: 1fr; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001s !important; transition-duration: .001s !important; }
}
