/* ── macOS Minimal Theme ─────────────────────────────────────── */
/* Font: -apple-system stack (SF Pro / Helvetica Neue)            */
/* Palette: White / Light Gray (#f5f5f7) / Blue accent (#007AFF)  */
/* Radius: Squircle-inspired rounded corners                       */
/* ──────────────────────────────────────────────────────────────── */

:root {
  /* ── macOS Accent & Brand ── */
  --brand:         #1a5c38;   /* หัวโมดัล ปร.4 + สีหลักระบบ */
  --brand-light:   #e8f5ee;   /* พื้นหลัง table header */
  --accent:        #007AFF;
  --accent-hover:  #0066DD;
  --accent-light:  #E8F2FF;
  --accent-pale:   #F0F7FF;

  /* ── Semantic Colors ── */
  --danger:        #FF3B30;
  --danger-light:  #FFF2F1;
  --warn:          #FF9500;
  --warn-light:    #FFF8EE;
  --success:       #34C759;
  --success-light: #F0FBF3;

  /* ── Ink / Text ── */
  --ink:   #1d1d1f;
  --ink-2: #3d3d3f;
  --ink-3: #86868b;
  --ink-4: #c7c7cc;

  /* ── Surfaces & Borders ── */
  --border:   #d2d2d7;
  --bg:       #f5f5f7;
  --surface:  #ffffff;
  --surface-2:#f9f9fb;

  /* ── Apple Font Stack ── */
  /* [FIX 2026-07] เดิม --font ไม่มีฟอนต์ไทยเลยสักตัว (Apple/Helvetica/Arial ไม่มี glyph ไทย)
     ทั้งที่ index.html โหลด "Noto Sans Thai" จาก Google Fonts ไว้แล้วแต่ลืมใส่ในนี้ — ทำให้ข้อความ
     ไทยทั้งเว็บ fallback ไปใช้ฟอนต์ของระบบปฏิบัติการเงียบๆ ซึ่งแต่ละบริบทการ render (ปกติ vs
     popup ของ <select> ที่บางเบราว์เซอร์ใช้ path render ต่างจากหน้าเว็บปกติ) เลือก fallback
     คนละตัวกัน เห็นเป็นฟอนต์ไม่ตรงกัน (เช่น dropdown จังหวัดในหน้าเลือกวัสดุ) ใส่ Noto Sans Thai
     ไว้ก่อน Apple/Helvetica ให้ข้อความไทยทั้งเว็บ (รวม <option> ใน native select) ใช้ฟอนต์
     เดียวกันเสมอ ไม่ว่าจะ render ที่ไหน */
  --font: 'Noto Sans Thai', -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Helvetica Neue', Arial, sans-serif;
  --font-th: 'Noto Sans Thai', var(--font);

  /* ── Squircle Radii ── */
  --r-sm: 8px;
  --r-md: 12px;
  --r-lg: 16px;
  --r-xl: 20px;

  /* ── Shadows (subtle, macOS-like) ── */
  --shadow:    0 1px 4px rgba(0,0,0,.08), 0 2px 12px rgba(0,0,0,.05);
  --shadow-md: 0 4px 16px rgba(0,0,0,.10), 0 1px 4px rgba(0,0,0,.06);

  /* [FEAT 2026-07] ── Z-index layers (มาตรฐานทั้งระบบ) ──────────────────────
     เดิมแต่ละไฟล์กำหนด z-index เป็นเลขสุ่มเอง (9000, 9500, 10000, 10050, 55000,
     56000, 100000, 1000001, 1000002, 2147483647) ไม่มีระบบ เสี่ยง modal/popup
     ที่สร้างทีหลังถูกบัง หรือบังของเก่าโดยไม่ตั้งใจ — ใช้ 5 ชั้นนี้แทนเลขสุ่มเสมอ:
       --z-panel    หน้าเต็มจอ (ไม่มีพื้นหลังจาง แทนที่เนื้อหาหลักไปเลย เช่น
                    ประมาณการทหารช่าง ผนวก ข., บัญชีปรับปรุงสินทรัพย์)
       --z-modal    dialog มาตรฐาน (มีพื้นหลังจาง + การ์ดลอยกลางจอ)
       --z-picker   popup/picker ที่ต้องซ้อนทับ modal อื่นที่เปิดอยู่แล้ว
                    (เช่น เลือกวัสดุตามโครงการอื่น ที่เปิดจาก modal เลือกวัสดุ)
       --z-dropdown autocomplete/suggestion แบบลอย (ต้องพ้นทุก container/modal)
       --z-toast    แจ้งเตือนมุมจอ (อยู่บนสุดเสมอ)
  ───────────────────────────────────────────────────────────────────────── */
  /* [FIX 2026-07] ค่าอิงจาก .modal-overlay class เดิมที่ modal ส่วนใหญ่ในระบบใช้อยู่แล้ว (z-index:
     10000, ดูนิยามด้านล่าง) — ไม่ใช่เลขที่ตั้งขึ้นใหม่ลอยๆ กันชนกับ modal เดิมที่มีอยู่แล้วทั้งระบบ */
  --z-panel:    9000;
  --z-modal:    10000;
  --z-picker:   10500;
  --z-dropdown: 10800;
  /* --z-toast: .toast class ใช้ 2147483647 ตรงๆ (ไม่ผ่านตัวแปรนี้) — ตั้งใจคงค่าเดิมไว้ เพราะมี
     คอมเมนต์ fix เดิมอธิบายว่าต้องสูงกว่าทุก stacking context เสมอไม่ว่า modal จะซ้อนกี่ชั้น */
}

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

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--ink);
  min-height: 100vh;
  font-size: 16px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

a { color: var(--accent); text-decoration: none; }

.app-shell { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }

/* ── 1. TOPBAR ── */
.topbar {
  background: rgba(246,246,248,0.85);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  color: var(--ink);
  padding: 0 28px;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 100;
  border-bottom: 1px solid rgba(0,0,0,.10);
}

.topbar-left  { display: flex; align-items: center; gap: 12px; }
.topbar-title { font-size: 17px; font-weight: 600; letter-spacing: -.01em; color: var(--ink); }
.topbar-sub   { font-size: 12px; color: var(--ink-3); margin-top: 1px; }
.topbar-right { display: flex; align-items: center; gap: 10px; }

.user-pill {
  display: flex; align-items: center; gap: 7px;
  background: rgba(255,255,255,.8);
  border-radius: 20px;
  padding: 5px 14px;
  font-size: 13px;
  border: 1px solid var(--border);
}

.user-dot  { width: 8px; height: 8px; border-radius: 50%; background: var(--success); }
.role-badge { font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 10px; }
.role-admin   { background: #FFE5E0; color: #C0392B; }
.role-officer { background: var(--accent-light); color: var(--accent); }
.role-user    { background: var(--success-light); color: #1a8a3d; }

/* ── 2. SIDEBAR ── */
.sidebar {
  width: 220px;
  background: rgba(250,250,252,0.95);
  border-right: 1px solid var(--border);
  padding: 12px 0;
  flex-shrink: 0;
  position: sticky;
  top: 52px;
  height: calc(100vh - 52px);
  overflow-y: auto;
}

.sidebar-sec {
  font-size: 11px; font-weight: 600; color: var(--ink-3);
  text-transform: uppercase; letter-spacing: .08em;
  padding: 12px 16px 5px;
}

.nav-item {
  display: flex; align-items: center; gap: 9px;
  width: 100%; padding: 9px 16px; border: none; background: none;
  font-size: 15px; font-family: var(--font); color: var(--ink-2);
  cursor: pointer; text-align: left; transition: all .12s;
  border-radius: 0;
  /* [FEAT 2026-07] .nav-item ตอนนี้ใช้ทั้งกับ <button> (เมนูภายใน) และ <a> (ลิงก์ภายนอกในกลุ่ม
     "ข้อมูลเพิ่มเติม") — ต้องเซ็ต text-decoration:none กับ box-sizing กันเบราว์เซอร์ขีดเส้นใต้/
     บวก padding เกิน 100% ให้ตัว <a> */
  text-decoration: none;
  box-sizing: border-box;
}

.nav-item:hover  { background: rgba(0,122,255,.07); color: var(--accent); }
.nav-item.active { background: rgba(0,122,255,.10); color: var(--accent); font-weight: 600; }
.nav-item .nav-icon { font-size: 17px; width: 22px; flex-shrink: 0; }
/* [FEAT 2026-07] a:visited ของเบราว์เซอร์ (specificity 0,1,1) ชนะ .nav-item เฉยๆ (0,1,0) ได้ —
   ล็อกสีไม่ให้ลิงก์ภายนอกที่เคยคลิกแล้วเปลี่ยนเป็นสีม่วงต่างจากเมนูอื่น */
a.nav-item:visited, a.nav-item:link { color: var(--ink-2); }
a.nav-item:visited:hover, a.nav-item:link:hover { color: var(--accent); }

/* ── 3. CONTENT & LAYOUT ── */
.main-layout {
  display: flex;
  flex: 1;
  height: calc(100vh - 52px);
  overflow: hidden;
}
.content {
  flex: 1;
  min-width: 0; /* [FIX 2026-07] จำเป็นสำหรับ flex item ที่มีลูกกว้างเกิน (เช่น ตารางกว้าง) ให้เลื่อนดูได้
                   ปกติแทนที่จะดัน .content กว้างเกิน viewport แล้วโดน overflow-x:clip ตัดทิ้งบนมือถือ */
  padding: 28px 32px;
  overflow-y: auto;
  max-width: 100%;
  height: 100%;
}
.page-header  { margin-bottom: 22px; }
.page-title   { font-size: 22px; font-weight: 700; color: var(--ink); display: flex; align-items: center; gap: 8px; letter-spacing: -.02em; }
.page-sub     { font-size: 14px; color: var(--ink-3); margin-top: 5px; }

/* ── CARDS ── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  overflow: hidden;
  margin-bottom: 18px;
  box-shadow: var(--shadow);
}
.card-head {
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  color: var(--ink);
  padding: 13px 20px;
  font-size: 15px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
}
.card-body { padding: 20px; }

/* ── BUTTONS ── */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 9px 18px; border: none; border-radius: var(--r-md);
  font-size: 15px; font-weight: 500; font-family: var(--font);
  cursor: pointer; transition: all .12s; white-space: nowrap;
  letter-spacing: -.01em;
}
.btn:disabled { opacity: .4; cursor: not-allowed; }

.btn-brand {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 1px 3px rgba(0,122,255,.30);
}
.btn-brand:hover:not(:disabled) {
  background: var(--accent-hover);
  box-shadow: 0 2px 8px rgba(0,122,255,.35);
}

.btn-outline {
  background: var(--surface);
  color: var(--accent);
  border: 1px solid var(--border);
}
.btn-outline:hover { background: var(--accent-pale); border-color: var(--accent); }

.btn-danger { background: var(--danger-light); color: var(--danger); border: 1px solid #ffd0ce; }
.btn-danger:hover { background: var(--danger); color: #fff; border-color: var(--danger); }

.btn-warn { background: var(--warn-light); color: var(--warn); border: 1px solid #ffd9a0; }
.btn-warn:hover { background: var(--warn); color: #fff; border-color: var(--warn); }

.btn-sm { padding: 6px 13px; font-size: 14px; }
.btn-lg { padding: 12px 28px; font-size: 15px; }

/* ── 4. FORMS ── */
.form-group  { margin-bottom: 15px; }
.form-label  { display: block; font-size: 14px; font-weight: 500; color: var(--ink-2); margin-bottom: 5px; }

.form-input, .form-select {
  width: 100%; padding: 10px 13px;
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  font-size: 15px; font-family: var(--font);
  background: var(--surface); color: var(--ink);
  outline: none; transition: all .12s;
}

.form-input:focus, .form-select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(0,122,255,.15);
}

.form-select { cursor: pointer; }
/* [FIX 2026-07] กันบางเบราว์เซอร์ไม่ inherit font-family เข้า <option> อัตโนมัติ (พบใน dropdown
   จังหวัดที่ font ไม่ตรงกับตัว select เอง) — ระบุตรงให้ชัดเจน ไม่พึ่งพา inheritance อย่างเดียว */
.form-select option, select option { font-family: inherit; }
.form-row    { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }

/* ── TABLES ── */
.tbl-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 16px; }

thead th {
  background: var(--surface-2);
  padding: 10px 14px;
  text-align: left;
  font-size: 14px;
  font-weight: 600;
  color: var(--ink-3);
  white-space: nowrap;
  border-bottom: 1px solid var(--border);
}
tbody tr  { border-bottom: 1px solid var(--border); }
tbody tr:hover td { background: var(--accent-pale); }
td        { padding: 10px 14px; vertical-align: middle; }
.td-actions { display: flex; gap: 6px; }

/* ── BADGES ── */
.badge { display: inline-block; font-size: 13px; font-weight: 600; padding: 3px 9px; border-radius: 10px; vertical-align: middle; }
.badge-green  { background: #d1fae5; color: #065f46; }
.badge-blue   { background: var(--accent-light); color: #0051a8; }
.badge-amber  { background: #fef9c3; color: #854d0e; }
.badge-gray   { background: #f1f5f9; color: #64748b; }
.badge-red    { background: #fee2e2; color: #991b1b; }
.badge-sky    { background: #e0f2fe; color: #075985; }
.badge-orange { background: #fff7ed; color: #9a3412; }
.badge-teal   { background: #f0fdfa; color: #134e4a; }
.badge-violet { background: #f5f3ff; color: #5b21b6; }

/* ── LOGIN ── */
.login-wrap {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: var(--bg);
}
.login-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  padding: 40px;
  width: 400px;
  box-shadow: var(--shadow-md);
}
.login-logo  { text-align: center; font-size: 44px; margin-bottom: 10px; }
.login-title { text-align: center; font-size: 20px; font-weight: 700; color: var(--ink); margin-bottom: 5px; letter-spacing: -.02em; }
.login-sub   { text-align: center; font-size: 13px; color: var(--ink-3); margin-bottom: 26px; }
.login-err   { background: var(--danger-light); color: var(--danger); border-radius: var(--r-md); padding: 10px 14px; font-size: 13px; margin-bottom: 14px; display: none; }

/* ── PROJECTS ── */
.proj-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 16px; }
.proj-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  padding: 18px;
  cursor: pointer;
  transition: all .15s;
  position: relative;
}
.proj-card:hover { border-color: var(--accent); box-shadow: var(--shadow-md); transform: translateY(-2px); }
.proj-card-title { font-size: 15px; font-weight: 600; color: var(--ink); margin-bottom: 7px; }
.proj-card-meta  { font-size: 13px; color: var(--ink-3); line-height: 1.7; }
.proj-card-status { position: absolute; top: 14px; right: 14px; }

/* ── UPLOAD ── */
.upload-zone {
  border: 1.5px dashed var(--border);
  border-radius: var(--r-lg);
  padding: 32px;
  text-align: center;
  cursor: pointer;
  transition: all .15s;
}
.upload-zone:hover, .upload-zone.drag { border-color: var(--accent); background: var(--accent-pale); }
.upload-zone input { display: none; }
.upload-icon { font-size: 36px; margin-bottom: 10px; }
.upload-text { font-size: 15px; font-weight: 600; color: var(--ink-2); margin-bottom: 4px; }
.upload-sub  { font-size: 13px; color: var(--ink-3); }

/* ── TOAST ── */
.toast {
  position: fixed; bottom: 20px; right: 20px;
  background: rgba(29,29,31,.92);
  backdrop-filter: blur(12px);
  color: #fff;
  padding: 11px 18px;
  border-radius: var(--r-md);
  font-size: 14px; font-weight: 500;
  opacity: 0; transform: translateY(8px);
  /* [FIX 2026-07] เดิม z-index:9999 ต่ำกว่า .modal-overlay (10000, มี backdrop-filter:blur(2px))
     และโมดัลพิเศษบางตัวในระบบสูงถึง 50000+ ทำให้ toast โผล่ "หลัง" ชั้น blur ของ modal ที่เปิดอยู่
     เห็นเป็นข้อความเบลอ อ่านไม่ออก — ใช้ 2147483647 (ค่าสูงสุดของ z-index) ให้ชนะทุก stacking
     context ในระบบเสมอ ไม่ว่าจะมี modal ซ้อนกี่ชั้นก็ตาม (toast เป็น pointer-events:none อยู่แล้ว
     จึงไม่บังการคลิกอะไรแม้จะทับภาพอื่นชั่วครู่) */
  transition: all .2s; z-index: 2147483647;
  pointer-events: none; max-width: 340px;
  box-shadow: var(--shadow-md);
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.ok   { background: rgba(52,199,89,.95); }
.toast::after {
  content: '';
  position: absolute;
  bottom: 0; left: 0;
  height: 3px;
  background: rgba(255,255,255,.4);
  border-radius: 0 0 var(--r-md) var(--r-md);
  animation: toast-progress 3s linear forwards;
}
@keyframes toast-progress { from { width: 100% } to { width: 0% } }
.toast.err  { background: rgba(255,59,48,.95); }

/* ── MODAL ── */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 10000; display: none; align-items: center; justify-content: center; backdrop-filter: blur(2px); }
.modal-overlay.show { display: flex; }
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  width: 500px; max-width: 95vw; max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,.20);
}
.modal-head {
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  color: var(--ink);
  padding: 15px 20px;
  font-size: 15px; font-weight: 600;
  display: flex; align-items: center; justify-content: space-between;
  border-radius: var(--r-xl) var(--r-xl) 0 0;
}
.modal-close {
  background: rgba(0,0,0,.06); border: none;
  color: var(--ink-3); font-size: 16px; cursor: pointer;
  width: 28px; height: 28px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  transition: background .12s;
}
.modal-close:hover { background: rgba(0,0,0,.12); color: var(--ink); }
.modal-body   { padding: 22px; }
.modal-footer {
  padding: 14px 22px; border-top: 1px solid var(--border);
  display: flex; justify-content: flex-end; gap: 10px;
  background: var(--surface-2);
  border-radius: 0 0 var(--r-xl) var(--r-xl);
}

.divider    { border: none; border-top: 1px solid var(--border); margin: 16px 0; }
.breadcrumb { display:flex; align-items:center; gap:5px; font-size:13.5px; color:var(--ink-3); flex-wrap:wrap; margin-bottom:4px; }
.breadcrumb-sep { color:var(--border); font-size:14px; }
.breadcrumb-link { background:none; border:none; color:var(--ink-3); cursor:pointer; font-size:13.5px; font-family:var(--font); font-weight:600; padding:0; transition:color .12s; }
.breadcrumb-link:hover { color:var(--ink); }
.breadcrumb-current { color:var(--ink); font-weight:700; }
.text-sm    { font-size: 13px; color: var(--ink-3); }
.flex       { display: flex; }
.flex-center { align-items: center; }
.gap-8      { gap: 8px; }
.gap-12     { gap: 12px; }
.mb-12      { margin-bottom: 12px; }
.hidden     { display: none !important; }

._uc-pick-item:hover { background: var(--accent-pale) !important; }

/* ── PR4 Wizard ── */
.steps {
  display: flex; padding: 8px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  gap: 0; flex-shrink: 0; align-items: center;
}
.step-pill {
  display: flex; align-items: center; gap: 6px;
  padding: 5px 14px; border-radius: 18px;
  font-size: 12px; font-weight: 500;
  cursor: pointer; color: var(--ink-3);
  transition: all .15s; border: none; background: transparent; font-family: var(--font);
}
.step-pill.active { background: var(--accent-light); color: var(--accent); font-weight: 600; }
.step-arrow { color: var(--ink-4); font-size: 14px; padding: 0 2px; flex-shrink: 0; }
.step-content { flex: 1; overflow-y: auto; padding: 18px 22px; }
.form-section-title { font-size: 14px; font-weight: 600; color: var(--ink); margin-bottom: 14px; padding-bottom: 8px; border-bottom: 1px solid var(--border); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; max-width: 720px; }
.form-group.full { grid-column: 1 / -1; }

.building-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-xl); margin-bottom: 14px; overflow: hidden; box-shadow: var(--shadow); }
.building-header { background: var(--surface-2); border-bottom: 1px solid var(--border); padding: 10px 16px; display: flex; align-items: center; justify-content: space-between; }
.building-name-input { background: transparent; border: none; border-bottom: 1px solid var(--border); color: var(--ink); font-size: 16px; font-weight: 600; outline: none; width: 220px; font-family: var(--font); padding: 2px 4px; }
.building-body { padding: 12px 16px; }
.cat-row { display: flex; align-items: center; gap: 8px; padding: 6px 10px; border: 1px solid var(--border); border-radius: var(--r-md); margin-bottom: 6px; background: var(--surface-2); }
.cat-seq { font-size: 11px; color: var(--accent); min-width: 28px; font-weight: 600; font-family: var(--font); }
.cat-input { flex: 1; padding: 5px 10px; border: 1px solid var(--border); border-radius: var(--r-sm); font-size: 14px; font-family: var(--font); outline: none; background: var(--surface); }

/* Custom Dropdown Fix */
.sel-wrap { position: relative; width: 100%; }
.sel-display {
  width: 100%; padding: 10px 34px 10px 12px;
  border: 1px solid var(--border); border-radius: var(--r-md);
  font-size: 13px; font-family: var(--font);
  outline: none;
  background: #fff url("data:image/svg+xml;charset=US-ASCII,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%2216%22%20height%3D%2216%22%20viewBox%3D%220%200%2016%2016%22%3E%3Cpath%20d%3D%22M8%2011L3%206h10z%22%20fill%3D%22%2386868b%22%2F%3E%3C%2Fsvg%3E") no-repeat right 10px center;
  cursor: pointer; text-align: left; white-space: normal; overflow: hidden;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  color: var(--ink); transition: all .12s;
}
.sel-display:hover  { border-color: #adadb3; }
.sel-display:focus  { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(0,122,255,.15); }
.placeholder { color: var(--ink-4); }

.sel-dropdown {
  display: none; position: fixed; z-index: 9999;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: 0 8px 30px rgba(0,0,0,.14);
  min-width: 320px; max-width: 480px;
  flex-direction: column;
}
.sel-dropdown.open    { display: flex; }
.sel-search-wrap      { padding: 8px 10px; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.sel-search           { width: 100%; padding: 7px 10px; border: 1px solid var(--border); border-radius: var(--r-md); font-size: 15px; font-family: var(--font); outline: none; }
.sel-search:focus     { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(0,122,255,.12); }
.sel-list             { overflow-y: auto; max-height: 240px; padding: 4px; }
.sel-group            { font-size: 12px; font-weight: 600; color: var(--ink-3); text-transform: uppercase; letter-spacing: .6px; padding: 6px 8px 3px; margin-top: 4px; }
.sel-opt              { padding: 7px 10px; font-size: 15px; border-radius: var(--r-sm); cursor: pointer; display: flex; justify-content: space-between; align-items: center; gap: 8px; color: var(--ink); }
.sel-opt:hover        { background: var(--accent-pale); }
.sel-opt-name         { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sel-opt-price        { font-family: var(--font); font-size: 13px; font-weight: 600; flex-shrink: 0; color: var(--accent); }
.sel-clear            { padding: 5px 10px; border-top: 1px solid var(--border); flex-shrink: 0; }
.sel-clear-btn        { font-size: 13px; color: var(--danger); background: none; border: none; cursor: pointer; font-family: var(--font); font-weight: 600; }

.shortcut-bar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 16px; padding: 12px 16px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-xl); align-items: center; box-shadow: var(--shadow); }
.sc-btn { display: flex; align-items: center; gap: 5px; padding: 6px 14px; border-radius: var(--r-md); font-size: 13px; font-weight: 500; cursor: pointer; font-family: var(--font); transition: all .12s; white-space: nowrap; border: 1px solid; }

.draft-banner { background: var(--warn-light); border: 1px solid #ffe0a0; border-radius: var(--r-md); padding: 10px 16px; margin-bottom: 16px; display: flex; align-items: center; justify-content: space-between; gap: 10px; }

.work-building { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-xl); overflow: hidden; margin-bottom: 14px; box-shadow: var(--shadow); }
.work-building-hdr { background: var(--surface-2); border-bottom: 1px solid var(--border); padding: 9px 16px; font-size: 14px; font-weight: 600; color: var(--ink); }
.work-cat       { border-top: 1px solid var(--border); }
.work-cat-hdr   { background: var(--accent-pale); padding: 7px 16px; display: flex; align-items: center; justify-content: space-between; }
.work-cat-label { font-size: 14px; font-weight: 600; color: var(--accent); }
.work-cat-seq   { font-size: 11px; color: var(--accent); font-family: var(--font); margin-right: 6px; }
.work-body      { padding: 12px 16px; display: flex; flex-direction: column; gap: 10px; }

.item-card      { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg); overflow: hidden; }
.item-card-head { background: var(--surface-2); padding: 9px 14px; display: flex; align-items: center; gap: 9px; border-bottom: 1px solid var(--border); }
.item-seq-badge { background: var(--accent); color: #fff; font-size: 13px; font-weight: 600; padding: 2px 9px; border-radius: 10px; flex-shrink: 0; font-family: var(--font); }
.item-name-input { flex: 1; padding: 6px 11px; border: 1px solid var(--border); border-radius: var(--r-md); font-size: 16px; font-family: var(--font); outline: none; background: var(--surface); }
.item-name-input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(0,122,255,.12); }
.item-card-body { padding: 12px 14px; display: flex; flex-direction: column; gap: 10px; }
.two-col        { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.section-hdr    { display: flex; align-items: center; justify-content: space-between; margin-bottom: 5px; }
.section-label  { font-size: 11px; font-weight: 600; color: var(--ink-3); text-transform: uppercase; letter-spacing: .4px; display: flex; align-items: center; gap: 5px; }
.ml-table       { width: 100%; border-collapse: collapse; font-size: 15px; }
.c-sel  { width: 100%; padding: 5px 7px; border: none; font-size: 12px; font-family: var(--font); outline: none; background: transparent; }
.c-num  { width: 100%; padding: 5px 7px; border: none; font-size: 13px; font-family: var(--font); outline: none; background: transparent; text-align: right; }
.c-txt  { width: 100%; padding: 5px 7px; border: none; font-size: 13px; font-family: var(--font); outline: none; background: transparent; }
.c-sum  { padding: 5px 8px; text-align: right; font-size: 12px; font-weight: 700; white-space: nowrap; font-family: var(--font); color: var(--accent); }
.c-del  { padding: 3px 6px; text-align: center; cursor: pointer; color: var(--ink-4); border-radius: 4px; transition: background .12s; }
.c-del:hover { background: var(--danger-light); color: var(--danger); }

.total-box    { background: var(--accent-pale); border: 1px solid rgba(0,122,255,.2); border-radius: var(--r-md); padding: 9px 14px; display: flex; align-items: center; justify-content: space-between; }
.total-label  { font-size: 12px; color: var(--accent); font-weight: 600; text-transform: uppercase; letter-spacing: .4px; }
.total-value  { font-size: 16px; font-weight: 700; color: var(--accent); font-family: var(--font); }
.no-items     { text-align: center; padding: 10px; color: var(--ink-4); font-size: 11px; font-style: italic; }

.statusbar    { background: var(--surface-2); border-top: 1px solid var(--border); color: var(--ink-3); font-size: 12px; padding: 4px 16px; display: flex; gap: 18px; flex-shrink: 0; align-items: center; }
.btn-xs       { padding: 3px 8px; font-size: 12px; }
.form-hint    { font-size: 11px; color: var(--ink-4); margin-top: 2px; }
.status-dot   { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--success); margin-right: 5px; vertical-align: middle; }
.btn-success  { background: var(--success); color: #fff; }

/* ── Notification ── */
.notif-bell  { position: relative; display: inline-flex; align-items: center; }
.notif-badge { position: absolute; top: -4px; right: -6px; background: var(--danger); color: #fff; font-size: 9px; font-weight: 700; min-width: 15px; height: 15px; border-radius: 8px; display: flex; align-items: center; justify-content: center; padding: 0 3px; border: 2px solid var(--bg); }
@keyframes badge-pulse { from { transform: scale(1) } to { transform: scale(1.12) } }

.notif-panel {
  position: fixed; top: 46px; right: 12px;
  width: 340px; max-height: 450px;
  background: rgba(255,255,255,.95);
  backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  box-shadow: 0 10px 40px rgba(0,0,0,.14);
  z-index: 9999; overflow: hidden;
  display: none; flex-direction: column;
}
.notif-panel.open          { display: flex; }
.notif-panel-head          { padding: 11px 16px; background: var(--surface-2); border-bottom: 1px solid var(--border); color: var(--ink); font-size: 14px; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.notif-list                { overflow-y: auto; flex: 1; }
.notif-item                { padding: 10px 16px; border-bottom: 1px solid var(--border); cursor: pointer; transition: background .1s; }
.notif-item:hover          { background: var(--surface-2); }
.notif-item.unread         { background: var(--accent-pale); border-left: 3px solid var(--accent); }
.notif-item-title          { font-size: 13px; font-weight: 600; color: var(--ink); margin-bottom: 2px; }
.notif-item-body           { font-size: 12px; color: var(--ink-3); line-height: 1.5; }
.notif-item-time           { font-size: 11px; color: var(--ink-4); margin-top: 3px; }
.notif-actions             { display: flex; gap: 6px; margin-top: 6px; }

/* ── Upload Sec Heads ── */
.upload-sec-head      { display: flex; align-items: center; gap: 8px; font-size: 13px; font-weight: 600; color: var(--ink); padding: 10px 0 8px; border-bottom: 1px solid var(--border); margin: 18px 0 12px; }
.upload-sec-head:first-of-type { margin-top: 0; }
.upchip               { font-size: 10px; padding: 2px 8px; border-radius: 8px; font-weight: 600; flex-shrink: 0; }
.upchip-build         { background: var(--accent-light); color: #0051a8; border: 1px solid rgba(0,122,255,.2); }
.upchip-road          { background: #fff0f0; color: #c0392b; border: 1px solid #ffd0ce; }
.upchip-irr           { background: #e0f4ff; color: #075985; border: 1px solid #b3e5fc; }
.upchip-ff            { background: var(--success-light); color: #065f46; border: 1px solid #a7f3d0; }
.upchip-adm           { background: #f5f3ff; color: #5b21b6; border: 1px solid #ddd6fe; }

/* ── Formula Library ── */
.flib-card  { border: 1px solid var(--border); border-radius: var(--r-md); margin-bottom: 8px; overflow: hidden; }
.flib-head  { display: flex; align-items: center; gap: 8px; padding: 8px 14px; background: var(--surface); border-bottom: 1px solid var(--border); font-size: 13px; }
.scope-badge    { font-size: 10px; font-weight: 600; padding: 2px 8px; border-radius: 8px; }
.scope-private  { background: #f1f5f9; color: #475569; }
.scope-pending  { background: #fffbeb; color: #d97706; border: 1px solid #fde68a; }
.scope-approved { background: var(--success-light); color: #065f46; border: 1px solid #a7f3d0; }
.scope-rejected { background: var(--danger-light); color: var(--danger); border: 1px solid #ffd0ce; }

/* ── Step Builder ── */
.step-builder-wrap  { border: 1px solid #ddd6fe; border-radius: var(--r-md); overflow: hidden; margin: 8px 0; }
.step-builder-head  { background: #7c3aed; color: #fff; padding: 8px 14px; font-size: 13px; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.step-row-b         { display: grid; grid-template-columns: 32px 1fr; gap: 10px; align-items: start; padding: 12px 14px; border-bottom: 1px solid #ede9fe; font-size: 12px; background: #fdfcff; }
.step-row-b:nth-child(even) { background: #faf8ff; }
.step-row-b:last-child { border-bottom: none; }
.step-n-b           { width: 26px; height: 26px; border-radius: 50%; background: #7c3aed; color: #fff; font-size: 12px; font-weight: 700; display: flex; align-items: center; justify-content: center; flex-shrink: 0; margin-top: 2px; }
.step-fields        { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 12px; }
.step-field         { display: flex; flex-direction: column; gap: 3px; }
.step-field.full    { grid-column: 1 / -1; }
.step-field-label   { font-size: 10px; font-weight: 600; color: #7c5cbf; letter-spacing: .02em; }
.step-row-b .form-input, .step-row-b select { width: 100%; box-sizing: border-box; }
.step-del-btn       { background: var(--danger-light); border: 1px solid #ffd0ce; border-radius: var(--r-sm); color: var(--danger); cursor: pointer; font-size: 12px; padding: 3px 10px; font-weight: 600; }

.calc-step-row        { display: grid; grid-template-columns: 26px 1fr 130px 80px; gap: 8px; align-items: center; padding: 6px 10px; border-bottom: 1px solid var(--border); font-size: 12px; }
.calc-step-row.result-r { background: #f5f3ff; border-radius: var(--r-sm); padding: 8px 10px; border: none; margin: 4px 0; }
.oil-tag   { font-size: 9px; background: #fff7ed; color: #c2410c; padding: 1px 5px; border-radius: 5px; display: inline-block; }
.admin-tag { font-size: 9px; background: var(--success-light); color: #065f46; padding: 1px 5px; border-radius: 5px; display: inline-block; }
.user-tag  { font-size: 9px; background: var(--danger-light); color: var(--danger); padding: 1px 5px; border-radius: 5px; display: inline-block; }
.override-note { background: #fffbeb; border: 1px solid #fde68a; border-radius: var(--r-sm); padding: 10px 14px; font-size: 11px; color: #92400e; margin-top: 8px; display: none; }
.override-note.visible { display: block; }

/* ── Sheet Editor ── */
.sheet-cell:focus       { background: var(--accent-pale) !important; box-shadow: inset 0 0 0 2px var(--accent) !important; z-index: 1; position: relative; }
.sheet-cell:hover:not(:focus) { background: #f0fff4 !important; }
#sheetEditorBody tr:hover td  { background: var(--surface-2); }

/* ── Responsive Mobile ── */
.hamburger-btn {
  display: flex; background: rgba(0,0,0,.06); border: none;
  color: var(--ink); font-size: 18px; cursor: pointer;
  width: 36px; height: 36px; border-radius: var(--r-sm);
  align-items: center; justify-content: center; flex-shrink: 0; transition: background .12s;
}
.hamburger-btn:hover { background: rgba(0,0,0,.12); }
.sidebar-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 199; }
.sidebar-overlay.active { display: block; }
/* Desktop sidebar collapsed state */
.sidebar.collapsed {
  width: 0;
  overflow: hidden;
  padding: 0;
  border: none;
  min-width: 0;
  flex-shrink: 0;
  transition: width .22s ease, padding .22s ease;
}
.sidebar { transition: width .22s ease; }

/* ── Select Modal Split Panel ── */
.sel-modal-body  { display: flex; flex: 1; overflow: hidden; min-height: 0; }
.sel-panel-list  { flex: 1; overflow-y: auto; border-right: 1px solid var(--border); }
.sel-panel-cart  { width: 280px; overflow-y: auto; padding: 10px; background: var(--accent-pale); flex-shrink: 0; }

@media (max-width: 768px) {
  .hamburger-btn  { display: flex; }
  .topbar         { padding: 0 14px; height: 50px; }
  .topbar-sub     { display: none; }
  #topUserName    { display: none; }
  .user-pill      { padding: 4px 10px; gap: 5px; }
  .topbar-title   { font-size: 15px; }
  .main-layout    { height: calc(100vh - 50px); overflow: hidden; }
  .sidebar {
    position: fixed; top: 50px; left: -280px;
    height: calc(100vh - 50px); width: 240px;
    z-index: 200; transition: left .22s ease;
    box-shadow: 4px 0 20px rgba(0,0,0,.15);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding-bottom: 40px;
  }
  .sidebar.open { left: 0; }
  .content      { width: 100%; max-width: 100vw; padding: 16px 14px; overflow-x: clip; overflow-y: auto; box-sizing: border-box; }
  .tbl-wrap     { overflow-x: auto; -webkit-overflow-scrolling: touch; width: 100%; }
  .form-row, .form-grid, .two-col { grid-template-columns: 1fr !important; }
  .form-group.full { grid-column: 1 !important; }
  .steps        { overflow-x: auto; flex-wrap: nowrap; gap: 0; padding: 7px 12px; scrollbar-width: none; }
  .steps::-webkit-scrollbar { display: none; }
  .step-pill    { white-space: nowrap; padding: 5px 12px; font-size: 11px; }
  table         { min-width: 520px; }
  .step-fields  { grid-template-columns: 1fr; }
  .calc-step-row { grid-template-columns: 26px 1fr; grid-template-rows: auto auto; row-gap: 4px; }
  .calc-step-row > *:nth-child(n+3) { grid-column: 2; }
  .card-body    { padding: 14px; }
  .modal        { width: 100% !important; max-width: 100vw !important; border-radius: var(--r-xl) var(--r-xl) 0 0 !important; position: fixed; bottom: 0; max-height: 92vh; }
  .modal-overlay { align-items: flex-end !important; }
  .page-title   { font-size: 20px; }
  .shortcut-bar { flex-wrap: wrap; gap: 6px; padding: 10px 12px; }
  .building-name-input { width: 160px; }
  .topbar-right { gap: 6px; }
  .building-body, .work-body, .item-card-body { overflow-x: auto; }
  .ml-table     { min-width: 480px; }
  table         { font-size: 15px; }
}

@media (max-width: 480px) {
  .topbar-title { font-size: 13px; }
  .role-badge   { display: none; }
  .content      { padding: 12px 10px; }
  .page-title   { font-size: 17px; }
}

/* ═══════════════════════════════════════════════════════
   MOBILE UX — portrait phone (≤ 600px)
   ═══════════════════════════════════════════════════════ */
@media (max-width: 600px) and (orientation: portrait),
       (max-width: 600px) {

  :root {
    --font-base: 17px;
    --font-sm:   15px;
    --font-xs:   14px;
    --tap: 48px;
  }

  body { font-size: var(--font-base); }
  .topbar { overflow: hidden; }
  .topbar-right { flex-shrink: 0; gap: 8px; }
  .topbar { height: 52px; padding: 0 12px; gap: 8px; }
  .topbar-left  { gap: 8px; overflow: hidden; }
  .topbar-title { font-size: 14px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 140px; }
  .topbar-sub   { display: none; }
  #topUserName  { display: none; }
  .role-badge   { display: none; }
  /* user-pill บนมือถือ: แสดงเป็นปุ่มกลม tap-friendly */
  .user-pill    { padding: 6px 10px; gap: 4px; background: rgba(0,0,0,.06); border: 1.5px solid rgba(0,0,0,.08); border-radius: 10px; min-width: 44px; min-height: 44px; display: flex; align-items: center; justify-content: center; box-shadow: none; }
  .user-pill::before { content: '👤'; font-size: 20px; }
  .user-dot     { display: none; }
  #userMenu     { right: 8px !important; min-width: 220px !important; max-width: 90vw; }
  .hamburger-btn { width: var(--tap); height: var(--tap); font-size: 20px; }
  .topbar-right { gap: 6px; }
  .main-layout  { height: calc(100vh - 52px); }
  .sidebar      { top: 52px; height: calc(100vh - 52px); }
  .sidebar      { width: 80vw; max-width: 300px; }

  .nav-item     { padding: 14px 20px; font-size: 16px; gap: 12px; border-left-width: 4px; }
  .nav-item .nav-icon { font-size: 20px; width: 26px; }
  .sidebar-sec  { font-size: 12px; padding: 14px 20px 6px; }

  .content      { padding: 16px 14px; }
  .page-title   { font-size: 20px; gap: 8px; }
  .page-sub     { font-size: var(--font-sm); margin-top: 5px; }

  .btn    { min-height: var(--tap); padding: 12px 20px; font-size: 15px; }
  .btn-sm { min-height: 40px; padding: 9px 16px; font-size: 14px; }
  .btn-lg { min-height: 52px; padding: 14px 28px; font-size: 17px; }
  .btn-xs { min-height: 36px; padding: 7px 12px; font-size: 13px; }

  .form-label  { font-size: var(--font-sm); margin-bottom: 6px; }
  .form-input,
  .form-select { padding: 13px 14px; font-size: 16px; }
  .form-hint   { font-size: 12px; }
  .form-row, .form-grid, .two-col { grid-template-columns: 1fr !important; gap: 12px; }
  .form-group.full { grid-column: 1 !important; }

  .card-head { padding: 13px 16px; font-size: 15px; }
  .card-body { padding: 16px; }

  .tbl-wrap     { overflow-x: auto; -webkit-overflow-scrolling: touch; border-radius: var(--r-md); }
  table         { min-width: 500px; font-size: 14px; }
  thead th      { padding: 12px 12px; font-size: 13px; white-space: nowrap; }
  td            { padding: 11px 12px; }
  .td-actions   { gap: 8px; flex-wrap: wrap; }

  .modal-overlay:not(#modalMatSelect):not(#modalLabSelect):not(#modalEqSelect):not(#modalPR5):not([style*="align-items:stretch"]) { align-items: flex-end !important; }
  .modal-overlay:not(#modalMatSelect):not(#modalLabSelect):not(#modalEqSelect):not(#modalPR5):not([style*="align-items:stretch"]) > .modal { width: 100% !important; max-width: 100vw !important; border-radius: 20px 20px 0 0 !important; position: fixed; bottom: 0; max-height: 92vh; }
  .modal-overlay[style*="align-items:stretch"] > .modal { width: 95vw !important; max-width: 860px !important; max-height: 90vh !important; border-radius: 14px !important; position: relative; bottom: auto; margin: auto; }
  .modal-head   { padding: 16px 18px; font-size: 16px; border-radius: 20px 20px 0 0; }
  .modal-body   { padding: 20px 16px; font-size: var(--font-base); }
  .modal-footer { padding: 16px; gap: 10px; }
  .modal-footer .btn { flex: 1; justify-content: center; }

  .toast { bottom: 16px; top: auto; left: auto; right: 12px; max-width: 280px; font-size: 13px; padding: 9px 14px; border-radius: var(--r-md); }

  .proj-grid       { grid-template-columns: 1fr; gap: 12px; }
  .proj-card       { padding: 16px; }
  .proj-card-title { font-size: 15px; }
  .proj-card-meta  { font-size: 13px; }

  .steps { padding: 8px 12px; gap: 0; overflow-x: auto; flex-wrap: nowrap; scrollbar-width: none; }
  .steps::-webkit-scrollbar { display: none; }
  .step-pill  { white-space: nowrap; padding: 8px 14px; font-size: 13px; }
  .step-arrow { font-size: 14px; }
  .step-content { padding: 16px; }
  .form-section-title { font-size: 15px; }

  .item-card-head   { padding: 12px 14px; gap: 10px; }
  .item-name-input  { font-size: 15px; padding: 8px 11px; }
  .item-seq-badge   { font-size: 12px; padding: 3px 10px; }
  .item-card-body   { padding: 14px; overflow-x: auto; }
  .ml-table         { min-width: 460px; font-size: 15px; }

  .shortcut-bar { flex-wrap: wrap; gap: 8px; padding: 12px; }
  .sc-btn       { min-height: 40px; padding: 8px 14px; font-size: 13px; }

  .building-header   { padding: 12px 14px; }
  .building-name-input { font-size: 15px; width: 180px; }
  .building-body, .work-body, .item-card-body { overflow-x: auto; }
  .work-cat-hdr     { padding: 10px 14px; }
  .work-cat-label   { font-size: 14px; }
  .work-building-hdr { padding: 10px 14px; font-size: 14px; }

  .sel-display    { padding: 13px 36px 13px 13px; font-size: 16px; }
  .sel-dropdown   { min-width: 90vw; max-width: 95vw; left: 50% !important; transform: translateX(-50%); }
  .sel-opt        { padding: 10px 12px; font-size: 14px; min-height: 44px; }
  .sel-opt-name   { font-size: 14px; white-space: normal; }
  .sel-opt-price  { font-size: 13px; }
  .sel-search     { padding: 10px; font-size: 15px; }
  .sel-list       { max-height: 280px; }

  .notif-panel { top: 56px; right: 0; left: 0; width: 100%; max-height: 75vh; border-radius: 0 0 var(--r-lg) var(--r-lg); }
  .notif-item       { padding: 14px 16px; }
  .notif-item-title { font-size: 14px; }
  .notif-item-body  { font-size: 13px; }

  .upload-zone { padding: 28px 20px; }
  .upload-text { font-size: 16px; }
  .upload-sub  { font-size: 14px; }

  .badge       { font-size: 12px; padding: 4px 11px; }
  .total-value { font-size: 18px; }
  .total-box   { padding: 11px 14px; }

  .tbl-wrap table { table-layout: auto; }
  .tbl-2col { display: block; }
  .tbl-2col thead,
  .tbl-2col tbody,
  .tbl-2col tr { display: block; }
  .tbl-2col thead { display: none; }
  .tbl-2col td { display: flex; justify-content: space-between; align-items: center; padding: 9px 12px; border-bottom: 1px solid var(--border); font-size: 14px; gap: 8px; min-height: 44px; }
  .tbl-2col td[data-label]::before { content: attr(data-label); font-size: 12px; color: var(--ink-3); font-weight: 600; flex-shrink: 0; width: 40%; }
  .tbl-2col tr { border: 1px solid var(--border); border-radius: var(--r-md); margin-bottom: 10px; overflow: hidden; }

  .sel-modal-body  { flex-direction: column !important; overflow: hidden; height: 100%; }
  .sel-panel-list  { flex: 0 0 55% !important; border-right: none !important; border-bottom: 2px solid var(--border); overflow-y: auto; -webkit-overflow-scrolling: touch; }
  .sel-panel-cart  { width: 100% !important; flex: 0 0 45% !important; overflow-y: auto; -webkit-overflow-scrolling: touch; border-top: 2px solid var(--accent); }

  #modalMatSelect, #modalLabSelect, #modalEqSelect { padding: 0 !important; align-items: stretch !important; }
  #modalMatSelect > .modal, #modalLabSelect > .modal, #modalEqSelect > .modal { width: 100% !important; max-width: 100vw !important; max-height: 100vh !important; border-radius: 0 !important; height: 100vh; }

  .sidebar { left: -100vw !important; width: 80vw !important; box-shadow: none; overflow-y: auto !important; }
  /* เมื่อเปิดบนมือถือ: ลอยเต็มความสูงจอ ทับทุกอย่าง ไม่ผูกกับ topbar (กันโดนทับ/ไม่แสดง) */
  .sidebar.open {
    left: 0 !important;
    top: 0 !important;
    height: 100vh !important;
    height: 100dvh !important;
    padding-top: calc(env(safe-area-inset-top, 0px) + 12px) !important;
    z-index: 600 !important;
    box-shadow: 4px 0 24px rgba(0,0,0,.2) !important;
  }
  .sidebar-overlay { z-index: 599 !important; }
  .sidebar-overlay.active { display: block !important; }

  .td-actions .btn,
  .td-actions button { min-height: 40px; min-width: 40px; }

  .flib-head { flex-direction: column; align-items: flex-start; gap: 10px; padding: 14px 14px 12px; }
  .flib-head > span:first-child { font-size: 22px; }
  .flib-actions { flex-direction: column !important; align-items: stretch !important; width: 100% !important; gap: 8px !important; justify-content: flex-start !important; }
  .flib-actions > .scope-badge, .flib-actions > span { display: inline-flex; }
  .flib-actions > button, .flib-actions > div > button { width: 100% !important; min-height: 44px !important; font-size: 14px !important; padding: 10px 14px !important; justify-content: center !important; text-align: center !important; border-radius: 10px !important; display: flex !important; align-items: center !important; }
  .flib-actions > div { display: flex; flex-direction: column; gap: 8px; width: 100%; }

  .logout-text { display: none; }
  .btn-logout-top { padding: 0; width: var(--tap); height: var(--tap); justify-content: center; border-radius: 50% !important; }
  .logout-icon { font-size: 20px; }
}

@media (max-width: 600px) {
  ._pd [style*="grid-template-columns:repeat(3"] {
    grid-template-columns: repeat(2, 1fr) !important;
  }
}

/* ── Keyboard shortcut tooltip ── */
[title] { cursor: pointer; }
.kbd-hint { display:inline-flex; align-items:center; gap:3px; font-size:10px; color:var(--ink-4); }
kbd {
  display:inline-block; padding:1px 5px;
  background:var(--surface-2); border:1px solid var(--border);
  border-radius:4px; font-size:10px; font-family:var(--font);
  color:var(--ink-3); box-shadow:0 1px 0 var(--border);
}

/* ═══════════════════════════════════════════════════
   UX/UI IMPROVEMENT — Low Risk Layer
   ═══════════════════════════════════════════════════ */

/* ── Loading Overlay (TASK 1) ─────────────────────── */
.app-loading-overlay {
  position: fixed; inset: 0; z-index: 20000;
  background: rgba(255,255,255,.72); backdrop-filter: blur(3px);
  display: none; align-items: center; justify-content: center;
}
.app-loading-overlay.show { display: flex; }
.app-loading-box {
  background: #fff; border: 1px solid #e2e8f0; border-radius: 14px;
  padding: 22px 32px; display: flex; align-items: center; gap: 16px;
  box-shadow: 0 12px 32px rgba(0,0,0,.18);
}
.app-loading-spinner {
  width: 32px; height: 32px; border-radius: 50%;
  border: 3px solid #e2e8f0; border-top-color: #16a34a;
  animation: app-spin .8s linear infinite; flex-shrink: 0;
}
@keyframes app-spin { to { transform: rotate(360deg) } }
.app-loading-text {
  font-family: var(--font-th); font-size: 14px; font-weight: 600; color: #0f172a;
}

/* ── Search Box (TASK 2) ─────────────────────────── */
.app-search-box {
  position: relative; display: inline-flex; align-items: center;
  background: #fff; border: 1.5px solid #e2e8f0; border-radius: 10px;
  padding: 7px 12px 7px 34px; font-family: var(--font-th);
  font-size: 13px; min-width: 220px; transition: border-color .12s;
}
.app-search-box:focus-within { border-color: #16a34a; box-shadow: 0 0 0 3px rgba(22,163,74,.12); }
.app-search-box input {
  border: none; outline: none; background: transparent; flex: 1;
  font-family: inherit; font-size: inherit; color: #0f172a;
}
.app-search-box::before {
  content: "🔍"; position: absolute; left: 10px; font-size: 14px; opacity: .55;
}
.app-search-meta { font-size: 11px; color: #94a3b8; margin-left: 10px; font-weight: 600; }
tr.app-search-hidden { display: none !important; }

/* ── Sticky Table Header & Action Bar (TASK 5) ─────── */
.app-table-sticky thead th {
  position: sticky; top: 0; z-index: 5;
  background: #f8fafc; border-bottom: 1.5px solid #e2e8f0;
}
.app-action-bar-sticky {
  position: sticky; top: 0; z-index: 6;
  background: #fff; padding: 10px 0; border-bottom: 1px solid #f1f5f9;
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  margin-bottom: 12px;
}
.app-action-bar-sticky .item-count {
  font-size: 12px; font-weight: 600; color: #64748b; margin-left: auto;
}

/* ── Validation Feedback (TASK 6) ─────────────────── */
.input-state-ok    { border-color: #16a34a !important; background: #f0fdf4 !important; }
.input-state-warn  { border-color: #d97706 !important; background: #fffbeb !important; }
.input-state-error { border-color: #dc2626 !important; background: #fef2f2 !important; }
.input-state-msg {
  font-size: 11px; margin-top: 4px; font-family: var(--font-th);
  display: flex; align-items: center; gap: 4px;
}
.input-state-msg.ok    { color: #16a34a; }
.input-state-msg.warn  { color: #d97706; }
.input-state-msg.error { color: #dc2626; }

/* ── Save Indicator (TASK 8) ──────────────────────── */
.app-save-indicator {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 11.5px; font-weight: 600; font-family: var(--font-th);
  padding: 5px 11px; border-radius: 20px;
  background: #f1f5f9; color: #64748b;
  border: 1px solid #e2e8f0; transition: all .2s;
  white-space: nowrap;
}
.app-save-indicator.dirty    { background: #fef3c7; color: #92400e; border-color: #fde68a; }
.app-save-indicator.saving   { background: #dbeafe; color: #1e40af; border-color: #bfdbfe; }
.app-save-indicator.saved    { background: #dcfce7; color: #166534; border-color: #bbf7d0; }
.app-save-indicator .dot     { width: 7px; height: 7px; border-radius: 50%; background: currentColor; opacity: .7; flex-shrink: 0; }
.app-save-indicator.saving .dot { animation: app-pulse 1.2s infinite; }
@keyframes app-pulse { 0%,100% { opacity: .35 } 50% { opacity: 1 } }

/* ── Workflow Card (TASK 4) ───────────────────────── */
.workflow-card {
  background: linear-gradient(135deg, #f0f9ff 0%, #ecfdf5 100%);
  border: 1.5px solid #bae6fd; border-radius: 16px;
  padding: 18px 22px; margin-bottom: 20px;
}
.workflow-card-title {
  font-size: 13px; font-weight: 700; color: #0c4a6e;
  text-transform: uppercase; letter-spacing: .08em; margin-bottom: 14px;
  display: flex; align-items: center; gap: 7px;
}
.workflow-steps {
  display: flex; gap: 6px; align-items: stretch; overflow-x: auto;
  padding-bottom: 4px; scrollbar-width: thin;
}
.workflow-step {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 12px 14px; background: #fff; border-radius: 11px;
  border: 1.5px solid #e2e8f0; min-width: 96px; flex-shrink: 0;
  transition: all .15s; text-align: center;
}
.workflow-step .num {
  width: 24px; height: 24px; border-radius: 50%;
  background: #f1f5f9; color: #64748b;
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 800; font-family: var(--font-th);
}
.workflow-step .ic    { font-size: 20px; }
.workflow-step .lbl   { font-size: 11.5px; font-weight: 700; color: #334155; font-family: var(--font-th); }
.workflow-step.done   { border-color: #86efac; background: #f0fdf4; }
.workflow-step.done .num { background: #16a34a; color: #fff; }
.workflow-step.active { border-color: #0ea5e9; background: #f0f9ff; box-shadow: 0 4px 12px rgba(14,165,233,.18); }
.workflow-step.active .num { background: #0ea5e9; color: #fff; }
.workflow-step-arrow {
  display: flex; align-items: center; color: #cbd5e1; font-size: 16px;
  flex-shrink: 0; padding: 0 2px;
}

/* ── Sidebar Group Section (TASK 3) ───────────────── */
.sidebar-group {
  border-bottom: 1px solid #f1f5f9; padding-bottom: 6px; margin-bottom: 4px;
}
.sidebar-group:last-of-type { border-bottom: none; }
.nav-item.nav-soft-link {
  opacity: .85; font-style: italic;
}
.nav-item.nav-soft-link::after {
  content: "↗"; font-size: 10px; margin-left: 4px; opacity: .55;
}

/* ═══════════════════════════════════════════════════
   MOBILE PERFORMANCE — ปิด GPU-heavy effects บน mobile
   backdrop-filter: blur() ใช้ GPU มาก บน Android mid-range
   ทำให้ scroll/animation กระตุก → ใช้ solid bg แทน
   ═══════════════════════════════════════════════════ */

/* [PERF] touch-action: manipulation — กำจัด 300ms tap delay บน mobile
   ไม่กระทบ scroll gesture (manipulation = tap+drag ok, double-tap-zoom ไม่ ok)
   ใส่ปุ่มทั่วไปทุกประเภทในระบบ */
button,
.btn, .btn-primary, .btn-secondary, .btn-danger,
.gt-btn, .gt-ib, .gt-scale-btn,
.nav-item, .nav-btn,
[role="button"],
.form-select, select {
  touch-action: manipulation;
}
@media (max-width: 768px) {
  /* Topbar */
  .topbar {
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    background: rgba(246,246,248,0.98);
  }
  /* Toast notification */
  .toast {
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    background: rgba(29,29,31,0.96);
  }
  /* Modal overlay */
  .modal-overlay {
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
  }
  /* Notification panel */
  .notif-panel {
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    background: rgba(255,255,255,0.99);
  }
  /* Loading overlay */
  .app-loading-overlay {
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    background: rgba(255,255,255,0.90);
  }
}

/* ═══════════════════════════════════════════════════════════
   STICKY TABLE HEADERS — เพิ่ม class "table-sticky" ที่ <table>
   ─────────────────────────────────────────────────────────── */
/* ใช้กับ scroll container ที่มี <table class="table-sticky"> ข้างใน */
.table-sticky thead th {
  position: sticky;
  top: 0;
  z-index: 10;
  /* background ต้องทึบ ไม่งั้นเนื้อหาข้างหลังจะทะลุผ่าน */
  background: var(--brand, #1b5e20);
  color: #fff;
}
/* ถ้า table อยู่ใน scroll container ที่มี border-radius — ตัด z-index ของ header
   ไม่ให้ overlap shadow ของ container */
.table-sticky thead th:first-child { border-top-left-radius: inherit; }
.table-sticky thead th:last-child  { border-top-right-radius: inherit; }

/* tbody hover เน้นแถวเมื่อ user เลื่อน mouse (UX) */
.table-sticky tbody tr:hover {
  background: rgba(27, 94, 32, 0.04);
}

/* mobile: ลด padding ของ sticky header เพื่อให้ข้อความไม่ตัด */
@media (max-width: 768px) {
  .table-sticky thead th {
    padding: 6px 8px !important;
    font-size: 11px;
  }
}

/* ═══════════════════════════════════════════════════════════
   PR4 FULLSCREEN MODALS — mobile UX
   ─────────────────────────────────────────────────────────── */

/* ทุกหน้าจอ: PR4 modals ใช้ align-items:stretch อยู่แล้ว
   เพิ่ม safe-area inset สำหรับ iPhone notch/home indicator */
#modalPR4Editor > div,
#modalPR4EquipEditor > div,
#modalPR4Road > div,
#modalPR4Irr > div,
#modalPR4RoadDoc > div,
#modalPR4IrrDoc > div {
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

/* steps bar ใน PR4 modals — scroll แนวนอน ไม่ wrap บนมือถือ */
#modalPR4Editor .steps,
#modalPR4EquipEditor .steps,
#modalPR4Road .steps,
#modalPR4Irr .steps,
#modalPR4RoadDoc .steps,
#modalPR4IrrDoc .steps {
  overflow-x: auto;
  flex-wrap: nowrap;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}
#modalPR4Editor .steps::-webkit-scrollbar,
#modalPR4EquipEditor .steps::-webkit-scrollbar,
#modalPR4Road .steps::-webkit-scrollbar,
#modalPR4Irr .steps::-webkit-scrollbar,
#modalPR4RoadDoc .steps::-webkit-scrollbar,
#modalPR4IrrDoc .steps::-webkit-scrollbar { display: none; }

@media (max-width: 600px) {
  /* header bar ใน PR4 modals — ลด font และ layout compact */
  #modalPR4Editor > div > div:first-child,
  #modalPR4EquipEditor > div > div:first-child,
  #modalPR4Road > div > div:first-child,
  #modalPR4Irr > div > div:first-child,
  #modalPR4RoadDoc > div > div:first-child,
  #modalPR4IrrDoc > div > div:first-child {
    height: 48px !important;
    padding: 0 14px !important;
  }
  #modalPR4Editor > div > div:first-child span:first-child,
  #modalPR4EquipEditor > div > div:first-child span:first-child,
  #modalPR4Road > div > div:first-child span:first-child,
  #modalPR4Irr > div > div:first-child span:first-child,
  #modalPR4RoadDoc > div > div:first-child span:first-child,
  #modalPR4IrrDoc > div > div:first-child span:first-child {
    font-size: 14px !important;
    /* ตัดข้อความยาวไม่ให้ดัน layout */
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 55vw;
  }
  /* step-pills เล็กลงบนมือถือ */
  #modalPR4Editor .step-pill,
  #modalPR4EquipEditor .step-pill,
  #modalPR4Road .step-pill,
  #modalPR4Irr .step-pill,
  #modalPR4RoadDoc .step-pill,
  #modalPR4IrrDoc .step-pill {
    font-size: 11px !important;
    padding: 5px 10px !important;
    white-space: nowrap;
  }
  /* content area — full scroll ไม่มีขอบ padding เกิน */
  #pr4StepContent,
  #pr4eqStepContent,
  #pr4RoadContent,
  #pr4IrrContent,
  #pr4RoadDocContent,
  #pr4IrrDocContent {
    padding: 14px !important;
  }
  /* generate bar ปุ่มเต็มความกว้าง */
  #pr4GenerateBar,
  #pr4EquipGenBar {
    flex-direction: column !important;
    padding: 12px 14px !important;
  }
  #pr4GenerateBar .btn,
  #pr4EquipGenBar .btn {
    width: 100% !important;
    justify-content: center !important;
  }
  /* status bar ซ่อน (มีพื้นที่น้อย) */
  #modalPR4Editor > div > div:last-child,
  #modalPR4EquipEditor > div > div:last-child {
    display: none !important;
  }
}

/* ═══════════════════════════════════════════════════════
   LARGE SCREEN — จอ 19" ขึ้นไป (≥ 1440px)
   bump ตัวอักษรและ spacing ทั้งระบบ ~12-14%
   ═══════════════════════════════════════════════════════ */
@media (min-width: 1440px) {
  :root {
    --r-sm: 9px;
    --r-md: 13px;
    --r-lg: 18px;
    --r-xl: 22px;
  }

  body { font-size: 17px; }

  /* Topbar */
  .topbar        { height: 56px; padding: 0 32px; }
  .topbar-title  { font-size: 19px; }
  .topbar-sub    { font-size: 13px; }

  /* Sidebar nav */
  .sidebar       { width: 240px; }
  .nav-item      { font-size: 16px; padding: 11px 22px; }
  .nav-item .nav-icon { font-size: 19px; }
  .sidebar-sec   { font-size: 12px; padding: 16px 22px 6px; }

  /* Page content */
  .content       { padding: 28px 32px; }
  .page-title    { font-size: 25px; }
  .page-sub      { font-size: 15px; }

  /* Cards */
  .card-head     { padding: 16px 22px; font-size: 17px; }
  .card-body     { padding: 22px; }

  /* Buttons */
  .btn           { padding: 11px 22px; font-size: 16px; border-radius: 10px; }
  .btn-sm        { padding: 7px 16px; font-size: 15px; }
  .btn-lg        { padding: 14px 32px; font-size: 17px; }

  /* Forms */
  .form-label    { font-size: 15px; margin-bottom: 6px; }
  .form-input,
  .form-select   { padding: 11px 14px; font-size: 16px; border-radius: 9px; }
  .form-hint     { font-size: 13px; }

  /* Tables */
  table          { font-size: 16px; }
  thead th       { padding: 11px 14px; font-size: 14px; }
  td             { padding: 11px 14px; }

  /* Modals */
  .modal-head    { padding: 18px 24px; font-size: 17px; }
  .modal-body    { padding: 24px; font-size: 17px; }
  .modal-footer  { padding: 16px 24px; gap: 12px; }

  /* Badges / misc */
  .badge         { font-size: 13px; padding: 4px 11px; }
  .breadcrumb    { font-size: 14.5px; }
  .breadcrumb-link { font-size: 14.5px; }
  .step-pill     { font-size: 14px; padding: 7px 17px; }

  /* Project cards */
  .proj-card-title { font-size: 16px; }
  .proj-card-meta  { font-size: 14px; }

  /* Shortcut bar */
  .sc-btn        { font-size: 14px; padding: 9px 16px; }
}

/* ═══════════════════════════════════════════════════════
   MOBILE SAFE-AREA & EDGE PADDING
   ปุ่ม/เนื้อหาไม่ชิดขอบจอมือถือ (notch, rounded corners)
   ═══════════════════════════════════════════════════════ */
@supports (padding: env(safe-area-inset-left)) {
  /* Content area — เว้นจากขอบซ้าย/ขวา/ล่าง */
  .content {
    padding-left:  max(16px, calc(env(safe-area-inset-left)  + 14px));
    padding-right: max(16px, calc(env(safe-area-inset-right) + 14px));
    padding-bottom: max(16px, calc(env(safe-area-inset-bottom) + 12px));
  }

  /* Modal footer — กัน home indicator บน iPhone */
  .modal-footer {
    padding-bottom: max(16px, calc(env(safe-area-inset-bottom) + 12px));
  }

  /* Topbar — กัน notch/status bar บน iOS */
  .topbar {
    padding-left:  max(14px, calc(env(safe-area-inset-left)  + 12px));
    padding-right: max(14px, calc(env(safe-area-inset-right) + 12px));
  }

  /* Toast ไม่ทับ home indicator */
  .toast {
    bottom: max(20px, calc(env(safe-area-inset-bottom) + 14px));
  }

  /* Sidebar บนมือถือ — กัน notch ซ้าย (landscape) */
  .sidebar {
    padding-left:  max(0px, env(safe-area-inset-left));
    padding-bottom: max(24px, calc(env(safe-area-inset-bottom) + 16px));
  }
}

/* เพิ่ม min-margin บน mobile ให้ปุ่มใน card/modal ไม่ชิดขอบ */
@media (max-width: 600px) {
  /* shortcut-bar และ action button group เว้นขอบ */
  .shortcut-bar  { padding-left: max(12px, calc(env(safe-area-inset-left, 0px)  + 10px));
                   padding-right: max(12px, calc(env(safe-area-inset-right, 0px) + 10px)); }
  /* generate bar (ท้าย modal ปร.4) */
  #pr4GenerateBar,
  #pr4EquipGenBar { padding-left:  max(14px, calc(env(safe-area-inset-left, 0px)  + 10px)) !important;
                    padding-right: max(14px, calc(env(safe-area-inset-right, 0px) + 10px)) !important;
                    padding-bottom: max(14px, calc(env(safe-area-inset-bottom, 0px) + 10px)) !important; }
  /* step-content ไม่ชิดขอบ */
  .step-content  { padding-left:  max(14px, calc(env(safe-area-inset-left, 0px)  + 10px));
                   padding-right: max(14px, calc(env(safe-area-inset-right, 0px) + 10px)); }
  /* project card grid margin */
  .proj-grid     { padding: 0 max(0px, env(safe-area-inset-left, 0px)); }
}
