:root {
  --ink: #1f2936; --ink-2: #4b5563; --muted: #6b7280; --line: #e5e7eb; --bg: #f4f5f7; --card: #fff;
  --gold: #c49a40; --red: #b91c1c; --red-bg: #fee2e2; --amber: #b45309; --amber-bg: #fef3c7;
  --green: #15803d; --green-bg: #dcfce7; --blue: #1d4ed8; --blue-bg: #dbeafe; --gray-bg: #f3f4f6;
  --radius: 12px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans Thai", Tahoma, sans-serif;
  color: var(--ink); -webkit-text-size-adjust: 100%;
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
a { color: inherit; }
button, input, select, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.pad { padding: 16px; }
.th { color: var(--ink-2); }

.top { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 8px;
  background: var(--ink); color: #fff; padding: calc(10px + env(safe-area-inset-top)) 16px 10px; }
.top h1 { font-size: 17px; margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top .spacer { flex: 1; }
.top .back { background: none; border: 0; color: #fff; font-size: 30px; line-height: 1; padding: 0 8px 4px 0; cursor: pointer; }
.top .ghost { background: none; border: 1px solid rgba(255,255,255,.35); color: #fff; border-radius: 8px; padding: 4px 10px; font-size: 13px; }

nav.bottom { position: fixed; bottom: 0; left: 0; right: 0; z-index: 20; display: flex; background: #fff;
  border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
nav.bottom a { flex: 1; text-align: center; padding: 14px 0; text-decoration: none; font-size: 14px; color: var(--muted); font-weight: 600; }
nav.bottom a.on { color: var(--ink); box-shadow: inset 0 3px 0 var(--gold); }

main { max-width: 900px; margin: 0 auto; padding: 16px; }
h2 { font-size: 18px; margin: 18px 0 10px; }
h3 { font-size: 15px; margin: 16px 0 8px; }

.card { background: var(--card); border-radius: var(--radius); padding: 14px; margin-bottom: 12px;
  box-shadow: 0 1px 2px rgba(0,0,0,.05); }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; margin-bottom: 14px; }
.stat { background: var(--card); border-radius: var(--radius); padding: 12px 14px; }
.stat b { display: block; font-size: 22px; }
.stat span { font-size: 13px; color: var(--muted); }

.villa { display: flex; gap: 12px; text-decoration: none; align-items: stretch; }
.villa img, .villa .noimg { width: 84px; height: 84px; border-radius: 10px; object-fit: cover; background: var(--gray-bg); flex: none; }
.villa .name { font-weight: 700; font-size: 16px; }
.villa .sub { font-size: 13px; color: var(--muted); margin: 2px 0 6px; }

.chip { display: inline-block; font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px;
  background: var(--gray-bg); color: var(--ink-2); white-space: nowrap; }
.chip.red { background: var(--red-bg); color: var(--red); }
.chip.amber { background: var(--amber-bg); color: var(--amber); }
.chip.green { background: var(--green-bg); color: var(--green); }
.chip.blue { background: var(--blue-bg); color: var(--blue); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }

.tabs { display: flex; gap: 4px; overflow-x: auto; margin: -4px -16px 14px; padding: 0 16px; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tabs button { border: 0; background: none; padding: 10px 12px; font-weight: 600; color: var(--muted);
  border-bottom: 3px solid transparent; white-space: nowrap; cursor: pointer; }
.tabs button.on { color: var(--ink); border-color: var(--gold); }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--line);
  background: #fff; border-radius: 10px; padding: 10px 14px; font-weight: 600; cursor: pointer; text-decoration: none; min-height: 44px; }
.btn.primary { background: var(--ink); color: #fff; border-color: var(--ink); }
.btn.gold { background: var(--gold); color: #fff; border-color: var(--gold); }
.btn.danger { color: var(--red); border-color: #fecaca; }
.btn.small { min-height: 34px; padding: 6px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }
.fab { position: fixed; right: 18px; bottom: calc(80px + env(safe-area-inset-bottom)); z-index: 15;
  border-radius: 999px; padding: 14px 20px; box-shadow: 0 6px 18px rgba(0,0,0,.2); font-size: 16px; }

.defect { display: flex; gap: 12px; align-items: flex-start; cursor: pointer; }
.defect img, .defect .noimg { width: 72px; height: 72px; border-radius: 8px; object-fit: cover; background: var(--gray-bg); flex: none; }
.defect .t { font-weight: 600; }
.defect .l { font-size: 13px; color: var(--muted); margin: 2px 0 6px; }
.order { display: flex; flex-direction: column; gap: 4px; }
.filters { display: flex; gap: 6px; overflow-x: auto; margin-bottom: 12px; scrollbar-width: none; }
.filters button { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 6px 12px; font-size: 13px; white-space: nowrap; cursor: pointer; }
.filters button.on { background: var(--ink); color: #fff; border-color: var(--ink); }

.form { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; }
.form .full { grid-column: 1 / -1; }
.field label { display: block; font-size: 13px; font-weight: 600; color: var(--ink-2); margin-bottom: 4px; }
.field input:not([type=checkbox]), .field select, .field textarea { width: 100%; border: 1px solid #d1d5db; border-radius: 10px;
  padding: 10px 12px; background: #fff; font-size: 16px; }
.field textarea { min-height: 76px; resize: vertical; }
.field.check label { display: flex; gap: 8px; align-items: center; font-size: 15px; color: var(--ink); }
.field.check input { width: 20px; height: 20px; }
@media (max-width: 560px) { .form { grid-template-columns: 1fr; } }

.photos { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; }
.photo { position: relative; aspect-ratio: 1; border-radius: 8px; overflow: hidden; background: var(--gray-bg); }
.photo img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photo button { position: absolute; top: 4px; right: 4px; width: 28px; height: 28px; border-radius: 999px; border: 0;
  background: rgba(0,0,0,.6); color: #fff; font-size: 16px; line-height: 1; cursor: pointer; }
.photo.pending { opacity: .6; }
.addphoto { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.addphoto input { display: none; }

.sheet-bg { position: fixed; inset: 0; z-index: 40; background: rgba(15,23,42,.45); display: flex; align-items: flex-end; justify-content: center; }
.sheet { background: var(--bg); width: 100%; max-width: 760px; max-height: 94vh; overflow-y: auto; border-radius: 16px 16px 0 0;
  padding: 0 16px calc(16px + env(safe-area-inset-bottom)); }
.sheet-head { position: sticky; top: 0; z-index: 2; background: var(--bg); display: flex; align-items: center; gap: 8px; padding: 14px 0 10px; border-bottom: 1px solid var(--line); margin-bottom: 12px; }
.sheet-head h2 { margin: 0; flex: 1; font-size: 17px; }
.sheet-foot { position: sticky; bottom: 0; background: var(--bg); padding: 12px 0 0; display: flex; gap: 8px; flex-wrap: wrap; border-top: 1px solid var(--line); margin-top: 14px; }
@media (min-width: 761px) { .sheet-bg { align-items: center; } .sheet { border-radius: 16px; } }

.tablewrap { overflow-x: auto; background: #fff; border-radius: var(--radius); }
table.data { width: 100%; border-collapse: collapse; font-size: 14px; }
table.data th { text-align: left; font-size: 12px; color: var(--muted); padding: 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.data td:first-child { white-space: nowrap; }
table.data td { padding: 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.data td.num, table.data th.num { text-align: right; white-space: nowrap; }
table.data tr.total td { font-weight: 700; background: var(--gray-bg); }

.list-item { display: flex; gap: 10px; align-items: flex-start; padding: 10px 0; border-bottom: 1px solid var(--line); }
.list-item:last-child { border-bottom: 0; }
.timeline .when { font-size: 12px; color: var(--muted); white-space: nowrap; min-width: 96px; }

.login { max-width: 360px; margin: 12vh auto 0; text-align: center; }
.login input { width: 100%; text-align: center; letter-spacing: 2px; font-size: 20px; padding: 14px; border-radius: 12px; border: 1px solid #d1d5db; margin: 16px 0 10px; text-transform: uppercase; }
.err { color: var(--red); font-size: 14px; min-height: 20px; }

#toast { position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 14px; opacity: 0; pointer-events: none;
  transition: .2s; z-index: 60; max-width: calc(100% - 32px); }
#toast.on { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.bad { background: var(--red); }
