/* =====================================================================
   App shell: sidebar / topbar / content / footer / page-header.
   Chỉ dùng token (tokens.css). Không hex, không !important.
   ===================================================================== */

html { height: 100%; }
body {
  min-height: 100%;
  margin: 0;
  background: var(--sf-bg);
  color: var(--tx-1);
  font-family: var(--ff-sans);
  font-size: var(--fs-md);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  font-feature-settings: "cv11", "ss01", "tnum" 0;
}
a { color: var(--c-primary); text-decoration: none; }
a:hover { color: var(--c-primary-hover); }
code, kbd, pre, .mono { font-family: var(--ff-mono); font-size: 0.92em; }
kbd { padding: 1px 5px; border: 1px solid var(--sf-border); border-bottom-width: 2px; border-radius: var(--r-sm); background: var(--sf-surface); color: var(--tx-2); font-size: var(--fs-xs); }
.tnum { font-variant-numeric: tabular-nums; }
.text-2 { color: var(--tx-2); }
.text-3 { color: var(--tx-3); }
.fs-xs { font-size: var(--fs-xs); }
.fs-sm { font-size: var(--fs-sm); }
.fs-lg { font-size: var(--fs-lg); }
*:focus-visible { outline: none; box-shadow: var(--focus-ring); border-radius: var(--r-sm); }
::selection { background: rgba(var(--c-primary-rgb), 0.18); }

/* ---------- Layout ---------- */
.app { display: flex; min-height: 100vh; }

.app-sidebar {
  position: fixed; inset: 0 auto 0 0;
  width: var(--sidebar-w);
  background: var(--sidebar-bg);
  border-right: 1px solid var(--sf-border);
  display: flex; flex-direction: column;
  z-index: 1035;
  transition: width .16s ease, transform .2s ease;
}
.app-main {
  flex: 1; min-width: 0;
  margin-left: var(--sidebar-w);
  display: flex; flex-direction: column;
  transition: margin-left .16s ease;
}
body.sidebar-collapsed .app-sidebar { width: var(--sidebar-w-collapsed); }
body.sidebar-collapsed .app-main { margin-left: var(--sidebar-w-collapsed); }

/* ---------- Brand + toggle ---------- */
.sidebar-head {
  height: var(--topbar-h);
  display: flex; align-items: center; gap: 8px;
  padding: 0 10px 0 14px;
  border-bottom: 1px solid var(--sf-border);
}
.brand {
  display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1;
  color: var(--tx-1); text-decoration: none; overflow: hidden; white-space: nowrap;
}
.brand:hover { color: var(--tx-1); }
/* Logo phụ (mascot) — dùng ở sidebar thu gọn, login, trang lỗi */
.brand-mark {
  flex: 0 0 32px; width: 32px; height: 32px;
  object-fit: contain; display: block;
}
/* Logo chính (wordmark): bản sáng/tối theo theme */
.brand-logo { height: 38px; width: auto; display: block; }
.brand .brand-mark { display: none; }
.brand-logo-dark { display: none; }
[data-bs-theme="dark"] .brand-logo-light { display: none; }
[data-bs-theme="dark"] .brand-logo-dark { display: block; }

.sidebar-toggle {
  flex: 0 0 28px; width: 28px; height: 28px; padding: 0;
  border: 0; border-radius: var(--r-md); background: transparent; color: var(--tx-3);
  display: inline-grid; place-items: center; cursor: pointer;
  transition: background .12s, color .12s;
}
.sidebar-toggle:hover { background: var(--sf-surface-3); color: var(--tx-1); }
.sidebar-toggle i { font-size: 15px; }
.sidebar-toggle .bi-chevron-bar-right { display: none; }
body.sidebar-collapsed .sidebar-toggle .bi-chevron-bar-left { display: none; }
body.sidebar-collapsed .sidebar-toggle .bi-chevron-bar-right { display: inline; }

/* ---------- Nav ---------- */
.app-nav { padding: 10px 8px; display: flex; flex-direction: column; gap: 1px; }
.app-nav-label {
  padding: 14px 10px 4px; font-size: 10.5px; font-weight: 600;
  letter-spacing: .06em; text-transform: uppercase; color: var(--tx-3);
}
.nav-link-item {
  position: relative;
  display: flex; align-items: center; gap: 10px;
  height: 32px; padding: 0 10px; border-radius: var(--r-md);
  color: var(--tx-2); font-weight: 500; font-size: var(--fs-md); text-decoration: none;
  white-space: nowrap; overflow: hidden;
  border: 1px solid transparent;
  transition: background .12s, color .12s;
}
.nav-link-item i { font-size: 15px; width: 18px; text-align: center; flex: 0 0 18px; color: var(--tx-3); }
.nav-link-item:hover { background: var(--sf-surface-3); color: var(--tx-1); }
.nav-link-item:hover i { color: var(--tx-2); }
.nav-link-item.is-active { background: var(--sidebar-active-bg); color: var(--tx-1); border-color: var(--sf-border); box-shadow: var(--sh-1); }
.nav-link-item.is-active i { color: var(--c-primary); }
.app-sidebar-foot { margin-top: auto; padding: 10px 8px; border-top: 1px solid var(--sf-border); }
.user-menu-btn {
  width: 100%; display: flex; align-items: center; gap: 10px; min-width: 0;
  padding: 6px 8px; border: 1px solid transparent; border-radius: var(--r-md);
  background: transparent; color: var(--tx-1); text-align: left; cursor: pointer;
}
.user-menu-btn:hover { background: var(--sf-surface-3); }
.avatar {
  flex: 0 0 28px; width: 28px; height: 28px; border-radius: 50%;
  background: var(--sf-surface-3); color: var(--tx-2);
  border: 1px solid var(--sf-border);
  display: grid; place-items: center; font-weight: 600; font-size: 11px;
}
.user-meta { min-width: 0; line-height: 1.2; flex: 1; }
.user-name { font-weight: 500; font-size: var(--fs-sm); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-sub { font-size: 10.5px; color: var(--tx-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-menu-btn .bi-chevron-expand { color: var(--tx-3); font-size: 13px; }

/* collapsed: icon rail */
body.sidebar-collapsed .sidebar-head { padding: 0; flex-direction: column; justify-content: center; gap: 2px; height: auto; padding: 8px 0; }
body.sidebar-collapsed .brand { flex: 0 0 auto; justify-content: center; }
body.sidebar-collapsed .brand .brand-logo { display: none; }
body.sidebar-collapsed .brand .brand-mark { display: block; }
body.sidebar-collapsed .app-nav-label,
body.sidebar-collapsed .app-nav-label,
body.sidebar-collapsed .nav-link-item span,
body.sidebar-collapsed .user-meta,
body.sidebar-collapsed .user-menu-btn .bi-chevron-expand { display: none; }
body.sidebar-collapsed .nav-link-item { justify-content: center; padding: 0; }
body.sidebar-collapsed .nav-link-item i { width: auto; }
body.sidebar-collapsed .user-menu-btn { justify-content: center; padding: 6px 0; }
body.sidebar-collapsed .app-sidebar-foot, body.sidebar-collapsed .app-nav { padding-left: 6px; padding-right: 6px; }

/* ---------- Topbar ---------- */
.app-topbar {
  position: sticky; top: 0; z-index: 1020;
  height: var(--topbar-h);
  background: var(--sf-surface);
  border-bottom: 1px solid var(--sf-border);
  display: flex; align-items: center; gap: 12px;
  padding: 0 20px;
}
.breadcrumb-lite {
  display: flex; align-items: center; gap: 6px; min-width: 0; flex: 1 1 auto;
  font-size: var(--fs-md); color: var(--tx-3);
}
.breadcrumb-lite a { color: var(--tx-2); }
.breadcrumb-lite a:hover { color: var(--tx-1); }
.breadcrumb-lite .sep { font-size: 10px; color: var(--tx-3); }
.breadcrumb-lite .current { color: var(--tx-1); font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 8px; flex: 0 0 auto; }

.topbar-search { position: relative; }
.topbar-search .form-control {
  width: 240px; min-height: 32px; padding-left: 30px; padding-right: 40px;
  background: var(--sf-surface-2); border-color: var(--sf-border); font-size: var(--fs-sm);
}
.topbar-search .form-control:focus { background: var(--sf-surface); width: 300px; }
.topbar-search .bi-search { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--tx-3); font-size: 13px; pointer-events: none; }
.topbar-search kbd { position: absolute; right: 8px; top: 50%; transform: translateY(-50%); pointer-events: none; }

.icon-btn {
  width: 32px; height: 32px; padding: 0;
  border-radius: var(--r-md);
  border: 1px solid var(--sf-border);
  background: var(--sf-surface); color: var(--tx-2);
  display: inline-grid; place-items: center; cursor: pointer;
  transition: background .12s, color .12s, border-color .12s;
}
.icon-btn:hover { background: var(--sf-surface-2); color: var(--tx-1); border-color: var(--sf-border-strong); }
.icon-btn i { font-size: 15px; }

/* Segmented control (kiểu iOS: nền chìm, mục active nổi trắng) */
.segmented {
  display: inline-flex; gap: 2px; padding: 2px;
  border: 1px solid var(--sf-border); border-radius: var(--r-md);
  background: var(--sf-surface-2);
}
.segmented > * {
  border: 0; background: transparent; cursor: pointer;
  padding: 4px 10px; border-radius: var(--r-sm);
  font-size: var(--fs-sm); font-weight: 500; line-height: 1.4;
  color: var(--tx-2); text-decoration: none; white-space: nowrap;
  transition: background .12s, color .12s;
}
.segmented > *:hover { color: var(--tx-1); }
.segmented > .is-active { background: var(--sf-surface); color: var(--tx-1); box-shadow: var(--sh-1); font-weight: 600; }

/* Tab shop (topbar): shop đang xem tô màu chính để luôn biết đang thao tác trên shop nào */
.shop-switch { align-items: center; }
.shop-switch .shop-switch-label { padding: 0 6px 0 8px; font-size: var(--fs-xs); font-weight: 600; color: var(--tx-3); text-transform: uppercase; letter-spacing: .04em; }
.shop-switch > button { font-variant-numeric: tabular-nums; }
.shop-switch > button.is-active { background: var(--c-primary); color: var(--tx-inverse); }

.theme-toggle .bi-sun-fill { display: none; }
[data-bs-theme="dark"] .theme-toggle .bi-sun-fill { display: inline; }
[data-bs-theme="dark"] .theme-toggle .bi-moon-fill { display: none; }

/* ---------- Content ---------- */
.app-content {
  flex: 1; width: 100%; max-width: var(--content-max);
  margin: 0 auto; padding: 24px 24px 32px;
}
.app-content.is-wide { max-width: none; }
.app-footer {
  padding: 12px 24px; font-size: var(--fs-xs); color: var(--tx-3);
  border-top: 1px solid var(--sf-border);
  display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap;
}

/* ---------- Page header ---------- */
.page-header {
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 16px; margin-bottom: 18px; flex-wrap: wrap;
}
.page-header-main { min-width: 0; }
.page-header h1 { font-size: var(--fs-xl); font-weight: 600; margin: 0; letter-spacing: -.02em; line-height: 1.25; }
.page-header .page-desc { color: var(--tx-2); font-size: var(--fs-sm); margin: 3px 0 0; }
.page-header .page-meta { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.page-header .page-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.page-crumbs { font-size: var(--fs-sm); color: var(--tx-3); margin-bottom: 6px; display: flex; gap: 6px; align-items: center; }
.page-crumbs a { color: var(--tx-2); }

/* ---------- Flash / errors ---------- */
.page-alerts { margin-bottom: 16px; }

/* ---------- Mobile ---------- */
.sidebar-backdrop {
  position: fixed; inset: 0; background: var(--sf-overlay);
  z-index: 1030; display: none;
}
.topbar-menu-btn, .topbar-brand, .lang-menu { display: none; }
.topbar-brand img { width: 30px; height: 30px; object-fit: contain; display: block; }
.lang-menu-btn { width: auto; padding: 0 8px; gap: 4px; grid-auto-flow: column; }
.lang-menu-btn .lang-code { font-size: var(--fs-xs); font-weight: 600; }
.lang-menu .dropdown-menu { min-width: 180px; }
.lang-menu .dropdown-item.active { background: var(--c-primary-soft); color: var(--c-primary); }
.lang-menu .dropdown-item.active i { color: var(--c-primary); }
@media (max-width: 991.98px) {
  .app-sidebar { transform: translateX(-100%); width: var(--sidebar-w); }
  body.sidebar-collapsed .app-sidebar { width: var(--sidebar-w); }
  body.sidebar-collapsed .sidebar-head { flex-direction: row; height: var(--topbar-h); padding: 0 10px 0 14px; }
  /* drawer mobile luôn hiện logo chính */
  body.sidebar-collapsed .brand .brand-mark, body.sidebar-collapsed .brand .brand-logo-dark { display: none; }
  body.sidebar-collapsed .brand .brand-logo-light, [data-bs-theme="dark"] body.sidebar-collapsed .brand .brand-logo-dark { display: block; }
  [data-bs-theme="dark"] body.sidebar-collapsed .brand .brand-logo-light { display: none; }
  body.sidebar-collapsed .nav-link-item span, body.sidebar-collapsed .user-meta, body.sidebar-collapsed .app-nav-label { display: initial; }
  body.sidebar-collapsed .nav-link-item { justify-content: flex-start; padding: 0 10px; }
  .sidebar-toggle { display: none; }
  .topbar-menu-btn { display: inline-grid; }
  .app-main, body.sidebar-collapsed .app-main { margin-left: 0; }
  body.sidebar-open .app-sidebar { transform: translateX(0); }
  body.sidebar-open .sidebar-backdrop { display: block; }
  body.sidebar-open { overflow: hidden; }
  .app-topbar { padding: 0 12px; gap: 8px; }
  .topbar-brand { display: block; flex: 0 0 auto; }
  .app-content { padding: 16px 16px 24px; }
  .topbar-search { display: none; }
}
@media (max-width: 575.98px) {
  .page-header h1 { font-size: 18px; }
  .page-header .page-actions { width: 100%; }
  .page-header .page-actions .btn { flex: 1; }
  .topbar-right .segmented { display: none; }
  .topbar-right .segmented.shop-switch { display: inline-flex; }
  .shop-switch .shop-switch-label { display: none; }
  .lang-menu { display: block; }
  /* vùng chạm tối thiểu ~40px */
  .app-topbar .icon-btn { width: 38px; height: 38px; }
  .app-topbar .lang-menu-btn { width: auto; }
  .lang-menu .dropdown-item { padding: 10px 12px; }
  .app-content { padding: 14px 12px 20px; }
  /* iOS Safari tự zoom khi focus input < 16px */
  .form-control, .form-select { font-size: 16px; }
}

/* ---------- Print ---------- */
@media print {
  .app-sidebar, .app-topbar, .app-footer, .page-actions { display: none; }
  .app-main { margin: 0; }
}
