/* ==========================================================================
   1. UI การ์ดแนวตั้ง (Grid Layout - หน้ารวมทั้งหมด)
   ========================================================================== */
.rgf-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
  margin-bottom: 30px;
  font-family: sans-serif;
}

.rgf-card {
  border: 1px solid #eaeaea;
  border-radius: 12px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: #fff;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.02);
  transition:
    transform 0.2s,
    box-shadow 0.2s;
}

.rgf-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 15px rgba(0, 0, 0, 0.06);
}

.rgf-img-box {
  width: 100%;
  height: 160px;
  background: #f5f5f5;
  overflow: hidden;
  position: relative;
}

.rgf-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center top !important;
}

.rgf-no-img {
  padding: 60px 0;
  text-align: center;
  color: #ccc;
  font-size: 14px;
}

.rgf-content {
  padding: 15px;
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.rgf-title {
  font-size: 16px;
  font-weight: bold;
  margin: 0 0 10px 0;
  color: #333;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.rgf-meta {
  font-size: 13px;
  color: #666;
  margin-bottom: 8px;
}

.rgf-btn {
  display: block;
  text-align: center;
  background: #0073aa;
  color: #fff !important;
  padding: 10px;
  text-decoration: none !important;
  border-radius: 6px;
  font-size: 14px;
  margin-top: 15px;
  font-weight: bold;
  transition: background 0.2s;
}

.rgf-btn:hover {
  background: #005177;
}

.rgf-grid-sync-footer {
  width: 100% !important;
  display: block !important;
  clear: both !important;
  text-align: center !important; /* จัดให้อยู่กึ่งกลางหน้าจอ */
  margin-top: 25px; /* เว้นระยะห่างจากขอบกล่องการ์ดด้านบน */
  margin-bottom: 15px; /* เว้นระยะก่อนถึงปุ่มเปลี่ยนหน้า (Pagination) */
}

/* 📝 สไตล์ตัวหนังสือสีขาวนวลโปร่งแสง 50% สไตล์ดาร์กโหมด */
.rgf-grid-sync-footer span {
  font-size: 12px !important;
  color: rgba(255, 255, 255, 0.5) !important; /* จางเนียนเข้ากับพื้นหลังเข้ม */
  font-weight: normal !important;
  letter-spacing: 0.5px;
}

.rgf-pagination {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-top: 20px;
}

.rgf-page-link {
  padding: 8px 14px;
  border: 1px solid #ddd;
  text-decoration: none;
  color: #333;
  border-radius: 4px;
}

.rgf-page-link.active {
  background: #0073aa;
  color: #fff;
  border-color: #0073aa;
}

/* ==========================================================================
   2. UI การ์ดแนวนอนขนาดเล็ก (Horizontal Layout - หน้าแรกของเว็บ)
   ========================================================================== */

/* 🎯 ประกาศให้กล่องนอกสุดทำหน้าที่วัดขนาดความกว้างของบล็อกที่ไปวาง */

.rgf-hz-wrapper,
.rgf-horizontal-container {
  container-type: inline-size;
  container-name: hzcardcontainer;
  width: 100%;
  box-sizing: border-box;
}

/* คอนเทนเนอร์หลักแนวนอน */
.rgf-horizontal-container {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
  max-width: 900px;
  margin: 0 auto 20px auto;
  font-family: sans-serif;
}

/* ตัวการ์ดแนวนอนหลัก (เมื่อแสดงผลในบล็อกขนาดกว้างปกติ) */
.rgf-hz-card {
  display: flex;
  align-items: center;
  background: #ffffff;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 10px 15px;
  text-decoration: none !important;
  color: #333333 !important;
  transition: all 0.2s ease-in-out;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.01);
}

.rgf-hz-card:hover {
  transform: translateX(4px);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
  border-color: #0073aa;
}

/* ส่วนที่ 1: กล่องภาพขนาดเล็ก */
.rgf-hz-img-box {
  width: 150px;
  height: 85px;
  border-radius: 4px;
  overflow: hidden;
  background: #f5f5f5;
  flex-shrink: 0;
  margin-right: 15px;
  position: relative;
}

.rgf-hz-img-box img {
  width: 100% !important;
  height: 100% !important;
  object-fit: cover !important;
  border-radius: 6px;
}

.rgf-hz-no-img {
  font-size: 10px;
  color: #999;
  text-align: center;
  line-height: 50px;
}

/* ส่วนที่ 2: ชื่อทุนและหน่วยงาน */
.rgf-hz-details {
  flex-grow: 1;
  min-width: 0;
  padding-right: 15px;
  text-align: left !important;
}

.rgf-hz-title {
  font-size: 14px;
  font-weight: 600;
  color: #222;
  margin-bottom: 2px;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  display: -webkit-box;
  overflow: hidden;
  text-overflow: ellipsis; /* ตัดท้ายเป็น ... */
}

.rgf-hz-agency {
  font-size: 12px;
  color: #666;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ส่วนที่ 3: เวลาปิด + Countdown */
.rgf-hz-timebox {
  text-align: right;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
}

.rgf-date-container {
  display: flex;
  justify-content: space-between;
  /* align-items: center;  */
  width: 100%;
  gap: 10px;
}

/* สไตล์เดิมของคุณ */
.rgf-hz-date {
  font-size: 11px;
  color: #cf1322;
  font-weight: bold;
}

.rgf-approval-date {
  display: inline-block;
  font-size: 11px !important;
  color: #666666 !important;
  -webkit-text-size-adjust: none; /* ป้องกัน iOS Safari ปรับขนาดฟอนต์เอง */
}

/* ป้ายนับถอยหลังในเวอร์ชันแนวนอน */
.rgf-hz-badge {
  padding: 3px 8px;
  border-radius: 4px;
  font-size: 11px;
}

/* ตัวเลขแจ้งเตือนทุนคงเหลือ +X */
.rgf-hz-more-wrapper {
  display: flex !important;
  justify-content: space-between !important; /* ดันชิ้นนึงไปซ้ายสุด อีกชิ้นไปขวาสุด */
  /* align-items: center !important; จัดให้อยู่กึ่งกลางในแนวนอนเสมอกัน */
  width: 100%;
  /* margin-top: 15px; */
  padding: 0 4px; /* เว้นระยะขอบสัดส่วนการ์ดเบาๆ */
}

.rgf-hz-sync-text {
  font-size: 11.5px !important; /* ตัวหนังสือขนาดเล็กตามบรีฟ */
  color: rgba(255, 255, 255, 0.5) !important;
  font-weight: normal !important;
  text-align: left !important;
}

/* 📥 สไตล์กล่องควบคุมปุ่มฝั่งขวา */
.rgf-hz-more-btn-box {
  text-align: right !important;
}

.rgf-hz-more-badge {
  background: #f0f5ff;
  color: #2f54eb;
  border: 1px solid #adc6ff;
  padding: 5px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: bold;
  display: inline-block;
  transition: all 0.2s ease-in-out; /* เพิ่มความนุ่มนวลตอนเปลี่ยนสี */
  cursor: pointer; /* เปลี่ยนเมาส์เป็นรูปมือเมื่อชี้ */
}
.rgf-hz-more-badge:hover {
  background: #2f54eb;
  color: #ffffff;
  border-color: #2f54eb;
  box-shadow: 0 4px 8px rgba(47, 84, 235, 0.15);
}

.rgf-hz-top-row {
  display: flex;
  flex-direction: row;
  align-items: center; /* จัดให้รูปกับข้อความอยู่กึ่งกลางแนวนอนขนานกันบนจอใหญ่ */
  width: 100%;
  gap: 20px;
}
.rgf-hz-details-text {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
}

/* ==========================================================================
   3. RESPONSIVE BY BLOCK SIZE (ระบบ Container Queries อิงตามขนาดของบล็อก)
   ========================================================================== */

@container hzcardcontainer (max-width: 650px) {
  .rgf-hz-more-wrapper {
    flex-direction: column !important; /* บนมือถือแคบๆ บังคับให้เรียงต่อกันแนวดิ่ง */
    gap: 8px !important; /* เว้นช่องไฟระหว่าข้อความกับปุ่ม */
    align-items: center !important; /* ปรับให้มาอยู่ตรงกลางหน้าจอ */
    text-align: center !important;
  }
  .rgf-hz-sync-text {
    text-align: center !important;
  }

  /* จัดการให้การ์ดเรียง แถวบน-แถวล่าง */
  .rgf-hz-card {
    display: flex !important;
    flex-direction: column !important;
    padding: 12px !important;
    gap: 12px !important;
    width: 100% !important;
    max-width: 100% !important;
    box-sizing: border-box !important;
    overflow: hidden !important; /* กันธาตุภายในล้น */
  }

  /* ล้างค่าดั้งเดิมของจอใหญ่บน .rgf-hz-details ออกเพื่อไม่ให้จัดพื้นที่ซ้อนทับ */

  .rgf-hz-details,
  .rgf-hz-top-row {
    display: flex !important;
    width: 100% !important;
    min-width: 0 !important; /*  จุดสำคัญมาก: ป้องกัน Flexbox ดันขอบ */
    box-sizing: border-box !important;
  }

  .rgf-hz-top-row {
    flex-direction: row !important;
    align-items: flex-start !important;
    gap: 12px !important;
  }

  /* ช่องซ้าย: ขนาดรูปภาพสำหรับมือถือ */

  .rgf-hz-img-box {
    width: 80px !important;
    height: 80px !important;
    flex-shrink: 0 !important;
    margin: 0 !important;
  }

  /* ช่องขวา: บังคับข้อความไหลดิ่ง 3 บรรทัด */

  .rgf-hz-details-text {
    flex: 1 !important;
    min-width: 0 !important; /*  จุดสำคัญมาก: ทำให้ข้อความพับคำแทนการดันขอบ */
    display: flex !important;
    flex-direction: column !important;
    gap: 4px !important;
    overflow: hidden !important;
  }

  /* ขนาดฟอนต์เวอร์ชันจอมือถือ */

  .rgf-hz-title {
    font-size: 13px !important;
    font-weight: bold !important;
    line-height: 1.4 !important;
    margin: 0 !important;
    color: #222 !important;

    /* ตัดคำเมื่อยาวเกิน 3 บรรทัด + รองรับภาษาอังกฤษคำยาว */
    display: -webkit-box !important;
    -webkit-line-clamp: 3 !important;
    -webkit-box-orient: vertical !important;
    overflow: hidden !important;
    word-break: break-word !important;
    overflow-wrap: anywhere !important;
  }

  .rgf-hz-agency,
  .rgf-hz-date,
  .rgf-approval-date {
    word-break: break-word !important;
    overflow-wrap: anywhere !important;
  }

  .rgf-hz-date {
    font-size: 10.5px !important;
    color: #b32b2b !important;
    font-weight: 600 !important;
  }

  .rgf-hz-agency {
    font-size: 11px !important;
    color: #666 !important;
    margin: 0 !important;
  }

  .rgf-approval-date {
    font-size: 10.5px !important;
    color: #666 !important;
  }

  /* แถวที่ 2 บนมือถือ: กล่องเวลานับถอยหลังแผ่เต็มจอ */

  .rgf-hz-timebox {
    width: 100% !important;
    border-top: 1px dashed #eaeaea !important;
    padding-top: 8px !important;
    margin: 0 !important;
  }

  .rgf-hz-timebox .rgf-countdown {
    width: 100% !important;
    box-sizing: border-box !important;
  }

  .rgf-date-container {
    display: flex !important;
    flex-direction: column !important;
    gap: 2px !important;
    margin-top: 4px !important;
  }
}

/* ==========================================================================
   4. SYSTEM COUNTDOWN (ระบบกล่องนับถอยหลังสีแดงแบบ 3 บรรทัด)
   ========================================================================== */
.rgf-countdown {
  background: #fff1f0;
  color: #cf1322;
  border: 1px solid #ffa39e;
  padding: 10px;
  border-radius: 8px;
  text-align: center;
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 2px;
  line-height: 1.2;
}

/* บรรทัดที่ 1: "เหลือเวลาอีก" (ตัวเล็ก สวยงาม) */
.rgf-cd-line1 {
  font-size: 11px;
  font-weight: normal;
  color: #f5222d;
  opacity: 0.8;
}

/* บรรทัดที่ 2: ตัวเลขหน่วยเวลาที่ใหญ่ที่สุด (ตัวใหญ่เบิ้ม ชัดเจน ดึงสายตา) */
.rgf-cd-line2 {
  font-size: 22px;
  font-weight: 800;
  color: #cf1322;
  margin: 2px 0;
}

/* บรรทัดที่ 3: หน่วยเศษเวลาที่เหลือด้านหลัง (ขนาดกลาง-เล็ก สมดุลสายตา) */
.rgf-cd-line3 {
  font-size: 12px;
  font-weight: 600;
  color: #a8071a;
}

/* สไตล์เมื่อเวลานับถอยหลังสิ้นสุดลง */
.rgf-cd-expired {
  font-size: 14px;
  font-weight: bold;
  padding: 15px 0;
}

/* ไม่เหลือทุนที่เปิดรับแล้วใน google sheet*/
.rgf-empty-grants {
  background: #ffffff;
  border: 1px dashed #cccccc;
  border-radius: 8px;
  padding: 40px 20px;
  text-align: center;
  max-width: 600px;
  margin: 30px auto;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.rgf-empty-icon {
  font-size: 40px;
  margin-bottom: 15px;
  animation: rgfPulse 2s infinite; /* มีลูกเล่นไอคอนขยับดึงสายตา */
}

.rgf-empty-grants h3 {
  font-size: 18px;
  color: #333333;
  margin: 0 0 8px 0;
  font-weight: bold;
}

.rgf-empty-grants p {
  font-size: 14px;
  color: #666666;
  margin: 0;
  line-height: 1.5;
}

@keyframes rgfPulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.1);
  }
  100% {
    transform: scale(1);
  }
}
