:root {
  --bg: #f3f5f9;
  --panel: #ffffff;
  --panel-2: #f7f8fb;
  --line: #e3e7ef;
  --text: #141a26;
  --muted: #667085;
  --brand: #0b5cff;
  --brand-ink: #ffffff;
  --brand-soft: #e7efff;
  --out: #dbe8ff;
  --in: #ffffff;
  --ok: #12a150;
  --warn: #d97706;
  --bad: #dc2626;
  --ai: #7c3aed;
  --ai-soft: #f1eaff;
  --radius: 12px;
  --shadow: 0 8px 30px rgba(16, 24, 40, .12);
  --nav-h: 60px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1218; --panel: #161b24; --panel-2: #1b2130; --line: #273042; --text: #e7ebf3; --muted: #94a0b8;
    --brand: #4d8bff; --brand-soft: #1b2a4a; --out: #20365f; --in: #1f2633; --ai-soft: #2a2140; --shadow: 0 8px 30px rgba(0,0,0,.5);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.45 -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg); color: var(--text); -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--brand); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.hidden { display: none !important; }

/* Buttons & inputs */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--line); background: var(--panel); border-radius: 10px;
  padding: 8px 14px; cursor: pointer; min-height: 38px; white-space: nowrap;
}
.btn:hover { background: var(--panel-2); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--bad); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.icon { padding: 0; width: 40px; min-height: 40px; border-radius: 50%; font-size: 18px; }
.btn:disabled { opacity: .5; cursor: default; }
input[type=text], input[type=password], input[type=search], input[type=tel], input[type=date], select, textarea {
  width: 100%; border: 1px solid var(--line); background: var(--panel); border-radius: 10px;
  padding: 9px 12px; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 4px; }
textarea { resize: vertical; min-height: 80px; }

/* Login */
.login { height: 100%; display: grid; place-items: center; padding: 20px; }
.login form { width: 100%; max-width: 360px; background: var(--panel); padding: 28px; border-radius: 18px; box-shadow: var(--shadow); }
.login h1 { margin: 0 0 4px; font-size: 22px; display: flex; align-items: center; gap: 10px; }
.login h1 img { width: 34px; }
.login .btn { width: 100%; margin-top: 18px; }
.err { color: var(--bad); font-size: 14px; min-height: 20px; margin-top: 8px; }

/* Shell */
.shell { display: grid; grid-template-columns: 72px 1fr; height: 100dvh; }
.nav { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; align-items: center; padding: 12px 0; gap: 6px; }
.nav .logo { width: 40px; margin-bottom: 10px; }
.nav a {
  width: 60px; padding: 8px 0; border-radius: 12px; text-decoration: none; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 11px; position: relative;
}
.nav a .i { font-size: 21px; line-height: 1; }
.nav a.on { background: var(--brand-soft); color: var(--brand); font-weight: 600; }
.nav .badge { position: absolute; top: 2px; right: 8px; }
.main { min-width: 0; height: 100dvh; overflow: hidden; }
.badge { background: var(--bad); color: #fff; font-size: 11px; font-weight: 700; border-radius: 10px; padding: 1px 6px; min-width: 18px; text-align: center; }

/* Page (non-inbox) */
.page { height: 100%; overflow: auto; padding: 24px; }
.page-inner { max-width: 980px; margin: 0 auto; }
.page h2 { margin: 0 0 16px; font-size: 22px; }
.page h3 { margin: 24px 0 8px; font-size: 17px; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 16px; }
.toolbar input { flex: 1; min-width: 180px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 12px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.row { display: flex; align-items: center; gap: 12px; }
.row .grow { flex: 1; min-width: 0; }
.list-item { display: flex; gap: 12px; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--line); cursor: pointer; }
.list-item:last-child { border-bottom: none; }
.list-item:hover { background: var(--panel-2); }
.list { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pill { display: inline-block; font-size: 12px; padding: 2px 8px; border-radius: 20px; background: var(--panel-2); border: 1px solid var(--line); color: var(--muted); }
.pill.ai { background: var(--ai-soft); color: var(--ai); border-color: transparent; }
.pill.ok { color: var(--ok); }
.pill.warn { color: var(--warn); }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; background: var(--muted); }
.dot.online { background: var(--ok); } .dot.offline { background: var(--warn); } .dot.error { background: var(--bad); }

/* Avatars */
.av { width: 44px; height: 44px; border-radius: 50%; flex: none; object-fit: cover; background: var(--brand-soft); color: var(--brand); display: grid; place-items: center; font-weight: 700; overflow: hidden; }
.av.sm { width: 36px; height: 36px; font-size: 14px; }

/* Inbox */
.inbox { display: grid; grid-template-columns: 340px 1fr; height: 100%; }
.inbox.with-info { grid-template-columns: 340px 1fr 320px; }
.convs { border-right: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; min-height: 0; }
.convs-head { padding: 12px; display: flex; flex-direction: column; gap: 8px; border-bottom: 1px solid var(--line); }
.convs-head .row select { flex: 1; }
.convs-list { overflow-y: auto; flex: 1; }
.conv { display: flex; gap: 10px; padding: 10px 12px; cursor: pointer; border-bottom: 1px solid var(--line); }
.conv:hover { background: var(--panel-2); }
.conv.on { background: var(--brand-soft); }
.conv .meta { flex: 1; min-width: 0; }
.conv .top { display: flex; gap: 6px; align-items: baseline; }
.conv .name { font-weight: 600; flex: 1; min-width: 0; }
.conv.unread .name, .conv.unread .last { font-weight: 700; color: var(--text); }
.conv .time { font-size: 12px; color: var(--muted); flex: none; }
.conv .last { color: var(--muted); font-size: 14px; }
.conv .tags { display: flex; gap: 4px; margin-top: 3px; flex-wrap: wrap; }
.conv .tags .pill { font-size: 11px; padding: 0 6px; }

.chat { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--bg); position: relative; }
.chat-head { display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--line); min-height: 60px; }
.chat-head .title { flex: 1; min-width: 0; }
.chat-head .title b { display: block; }
.empty { flex: 1; display: grid; place-items: center; color: var(--muted); text-align: center; padding: 20px; }
.msgs { flex: 1; overflow-y: auto; padding: 14px 14px 8px; display: flex; flex-direction: column; gap: 4px; overscroll-behavior: contain; }
.day { align-self: center; font-size: 12px; color: var(--muted); background: var(--panel); border: 1px solid var(--line); padding: 2px 10px; border-radius: 20px; margin: 10px 0; }
.m { max-width: min(75%, 560px); display: flex; flex-direction: column; }
.m.in { align-self: flex-start; }
.m.out { align-self: flex-end; align-items: flex-end; }
.m .who { font-size: 12px; color: var(--muted); margin: 6px 4px 2px; }
.m .who.ai { color: var(--ai); }
.bubble { background: var(--in); border: 1px solid var(--line); padding: 8px 12px; border-radius: 16px; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }
.m.out .bubble { background: var(--out); border-color: transparent; }
.m.out.by-ai .bubble { background: var(--ai-soft); }
.bubble .t { font-size: 11px; color: var(--muted); margin-left: 8px; float: right; margin-top: 6px; }
.media-img { max-width: min(280px, 100%); max-height: 320px; border-radius: 14px; display: block; cursor: zoom-in; background: var(--panel-2); }
.media-video { max-width: min(320px, 100%); max-height: 360px; border-radius: 14px; display: block; background: #000; }
.file { display: flex; gap: 10px; align-items: center; text-decoration: none; color: var(--text); }
.file .ico { font-size: 26px; }
.m audio { max-width: 260px; }

.suggest { margin: 0 12px 8px; background: var(--ai-soft); border-radius: 14px; padding: 10px 12px; border: 1px dashed var(--ai); }
.suggest .head { font-size: 12px; color: var(--ai); font-weight: 700; margin-bottom: 4px; }
.suggest .txt { white-space: pre-wrap; }
.suggest .row { margin-top: 8px; gap: 6px; flex-wrap: wrap; }
.banner { margin: 0 12px 8px; padding: 8px 12px; border-radius: 10px; background: #fff7e6; color: #92400e; font-size: 14px; display: flex; gap: 8px; align-items: center; }
@media (prefers-color-scheme: dark) { .banner { background: #3b2a0b; color: #fcd9a0; } }

.composer { background: var(--panel); border-top: 1px solid var(--line); padding: 8px; padding-bottom: max(8px, env(safe-area-inset-bottom)); }
.composer .row { align-items: flex-end; gap: 6px; }
.composer textarea { min-height: 42px; max-height: 160px; resize: none; border-radius: 20px; padding: 10px 14px; }
.attachments { display: flex; gap: 8px; flex-wrap: wrap; padding: 4px 4px 8px; }
.att { position: relative; width: 72px; height: 72px; border-radius: 10px; background: var(--panel-2); border: 1px solid var(--line); display: grid; place-items: center; font-size: 11px; text-align: center; overflow: hidden; padding: 4px; }
.att img { width: 100%; height: 100%; object-fit: cover; position: absolute; inset: 0; }
.att .x { position: absolute; top: 2px; right: 2px; width: 22px; height: 22px; border-radius: 50%; border: none; background: rgba(0,0,0,.6); color: #fff; cursor: pointer; z-index: 1; }
.drop { position: absolute; inset: 0; background: rgba(11,92,255,.12); border: 3px dashed var(--brand); display: grid; place-items: center; font-size: 18px; font-weight: 700; color: var(--brand); z-index: 5; pointer-events: none; }

.info { background: var(--panel); border-left: 1px solid var(--line); overflow-y: auto; padding: 16px; }
.info h4 { margin: 18px 0 6px; font-size: 14px; }
.seg { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; background: var(--panel-2); padding: 4px; border-radius: 10px; border: 1px solid var(--line); }
.seg button { border: none; background: transparent; padding: 7px 2px; border-radius: 8px; cursor: pointer; font-size: 13px; }
.seg button.on { background: var(--panel); box-shadow: 0 1px 3px rgba(0,0,0,.15); font-weight: 700; color: var(--brand); }

/* Modal & lightbox */
.overlay { position: fixed; inset: 0; background: rgba(10, 14, 22, .55); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal { background: var(--panel); border-radius: 16px; width: 100%; max-width: 480px; max-height: 90dvh; overflow: auto; padding: 20px; box-shadow: var(--shadow); }
.modal h3 { margin: 0 0 8px; }
.modal .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; flex-wrap: wrap; }
.qr { width: 240px; height: 240px; margin: 12px auto; display: grid; place-items: center; background: #fff; border-radius: 12px; }
.qr img { width: 100%; }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.92); z-index: 60; display: grid; place-items: center; }
.lightbox img { max-width: 100vw; max-height: 100dvh; object-fit: contain; }
.lightbox .close { position: fixed; top: 12px; right: 12px; background: rgba(255,255,255,.15); color: #fff; border: none; }
.lightbox .dl { position: fixed; top: 12px; right: 64px; background: rgba(255,255,255,.15); color: #fff; border: none; text-decoration: none; }

#toasts { position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%); z-index: 70; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
.toast { background: #111827; color: #fff; padding: 10px 16px; border-radius: 10px; box-shadow: var(--shadow); font-size: 14px; max-width: 90vw; }
.toast.error { background: var(--bad); }

/* Mobile */
@media (max-width: 900px) {
  .inbox.with-info { grid-template-columns: 340px 1fr; }
  .inbox.with-info .info { position: fixed; inset: 0; z-index: 40; border: none; }
}
@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  .nav { flex-direction: row; justify-content: space-around; border-right: none; border-top: 1px solid var(--line); padding: 4px 0; padding-bottom: max(4px, env(safe-area-inset-bottom)); order: 2; }
  .nav .logo { display: none; }
  .main { height: auto; min-height: 0; }
  .shell.chatting .nav { display: none; }
  .shell.chatting { grid-template-rows: 1fr; }
  .inbox, .inbox.with-info { grid-template-columns: 1fr; }
  .inbox.open .convs { display: none; }
  .inbox:not(.open) .chat { display: none; }
  .page { padding: 16px; }
  .grid2 { grid-template-columns: 1fr; }
  .m { max-width: 85%; }
  .hide-m { display: none !important; }
  input, select, textarea { font-size: 16px; } /* chặn iOS tự phóng to */
}

/* v2: sản phẩm, bài đăng */
.page-inner.wide { max-width: 1200px; }
.btn.sm { min-height: 30px; padding: 4px 10px; font-size: 13px; border-radius: 8px; }
.pill.bad { color: var(--bad); }
.pgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }
.pcard { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; cursor: pointer; display: flex; flex-direction: column; }
.pcard:hover { box-shadow: var(--shadow); }
.pcard.off { opacity: .55; }
.pcard img, .pcard .noimg { width: 100%; aspect-ratio: 1; object-fit: cover; background: var(--panel-2); display: grid; place-items: center; font-size: 40px; }
.pcard .pb { padding: 8px 10px 10px; display: flex; flex-direction: column; gap: 2px; }
.pcard .pn { font-weight: 600; font-size: 14px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; min-height: 40px; }
.pcard .pp { color: #e11d48; font-weight: 800; }
.imgs { display: flex; gap: 8px; flex-wrap: wrap; }
.att.pick { cursor: pointer; opacity: .45; border: 3px solid transparent; }
.att.pick.on { opacity: 1; border-color: var(--brand); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.chip { display: inline-flex; gap: 6px; align-items: center; background: var(--brand-soft); color: var(--text); border-radius: 20px; padding: 4px 6px 4px 12px; font-size: 13px; }
.chip button { border: none; background: transparent; cursor: pointer; color: var(--muted); }
code.copy { background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px; padding: 1px 6px; cursor: copy; word-break: break-all; }
@media (max-width: 760px) {
  .nav a { width: auto; flex: 1; font-size: 10px; }
  .pgrid { grid-template-columns: repeat(2, 1fr); gap: 8px; }
}

/* v3: phòng */
.grid3 { display: grid; grid-template-columns: 1fr 1fr 110px; gap: 0 10px; }
.grid4 { display: grid; grid-template-columns: 1.2fr 1fr 1fr 1fr; gap: 6px; }
.pill.warn { color: var(--warn); }
.legend { display: flex; gap: 6px; align-items: center; margin-bottom: 8px; flex-wrap: wrap; color: var(--muted); }
.legend .cell { display: inline-block; width: 14px; height: 14px; border-radius: 3px; margin-left: 8px; }
.cal-wrap { overflow-x: auto; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }
table.cal { border-collapse: collapse; font-size: 11px; }
table.cal th { padding: 4px 2px; font-weight: 600; color: var(--muted); text-align: center; min-width: 30px; }
table.cal th.we { color: #e11d48; }
table.cal th.today { background: var(--brand-soft); color: var(--brand); }
table.cal th.rn { position: sticky; left: 0; background: var(--panel); text-align: left; min-width: 150px; max-width: 180px; padding: 6px 8px; border-right: 1px solid var(--line); cursor: pointer; font-size: 13px; color: var(--text); z-index: 1; }
table.cal td { border: 1px solid var(--panel); height: 44px; cursor: pointer; }
.cell.free { background: #dcfce7; }
.cell.ical { background: #fca5a5; cursor: not-allowed; }
.cell.manual { background: repeating-linear-gradient(45deg, #fde68a, #fde68a 4px, #fcd34d 4px, #fcd34d 8px); }
@media (prefers-color-scheme: dark) { .cell.free { background: #14532d; } .cell.ical { background: #7f1d1d; } }
@media (max-width: 760px) { .grid3 { grid-template-columns: 1fr 1fr; } .grid4 { grid-template-columns: 1fr 1fr; } table.cal th.rn { min-width: 110px; } }
@media (min-width: 761px) { .show-m { display: none !important; } }

/* sửa hiển thị */
a.btn { text-decoration: none; color: inherit; } a.btn.primary { color: var(--brand-ink); }
.grid2 > *, .grid3 > *, .grid4 > * { min-width: 0; }
input[type=date] { min-width: 0; }
