/* ==========================================================================
   App chrome: sidebar + topbar + page layout
   ========================================================================== */

.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: var(--topbar-h) 1fr;
  min-height: 100vh;
  transition: grid-template-columns 0.18s ease;
}
.app[data-sidebar="collapsed"] {
  grid-template-columns: var(--sidebar-w-collapsed) 1fr;
}

/* ---------- Sidebar ---------- */
.sidebar {
  grid-row: 1 / 3;
  background: #FAFAFB;
  border-right: 1px solid var(--c-border-soft);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 16px;
  height: var(--topbar-h);
  border-bottom: 1px solid var(--c-border-soft);
  flex-shrink: 0;
}
.brand-mark {
  width: 28px;
  height: 28px;
  border-radius: var(--r-md);
  background: var(--c-primary);
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: var(--fw-semibold);
  font-size: 15px;
  flex-shrink: 0;
  letter-spacing: -0.5px;
}
.brand-name {
  font-size: 15px;
  font-weight: var(--fw-semibold);
  color: var(--c-ink);
  letter-spacing: 0.5px;
}
.brand-tag {
  font-size: var(--fs-10);
  color: var(--c-text-3);
  padding: 2px 5px;
  background: var(--c-bg-panel);
  border-radius: var(--r-sm);
  margin-left: auto;
}

.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: 8px 8px 16px;
}
.nav-group {
  margin-top: 10px;
}
.nav-group-title {
  font-size: var(--fs-11);
  color: var(--c-text-3);
  padding: 4px 10px 6px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  font-weight: var(--fw-medium);
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  height: 32px;
  padding: 0 10px;
  border-radius: var(--r-md);
  color: var(--c-text);
  font-size: var(--fs-13);
  cursor: pointer;
  margin-bottom: 1px;
  position: relative;
  white-space: nowrap;
}
.nav-item:hover { background: var(--c-bg-hover); }
.nav-item.active {
  background: var(--c-primary-soft);
  color: var(--c-primary-text);
  font-weight: var(--fw-medium);
}
.nav-item .ico {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  color: var(--c-text-2);
}
.nav-item.active .ico { color: var(--c-primary); }
.nav-item .badge {
  margin-left: auto;
  font-size: var(--fs-11);
  background: var(--c-danger);
  color: #fff;
  padding: 1px 5px;
  border-radius: var(--r-pill);
  min-width: 16px;
  text-align: center;
  font-family: var(--font-num);
}
.nav-item .chev {
  margin-left: auto;
  color: var(--c-text-3);
  transition: transform 0.15s ease;
}
.nav-item.open .chev { transform: rotate(90deg); }

.nav-sub {
  padding-left: 26px;
  display: none;
  margin: 4px 0 8px;
}
.nav-item.open + .nav-sub { display: block; }
.nav-sub .nav-item {
  height: 32px;
  font-size: var(--fs-13);
  color: var(--c-text-2);
  margin-bottom: 2px;
}
.nav-sub .nav-item.active {
  background: var(--c-primary-soft);
  color: var(--c-primary-text);
}

.app[data-sidebar="collapsed"] .brand-name,
.app[data-sidebar="collapsed"] .brand-tag,
.app[data-sidebar="collapsed"] .nav-group-title,
.app[data-sidebar="collapsed"] .nav-item span:not(.ico):not(.badge),
.app[data-sidebar="collapsed"] .nav-item .chev,
.app[data-sidebar="collapsed"] .nav-sub {
  display: none !important;
}
.app[data-sidebar="collapsed"] .nav-item {
  justify-content: center;
  padding: 0;
  width: 36px;
  margin: 1px auto;
}
.app[data-sidebar="collapsed"] .sidebar-brand {
  justify-content: center;
  padding: 0;
}

.sidebar-foot {
  padding: 10px;
  border-top: 1px solid var(--c-border-soft);
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}
.user-avatar {
  width: 28px;
  height: 28px;
  border-radius: var(--r-pill);
  background: linear-gradient(135deg, #5B8DEF, #3B5BDB);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: var(--fs-12);
  font-weight: var(--fw-semibold);
  flex-shrink: 0;
}
.user-meta {
  flex: 1;
  overflow: hidden;
}
.user-name {
  font-size: var(--fs-13);
  color: var(--c-ink);
  font-weight: var(--fw-medium);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.user-org {
  font-size: var(--fs-11);
  color: var(--c-text-3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.app[data-sidebar="collapsed"] .user-meta { display: none; }
.app[data-sidebar="collapsed"] .sidebar-foot { justify-content: center; }

/* ---------- Topbar ---------- */
.topbar {
  grid-column: 2;
  grid-row: 1;
  background: var(--c-bg);
  border-bottom: 1px solid var(--c-border-soft);
  display: flex;
  align-items: center;
  padding: 0 var(--page-pad);
  gap: 16px;
  z-index: 10;
}
.topbar-toggle {
  width: 30px;
  height: 30px;
  border-radius: var(--r-md);
  display: grid;
  place-items: center;
  color: var(--c-text-2);
}
.topbar-toggle:hover { background: var(--c-bg-hover); color: var(--c-text); }

.crumbs {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: var(--fs-13);
  color: var(--c-text-2);
}
.crumbs .sep { color: var(--c-text-4); }
.crumbs .curr {
  color: var(--c-ink);
  font-weight: var(--fw-medium);
}

.topbar-search {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--c-bg-panel);
  border-radius: var(--r-md);
  padding: 5px 10px;
  width: 280px;
  color: var(--c-text-3);
  font-size: var(--fs-13);
}
.topbar-search input {
  border: none;
  background: transparent;
  outline: none;
  width: 100%;
  font-size: var(--fs-13);
  color: var(--c-text);
}
.topbar-search input::placeholder { color: var(--c-text-3); }
.topbar-search .kbd {
  font-family: var(--font-num);
  font-size: var(--fs-11);
  background: #fff;
  border: 1px solid var(--c-border);
  border-radius: var(--r-xs);
  padding: 1px 5px;
  color: var(--c-text-3);
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}
.top-action {
  width: 32px;
  height: 32px;
  border-radius: var(--r-md);
  display: grid;
  place-items: center;
  color: var(--c-text-2);
  position: relative;
}
.top-action:hover { background: var(--c-bg-hover); color: var(--c-text); }
.top-action .dot {
  position: absolute;
  top: 7px;
  right: 7px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--c-danger);
  border: 1.5px solid var(--c-bg);
}

/* ---------- Main page area ---------- */
.main {
  grid-column: 2;
  grid-row: 2;
  overflow: auto;
  background: var(--c-bg-soft);
}
.page {
  padding: var(--page-pad);
  max-width: 100%;
}
.page-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  margin-bottom: 16px;
  gap: 16px;
  flex-wrap: wrap;
}
.page-title {
  font-size: var(--fs-20);
  font-weight: var(--fw-semibold);
  color: var(--c-ink);
  line-height: 1.2;
  margin: 0 0 4px;
}
.page-sub {
  font-size: var(--fs-13);
  color: var(--c-text-2);
}
.page-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

/* ==========================================================================
   Reusable primitives
   ========================================================================== */

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 32px;
  padding: 0 14px;
  border-radius: var(--r-md);
  font-size: var(--fs-13);
  font-weight: var(--fw-medium);
  color: var(--c-text);
  background: #fff;
  border: 1px solid var(--c-border);
  transition: all 0.12s ease;
  white-space: nowrap;
}
.btn:hover { background: var(--c-bg-hover); border-color: var(--c-border-strong); }
.btn.primary {
  background: var(--c-primary);
  color: #fff;
  border-color: var(--c-primary);
}
.btn.primary:hover {
  background: var(--c-primary-hover);
  border-color: var(--c-primary-hover);
}
.btn.ghost { background: transparent; border-color: transparent; color: var(--c-text-2); }
.btn.ghost:hover { background: var(--c-bg-hover); color: var(--c-text); }
.btn.danger { color: var(--c-danger); border-color: var(--c-border); }
.btn.danger:hover { background: var(--c-danger-soft); border-color: var(--c-danger); }
.btn.sm { height: 26px; padding: 0 10px; font-size: var(--fs-12); }
.btn.lg { height: 38px; padding: 0 18px; font-size: var(--fs-14); }
.btn.icon { width: 32px; padding: 0; }
.btn.icon.sm { width: 26px; }

/* ---------- Cards ---------- */
.card {
  background: #fff;
  border: 1px solid var(--c-border-soft);
  border-radius: var(--r-lg);
  overflow: hidden;
}
.card-head {
  padding: 14px 16px;
  border-bottom: 1px solid var(--c-border-soft);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.card-title {
  font-size: var(--fs-14);
  font-weight: var(--fw-semibold);
  color: var(--c-ink);
}
.card-body { padding: 16px; }

/* ---------- Tables ---------- */
.table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: var(--fs-13);
}
.table thead th {
  text-align: left;
  font-weight: var(--fw-medium);
  color: var(--c-text-2);
  background: var(--c-bg-soft);
  padding: 10px var(--cell-px);
  border-bottom: 1px solid var(--c-border-soft);
  border-top: 1px solid var(--c-border-soft);
  font-size: var(--fs-12);
  position: sticky;
  top: 0;
  z-index: 1;
  white-space: nowrap;
}
.table tbody td {
  padding: var(--cell-py) var(--cell-px);
  border-bottom: 1px solid var(--c-border-soft);
  color: var(--c-text);
  vertical-align: middle;
}
.table tbody tr:hover { background: var(--c-bg-soft); }
.table tbody tr:last-child td { border-bottom: none; }
.table .num-col { text-align: right; font-family: var(--font-num); font-variant-numeric: tabular-nums; }
.table .right { text-align: right; }
.table .center { text-align: center; }
.table th.sticky-r, .table td.sticky-r {
  position: sticky;
  right: 0;
  background: #fff;
}
.table tbody tr:hover td.sticky-r { background: var(--c-bg-soft); }

/* ---------- Badges / tags ---------- */
.tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 1px 7px;
  font-size: var(--fs-11);
  border-radius: var(--r-sm);
  background: var(--c-bg-panel);
  color: var(--c-text-2);
  line-height: 1.5;
  font-weight: var(--fw-medium);
}
.tag.dot::before {
  content: "";
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: currentColor;
}
.tag.success { background: var(--c-success-soft); color: var(--c-success); }
.tag.warning { background: var(--c-warning-soft); color: var(--c-warning); }
.tag.danger { background: var(--c-danger-soft); color: var(--c-danger); }
.tag.info { background: var(--c-info-soft); color: var(--c-info); }
.tag.primary { background: var(--c-primary-soft); color: var(--c-primary-text); }
.tag.purple { background: var(--c-purple-soft); color: var(--c-purple); }
.tag.pink { background: var(--c-pink-soft); color: var(--c-pink); }
.tag.outline { background: transparent; border: 1px solid var(--c-border); }

/* ---------- Inputs ---------- */
.input, .select {
  height: 32px;
  padding: 0 10px;
  border: 1px solid var(--c-border);
  border-radius: var(--r-md);
  background: #fff;
  font-size: var(--fs-13);
  color: var(--c-text);
  outline: none;
  width: 100%;
  transition: border-color 0.12s ease;
}
.input:hover, .select:hover { border-color: var(--c-border-strong); }
.input:focus, .select:focus {
  border-color: var(--c-primary);
  box-shadow: 0 0 0 3px rgba(59, 91, 219, 0.10);
}
.input::placeholder { color: var(--c-text-4); }
.input-wrap {
  position: relative;
  display: flex;
  align-items: center;
}
.input-wrap .input { padding-left: 30px; }
.input-wrap .ico {
  position: absolute;
  left: 10px;
  color: var(--c-text-3);
  pointer-events: none;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.field label {
  font-size: var(--fs-12);
  color: var(--c-text-2);
}
.field label .req { color: var(--c-danger); margin-right: 2px; }

/* ---------- Filter bar ---------- */
.filter-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  background: #fff;
  border-bottom: 1px solid var(--c-border-soft);
  flex-wrap: wrap;
}
.filter-bar .input-wrap { width: 220px; }
.filter-bar .select { width: auto; min-width: 110px; padding-right: 24px; }

/* ---------- Toolbar above tables ---------- */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--c-border-soft);
  gap: 12px;
}
.toolbar .left, .toolbar .right {
  display: flex;
  gap: 8px;
  align-items: center;
}

/* ---------- Stat cards ---------- */
.stat {
  background: #fff;
  border: 1px solid var(--c-border-soft);
  border-radius: var(--r-lg);
  padding: 16px 18px;
  position: relative;
  overflow: hidden;
}
.stat-label {
  font-size: var(--fs-12);
  color: var(--c-text-2);
  margin-bottom: 6px;
  display: flex;
  align-items: center;
  gap: 4px;
}
.stat-value {
  font-size: var(--fs-24);
  font-weight: var(--fw-semibold);
  color: var(--c-ink);
  font-family: var(--font-num);
  font-variant-numeric: tabular-nums;
  line-height: 1.2;
  letter-spacing: -0.3px;
}
.stat-delta {
  font-size: var(--fs-12);
  margin-top: 6px;
  display: flex;
  align-items: center;
  gap: 3px;
  color: var(--c-text-2);
}
.stat-delta.up { color: var(--c-success); }
.stat-delta.down { color: var(--c-danger); }

/* ---------- Empty state ---------- */
.empty {
  padding: 60px 20px;
  text-align: center;
  color: var(--c-text-3);
  font-size: var(--fs-13);
}

/* ---------- Drawer ---------- */
.drawer-mask {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.32);
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease;
}
.drawer-mask.show { opacity: 1; pointer-events: auto; }
.drawer {
  position: fixed;
  top: 0;
  right: 0;
  height: 100vh;
  width: 560px;
  max-width: 92vw;
  background: #fff;
  z-index: 101;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.22s cubic-bezier(.2, .8, .2, 1);
  box-shadow: -8px 0 32px rgba(15, 23, 42, 0.12);
}
.drawer.show { transform: translateX(0); }
.drawer.wide { width: 720px; }
.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid var(--c-border-soft);
}
.drawer-title { font-size: var(--fs-15); font-weight: var(--fw-semibold); color: var(--c-ink); }
.drawer-body { flex: 1; overflow: auto; padding: 20px; }
.drawer-foot {
  padding: 14px 20px;
  border-top: 1px solid var(--c-border-soft);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  background: var(--c-bg-soft);
}

/* ---------- Module color dots ---------- */
.mdot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex-shrink: 0; }
.mdot.sales { background: var(--m-sales); }
.mdot.purchase { background: var(--m-purchase); }
.mdot.warehouse { background: var(--m-warehouse); }
.mdot.finance { background: var(--m-finance); }
.mdot.data { background: var(--m-data); }

/* ---------- Tweaks panel (lives inside .tweaks) ---------- */
.tweaks-fab {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--c-ink);
  color: #fff;
  display: grid;
  place-items: center;
  box-shadow: var(--sh-lg);
  z-index: 50;
  cursor: pointer;
}
.tweaks-panel {
  position: fixed;
  bottom: 72px;
  right: 20px;
  width: 280px;
  background: #fff;
  border: 1px solid var(--c-border);
  border-radius: var(--r-xl);
  box-shadow: var(--sh-lg);
  padding: 14px 16px;
  z-index: 51;
  display: none;
}
.tweaks-panel.show { display: block; }
.tweaks-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.tweaks-title { font-size: var(--fs-13); font-weight: var(--fw-semibold); color: var(--c-ink); }
.tweaks-section { margin-bottom: 14px; }
.tweaks-section:last-child { margin-bottom: 0; }
.tweaks-label { font-size: var(--fs-11); color: var(--c-text-3); margin-bottom: 6px; text-transform: uppercase; letter-spacing: 0.5px; }
.seg {
  display: flex;
  background: var(--c-bg-panel);
  border-radius: var(--r-md);
  padding: 2px;
  gap: 2px;
}
.seg-item {
  flex: 1;
  padding: 4px 8px;
  font-size: var(--fs-12);
  text-align: center;
  border-radius: calc(var(--r-md) - 2px);
  color: var(--c-text-2);
  cursor: pointer;
}
.seg-item.on {
  background: #fff;
  color: var(--c-ink);
  font-weight: var(--fw-medium);
  box-shadow: var(--sh-xs);
}
