:root {
  --purple: #4b1f78;
  --purple-dark: #331454;
  --purple-soft: #f1ebf8;
  --yellow: #f6c700;
  --yellow-soft: #fff6cc;
  --ink: #241b2f;
  --muted: #6b6178;
  --line: #e4dcee;
  --bg: #faf8fc;
  --card: #ffffff;
  --radius: 14px;
}

* { box-sizing: border-box; }

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

h1, h2, .brand strong { font-family: 'Prompt', 'Sarabun', sans-serif; font-weight: 600; }

a { color: var(--purple); }

[hidden] { display: none !important; }

/* ===== Header ===== */
.site-header {
  background: var(--purple);
  border-bottom: 5px solid var(--yellow);
  padding: 10px 16px;
}
.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  max-width: 1200px;
  margin: 0 auto;
}
.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  color: #fff;
  text-decoration: none;
  min-width: 0;
}
.nav-me {
  flex-shrink: 0;
  background: var(--yellow);
  color: var(--purple-dark);
  font-weight: 600;
  text-decoration: none;
  padding: 8px 16px;
  border-radius: 999px;
}
.nav-me:hover { filter: brightness(1.05); }
.brand img {
  height: 56px;
  width: auto;
  background: #fff;
  border-radius: 10px;
  padding: 3px;
}
.brand strong { display: block; font-size: 1.35rem; line-height: 1.2; }
.brand small { color: var(--yellow); font-size: 0.95rem; }

main { max-width: 1200px; margin: 0 auto; padding: 20px 16px 48px; }

.status { color: var(--muted); text-align: center; padding: 40px 0; }

/* ===== Filters ===== */
.filters {
  display: grid;
  grid-template-columns: 1fr repeat(3, 150px) auto;
  gap: 10px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
}
.filters input, .filters select, .filters button {
  font: inherit;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0 12px;
  background: #fff;
  color: var(--ink);
  min-width: 0;
}
.filters input:focus, .filters select:focus {
  outline: 2px solid var(--purple);
  outline-offset: 1px;
}
.filters select:disabled { background: #f4f2f6; color: #aaa; }
.btn-ghost { cursor: pointer; color: var(--purple) !important; }
.btn-ghost:hover { background: var(--purple-soft) !important; }

/* ===== Tags ===== */
.tags { display: flex; flex-wrap: wrap; gap: 6px; margin: 14px 0 0; }
.tag {
  font: inherit;
  font-size: 0.9rem;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--purple);
  border-radius: 999px;
  padding: 2px 12px;
  cursor: pointer;
  text-decoration: none;
}
.tag:hover { background: var(--purple-soft); }
.tag.active { background: var(--purple); border-color: var(--purple); color: #fff; }

.count { color: var(--muted); margin: 14px 0 10px; }

/* ===== Album grid ===== */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 18px;
}
.card {
  display: flex;
  flex-direction: column;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  color: inherit;
  text-decoration: none;
  transition: transform .15s, box-shadow .15s;
}
.card:hover { transform: translateY(-3px); box-shadow: 0 10px 24px rgba(75, 31, 120, .15); }
.card img { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; background: var(--purple-soft); display: block; }
.card-body { padding: 12px 14px 14px; }
.card-date {
  display: inline-block;
  background: var(--yellow-soft);
  color: var(--purple-dark);
  font-weight: 600;
  font-size: 0.85rem;
  border-radius: 6px;
  padding: 0 8px;
}
.card-date.special { background: var(--purple); color: var(--yellow); }
.others { margin-top: 40px; padding-top: 8px; border-top: 2px dashed var(--line); }
.card-title { margin: 6px 0 4px; font-weight: 600; font-size: 1.05rem; line-height: 1.4; }
.card-meta { color: var(--muted); font-size: 0.9rem; }

.btn-more {
  display: block;
  margin: 28px auto 0;
  font: inherit;
  font-weight: 600;
  padding: 10px 32px;
  border-radius: 999px;
  border: 2px solid var(--purple);
  background: #fff;
  color: var(--purple);
  cursor: pointer;
}
.btn-more:hover { background: var(--purple); color: #fff; }

/* ===== Album page ===== */
.back { display: inline-block; margin-bottom: 12px; text-decoration: none; font-weight: 600; }
.album-cover {
  width: 100%;
  max-height: 520px;
  object-fit: cover;
  border-radius: var(--radius);
  background: var(--purple-soft);
  display: block;
  cursor: zoom-in;
}
.album-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 20px;
  margin: 18px 0 8px;
}
.album-date { margin: 0; color: var(--purple); font-weight: 600; }
.album-head h1 { margin: 2px 0 0; font-size: 1.7rem; line-height: 1.35; }
.album-head .tags { margin-top: 10px; }
.btn-download {
  flex-shrink: 0;
  background: var(--yellow);
  color: var(--purple-dark);
  font-weight: 600;
  text-decoration: none;
  padding: 12px 22px;
  border-radius: 12px;
  box-shadow: 0 4px 0 #c9a200;
  text-align: center;
}
.btn-download:hover { filter: brightness(1.05); }
.btn-download:active { transform: translateY(2px); box-shadow: 0 2px 0 #c9a200; }

.section-title { font-size: 1.2rem; margin: 28px 0 12px; }
.previews {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 10px;
}
.previews button { padding: 0; border: 0; background: none; cursor: zoom-in; border-radius: 10px; overflow: hidden; }
.previews img { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; display: block; background: var(--purple-soft); transition: transform .2s; }
.previews button:hover img { transform: scale(1.04); }

/* ===== Lightbox ===== */
.lightbox {
  width: 100vw;
  height: 100vh;
  max-width: none;
  max-height: none;
  margin: 0;
  padding: 0;
  border: 0;
  background: rgba(20, 8, 32, .94);
}
.lightbox::backdrop { background: rgba(20, 8, 32, .94); }
.lightbox[open] { display: flex; align-items: center; justify-content: center; }
.lightbox img { max-width: 92vw; max-height: 88vh; object-fit: contain; }
.lb-btn {
  position: absolute;
  border: 0;
  background: rgba(255, 255, 255, .15);
  color: #fff;
  font-size: 2rem;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  cursor: pointer;
  line-height: 1;
}
.lb-btn:hover { background: rgba(255, 255, 255, .3); }
.lb-close { top: 16px; right: 16px; font-size: 1.4rem; }
.lb-prev { left: 16px; top: 50%; transform: translateY(-50%); }
.lb-next { right: 16px; top: 50%; transform: translateY(-50%); }

/* ===== Mobile ===== */
@media (max-width: 760px) {
  .filters { grid-template-columns: repeat(3, 1fr); }
  .filters input, .filters button { grid-column: 1 / -1; }
  .album-head { flex-direction: column; }
  .btn-download { width: 100%; }
  .album-head h1 { font-size: 1.4rem; }
  .previews { grid-template-columns: repeat(2, 1fr); }
  .grid { grid-template-columns: 1fr; }
  .lb-prev, .lb-next { top: auto; bottom: 20px; transform: none; }
}

/* ===== Tags at bottom ===== */
.tag-section { margin-top: 40px; padding-top: 8px; border-top: 2px dashed var(--line); }
.tag-section .tags { margin-top: 0; }
.count .tag { margin-left: 6px; vertical-align: middle; }

/* ===== Footer ===== */
.site-footer { text-align: center; padding: 8px 16px 28px; font-size: 0.85rem; }
.site-footer a { color: var(--muted); text-decoration: none; }
.site-footer a:hover { color: var(--purple); text-decoration: underline; }

/* ===== Admin ===== */
.btn-primary {
  font: inherit;
  font-weight: 600;
  background: var(--purple);
  color: #fff;
  border: 0;
  border-radius: 10px;
  padding: 8px 18px;
  cursor: pointer;
}
.btn-primary:hover { background: var(--purple-dark); }
.btn-primary:disabled { opacity: .6; cursor: wait; }
.btn-danger {
  font: inherit;
  font-size: 0.85rem;
  background: #fff;
  color: #c62828;
  border: 1px solid #f1b5b5;
  border-radius: 10px;
  padding: 4px 14px;
  cursor: pointer;
}
.btn-danger:hover { background: #fdecea; }
.btn-danger:disabled { opacity: .5; cursor: wait; }
.admin-heading { font-size: 1.5rem; margin: 0 0 16px; }
.admin-login {
  display: grid;
  gap: 10px;
  max-width: 360px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
}
.admin-login input, .admin-tools input[type="search"] {
  font: inherit;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0 12px;
}
.admin-msg { margin: 0; color: var(--purple); font-size: 0.9rem; }
.admin-tools { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; }
.admin-tools input[type="search"] { flex: 1; min-width: 200px; }
.admin-tools .btn-ghost { font: inherit; min-height: 44px; border: 1px solid var(--line); border-radius: 10px; padding: 0 14px; background: #fff; }
.admin-hint { color: var(--muted); font-size: 0.9rem; margin: 12px 0 0; }
.admin-list { display: grid; gap: 10px; }
.admin-row {
  display: grid;
  grid-template-columns: 96px 1fr auto auto;
  gap: 14px;
  align-items: center;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px;
}
.admin-row img { width: 96px; height: 72px; object-fit: cover; border-radius: 8px; background: var(--purple-soft); }
.admin-row.is-hidden { background: #f4f2f6; }
.admin-row.is-hidden img { opacity: .4; }
.admin-info { min-width: 0; }
.admin-date { font-size: 0.85rem; color: var(--purple); font-weight: 600; }
.admin-title {
  font: inherit;
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px;
  margin: 4px 0 2px;
}
.admin-title:focus { outline: 2px solid var(--purple); outline-offset: 1px; }
.admin-orig { font-size: 0.8rem; color: var(--muted); overflow-wrap: anywhere; }
.admin-hide { white-space: nowrap; cursor: pointer; }
.admin-actions { display: grid; gap: 4px; justify-items: center; min-width: 90px; }

@media (max-width: 760px) {
  .admin-row { grid-template-columns: 72px 1fr; }
  .admin-row img { width: 72px; height: 54px; }
  .admin-hide, .admin-actions { grid-column: 2; }
  .admin-actions { justify-items: start; grid-auto-flow: column; align-items: center; }
}

/* ===== Tabs ===== */
.tabs { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-bottom: 14px; border-bottom: 2px solid var(--line); }
.tabs [data-tab] {
  font: inherit; font-weight: 600; background: none; border: 0; color: var(--muted);
  padding: 8px 14px; border-bottom: 3px solid transparent; margin-bottom: -2px; cursor: pointer;
}
.tabs [data-tab].active { color: var(--purple); border-bottom-color: var(--purple); }
.tabs .tabs-end { margin-left: auto; font: inherit; min-height: 36px; border: 1px solid var(--line); border-radius: 10px; padding: 0 14px; background: #fff; }

/* ===== Admin: teacher accounts ===== */
.chip { display: inline-block; font-size: 0.78rem; border-radius: 999px; padding: 0 8px; background: #eee; color: var(--muted); margin-right: 4px; }
.chip.ok { background: #e8f5e9; color: #2e7d32; }
.chip.no { background: #fdecea; color: #c62828; }
.teacher-row {
  display: grid;
  grid-template-columns: minmax(160px, 1fr) 180px auto auto;
  gap: 10px;
  align-items: center;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px;
}
.teacher-user { font: inherit; min-height: 40px; border: 1px solid var(--line); border-radius: 8px; padding: 0 10px; min-width: 0; }
.teacher-row .btn-ghost { font: inherit; min-height: 40px; border: 1px solid var(--line); border-radius: 10px; padding: 0 12px; background: #fff; }
.teacher-msg { grid-column: 1 / -1; }
.teacher-msg:empty { display: none; }
code.pw { font-size: 1.15rem; background: var(--yellow-soft); padding: 2px 8px; border-radius: 6px; letter-spacing: 1px; }

/* ===== ภาพของฉัน ===== */
#meLogin { margin: 10px auto; }
#meLogin .admin-heading { margin-bottom: 4px; }
.consent { max-width: 760px; margin: 0 auto; }
.consent-text { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 6px 18px; line-height: 1.8; }
.consent-text li { margin-bottom: 6px; }
.consent-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 14px; }
.consent-actions .btn-ghost, .me-menu .btn-ghost { font: inherit; min-height: 42px; border: 1px solid var(--line); border-radius: 10px; padding: 0 14px; background: #fff; }
.consent-actions .btn-primary { min-height: 42px; padding: 0 28px; }
.me-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; flex-wrap: wrap; }
.me-head .admin-heading { margin-bottom: 0; }
.me-head .admin-hint { margin-top: 2px; }
.me-menu { display: flex; gap: 8px; flex-wrap: wrap; }
.me-album { margin-bottom: 8px; }
.me-album .section-title a { color: inherit; text-decoration: none; }
.me-album .section-title a:hover { color: var(--purple); text-decoration: underline; }
.me-album .section-title small { font-family: 'Sarabun', sans-serif; font-weight: 400; font-size: 0.85rem; color: var(--muted); margin-left: 6px; }
.me-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 10px; }
.me-photo { background: var(--card); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.me-open { display: block; width: 100%; padding: 0; border: 0; background: var(--purple-soft); cursor: zoom-in; }
.me-open img { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; display: block; }
.me-actions { display: flex; gap: 6px; padding: 6px; }
.me-actions > * { flex: 1; text-align: center; text-decoration: none; padding: 6px 4px; font-size: 0.85rem; }

@media (max-width: 760px) {
  .nav-me { padding: 6px 12px; font-size: 0.9rem; }
  .brand strong { font-size: 1.1rem; }
  .brand img { height: 46px; }
  .teacher-row { grid-template-columns: 1fr auto; }
  .teacher-row .teacher-name { grid-column: 1 / -1; }
  .teacher-row .teacher-pw { grid-column: 1 / -1; }
  .me-grid { grid-template-columns: repeat(2, 1fr); }
}

/* ===== แอดมินลบภาพในหน้าอัลบั้ม ===== */
.admin-note { background: #fff8e1; border: 1px solid #f5d77a; border-radius: 10px; padding: 8px 12px; margin: 0 0 12px; font-size: .9rem; }
.cover-wrap, .preview { position: relative; }
.cover-wrap { min-height: 56px; } /* ปุ่มลบไม่ทับปุ่มดาวน์โหลดตอนภาพปกยังโหลดไม่เสร็จ */
.preview > button[data-index] { width: 100%; }
.cover-wrap .img-del, .previews .img-del {
  position: absolute; top: 8px; right: 8px;
  width: 40px; height: 40px; border-radius: 50%;
  border: 1px solid #f1b5b5; background: #fff; color: #c62828;
  font-size: 1.1rem; cursor: pointer; box-shadow: 0 1px 4px rgba(0,0,0,.25);
}
.cover-wrap .img-del:hover, .previews .img-del:hover { background: #fdecea; }
.cover-wrap .img-del:disabled, .previews .img-del:disabled { opacity: .5; cursor: wait; }
.admin-actions .admin-view { font: inherit; font-size: .85rem; border: 1px solid var(--line); border-radius: 10px; padding: 6px 12px; background: #fff; text-decoration: none; }

/* ===== แอดมิน: สถิติ ===== */
.stat-tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; }
.stat-tile { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 16px; }
.stat-label { color: var(--muted); font-size: .9rem; }
.stat-value { font-family: 'Prompt', sans-serif; font-size: 2rem; font-weight: 600; color: var(--purple); line-height: 1.2; }
.stat-sub { color: var(--muted); font-size: .85rem; }
.stats-title { font-size: 1.1rem; margin: 28px 0 10px; }
.bars { display: flex; align-items: flex-end; gap: 2px; height: 180px; padding: 8px; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); }
.bar { flex: 1; height: 100%; display: flex; align-items: flex-end; position: relative; outline: none; }
.bar i { display: block; width: 100%; min-height: 1px; background: var(--purple); border-radius: 4px 4px 0 0; }
.bar:hover i, .bar:focus i { background: var(--purple-dark); }
.bar:hover::after, .bar:focus::after {
  content: attr(data-tip); position: absolute; bottom: calc(100% + 6px); left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff; font-size: .8rem; padding: 4px 8px; border-radius: 6px; white-space: nowrap; z-index: 2; pointer-events: none;
}
.bar:first-child:hover::after, .bar:first-child:focus::after { left: 0; transform: none; }
.bar:last-child:hover::after, .bar:last-child:focus::after { left: auto; right: 0; transform: none; }
.bars-axis { display: flex; justify-content: space-between; color: var(--muted); font-size: .8rem; margin-top: 4px; }
.stats-table { width: 100%; border-collapse: collapse; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.stats-table th, .stats-table td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.stats-table th { color: var(--muted); font-weight: 600; font-size: .85rem; }
.stats-table .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.stats-table a { color: var(--ink); }
.rank-bar { height: 4px; background: var(--purple-soft); border-radius: 2px; margin-top: 4px; }
.rank-bar i { display: block; height: 100%; background: var(--purple); border-radius: 2px; }
.stats-cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 0 24px; }
.term-list { margin: 0; padding: 0; list-style: none; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); }
.term-list li { display: flex; justify-content: space-between; gap: 12px; padding: 6px 12px; border-bottom: 1px solid var(--line); }
.term-list li:last-child { border-bottom: 0; }
.term-list b { font-variant-numeric: tabular-nums; }
#adminStatsTab select { font: inherit; min-height: 44px; border: 1px solid var(--line); border-radius: 10px; padding: 0 12px; background: #fff; }
