:root {
  --bg: #f6f8fb;
  --surface: #ffffff;
  --surface-alt: #eef2f8;
  --text: #0f172a;
  --text-muted: #475569;
  --border: #e2e8f0;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #eff6ff;
  --accent: #0f766e;
  --video-bg: #fef2f2;
  --video-text: #b91c1c;
  --video-border: #fecaca;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 8px 24px rgba(15, 23, 42, 0.08);
  --radius: 14px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  font-family: "Noto Sans Thai", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

main {
  flex: 1;
}

a {
  color: inherit;
}

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

.hidden {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.container {
  width: min(1200px, calc(100% - 2rem));
  margin-inline: auto;
}

/* Header */
.site-header {
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  min-height: 64px;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 0.75rem;
  text-decoration: none;
}

.brand-mark {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  background: var(--primary-soft);
  border-radius: 10px;
  font-size: 1.25rem;
}

.brand strong {
  display: block;
  font-size: 1rem;
  line-height: 1.2;
}

.brand small {
  display: block;
  font-size: 0.75rem;
  color: var(--text-muted);
}

.header-links {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.header-links a {
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--text-muted);
  padding: 0.45rem 0.75rem;
  border-radius: 8px;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.header-links a:hover {
  background: var(--surface-alt);
  color: var(--text);
}

/* Hero */
.hero {
  background: linear-gradient(135deg, #ffffff 0%, #eff6ff 100%);
  border-bottom: 1px solid var(--border);
  padding: 2.5rem 0;
}

.hero-grid {
  display: grid;
  grid-template-columns: 1.25fr 0.75fr;
  gap: 2rem;
  align-items: center;
}

.eyebrow {
  display: inline-block;
  background: var(--primary-soft);
  color: var(--primary);
  font-size: 0.82rem;
  font-weight: 600;
  padding: 0.3rem 0.75rem;
  border-radius: 999px;
  margin-bottom: 0.75rem;
}

.hero h1 {
  margin: 0 0 0.75rem;
  font-size: clamp(1.75rem, 3.2vw, 2.5rem);
  line-height: 1.25;
}

.hero-sub {
  display: block;
  font-size: 0.55em;
  color: var(--primary);
  font-weight: 600;
  margin-top: 0.25rem;
}

.hero p {
  margin: 0 0 1.5rem;
  color: var(--text-muted);
  max-width: 62ch;
}

.hero-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
}

.stat-pill {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.65rem 1rem;
  box-shadow: var(--shadow-sm);
  display: flex;
  align-items: baseline;
  gap: 0.45rem;
}

.stat-pill strong {
  font-size: 1.15rem;
  color: var(--primary);
}

.stat-pill span {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.hero-media {
  display: flex;
  justify-content: center;
}

.hero-media img {
  max-width: 100%;
  height: auto;
  border-radius: 16px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow-md);
  background: var(--surface);
}

/* Recent section */
.recent-section {
  padding: 1.75rem 0 0.5rem;
}

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.85rem;
}

.section-head h2 {
  margin: 0;
  font-size: 1.15rem;
}

.text-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  padding: 0.25rem 0.5rem;
  border-radius: 6px;
}

.text-btn:hover {
  color: var(--text);
  background: var(--surface-alt);
}

.recent-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 0.85rem;
}

.recent-card {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.75rem 0.9rem;
  text-decoration: none;
  box-shadow: var(--shadow-sm);
  transition: transform 0.15s ease, border-color 0.15s ease;
}

.recent-card:hover {
  transform: translateY(-2px);
  border-color: var(--primary);
}

.recent-card-title {
  font-weight: 600;
  font-size: 0.92rem;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.recent-card-meta {
  font-size: 0.78rem;
  color: var(--text-muted);
}

/* Catalog */
.catalog-section {
  padding: 1.75rem 0 3.5rem;
}

.toolbar {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.search-wrap {
  position: relative;
}

.search-icon {
  position: absolute;
  left: 1rem;
  top: 50%;
  transform: translateY(-50%);
  pointer-events: none;
  font-size: 1rem;
}

#search-input {
  width: 100%;
  padding: 0.85rem 1rem 0.85rem 2.75rem;
  border: 1px solid var(--border);
  border-radius: 12px;
  font: inherit;
  font-size: 1rem;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

#search-input:focus {
  border-color: var(--primary);
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.chip {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  font: inherit;
  font-size: 0.9rem;
  font-weight: 500;
  padding: 0.48rem 0.95rem;
  border-radius: 999px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.chip:hover {
  border-color: var(--primary);
  color: var(--primary);
}

.chip.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #ffffff;
}

.catalog-meta {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
}

.catalog-meta h2 {
  margin: 0;
  font-size: 1.25rem;
}

.result-count {
  margin: 0;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.books-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1.15rem;
}

.book-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.2rem;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  box-shadow: var(--shadow-sm);
  transition: transform 0.18s ease, box-shadow 0.18s ease, border-color 0.18s ease;
}

.book-card:hover {
  transform: translateY(-3px);
  box-shadow: var(--shadow-md);
  border-color: #cbd5e1;
}

.card-top {
  display: flex;
  align-items: flex-start;
  gap: 0.85rem;
}

.icon-box {
  width: 48px;
  height: 48px;
  flex: 0 0 48px;
  border-radius: 12px;
  background: var(--surface-alt);
  display: grid;
  place-items: center;
  overflow: hidden;
}

.icon-box img {
  width: 36px;
  height: 36px;
  object-fit: contain;
}

.icon-box.sm {
  width: 38px;
  height: 38px;
  flex: 0 0 38px;
  border-radius: 10px;
}

.icon-box.sm img {
  width: 28px;
  height: 28px;
}

.icon-fallback {
  display: none;
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--primary);
}

.card-badges {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.45rem;
  margin-bottom: 0.35rem;
}

.badge {
  display: inline-block;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.18rem 0.55rem;
  border-radius: 999px;
  background: var(--primary-soft);
  color: var(--primary);
}

.size-tag {
  font-size: 0.78rem;
  color: var(--text-muted);
}

.card-title {
  margin: 0;
  font-size: 1.05rem;
  line-height: 1.35;
}

.card-desc {
  margin: 0;
  font-size: 0.9rem;
  color: var(--text-muted);
  flex: 1;
}

.card-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: auto;
  padding-top: 0.25rem;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  text-decoration: none;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 0.52rem 0.85rem;
  min-height: 38px;
  line-height: 1.2;
  font: inherit;
  font-size: 0.86rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.btn-primary {
  background: var(--primary);
  color: #ffffff;
}

.btn-primary:hover {
  background: var(--primary-hover);
}

.btn-secondary {
  background: var(--surface-alt);
  color: var(--text);
  border-color: var(--border);
}

.btn-secondary:hover {
  background: #e2e8f0;
}

.btn-video {
  background: var(--video-bg);
  color: var(--video-text);
  border-color: var(--video-border);
}

.btn-video:hover {
  background: #fee2e2;
}

.btn-ghost {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
}

.btn-ghost:hover {
  background: var(--surface-alt);
}

/* Empty state */
.empty-state {
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  padding: 2.5rem 1.25rem;
  text-align: center;
}

.empty-icon {
  font-size: 2.25rem;
  margin: 0 0 0.5rem;
}

.empty-state h2,
.empty-state h3 {
  margin: 0 0 0.4rem;
}

.empty-state p {
  margin: 0 auto 1.2rem;
  max-width: 44ch;
  color: var(--text-muted);
}

/* Footer */
.site-footer {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 1.5rem 0;
}

.footer-inner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.footer-title {
  margin: 0;
  font-weight: 700;
}

.footer-note {
  margin: 0.2rem 0 0;
  font-size: 0.86rem;
  color: var(--text-muted);
}

.footer-links {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}

.footer-links a {
  font-size: 0.9rem;
  color: var(--primary);
  text-decoration: none;
  font-weight: 500;
}

.footer-links a:hover {
  text-decoration: underline;
}

/* Viewer page */
.viewer-page {
  height: 100vh;
  overflow: hidden;
}

.viewer-bar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0.65rem 1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.viewer-bar-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 0;
  flex: 1;
}

.viewer-title-wrap {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
  flex-wrap: wrap;
}

.viewer-title {
  margin: 0;
  font-size: 1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.viewer-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.viewer-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: #334155;
}

.pdf-frame {
  width: 100%;
  flex: 1;
  border: 0;
  background: #ffffff;
}

.mobile-hint {
  display: none;
  background: var(--primary-soft);
  border-bottom: 1px solid #bfdbfe;
  padding: 0.85rem 1rem;
  color: var(--text);
}

.mobile-hint p {
  margin: 0 0 0.65rem;
  font-size: 0.88rem;
}

.mobile-hint-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.viewer-missing {
  padding: 2rem 1rem;
  margin: auto;
  width: min(520px, 100%);
}

@media (max-width: 820px) {
  .hero-grid {
    grid-template-columns: 1fr;
  }

  .hero-media {
    order: -1;
  }

  .hero-media img {
    max-height: 220px;
  }
}

@media (max-width: 768px), (pointer: coarse) {
  .mobile-hint {
    display: block;
  }
}

@media (max-width: 600px) {
  .header-links {
    display: none;
  }

  .viewer-page {
    height: auto;
    min-height: 100vh;
    overflow: auto;
  }

  .pdf-frame {
    min-height: 70vh;
  }
}
