/* ==========================================================================
   Giao dien web quan ly bot ban hang Zalo.
   Khong dung framework/CDN — chi CSS thuan + system font stack de chay duoc
   tren VPS khong co internet.
   ========================================================================== */

:root {
  --bg: #f4f6f9;
  --bg-panel: #ffffff;
  --bg-soft: #eef1f6;
  --bg-hover: #e7ecf3;
  --text: #17202a;
  --text-muted: #62707f;
  --border: #d8dfe8;
  --border-strong: #b9c4d1;

  --primary: #1a6ee0;
  --primary-text: #ffffff;
  --primary-soft: #e4efff;

  --ok: #17804a;
  --ok-soft: #e2f6ea;
  --warn: #9a6100;
  --warn-soft: #fdf0d8;
  --danger: #c02626;
  --danger-soft: #fdeaea;

  --radius: 10px;
  --shadow: 0 1px 3px rgba(16, 27, 43, .10), 0 6px 18px rgba(16, 27, 43, .06);
  --sidebar-w: 220px;
  --topbar-h: 52px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial,
          "Noto Sans", "Liberation Sans", sans-serif;
  --mono: ui-monospace, SFMono-Regular, "Cascadia Mono", Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12161c;
    --bg-panel: #1a2029;
    --bg-soft: #222a35;
    --bg-hover: #2b3644;
    --text: #e8edf4;
    --text-muted: #9aa8b8;
    --border: #2e3946;
    --border-strong: #435264;

    --primary: #4d95f5;
    --primary-text: #06101d;
    --primary-soft: #1c2c42;

    --ok: #5fd39b;
    --ok-soft: #163024;
    --warn: #f0b64a;
    --warn-soft: #33290f;
    --danger: #ff7b7b;
    --danger-soft: #3a1c1c;

    --shadow: 0 1px 3px rgba(0, 0, 0, .5), 0 6px 18px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

h1, h2, h3, h4 { margin: 0 0 .5rem; line-height: 1.25; }
h1 { font-size: 1.4rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 .6rem; }
a { color: var(--primary); }

.muted { color: var(--text-muted); }
.small { font-size: .85rem; }
.spacer { flex: 1; }
.nowrap { white-space: nowrap; }
.mono { font-family: var(--mono); font-size: .85rem; }
.pre-wrap { white-space: pre-wrap; word-break: break-word; }
.center { text-align: center; }
.row { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.mt { margin-top: 1rem; }

/* ---------- Nut bam ---------- */

.btn {
  font: inherit;
  font-weight: 600;
  /* Can thiet vi mot so nut la the <a> — inline se lam padding doc de len dong tren */
  display: inline-block;
  text-align: center;
  text-decoration: none;
  border: 1px solid var(--border-strong);
  background: var(--bg-panel);
  color: var(--text);
  padding: .5rem .85rem;
  border-radius: var(--radius);
  cursor: pointer;
  min-height: 38px;
}
.btn:hover:not(:disabled) { background: var(--bg-hover); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn-primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--primary); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { filter: brightness(1.08); background: var(--danger); }
.btn-ghost { background: transparent; }
.btn-block { width: 100%; }
.btn-sm { padding: .3rem .6rem; min-height: 30px; font-size: .85rem; }

.icon-btn {
  font: inherit; font-size: 1.2rem; line-height: 1;
  background: transparent; border: 0; color: inherit;
  padding: .4rem .6rem; border-radius: var(--radius); cursor: pointer;
}
.icon-btn:hover { background: var(--bg-hover); }

/* ---------- Form ---------- */

label { display: block; font-size: .85rem; font-weight: 600; margin: .6rem 0 .2rem; color: var(--text-muted); }

input[type=text], input[type=password], input[type=number], input[type=search], select, textarea {
  font: inherit;
  width: 100%;
  padding: .5rem .6rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--bg-panel);
  color: var(--text);
  min-height: 38px;
}
textarea { resize: vertical; line-height: 1.45; }
input:focus-visible, select:focus-visible, textarea:focus-visible, .btn:focus-visible, a:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}

.check { display: flex; align-items: center; gap: .5rem; font-weight: 500; color: var(--text); margin: .5rem 0; cursor: pointer; }
.check input { width: 18px; height: 18px; min-height: 0; accent-color: var(--primary); }

.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 0 1rem; }

/* ---------- Man hinh dang nhap ---------- */

.login-screen {
  min-height: 100%;
  display: flex; align-items: center; justify-content: center;
  padding: 1.5rem;
}
.login-box {
  width: 100%; max-width: 360px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 1.5rem;
}
.login-box h1 { margin-bottom: .15rem; }
.login-box .btn { margin-top: 1.1rem; }
.login-error {
  margin-top: .8rem; padding: .55rem .7rem;
  background: var(--danger-soft); color: var(--danger);
  border: 1px solid var(--danger); border-radius: var(--radius);
  font-size: .9rem; font-weight: 600;
}

/* ---------- Khung ung dung ---------- */

.shell { min-height: 100%; }

.topbar {
  position: sticky; top: 0; z-index: 30;
  height: var(--topbar-h);
  display: flex; align-items: center; gap: .4rem;
  padding: 0 .6rem;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
}
.topbar-title { font-weight: 700; }

.sidebar {
  position: fixed; top: 0; left: 0; bottom: 0;
  width: var(--sidebar-w);
  background: var(--bg-panel);
  border-right: 1px solid var(--border);
  padding: .75rem;
  display: flex; flex-direction: column; gap: .15rem;
  overflow-y: auto;
  z-index: 50;
  transform: translateX(-102%);
  transition: transform .18s ease;
}
.sidebar.open { transform: none; box-shadow: var(--shadow); }
.brand { font-weight: 800; font-size: 1.05rem; padding: .3rem .5rem 1rem; }

.navlink {
  display: flex; align-items: center; gap: .55rem;
  padding: .55rem .6rem;
  border-radius: var(--radius);
  color: var(--text); text-decoration: none; font-weight: 600; font-size: .93rem;
}
.navlink:hover { background: var(--bg-hover); }
.navlink.active { background: var(--primary-soft); color: var(--primary); }
.navlink .ico { font-size: 1rem; width: 1.2rem; text-align: center; }

.nav-foot { margin-top: auto; padding-top: 1rem; border-top: 1px solid var(--border); }
.nav-user { font-size: .88rem; margin-bottom: .5rem; overflow: hidden; text-overflow: ellipsis; }

.nav-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 45; }

.view { padding: 1rem; max-width: 1200px; }

@media (min-width: 900px) {
  .topbar { display: none; }
  .sidebar { transform: none; }
  .nav-backdrop { display: none !important; }
  .view { margin-left: var(--sidebar-w); padding: 1.5rem; }
}

/* ---------- The / bang ---------- */

.card {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 1rem;
}
.card > h2:first-child, .card > h3:first-child { margin-top: 0; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: .75rem; margin-bottom: 1rem; }
.stat {
  background: var(--bg-panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: .7rem .8rem;
}
.stat .num { font-size: 1.6rem; font-weight: 800; line-height: 1.1; }
.stat .lbl { font-size: .8rem; color: var(--text-muted); }
.stat.alert { border-color: var(--warn); background: var(--warn-soft); }
.stat.alert .num { color: var(--warn); }

.banner {
  border-radius: var(--radius); padding: 1rem;
  border: 2px solid var(--border); background: var(--bg-panel);
  margin-bottom: 1rem;
}
.banner .banner-title { font-size: 1.15rem; font-weight: 800; margin-bottom: .25rem; }
.banner-ok { border-color: var(--ok); background: var(--ok-soft); color: var(--ok); }
.banner-warn { border-color: var(--warn); background: var(--warn-soft); color: var(--warn); }
.banner-danger { border-color: var(--danger); background: var(--danger-soft); color: var(--danger); }
.banner-danger .banner-title { font-size: 1.35rem; }
.banner .btn { margin-top: .6rem; }

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { border-collapse: collapse; width: 100%; font-size: .9rem; }
th, td { text-align: left; padding: .5rem .6rem; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .02em; color: var(--text-muted); white-space: nowrap; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover { background: var(--bg-hover); }
tbody tr.selected { background: var(--primary-soft); }

.pill {
  display: inline-block; padding: .1rem .5rem; border-radius: 999px;
  font-size: .75rem; font-weight: 700; white-space: nowrap;
  border: 1px solid transparent;
}
.pill-muted { background: var(--bg-soft); color: var(--text-muted); border-color: var(--border); }
.pill-ok { background: var(--ok-soft); color: var(--ok); border-color: var(--ok); }
.pill-warn { background: var(--warn-soft); color: var(--warn); border-color: var(--warn); }
.pill-danger { background: var(--danger-soft); color: var(--danger); border-color: var(--danger); }
.pill-info { background: var(--primary-soft); color: var(--primary); border-color: var(--primary); }

.empty { padding: 1.5rem; text-align: center; color: var(--text-muted); }

hr { border: 0; border-top: 1px solid var(--border); margin: 1rem 0; }

.toolbar { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; margin-bottom: .75rem; }
.toolbar input[type=search] { flex: 1 1 200px; min-width: 160px; }
.toolbar select { width: auto; min-width: 140px; }

/* ---------- Lead: panel chi tiet ---------- */

.split { display: grid; grid-template-columns: 1fr; gap: 1rem; }
@media (min-width: 1000px) {
  .split { grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr); align-items: start; }
  .split .detail { position: sticky; top: 1rem; max-height: calc(100vh - 2rem); overflow-y: auto; }
}

/* ---------- Inbox ---------- */

.inbox { display: grid; grid-template-columns: 1fr; gap: 1rem; }
@media (min-width: 900px) {
  .inbox { grid-template-columns: 300px minmax(0, 1fr); height: calc(100vh - 3rem); }
}

.conv-list {
  background: var(--bg-panel); border: 1px solid var(--border);
  border-radius: var(--radius); overflow-y: auto;
}
.conv-item {
  display: block; width: 100%; text-align: left; font: inherit;
  background: transparent; border: 0; border-bottom: 1px solid var(--border);
  padding: .6rem .7rem; cursor: pointer; color: inherit;
}
.conv-item:hover { background: var(--bg-hover); }
.conv-item.active { background: var(--primary-soft); }
.conv-name { font-weight: 700; font-size: .92rem; }
.conv-last {
  color: var(--text-muted); font-size: .82rem;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.conv-tags { display: flex; gap: .25rem; flex-wrap: wrap; margin-top: .2rem; }

.chat {
  background: var(--bg-panel); border: 1px solid var(--border);
  border-radius: var(--radius); display: flex; flex-direction: column; min-height: 60vh;
}
.chat-head { padding: .6rem .8rem; border-bottom: 1px solid var(--border); display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.chat-body { flex: 1; overflow-y: auto; padding: .8rem; display: flex; flex-direction: column; gap: .6rem; min-height: 200px; }
.chat-foot { border-top: 1px solid var(--border); padding: .6rem .8rem; }

.msg { max-width: 78%; padding: .45rem .65rem; border-radius: 12px; font-size: .9rem; }
.msg .who { font-size: .7rem; font-weight: 700; opacity: .75; display: block; margin-bottom: .1rem; }
.msg .when { font-size: .7rem; opacity: .65; display: block; margin-top: .15rem; }
.msg-in { align-self: flex-start; background: var(--bg-soft); border: 1px solid var(--border); }
.msg-bot { align-self: flex-end; background: var(--primary-soft); border: 1px solid var(--primary); }
.msg-human { align-self: flex-end; background: var(--ok-soft); border: 1px solid var(--ok); }
.msg-sys { align-self: center; background: var(--warn-soft); border: 1px solid var(--warn); color: var(--warn); font-size: .78rem; }

/* Tren dien thoai: 1 cot — chon hoi thoai thi an danh sach, hien khung chat */
@media (max-width: 899px) {
  .inbox.show-chat .conv-list { display: none; }
  .inbox:not(.show-chat) .chat { display: none; }
  .conv-list { max-height: 70vh; }
}

.hide-mobile { display: none; }
@media (min-width: 900px) {
  .hide-desktop { display: none !important; }
  .hide-mobile { display: initial; }
}

/* ---------- QR ---------- */

.qr-box { text-align: center; padding: 1rem; }
.qr-box img {
  width: 260px; max-width: 100%; height: auto;
  background: #fff; padding: 10px; border-radius: var(--radius);
  border: 1px solid var(--border);
}

/* ---------- Khoi text (bao cao / prompt / JSON) ---------- */

.textblock {
  background: var(--bg-soft); border: 1px solid var(--border);
  border-radius: var(--radius); padding: .7rem .8rem;
  white-space: pre-wrap; word-break: break-word;
  font-family: var(--mono); font-size: .84rem; margin: 0;
  max-height: 420px; overflow: auto;
}
.reply-highlight {
  background: var(--primary-soft); border: 1px solid var(--primary);
  border-radius: var(--radius); padding: .8rem;
  white-space: pre-wrap; word-break: break-word; font-size: .98rem;
}

/* ---------- Toast ---------- */

.toasts {
  position: fixed; z-index: 200; left: 50%; bottom: 1rem;
  transform: translateX(-50%);
  display: flex; flex-direction: column; gap: .5rem;
  width: min(420px, calc(100vw - 2rem));
}
.toast {
  background: var(--bg-panel); border: 1px solid var(--border-strong);
  border-left: 5px solid var(--primary);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: .6rem .8rem; font-size: .9rem; word-break: break-word;
}
.toast.err { border-left-color: var(--danger); color: var(--danger); font-weight: 600; }
.toast.ok { border-left-color: var(--ok); }

/* ---------- Modal ---------- */

.modal {
  position: fixed; inset: 0; z-index: 300;
  background: rgba(0,0,0,.5);
  display: flex; align-items: center; justify-content: center; padding: 1rem;
}
.modal-card {
  background: var(--bg-panel); border: 1px solid var(--border);
  border-radius: 14px; box-shadow: var(--shadow);
  padding: 1.1rem; width: min(620px, 100%);
  max-height: 85vh; display: flex; flex-direction: column;
}
.modal-body { overflow: auto; margin-bottom: .8rem; }
.modal-actions { display: flex; gap: .5rem; justify-content: flex-end; }

[hidden] { display: none !important; }
