* { box-sizing: border-box; margin: 0; padding: 0; }

/* ให้ attribute hidden ชนะเสมอ (กัน element ที่ตั้ง display:flex/grid โชว์ทั้งที่ควรซ่อน) */
[hidden] { display: none !important; }

:root {
  --accent: #1f7a44;
  --bg: #ffffff;
  --text: #333;
  --muted: #9aa0a6;
}

html, body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Noto Sans Thai', 'Segoe UI', Tahoma, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* ---------- Header ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 16px;
  border-bottom: 1px solid #f0f0f0;
}

.gallery-title {
  font-family: 'Sriracha', 'Noto Sans Thai', cursive;
  font-size: 24px;
  font-weight: 400;
  color: #2b2b2b;
  line-height: 1.3;
  /* ให้ชื่อยืดเต็มพื้นที่ที่เหลือและตัด ... ถ้ายาวเกิน (กันทับไอคอน) */
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.header-actions {
  flex: 0 0 auto;
  display: flex;
  gap: 4px;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border: none;
  background: transparent;
  color: #5f6368;
  border-radius: 50%;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease;
}
.icon-btn:hover { background: #f1f3f4; }

/* มือถือ: ย่อไอคอน + ชื่อ ให้ทุกปุ่มพอดีและไม่ทับชื่อ */
@media (max-width: 480px) {
  .app-header { padding: 10px 10px; gap: 6px; }
  .gallery-title { font-size: 18px; }
  .icon-btn { width: 34px; height: 34px; }
  .icon-btn svg { width: 19px; height: 19px; }
  .header-actions { gap: 2px; }
}

/* ---------- States ---------- */
.state-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 80px 20px;
  color: var(--muted);
}

.spinner {
  width: 42px;
  height: 42px;
  border: 4px solid #e8eaed;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Grid ---------- */
.grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 18px;
  padding: 24px;
  max-width: 1100px;
  margin: 0 auto;
}

@media (max-width: 900px) { .grid { grid-template-columns: repeat(3, 1fr); gap: 14px; padding: 16px; } }
@media (max-width: 640px) { .grid { grid-template-columns: repeat(2, 1fr); gap: 12px; padding: 12px; } }

.grid-item {
  position: relative;
  cursor: pointer;
  border-radius: 6px;
  overflow: hidden;
  background: #f4f5f7;
  aspect-ratio: 1 / 1;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.grid-item:hover { transform: translateY(-2px); box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); }

@supports not (aspect-ratio: 1 / 1) {
  .grid-item { height: 0; padding-bottom: 100%; }
}

.grid-item img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

/* ไอคอนมุมบนขวา: มีวิดีโอ/หลายไฟล์ */
.badge-corner {
  position: absolute;
  top: 6px;
  right: 6px;
  background: rgba(0, 0, 0, 0.0);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.6));
}

/* play overlay สำหรับรายการที่มีวิดีโอ */
.play-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
.play-overlay svg {
  filter: drop-shadow(0 2px 6px rgba(0, 0, 0, 0.5));
  opacity: 0.92;
}

/* ---------- Lightbox ---------- */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(0, 0, 0, 0.92);
  display: flex;
  align-items: center;
  justify-content: center;
}

.lightbox-content {
  position: relative;
  z-index: 1;
  max-width: 92vw;
  max-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 14px;
  overflow-y: auto;
  /* เว้นบน (ปุ่ม X) และล่าง (toolbar) ไม่ให้เนื้อหาชนปุ่ม */
  padding: 64px 0 84px;
  -webkit-overflow-scrolling: touch;
}

.lightbox-content img,
.lightbox-content video {
  max-width: 92vw;
  max-height: 72vh;
  width: auto;
  height: auto;
  border-radius: 6px;
  background: #000;
  object-fit: contain;
}

.lb-error {
  color: #fff;
  font-size: 16px;
  padding: 40px 20px;
  opacity: 0.85;
}

.lightbox-close {
  position: absolute;
  top: 12px;
  right: 14px;
  z-index: 10;
  width: 44px;
  height: 44px;
  font-size: 30px;
  line-height: 1;
  color: #fff;
  background: rgba(0, 0, 0, 0.55);
  border: none;
  border-radius: 50%;
  cursor: pointer;
  opacity: 0.95;
  display: flex;
  align-items: center;
  justify-content: center;
}
.lightbox-close:hover { opacity: 1; background: rgba(0, 0, 0, 0.75); }

.lightbox-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  z-index: 10;
  font-size: 30px;
  color: #fff;
  background: rgba(0, 0, 0, 0.55);
  border: none;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.lightbox-nav:hover { background: rgba(0, 0, 0, 0.75); }
.lightbox-prev { left: 16px; }
.lightbox-next { right: 16px; }

.lightbox-toolbar {
  position: absolute;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
  display: flex;
  gap: 10px;
}
.lb-tool {
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  text-decoration: none;
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 14px;
  transition: background 0.15s ease;
}
.lb-tool:hover { background: rgba(255, 255, 255, 0.28); }

@media (max-width: 640px) {
  .lightbox-nav { width: 40px; height: 40px; font-size: 24px; }
  .lightbox-prev { left: 6px; }
  .lightbox-next { right: 6px; }
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #323232;
  color: #fff;
  padding: 11px 20px;
  border-radius: 24px;
  font-size: 14px;
  z-index: 200;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
  animation: toastIn 0.2s ease;
}
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 10px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* ---------- Lightbox toolbar icon buttons ---------- */
.lb-tool-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 8px 12px;
  border: none;
  cursor: pointer;
  line-height: 0;
}

/* ---------- Select mode: toolbar ---------- */
.select-bar {
  position: sticky;
  top: 65px;
  z-index: 9;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 10px 16px;
  background: #f8f9fa;
  border-bottom: 1px solid #eceff1;
}
/* ---------- Download mode menu (header) ---------- */
.download-menu-wrap { position: relative; display: inline-flex; }
.download-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 30;
  background: #fff;
  border: 1px solid #e6e8eb;
  border-radius: 10px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
  overflow: hidden;
  min-width: 190px;
}
.download-menu-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 11px 16px;
  border: none;
  background: #fff;
  color: #333;
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
}
.download-menu-item:hover { background: #f1f3f4; }
.download-menu-item + .download-menu-item { border-top: 1px solid #f0f0f0; }

.select-info { display: flex; flex-direction: column; gap: 2px; }
.select-count { font-size: 14px; color: #333; font-weight: 600; }
.select-hint { font-size: 12px; color: #9aa0a6; }
.select-bar-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ปุ่มเลือกใน header: แสดงสถานะกำลังเปิดโหมดเลือก */
.icon-btn.active {
  background: var(--accent);
  color: #fff;
}
.icon-btn.active:hover { background: #1a6a3b; }

.select-btn {
  border: 1px solid #d7dbdf;
  background: #fff;
  color: #333;
  padding: 7px 14px;
  border-radius: 20px;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.select-btn:hover { background: #f1f3f4; }
.select-btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.select-btn.primary:hover { background: #1a6a3b; }
.select-btn:disabled { opacity: 0.5; cursor: default; }

/* ---------- Grid selection state ---------- */
.grid-item.selectable { cursor: pointer; }

.grid-select-check {
  position: absolute;
  top: 8px;
  left: 8px;
  z-index: 3;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.85);
  border: 2px solid #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  display: none;
  align-items: center;
  justify-content: center;
  color: transparent;
}
body.select-mode .grid-select-check { display: inline-flex; }

.grid-item.selected .grid-select-check {
  background: var(--accent);
  border-color: #fff;
  color: #fff;
}
.grid-item.selected::after {
  content: '';
  position: absolute;
  inset: 0;
  border: 3px solid var(--accent);
  border-radius: 6px;
  pointer-events: none;
}

/* ---------- QR modal ---------- */
.qr-modal {
  position: fixed;
  inset: 0;
  z-index: 150;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.qr-card {
  position: relative;
  background: #fff;
  border-radius: 14px;
  padding: 26px 24px 20px;
  width: 320px;
  max-width: 100%;
  text-align: center;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
}
.qr-close {
  position: absolute;
  top: 8px;
  right: 10px;
  width: 34px;
  height: 34px;
  border: none;
  background: transparent;
  font-size: 26px;
  line-height: 1;
  color: #777;
  cursor: pointer;
  border-radius: 50%;
}
.qr-close:hover { background: #f1f3f4; }
.qr-title { font-size: 18px; margin-bottom: 16px; color: #2b2b2b; }
.qr-box {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 200px;
  margin-bottom: 10px;
}
.qr-box img, .qr-box canvas { display: block; }
.qr-hint { font-size: 12px; color: #888; margin-bottom: 14px; }

.qr-link-row { display: flex; gap: 6px; }
.qr-link-row input {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 13px;
  color: #333;
}
.qr-link-row button {
  padding: 8px 14px;
  border: none;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  font-size: 13px;
}
.qr-link-row button:hover { background: #1a6a3b; }
