*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  font-family: Arial, sans-serif;
  text-align: center;
  margin: 0;
  min-height: 100vh;
  background-color: #f4f4f4;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: url('../images/search_001.png') no-repeat center center fixed;
  background-size: cover;
  padding: 20px;
}

/* Wrapper around the title + search box: pin it dead-center of the
   viewport (both axes) regardless of content height or page flow. */
.hero {
  position: fixed;
  top: 38%; /* a bit further above dead-center */
  left: 50%;
  transform: translate(-50%, -50%);
  width: 92%;
  max-width: 950px;
  text-align: center;
  z-index: 10;
}

/* Title Styling */
#first_line {
  font-weight: bold;
  width: 100%;
  font-size: clamp(1.25rem, 4vw, 2rem);
  color: #0f3d2e; /* deep ink green */
  text-shadow: 2px 2px 5px rgba(0, 0, 0, 0.2);
  margin: 0 0 16px;
  padding: 0 10px;
}

#searchBox {
  display: block;
  width: min(800px, 92vw);
  max-width: 100%;
  margin: 0 auto;
  padding: 12px 20px;
  font-size: 16px; /* >=16px avoids iOS Safari auto-zoom on focus */
  border: 2px solid #007bff;
  border-radius: 25px;
  outline: none;
  transition: all 0.3s ease-in-out;
  z-index: 1001;
}

#searchBox:focus {
  border-color: #47ee0f;
  box-shadow: 0 0 10px rgba(164, 237, 36, 0.5);
}

/* Loading / Result popups */
.popup {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 85vw;
  max-width: 1700px;
  max-height: 85vh;
  background: white;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
  padding: 20px;
  border-radius: 8px;
  z-index: 1000;
  overflow-y: auto;
  opacity: 0;
  display: none;
  transition: opacity 0.3s ease-in-out;
}

.popup.show {
  opacity: 1;
  display: block;
}

#loadingPopup {
  width: min(320px, 80vw);
  max-height: none;
}

.popup .close {
  position: sticky;
  top: 0;
  float: right;
  margin-top: -10px;
  cursor: pointer;
  font-size: 28px;
  line-height: 1;
  color: red;
  padding: 6px 4px;
  transition: transform 0.3s ease-in-out;
}

.popup .close:hover {
  transform: rotate(120deg);
}

.overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  z-index: 999;
  transition: opacity 0.3s ease-in-out;
}

.overlay.show {
  display: block;
}

/* Table Container */
#dataContainer {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid black;
}

table {
  width: 100%;
  min-width: 560px; /* keeps columns legible; container scrolls on desktop/tablet */
  border-collapse: collapse;
  table-layout: auto;
}

th,
td {
  border: 1px solid black;
  padding: 8px;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

th {
  background-color: yellow;
  font-weight: bold;
}

td {
  background-color: white;
}

.pagination {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 15px;
  margin-top: 15px;
}

.nav-button {
  display: inline-block;
  padding: 12px 20px;
  font-size: 16px;
  font-weight: bold;
  color: white;
  background: linear-gradient(135deg, #007bff, #0056b3);
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease-in-out;
  text-transform: uppercase;
  min-width: 120px;
  text-align: center;
}

.nav-button:hover {
  background: linear-gradient(135deg, #0056b3, #003d80);
  transform: scale(1.1);
  box-shadow: 0 4px 15px rgba(0, 91, 187, 0.5);
}

.nav-button:disabled {
  background: #ccc;
  cursor: not-allowed;
  box-shadow: none;
  transform: none;
}

/* ---------------------------------------------------------------- */
/* Responsive breakpoints                                            */
/* ---------------------------------------------------------------- */

/* Tablets and small laptops: tighten spacing a little */
@media (max-width: 768px) {
  body {
    padding: 12px;
    background-attachment: scroll; /* fixed backgrounds are unreliable on mobile browsers */
  }

  .popup {
    width: 92vw;
    max-width: none;
    padding: 14px;
    border-radius: 6px;
  }

  .nav-button {
    padding: 10px 16px;
    min-width: 100px;
  }
}

/* Phones: turn the table into a stacked card list so nothing gets clipped */
@media (max-width: 600px) {
  .popup {
    width: 96vw;
    max-height: 90vh;
  }

  #dataContainer {
    overflow-x: visible;
    border: none;
  }

  table {
    min-width: 0;
    width: 100%;
  }

  table thead {
    display: none; /* labels are shown per-cell instead, see td::before below */
  }

  table,
  table tbody {
    display: block;
    width: 100%;
  }

  /* Each row is a card. flex-direction:column keeps cells stacked like
     block layout did, but lets us reorder cells with `order` below and
     append a tap-to-expand hint as generated content. */
  table tr {
    display: flex;
    flex-direction: column;
    width: 100%;
    margin-bottom: 12px;
    border: 1px solid black;
    border-radius: 6px;
    overflow: hidden;
    cursor: pointer;
  }

  table td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    text-align: right;
    white-space: normal;
    word-break: break-word;
    width: 100%;
    border: none;
    border-top: 1px solid #eee;
    padding: 10px 12px;
  }

  /* First visible cell in each card gets no top divider (the card border
     already separates it). Empty cells (details/bill_id/remarks with no
     data) are dropped entirely so a card doesn't waste height on blank
     rows. */
  table td:first-child {
    border-top: none;
  }

  table td:empty {
    display: none;
  }

  table td::before {
    content: attr(data-label);
    font-weight: bold;
    text-align: left;
    color: #444;
    flex-shrink: 0;
  }

  /* Collapsed by default: only 柜号 (ct_id) then 日期 (record_date) show,
     in that order. Tapping the card (see toggleCard() in script.js) adds
     .expanded and reveals the rest. */
  table td[data-field="ct_id"] {
    order: 1;
  }

  table td[data-field="record_date"] {
    order: 2;
  }

  table td[data-field="cs_id"] {
    order: 3;
  }

  table td[data-field="quantity"] {
    order: 4;
  }

  table td[data-field="details"] {
    order: 5;
  }

  table td[data-field="bill_id"] {
    order: 6;
  }

  table td[data-field="remarks"] {
    order: 7;
  }

  table tr:not(.expanded) td[data-field="cs_id"],
  table tr:not(.expanded) td[data-field="quantity"],
  table tr:not(.expanded) td[data-field="details"],
  table tr:not(.expanded) td[data-field="bill_id"],
  table tr:not(.expanded) td[data-field="remarks"] {
    display: none;
  }

  /* Tap-to-expand hint, appended after the visible cells */
  table tr::after {
    content: "展开详情/ดูเพิ่มเติม \25BE";
    order: 99;
    text-align: center;
    padding: 6px;
    font-size: 12px;
    color: #007bff;
    border-top: 1px solid #eee;
  }

  table tr.expanded::after {
    content: "收起 \25B4";
  }

  /* "No Result" row: keep it centered, no expand affordance, not clickable */
  table tr.no-result {
    cursor: default;
  }

  table tr.no-result::after {
    content: none;
  }

  table td[colspan] {
    display: block;
    text-align: center;
  }

  table td[colspan]::before {
    content: none;
  }
}

@media (max-width: 380px) {
  #first_line {
    font-size: 1.1rem;
  }

  #searchBox {
    padding: 10px 16px;
  }
}
