:root {
  --brand: #be185d;
  --brand-dark: #9d174d;
  --brand-soft: #fce7f3;
  --bg: #f7f3f5;
  --card: #ffffff;
  --ink: #1f1a1d;
  --muted: #7b6f75;
  --line: #ece4e8;
  --zalo: #0068ff;
  --call: #16a34a;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --ok: #15803d;
  --ok-soft: #dcfce7;
  --danger: #dc2626;
  --top-h: 52px;
  --tabs-h: 62px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--ink); }
body {
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  touch-action: manipulation;
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--brand); text-decoration: none; }
.boot { padding: 40vh 0 0; text-align: center; color: var(--muted); }
[hidden] { display: none !important; }

/* ------------------------------ Khung app ------------------------------ */
.top {
  position: sticky; top: 0; z-index: 20;
  height: calc(var(--top-h) + var(--safe-t)); padding: var(--safe-t) 8px 0;
  display: flex; align-items: center; gap: 4px;
  background: rgba(255, 255, 255, .96); border-bottom: 1px solid var(--line);
  backdrop-filter: saturate(1.4) blur(8px);
}
.top h1 { flex: 1; margin: 0 6px; font-size: 18px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  position: relative; width: 44px; height: 44px; border: 0; border-radius: 12px; background: transparent;
  display: grid; place-items: center; cursor: pointer; color: var(--ink);
}
.icon-btn:active { background: var(--brand-soft); }
.icon-btn svg { width: 24px; height: 24px; }
.icon-btn .dot { position: absolute; top: 9px; right: 10px; width: 9px; height: 9px; border-radius: 50%; background: var(--danger); border: 2px solid #fff; }
.icon-btn.on { color: var(--brand); }

main { max-width: 640px; margin: 0 auto; padding: 12px 12px calc(var(--tabs-h) + var(--safe-b) + 24px); }

.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; justify-content: center;
  padding-bottom: var(--safe-b); height: calc(var(--tabs-h) + var(--safe-b));
  background: rgba(255, 255, 255, .97); border-top: 1px solid var(--line);
}
.tabs a {
  position: relative; flex: 1; max-width: 128px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); font-size: 11.5px; font-weight: 600;
}
.tabs a svg { width: 24px; height: 24px; }
.tabs a.on { color: var(--brand); }
.tabs .badge {
  position: absolute; top: 5px; left: calc(50% + 6px); 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;
}

/* ------------------------------- Chung ------------------------------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 14px; margin-bottom: 12px; }
.muted { color: var(--muted); font-size: 14px; }
.small { font-size: 13px; }
.sec { margin: 20px 4px 8px; font-size: 15px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .02em; }
.empty { padding: 28px 12px; text-align: center; color: var(--muted); }
.loading { padding: 40px 0; text-align: center; color: var(--muted); }
.row { display: flex; align-items: center; gap: 8px; }
.grow { flex: 1; min-width: 0; }
.wrap { flex-wrap: wrap; }
.mt { margin-top: 10px; }

.lbl { display: block; margin: 12px 0 5px; font-size: 13px; font-weight: 700; color: var(--muted); }
input, select, textarea {
  width: 100%; min-height: 46px; padding: 10px 12px; font-size: 16px;
  background: #fff; border: 1.5px solid var(--line); border-radius: 12px; outline: none;
}
textarea { min-height: 64px; resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
/* An nhung van bam duoc qua label (display:none lam iPhone doi khi khong mo chon anh) */
input[type="file"] { position: absolute; width: 1px; height: 1px; min-height: 0; padding: 0; border: 0; opacity: 0; overflow: hidden; clip: rect(0 0 0 0); }
.add-photo { position: relative; }

.btn, .btn-primary, .btn-zalo, .btn-call, .btn-ghost {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 42px; padding: 0 14px; border-radius: 12px; border: 1.5px solid var(--line);
  background: #fff; font-weight: 700; font-size: 15px; cursor: pointer; white-space: nowrap; color: var(--ink);
}
.btn:active, .btn-ghost:active { background: var(--bg); }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-primary:active { background: var(--brand-dark); }
.btn-primary:disabled { opacity: .6; }
.btn-primary.big { width: 100%; min-height: 54px; margin-top: 18px; font-size: 18px; border-radius: 14px; }
.btn-zalo { background: var(--zalo); border-color: var(--zalo); color: #fff; padding: 0 12px; }
.btn-call { background: var(--ok-soft); border-color: transparent; color: var(--call); padding: 0 11px; }
.btn-call svg { width: 20px; height: 20px; }
.btn-zalo.off, .btn-call.off { opacity: .35; pointer-events: none; }
.btn-ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn.danger, .btn-ghost.danger { color: var(--danger); }
.btn.sm, .btn-primary.sm, .btn-zalo.sm, .btn-call.sm { min-height: 36px; padding: 0 10px; font-size: 14px; border-radius: 10px; }
.btn.done { background: var(--ok); border-color: var(--ok); color: #fff; }

.pill { display: inline-block; padding: 2px 9px; border-radius: 99px; font-size: 12.5px; font-weight: 700; background: var(--bg); color: var(--muted); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.ok { background: var(--ok-soft); color: var(--ok); }
.pill.brand { background: var(--brand-soft); color: var(--brand); }

/* ------------------------------ Dang nhap ------------------------------ */
.login { max-width: 380px; margin: 0 auto; padding: calc(12vh + var(--safe-t)) 20px 40px; }
.login .logo { width: 76px; height: 76px; border-radius: 20px; display: block; margin: 0 auto 14px; }
.login h1 { text-align: center; font-size: 22px; margin: 0 0 4px; }
.login p { text-align: center; margin: 0 0 22px; }
.gate .gate-ico { width: 84px; height: 84px; margin: 0 auto 16px; border-radius: 50%; display: grid; place-items: center; background: var(--brand-soft); color: var(--brand); }
.gate .gate-ico svg { width: 42px; height: 42px; }
.gate .card { text-align: left; }
.remember { display: flex; align-items: center; gap: 10px; margin-top: 14px; font-size: 15px; cursor: pointer; }
.remember input { width: 22px; min-height: 22px; height: 22px; margin: 0; accent-color: var(--brand); }

/* ------------------------------ Nhap mau ------------------------------ */
.seg { display: flex; padding: 4px; gap: 4px; background: #ebe3e7; border-radius: 14px; margin-bottom: 12px; }
.seg button { flex: 1; min-height: 42px; border: 0; border-radius: 11px; background: transparent; font-weight: 700; color: var(--muted); cursor: pointer; }
.seg button.on { background: #fff; color: var(--brand); box-shadow: 0 1px 3px rgba(0, 0, 0, .08); }
.seg.prov { margin-bottom: 10px; }
.seg.prov button { min-width: 0; padding: 4px 4px; font-size: 14px; line-height: 1.2; white-space: nowrap; }
.seg.prov small { display: block; font-size: 12px; font-weight: 600; opacity: .75; }
form .seg.prov { margin-bottom: 0; }

.when { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-radius: 12px; background: var(--brand-soft); color: var(--brand-dark); font-weight: 600; font-size: 15px; }
.when .grow b { font-size: 17px; }
.when input { min-height: 40px; background: #fff; }

.phone-row { display: flex; gap: 6px; align-items: stretch; }
.phone-row input { flex: 1; font-size: 20px; font-weight: 700; letter-spacing: .03em; }

.sugg { margin-top: 6px; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: #fff; }
.sugg:empty { display: none; }
.sugg button { display: block; width: 100%; text-align: left; padding: 10px 12px; border: 0; border-bottom: 1px solid var(--line); background: #fff; cursor: pointer; }
.sugg button:last-child { border-bottom: 0; }
.sugg button:active { background: var(--bg); }

.known { margin-top: 10px; padding: 12px; border-radius: 12px; background: #fdf6f9; border: 1.5px solid #f6c9dd; }
.known-h { display: flex; align-items: center; gap: 8px; }
.known-h b { font-size: 17px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tag-old { padding: 2px 8px; border-radius: 6px; background: var(--brand); color: #fff; font-size: 12px; font-weight: 800; }
.newtag { margin-top: 10px; padding: 9px 12px; border-radius: 12px; background: var(--ok-soft); color: var(--ok); font-weight: 700; font-size: 14px; }
.pin { margin-top: 8px; padding: 8px 10px; border-radius: 10px; background: #fff; font-size: 14px; white-space: pre-wrap; }
.mini { list-style: none; margin: 8px 0 0; padding: 0; font-size: 14px; }
.mini li { padding: 6px 0; border-top: 1px dashed var(--line); }
.mini .d { font-weight: 700; }
.mini .n { display: block; color: var(--muted); white-space: pre-wrap; }

.chips { display: flex; flex-wrap: wrap; gap: 7px; }
.chip { min-height: 38px; padding: 0 14px; border-radius: 99px; border: 1.5px solid var(--line); background: #fff; font-weight: 600; font-size: 15px; cursor: pointer; }
.chip.on { background: var(--brand); border-color: var(--brand); color: #fff; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.photos { display: flex; flex-wrap: wrap; gap: 8px; }
.photos .th { position: relative; width: 72px; height: 72px; border-radius: 12px; overflow: hidden; background: var(--bg); }
.photos .th img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photos .th button { position: absolute; top: 3px; right: 3px; width: 24px; height: 24px; border: 0; border-radius: 50%; background: rgba(0, 0, 0, .55); color: #fff; font-size: 14px; line-height: 24px; padding: 0; cursor: pointer; }
.add-photo { width: 72px; height: 72px; border-radius: 12px; border: 1.5px dashed #d9c3cd; display: grid; place-items: center; color: var(--brand); cursor: pointer; font-size: 12px; font-weight: 700; text-align: center; }
.add-photo svg { width: 24px; height: 24px; display: block; margin: 0 auto 2px; }

.strip { display: flex; gap: 6px; margin-top: 8px; overflow-x: auto; }
.strip img { width: 58px; height: 58px; border-radius: 10px; object-fit: cover; flex: none; cursor: pointer; background: var(--bg); }

.banner { display: flex; align-items: center; gap: 10px; padding: 10px 12px; margin-bottom: 12px; border-radius: 14px; background: #fff; border: 1.5px solid #f6c9dd; font-size: 14px; }
.banner .grow b { display: block; font-size: 15px; }
.banner .x { border: 0; background: none; color: var(--muted); font-size: 20px; padding: 4px 6px; cursor: pointer; }

/* ------------------------------ Danh sach ------------------------------ */
.list { background: var(--card); border: 1px solid var(--line); border-radius: 16px; overflow: hidden; }
.item { padding: 12px 14px; border-bottom: 1px solid var(--line); }
.item:last-child { border-bottom: 0; }
.item-main { cursor: pointer; display: flex; gap: 10px; align-items: flex-start; }
.item-main .name { font-weight: 700; font-size: 16px; }
.item .time { flex: none; min-width: 52px; padding: 4px 0; border-radius: 10px; background: var(--brand-soft); color: var(--brand-dark); text-align: center; font-weight: 800; font-size: 15px; }
.item .acts { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 9px; }
.item.contacted { background: #fbfafa; }
.item.contacted .name { color: var(--muted); }
.note-line { margin-top: 3px; font-size: 14px; white-space: pre-wrap; }

.buckets { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 10px; scrollbar-width: none; }
.buckets::-webkit-scrollbar { display: none; }
.buckets button { flex: none; min-height: 44px; padding: 0 13px; border-radius: 12px; border: 1.5px solid var(--line); background: #fff; font-weight: 700; cursor: pointer; }
.buckets button span { display: inline-block; min-width: 22px; margin-left: 5px; padding: 1px 6px; border-radius: 99px; background: var(--bg); font-size: 12.5px; }
.buckets button.on { background: var(--brand); border-color: var(--brand); color: #fff; }
.buckets button.on span { background: rgba(255, 255, 255, .25); }

.search { position: sticky; top: calc(var(--top-h) + var(--safe-t) + 8px); z-index: 5; margin-bottom: 10px; }
.search input { padding-left: 40px; background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%237b6f75' stroke-width='2.2' stroke-linecap='round'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='m20 20-3.5-3.5'/%3E%3C/svg%3E") no-repeat 12px center / 20px; }

/* ------------------------------ Ho so mau ------------------------------ */
.profile .p-name { font-size: 23px; font-weight: 800; }
.profile .p-phone { flex: 1; font-size: 20px; font-weight: 700; letter-spacing: .03em; align-self: center; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 12px 0 4px; }
.stats div { padding: 9px 6px; border-radius: 12px; background: var(--bg); text-align: center; }
.stats b { display: block; font-size: 15px; }
.stats span { font-size: 12px; color: var(--muted); }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.btn-row > * { flex: 1; }

.gallery { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.gallery figure { margin: 0; position: relative; aspect-ratio: 1; border-radius: 12px; overflow: hidden; background: #ebe3e7; cursor: pointer; }
.gallery img { width: 100%; height: 100%; object-fit: cover; display: block; }
.gallery figcaption { position: absolute; left: 0; right: 0; bottom: 0; padding: 14px 6px 4px; font-size: 11.5px; font-weight: 700; color: #fff; background: linear-gradient(transparent, rgba(0, 0, 0, .55)); }

.timeline { position: relative; }
.tl { position: relative; padding: 12px 44px 12px 40px; background: var(--card); border: 1px solid var(--line); border-radius: 14px; margin-bottom: 8px; }
.tl .ico { position: absolute; left: 11px; top: 12px; width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; font-size: 12px; font-weight: 800; color: #fff; background: var(--ok); }
.tl.booked .ico { background: var(--warn); }
.tl.note .ico { background: #8b7d84; }
.tl .when-l { font-weight: 800; }
.tl .del { position: absolute; top: 6px; right: 6px; }

/* ------------------------------ Lop phu ------------------------------ */
.sheet-bg { position: fixed; inset: 0; z-index: 50; background: rgba(20, 10, 15, .45); display: flex; align-items: flex-end; justify-content: center; animation: fade .15s; }
.sheet { width: 100%; max-width: 560px; max-height: 88vh; overflow: auto; background: #fff; border-radius: 20px 20px 0 0; padding: 18px 16px calc(16px + var(--safe-b)); animation: up .18s ease-out; }
.sheet h2 { margin: 0 0 6px; font-size: 19px; }
.sheet-btns { display: flex; gap: 8px; margin-top: 18px; }
.sheet-btns > * { flex: 1; min-height: 48px; }
@media (min-width: 700px) { .sheet-bg { align-items: center; } .sheet { border-radius: 20px; } }
@keyframes fade { from { opacity: 0; } }
@keyframes up { from { transform: translateY(40px); opacity: .4; } }

.viewer { position: fixed; inset: 0; z-index: 60; background: #000; display: flex; flex-direction: column; }
.viewer .vbar { display: flex; align-items: center; gap: 8px; padding: calc(8px + var(--safe-t)) 10px 8px; color: #fff; }
.viewer .vbar .grow { font-weight: 700; }
.viewer .vbar button { min-height: 40px; padding: 0 12px; border-radius: 10px; border: 0; background: rgba(255, 255, 255, .15); color: #fff; font-weight: 700; cursor: pointer; }
.viewer .vimg { flex: 1; display: grid; place-items: center; overflow: hidden; touch-action: pan-y; }
.viewer .vimg img { max-width: 100%; max-height: 100%; object-fit: contain; }
.viewer .vnav { display: flex; justify-content: space-between; padding: 8px 10px calc(10px + var(--safe-b)); }
.viewer .vnav button { min-width: 64px; min-height: 44px; border: 0; border-radius: 12px; background: rgba(255, 255, 255, .15); color: #fff; font-size: 22px; cursor: pointer; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabs-h) + var(--safe-b) + 14px); z-index: 70;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  max-width: calc(100vw - 32px); padding: 12px 18px; border-radius: 14px;
  background: #1f1a1d; color: #fff; font-weight: 600; font-size: 15px; text-align: center;
  transition: opacity .2s, transform .2s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.err { background: var(--danger); }

.set-item { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.set-item:last-child { border-bottom: 0; }
.steps { margin: 6px 0 0; padding-left: 20px; font-size: 14.5px; }
.steps li { margin: 4px 0; }
.switch { position: relative; width: 52px; height: 32px; flex: none; border-radius: 99px; border: 0; background: #d9cfd4; cursor: pointer; transition: background .15s; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 26px; height: 26px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, .25); transition: transform .15s; }
.switch.on { background: var(--brand); }
.switch.on::after { transform: translateX(20px); }
.switch:disabled { opacity: .5; }
