:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #1c1f24;
  --muted: #6b7280;
  --line: #e5e7eb;
  --primary: #06c755;
  --primary-ink: #ffffff;
  --primary-soft: #e6f9ee;
  --danger: #dc2626;
  --danger-soft: #fdecec;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --info: #1d4ed8;
  --info-soft: #e0e9ff;
  --radius: 14px;
  --tabbar-h: 62px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans Thai", "Leelawadee UI", Tahoma, sans-serif;
  color-scheme: light;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body { font-size: 16px; line-height: 1.5; -webkit-text-size-adjust: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
img { max-width: 100%; }

/* ---------- layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.topbar-title { display: flex; flex-direction: column; min-width: 0; }
.brand { font-weight: 700; font-size: 15px; }
.company { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.avatar { width: 34px; height: 34px; border-radius: 50%; object-fit: cover; background: var(--line); flex: none; }
.app { padding: 16px 16px calc(var(--tabbar-h) + var(--safe-b) + 24px); max-width: 640px; margin: 0 auto; }
.app.no-tabbar { padding-bottom: calc(24px + var(--safe-b)); }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: var(--safe-b); height: calc(var(--tabbar-h) + var(--safe-b));
}
.tabbar button {
  flex: 1; border: 0; background: none; padding: 8px 4px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 12px; color: var(--muted);
}
.tabbar button svg { width: 24px; height: 24px; }
.tabbar button[aria-current="page"] { color: var(--primary); font-weight: 600; }
.tabbar .dot { position: relative; }
.tabbar .dot::after { content: attr(data-count); position: absolute; top: -6px; right: -12px; min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 9px; background: var(--danger); color: #fff; font-size: 11px; line-height: 18px; text-align: center; }

/* ---------- typography ---------- */
h1 { font-size: 22px; margin: 4px 0 4px; }
h2 { font-size: 18px; margin: 20px 0 10px; }
.sub { color: var(--muted); margin: 0 0 16px; font-size: 14px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* ---------- cards & lists ---------- */
.card { background: var(--surface); border-radius: var(--radius); padding: 16px; border: 1px solid var(--line); }
.card + .card { margin-top: 12px; }
.list { display: flex; flex-direction: column; gap: 10px; }
.item {
  display: block; width: 100%; text-align: left; background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 14px; cursor: pointer;
}
.item:active { background: #fafafa; }
.item-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.item-title { font-weight: 600; margin: 4px 0 2px; overflow-wrap: anywhere; }
.item-meta { font-size: 13px; color: var(--muted); }
.row { display: flex; align-items: center; gap: 12px; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.grow { flex: 1; min-width: 0; }

.badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; background: var(--line); color: var(--text); }
.badge.submitted, .badge.pending { background: var(--info-soft); color: var(--info); }
.badge.in_progress, .badge.escalated, .badge.change_request { background: var(--warn-soft); color: var(--warn); }
.badge.waiting_customer, .badge.wait_confirm { background: #fde7f3; color: #9d174d; }
.badge.resolved, .badge.approved { background: var(--primary-soft); color: #047a35; }
.badge.closed, .badge.rejected, .badge.blocked { background: #eceef1; color: #4b5563; }

/* ---------- forms ---------- */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; }
.field label { font-size: 14px; font-weight: 600; }
.field .req { color: var(--danger); }
.field .hint { font-size: 12px; color: var(--muted); }
.input, .select, .textarea {
  width: 100%; border: 1px solid #d1d5db; border-radius: 10px; padding: 12px; background: #fff; font-size: 16px;
}
.textarea { min-height: 140px; resize: vertical; }
.input:focus, .select:focus, .textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; }
.counter { align-self: flex-end; font-size: 12px; color: var(--muted); }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg label { display: flex; align-items: center; justify-content: center; text-align: center; min-height: 48px; padding: 8px;
  border: 1px solid #d1d5db; border-radius: 10px; background: #fff; font-weight: 500; font-size: 14px; cursor: pointer; }
.seg input:checked + label { border-color: var(--primary); background: var(--primary-soft); color: #047a35; font-weight: 700; }
.seg input:focus-visible + label { outline: 2px solid var(--primary); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 0 18px; border-radius: 12px; border: 1px solid transparent; cursor: pointer; font-weight: 600;
  background: var(--primary); color: var(--primary-ink);
}
.btn.block { width: 100%; }
.btn.secondary { background: #fff; color: var(--text); border-color: #d1d5db; }
.btn.danger { background: #fff; color: var(--danger); border-color: #f3b4b4; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 14px; border-radius: 10px; }
.btn:disabled { opacity: .55; cursor: default; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-row .btn { flex: 1; }
.link { background: none; border: 0; color: var(--info); padding: 0; cursor: pointer; font-weight: 600; }

/* ---------- photos ---------- */
.photos { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.photo { position: relative; aspect-ratio: 1; border-radius: 10px; overflow: hidden; background: var(--line); }
.photo img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photo .rm { position: absolute; top: 4px; right: 4px; width: 26px; height: 26px; border-radius: 50%; border: 0; background: rgba(0,0,0,.6); color: #fff; cursor: pointer; line-height: 26px; padding: 0; }
.photo-add { aspect-ratio: 1; border: 1.5px dashed #c3c8cf; border-radius: 10px; display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: var(--muted); font-size: 12px; cursor: pointer; background: #fff; }
.photo-add svg { width: 26px; height: 26px; }

/* ---------- detail ---------- */
.kv { display: grid; grid-template-columns: 110px 1fr; gap: 6px 12px; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.desc { white-space: pre-wrap; overflow-wrap: anywhere; }
.timeline { list-style: none; padding: 0; margin: 0; }
.timeline li { position: relative; padding: 0 0 14px 22px; font-size: 14px; }
.timeline li::before { content: ""; position: absolute; left: 4px; top: 7px; width: 10px; height: 10px; border-radius: 50%; background: var(--primary); }
.timeline li::after { content: ""; position: absolute; left: 8px; top: 20px; bottom: 0; width: 2px; background: var(--line); }
.timeline li:last-child::after { display: none; }

/* ---------- misc ---------- */
.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 12px; scrollbar-width: none; }
.chips button { flex: none; border: 1px solid #d1d5db; background: #fff; border-radius: 999px; padding: 6px 14px; font-size: 14px; cursor: pointer; }
.chips button[aria-pressed="true"] { background: var(--text); color: #fff; border-color: var(--text); }
.center-state { min-height: 60vh; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; gap: 8px; padding: 24px; }
.center-state .icon { width: 64px; height: 64px; border-radius: 50%; display: flex; align-items: center; justify-content: center; background: var(--primary-soft); color: #047a35; font-size: 30px; }
.center-state .icon.warn { background: var(--warn-soft); color: var(--warn); }
.center-state .icon.err { background: var(--danger-soft); color: var(--danger); }
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--primary); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.empty { text-align: center; color: var(--muted); padding: 32px 8px; }
.error-box { background: var(--danger-soft); color: var(--danger); border-radius: 10px; padding: 10px 12px; font-size: 14px; margin-bottom: 12px; }
.note { background: var(--info-soft); color: #1e3a8a; border-radius: 10px; padding: 10px 12px; font-size: 14px; }
.code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; background: #f3f4f6; padding: 8px 10px; border-radius: 8px; overflow-wrap: anywhere; }
.divider { height: 1px; background: var(--line); margin: 16px 0; }
.menu { display: flex; flex-direction: column; gap: 10px; }
.menu .item { display: flex; align-items: center; gap: 12px; }
.menu .item .ico { width: 40px; height: 40px; border-radius: 10px; background: var(--primary-soft); color: #047a35; display: flex; align-items: center; justify-content: center; flex: none; }
.menu .item .ico svg { width: 22px; height: 22px; }
.back { display: inline-flex; align-items: center; gap: 4px; border: 0; background: none; color: var(--muted); padding: 4px 0; margin-bottom: 8px; cursor: pointer; font-size: 14px; }

.toast { position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + var(--safe-b) + 16px); transform: translateX(-50%) translateY(20px);
  background: #111827; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 14px; opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s; z-index: 50; max-width: calc(100% - 32px); }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 40; }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; z-index: 41; background: var(--surface); border-radius: 18px 18px 0 0;
  max-height: 88vh; overflow-y: auto; padding: 8px 16px calc(16px + var(--safe-b)); max-width: 640px; margin: 0 auto; }
.sheet .grab { width: 40px; height: 4px; border-radius: 2px; background: var(--line); margin: 4px auto 12px; }

.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.92); z-index: 60; display: flex; align-items: center; justify-content: center; }
.lightbox img { max-width: 100%; max-height: 100%; }

[hidden] { display: none !important; }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
