* { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg: #f4f6fa; --card: #fff; --line: #e3e8f0;
  --text: #1f2937; --muted: #6b7280;
  --primary: #2563eb; --primary-d: #1d4ed8;
  --green: #16a34a; --red: #dc2626; --amber: #d97706;
}
body { font-family: 'Sarabun', 'Noto Sans Thai', -apple-system, sans-serif; background: var(--bg); color: var(--text); font-size: 15px; }
a { color: var(--primary); text-decoration: none; }

/* layout */
header { background: #1e293b; color: #fff; padding: 10px 20px; display: flex; align-items: center; gap: 16px; position: sticky; top: 0; z-index: 50; }
header h1 { font-size: 17px; font-weight: 700; }
nav { display: flex; gap: 4px; flex: 1; }
nav a { color: #cbd5e1; padding: 6px 12px; border-radius: 8px; font-size: 14px; }
nav a.active, nav a:hover { background: #334155; color: #fff; }
.userbox { font-size: 13px; color: #cbd5e1; display: flex; gap: 10px; align-items: center; }
main { max-width: 1100px; margin: 20px auto; padding: 0 16px 60px; }

/* cards */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 18px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 16px; }
.stat .num { font-size: 30px; font-weight: 700; }
.stat .lbl { color: var(--muted); font-size: 13px; }
.stat.green .num { color: var(--green); } .stat.red .num { color: var(--red); } .stat.blue .num { color: var(--primary); }

.panel { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 16px; margin-bottom: 16px; }
.panel h2 { font-size: 16px; margin-bottom: 12px; display: flex; align-items: center; justify-content: space-between; gap: 8px; }

/* table */
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; color: var(--muted); font-weight: 600; padding: 8px; border-bottom: 2px solid var(--line); white-space: nowrap; }
td { padding: 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
tr.clickable:hover { background: #f0f5ff; cursor: pointer; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--primary); background: #f8fafc; }
th .sort-hint { opacity: 0; font-weight: 400; }
th.sortable:hover .sort-hint { opacity: .45; }
.searchbox { width: auto; min-width: 260px; max-width: 340px; font-weight: 400; font-size: 14px; padding: 6px 10px; }

/* badges */
.badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.badge.occupied { background: #dcfce7; color: #166534; }
.badge.vacant { background: #fee2e2; color: #991b1b; }
.badge.repair { background: #fef3c7; color: #92400e; }
.badge.ac_cleaning { background: #dbeafe; color: #1e40af; }
.badge.renovation { background: #f3e8ff; color: #6b21a8; }
.badge.other { background: #e5e7eb; color: #374151; }
.badge.pending { background: #fee2e2; color: #991b1b; }
.badge.in_progress { background: #fef3c7; color: #92400e; }
.badge.done { background: #dcfce7; color: #166534; }
.badge.current { background: #dcfce7; color: #166534; }
.badge.past { background: #e5e7eb; color: #6b7280; }
.days-ok { color: var(--green); font-weight: 600; }
.days-warn { color: var(--amber); font-weight: 600; }
.days-danger { color: var(--red); font-weight: 600; }

/* forms */
input, select, textarea { font: inherit; padding: 8px 10px; border: 1px solid #cbd5e1; border-radius: 8px; width: 100%; background: #fff; }
input:focus, select:focus, textarea:focus { outline: 2px solid #bfdbfe; border-color: var(--primary); }
label { font-size: 13px; color: var(--muted); display: block; margin-bottom: 3px; }
.frm { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px; margin-bottom: 10px; }
.frm .full { grid-column: 1 / -1; }
button, .btn { font: inherit; padding: 8px 16px; border: none; border-radius: 8px; background: var(--primary); color: #fff; cursor: pointer; font-weight: 600; font-size: 14px; }
button:hover { background: var(--primary-d); }
button.sm { padding: 4px 10px; font-size: 13px; }
button.ghost { background: #fff; color: var(--text); border: 1px solid #cbd5e1; }
button.ghost:hover { background: #f1f5f9; }
button.danger { background: #fff; color: var(--red); border: 1px solid #fecaca; }
button.danger:hover { background: #fef2f2; }

/* tabs */
.tabs { display: flex; gap: 4px; border-bottom: 2px solid var(--line); margin-bottom: 14px; flex-wrap: wrap; }
.tabs button { background: none; color: var(--muted); border-radius: 8px 8px 0 0; padding: 8px 14px; }
.tabs button.active { color: var(--primary); background: #eff6ff; font-weight: 700; }

/* photos */
.photo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.photo-grid .ph { border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: #fff; }
.photo-grid img { width: 100%; height: 110px; object-fit: cover; display: block; cursor: pointer; }
.photo-grid .meta { padding: 6px 8px; font-size: 12px; color: var(--muted); }

/* modal */
.modal-bg { position: fixed; inset: 0; background: rgba(15,23,42,.55); display: flex; align-items: center; justify-content: center; z-index: 100; padding: 20px; }
.modal { background: #fff; border-radius: 12px; max-width: 860px; width: 100%; max-height: 90vh; display: flex; flex-direction: column; overflow: hidden; }
.modal .mhead { padding: 12px 16px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; font-weight: 700; }
.modal .mbody { padding: 16px; overflow: auto; }
.modal iframe { width: 100%; height: 70vh; border: none; }
.modal img.big { max-width: 100%; }

.muted { color: var(--muted); font-size: 13px; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #1e293b; color: #fff; padding: 10px 20px; border-radius: 10px; z-index: 200; font-size: 14px; }
.empty { color: var(--muted); padding: 16px; text-align: center; }

/* login */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; }
.login-card { background: #fff; border: 1px solid var(--line); border-radius: 16px; padding: 32px; width: 360px; box-shadow: 0 10px 30px rgba(0,0,0,.06); }
.login-card h1 { font-size: 20px; margin-bottom: 4px; }
.login-card .sub { color: var(--muted); font-size: 13px; margin-bottom: 20px; }
.login-card .field { margin-bottom: 12px; }
.login-card button { width: 100%; margin-top: 6px; }
.err { color: var(--red); font-size: 13px; margin-top: 8px; }

/* ===== จอมือถือ (ไม่กระทบหน้าตาบนคอมเลย) ===== */
@media (max-width: 700px) {
  body { font-size: 16px; }                 /* กัน iOS ซูมเองตอนโฟกัสช่องกรอก */
  main { margin: 12px auto; padding: 0 12px 60px; }

  /* --- เมนูบนสุด: เลื่อนแนวนอนได้ ไม่ตัดคำ --- */
  header { flex-wrap: wrap; padding: 10px 12px; gap: 10px; }
  header h1 { font-size: 16px; }
  /* flex:1 0 100% เพื่อทับ flex:1 ของเดิม ไม่งั้นเมนูจะไปเบียดแถวเดียวกับชื่อผู้ใช้แล้วโดนตัด */
  nav { order: 3; flex: 1 0 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; padding-bottom: 2px; }
  header h1 { flex: 1 1 auto; }
  nav::-webkit-scrollbar { display: none; }
  nav a { white-space: nowrap; flex: 0 0 auto; padding: 8px 12px; }
  .userbox { margin-left: auto; font-size: 12px; }

  /* --- การ์ดสรุป: ให้ 3 ตัวเลขอยู่แถวเดียว --- */
  .cards { grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .stat { padding: 12px 8px; text-align: center; }
  .stat .num { font-size: 24px; }
  .stat .lbl { font-size: 12px; }

  /* --- หัวข้อ + ปุ่ม + ช่องค้นหา: ขึ้นบรรทัดใหม่ได้ --- */
  .panel { padding: 12px; }
  .panel h2 { flex-wrap: wrap; row-gap: 8px; }
  .searchbox { min-width: 0; width: 100%; max-width: none; }

  /* --- ตาราง → การ์ด (หัวใจของการใช้บนมือถือ) ---
     แต่ละแถวกลายเป็นบล็อกเดียว แสดงชื่อคอลัมน์กำกับค่าไว้ข้างหน้า
     ชื่อคอลัมน์มาจาก data-label ที่ JS ใส่ให้อัตโนมัติจาก <th> */
  table, table tbody, table tr, table td { display: block; width: 100%; }
  table thead { display: none; }
  table tr {
    border: 1px solid var(--line); border-radius: 10px;
    padding: 4px 12px; margin-bottom: 10px; background: #fff;
  }
  table td {
    display: flex; justify-content: space-between; align-items: flex-start;
    gap: 12px; padding: 7px 0; border-bottom: 1px solid #f1f5f9; text-align: right;
  }
  table tr td:last-child { border-bottom: none; }
  table td::before {
    content: attr(data-label);
    color: var(--muted); font-size: 13px; font-weight: 600;
    text-align: left; flex: 0 0 38%; white-space: normal;
  }
  table td:not([data-label])::before { content: none; }
  table td[colspan] { display: block; text-align: left; }
  table td[colspan]::before { content: none; }
  table td:empty { display: none; }
  /* ช่องปุ่มท้ายแถว: กระจายเต็มความกว้าง กดง่าย */
  table td:not([data-label]) { justify-content: flex-start; flex-wrap: wrap; gap: 8px; padding-top: 10px; }

  /* --- ปุ่ม: ขยายให้นิ้วกดถูก (มาตรฐาน 44px) --- */
  button, .btn { padding: 11px 18px; font-size: 15px; }
  button.sm, .btn.sm { padding: 9px 14px; font-size: 14px; min-height: 40px; }
  table td button.sm, table td .btn.sm { min-height: 42px; flex: 1 1 auto; }
  table td button.danger { flex: 0 0 auto; margin-left: auto; }  /* ปุ่มลบแยกไปขวา กันกดพลาด */

  /* --- ป๊อปอัป --- */
  .modal-bg { padding: 8px; align-items: flex-end; }
  .modal { max-height: 94vh; border-radius: 14px; }
  .modal .mbody { padding: 12px; }
  .modal iframe { height: 62vh; }

  .photo-grid { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .tabs button { padding: 10px 12px; }
}
