:root {
  --primary: #4f46e5;
  --primary-dark: #3730a3;
  --primary-soft: #eef2ff;
  --bg: #f8fafc;
  --panel: #ffffff;
  --text: #1e293b;
  --muted: #64748b;
  --border: #e2e8f0;
  --sidebar-w: 300px;
  --header-h: 60px;
  --bar-h: 44px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: 'Segoe UI', system-ui, -apple-system, Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--bg); color: var(--text);
  display: flex; flex-direction: column;
  height: 100vh; height: 100dvh;
  overflow: hidden;
  -webkit-text-size-adjust: 100%;
}
button, input, textarea { font-family: inherit; }
:focus-visible { outline: 3px solid rgba(79, 70, 229, .55); outline-offset: 2px; }
.skip-link { position: absolute; left: -999px; top: 0; background: #fff; padding: 8px 12px; z-index: 100; }
.skip-link:focus { left: 8px; top: 8px; }

/* ---------- Header ---------- */
.site-header {
  flex-shrink: 0; height: calc(var(--header-h) + var(--safe-t)); padding: var(--safe-t) 14px 0;
  display: flex; align-items: center; gap: 10px;
  background: var(--panel); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: inherit; min-width: 0; }
.brand-logo { width: 42px; height: 42px; flex-shrink: 0; }
.brand-logo .lg-shape { mix-blend-mode: multiply; }
.brand-text { display: flex; flex-direction: column; line-height: 1.15; min-width: 0; }
.brand-text .l1 { font-weight: 800; font-size: 1.05rem; letter-spacing: .02em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-text .l2 { font-size: .7rem; color: var(--muted); letter-spacing: .08em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.header-actions { margin-left: auto; display: flex; gap: 8px; align-items: center; }

.icon-btn {
  width: 42px; height: 42px; border-radius: 10px; border: 1px solid var(--border); background: var(--panel);
  display: none; align-items: center; justify-content: center; cursor: pointer; flex-shrink: 0;
}
.burger, .burger::before, .burger::after { display: block; width: 18px; height: 2px; background: var(--text); border-radius: 2px; position: relative; content: ""; }
.burger::before { position: absolute; top: -6px; left: 0; }
.burger::after { position: absolute; top: 6px; left: 0; }

.btn, .btn-pager {
  border: 1px solid var(--border); background: var(--panel); color: var(--text);
  border-radius: 9px; padding: 8px 12px; min-height: 38px; font-size: .86rem; font-weight: 600; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; white-space: nowrap;
}
.btn:hover, .btn-pager:hover { background: var(--primary-soft); border-color: #c7d2fe; }
.btn:disabled, .btn-pager:disabled { opacity: .4; cursor: default; background: var(--panel); }
.ml-auto { margin-left: auto; }

.menu-wrap { position: relative; }
.popmenu {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 60; min-width: 250px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 6px;
  box-shadow: 0 12px 32px rgba(15, 23, 42, .18);
}
.popmenu[hidden] { display: none; }
.popmenu button { display: block; width: 100%; text-align: left; border: none; background: none; padding: 11px 12px; border-radius: 8px; font-size: .9rem; cursor: pointer; }
.popmenu button:hover { background: var(--primary-soft); }
.popmenu .danger { color: #b91c1c; }
.popmenu .danger:hover { background: #fee2e2; }
.pm-stat { padding: 8px 12px 10px; font-size: .84rem; color: #15803d; font-weight: 700; border-bottom: 1px solid var(--border); margin-bottom: 4px; }

/* ---------- Thanh vị trí + Trước/Sau ---------- */
.topicbar {
  flex-shrink: 0; min-height: var(--bar-h); padding: 4px 14px; display: flex; align-items: center; gap: 10px;
  color: #fff; background: linear-gradient(90deg, #ea580c, #f59e0b);
}
.crumbs { flex: 1; min-width: 0; font-size: .85rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.crumbs .sep { margin: 0 8px; opacity: .75; }
.crumbs strong { font-weight: 800; }
.pager { display: flex; gap: 6px; flex-shrink: 0; }
.btn-pager { background: rgba(255, 255, 255, .22); border-color: rgba(255, 255, 255, .45); color: #fff; padding: 5px 12px; min-height: 34px; }
.btn-pager:hover { background: rgba(255, 255, 255, .38); border-color: #fff; }
.btn-pager:disabled { background: rgba(255, 255, 255, .12); }

/* ---------- Bố cục chính ---------- */
.app { display: flex; flex: 1; min-height: 0; position: relative; }
.sidebar {
  width: var(--sidebar-w); flex-shrink: 0; display: flex; flex-direction: column; min-height: 0;
  background: var(--panel); border-right: 1px solid var(--border);
}
.search-box { padding: 12px 12px 6px; }
.search-box input {
  width: 100%; padding: 10px 12px; border: 1.5px solid var(--border); border-radius: 10px; font-size: .92rem; background: var(--bg);
}
.search-box input:focus { outline: none; border-color: var(--primary); background: #fff; }
.nav { overflow-y: auto; padding: 8px 10px calc(24px + var(--safe-b)); flex: 1; overscroll-behavior: contain; }
.nav-group { margin-bottom: 14px; }
.nav-group-label {
  width: 100%; display: flex; align-items: center; justify-content: space-between;
  font-size: 1rem; font-weight: 700; color: var(--group-color, var(--muted)); letter-spacing: .03em;
  padding: 7px 10px 7px 8px; border: none; border-left: 3px solid var(--group-color, transparent);
  border-radius: 4px; background: transparent; cursor: pointer; text-align: left; margin-bottom: 4px;
}
.nav-group-label:hover { background: rgba(15, 23, 42, .045); }
.chev { transition: transform .2s ease; font-size: .85rem; }
.nav-group.open .chev { transform: rotate(90deg); }
.nav-group-items { display: none; flex-direction: column; }
.nav-group.open .nav-group-items { display: flex; }
.nav-divider { height: 8px; }
.nav-item {
  display: flex; align-items: center; gap: 10px; padding: 10px 10px; border-radius: 8px; border-left: 3px solid transparent;
  color: var(--text); text-decoration: none; font-size: .93rem; background: var(--item-bg-rest, transparent);
  margin-bottom: 1px; transition: background .15s, border-color .15s;
}
.nav-item:hover { background: var(--item-bg, var(--primary-soft)); }
.nav-item.active {
  background: linear-gradient(180deg, var(--item-bg, var(--primary-soft)) 0%, var(--item-bg-rest, var(--primary-soft)) 100%);
  color: var(--item-text, var(--primary-dark)); font-weight: 700; border-left-color: var(--item-color, var(--primary));
  box-shadow: 0 1px 3px rgba(0, 0, 0, .08);
}
.nav-dot {
  width: 9px; height: 9px; border-radius: 50%; flex-shrink: 0;
  background: radial-gradient(circle at 32% 28%, rgba(255,255,255,.95) 0%, rgba(255,255,255,.15) 35%, var(--item-color, #cbd5e1) 60%);
  box-shadow: 0 0 0 1px rgba(0,0,0,.06), 0 1px 2px rgba(0,0,0,.25);
}
.nav-label { flex: 1; min-width: 0; }
.ngp-badge { margin-left: auto; font-size: .72rem; font-weight: 700; color: #15803d; background: #dcfce7; border-radius: 10px; padding: 1px 7px; white-space: nowrap; }
.nav-empty { padding: 18px 12px; color: var(--muted); font-size: .9rem; }
.nav.searching .nav-group-items { display: flex; }
.nav.searching .nav-divider { display: none; }

.content-area { flex: 1; min-width: 0; position: relative; background: var(--bg); }
#frame { width: 100%; height: 100%; border: none; display: block; background: var(--bg); }
.loading {
  position: absolute; inset: 0 0 auto 0; z-index: 5; display: none; justify-content: center; align-items: center; gap: 10px;
  padding: 12px; font-size: .9rem; color: var(--muted); background: rgba(248, 250, 252, .92);
}
.loading.show { display: flex; }
.spinner { width: 18px; height: 18px; border: 3px solid #c7d2fe; border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.backdrop { display: none; position: absolute; inset: 0; z-index: 30; background: rgba(15, 23, 42, .4); }

/* ---------- Hộp thoại sao lưu ---------- */
.modal { position: fixed; inset: 0; z-index: 100; display: flex; align-items: center; justify-content: center; padding: 16px; background: rgba(15, 23, 42, .6); }
.modal[hidden] { display: none; }
.modal-card { background: #fff; border-radius: 14px; padding: 18px 20px; max-width: 560px; width: 100%; max-height: 92vh; overflow: auto; }
.modal-card h3 { margin: 0 0 6px; }
.modal-card p { font-size: .9rem; color: #334155; margin: 0 0 10px; line-height: 1.5; }
.modal-card textarea { width: 100%; height: 110px; font-family: Consolas, monospace; font-size: .8rem; border: 1px solid #cbd5e1; border-radius: 8px; padding: 8px; }
.modal-msg { font-size: .85rem; color: #15803d; margin-top: 6px; min-height: 1.3em; }
.modal-msg.err { color: #b91c1c; }
.modal-actions { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }

/* ---------- Máy tính bảng / điện thoại ---------- */
@media (max-width: 1100px) {
  :root { --sidebar-w: 270px; }
  .btn-label { display: none; }
  .btn { padding: 8px 11px; }
  .btn-pager .btn-label { display: inline; }
}
@media (max-width: 860px) {
  :root { --header-h: 56px; }
  .icon-btn { display: inline-flex; }
  .brand-logo { width: 36px; height: 36px; }
  .brand-text .l1 { font-size: .95rem; }
  .brand-text .l2 { display: none; }
  .sidebar {
    position: absolute; top: 0; left: 0; bottom: 0; z-index: 40; width: min(86vw, 340px);
    transform: translateX(-102%); transition: transform .25s ease; box-shadow: 0 10px 30px rgba(0, 0, 0, .2);
  }
  .app.menu-open .sidebar { transform: none; }
  .app.menu-open .backdrop { display: block; }
  .nav-item { padding: 12px 10px; font-size: 1rem; }
  .crumbs > span:nth-child(-n+2) { display: none; }
  .topicbar { padding: 4px 10px; }
  .site-header { padding-left: 10px; padding-right: 10px; }
}
@media (max-width: 480px) {
  .btn-pager .btn-label { display: none; }
  .btn-pager { padding: 5px 16px; font-size: 1.1rem; }
  .popmenu { position: fixed; left: 10px; right: 10px; top: calc(var(--header-h) + var(--safe-t) + 6px); min-width: 0; }
  .brand { gap: 7px; }
  .brand-logo { width: 30px; height: 30px; }
  .brand-text .l1 { font-size: .78rem; }
  .header-actions { gap: 6px; }
  .header-actions .btn { padding: 8px 9px; }
  .site-header { gap: 8px; }
}
@media (prefers-reduced-motion: reduce) {
  .sidebar, .chev, .nav-item { transition: none; }
  .spinner { animation-duration: 2s; }
}
@media print { .site-header, .topicbar, .sidebar { display: none !important; } }
