:root {
  --bg: #f5f3ee;
  --surface: #ffffff;
  --surface-2: #faf9f6;
  --text: #1c2130;
  --muted: #62697a;
  --line: #e7e3da;
  --accent: #3563d9;
  --accent-text: #ffffff;
  --on-c: #ffffff;
  --new: #e8590c;
  --shadow: 0 1px 2px rgba(28, 33, 48, 0.05), 0 4px 14px rgba(28, 33, 48, 0.06);
  --shadow-lift: 0 2px 4px rgba(28, 33, 48, 0.06), 0 10px 24px rgba(28, 33, 48, 0.1);
  --radius: 18px;
  --hero-from: #3563d9;
  --hero-to: #7b4fd6;

  --c-science: #13805f;
  --c-math: #2f63d6;
  --c-english: #c2410c;
  --c-social: #7c4dce;
  --c-thai: #c0266d;
  --c-other: #5b6472;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #13151a;
    --surface: #1d2129;
    --surface-2: #232833;
    --text: #eceef2;
    --muted: #a0a7b4;
    --line: #2d323c;
    --accent: #7ea6f7;
    --accent-text: #0e1522;
    --on-c: #13151a;
    --new: #ff8a50;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-lift: 0 6px 20px rgba(0, 0, 0, 0.4);
    --hero-from: #2a4fb0;
    --hero-to: #5a36a8;

    --c-science: #4fd6a6;
    --c-math: #7ea6f7;
    --c-english: #f7a26a;
    --c-social: #b797f3;
    --c-thai: #f47bb2;
    --c-other: #a9b0bc;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Sarabun", system-ui, sans-serif;
  font-size: 17px;
  line-height: 1.6;
}

a { color: inherit; }

.wrap {
  max-width: 760px;
  margin: 0 auto;
  padding: 0 16px;
}

/* ---------- หัวเว็บ ---------- */
.hero {
  padding: 16px 0 6px;
}
.hero-card {
  position: relative;
  overflow: hidden;
  padding: 24px 22px 22px;
  border-radius: 24px;
  color: #fff;
  background: linear-gradient(135deg, var(--hero-from), var(--hero-to));
  box-shadow: 0 4px 12px color-mix(in srgb, var(--hero-to) 20%, transparent);
}
/* วงกลมตกแต่งด้านหลัง */
.hero-card::before,
.hero-card::after {
  content: "";
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  pointer-events: none;
}
.hero-card::before { width: 220px; height: 220px; right: -70px; top: -90px; }
.hero-card::after { width: 120px; height: 120px; right: 60px; bottom: -70px; }
.hero-emoji {
  position: absolute;
  right: 18px;
  top: 16px;
  font-size: 44px;
  line-height: 1;
  transform: rotate(8deg);
  filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.2));
}
.hero h1 {
  position: relative;
  margin: 0;
  padding-right: 56px;
  font-size: 1.85rem;
  font-weight: 700;
  line-height: 1.25;
}
.hero h1 .grade {
  display: inline-block;
  margin-left: 4px;
  padding: 0 10px;
  border-radius: 10px;
  background: #ffd43b;
  color: #3b2a00;
  font-size: 0.8em;
  transform: rotate(-3deg);
}
.hero p {
  position: relative;
  margin: 8px 0 0;
  max-width: 30em;
  color: rgba(255, 255, 255, 0.88);
}
.hero-stats {
  position: relative;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 16px;
}
.hero-stats span {
  padding: 3px 12px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.18);
  font-size: 0.92rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  backdrop-filter: blur(4px);
}
.hero-stats span:empty { display: none; }

/* ---------- ช่องค้นหา + หมวดวิชา (ติดด้านบน) ---------- */
.toolbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  padding: 14px 0 10px;
  border-bottom: 1px solid transparent;
  transition: border-color 0.2s;
}
.toolbar.stuck { border-bottom-color: var(--line); }

.search {
  position: relative;
  display: block;
}
.search svg {
  position: absolute;
  left: 16px;
  top: 50%;
  transform: translateY(-50%);
  width: 20px;
  height: 20px;
  color: var(--muted);
  pointer-events: none;
}
.search input {
  width: 100%;
  font: inherit;
  font-size: 17px; /* >=16px กัน iPhone ซูมอัตโนมัติ */
  padding: 12px 46px;
  border: 1.5px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  outline: none;
  box-shadow: var(--shadow);
  -webkit-appearance: none;
  appearance: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.search input::placeholder { color: var(--muted); opacity: 0.8; }
.search input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 18%, transparent);
}
.search input::-webkit-search-cancel-button { display: none; }
.search .clear {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 50%;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}
.search .clear[hidden] { display: none; }

.chips {
  display: flex;
  gap: 8px;
  margin: 10px -16px 0;
  padding: 2px 16px 4px;
  overflow-x: auto;
  scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  --c: var(--accent);
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  padding: 6px 8px 6px 14px;
  border-radius: 999px;
  border: 1.5px solid var(--line);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}
.chip .n {
  min-width: 24px;
  padding: 0 7px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 13px;
  line-height: 22px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}
.chip[aria-pressed="true"] {
  background: var(--c);
  border-color: var(--c);
  color: var(--on-c);
}
.chip[aria-pressed="true"] .n {
  background: rgba(255, 255, 255, 0.22);
  color: inherit;
}

/* ---------- หมวดวิชา ---------- */
.subject {
  margin: 22px 0 30px;
  scroll-margin-top: 140px;
}
.subject-head {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}
.subject-icon {
  width: 46px;
  height: 46px;
  flex: none;
  display: grid;
  place-items: center;
  border-radius: 14px;
  font-size: 24px;
  background: color-mix(in srgb, var(--c) 15%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--c) 25%, transparent);
}
.subject-head h2 {
  flex: 1;
  margin: 0;
  font-size: 1.35rem;
  font-weight: 700;
  line-height: 1.3;
}
.subject-head .count {
  flex: none;
  padding: 2px 12px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--c) 12%, transparent);
  color: var(--c);
  font-size: 0.9rem;
  font-weight: 700;
}

.sheet-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 12px;
}

.sheet {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
  transition: box-shadow 0.2s, transform 0.2s;
}
.sheet-main {
  display: flex;
  align-items: flex-start;
  gap: 14px;
  text-decoration: none;
}
/* ไอคอนไฟล์ PDF สีตามวิชา */
.sheet-thumb {
  position: relative;
  flex: none;
  width: 48px;
  height: 58px;
  border-radius: 8px 16px 8px 8px;
  background: color-mix(in srgb, var(--c) 14%, var(--surface));
  border: 1.5px solid color-mix(in srgb, var(--c) 35%, transparent);
  color: var(--c);
  display: grid;
  place-items: center;
}
.sheet-thumb::after {
  content: "";
  position: absolute;
  right: -1.5px;
  top: -1.5px;
  width: 16px;
  height: 16px;
  border-radius: 0 16px 0 6px;
  background: color-mix(in srgb, var(--c) 35%, var(--surface));
}
.sheet-thumb b {
  margin-top: 10px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
}
.sheet-info {
  flex: 1;
  min-width: 0;
}
.sheet-title {
  display: block;
  font-size: 1.1rem;
  font-weight: 600;
  line-height: 1.45;
  word-break: break-word;
}
.sheet-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 2px 10px;
  margin-top: 4px;
  font-size: 0.88rem;
  color: var(--muted);
}
.sheet-meta span + span::before {
  content: "•";
  margin-right: 10px;
  opacity: 0.6;
}
.badge-new {
  padding: 0 9px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 700;
  line-height: 1.7;
  color: var(--on-c);
  background: var(--new);
}
.sheet-meta .badge-new::before { content: none; }
.sheet-actions {
  display: flex;
  gap: 8px;
  margin-top: 14px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 46px;
  padding: 8px 16px;
  border-radius: 12px;
  font: inherit;
  font-size: 16px;
  font-weight: 600;
  text-decoration: none;
  border: 1.5px solid var(--line);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: filter 0.15s, background 0.15s;
}
.btn svg { width: 18px; height: 18px; flex: none; }
.btn-primary {
  flex: 1;
  background: var(--c, var(--accent));
  border-color: var(--c, var(--accent));
  color: var(--on-c);
}
.btn:hover { filter: brightness(0.96); }
.btn-primary:hover { filter: brightness(1.08); }
.btn:active { transform: translateY(1px); }

.empty {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 18px;
  border: 1.5px dashed color-mix(in srgb, var(--c) 30%, var(--line));
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--c) 4%, transparent);
  color: var(--muted);
  font-size: 0.95rem;
  line-height: 1.5;
}
.empty .e-icon { font-size: 26px; flex: none; }
.empty b { display: block; color: var(--text); font-weight: 600; }

.notice {
  margin: 48px 0;
  text-align: center;
  color: var(--muted);
}
.notice .e-icon { display: block; font-size: 44px; margin-bottom: 6px; }
.notice strong { display: block; color: var(--text); font-size: 1.1rem; }

mark {
  background: color-mix(in srgb, #ffd43b 60%, transparent);
  color: inherit;
  border-radius: 4px;
  padding: 0 1px;
}

.site-foot {
  margin: 44px auto 32px;
  color: var(--muted);
  font-size: 0.9rem;
  text-align: center;
}

@media (hover: hover) {
  .sheet:hover { box-shadow: var(--shadow-lift); transform: translateY(-2px); }
}

@media (min-width: 640px) {
  .hero { padding-top: 28px; }
  .hero-card { padding: 34px 36px 30px; }
  .hero h1 { font-size: 2.3rem; }
  .hero-emoji { font-size: 64px; right: 36px; top: 50%; transform: translateY(-50%) rotate(8deg); }
  .sheet { display: flex; align-items: center; gap: 12px; }
  .sheet-main { flex: 1; min-width: 0; align-items: center; }
  .sheet-actions { margin-top: 0; flex: none; }
  .btn-primary { flex: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

/* ---------- หน้าเปิดดูชีต ---------- */
.viewer-body { background: var(--bg); }
.viewer-bar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.viewer-bar h1 {
  flex: 1;
  min-width: 0;
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
  line-height: 1.35;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.icon-btn {
  flex: none;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 10px;
  border: 1.5px solid var(--line);
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
  font: inherit;
  font-size: 20px;
  cursor: pointer;
}
.icon-btn svg { width: 20px; height: 20px; }
.viewer-status {
  padding: 40px 16px;
  text-align: center;
  color: var(--muted);
}
.viewer-status .btn { margin-top: 12px; }
.pages {
  display: grid;
  gap: 12px;
  padding: 12px 12px 88px;
  justify-items: safe center;
  overflow-x: auto;
}
.page {
  background: #fff;
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
  overflow: hidden;
}
.page canvas { display: block; width: 100%; height: 100%; }
.zoom {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}
.zoom .icon-btn { border: 0; border-radius: 50%; }
.zoom span {
  min-width: 48px;
  text-align: center;
  font-size: 0.9rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
