:root {
  --tp-navy: #1b2a4a;
  --tp-navy-2: #243552;
  --tp-accent: #2f6fed;
  --tp-bg: #f3f5f8;
  --tp-card: #ffffff;
  --tp-border: #e4e8ef;
  --tp-text: #1f2937;
  --tp-muted: #6b7280;
  --tp-danger: #dc3545;
  --tp-warn: #e67e22;
  --tp-sidebar-w: 248px;
  --tp-radius: 12px;
  --tp-bottom-nav-h: 0px;
  --tp-safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html {
  font-size: 14px;
  -webkit-text-size-adjust: 100%;
}

body.app-body {
  margin: 0;
  background: var(--tp-bg);
  color: var(--tp-text);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  overflow: hidden;
  height: 100vh;
  height: 100dvh;
}

/* ---------- Shell ---------- */
.app-shell {
  display: flex;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.app-sidebar {
  width: var(--tp-sidebar-w);
  background: var(--tp-navy);
  color: #fff;
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  height: 100dvh;
  flex-shrink: 0;
  z-index: 300;
}

.sidebar-brand {
  padding: 1.1rem 1rem;
  border-bottom: 1px solid rgba(255,255,255,.08);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .5rem;
}

.sidebar-brand-link {
  display: flex;
  align-items: center;
  gap: .75rem;
  text-decoration: none;
  color: #fff;
  min-width: 0;
}

.btn-sidebar-close {
  display: none;
  border: 0;
  background: rgba(255,255,255,.1);
  color: #fff;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  font-size: 1rem;
  flex-shrink: 0;
}

.brand-mark {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: var(--tp-accent);
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: .9rem;
  flex-shrink: 0;
}

.brand-text strong {
  display: block;
  font-size: 1rem;
  line-height: 1.1;
}

.brand-text small {
  color: rgba(255,255,255,.55);
  font-size: .72rem;
}

.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: .75rem .65rem 1rem;
  -webkit-overflow-scrolling: touch;
}

.nav-section {
  color: rgba(255,255,255,.4);
  font-size: .68rem;
  text-transform: uppercase;
  letter-spacing: .06em;
  margin: .85rem .55rem .35rem;
}

.app-sidebar .nav-link {
  display: block;
  color: rgba(255,255,255,.78);
  text-decoration: none;
  padding: .7rem .85rem;
  border-radius: 8px;
  margin-bottom: 2px;
  font-size: .95rem;
  transition: background .15s, color .15s;
  min-height: 44px;
}

.app-sidebar .nav-link:hover,
.app-sidebar .nav-link:focus {
  background: rgba(255,255,255,.08);
  color: #fff;
}

.app-sidebar .nav-link.active {
  background: rgba(47, 111, 237, .28);
  color: #fff;
  font-weight: 600;
}

.sidebar-footer {
  padding: .9rem;
  border-top: 1px solid rgba(255,255,255,.08);
}

.user-chip {
  display: flex;
  gap: .65rem;
  align-items: center;
  margin-bottom: .65rem;
}

.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: rgba(255,255,255,.15);
  display: grid;
  place-items: center;
  font-weight: 600;
  font-size: .8rem;
}

.user-name {
  font-size: .8rem;
  max-width: 140px;
}

.user-role {
  font-size: .68rem;
  color: rgba(255,255,255,.5);
}

.btn-logout {
  background: transparent;
  border: 1px solid rgba(255,255,255,.2);
  color: rgba(255,255,255,.85);
  font-size: .85rem;
  padding: .55rem .6rem;
  border-radius: 8px;
  min-height: 42px;
}

.btn-logout:hover {
  background: rgba(255,255,255,.08);
  color: #fff;
}

.app-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.app-topbar {
  background: var(--tp-card);
  border-bottom: 1px solid var(--tp-border);
  padding: .55rem 1rem;
  display: flex;
  align-items: center;
  gap: .75rem;
  flex-shrink: 0;
  z-index: 50;
}

.topbar-title h1 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
  line-height: 1.2;
}

.btn-sidebar-toggle {
  display: none;
  border: 1px solid var(--tp-border);
  background: #fff;
  border-radius: 8px;
  min-width: 42px;
  min-height: 42px;
  padding: .2rem .55rem;
  font-size: 1.2rem;
  line-height: 1;
}

.app-content {
  padding: 1.25rem 1.5rem 2rem;
  flex: 1;
  min-height: 0;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: calc(2rem + var(--tp-bottom-nav-h) + var(--tp-safe-bottom));
}

.app-content:has(.dash-fit),
.app-content:has(.dashboard) {
  padding: .7rem 1rem;
  padding-bottom: calc(.7rem + var(--tp-bottom-nav-h) + var(--tp-safe-bottom));
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.sidebar-backdrop {
  display: none;
}

.mobile-bottom-nav {
  display: none;
}

/* ---------- Dashboard (desktop + mobile) ---------- */
.dashboard {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: .75rem;
  height: 100%;
}

.dashboard-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: .65rem;
}

.dashboard-kpis {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: .55rem;
  flex: 1 1 420px;
  min-width: 0;
}

.dash-kpi {
  background: var(--tp-card);
  border: 1px solid var(--tp-border);
  border-radius: 12px;
  padding: .65rem .85rem;
  display: flex;
  flex-direction: column;
  gap: .15rem;
  min-width: 0;
}

.dash-kpi.ok { border-left: 4px solid #198754; }
.dash-kpi.danger { border-left: 4px solid var(--tp-danger); }
.dash-kpi.warn { border-left: 4px solid var(--tp-warn); }

.dash-kpi-label {
  color: var(--tp-muted);
  font-size: .72rem;
}

.dash-kpi-value {
  color: var(--tp-navy);
  font-size: 1.35rem;
  font-weight: 700;
  line-height: 1.15;
}

.dashboard-controls {
  display: flex;
  align-items: center;
  gap: .5rem;
  flex-wrap: wrap;
}

.dash-segment {
  display: inline-flex;
  background: #fff;
  border: 1px solid var(--tp-border);
  border-radius: 10px;
  overflow: hidden;
}

.dash-segment a {
  padding: .45rem .7rem;
  text-decoration: none;
  color: var(--tp-muted);
  font-size: .82rem;
  font-weight: 500;
  border-right: 1px solid var(--tp-border);
  min-height: 36px;
  display: inline-flex;
  align-items: center;
  white-space: nowrap;
}

.dash-segment a:last-child { border-right: 0; }

.dash-segment a.active {
  background: var(--tp-navy);
  color: #fff;
}

.dash-segment a:hover:not(.active) {
  background: #f8fafc;
  color: var(--tp-text);
}

.dashboard-custom {
  flex: 1 0 100%;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr)) auto auto;
  gap: .55rem;
  align-items: end;
  background: var(--tp-card);
  border: 1px solid var(--tp-border);
  border-radius: 12px;
  padding: .65rem .75rem;
}

.dashboard-custom-field label {
  display: block;
  font-size: .72rem;
  color: var(--tp-muted);
  margin-bottom: .2rem;
}

.dashboard-custom-hint {
  font-size: .78rem;
  color: var(--tp-muted);
  align-self: center;
}

.dashboard-charts {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: .65rem;
  min-height: 0;
}

.dashboard-card {
  background: var(--tp-card);
  border: 1px solid var(--tp-border);
  border-radius: 12px;
  padding: .7rem .8rem;
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
}

.dashboard-card-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: .5rem;
  margin-bottom: .5rem;
  flex-shrink: 0;
}

.dashboard-card-head h2 {
  margin: 0;
  font-size: .85rem;
  font-weight: 600;
  color: var(--tp-navy);
}

.dashboard-card-meta {
  font-size: .72rem;
  color: var(--tp-muted);
}

.dashboard-chart {
  position: relative;
  width: 100%;
  flex: 1 1 auto;
  height: 100%;
  min-height: 200px;
  overflow: hidden;
}

.dashboard-chart canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
}

.dashboard-ranks {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: .65rem;
}

.dashboard-rank-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.dashboard-rank-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: .5rem;
  padding: .35rem 0;
  border-bottom: 1px solid var(--tp-border);
  font-size: .84rem;
}

.dashboard-rank-list li:last-child { border-bottom: 0; }
.dashboard-rank-list code { font-size: .78rem; }
.dashboard-rank-list strong { color: var(--tp-navy); }

.dashboard-empty {
  margin: 0;
  color: var(--tp-muted);
  font-size: .82rem;
}

/* ---------- ProductStats fit helpers ---------- */
.dash-fit {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: .55rem;
  height: 100%;
}

.dash-fit:has(> .stats-chart-panel),
.dash-fit:has(> .dash-panel:last-child:not(.dash-ranks)) {
  grid-template-rows: auto minmax(0, 1fr);
}

.stats-select {
  display: flex;
  align-items: center;
  gap: .5rem;
  flex-wrap: wrap;
  min-width: 0;
  width: 100%;
}

.stats-select .form-select {
  width: auto;
  min-width: 160px;
  flex: 1;
}

.stats-kpis {
  grid-template-columns: repeat(4, minmax(90px, 1fr));
  flex: 1 1 100%;
}

.kpi-text {
  font-size: 1rem !important;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.stats-chart-panel { min-height: 0; }

.dash-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .65rem;
  flex-wrap: wrap;
}

.dash-kpis {
  display: grid;
  grid-template-columns: repeat(3, minmax(100px, 1fr));
  gap: .5rem;
  flex: 1;
  min-width: 0;
}

.kpi-compact { padding: .45rem .7rem; }
.kpi-compact .kpi-label { font-size: .7rem; margin-bottom: 0; }
.kpi-compact .kpi-value { font-size: 1.2rem; }

.dash-actions {
  display: flex;
  align-items: center;
  gap: .4rem;
  flex-wrap: wrap;
}

.dash-charts {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: .55rem;
  min-height: 0;
}

.dash-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: .55rem .7rem;
}

.dash-panel .panel-title {
  margin: 0 0 .25rem;
  font-size: .8rem;
}

.chart-box {
  flex: 1;
  min-height: 0;
  position: relative;
}

.dash-ranks {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: .55rem;
}

.rank-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.rank-list li {
  display: flex;
  justify-content: space-between;
  gap: .5rem;
  padding: .15rem 0;
  border-bottom: 1px solid var(--tp-border);
  font-size: .8rem;
}

.rank-list li:last-child { border-bottom: 0; }
.rank-list code { font-size: .75rem; }

/* ---------- Page helpers ---------- */
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 1.25rem;
  flex-wrap: wrap;
}

.page-header h1,
.page-header .page-title {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--tp-navy);
}

.page-header .page-sub {
  color: var(--tp-muted);
  margin-top: .2rem;
  font-size: .88rem;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: .5rem;
  align-items: center;
}

.filter-bar {
  background: var(--tp-card);
  border: 1px solid var(--tp-border);
  border-radius: var(--tp-radius);
  padding: .85rem 1rem;
  margin-bottom: 1rem;
}

.data-card {
  background: var(--tp-card);
  border: 1px solid var(--tp-border);
  border-radius: var(--tp-radius);
  overflow: hidden;
}

.data-card .table { margin-bottom: 0; }

.data-card .table thead th {
  background: #f8fafc;
  border-bottom: 1px solid var(--tp-border);
  color: #475569;
  font-weight: 600;
  font-size: .8rem;
  text-transform: uppercase;
  letter-spacing: .02em;
  white-space: nowrap;
}

.data-card .table td { vertical-align: middle; }

.empty-state {
  text-align: center;
  padding: 2.5rem 1rem;
  color: var(--tp-muted);
}

.kpi-card {
  background: var(--tp-card);
  border-radius: var(--tp-radius);
  padding: 1.1rem 1.2rem;
  border: 1px solid var(--tp-border);
}

.kpi-card.danger { border-left: 4px solid var(--tp-danger); }
.kpi-card.warn { border-left: 4px solid var(--tp-warn); }
.kpi-card.ok { border-left: 4px solid #198754; }

.kpi-label {
  color: var(--tp-muted);
  font-size: .82rem;
  margin-bottom: .25rem;
}

.kpi-value {
  font-size: 1.7rem;
  font-weight: 700;
  color: var(--tp-navy);
  line-height: 1.2;
}

.panel {
  background: var(--tp-card);
  border: 1px solid var(--tp-border);
  border-radius: var(--tp-radius);
  padding: 1rem 1.15rem;
}

.panel-title {
  font-size: .92rem;
  font-weight: 600;
  margin: 0 0 .75rem;
  color: var(--tp-navy);
}

.btn-export-pdf {
  background: #c0392b;
  border-color: #c0392b;
  color: #fff;
}

.btn-export-pdf:hover {
  background: #a93226;
  border-color: #a93226;
  color: #fff;
}

.btn-export-excel {
  background: #1e8449;
  border-color: #1e8449;
  color: #fff;
}

.btn-export-excel:hover {
  background: #196f3d;
  border-color: #196f3d;
  color: #fff;
}

/* ---------- Auth ---------- */
.auth-shell {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  background:
    radial-gradient(ellipse at top left, rgba(47,111,237,.12), transparent 50%),
    radial-gradient(ellipse at bottom right, rgba(27,42,74,.1), transparent 45%),
    var(--tp-bg);
  padding: 1.25rem;
  padding-bottom: calc(1.25rem + env(safe-area-inset-bottom, 0px));
  overflow: auto;
}

.auth-card {
  width: 100%;
  max-width: 420px;
  background: #fff;
  border: 1px solid var(--tp-border);
  border-radius: 16px;
  padding: 1.75rem 1.5rem;
  box-shadow: 0 8px 28px rgba(27, 42, 74, .08);
}

.auth-card .brand {
  display: flex;
  align-items: center;
  gap: .75rem;
  margin-bottom: 1.5rem;
}

.auth-card .form-control-lg {
  min-height: 48px;
  font-size: 1rem;
}

.auth-card .btn-lg {
  min-height: 48px;
}

/* Touch-friendly defaults */
.btn, .form-control, .form-select {
  min-height: 40px;
}

.btn-sm {
  min-height: 34px;
}

.table-responsive {
  -webkit-overflow-scrolling: touch;
}

/* ---------- Tablet / Mobile ---------- */
@media (max-width: 992px) {
  :root { --tp-bottom-nav-h: 64px; }

  body.app-body {
    overflow: hidden;
    height: 100vh;
    height: 100dvh;
  }

  body.sidebar-open {
    overflow: hidden;
  }

  .app-shell {
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
  }

  .app-main {
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    width: 100%;
  }

  .btn-sidebar-toggle,
  .btn-sidebar-close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, .45);
    z-index: 250;
  }

  .sidebar-backdrop[hidden] {
    display: none !important;
  }

  .app-sidebar {
    position: fixed;
    left: 0;
    top: 0;
    width: min(86vw, 300px);
    transform: translateX(-105%);
    transition: transform .22s ease;
    box-shadow: 8px 0 24px rgba(0,0,0,.2);
  }

  .app-sidebar.open {
    transform: translateX(0);
  }

  .mobile-bottom-nav {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    position: sticky;
    bottom: 0;
    z-index: 200;
    background: #fff;
    border-top: 1px solid var(--tp-border);
    padding: .25rem .2rem calc(.25rem + var(--tp-safe-bottom));
    flex-shrink: 0;
  }

  .mob-nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: .1rem;
    text-decoration: none;
    color: var(--tp-muted);
    background: transparent;
    border: 0;
    font-size: .68rem;
    min-height: 52px;
    border-radius: 10px;
    padding: .25rem .15rem;
  }

  .mob-nav-item .mob-ico {
    font-size: 1.05rem;
    line-height: 1;
  }

  .mob-nav-item.active,
  .mob-nav-item:active {
    color: var(--tp-accent);
    font-weight: 600;
  }

  .app-content {
    padding: 1rem;
    padding-bottom: calc(1rem + var(--tp-bottom-nav-h) + var(--tp-safe-bottom));
  }

  .app-content:has(.dash-fit),
  .app-content:has(.dashboard) {
    overflow: auto;
    display: block;
    padding: .75rem .75rem calc(.85rem + var(--tp-bottom-nav-h) + var(--tp-safe-bottom));
  }

  /* Mobile: flow layout — không ép fit 1 màn hình (tránh chart đè nhau) */
  .dashboard {
    display: block;
    height: auto !important;
    min-height: 0;
    overflow: visible;
  }

  .dashboard-toolbar,
  .dashboard-charts,
  .dashboard-ranks,
  .dashboard-card {
    min-height: 0;
    height: auto;
    overflow: visible;
  }

  .dashboard-kpis {
    flex: none;
    width: 100%;
    margin-bottom: .65rem;
  }

  .dashboard-controls {
    width: 100%;
    margin-bottom: .65rem;
  }

  .dash-segment {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
  }

  .dash-segment a {
    justify-content: center;
    min-height: 42px;
    padding: .55rem .35rem;
    font-size: .8rem;
  }

  .dash-report-btn {
    width: 100%;
    min-height: 42px;
  }

  .dashboard-custom {
    grid-template-columns: 1fr;
    margin-top: .5rem;
  }

  .dashboard-custom .btn {
    width: 100%;
    min-height: 44px;
  }

  .dashboard-charts {
    display: flex;
    flex-direction: column;
    gap: .75rem;
    min-height: auto !important;
    margin-bottom: .75rem;
  }

  .dashboard-charts .dashboard-card {
    display: block;
    min-height: auto;
    flex: none;
  }

  .dashboard-chart {
    display: block;
    width: 100%;
    height: 240px !important;
    min-height: 240px !important;
    max-height: 240px;
    flex: none !important;
    position: relative;
    overflow: hidden;
  }

  .dashboard-ranks {
    display: flex;
    flex-direction: column;
    gap: .65rem;
    min-height: auto;
  }

  .dash-fit {
    height: auto;
    min-height: calc(100dvh - 120px - var(--tp-bottom-nav-h));
    grid-template-rows: auto auto auto;
  }

  .dash-fit:has(> .stats-chart-panel) {
    grid-template-rows: auto minmax(240px, 55vh);
  }

  .dash-charts { grid-template-columns: 1fr; }
  .chart-box { height: 220px; min-height: 200px; }
  .dash-ranks { grid-template-columns: 1fr; }

  .page-header {
    flex-direction: column;
    align-items: stretch;
    gap: .75rem;
    margin-bottom: 1rem;
  }

  .page-header .page-title { font-size: 1.2rem; }

  .toolbar {
    width: 100%;
  }

  .toolbar .btn {
    flex: 1 1 auto;
    min-width: calc(50% - .35rem);
  }

  .filter-bar .row > [class*="col-"] {
    width: 100%;
    max-width: 100%;
    flex: 0 0 100%;
  }

  .filter-bar .form-control,
  .filter-bar .form-select,
  .filter-bar .btn {
    width: 100%;
  }

  .data-card {
    border-radius: 10px;
  }

  .data-card .table {
    font-size: .85rem;
  }

  .data-card .table th,
  .data-card .table td {
    padding: .55rem .6rem;
    white-space: nowrap;
  }

  /* Keep action buttons usable on narrow screens */
  .btn-group {
    flex-wrap: wrap;
  }
}

@media (max-width: 576px) {
  .dashboard-kpis {
    grid-template-columns: 1fr;
  }

  .dash-kpi {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    padding: .7rem .85rem;
  }

  .dash-kpi-value {
    font-size: 1.2rem;
  }

  .dash-kpis { grid-template-columns: 1fr 1fr; }
  .stats-kpis { grid-template-columns: 1fr 1fr; }

  .auth-card {
    padding: 1.35rem 1.1rem;
    border-radius: 14px;
  }

  .kpi-value { font-size: 1.4rem; }
  .kpi-compact .kpi-value { font-size: 1.05rem; }

  .topbar-title h1 {
    font-size: .98rem;
  }

  form.col-lg-7,
  form.col-lg-6,
  .col-lg-7, .col-lg-6 {
    width: 100% !important;
    max-width: 100% !important;
  }
}

@media print {
  .no-print,
  .app-sidebar,
  .app-topbar,
  .btn-sidebar-toggle,
  .mobile-bottom-nav,
  .sidebar-backdrop {
    display: none !important;
  }

  .app-shell { display: block; height: auto; }
  .app-content { padding: 0 !important; overflow: visible; }
  body { background: #fff; overflow: visible; height: auto; }
}
