* {
  box-sizing: border-box;
  font-family: "Noto Sans", Poppins, -apple-system, BlinkMacSystemFont,
    "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell,
    "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
}

body {
  margin: 0;
  background: #e6e6e6;
  /* ลบ overflow-x: hidden ออกไป */
}

/* ------------------ MAIN WRAPPER ------------------ */
.page {
  max-width: 1680px;
  width: 100%;
  margin: 0 auto;
  background: #ffffff;
  min-height: 950px;
  border-left: 1px solid #ccc;
  border-right: 1px solid #ccc;
  /* ✅ ให้เลื่อนแนวนอนได้ถ้ากราฟกว้าง แทนที่จะถูกตัด */
  overflow-x: auto;
  border: 1px solid #ccc;
}


/* ================= HEADER ================= */
.header {
  background: #2f6b2f;
  color: #fff;
  height: 70px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding: 0 10px;
  position: relative;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 8px;
  /* ระยะห่างระหว่างโลโก้สองอัน */
}

.logo-box {
  width: 48px;
  height: 48px;
  border-radius: 6px;
  background: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
}

.logo-box img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  /* ให้รูปไม่โดนยืด */
  display: block;
}




.header-title {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  text-align: center;
  font-weight: 700;
  font-size: 20px;
  pointer-events: none;
}

.header-right {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.header-link {
  color: #fff !important;
  text-decoration: none;
  font-size: 16px;
  background: rgba(255, 255, 255, 0.2);
  padding: 10px 24px;
  border-radius: 8px;
  border: 1.5px solid rgba(255, 255, 255, 0.5);
  transition: all 0.2s ease;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 220px;
}

.header-link:hover {
  background: rgba(255, 255, 255, 0.35);
  border-color: #ffffff;
  transform: scale(1.02);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.header-right-text {
  font-size: 12px;
  line-height: 1.2;
  text-align: right;
}

/* =============== FILTER BAR =============== */
.filter-bar {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  padding: 8px 16px;
  background: #f5f5f5;
  border-bottom: 1px solid #d0d0d0;
}

.filter-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.filter-label {
  background: #2f6b2f;
  color: #fff;
  padding: 4px 12px;
  border-radius: 3px;
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
}

.filter-select {
  flex: 1;
  position: relative;
}

.filter-select select {
  width: 100%;
  padding: 5px 30px 5px 8px;
  border: 1px solid #b3b3b3;
  border-radius: 3px;
  font-size: 14px;
  appearance: none;
}

.filter-select::after {
  content: "▼";
  font-size: 10px;
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  pointer-events: none;
  color: #555;
}

/* ============= MAIN LAYOUT (MID) ============= */
.main {
  display: grid;
  grid-template-columns: 370px 1fr;
  gap: 10px;
  padding: 8px 16px 0;
}

.panel-title {
  background: #2f6b2f;
  color: #fff;
  font-size: 14px;
  padding: 4px 8px;
  font-weight: 600;
}

.panel {
  border: 1px solid #b3b3b3;
  background: #fff;
}

/* LEFT MAP PANEL */
.map-panel {
  display: flex;
  flex-direction: column;
}

.map-body {
  flex: 1 1 auto;
  min-height: 0;
  height: auto;
  /* กันชนกับ calc เดิม */
  position: relative;
  background: #6c4a3c;
  margin: 4px;
  border-radius: 2px;
  overflow: hidden;
}

.map-placeholder {
  position: absolute;
  top: 10px;
  left: 10px;
  right: 10px;
  bottom: 10px;
  background: #7c5a45;
  border: 1px solid #fff;
  color: #fff;
  font-size: 13px;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
}

#map {
  position: absolute;
  top: 8px;
  left: 8px;
  right: 8px;
  bottom: 8px;
  border-radius: 4px;
}

/* RIGHT CHART (RAIN) */
.rain-panel {
  height: 420px;
  display: flex;
  flex-direction: column;
  /* แก้ตรงนี้: ให้ยืดเต็มแทนที่จะจัดกลาง */
  align-items: stretch;
  /* หรือจะลบทั้งบรรทัดนี้ก็ได้ */
}

.chart-legend {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 4px 8px;
  font-size: 12px;
  border-bottom: 1px solid #ccc;
  flex-wrap: wrap;
  /* เผื่อจอแคบตัวหนังสือไม่ล้น */
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 4px;
}

.legend-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #0078d7;
}

/* ===== Mobile Rain Split (hidden on desktop) ===== */
.mobile-rain-split {
  display: none;
}

.legend-rect {
  width: 14px;
  height: 10px;
  background: #92c5de;
}

.rain-chart-body {
  flex: 1 1 auto;
  min-height: 0;
  /* ให้มี margin ซ้ายขวานิดหน่อย */
  /* margin: 4px 6px 0; */
  border: 1px solid #d0d0d0;
  padding: 6px 0;
  /* ใช้เต็มความกว้าง panel ไปเลย */
  width: 100%;
  /* ลบ max-width: 1100px; ทิ้ง */
  position: relative;
  overflow: hidden;
  /* ถ้า canvas กว้างเกินให้ตัดในกรอบ */
}

.rain-chart-body canvas {
  width: 100% !important;
  max-width: 100% !important;
  /* กันไม่ให้กว้างกว่า container */
}

/* ========== LOADING OVERLAY / SKELETON ========== */

.chart-loading {
  position: absolute;
  inset: 0;
  /* ทับเต็มกรอบ chart-body */
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: rgba(255, 255, 255, 0.8);
  z-index: 10;
  font-size: 12px;
  color: #555;
}

.chart-loading.hidden {
  display: none;
}

.loading-text {
  font-size: 12px;
  color: #555;
}

/* แท่ง skeleton ใช้ตอนกราฟแท่ง */
.skeleton-bars {
  display: flex;
  gap: 4px;
  align-items: flex-end;
}

.skeleton-bar {
  width: 10px;
  height: 40px;
  border-radius: 3px;
  background: linear-gradient(90deg,
      #e0e0e0 0%,
      #f5f5f5 50%,
      #e0e0e0 100%);
  background-size: 200% 100%;
  animation: skeleton-shimmer 1.3s infinite;
}

.skeleton-bar:nth-child(2) {
  height: 55px;
}

.skeleton-bar:nth-child(3) {
  height: 35px;
}

.skeleton-bar:nth-child(4) {
  height: 50px;
}

.skeleton-bar:nth-child(5) {
  height: 32px;
}

.skeleton-bar:nth-child(6) {
  height: 45px;
}

/* เส้น skeleton ใช้ตอนกราฟเส้น */
.skeleton-line {
  width: 80%;
  height: 6px;
  border-radius: 999px;
  background: linear-gradient(90deg,
      #e0e0e0 0%,
      #f5f5f5 50%,
      #e0e0e0 100%);
  background-size: 200% 100%;
  animation: skeleton-shimmer 1.3s infinite;
}

/* effect วิ่ง ๆ */
@keyframes skeleton-shimmer {
  0% {
    background-position: 200% 0;
  }

  100% {
    background-position: -200% 0;
  }
}

/* ต้องให้ chart-body เป็น relative เพื่อให้ overlay วางทับได้ */
.rain-chart-body,
.kpi-chart-body {
  position: relative;
}

/* ===== ERROR OVERLAY + RETRY BUTTON ===== */

.chart-error {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: rgba(255, 255, 255, 0.9);
  z-index: 11;
  text-align: center;
}

.chart-error.hidden {
  display: none;
}

.error-text {
  font-size: 12px;
  color: #b00020;
}

.retry-btn {
  padding: 4px 12px;
  font-size: 12px;
  border-radius: 4px;
  border: 1px solid #2f6b2f;
  background: #2f6b2f;
  color: #fff;
  cursor: pointer;
}

.retry-btn:hover {
  background: #275727;
}

/* ========== SMALL ICON BAR (ช่วงรูปข้าว) ========== */
/* แถวรูปข้าวใต้กราฟ */
.icon-bar {
  margin: 0;
  /* ✅ กำหนดให้มี 36 คอลัมน์เท่าจำนวนช่องรูปข้าว */
  display: grid;
  grid-template-columns: repeat(36, 1fr);
  border: 1px solid #b3b3b3;
  height: 42px;
  background: #ffffff;
  box-sizing: border-box;
  width: 100%;
  overflow: hidden;
}

/* Desktop: month-group is transparent to the grid — cells join the 36-col grid directly */
.icon-bar>.month-group {
  display: contents;
}

/* Hide month/dekad labels on desktop */
.icon-bar>.month-group>.month-label,
.icon-bar>.month-group>.dekad-label {
  display: none;
}


.icon-cell {
  border-right: 1px solid #dddddd;
  border-left: 1px solid #dddddd;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.15s, box-shadow 0.15s;
  user-select: none;
  background: #ffffff;
  height: 36px;
  /* fix ความสูงกันดัน */
  line-height: 1;
}

/* .icon-cell:last-child {
  border-right: none;
} */

.icon-cell:hover {
  background: #fffbe6;
}

.icon-cell.active {
  background: #ffe08a;
  box-shadow: inset 0 0 0 2px #d39b00;
}

/* สีช่องรูปข้าวตามระดับผลผลิต */
.icon-cell.level-0 {
  color: #fff;
}

.icon-cell.level-1 {
  color: #000;
}

.icon-cell.level-2 {
  color: #fff;
}

/* ช่องรูปข้าวที่เป็น dekad ปัจจุบัน (จาก API ฝน) ให้กรอบสีฟ้า */
/* 1) วันปัจจุบัน */
.icon-cell.current-dekad {
  background: #5fb8fb;
}

/* 2) วันที่ผู้ใช้เลือก */
/* .icon-cell.active {
  background: #ffe08a;
  box-shadow: inset 0 0 0 2px #d39b00;
} */

/* 3) วันปัจจุบัน + ถูกเลือก */
.icon-cell.current-dekad.active {
  background: #5fb8fb;
  box-shadow: inset 0 0 0 2px #d39b00;
}

/* 4) ไม่ใช่ช่วงปลูก */
.icon-cell.no-crop {
  /* background: #f5f5f5; */
  background: rgba(255, 255, 255, 0.4);
  opacity: 0.4;
}

.icon-cell.no-crop.active {
  background: #ffe08a;
  opacity: 0.4;
}

/* 5) วันนี้ แต่ไม่ปลูก */
.icon-cell.no-crop.current-dekad,
.icon-cell.no-crop.current-dekad.active {
  background: #5fb8fb;
  opacity: 1;
}

/* =========== KPI ROW =========== */
.main-lower .kpi-row {
  margin: 0;
  /* ใช้ padding ของ main-lower แทน */
}

.kpi-row {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  /* ทั้ง 5 ใบกว้างเท่ากัน */
  gap: 6px;
  margin: 6px 16px 0;
}

/* การ์ดสรุปสถานการณ์น้ำ 4 ใบแรก */
.kpi-card {
  border: 1px solid #b3b3b3;
  background: #fff;
  height: auto;

  /* ลด padding บน–ล่างลงให้ดูเต็มกรอบขึ้น */
  padding: 2px 6px 2px;

  display: flex;
  flex-direction: column;
  justify-content: center;
  /* ให้อยู่กลางกรอบแนวตั้ง */
  font-size: 11px;
  min-width: 0;
}

/* หัวข้อการ์ด – ตัด margin ล่างให้แน่นขึ้น */


.kpi-icon {
  width: 22px;
  height: 22px;
  border-radius: 3px;
  background: #ff9900;
}

.kpi-value {
  font-size: 40px;
  font-weight: 700;
  text-align: right;
  color: #e51b24;
  padding-right: 2px;
}

/* แถบช่วงปลูก (การ์ดใบแรก) – ลดช่องว่างด้านบน */
.kpi-period {
  text-align: center;
  font-size: 16px;
  font-weight: 700;
  background: #fce6b2;

  padding: 1px 4px;
  /* เดิม 2px → 1px */
  margin-top: 0;
  /* เดิม 2px → 0 */

  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ===== ปรับแถวสรุปสถานการณ์น้ำให้แน่น และตัวเลขขึ้นข้างข้อความ ===== */

/* ลดช่องว่างรอบ ๆ แถวการ์ดใน panel ขวาล่าง */
.kpi-panel .kpi-row {
  margin: 4px 6px 6px;
  gap: 4px;
  /* 4 ใบแรกแคบลง, ใบขวาสุด (เกณฑ์ผลผลิต) กว้างขึ้น */
  grid-template-columns: 0.9fr 0.95fr 0.95fr 0.9fr 1.2fr;
}


.kpi-title {
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 8px;
}

/* การ์ดที่มีตัวเลข */
.kpi-card-number {
  flex-direction: row;
  align-items: center;
  /* ✅ ให้ title + ตัวเลขอยู่กลางกรอบแนวตั้ง */
}

.kpi-card-number .kpi-title {
  margin-bottom: 0;
}

.kpi-card-number .kpi-value {
  font-size: 22px;
  /* เล็กลงนิดให้ไม่ดันพื้นที่ */
  font-weight: 800;
  text-align: right;
  padding-right: 2px;
  margin-left: auto;
  /* ดันไปชิดขวาการ์ด */
}

/* panel ฝั่งขวาใต้กราฟน้ำฝน */
.kpi-panel {
  border: 1px solid #b3b3b3;
  background: #fff;
  display: flex;
  flex-direction: column;
  overflow: visible;
  /* กันโดนตัดเวลา layout แกว่ง */
}

/* ให้กินความสูงเท่ากับ calendar-panel ในแถวเดียวกัน */
.main-lower .kpi-panel {
  height: 100%;
}

/* ===== KPI PANEL → CHARTS ===== */
.kpi-charts-wrapper {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 10px 6px 10px;
}

.kpi-chart-panel {
  border: 1px solid #b3b3b3;
  background: #fff;
  height: 320px;
  display: flex;
  flex-direction: column;
}

.kpi-chart-body {
  flex: 1;
  margin: 4px 6px 6px;
  /* border: 1px solid #d0d0d0; */
  padding: 6px;
  overflow: hidden;
  /* กันไม่ให้ canvas โผล่เกินกรอบ */
}

.kpi-chart-body canvas {
  width: 100% !important;
  /* ยึดเท่ากรอบเสมอ */
  max-width: 100% !important;
  height: 100%;
}

/* ===== ต้นทุนผลผลิต แบบตามรูปตัวอย่าง ===== */
.cost-card {
  border: 1px solid #b3b3b3;
  background: #fff;
  /* ให้สูงตามเนื้อหาจริง */
  height: auto;
  padding: 4px 6px 3px;
  font-size: 12px;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  min-width: 0;
}


.cost-header {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 2px;
}

.cost-icon {
  width: 22px;
  height: 22px;
  border-radius: 3px;
  background: #ffcc00;
}

.cost-title {
  font-weight: 600;
}

.cost-bar-wrapper {
  position: relative;
  margin: 4px;
  height: 20px;
  border: 1px solid #666;
  display: flex;
  overflow: visible;
}

.cost-bar-seg {
  flex: 1;
}

.cost-green {
  background: #00a651;
}

.cost-yellow {
  background: #f7e08b;
}

.cost-red {
  background: #e84c3d;
}

.cost-label-row {
  display: flex;
  margin-top: 1px;
  font-size: 11px;
  font-weight: 600;
}

.cost-label {
  flex: 1;
  text-align: center;
  /* white-space: nowrap; */
}

/* สามเหลี่ยมตัวชี้ */
.cost-pointer {
  position: absolute;
  top: -8px;
  /* ยื่นลงมาแตะแถบสี */
  left: 15%;
  /* ตำแหน่งเริ่มต้น (JS จะมาเปลี่ยน left ทีหลัง) */
  width: 0;
  height: 0;
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-top: 8px solid #000;
  /* ใช้ border-top แทน → สามเหลี่ยมชี้ลง */
  border-bottom: none;
}


/* =========== BOTTOM CHARTS =========== */
.bottom-row {
  display: grid;
  grid-template-columns: 1.2fr 1fr;
  gap: 10px;
  margin: 10px 16px 16px;
}

.bottom-panel {
  border: 1px solid #b3b3b3;
  background: #fff;
  height: 260px;
}

.bottom-chart-body {
  height: calc(260px - 28px);
  margin: 4px 6px 6px;
  border: 1px solid #d0d0d0;
  padding: 6px;
}

.bottom-chart-body canvas {
  width: 100%;
  height: 100%;
}

/* ============ FOOTER ============ */
.footer-note {
  text-align: center;
  font-size: 11px;
  color: #444;

  /* ✅ ทำให้เป็นแถบเทา */
  background: #eeeeee;
  border-top: 1px solid #cfcfcf;

  /* ✅ ให้ติดกับ dashboard แต่ไม่ชนขอบ */
  margin: 10px 16px 0;
  padding: 10px 12px;

  border-radius: 8px;
  /* ถ้าไม่อยากโค้ง ลบได้ */
}


/* RESPONSIVE LAYOUT ไม่แตะข้อมูล / โครงสร้าง HTML — ปรับเฉพาะ layout */
/* --- จอเล็กกว่า 1200px (โน้ตบุ๊กเล็ก / แท็บเล็ตแนวนอน) --- */
@media (max-width: 1200px) {

  .header {
    height: auto;
    padding: 10px;
  }

  .header-right {
    justify-content: flex-end;
  }

  .main {
    grid-template-columns: 1fr;
    /* map + chart เรียงบน-ล่าง */
  }

  .map-panel,
  .rain-panel {
    height: 400px;
  }

  .rain-chart-body {
    height: calc(400px - 28px - 32px);
  }

  .filter-bar {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .kpi-panel .kpi-row {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .bottom-row {
    grid-template-columns: 1fr;
    /* กราฟล่างเรียงบน-ล่าง */
  }
}

/* --- จอเล็กกว่า 768px (แท็บเล็ตตั้ง / มือถือ) --- */
@media (max-width: 768px) {

  .page {
    border-left: none;
    border-right: none;
  }

  .header {
    grid-template-columns: 1fr;
    height: auto;
    text-align: center;
    padding: 8px;
  }

  .header-left {
    justify-content: center;
  }

  .header-right {
    justify-content: center;
    margin-top: 4px;
  }

  .header-link {
    font-size: 13px;
    padding: 6px 12px;
  }

  .header-right-text {
    text-align: center;
  }

  .filter-bar {
    padding: 8px;
    display: flex;
    overflow-x: auto;
    gap: 8px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

  .filter-bar::-webkit-scrollbar {
    display: none;
  }

  .filter-bar .filter-group {
    flex: 0 0 120px;
  }

  .filter-group {
    flex-direction: column;
    align-items: stretch;
  }

  .filter-label {
    width: 100%;
    text-align: center;
  }

  .main {
    padding: 8px;
  }

  .icon-bar {
    margin: 4px 8px 0;
  }

  .kpi-panel .kpi-row {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
    margin: 8px;
    padding-bottom: 0;
    overflow-x: visible;
  }

  /* Row 1: Planting period (first child) — full width */
  .kpi-panel .kpi-row>*:first-child {
    grid-column: 1 / -1;
  }

  /* Row 2: 3 KPI number cards in 3 columns */
  .kpi-panel .kpi-row .kpi-card-number {
    grid-column: auto;
    flex-direction: column;
    align-items: center;
    text-align: center;
    padding: 6px 4px;
  }

  .kpi-panel .kpi-row .kpi-card-number .kpi-title {
    font-size: clamp(8px, 2.2vw, 12px);
    line-height: 1.2;
    margin-bottom: 2px;
  }

  .kpi-panel .kpi-row .kpi-card-number .kpi-value {
    margin: 0;
    font-size: clamp(16px, 5vw, 28px);
    white-space: nowrap;
    align-self: center;
  }

  /* Row 3: Cost card (last child) — full width */
  .kpi-panel .kpi-row>*:last-child {
    grid-column: 1 / -1;
  }

  .cost-label-row {
    font-size: clamp(8px, 2vw, 11px);
  }

  .bottom-row {
    margin: 10px 8px 16px;
  }

  .footer-note {
    margin: 4px 8px 10px;
  }
}

/* --- จอเล็กมาก < 480px (มือถือเล็ก) --- */
@media (max-width: 480px) {

  /* .filter-bar grid removed in favor of flex scroll */

  /* Keep the 3-column layout for the stats cards */
  .kpi-panel .kpi-row {
    grid-template-columns: repeat(3, 1fr);
  }

  .kpi-panel .kpi-row .kpi-card-number .kpi-value {
    font-size: clamp(14px, 4.5vw, 22px);
  }

  .kpi-panel .kpi-row .kpi-card-number .kpi-title {
    font-size: clamp(7px, 2vw, 10px);
  }

  .kpi-period {
    font-size: clamp(14px, 3.5vw, 20px);
  }

  .chart-legend {
    gap: 8px;
  }

  .header-title {
    font-size: clamp(14px, 4vw, 20px);
  }
}

/* ===== ปฏิทินระดับความเสี่ยง ===== */
.calendar-panel {
  border: 1px solid #b3b3b3;
  background: #fff;
  padding-bottom: 8px;
  display: flex;
  flex-direction: column;
}

.calendar-header-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 8px 0;
  font-size: 12px;
}

/* .calendar-title-text {
  font-weight: 600;
} */

.calendar-legend {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  font-size: 11px;
  font-weight: 500;
}

.calendar-legend-item {
  display: flex;
  align-items: center;
  gap: 4px;
}

.calendar-legend-color {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  display: inline-block;
}

/* รี-use สีเดียวกับ bar ต้นทุน */
.yield-level-0 {
  background: #00a651;
}

/* ตามเกณฑ์ */
.yield-level-1 {
  background: #f7e08b;
}

/* ต่ำกว่าเกณฑ์ */
.yield-level-2 {
  background: #e84c3d;
}

/* ต่ำกว่าเกณฑ์มาก */

.calendar-grid {
  flex: 1;
  margin: 4px 8px 8px;
  display: grid;
  grid-template-columns: 70px repeat(7, 1fr);
  grid-auto-rows: 1fr;
  /* แถวสูงเท่ากัน เติมเต็มกรอบ */
  font-size: 13px;
  border-top: 1px solid #dddddd;
  border-left: 1px solid #dddddd;
  border-right: 1px solid #dddddd;
  border-bottom: 1px solid #dddddd;
}

/* header แถวชื่อวัน */
.calendar-day-header {
  font-size: 11px;
  font-weight: 500;
  text-align: center;
  display: flex;
  align-items: center;
  justify-self: center;
}

/* ไม่ให้ cell แถวล่างสุดวาดเส้นทับกรอบ */
.calendar-cell:last-child,
.calendar-cell:nth-last-child(-n + 7) {
  border-bottom: none;
}

/* ไม่ให้ cell วันอาทิตย์วาดเส้นทับกรอบขวา */
.calendar-cell:nth-child(8n) {
  border-right: none;
}

/* ช่อง label สัปดาห์ */
.calendar-week-label {
  display: flex;
  font-weight: 500;
  align-items: center;
  justify-content: flex-start;
  padding-left: 6px;
}

/* ช่องวัน */
.calendar-cell {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2px 0;
  border-right: 1px solid #dddddd;
  border-bottom: 1px solid #dddddd;
  height: auto;
}

.calendar-cell-box {
  width: 24px;
  height: 24px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
}

/* สีของ cell ตาม level (ใช้ร่วมกับ legend ได้เลย) */
.calendar-cell-box.level-0 {
  background: #00a651;
  color: #fff;
}

.calendar-cell-box.level-1 {
  background: #f7e08b;
  color: #000;
}

.calendar-cell-box.level-2 {
  background: #e84c3d;
  color: #fff;
}

.calendar-cell-box.level-4 {
  background: #d9d9d9;
  /* เทา */
  color: #777;
}

/* วันไม่มีข้อมูล */
.calendar-cell-box.no-data {
  background: #f0f0f0;
  color: #aaa;
}

.main-lower {
  display: grid;
  grid-template-columns: 370px 1fr;
  /* ซ้ายกว้างเท่า map-panel, ขวาเต็มที่เหลือ */
  gap: 10px;
  padding: 6px 16px 0;
  align-items: stretch;
}

/* 📱 Responsive */
@media (max-width: 1200px) {
  .kpi-charts-wrapper {
    grid-template-columns: 1fr;
  }
}


/* ปรับต้นทุนผลผลิตให้แน่นขึ้นด้วย
.cost-card {
  height: auto;
  padding: 4px 6px 3px;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
} */

.cost-header {
  gap: 6px;
  margin-bottom: 2px;
}

.cost-bar-wrapper {
  height: 18px;
  /* เตี้ยลงนิด */
}

.cost-label-row {
  margin-top: 0;
  font-size: 10px;
}

/* ---------- แก้ layout ให้ไม่เบี้ยวเวลา zoom / จอแคบ ---------- */
@media (max-width: 1400px) {

  /* ให้ panel หลักยืดความสูงตามเนื้อหา */
  .map-panel {
    min-width: 320px;
  }

  .rain-panel {
    min-width: 320px;
  }

  .bottom-panel {
    min-width: 280px;
  }

  .kpi-chart-panel {
    min-width: 280px;
  }

  /* map ยังอยากให้สูงพอใช้ เลย fix แค่ตัวพื้นที่แผนที่ */
  .map-body {
    height: 100%;
    min-width: 280px;
  }

  /* กรอบที่ห่อ canvas ของกราฟ ให้สูงอัตโนมัติ
     แต่มี min-height กันเตี้ยเกินไป */
  .rain-chart-body,
  .kpi-chart-body,
  .bottom-chart-body {
    height: 220px;
    min-height: 220px;
  }

  /* canvas ของกราฟ ให้ยืดเต็มกรอบได้ */
  .rain-chart-body canvas,
  .kpi-chart-body canvas,
  .bottom-chart-body canvas {
    width: 100% !important;
    max-width: 100% !important;
    height: 100% !important;
  }
}

/* DESKTOP DASHBOARD FRAME (GREEN BORDER MODE) */
/*  กรอบครอบ dashboard: เลื่อนอยู่ในกรอบนี้เท่านั้น */
.dash-viewport {
  width: min(1400px, 92vw);
  height: 870px;
  margin: 24px auto;
  /* ⬅️ เพิ่มช่องว่างรอบกรอบ */
  background: #fff;
  border: 6px solid #2f6b2f;
  /* border-radius: 18px; */
  overflow: auto;
  box-sizing: border-box;

  /* ✅ เพิ่ม padding รอบใน ให้ dashboard ไม่ชิดขอบ */
  padding: 24px;
  /* ⭐ สำคัญมาก */

  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
}


/* ✅ กล่องขนาด “จริงตามสเกล” เพื่อให้ scrollbar ถูกต้อง */
.dash-inner {
  --dash-scale: 1;
  flex: 0 0 auto;
  width: calc(1680px * var(--dash-scale));
  height: calc(950px * var(--dash-scale));
  /* ⬅️ ใช้ height ไม่ใช้ min-height */
}

/* ✅ หน้า dashboard ขนาดฐาน แล้วค่อย scale */
.dash-inner .page {
  width: 1680px;
  min-height: 950px;
  margin: 0;
  overflow: visible;
  transform: scale(var(--dash-scale));
  transform-origin: top left;
}

/* ✅ ซ่อน scrollbar ของกรอบ แต่ยัง scroll ได้ */
.dash-viewport.hide-scrollbars {
  scrollbar-width: none;
  /* Firefox */
  -ms-overflow-style: none;
  /* IE/Edge เก่า */
}

.dash-viewport.hide-scrollbars::-webkit-scrollbar {
  width: 0;
  height: 0;
}

/* footer ใน dashboard ให้ติดกับหน้าและเป็นพื้นเทา */
.dash-inner .footer-note {
  margin: 0;
  padding: 10px 16px 12px;
  background: #eee;
  border-top: 1px solid #cfcfcf;
  color: #444;
  text-align: center;
  font-size: 11px;
}

.calendar-header {
  padding: 6px 8px 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

/* แถวบน: ลูกศร + ชื่อเดือน */
.calendar-header-top {
  display: grid;
  grid-template-columns: 28px 1fr 28px;
  align-items: center;
}

.calendar-title-text {
  text-align: center;
  font-weight: 600;
  font-size: 13px;
}

/* legend ลงแถวล่าง */
.calendar-legend {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
  font-size: 11px;
  font-weight: 500;
  margin: 4px 0 0;
}

.calendar-header-top button {
  color: #2f6b2f;
  /* สีลูกศร */
  background: none;
  border: none;
  font-size: 16px;
  /* ปรับขนาดลูกศรได้ */
  cursor: pointer;
}

.calendar-header-top button:hover {
  color: #f08419;
  transform: scale(1.1);
}

/* ไฮไลท์วันปัจจุบัน */
.calendar-cell-box.today {
  border: 2px solid #439ef9;
  box-sizing: border-box;
  box-shadow: 0 0 0 3px #439ef9;
}

/* .calendar-title-text {
  font-size: 16px;
  font-weight: 600;
} */

#yield-calendar-title {
  text-align: center;
  font-weight: 600;
  font-size: 16px;
}

/* MOBILE MODE: ปิด scale dashboard */
@media (max-width: 1024px) {

  /* ปิดกรอบเขียว */
  .dash-viewport {
    width: 100%;
    height: auto;
    margin: 0;
    border: none;
    padding: 0;
    overflow: visible;
    display: block;
    /* สำคัญ */
  }

  /* ยกเลิก inner fixed size */
  .dash-inner {
    width: 100% !important;
    height: auto !important;
  }

  /* ยกเลิก 1680px base */
  .dash-inner .page {
    width: 100% !important;
    min-height: auto !important;
    transform: none !important;
  }

  /* ลบ min-height 950px ที่ล็อกไว้ */
  .page {
    min-height: auto !important;
  }
}

@media (max-width: 768px) {
  .map-panel {
    height: 300px;
  }

  .map-body {
    height: 100%;
  }

  #map {
    position: absolute;
    inset: 0;
  }
}

@media (max-width: 768px) {
  .icon-bar {
    overflow-x: auto;
    display: flex;
  }

  .icon-cell {
    flex: 0 0 40px;
  }
}

@media (max-width: 768px) {
  .kpi-panel .kpi-row {
    display: grid;
  }

  .kpi-card,
  .cost-card {
    width: 100%;
  }
}

@media (max-width: 768px) {
  .main-lower {
    grid-template-columns: 1fr;
  }

  .calendar-panel {
    margin-bottom: 10px;
  }

  .kpi-charts-wrapper {
    grid-template-columns: 1fr;
  }
}

/* ===============================
   MOBILE TAB SYSTEM
================================= */

.mobile-tabs {
  display: none;
}

/* เฉพาะมือถือ */
@media (max-width: 768px) {

  .mobile-tabs {
    display: flex;
    position: fixed;
    bottom: 50px;
    /* Stacked above the 50px bottom nav */
    left: 0;
    width: 100%;
    z-index: 1000;
    background: #ffffff;
    border-top: 2px solid #2f6b2f;
    border-bottom: none;
    box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.05);
  }

  /* Back/Next Bottom Nav */
  .mobile-nav-bottom {
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 50px;
    background: #f1f1f1;
    border-top: 1px solid #ccc;
    padding: 0 12px;
    z-index: 1001;
  }

  .nav-btn {
    padding: 6px 16px;
    font-size: 14px;
    cursor: pointer;
    background: #fff;
    border: 1px solid #bbb;
    border-radius: 4px;
  }

  .nav-indicator {
    font-size: 14px;
    font-weight: 600;
    color: #333;
  }

  .tab-btn {
    flex: 1;
    padding: 10px 4px;
    font-size: 11.5px;
    font-weight: 600;
    border: none;
    background: #f2f2f2;
    cursor: pointer;
    transition: background 0.25s, color 0.25s;
    position: relative;
    color: #555;
    white-space: nowrap;
  }

  .tab-btn.active {
    background: #2f6b2f;
    color: #ffffff;
  }

  .tab-btn:not(.active):hover {
    background: #e0e0e0;
  }

  /* Animated underline indicator */
  .tab-btn.active::after {
    content: '';
    position: absolute;
    bottom: -2px;
    left: 10%;
    right: 10%;
    height: 3px;
    background: #4caf50;
    border-radius: 2px;
  }

  /* ---- .main grid → mobile single-column ---- */
  .main {
    display: block;
    padding: 8px;
  }

  /* ---- .main-lower grid → mobile single-column ---- */
  .main-lower {
    display: block;
    padding: 8px;
  }

  /* Map responsive height on mobile */
  .map-panel {
    height: 23vh;
    /* เดิมคือ 35vh */
    min-height: 180px;
    max-height: 320px;
  }

  .map-panel #map {
    height: 100%;
  }

  /* Extra-small phones (iPhone SE, etc.) */
  @media (max-height: 700px) {
    .map-panel {
      height: 28vh;
      min-height: 160px;
    }
  }

  /* Rain panel — split charts on mobile */
  .rain-panel {
    height: auto;
    margin-bottom: 10px;
  }

  /* Hide original combined chart + legend on mobile */
  .rain-panel .rain-chart-body {
    display: none;
  }

  .rain-panel .chart-legend {
    display: none;
  }

  /* Show mobile split charts */
  .rain-panel .mobile-rain-split {
    display: flex;
    flex-direction: column;
    gap: 0;
    padding: 8px;
  }

  /* Toggle bar */
  .rain-toggle-bar {
    display: flex;
    background: #e8e8e8;
    border-radius: 8px;
    padding: 3px;
    margin-bottom: 10px;
  }

  .rain-toggle-btn {
    flex: 1;
    padding: 8px 6px;
    font-size: 13px;
    font-weight: 600;
    border: none;
    border-radius: 6px;
    background: transparent;
    color: #666;
    cursor: pointer;
    transition: all 0.25s ease;
  }

  .rain-toggle-btn.active {
    background: #2f6b2f;
    color: #fff;
    box-shadow: 0 2px 6px rgba(47, 107, 47, 0.3);
  }

  .mobile-rain-section {
    border: 1px solid #e0e0e0;
    border-radius: 10px;
    padding: 10px;
    background: #fafafa;
  }

  /* .mobile-rain-section.mobile-rain-actual {
    border-left: 4px solid #1f4e79;
  } */

  /* .mobile-rain-section.mobile-rain-forecast {
    border-left: 4px solid #5b9bd5;
  } */

  .mobile-rain-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 4px 12px;
    margin-bottom: 8px;
    font-size: 11px;
  }

  .mobile-rain-chart-body {
    width: 100%;
    min-height: 280px;
    border: 1px solid #e8e8e8;
    border-radius: 6px;
    padding: 4px;
    background: #fff;
  }

  .mobile-rain-chart-body canvas {
    width: 100% !important;
    min-height: 260px;
  }

  /* Hide icon-bar (rice stage) on mobile rain tab to keep it clean */
  .rain-panel .icon-bar {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Calendar panel full width */
  .calendar-panel {
    margin-bottom: 10px;
  }

  /* KPI charts stacked */
  .kpi-charts-wrapper {
    grid-template-columns: 1fr;
  }

  .kpi-chart-panel {
    height: auto;
    max-height: 202px;
    overflow: hidden;
    /* Ensure no overlap if slight mismatch */
  }

  .kpi-chart-body {
    height: 140px;
    /* Fit within ~202px (minus header/legend) */
    min-height: 140px;
    /* Override the 220px from desktop */
    margin-top: 2px;
  }

  .kpi-chart-body canvas {
    height: 100% !important;
    min-height: 0 !important;
    /* Allow shrinking */
  }

  /* Rice Stage Bar in Calendar Panel on Mobile:
     3 month-groups per row × 4 rows = 12 months, each group has label + 3 dekad cells */
  .calendar-panel #riceStageBar {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 4px;
    padding: 4px;
    margin-bottom: 12px;
    height: auto;
    overflow: visible;
    border: 1px solid #eee;
    border-radius: 4px;
  }

  /* Each month-group: month number (top) → 3 cells separated by vertical lines */
  .calendar-panel #riceStageBar .month-group {
    display: grid;
    grid-template-rows: auto 1fr;
    grid-template-columns: repeat(3, 1fr);
    border: 1px solid #d0d0d0;
    border-radius: 3px;
    overflow: hidden;
  }

  /* Month number (1–12) spans all 3 columns at top */
  .calendar-panel #riceStageBar .month-label {
    display: block;
    grid-column: 1 / -1;
    text-align: center;
    font-size: 9px;
    font-weight: 600;
    padding: 1px 0;
    background: #c8c8c8;
    color: #333;
    line-height: 1.4;
  }

  /* Cells are transparent so group color shows through; vertical line separates dekads */
  .calendar-panel #riceStageBar .icon-cell {
    flex: unset;
    height: 30px;
    font-size: 12px;
    border: none;
    border-top: 1px solid rgba(0, 0, 0, 0.08);
    border-right: 1px solid rgba(0, 0, 0, 0.15);
    background: transparent;
  }

  .calendar-panel #riceStageBar .icon-cell:last-child {
    border-right: none;
  }

  /* Re-apply special state backgrounds with enough specificity to override transparent */
  .calendar-panel #riceStageBar .icon-cell.active {
    background: #ffe08a;
  }

  .calendar-panel #riceStageBar .icon-cell.current-dekad {
    background: #5fb8fb;
  }

  .calendar-panel #riceStageBar .icon-cell.current-dekad.active {
    background: #5fb8fb;
  }

  .calendar-panel #riceStageBar .icon-cell.no-crop {
    background: transparent;
    opacity: 0.45;
  }

  /* Mobile filters inside calendar panel */
  #mobile-calendar-filters {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin-bottom: 12px;
    width: 100%;
  }

  /* Reduce panel title margin near filters */
  .calendar-panel .panel-title {
    margin-top: 4px;
  }

  /* Bottom content padding so content isn't hidden behind fixed nav */
  .footer-note {
    margin-bottom: 120px;
    font-size: 11px;
    color: #666;
    text-align: center;
    padding: 10px;
  }
}

/* ===== MOBILE BOTTOM NAV BAR ===== */
.mobile-nav-bottom {
  display: none;
}

@media (max-width: 768px) {
  .mobile-nav-bottom {
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 1001;
    background: #ffffff;
    border-top: 2px solid #2f6b2f;
    box-shadow: 0 -2px 6px rgba(0, 0, 0, 0.1);
    padding: 0 12px;
    height: 50px;
    box-sizing: border-box;
  }

  .nav-btn {
    padding: 8px 16px;
    font-size: 13px;
    font-weight: 600;
    border: 2px solid #2f6b2f;
    border-radius: 8px;
    background: #ffffff;
    color: #2f6b2f;
    cursor: pointer;
    transition: background 0.2s, color 0.2s, opacity 0.2s;
    min-width: 100px;
  }

  .nav-btn:active {
    background: #2f6b2f;
    color: #ffffff;
  }

  .nav-btn:disabled {
    opacity: 0.35;
    cursor: default;
    border-color: #aaa;
    color: #aaa;
  }

  .nav-indicator {
    font-size: 13px;
    font-weight: 600;
    color: #2f6b2f;
  }
}