/* ===========================================================================
   LAYOUT — app shell (sidebar + topbar), grid, page structure
   =========================================================================== */

/* ---- App shell ---- */
.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  grid-template-rows: var(--topbar-height) 1fr;
  grid-template-areas:
    "sidebar topbar"
    "sidebar main";
  min-height: 100vh;
}

.app-topbar {
  grid-area: topbar;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: 0 var(--space-5);
  padding-top: env(safe-area-inset-top);
  background: var(--bg-card);
  border-bottom: 1px solid var(--border-soft);
  position: sticky;
  top: 0;
  z-index: var(--z-nav);
}

.app-sidebar {
  grid-area: sidebar;
  background: var(--bg-card);
  border-right: 1px solid var(--border-soft);
  padding: var(--space-4) var(--space-3);
  overflow-y: auto;
  position: sticky;
  top: 0;
  height: 100vh;
}

.app-main {
  grid-area: main;
  padding: var(--space-5);
  /* ต้องเป็น clip ไม่ใช่ hidden — ตามสเปก ถ้าแกนหนึ่งไม่ใช่ visible อีกแกนจะ
     กลายเป็น auto โดยปริยาย ทำให้ main กลายเป็น scroll container ทั้งที่ไม่เคย
     เลื่อนจริง (ตัวที่เลื่อนคือ body) ผลคือ position:sticky ทุกตัวข้างในผูกกับ
     กล่องที่ไม่ขยับ จึงไม่ติดบนสักที · clip ตัดแกน x ได้เหมือนเดิมโดยไม่สร้าง
     scroll container */
  overflow-x: clip;
  min-width: 0;
  /* Static aura — makes translucent glass surfaces (.card, .bento-widget)
     read as glass on every page. Painted once; no runtime cost. */
  background:
    radial-gradient(900px 480px at 10% -4%, rgba(214, 168, 79, 0.13), transparent 70%),
    radial-gradient(760px 420px at 92% 4%, rgba(59, 130, 246, 0.07), transparent 70%),
    radial-gradient(900px 520px at 45% 34%, rgba(34, 160, 107, 0.05), transparent 72%);
}

.app-brand {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-3);
  margin-bottom: var(--space-4);
  text-decoration: none;
  color: var(--text-main);
}

.app-brand-logo {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  background: linear-gradient(135deg, var(--gold-primary), var(--gold-active));
  color: var(--text-inverse);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: var(--weight-bold);
  box-shadow: var(--shadow-sm);
}

.app-brand-name {
  font-size: var(--text-lg);
  font-weight: var(--weight-semibold);
  letter-spacing: -0.01em;
}

/* ---- Nav ---- */
.nav-group {
  margin-bottom: var(--space-4);
}

.nav-group-title {
  font-size: 11px;
  font-weight: var(--weight-semibold);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-subtle);
  padding: 6px var(--space-3);
  margin-bottom: 2px;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 10px var(--space-3);
  border-radius: var(--radius-sm);
  color: var(--text-body);
  text-decoration: none;
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  transition: all var(--dur-fast) var(--ease-ios);
  margin-bottom: 2px;
}

.nav-link:hover {
  background: var(--bg-hover);
  color: var(--text-main);
}

.nav-link.is-active {
  /* Soft gold tint gradient — active stays readable (dark gold text), minimal */
  background: linear-gradient(135deg, var(--gold-light), rgba(214, 168, 79, 0.10));
  color: var(--gold-active);
  box-shadow: inset 0 0 0 1px rgba(214, 168, 79, 0.18);
}

.nav-link.is-active .nav-icon {
  color: var(--gold-primary);
}

.nav-icon {
  width: 20px;
  height: 20px;
  color: var(--text-muted);
  flex-shrink: 0;
  transition: color var(--dur-fast) var(--ease-ios);
}

.nav-link:hover .nav-icon {
  color: var(--text-body);
}

.nav-badge {
  margin-left: auto;
  padding: 1px 6px;
  font-size: 10px;
  background: var(--gold-primary);
  color: var(--text-on-gold);
  border-radius: var(--radius-full);
}

/* ---- Topbar ---- */
.topbar-title {
  font-size: var(--text-lg);
  font-weight: var(--weight-semibold);
  color: var(--text-main);
  margin: 0;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.topbar-clock {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  background: var(--bg-subtle);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-md);
  color: var(--text-body);
  font-family: var(--font-mono);
  margin-left: auto;
  margin-right: var(--space-3);
}

.topbar-clock>.icon {
  color: var(--gold-active);
  flex-shrink: 0;
}

.topbar-clock-body {
  display: flex;
  flex-direction: column;
  line-height: 1.2;
}

.topbar-clock-date {
  font-size: 11px;
  color: var(--text-muted);
  font-family: var(--font-body);
}

.topbar-clock-time {
  font-size: var(--text-md);
  font-weight: var(--weight-semibold);
  font-variant-numeric: tabular-nums;
  color: var(--text-main);
  letter-spacing: 0.02em;
}

.topbar-clock-tz {
  padding: 2px 6px;
  background: var(--gold-soft);
  color: var(--gold-active);
  font-size: 10px;
  font-weight: var(--weight-semibold);
  border-radius: 999px;
  font-family: var(--font-body);
}

.topbar-search {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  min-width: 260px;
  background: var(--bg-subtle);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-md);
  color: var(--text-muted);
  cursor: pointer;
  transition: all 120ms ease;
  font-family: inherit;
  font-size: var(--text-sm);
}

.topbar-search:hover {
  border-color: var(--gold-primary);
  background: var(--bg-card);
}

.topbar-search-placeholder {
  flex: 1;
  text-align: left;
}

.topbar-search-kbd {
  font-family: var(--font-mono);
  font-size: 10px;
  padding: 2px 6px;
  background: var(--bg-card);
  border-radius: 3px;
  border: 1px solid var(--border-soft);
  color: var(--text-muted);
}

.topbar-divider {
  width: 1px;
  height: 24px;
  background: var(--border-soft);
  margin: 0 4px;
}

.topbar-user {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px;
  border-radius: var(--radius-md);
}

.topbar-user-body {
  display: flex;
  flex-direction: column;
  line-height: 1.2;
}

.topbar-user-name {
  font-size: var(--text-sm);
  font-weight: var(--weight-semibold);
  color: var(--text-main);
}

.topbar-user-role {
  font-size: 10px;
  color: var(--gold-active);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: var(--weight-semibold);
}

@media (max-width: 640px) {
  .topbar-clock {
    padding: 4px 8px;
    margin-right: var(--space-2);
    gap: 4px;
  }

  .topbar-clock-date {
    display: none;
  }

  .topbar-clock-tz {
    display: none;
  }

  .topbar-clock-time {
    font-size: 12px;
  }
}



/* ---- Page structure ---- */
.page-container {
  max-width: var(--content-max);
  margin: 0 auto;
}

.page-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-5);
  flex-wrap: wrap;
}

.page-title {
  margin: 0 0 4px;
  font-size: var(--text-2xl);
  font-weight: var(--weight-bold);
  letter-spacing: -0.02em;
}

.page-subtitle {
  margin: 0;
  color: var(--text-muted);
  font-size: var(--text-sm);
}

.page-actions {
  display: flex;
  gap: var(--space-2);
  align-items: center;
  /* หน้าที่มีปุ่มหลายกลุ่ม (เช่น รายงานการเงินที่มีทั้งมุมมองและช่วงเวลา)
     เดิมถูกบีบจนตัวเลือกท้าย ๆ ถูกตัดหาย · ให้ตกบรรทัดแทนการบีบ */
  flex-wrap: wrap;
  justify-content: flex-end;
}

.page-actions>.segment {
  flex: 0 0 auto;
  width: auto;
}

/* ---- Grid ---- */
.grid {
  display: grid;
  gap: var(--space-4);
}

.grid-2 {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.grid-3 {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.grid-4 {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.grid-5 {
  grid-template-columns: repeat(5, minmax(0, 1fr));
}

.grid-6 {
  grid-template-columns: repeat(6, minmax(0, 1fr));
}

@media (max-width: 1024px) {
  .grid-4 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .grid-3 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .grid-5 {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .grid-6 {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (max-width: 640px) {

  .grid-2,
  .grid-3,
  .grid-4 {
    grid-template-columns: 1fr;
  }

  .grid-5,
  .grid-6 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

/* ---- Flex utilities ---- */
.flex {
  display: flex;
}

.flex-col {
  flex-direction: column;
}

.flex-wrap {
  flex-wrap: wrap;
}

.items-center {
  align-items: center;
}

.items-start {
  align-items: flex-start;
}

.items-end {
  align-items: flex-end;
}

.justify-between {
  justify-content: space-between;
}

.gap-1 {
  gap: var(--space-1);
}

.gap-2 {
  gap: var(--space-2);
}

.gap-3 {
  gap: var(--space-3);
}

.gap-4 {
  gap: var(--space-4);
}

/* ---- Sidebar toggle (mobile drawer) ---- */
.sidebar-toggle {
  display: none;
  width: 40px;
  height: 40px;
  background: transparent;
  border: 0;
  border-radius: var(--radius-md);
  color: var(--text-main);
  cursor: pointer;
  align-items: center;
  justify-content: center;
}

.sidebar-toggle:hover {
  background: var(--bg-hover);
}

@media (max-width: 640px) {
  .sidebar-toggle {
    width: 44px;
    height: 44px;
  }
}

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(28, 28, 30, 0.4);
  z-index: var(--z-drawer);
  opacity: 0;
  transition: opacity var(--dur-base) var(--ease-ios);
}

.sidebar-backdrop.is-open {
  display: block;
  opacity: 1;
}

/* ---- Responsive ---- */
@media (max-width: 1023px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: var(--topbar-height-mobile) 1fr;
    grid-template-areas:
      "topbar"
      "main";
  }

  .app-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: 340px;
    height: 100vh;
    z-index: calc(var(--z-drawer) + 1);
    transform: translateX(-100%);
    transition: transform var(--dur-base) var(--ease-ios);
    box-shadow: var(--shadow-xl);
  }

  .app-sidebar.is-open {
    transform: translateX(0);
  }

  .sidebar-toggle {
    display: inline-flex;
  }

  .app-topbar {
    padding: 0 var(--space-3);
  }

  /* แถบบนต้องไม่ดันความกว้างของคอลัมน์เนื้อหา
     คอลัมน์เดียวของ shell ใช้ `1fr` ซึ่งมีพื้นขั้นต่ำเป็น min-content ของทุกไอเทม
     ในคอลัมน์ — แถบบนกับ main อยู่คอลัมน์เดียวกัน แถบบนที่ยังกางแบบเดสก์ท็อป
     (ช่องค้นหา min-width 260px + นาฬิกา + ชื่อผู้ใช้) จึงมี min-content ~809px
     ที่ 768px แล้วลาก main ให้กว้าง 809px ตามไปด้วย
     ผลคือ 41px ขวาสุดของ *ทุกหน้า* หลุดออกนอกจอ และเพราะ body ตั้ง
     overflow-x: hidden ไว้ ผู้ใช้เลื่อนไปดูไม่ได้ด้วย — ป้ายสถานะที่วางชิดขวา
     ของการ์ดถูกเฉือนหายไปครึ่งคำ
     กันที่ต้นเหตุ: ปลด min-content floor ของแถบบน แล้วให้ช่องค้นหาย่อได้จริง */
  .app-topbar,
  .topbar-actions {
    min-width: 0;
  }

  .topbar-search {
    min-width: 0;
    flex-shrink: 1;
  }

  .topbar-search-placeholder {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* ชื่อยาว ๆ อย่าง "สมศักดิ์ (พนักงานคีย์ 1)" ตัดเป็นสามบรรทัดในแถบสูง 56px
     แล้วล้นทะลุขอบล่างลงไปทับเนื้อหา — บรรทัดเดียวแล้วตัดด้วย … พอ */
  .topbar-user,
  .topbar-user-body {
    min-width: 0;
  }

  .topbar-user-name,
  .topbar-user-role {
    display: block;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* วันที่ในนาฬิกาก็ตัด "26" ตกไปอีกบรรทัดด้วยเหตุผลเดียวกัน */
  .topbar-clock-date {
    white-space: nowrap;
  }

  .app-main {
    padding: var(--space-3);
  }
}

/* ---- Auth layout ---- */
.auth-shell {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-5);
  background:
    radial-gradient(circle at 20% 20%, var(--gold-light) 0%, transparent 40%),
    radial-gradient(circle at 80% 80%, var(--gold-tint) 0%, transparent 40%),
    var(--bg-main);
}

.auth-card {
  width: 100%;
  max-width: 400px;
  padding: var(--space-7);
  background: var(--bg-card);
  border-radius: var(--radius-2xl);
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--border-soft);
}

.auth-brand {
  text-align: center;
  margin-bottom: var(--space-6);
}

.auth-brand-logo {
  width: 56px;
  height: 56px;
  border-radius: var(--radius-lg);
  background: linear-gradient(135deg, var(--gold-primary), var(--gold-active));
  color: var(--text-inverse);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: var(--text-xl);
  font-weight: var(--weight-bold);
  margin-bottom: var(--space-3);
  box-shadow: var(--shadow-md);
}

.auth-brand-logo .icon {
  color: var(--text-inverse);
}

.auth-brand-name {
  font-size: var(--text-xl);
  font-weight: var(--weight-bold);
  margin: 0 0 4px;
}

.auth-brand-tagline {
  font-size: var(--text-sm);
  color: var(--text-muted);
  margin: 0;
}

/* ---- Mobile Bottom Nav ---- */
.mobile-nav {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 64px;
  background: rgb(255 253 247 / 20%);
  backdrop-filter: saturate(180%) blur(15px);
  border-top: 1px solid var(--border-soft);
  z-index: var(--z-nav);
  box-shadow: 0 -2px 12px rgba(28, 28, 30, 0.05);
  padding-bottom: env(safe-area-inset-bottom);
  grid-template-columns: repeat(4, 1fr);
}

.mobile-nav:has(.mobile-nav-fab) {
  grid-template-columns: 1fr 1fr auto 1fr 1fr;
}

.mobile-nav:not(:has(.mobile-nav-fab)):has(.mobile-nav-item:nth-child(3):last-child) {
  grid-template-columns: repeat(3, 1fr);
}

.mobile-nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 6px 4px;
  text-decoration: none;
  color: var(--text-muted);
  font-size: 10px;
  font-weight: var(--weight-medium);
  background: transparent;
  border: 0;
  cursor: pointer;
  transition: color var(--dur-fast) var(--ease-ios);
  font-family: inherit;
}

.mobile-nav-item.is-active,
.mobile-nav-item:active {
  color: var(--gold-primary);
}

.mobile-nav-item span {
  line-height: 1.1;
}

.mobile-nav-fab {
  flex: 0 0 auto;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: var(--gold-primary);
  color: white;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-top: -14px;
  box-shadow: var(--shadow-lg);
  text-decoration: none;
  border: 3px solid var(--bg-card);
  transition: transform var(--dur-fast) var(--ease-ios), background var(--dur-fast) var(--ease-ios);
}

.mobile-nav-fab:hover,
.mobile-nav-fab:active {
  transform: scale(0.94);
  background: var(--gold-hover);
  color: white;
}

@media (max-width: 1023px) {
  .mobile-nav {
    display: grid;
    align-items: center;
  }

  .mobile-nav-item {
    flex: initial;
    width: 100%;
  }

  .mobile-nav-fab {
    justify-self: center;
  }

  .app-main {
    padding-bottom: 88px;
  }
}

/* ---- Mobile Dashboard Refinement (UX Sprint) ---- */
@media (max-width: 767px) {
  .app-topbar {
    gap: var(--space-2);
    padding: 0 var(--space-3);
  }

  .topbar-actions {
    gap: 2px;
  }

  .topbar-clock {
    margin-left: auto;
    margin-right: 4px;
    padding: 4px 8px;
    gap: 4px;
  }

  .topbar-clock>.icon {
    display: none;
  }

  .page-header {
    align-items: flex-start;
    gap: var(--space-3);
    margin-bottom: var(--space-4);
  }

  .page-title {
    font-size: var(--text-xl);
  }

  .page-subtitle {
    font-size: var(--text-xs);
  }

  .page-actions {
    width: 100%;
    flex-wrap: wrap;
    row-gap: var(--space-2);
  }

  .page-actions .btn {
    flex: 1 1 calc(50% - var(--space-2));
    min-width: 0;
    min-height: 40px;
    justify-content: center;
    padding-inline: var(--space-2);
    font-size: var(--text-sm);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .page-actions .btn.btn-block {
    flex-basis: 100%;
  }
}

@media (max-width: 479px) {
  .app-main {
    padding: var(--space-2) var(--space-3);
  }

  .grid {
    gap: var(--space-3);
  }
}

@media (max-width: 1023px) {
  .mobile-nav-item {
    min-height: 56px;
    padding: 8px 4px;
    gap: 3px;
  }

  .mobile-nav-item span {
    font-size: 11px;
  }

  .mobile-nav-fab {
    width: 72px;
    height: 72px;
    margin-top: -40px;
  }

  .app-main {
    padding-bottom: 92px;
  }
}

/* ---- Print ---- */
@media print {

  .app-sidebar,
  .app-topbar,
  .mobile-nav,
  .no-print {
    display: none !important;
  }

  .app-main {
    padding: 0;
  }

  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr;
    grid-template-areas: "main";
  }

  body {
    background: #fff;
  }
}
/* ---- พื้นที่สำรองท้ายหน้าเมื่อแบนเนอร์ติดตั้งแอปโผล่ ----

   แบนเนอร์เป็น position:fixed จึงไม่กินพื้นที่ในโฟลว์ — เนื้อหาแถวสุดท้ายของ
   ทุกหน้า (ปุ่ม ลิงก์ แถวตาราง) จึงถูกทับโดยไม่มีทางเลื่อนให้พ้น ที่นี่จองที่ว่าง
   เพิ่มเท่าความสูงจริงของแบนเนอร์ ซึ่ง pwa.js วัดแล้วเขียนลง --pwa-banner-space
   (ความสูงไม่คงที่: ที่ ≤480px ปุ่มตกลงมาอีกแถว)

   ผูกกับคลาสบน <body> ไม่ใช่ตั้งค่าตายตัว — แบนเนอร์ซ่อนอยู่เกือบตลอดเวลา
   ถ้าจองที่ไว้ถาวรทุกหน้าจะมีช่องว่างท้ายหน้าโดยไม่มีเหตุผล */
body.has-pwa-banner .app-main {
  padding-bottom: calc(var(--space-5) + var(--pwa-banner-space, 0px));
}

@media (max-width: 1023px) {
  body.has-pwa-banner .app-main {
    padding-bottom: calc(92px + var(--pwa-banner-space, 0px));
  }
}

@media print {
  .pwa-banner {
    display: none !important;
  }

  body.has-pwa-banner .app-main {
    padding-bottom: 0;
  }
}
