@font-face {
  font-family: "Saysettha MX";
  src: url("../fonts/SaysetthaMX.ttf") format("truetype");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: "Saysettha MX";
  src: url("../fonts/SaysetthaMX-Bold.ttf") format("truetype");
  font-weight: 700;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: "DokChampa";
  src: url("../fonts/DokChampa.ttf") format("truetype");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

:root {
  --bg: #fff8f0;
  --surface: #ffffff;
  --primary: #e85d4c;
  --primary-dark: #c94a3b;
  --accent: #2d9c6e;
  --text: #2c2c2c;
  --muted: #6b6b6b;
  --border: #f0e4d8;
  --shadow: 0 8px 24px rgba(44, 44, 44, 0.08);
  --radius: 16px;
  --title-red: #7a1228;
  --font-lao: "Saysettha MX", "Noto Sans Lao", "Phetsarath OT", sans-serif;
  --font-title: "DokChampa", "Saysettha MX", sans-serif;
  --font-ui: "Nunito", system-ui, sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font-ui);
  color: var(--text);
  background: var(--bg);
  line-height: 1.5;
}

a {
  color: inherit;
  text-decoration: none;
}

img {
  max-width: 100%;
  height: auto;
  display: block;
}

/* Header */
.site-header {
  background: linear-gradient(135deg, var(--primary) 0%, #f4a261 100%);
  color: #fff;
  padding: 1rem 1.25rem 1.35rem;
  border-radius: 0 0 1.5rem 1.5rem;
  box-shadow: var(--shadow);
}

.site-header__inner {
  max-width: 1100px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.site-header__admin {
  font-family: var(--font-ui);
  font-size: 0.85rem;
  font-weight: 700;
  color: #fff;
  text-decoration: none;
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.35);
  white-space: nowrap;
}

.site-header__admin:hover {
  background: rgba(255, 255, 255, 0.32);
}

.site-header a.logo {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  color: #fff;
  font-family: var(--font-title);
  font-weight: 400;
  font-size: 1.65rem;
  line-height: 1.1;
  letter-spacing: 0.02em;
}

.logo span {
  font-size: 1.5rem;
}

/* Toolbar */
.toolbar {
  max-width: 1100px;
  margin: -0.85rem auto 1.5rem;
  padding: 0 1rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: center;
}

.search-box {
  flex: 1;
  min-width: 200px;
  position: relative;
}

.search-box input {
  width: 100%;
  padding: 0.85rem 1rem 0.85rem 2.75rem;
  border: none;
  border-radius: 999px;
  font-size: 1rem;
  font-family: var(--font-ui);
  box-shadow: var(--shadow);
  background: var(--surface);
}

.search-box::before {
  content: "🔍";
  position: absolute;
  left: 1rem;
  top: 50%;
  transform: translateY(-50%);
  font-size: 0.9rem;
}

.filter-select {
  padding: 0.75rem 1rem;
  border-radius: 999px;
  border: 2px solid var(--border);
  background: var(--surface);
  font-family: var(--font-ui);
  font-size: 0.9rem;
  cursor: pointer;
}

.stats {
  font-size: 0.85rem;
  color: var(--muted);
  width: 100%;
}

/* Book grid */
.main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 0 1rem 3rem;
}

.book-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 1.25rem;
}

.book-card {
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  transition: transform 0.2s, box-shadow 0.2s;
  display: flex;
  flex-direction: column;
}

.book-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 12px 32px rgba(44, 44, 44, 0.12);
}

.book-card__cover {
  aspect-ratio: 3 / 4;
  background: var(--border);
  overflow: hidden;
}

.book-card__cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.book-card__cover--placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.5rem;
  color: var(--muted);
}

.book-card__body {
  padding: 0.75rem;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.book-card__title {
  font-family: var(--font-title);
  font-size: 1.15rem;
  font-weight: 400;
  line-height: 1.25;
  color: var(--title-red);
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.book-card__meta {
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: auto;
}

.badge {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  background: #e8f5ef;
  color: var(--accent);
  font-size: 0.7rem;
  font-weight: 700;
}

.empty-state {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--muted);
}

.empty-state h2 {
  color: var(--text);
}

/* Reader */
body.reader-body {
  background: linear-gradient(180deg, #fff5eb 0%, var(--bg) 12rem);
}

.reader-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.65rem 1rem;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.reader-header__title {
  font-family: var(--font-title);
  font-size: 1.55rem;
  font-weight: 400;
  line-height: 1.15;
  letter-spacing: 0.02em;
  flex: 1;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--title-red);
}

.btn--play-all {
  background: var(--accent);
  color: #fff;
}

.btn--play-all:hover {
  background: #248f5c;
}

.btn--play-all.is-active {
  box-shadow: 0 0 0 3px rgba(45, 156, 110, 0.35);
  animation: tts-pulse 1.2s ease infinite;
}

.btn--play-all .icon-play-all {
  display: block;
}

.btn--icon {
  width: 2.75rem;
  height: 2.75rem;
  padding: 0;
  border-radius: 50%;
  flex-shrink: 0;
}

.btn--icon .icon-home {
  display: block;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 999px;
  font-family: var(--font-ui);
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
  background: var(--border);
  color: var(--text);
  transition: background 0.15s;
}

.btn:hover {
  background: #e5d5c5;
}

.btn--primary {
  background: var(--primary);
  color: #fff;
}

.btn--primary:hover {
  background: var(--primary-dark);
}

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.reader {
  max-width: 520px;
  margin: 0 auto;
  padding: 0.75rem 1rem 5.5rem;
}

.reader__progress {
  height: 4px;
  background: var(--border);
  border-radius: 999px;
  margin-bottom: 1rem;
  overflow: hidden;
}

.reader__progress-bar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--primary), #f4a261);
  border-radius: 999px;
  transition: width 0.25s ease;
}

.reader-page {
  background: var(--surface);
  border-radius: 20px;
  box-shadow:
    0 2px 8px rgba(44, 44, 44, 0.04),
    0 12px 40px rgba(232, 93, 76, 0.1);
  border: 1px solid rgba(240, 228, 216, 0.9);
  overflow: hidden;
}

.reader-page__figure {
  margin: 0;
  padding: 1rem 1rem 0.5rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
  background: linear-gradient(180deg, #fef9f3 0%, #fff 100%);
}

.reader-page__badge {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  z-index: 2;
  min-width: 2rem;
  padding: 0.2rem 0.55rem;
  font-family: var(--font-ui);
  font-size: 0.75rem;
  font-weight: 800;
  color: var(--primary);
  background: #fff;
  border-radius: 999px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  text-align: center;
}

.reader-page__image-frame {
  width: 100%;
  max-width: 280px;
  max-height: min(38vh, 320px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.5rem;
  background: #fff;
  border-radius: 12px;
  border: 1px solid var(--border);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.8);
}

.reader-page__image-frame img {
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: min(36vh, 300px);
  object-fit: contain;
  border-radius: 6px;
}

.reader-page__content {
  padding: 0.85rem 1.15rem 1.25rem;
  border-top: 1px dashed var(--border);
  text-align: center;
}

.reader-page__text {
  margin: 0 auto;
  max-width: 36em;
  font-family: var(--font-lao);
  font-size: 1.2rem;
  line-height: 1.95;
  color: var(--text);
  text-align: center;
}

.reader-page__text:empty::before {
  content: "—";
  color: var(--muted);
}

.reader-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.65rem 0.75rem max(0.65rem, env(safe-area-inset-bottom));
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-top: 1px solid var(--border);
  box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.06);
}

.reader-nav .btn--icon {
  width: 3rem;
  height: 3rem;
}

.reader-nav .btn--icon-primary {
  background: var(--primary);
  color: #fff;
}

.reader-nav .btn--icon-primary:hover:not(:disabled) {
  background: var(--primary-dark);
}

.reader-nav .btn--icon:disabled {
  opacity: 0.35;
}

.reader-nav__tools {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  margin-right: auto;
}

.btn--tts {
  background: var(--accent);
  color: #fff;
}

.btn--tts:hover {
  background: #248f5c;
}

.btn--tts.is-playing {
  box-shadow: 0 0 0 3px rgba(45, 156, 110, 0.35);
  animation: tts-pulse 1.2s ease infinite;
}

.btn--tts.is-loading {
  opacity: 0.75;
  animation: tts-pulse 0.8s ease infinite;
}

@keyframes tts-pulse {
  50% {
    opacity: 0.85;
  }
}

.btn--tts-secondary {
  width: 2.5rem;
  height: 2.5rem;
  font-family: var(--font-title);
  font-size: 1.1rem;
  color: var(--title-red);
  background: #fff;
  border: 1px solid var(--border);
}

.btn--tts-secondary.is-active {
  background: #e8f5ef;
  border-color: var(--accent);
  color: var(--accent);
}

.reader-nav__center {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
  min-width: 4.5rem;
}

.reader-nav__progress {
  font-size: 0.8rem;
  color: var(--muted);
  font-weight: 800;
  text-align: center;
  font-family: var(--font-ui);
}

.reader-nav__status {
  font-size: 0.65rem;
  color: var(--accent);
  font-weight: 700;
  text-align: center;
  max-width: 7rem;
  line-height: 1.2;
  min-height: 0.8rem;
}

.reader-loading,
.reader-error {
  text-align: center;
  padding: 4rem 1rem;
  color: var(--muted);
}

.reader-error h2 {
  color: var(--primary);
}

@media (min-width: 768px) {
  .site-header a.logo {
    font-size: 1.85rem;
  }

  .reader-header__title {
    font-size: 1.75rem;
  }

  .book-card__title {
    font-size: 1.2rem;
  }

  .book-grid {
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  }

  .reader {
    max-width: 680px;
    padding-top: 1.25rem;
  }

  .reader-page {
    display: grid;
    grid-template-columns: minmax(200px, 42%) 1fr;
    align-items: stretch;
  }

  .reader-page__figure {
    padding: 1.25rem;
    border-right: 1px dashed var(--border);
    min-height: 100%;
    justify-content: center;
  }

  .reader-page__image-frame {
    max-width: 240px;
    max-height: 360px;
  }

  .reader-page__image-frame img {
    max-height: 340px;
  }

  .reader-page__content {
    display: flex;
    align-items: center;
    justify-content: center;
    border-top: none;
    padding: 1.5rem 1.5rem 1.5rem 1.25rem;
  }

  .reader-page__text {
    font-size: 1.35rem;
    line-height: 2;
  }
}
