:root {
  --bg: #f4f5fa; --panel: #ffffff; --line: #e6e8f0; --text: #1d2033; --muted: #747a93;
  --accent: #6d5dfc; --accent-soft: #eeecff; --rail: #1b1d2e; --danger: #ef4444; --ok: #10b981;
  --bubble-in: #f1f2f7; --bubble-out: #6d5dfc; --note: #fff7d6;
  --fb: #1877f2; --ig: #d62976; --zalo: #0068ff; --web: #0ea5e9;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #11131d; --panel: #181b29; --line: #272b3d; --text: #e7e9f3; --muted: #9096b0; --accent-soft: #2a2752; --rail: #0b0c14; --bubble-in: #232738; --note: #3a3420; }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; height: 100dvh; font-family: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; font-size: 14px; color: var(--text); background: var(--bg); }
.hidden { display: none !important; }
.muted { color: var(--muted); } .small { font-size: 12px; } .error { color: var(--danger); min-height: 1em; }
.grow { flex: 1; min-width: 0; } .spacer { flex: 1; } .row { display: flex; gap: 6px; }
input, select, textarea { font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; outline: none; width: 100%; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
.btn { font: inherit; border: 1px solid var(--line); background: var(--panel); color: var(--text); padding: 8px 14px; border-radius: 8px; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn.danger { color: var(--danger); }
.icon-btn { border: 0; background: transparent; font-size: 18px; cursor: pointer; padding: 4px 8px; border-radius: 6px; color: var(--text); }
.icon-btn:hover { background: var(--accent-soft); }

/* Đăng nhập */
.login { height: 100%; display: grid; place-items: center; background: radial-gradient(circle at 20% 20%, #e3141b22, transparent 50%), var(--bg); }
.login-card { width: min(360px, calc(100% - 32px)); background: var(--panel); padding: 32px; border-radius: 16px; box-shadow: 0 20px 60px #0002; display: flex; flex-direction: column; gap: 10px; text-align: center; }
.login-card h1 { margin: 4px 0 0; }
.logo { width: 40px; height: 40px; border-radius: 12px; background: var(--accent); display: grid; place-items: center; font-size: 20px; }
.logo.big { width: 56px; height: 56px; font-size: 28px; margin: 0 auto; }

/* Khung chính */
.app { display: flex; height: 100%; }
.rail { width: 64px; background: var(--rail); display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 12px 0; }
.rail-btn { position: relative; width: 44px; height: 44px; border: 0; border-radius: 12px; background: transparent; font-size: 20px; cursor: pointer; filter: grayscale(.6); }
.rail-btn:hover, .rail-btn.active { background: #ffffff1a; filter: none; }
.badge { position: absolute; top: 2px; right: 2px; background: var(--danger); color: #fff; font-size: 10px; border-radius: 10px; padding: 1px 5px; font-weight: 700; }
.avatar-btn { width: 36px; height: 36px; border-radius: 50%; border: 0; color: #fff; font-weight: 700; cursor: pointer; }
.view { flex: 1; min-width: 0; }
.bell-wrap { position: relative; }
.notif-panel { position: absolute; left: 54px; bottom: 0; width: 340px; max-height: 70vh; overflow: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 10px 40px #0003; z-index: 20; }
.notif-panel .n { padding: 10px 14px; border-bottom: 1px solid var(--line); cursor: pointer; }
.notif-panel .n.unread { background: var(--accent-soft); }
.notif-panel .n:hover { background: var(--bg); }
.notif-head { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; font-weight: 600; border-bottom: 1px solid var(--line); }

/* Hội thoại */
.inbox { display: flex; height: 100%; }
.list-col { width: 340px; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; }
.list-head { padding: 12px; display: flex; flex-direction: column; gap: 8px; border-bottom: 1px solid var(--line); }
.chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chip { border: 1px solid var(--line); background: transparent; color: var(--text); border-radius: 20px; padding: 4px 10px; font-size: 12px; cursor: pointer; }
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conv { display: flex; gap: 10px; padding: 10px 12px; cursor: pointer; border-bottom: 1px solid var(--line); position: relative; }
.conv:hover { background: var(--bg); }
.conv.active { background: var(--accent-soft); }
.conv .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv.unread .name, .conv.unread .snippet { font-weight: 700; color: var(--text); }
.conv .snippet { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 13px; }
.conv .meta { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; font-size: 11px; color: var(--muted); }
.unread-dot { background: var(--danger); color: #fff; border-radius: 10px; padding: 0 6px; font-size: 11px; font-weight: 700; }
.line1 { display: flex; gap: 6px; align-items: center; }
.tags-mini { display: flex; gap: 3px; margin-top: 3px; flex-wrap: wrap; }
.tag { font-size: 11px; padding: 1px 7px; border-radius: 10px; color: #fff; }
.avatar { position: relative; width: 40px; height: 40px; flex: 0 0 40px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 700; }
.plat { position: absolute; right: -3px; bottom: -3px; width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--panel); font-size: 9px; display: grid; place-items: center; color: #fff; font-weight: 800; }
.plat.facebook { background: var(--fb); } .plat.instagram { background: var(--ig); } .plat.zalo { background: var(--zalo); } .plat.website { background: var(--web); }
.kind { font-size: 10px; padding: 0 6px; border-radius: 6px; background: var(--bg); color: var(--muted); border: 1px solid var(--line); }

.chat-col { flex: 1; min-width: 0; display: flex; }
.empty { margin: auto; text-align: center; color: var(--muted); }
.empty-icon { font-size: 48px; }
.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.chat-head { display: flex; gap: 10px; align-items: center; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
.chat-head select { width: auto; }
.chat-head .title { font-weight: 700; font-size: 15px; }
.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 6px; }
.post-banner { align-self: center; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 8px 14px; font-size: 13px; margin-bottom: 8px; }
.msg { max-width: 68%; padding: 8px 12px; border-radius: 16px; line-height: 1.4; white-space: pre-wrap; word-wrap: break-word; }
.msg.in { align-self: flex-start; background: var(--bubble-in); border-bottom-left-radius: 4px; }
.msg.out { align-self: flex-end; background: var(--bubble-out); color: #fff; border-bottom-right-radius: 4px; }
.msg.note { align-self: center; background: var(--note); border: 1px dashed #d4b106; max-width: 80%; font-size: 13px; }
.msg .by { display: block; font-size: 11px; opacity: .75; margin-top: 3px; }
.msg.err { outline: 2px solid var(--danger); }
.day { align-self: center; font-size: 11px; color: var(--muted); margin: 8px 0; }
.typing { padding: 0 16px 6px; font-size: 12px; color: var(--muted); font-style: italic; }
.composer { position: relative; background: var(--panel); border-top: 1px solid var(--line); padding: 8px 12px 12px; }
.composer-tools { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; gap: 8px; }
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { resize: none; max-height: 140px; }
.composer.noting textarea { background: var(--note); }
.toggle { display: flex; gap: 6px; align-items: center; font-size: 12px; cursor: pointer; }
.toggle input { width: auto; }
.qr-popup { position: absolute; bottom: 100%; left: 12px; right: 12px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 -6px 24px #0002; max-height: 220px; overflow: auto; }
.qr-item { padding: 8px 12px; cursor: pointer; border-bottom: 1px solid var(--line); }
.qr-item.sel, .qr-item:hover { background: var(--accent-soft); }
.qr-item b { color: var(--accent); }

.info-col { width: 290px; background: var(--panel); border-left: 1px solid var(--line); padding: 16px; display: flex; flex-direction: column; gap: 10px; overflow-y: auto; }
.info-col h3 { margin: 6px 0 0; font-size: 14px; }
.info-col label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.tag-picker { display: flex; flex-wrap: wrap; gap: 6px; }
.tag-picker .tag { cursor: pointer; opacity: .35; padding: 4px 10px; font-size: 12px; }
.tag-picker .tag.on { opacity: 1; }

/* Trang thống kê / cài đặt */
.page { overflow-y: auto; padding: 24px; }
.page h2 { margin: 0 0 16px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; margin-bottom: 20px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px; }
.card .num { font-size: 26px; font-weight: 800; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 16px; }
.bar-row { display: flex; align-items: center; gap: 8px; margin: 8px 0; }
.bar-row .lbl { width: 150px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar { height: 10px; border-radius: 5px; background: var(--accent); }
table { width: 100%; border-collapse: collapse; }
td, th { text-align: left; padding: 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
.form-inline { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.form-inline > * { flex: 1 1 140px; width: auto; }
.form-inline .btn { flex: 0 0 auto; }
code { background: var(--bg); padding: 2px 6px; border-radius: 4px; font-size: 12px; word-break: break-all; }
.section { margin-bottom: 18px; }

.toast { position: fixed; right: 16px; bottom: 16px; background: var(--rail); color: #fff; padding: 12px 16px; border-radius: 10px; max-width: 360px; box-shadow: 0 10px 30px #0004; cursor: pointer; z-index: 50; }
.mobile-only { display: none; }

@media (max-width: 1100px) { .info-col { position: fixed; right: 0; top: 0; bottom: 0; z-index: 15; box-shadow: -10px 0 30px #0003; } }
@media (max-width: 760px) {
  .rail { width: 52px; } .rail-btn { width: 40px; height: 40px; }
  .list-col { width: 100%; }
  .inbox.chatting .list-col { display: none; }
  .inbox:not(.chatting) .chat-col { display: none; }
  .mobile-only { display: inline-block; }
  .msg { max-width: 85%; }
  .chat-head { flex-wrap: wrap; padding: 8px; }
  .notif-panel { position: fixed; left: 56px; right: 8px; width: auto; bottom: 8px; }
  .page { padding: 16px; }
}

/* Ảnh trong chat */
.msg img.chat-img { display: block; max-width: 240px; max-height: 300px; border-radius: 12px; cursor: zoom-in; margin: 2px 0 4px; }
.img-preview { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.img-preview img { height: 64px; border-radius: 8px; border: 1px solid var(--line); }
.lightbox { position: fixed; inset: 0; background: #000c; display: grid; place-items: center; z-index: 60; cursor: zoom-out; }
.lightbox img { max-width: 94vw; max-height: 94vh; border-radius: 8px; }
.composer.dragging { outline: 2px dashed var(--accent); }

/* iPhone: vùng tai thỏ / thanh home */
.app { padding-top: env(safe-area-inset-top); }
.rail { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
.composer { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
@media (max-width: 760px) {
  .app { flex-direction: column-reverse; }
  .rail { width: 100%; height: auto; flex-direction: row; justify-content: space-around; padding: 6px 8px calc(6px + env(safe-area-inset-bottom)); }
  .rail .logo, .rail .spacer { display: none; }
  .view { min-height: 0; flex: 1; }
  .inbox, .page { height: 100%; }
  .notif-panel { left: 8px; right: 8px; bottom: calc(64px + env(safe-area-inset-bottom)); top: auto; }
  input, select, textarea { font-size: 16px; } /* tránh iPhone tự phóng to khi gõ */
  .chat-head select { max-width: 130px; }
  .msg img.chat-img { max-width: 70vw; }
  .chat-head .grow { flex: 1 1 calc(100% - 120px); }
  .chat-head .title, #chatSub { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .chat-head select { flex: 1; max-width: none; }
  .composer-tools .muted { display: none; }
  .toast { left: 12px; right: 12px; max-width: none; bottom: calc(76px + env(safe-area-inset-bottom)); }
}

/* Logo Mr Zin */
.logo { background: #fff; overflow: hidden; border-radius: 50%; }
.logo img { width: 100%; height: 100%; object-fit: contain; display: block; }
.logo.big { width: 84px; height: 84px; }

/* Đơn hàng */
.btn.small { padding: 4px 10px; font-size: 12px; }
.conv-orders { display: flex; flex-direction: column; gap: 6px; }
.order-mini { border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; cursor: pointer; display: flex; flex-direction: column; gap: 2px; }
.order-mini:hover { border-color: var(--accent); }
.ost-badge { color: #fff; font-size: 11px; padding: 1px 8px; border-radius: 10px; }
select.ost { width: auto; padding: 3px 6px; font-size: 12px; font-weight: 600; }
.page-head { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.page-head h2 { margin: 0; }
.order-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 10px; margin-top: 10px; }
.order-card { cursor: pointer; display: flex; flex-direction: column; gap: 4px; padding: 12px 14px; }
.order-card:hover { border-color: var(--accent); }
.order-card .line1 { gap: 8px; }
.modal { position: fixed; inset: 0; background: #0007; display: grid; place-items: center; z-index: 40; padding: 16px; }
.modal-card { background: var(--panel); border-radius: 14px; width: min(560px, 100%); max-height: calc(100dvh - 32px); display: flex; flex-direction: column; box-shadow: 0 20px 60px #0004; }
.modal-card form { display: flex; flex-direction: column; min-height: 0; }
.modal-head, .modal-foot { display: flex; align-items: center; gap: 8px; padding: 12px 16px; }
.modal-head { border-bottom: 1px solid var(--line); } .modal-head h3 { margin: 0; flex: 1; }
.modal-foot { border-top: 1px solid var(--line); flex-wrap: wrap; }
.modal-body { padding: 14px 16px; overflow-y: auto; display: flex; flex-direction: column; gap: 10px; }
.modal-body label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.item-row { display: flex; gap: 6px; margin-bottom: 6px; align-items: center; }
.item-row .it-name { flex: 1 1 auto; min-width: 0; }
.item-row .it-qty { flex: 0 0 64px; }
.item-row .it-price { flex: 0 0 110px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1.3fr; gap: 10px; }
.order-total { text-align: right; font-size: 15px; } .order-total b { font-size: 20px; color: var(--accent); }
@media (max-width: 760px) {
  .modal { padding: 0; place-items: end stretch; }
  .modal-card { width: 100%; border-radius: 16px 16px 0 0; max-height: 92dvh; }
  .modal-foot { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  .grid-3 { grid-template-columns: 1fr 1fr; } .grid-3 label:last-child { grid-column: span 2; }
  .item-row .it-price { flex-basis: 96px; } .item-row .it-qty { flex-basis: 54px; }
  #orderBtn .lbl { display: none; } #orderBtn { padding: 8px 10px; }
  .order-list { grid-template-columns: 1fr; }
}
